use of com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable in project convertigo by convertigo.
the class TransactionWithVariables method setDynamicVariable.
private void setDynamicVariable(String variableName, Object variableValue, Boolean multi, String variableMethod) {
checkSubLoaded();
needRestoreVariables = true;
if (originalVariables == null) {
originalVariables = new ArrayList<RequestableVariable>(vVariables);
}
RequestableVariable variable = (RequestableVariable) getVariable(variableName);
try {
// variable definition does not exist, creates it
if (variable == null) {
if ("POST".equals(variableMethod) || "GET".equals(variableMethod)) {
variable = (multi ? new RequestableHttpMultiValuedVariable() : new RequestableHttpVariable());
((RequestableHttpVariable) variable).setHttpMethod(variableMethod);
((RequestableHttpVariable) variable).setHttpName(variableMethod);
} else {
variable = new RequestableVariable();
}
variable.bNew = true;
variable.hasChanged = true;
}
// override existing variable definition with dynamic one
variable.setName(variableName);
variable.setDescription(variableName);
variable.setValueOrNull(variableValue);
variable.setWsdl(Boolean.FALSE);
variable.setPersonalizable(Boolean.FALSE);
variable.setCachedKey(Boolean.TRUE);
if (variable.bNew)
addVariable(variable);
} catch (EngineException e) {
}
}
use of com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable 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);
}
}
use of com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable 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);
}
}
use of com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable 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();
}
}
Aggregations