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);
}
}
Aggregations