Search in sources :

Example 16 with RequestableHttpVariable

use of com.twinsoft.convertigo.beans.variables.RequestableHttpVariable in project convertigo by convertigo.

the class DownloadHttpTransaction method parseInputDocument.

@Override
public void parseInputDocument(Context context) throws EngineException {
    super.parseInputDocument(context);
    currentFolder = folder;
    currentFilename = filename;
    for (RequestableVariable v : getAllVariables()) {
        if (v.getName().equals(DynamicHttpVariable.__download_folder.name())) {
            RequestableHttpVariable var = (RequestableHttpVariable) v;
            Object o = var.getDefaultValue();
            if (o != null && o instanceof String) {
                currentFolder = (String) o;
            }
        } else if (v.getName().equals(DynamicHttpVariable.__download_filename.name())) {
            RequestableHttpVariable var = (RequestableHttpVariable) v;
            Object o = var.getDefaultValue();
            if (o != null && o instanceof String) {
                currentFilename = (String) o;
            }
        }
    }
    NodeList nl = context.inputDocument.getElementsByTagName("download-folder");
    if (nl.getLength() == 1) {
        Element elt = (Element) nl.item(0);
        String val = elt.getAttribute("value");
        if (StringUtils.isNotBlank(val)) {
            if (getAccessibility() == Accessibility.Private) {
                currentFolder = val;
            } else {
                Engine.logBeans.error("(DownloadHttpTransaction) The transaction isn't Private, the download-folder cannot be override.");
            }
        }
    }
    nl = context.inputDocument.getElementsByTagName("download-filename");
    int len = nl.getLength();
    if (len > 0) {
        Element elt = (Element) nl.item(0);
        String val = elt.getAttribute("value");
        if (StringUtils.isNotBlank(val)) {
            if (getAccessibility() == Accessibility.Private) {
                currentFilename = val;
            } else {
                Engine.logBeans.error("(DownloadHttpTransaction) The transaction isn't Private, the download-folder cannot be override.");
            }
        }
    }
}
Also used : RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) NodeList(org.w3c.dom.NodeList) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable)

Example 17 with RequestableHttpVariable

use of com.twinsoft.convertigo.beans.variables.RequestableHttpVariable 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) {
            AbstractHttpTransaction defTransaction = 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);
                        if (defTransaction == null) {
                            defTransaction = transaction;
                            httpConnector.setDefaultTransaction(defTransaction);
                        }
                    }
                }
            }
        }
        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)

Example 18 with RequestableHttpVariable

use of com.twinsoft.convertigo.beans.variables.RequestableHttpVariable in project convertigo by convertigo.

the class SwaggerUtils method createRestConnector.

