Search in sources :

Example 6 with HttpMethodType

use of com.twinsoft.convertigo.engine.enums.HttpMethodType in project convertigo by convertigo.

the class OpenApiUtils method createRestConnector.

public static HttpConnector createRestConnector(OpenAPI openApi) throws Exception {
    try {
        HttpConnector httpConnector = new HttpConnector();
        httpConnector.bNew = true;
        Info info = openApi.getInfo();
        String title = info != null ? info.getTitle() : "";
        title = title == null || title.isEmpty() ? "RestConnector" : title;
        String description = info != null ? info.getDescription() : "";
        description = description == null || description.isEmpty() ? "" : description;
        httpConnector.setName(StringUtils.normalize(title));
        httpConnector.setComment(description);
        String httpUrl = "";
        List<Server> servers = openApi.getServers();
        if (servers.size() > 0) {
            httpUrl = servers.get(0).getUrl();
        }
        httpUrl = httpUrl.isEmpty() ? getConvertigoServeurUrl() : httpUrl;
        UrlFields urlFields = UrlParser.parse(httpUrl);
        if (urlFields != null) {
            String scheme = urlFields.getScheme();
            String host = urlFields.getHost();
            String port = urlFields.getPort();
            String basePath = urlFields.getPath();
            boolean isHttps = "https".equals(scheme);
            httpConnector.setHttps(isHttps);
            httpConnector.setServer(host);
            httpConnector.setPort(port == null ? (isHttps ? 443 : 80) : Integer.valueOf(port));
            httpConnector.setBaseDir(basePath);
        }
        httpConnector.setBaseUrl(httpUrl);
        List<SecurityRequirement> securityRequirements = openApi.getSecurity();
        if (securityRequirements != null && securityRequirements.size() > 0) {
            Map<String, SecurityScheme> securitySchemes = openApi.getComponents().getSecuritySchemes();
            for (SecurityRequirement sr : securityRequirements) {
                for (String s_name : sr.keySet()) {
                    SecurityScheme securityScheme = securitySchemes.get(s_name);
                    if (securityScheme != null) {
                        String scheme = securityScheme.getScheme() == null ? "" : securityScheme.getScheme();
                        boolean isBasicScheme = scheme.toLowerCase().equals("basic");
                        boolean isHttpType = securityScheme.getType().equals(SecurityScheme.Type.HTTP);
                        if (isHttpType && isBasicScheme) {
                            httpConnector.setAuthenticationType(AuthenticationMode.Basic);
                            break;
                        }
                    }
                }
            }
        }
        Paths paths = openApi.getPaths();
        if (paths != null) {
            for (Entry<String, PathItem> entry : paths.entrySet()) {
                HttpMethodType httpMethodType = null;
                Operation operation = null;
                String customHttpVerb = "";
                String subDir = entry.getKey();
                PathItem pathItem = entry.getValue();
                Map<HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
                for (HttpMethod httpMethod : operationMap.keySet()) {
                    operation = operationMap.get(httpMethod);
                    if (httpMethod.equals(HttpMethod.GET)) {
                        httpMethodType = HttpMethodType.GET;
                    } else if (httpMethod.equals(HttpMethod.POST)) {
                        httpMethodType = HttpMethodType.POST;
                    } else if (httpMethod.equals(HttpMethod.PUT)) {
                        httpMethodType = HttpMethodType.PUT;
                    } else if (httpMethod.equals(HttpMethod.PATCH)) {
                        httpMethodType = HttpMethodType.PUT;
                        customHttpVerb = "PATCH";
                    } else if (httpMethod.equals(HttpMethod.DELETE)) {
                        httpMethodType = HttpMethodType.DELETE;
                    } else if (httpMethod.equals(HttpMethod.HEAD)) {
                        httpMethodType = HttpMethodType.HEAD;
                    } else if (httpMethod.equals(HttpMethod.TRACE)) {
                        httpMethodType = HttpMethodType.TRACE;
                    } else if (httpMethod.equals(HttpMethod.OPTIONS)) {
                        httpMethodType = HttpMethodType.OPTIONS;
                    } else {
                        httpMethodType = null;
                    }
                    if (operation != null && httpMethodType != null) {
                        String operationId = operation.getOperationId();
                        String operationDesc = operation.getDescription();
                        String summary = operation.getSummary();
                        String name = StringUtils.normalize(subDir + ":" + (customHttpVerb.isEmpty() ? httpMethodType.toString() : customHttpVerb));
                        if (name.isEmpty()) {
                            name = StringUtils.normalize(operationId);
                            if (name.isEmpty()) {
                                name = StringUtils.normalize(summary);
                                if (name.isEmpty()) {
                                    name = "operation";
                                }
                            }
                        }
                        String comment = org.apache.commons.lang3.StringUtils.isNotBlank(summary) && !"null".equals(summary) ? summary : (org.apache.commons.lang3.StringUtils.isNotBlank(operationDesc) && !"null".equals(operationDesc) ? operationDesc : "");
                        XMLVector<XMLVector<String>> httpParameters = new XMLVector<XMLVector<String>>();
                        AbstractHttpTransaction transaction = new HttpTransaction();
                        String h_ContentType = MimeType.WwwForm.value();
                        /*if (consumeList != null) {
								if (consumeList.contains(MimeType.Json.value())) {
									h_ContentType = MimeType.Json.value();
								}
								else if (consumeList.contains(MimeType.Xml.value())) {
									h_ContentType = MimeType.Xml.value();
								}
								else {
									h_ContentType = consumeList.size() > 0 ? 
											consumeList.get(0) : MimeType.WwwForm.value();
								}
							}*/
                        String h_Accept = MimeType.Json.value();
                        if (h_Accept != null) {
                            XMLVector<String> xmlv = new XMLVector<String>();
                            xmlv.add("Accept");
                            xmlv.add(h_Accept);
                            httpParameters.add(xmlv);
                            if (h_Accept.equals(MimeType.Xml.value())) {
                                transaction = new XmlHttpTransaction();
                                ((XmlHttpTransaction) transaction).setXmlEncoding("UTF-8");
                            } else if (h_Accept.equals(MimeType.Json.value())) {
                                transaction = new JsonHttpTransaction();
                                ((JsonHttpTransaction) transaction).setIncludeDataType(false);
                            }
                        }
                        // Add variables
                        boolean hasBodyVariable = false;
                        RequestBody body = operation.getRequestBody();
                        if (body != null) {
                            Map<String, MediaType> medias = body.getContent();
                            for (String contentType : medias.keySet()) {
                                MediaType mediaType = medias.get(contentType);
                                Schema<?> mediaSchema = mediaType.getSchema();
                                List<String> requiredList = mediaSchema.getRequired();
                                if (contentType.equals("application/x-www-form-urlencoded")) {
                                    @SuppressWarnings("rawtypes") Map<String, Schema> properties = mediaSchema.getProperties();
                                    if (properties != null) {
                                        for (String p_name : properties.keySet()) {
                                            Schema<?> schema = properties.get(p_name);
                                            String p_description = schema.getDescription();
                                            boolean p_required = requiredList == null ? false : requiredList.contains(p_name);
                                            boolean isMultiValued = false;
                                            if (schema instanceof ArraySchema) {
                                                isMultiValued = true;
                                            }
                                            RequestableHttpVariable httpVariable = isMultiValued ? new RequestableHttpMultiValuedVariable() : new RequestableHttpVariable();
                                            httpVariable.bNew = true;
                                            httpVariable.setHttpMethod(HttpMethodType.POST.name());
                                            httpVariable.setName(p_name);
                                            httpVariable.setDescription(p_name);
                                            httpVariable.setHttpName(p_name);
                                            httpVariable.setRequired(p_required);
                                            httpVariable.setComment(p_description == null ? "" : p_description);
                                            if (schema instanceof FileSchema) {
                                                httpVariable.setDoFileUploadMode(DoFileUploadMode.multipartFormData);
                                            }
                                            Object defaultValue = schema.getDefault();
                                            if (defaultValue == null && p_required) {
                                                defaultValue = "";
                                            }
                                            httpVariable.setValueOrNull(defaultValue);
                                            transaction.addVariable(httpVariable);
                                        }
                                    }
                                } else if (!hasBodyVariable) {
                                    RequestableHttpVariable httpVariable = new RequestableHttpVariable();
                                    httpVariable.bNew = true;
                                    httpVariable.setHttpMethod(HttpMethodType.POST.name());
                                    httpVariable.setRequired(true);
                                    // overrides variable's name for internal use
                                    httpVariable.setName(com.twinsoft.convertigo.engine.enums.Parameter.HttpBody.getName());
                                    Object defaultValue = null;
                                    httpVariable.setValueOrNull(defaultValue);
                                    transaction.addVariable(httpVariable);
                                    h_ContentType = contentType;
                                    hasBodyVariable = true;
                                }
                            }
                        }
                        List<Parameter> parameters = operation.getParameters();
                        if (parameters != null) {
                            for (Parameter parameter : parameters) {
                                String p_name = parameter.getName();
                                String p_description = parameter.getDescription();
                                boolean p_required = parameter.getRequired();
                                boolean isMultiValued = false;
                                Schema<?> schema = parameter.getSchema();
                                if (schema instanceof ArraySchema) {
                                    isMultiValued = true;
                                }
                                RequestableHttpVariable httpVariable = isMultiValued ? new RequestableHttpMultiValuedVariable() : new RequestableHttpVariable();
                                httpVariable.bNew = true;
                                httpVariable.setName(p_name);
                                httpVariable.setDescription(p_name);
                                httpVariable.setHttpName(p_name);
                                httpVariable.setRequired(p_required);
                                httpVariable.setComment(p_description == null ? "" : p_description);
                                if (parameter instanceof QueryParameter || parameter instanceof PathParameter || parameter instanceof HeaderParameter) {
                                    httpVariable.setHttpMethod(HttpMethodType.GET.name());
                                    if (parameter instanceof HeaderParameter) {
                                        // overrides variable's name : will be treated as dynamic header
                                        httpVariable.setName(com.twinsoft.convertigo.engine.enums.Parameter.HttpHeader.getName() + p_name);
                                        // do not post on target server
                                        httpVariable.setHttpName("");
                                    }
                                    if (parameter instanceof PathParameter) {
                                        // do not post on target server
                                        httpVariable.setHttpName("");
                                    }
                                } else {
                                    httpVariable.setHttpMethod("");
                                }
                                Object defaultValue = schema.getDefault();
                                if (defaultValue == null && p_required) {
                                    defaultValue = "";
                                }
                                httpVariable.setValueOrNull(defaultValue);
                                transaction.addVariable(httpVariable);
                            }
                        }
                        // Set Content-Type
                        if (h_ContentType != null) {
                            XMLVector<String> xmlv = new XMLVector<String>();
                            xmlv.add(HeaderName.ContentType.value());
                            xmlv.add(hasBodyVariable ? h_ContentType : MimeType.WwwForm.value());
                            httpParameters.add(xmlv);
                        }
                        transaction.bNew = true;
                        transaction.setName(name);
                        transaction.setComment(comment);
                        transaction.setSubDir(subDir);
                        transaction.setHttpVerb(httpMethodType);
                        transaction.setCustomHttpVerb(customHttpVerb);
                        transaction.setHttpParameters(httpParameters);
                        transaction.setHttpInfo(true);
                        httpConnector.add(transaction);
                    }
                }
            }
        }
        return httpConnector;
    } catch (Throwable t) {
        Engine.logEngine.error("Unable to create connector", t);
        throw new Exception("Unable to create connector", t);
    }
}
Also used : XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) JsonHttpTransaction(com.twinsoft.convertigo.beans.transactions.JsonHttpTransaction) HttpTransaction(com.twinsoft.convertigo.beans.transactions.HttpTransaction) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) HttpConnector(com.twinsoft.convertigo.beans.connectors.HttpConnector) RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) Server(io.swagger.v3.oas.models.servers.Server) HttpMethodType(com.twinsoft.convertigo.engine.enums.HttpMethodType) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(io.swagger.v3.oas.models.media.Schema) UrlMappingOperation(com.twinsoft.convertigo.beans.core.UrlMappingOperation) Operation(io.swagger.v3.oas.models.Operation) AbstractRestOperation(com.twinsoft.convertigo.beans.rest.AbstractRestOperation) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) MediaType(io.swagger.v3.oas.models.media.MediaType) Paths(io.swagger.v3.oas.models.Paths) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) FileSchema(io.swagger.v3.oas.models.media.FileSchema) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) UrlFields(com.twinsoft.convertigo.engine.util.UrlParser.UrlFields) XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) Info(io.swagger.v3.oas.models.info.Info) JSONException(org.codehaus.jettison.json.JSONException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RequestableHttpMultiValuedVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) LinkParameter(io.swagger.v3.oas.models.links.LinkParameter) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) UrlMappingParameter(com.twinsoft.convertigo.beans.core.UrlMappingParameter) JsonHttpTransaction(com.twinsoft.convertigo.beans.transactions.JsonHttpTransaction) JSONObject(org.codehaus.jettison.json.JSONObject) HttpMethod(io.swagger.v3.oas.models.PathItem.HttpMethod) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Aggregations

HttpMethodType (com.twinsoft.convertigo.engine.enums.HttpMethodType)6 AbstractHttpTransaction (com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)4 RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)4 EngineException (com.twinsoft.convertigo.engine.EngineException)3 File (java.io.File)3 IOException (java.io.IOException)3 JSONException (org.codehaus.jettison.json.JSONException)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)2 HttpConnector (com.twinsoft.convertigo.beans.connectors.HttpConnector)2 Connector (com.twinsoft.convertigo.beans.core.Connector)2 Project (com.twinsoft.convertigo.beans.core.Project)2 UrlMappingOperation (com.twinsoft.convertigo.beans.core.UrlMappingOperation)2 UrlMappingParameter (com.twinsoft.convertigo.beans.core.UrlMappingParameter)2 AbstractRestOperation (com.twinsoft.convertigo.beans.rest.AbstractRestOperation)2 HtmlTransaction (com.twinsoft.convertigo.beans.transactions.HtmlTransaction)2 HttpTransaction (com.twinsoft.convertigo.beans.transactions.HttpTransaction)2 JsonHttpTransaction (com.twinsoft.convertigo.beans.transactions.JsonHttpTransaction)2 XmlHttpTransaction (com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction)2