public static HttpConnector createRestConnector(Swagger swagger) throws Exception {
    try {
        HttpConnector httpConnector = new HttpConnector();
        httpConnector.bNew = true;
        Info info = swagger.getInfo();
        String title = info != null ? info.getTitle() : "";
        title = title == null || title.isEmpty() ? "RestConnector" : title;
        httpConnector.setName(StringUtils.normalize(title));
        boolean isHttps = false;
        for (Scheme scheme : swagger.getSchemes()) {
            if (scheme.equals(Scheme.HTTPS)) {
                isHttps = true;
            }
        }
        String host = swagger.getHost();
        int index = host.indexOf(":");
        String server = index == -1 ? host : host.substring(0, index);
        int port = index == -1 ? 0 : Integer.parseInt(host.substring(index + 1), 10);
        httpConnector.setHttps(isHttps);
        httpConnector.setServer(server);
        httpConnector.setPort(port <= 0 ? (isHttps ? 443 : 80) : port);
        String basePath = swagger.getBasePath();
        httpConnector.setBaseDir(basePath);
        Map<String, SecuritySchemeDefinition> securityMap = swagger.getSecurityDefinitions();
        if (securityMap != null && securityMap.size() > 0) {
            for (String securityName : securityMap.keySet()) {
                SecuritySchemeDefinition securityScheme = securityMap.get(securityName);
                if (securityScheme != null) {
                    boolean isBasicScheme = securityScheme.getType().toLowerCase().equals("basic");
                    if (isBasicScheme) {
                        httpConnector.setAuthenticationType(AuthenticationMode.Basic);
                        break;
                    }
                }
            }
        }
        List<String> _consumeList = swagger.getConsumes();
        List<String> _produceList = swagger.getProduces();
        // Map<String, Model> models = swagger.getDefinitions();
        Map<String, Path> paths = swagger.getPaths();
        for (String subDir : paths.keySet()) {
            Path path = paths.get(subDir);
            // Add transactions
            List<Operation> operations = path.getOperations();
            for (Operation operation : operations) {
                HttpMethodType httpMethodType = null;
                if (operation.equals(path.getGet())) {
                    httpMethodType = HttpMethodType.GET;
                } else if (operation.equals(path.getPost())) {
                    httpMethodType = HttpMethodType.POST;
                } else if (operation.equals(path.getPut())) {
                    httpMethodType = HttpMethodType.PUT;
                } else if (operation.equals(path.getDelete())) {
                    httpMethodType = HttpMethodType.DELETE;
                } else if (operation.equals(path.getHead())) {
                    httpMethodType = HttpMethodType.HEAD;
                } else if (operation.equals(path.getOptions())) {
                    httpMethodType = HttpMethodType.OPTIONS;
                } else {
                    httpMethodType = null;
                }
                if (httpMethodType != null) {
                    List<String> consumeList = operation.getConsumes();
                    consumeList = consumeList == null || consumeList.isEmpty() ? _consumeList : consumeList;
                    List<String> produceList = operation.getProduces();
                    produceList = produceList == null || produceList.isEmpty() ? _produceList : produceList;
                    String operationId = operation.getOperationId();
                    String description = operation.getDescription();
                    String summary = operation.getSummary();
                    String name = StringUtils.normalize(subDir + ":" + httpMethodType.toString());
                    if (name.isEmpty()) {
                        name = StringUtils.normalize(operationId);
                        if (name.isEmpty()) {
                            name = StringUtils.normalize(summary);
                            if (name.isEmpty()) {
                                name = "operation";
                            }
                        }
                    }
                    String comment = summary;
                    if (comment == null)
                        comment = "";
                    if (comment.isEmpty()) {
                        comment = description;
                    }
                    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 (produceList != null) {
                        if (produceList.contains(h_ContentType)) {
                            h_Accept = h_ContentType;
                        } else {
                            if (produceList.contains(MimeType.Json.value())) {
                                h_Accept = MimeType.Json.value();
                            } else if (produceList.contains(MimeType.Xml.value())) {
                                h_Accept = MimeType.Xml.value();
                            }
                        }
                        if (consumeList == null && h_Accept != null) {
                            h_ContentType = h_Accept;
                        }
                    }
                    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;
                    List<io.swagger.models.parameters.Parameter> parameters = operation.getParameters();
                    for (io.swagger.models.parameters.Parameter parameter : parameters) {
                        // String p_access = parameter.getAccess();
                        String p_description = parameter.getDescription();
                        // String p_in = parameter.getIn();
                        String p_name = parameter.getName();
                        // String p_pattern = parameter.getPattern();
                        boolean p_required = parameter.getRequired();
                        // Map<String,Object> p_extensions = parameter.getVendorExtensions();
                        boolean isMultiValued = false;
                        if (parameter instanceof SerializableParameter) {
                            SerializableParameter serializable = (SerializableParameter) parameter;
                            if (serializable.getType().equalsIgnoreCase("array")) {
                                if (serializable.getCollectionFormat().equalsIgnoreCase("multi")) {
                                    isMultiValued = true;
                                }
                            }
                        }
                        RequestableHttpVariable httpVariable = isMultiValued ? new RequestableHttpMultiValuedVariable() : new RequestableHttpVariable();
                        httpVariable.bNew = true;
                        httpVariable.setName(p_name);
                        httpVariable.setHttpName(p_name);
                        httpVariable.setRequired(p_required);
                        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 if (parameter instanceof FormParameter || parameter instanceof BodyParameter) {
                            httpVariable.setHttpMethod(HttpMethodType.POST.name());
                            if (parameter instanceof FormParameter) {
                                FormParameter formParameter = (FormParameter) parameter;
                                if (formParameter.getType().equalsIgnoreCase("file")) {
                                    httpVariable.setDoFileUploadMode(DoFileUploadMode.multipartFormData);
                                }
                            } else if (parameter instanceof BodyParameter) {
                                hasBodyVariable = true;
                                // overrides variable's name for internal use
                                httpVariable.setName(com.twinsoft.convertigo.engine.enums.Parameter.HttpBody.getName());
                                // add internal __contentType variable
                                /*RequestableHttpVariable ct = new RequestableHttpVariable();
									ct.setName(Parameter.HttpContentType.getName());
									ct.setHttpMethod(HttpMethodType.POST.name());
									ct.setValueOrNull(null);
									ct.bNew = true;
									transaction.addVariable(ct);*/
                                BodyParameter bodyParameter = (BodyParameter) parameter;
                                Model model = bodyParameter.getSchema();
                                if (model != null) {
                                }
                            }
                        } else {
                            httpVariable.setHttpMethod("");
                        }
                        Object defaultValue = null;
                        if (parameter instanceof AbstractSerializableParameter<?>) {
                            defaultValue = ((AbstractSerializableParameter<?>) parameter).getDefaultValue();
                        }
                        if (defaultValue == null && parameter instanceof SerializableParameter) {
                            SerializableParameter serializable = (SerializableParameter) parameter;
                            if (serializable.getType().equalsIgnoreCase("array")) {
                                Property items = serializable.getItems();
                                try {
                                    Class<?> c = items.getClass();
                                    defaultValue = c.getMethod("getDefault").invoke(items);
                                } catch (Exception e) {
                                }
                            }
                        }
                        if (defaultValue == null && p_required) {
                            defaultValue = "";
                        }
                        httpVariable.setValueOrNull(defaultValue);
                        if (p_description != null) {
                            httpVariable.setDescription(p_description);
                            httpVariable.setComment(p_description);
                        }
                        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.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 : SerializableParameter(io.swagger.models.parameters.SerializableParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) HttpTransaction(com.twinsoft.convertigo.beans.transactions.HttpTransaction) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) JsonHttpTransaction(com.twinsoft.convertigo.beans.transactions.JsonHttpTransaction) HttpConnector(com.twinsoft.convertigo.beans.connectors.HttpConnector) RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) QueryParameter(io.swagger.models.parameters.QueryParameter) Scheme(io.swagger.models.Scheme) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) HttpMethodType(com.twinsoft.convertigo.engine.enums.HttpMethodType) Operation(io.swagger.models.Operation) AbstractRestOperation(com.twinsoft.convertigo.beans.rest.AbstractRestOperation) UrlMappingOperation(com.twinsoft.convertigo.beans.core.UrlMappingOperation) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) FormParameter(io.swagger.models.parameters.FormParameter) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) StringProperty(io.swagger.models.properties.StringProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) Path(io.swagger.models.Path) XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) Parameter(io.swagger.models.parameters.Parameter) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Info(io.swagger.models.Info) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RequestableHttpMultiValuedVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) IMappingRefModel(com.twinsoft.convertigo.beans.core.IMappingRefModel) SerializableParameter(io.swagger.models.parameters.SerializableParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) UrlMappingParameter(com.twinsoft.convertigo.beans.core.UrlMappingParameter) JsonHttpTransaction(com.twinsoft.convertigo.beans.transactions.JsonHttpTransaction) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 19 with RequestableHttpVariable

use of com.twinsoft.convertigo.beans.variables.RequestableHttpVariable in project convertigo by convertigo.

the class WsReference method extractSoapVariables.

private static void extractSoapVariables(XmlSchema xmlSchema, List<RequestableHttpVariable> variables, Node node, String longName, boolean isMulti, QName variableType) throws EngineException {
    if (node == null)
        return;
    int type = node.getNodeType();
    if (type == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        if (element != null) {
            String elementName = element.getLocalName();
            if (longName != null)
                elementName = longName + "_" + elementName;
            if (!element.getAttribute("soapenc:arrayType").equals("") && !element.hasChildNodes()) {
                String avalue = element.getAttribute("soapenc:arrayType");
                element.setAttribute("soapenc:arrayType", avalue.replaceAll("\\[\\]", "[1]"));
                Element child = element.getOwnerDocument().createElement("item");
                String atype = avalue.replaceAll("\\[\\]", "");
                child.setAttribute("xsi:type", atype);
                if (atype.startsWith("xsd:")) {
                    String variableName = elementName + "_item";
                    child.appendChild(element.getOwnerDocument().createTextNode("$(" + variableName.toUpperCase() + ")"));
                    RequestableHttpVariable httpVariable = createHttpVariable(true, variableName, new QName(Constants.URI_2001_SCHEMA_XSD, atype.split(":")[1]));
                    variables.add(httpVariable);
                }
                element.appendChild(child);
            }
            // extract from attributes
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node child = map.item(i);
                if (child.getNodeName().equals("soapenc:arrayType"))
                    continue;
                if (child.getNodeName().equals("xsi:type"))
                    continue;
                if (child.getNodeName().equals("soapenv:encodingStyle"))
                    continue;
                String variableName = getVariableName(variables, elementName + "_" + child.getLocalName());
                child.setNodeValue("$(" + variableName.toUpperCase() + ")");
                RequestableHttpVariable httpVariable = createHttpVariable(false, variableName, Constants.XSD_STRING);
                variables.add(httpVariable);
            }
            // extract from children nodes
            boolean multi = false;
            QName qname = Constants.XSD_STRING;
            NodeList children = element.getChildNodes();
            if (children.getLength() > 0) {
                Node child = element.getFirstChild();
                while (child != null) {
                    if (child.getNodeType() == Node.COMMENT_NODE) {
                        String value = child.getNodeValue();
                        if (value.startsWith("type:")) {
                            String schemaType = child.getNodeValue().substring("type:".length()).trim();
                            qname = getVariableSchemaType(xmlSchema, schemaType);
                        }
                        if (value.indexOf("repetitions:") != -1) {
                            multi = true;
                        }
                    } else if (child.getNodeType() == Node.TEXT_NODE) {
                        String value = child.getNodeValue().trim();
                        if (value.equals("?") || !value.equals("")) {
                            String variableName = getVariableName(variables, elementName);
                            child.setNodeValue("$(" + variableName.toUpperCase() + ")");
                            RequestableHttpVariable httpVariable = createHttpVariable(isMulti, variableName, variableType);
                            variables.add(httpVariable);
                        }
                    } else if (child.getNodeType() == Node.ELEMENT_NODE) {
                        extractSoapVariables(xmlSchema, variables, child, elementName, multi, qname);
                        multi = false;
                        qname = Constants.XSD_STRING;
                    }
                    child = child.getNextSibling();
                }
            }
        }
    }
}
Also used : RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) NamedNodeMap(org.w3c.dom.NamedNodeMap) XmlQName(com.twinsoft.convertigo.beans.common.XmlQName) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList)

Example 20 with RequestableHttpVariable

use of com.twinsoft.convertigo.beans.variables.RequestableHttpVariable in project convertigo by convertigo.

the class StatementAddVariableToTransactionAction method run.

public void run() {
    Display display = Display.getDefault();
    Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
    Shell shell = getParentShell();
    shell.setCursor(waitCursor);
    try {
        ProjectExplorerView explorerView = getProjectExplorerView();
        if (explorerView != null) {
            TreeObject treeObject = explorerView.getFirstSelectedTreeObject();
            Object databaseObject = treeObject.getObject();
            if ((databaseObject != null) && (databaseObject instanceof HTTPStatement)) {
                HTTPStatement httpStatement = (HTTPStatement) databaseObject;
                HtmlTransaction htmlTransaction = (HtmlTransaction) httpStatement.getParentTransaction();
                List<String> variables = new ArrayList<String>();
                int i, size;
                /*size = htmlTransaction.getVariablesDefinitionSize();
    				for (i = 0 ; i < size ; i++) {
    					variables.add(htmlTransaction.getVariableDefinitionHttpName(i));
    				}*/
                size = htmlTransaction.numberOfVariables();
                for (i = 0; i < size; i++) {
                    RequestableHttpVariable httpVariable = (RequestableHttpVariable) htmlTransaction.getVariable(i);
                    variables.add(httpVariable.getHttpName());
                }
                String variableName, variableDescription, variableMethod;
                Boolean variableType, variableRequired;
                Object variableValue;
                int variableVisibility;
                size = httpStatement.numberOfVariables();
                for (i = 0; i < size; i++) {
                    HttpStatementVariable httpStatementVariable = (HttpStatementVariable) httpStatement.getVariable(i);
                    if (httpStatementVariable != null) {
                        variableName = httpStatementVariable.getName();
                        variableDescription = httpStatementVariable.getDescription();
                        variableRequired = httpStatementVariable.isRequired();
                        variableValue = httpStatementVariable.getValueOrNull();
                        variableType = httpStatementVariable.isMultiValued();
                        variableMethod = httpStatementVariable.getHttpMethod();
                        variableVisibility = httpStatementVariable.getVisibility();
                        if (!variables.contains(variableName)) {
                            RequestableHttpVariable requestableVariable = (variableType ? new RequestableHttpMultiValuedVariable() : new RequestableHttpVariable());
                            requestableVariable.setName(variableName);
                            requestableVariable.setDescription(variableDescription);
                            requestableVariable.setRequired(variableRequired);
                            requestableVariable.setValueOrNull(variableValue);
                            requestableVariable.setWsdl(Boolean.TRUE);
                            requestableVariable.setPersonalizable(Boolean.FALSE);
                            requestableVariable.setCachedKey(Boolean.TRUE);
                            requestableVariable.setHttpMethod(variableMethod);
                            requestableVariable.setHttpName("");
                            requestableVariable.setVisibility(variableVisibility);
                            requestableVariable.bNew = true;
                            requestableVariable.hasChanged = true;
                            htmlTransaction.add(requestableVariable);
                        }
                    }
                }
                htmlTransaction.hasChanged = true;
                explorerView.reloadDatabaseObject(htmlTransaction);
            }
        }
    } catch (Throwable e) {
        ConvertigoPlugin.logException(e, "Unable to add HTTP variables to transaction!");
    } finally {
        shell.setCursor(null);
        waitCursor.dispose();
    }
}
Also used : RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) HtmlTransaction(com.twinsoft.convertigo.beans.transactions.HtmlTransaction) ArrayList(java.util.ArrayList) Cursor(org.eclipse.swt.graphics.Cursor) HTTPStatement(com.twinsoft.convertigo.beans.statements.HTTPStatement) HttpStatementVariable(com.twinsoft.convertigo.beans.variables.HttpStatementVariable) Shell(org.eclipse.swt.widgets.Shell) RequestableHttpMultiValuedVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) Display(org.eclipse.swt.widgets.Display)

Aggregations

RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)21 RequestableHttpMultiValuedVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable)9 RequestableVariable (com.twinsoft.convertigo.beans.variables.RequestableVariable)9 EngineException (com.twinsoft.convertigo.engine.EngineException)9 AbstractHttpTransaction (com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)7 XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)6 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)6 IOException (java.io.IOException)6 Element (org.w3c.dom.Element)5 HTTPStatement (com.twinsoft.convertigo.beans.statements.HTTPStatement)4 XmlHttpTransaction (com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction)4 HttpStatementVariable (com.twinsoft.convertigo.beans.variables.HttpStatementVariable)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 XmlQName (com.twinsoft.convertigo.beans.common.XmlQName)3 HttpConnector (com.twinsoft.convertigo.beans.connectors.HttpConnector)3 TestCase (com.twinsoft.convertigo.beans.core.TestCase)3 Variable (com.twinsoft.convertigo.beans.core.Variable)3 HttpStatementMultiValuedVariable (com.twinsoft.convertigo.beans.variables.HttpStatementMultiValuedVariable)3 RequestableMultiValuedVariable (com.twinsoft.convertigo.beans.variables.RequestableMultiValuedVariable)3 HttpMethodType (com.twinsoft.convertigo.engine.enums.HttpMethodType)3