use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.
the class OpenApiUtils method writeOpenApiToFile.
private static void writeOpenApiToFile(final String requestUrl, final UrlMapper urlMapper, final File yamlFile, boolean useExternalRef) throws Exception {
synchronized (lockObject) {
if (yamlFile.exists() && Engine.isEngineMode())
return;
Long t0 = System.currentTimeMillis();
Project project = urlMapper.getProject();
String projectName = project.getName();
String oasDirUrl = requestUrl.substring(0, requestUrl.indexOf("/" + servletMappingPath)) + "/projects/" + projectName + "/" + jsonSchemaDirectory + "/";
OpenAPI openAPI = parseCommon(requestUrl, project);
List<Tag> tags = new ArrayList<>();
Tag tag = new Tag();
tag.setName(project.getName());
tag.setDescription(project.getComment());
tags.add(tag);
openAPI.setTags(tags);
if (openAPI.getComponents() == null) {
openAPI.components(new Components());
}
// Security
Map<String, SecurityScheme> securitySchemes = openAPI.getComponents().getSecuritySchemes();
for (UrlAuthentication authentication : urlMapper.getAuthenticationList()) {
if (AuthenticationType.Basic.equals(authentication.getType())) {
if (securitySchemes == null || !securitySchemes.containsKey("basicAuth")) {
SecurityScheme securitySchemesItem = new SecurityScheme();
securitySchemesItem.setType(SecurityScheme.Type.HTTP);
securitySchemesItem.setScheme("basic");
openAPI.getComponents().addSecuritySchemes("basicAuth", securitySchemesItem);
SecurityRequirement securityRequirement = new SecurityRequirement();
securityRequirement.addList("basicAuth", new ArrayList<String>());
openAPI.addSecurityItem(securityRequirement);
}
}
}
List<String> refList = new ArrayList<String>();
List<String> opIdList = new ArrayList<String>();
// Paths
Paths paths = new Paths();
try {
for (UrlMapping urlMapping : urlMapper.getMappingList()) {
PathItem item = new PathItem();
for (UrlMappingOperation umo : urlMapping.getOperationList()) {
Operation operation = new Operation();
operation.setOperationId(getOperationId(opIdList, umo, false));
operation.setDescription(umo.getComment());
operation.setSummary(umo.getComment());
// Tags
List<String> list = Arrays.asList("" + project.getName());
operation.setTags(list);
// 1 - add path parameters
for (String pathVarName : urlMapping.getPathVariableNames()) {
PathParameter parameter = new PathParameter();
parameter.setName(pathVarName);
// retrieve parameter description from bean
UrlMappingParameter ump = null;
try {
ump = umo.getParameterByName(pathVarName);
} catch (Exception e) {
}
if (ump != null && ump.getType() == Type.Path) {
parameter.setDescription(ump.getComment());
Schema<?> schema = getSchema(ump);
if (schema != null) {
parameter.setSchema(schema);
}
}
operation.addParametersItem(parameter);
}
// 2 - add other parameters
for (UrlMappingParameter ump : umo.getParameterList()) {
Parameter parameter = null;
if (ump.getType() == Type.Query) {
parameter = new QueryParameter();
} else if (ump.getType() == Type.Form) {
addFormParameter(operation, ump);
} else if (ump.getType() == Type.Body) {
addBodyParameter(operation, ump, oasDirUrl, refList, useExternalRef);
} else if (ump.getType() == Type.Header) {
parameter = new HeaderParameter();
} else if (ump.getType() == Type.Path) {
// ignore : should have been treated before
}
if (parameter != null) {
// Query | Header
parameter.setName(ump.getName());
parameter.setDescription(ump.getComment());
parameter.setRequired(ump.isRequired());
// parameter.setAllowEmptyValue(allowEmptyValue);
Schema<?> schema = getSchema(ump);
if (schema != null) {
parameter.setSchema(schema);
}
// add parameter
if (ump.isExposed()) {
operation.addParametersItem(parameter);
}
}
}
// Responses
List<String> produces = new ArrayList<String>();
if (umo instanceof AbstractRestOperation) {
DataContent dataOutput = ((AbstractRestOperation) umo).getOutputContent();
if (dataOutput.equals(DataContent.toJson)) {
produces = Arrays.asList(MimeType.Json.value());
} else if (dataOutput.equals(DataContent.toXml)) {
produces = Arrays.asList(MimeType.Xml.value());
} else {
produces = Arrays.asList(MimeType.Json.value(), MimeType.Xml.value());
}
}
ApiResponses responses = new ApiResponses();
operation.setResponses(responses);
for (UrlMappingResponse umr : umo.getResponseList()) {
String statusCode = umr.getStatusCode();
if (!statusCode.isEmpty()) {
if (!responses.containsKey(statusCode)) {
ApiResponse response = new ApiResponse();
response.setDescription(umr.getStatusText());
responses.addApiResponse(statusCode, response);
String modelReference = ((IMappingRefModel) umr).getModelReference();
if (!modelReference.isEmpty() && !produces.isEmpty()) {
if (modelReference.indexOf(".jsonschema") != -1) {
modelReference = modelReference.replace(".jsonschema#/definitions/", ".json#/components/schemas/");
modelReference = oasDirUrl + modelReference;
}
Content content = new Content();
response.setContent(content);
for (String mt : produces) {
MediaType mediaType = new MediaType();
content.addMediaType(mt, mediaType);
ObjectSchema schema = new ObjectSchema();
if (!refList.contains(modelReference)) {
refList.add(modelReference);
}
if (!useExternalRef && modelReference.indexOf('#') != -1) {
modelReference = modelReference.substring(modelReference.indexOf('#'));
}
schema.set$ref(modelReference);
mediaType.setSchema(schema);
}
}
}
}
}
if (umo.getMethod().equals(HttpMethodType.DELETE.name())) {
item.setDelete(operation);
} else if (umo.getMethod().equals(HttpMethodType.GET.name())) {
item.setGet(operation);
} else if (umo.getMethod().equals(HttpMethodType.HEAD.name())) {
item.setHead(operation);
} else if (umo.getMethod().equals(HttpMethodType.OPTIONS.name())) {
item.setOptions(operation);
} else if (umo.getMethod().equals(HttpMethodType.POST.name())) {
item.setPost(operation);
} else if (umo.getMethod().equals(HttpMethodType.PUT.name())) {
item.setPut(operation);
} else if (umo.getMethod().equals(HttpMethodType.TRACE.name())) {
item.setTrace(operation);
}
}
paths.addPathItem(urlMapping.getPathWithPrefix(), item);
}
} catch (Exception e) {
e.printStackTrace();
Engine.logEngine.error("Unexpected exception while parsing UrlMapper to generate definition", e);
}
openAPI.setPaths(paths);
// Models and Schemas
try {
Map<String, JSONObject> modelMap = new HashMap<String, JSONObject>(1000);
String models = getModels(oasDirUrl, urlMapper, modelMap);
/*System.out.println("refList");
for (String keyRef: refList) {
System.out.println(keyRef);
}
System.out.println("modelMap");
for (String keyRef: modelMap.keySet()) {
System.out.println(keyRef);
}*/
Set<String> done = new HashSet<String>();
JSONObject jsonModels = new JSONObject(models);
for (String keyRef : refList) {
addModelsFromMap(done, modelMap, keyRef, jsonModels);
}
OpenAPI oa = new OpenAPI();
String s = Json.pretty(oa.info(new Info()));
JSONObject json = new JSONObject(s);
json.put("components", new JSONObject());
json.getJSONObject("components").put("schemas", jsonModels);
JsonNode rootNode = Json.mapper().readTree(json.toString());
OpenAPIDeserializer ds = new OpenAPIDeserializer();
SwaggerParseResult result = ds.deserialize(rootNode);
@SuppressWarnings("rawtypes") Map<String, Schema> map = result.getOpenAPI().getComponents().getSchemas();
openAPI.getComponents().schemas(map);
modelMap.clear();
} catch (Throwable t) {
t.printStackTrace();
Engine.logEngine.error("Unexpected exception while parsing UrlMapper to generate models", t);
}
// write yaml
try {
FileUtils.write(yamlFile, prettyPrintYaml(openAPI), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
Engine.logEngine.error("Unexpected exception while writing project YAML file", e);
} finally {
Long t1 = System.currentTimeMillis();
Engine.logEngine.info("YAML file for " + projectName + " project written in " + (t1 - t0) + " ms");
}
}
}
use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.
the class ReferencesView method handleConnectorSelection.
private void handleConnectorSelection(Object firstElement) {
ConnectorTreeObject connectorTreeObject = (ConnectorTreeObject) firstElement;
Connector connectorSelected = connectorTreeObject.getObject();
String connectorSelectedName = connectorSelected.getName();
Project projectConnectorSelected = connectorSelected.getProject();
String connectorProjectName = connectorSelected.getProject().getName();
List<Transaction> transactions = connectorSelected.getTransactionsList();
try {
Project project = null;
List<String> projectNames = Engine.theApp.databaseObjectsManager.getAllProjectNamesList();
ProjectExplorerView projectExplorerView = ConvertigoPlugin.getDefault().getProjectExplorerView();
treeViewer.setInput(null);
RootNode root = new RootNode();
ConnectorNode connectorNode = null;
connectorNode = getConnectorNode(root, connectorSelected);
root.addChild(connectorNode);
IsUsedByNode isUsedByNode = new IsUsedByNode(connectorNode, "Is used by");
RequiresNode requiresNode = new RequiresNode(connectorNode, "Requires");
ProjectNode projectFolder = null;
// Search handlers are that referenced by the selected connector for continue with site Clipper
if (connectorSelected instanceof HtmlConnector) {
ProjectNode projectNode = new ProjectNode(requiresNode, connectorProjectName, projectConnectorSelected);
for (Transaction transaction : transactions) {
List<Statement> statements = ((HtmlTransaction) transaction).getStatements();
List<String> siteClipperConnectorNames = new ArrayList<String>();
for (Statement statement : statements) {
List<Statement> statementList = ((FunctionStatement) statement).getStatements();
for (Statement st : statementList) {
if (st instanceof ContinueWithSiteClipperStatement) {
ContinueWithSiteClipperStatement continueWithSiteClipperStatement = (ContinueWithSiteClipperStatement) st;
String siteClipperconnectorName = continueWithSiteClipperStatement.getSiteClipperConnectorName();
if (!siteClipperConnectorNames.contains(siteClipperconnectorName)) {
siteClipperConnectorNames.add(siteClipperconnectorName);
Connector siteClipperConnector = projectConnectorSelected.getConnectorByName(siteClipperconnectorName);
ConnectorNode connectorSiteClipperNode = new SiteClipperConnectorNode(projectNode, siteClipperconnectorName, siteClipperConnector);
projectNode.addChild(connectorSiteClipperNode);
}
}
}
}
}
if (projectNode.hasChildren()) {
requiresNode.addChild(projectNode);
}
} else if (connectorSelected instanceof SiteClipperConnector) {
List<Connector> connectors = projectConnectorSelected.getConnectorsList();
ProjectNode projectNode = new ProjectNode(isUsedByNode, connectorProjectName, projectConnectorSelected);
for (Connector connector : connectors) {
if (connector instanceof HtmlConnector) {
List<Transaction> transactionList = ((HtmlConnector) connector).getTransactionsList();
for (Transaction transaction : transactionList) {
List<Statement> statements = ((HtmlTransaction) transaction).getStatements();
for (Statement statement : statements) {
List<Statement> statementList = ((FunctionStatement) statement).getStatements();
for (Statement st : statementList) {
if (st instanceof ContinueWithSiteClipperStatement) {
String sourceSiteClipperConnectorName = ((ContinueWithSiteClipperStatement) st).getSiteClipperConnectorName();
if (sourceSiteClipperConnectorName.equals(connectorSelectedName)) {
ContinueWithSiteClipperStatement continueWithSiteClipperStatement = (ContinueWithSiteClipperStatement) st;
HtmlConnectorNode htmlConnectorNode = new HtmlConnectorNode(projectNode, connector.getName(), connector);
projectNode.addChild(htmlConnectorNode);
TransactionNode transactionNode = new TransactionNode(htmlConnectorNode, transaction.getName(), continueWithSiteClipperStatement);
htmlConnectorNode.addChild(transactionNode);
}
}
}
}
}
}
}
if (projectNode.hasChildren()) {
isUsedByNode.addChild(projectNode);
}
}
// Searching all objects that are referenced by the selected connector
for (String projectName : projectNames) {
project = getProject(projectName, projectExplorerView);
if (project != null) {
projectFolder = new ProjectNode(isUsedByNode, projectName, project);
UrlMapper urlMapper = project.getUrlMapper();
if (urlMapper != null) {
MapperNode mapperNode = new MapperNode(projectFolder, urlMapper.getName(), urlMapper);
List<UrlMapping> mappings = urlMapper.getMappingList();
for (UrlMapping mapping : mappings) {
MappingPathNode pathNode = new MappingPathNode(mapperNode, mapping.getPath(), mapping);
List<UrlMappingOperation> operations = mapping.getOperationList();
for (UrlMappingOperation operation : operations) {
String targetRequestable = operation.getTargetRequestable();
if (targetRequestable.startsWith(projectConnectorSelected + "." + connectorSelectedName + ".")) {
MappingOperationNode operationNode = new MappingOperationNode(pathNode, operation.getName(), operation);
pathNode.addChild(operationNode);
}
}
if (pathNode.hasChildren()) {
mapperNode.addChild(pathNode);
}
}
if (mapperNode.hasChildren()) {
projectFolder.addChild(mapperNode);
}
}
List<Sequence> sequences = project.getSequencesList();
for (Sequence sequence : sequences) {
List<Step> steps = sequence.getSteps();
SequenceNode sequenceNode = new SequenceNode(projectFolder, sequence.getName(), sequence);
for (Step step : steps) {
getConnectorReferencingIsUsedBy(step, projectExplorerView, sequenceNode, transactions, connectorProjectName, connectorSelectedName);
}
if (sequenceNode.hasChildren()) {
projectFolder.addChild(sequenceNode);
}
}
if (projectFolder.hasChildren()) {
isUsedByNode.addChild(projectFolder);
}
}
}
if (requiresNode.hasChildren()) {
connectorNode.addChild(requiresNode);
}
if (isUsedByNode.hasChildren()) {
connectorNode.addChild(isUsedByNode);
}
if (!connectorNode.hasChildren()) {
connectorNode.addChild(new InformationNode(connectorNode, "This connector is not used by any other objects"));
}
treeViewer.setInput(root);
treeViewer.expandAll();
} catch (EngineException e) {
ConvertigoPlugin.logException(e, "Error while analyzing the projects hierarchy", true);
}
}
use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.
the class ReferencesView method handleSequenceSelection.
private void handleSequenceSelection(Object firstElement) {
SequenceTreeObject sequenceTreeObject = (SequenceTreeObject) firstElement;
Sequence sequenceSelected = sequenceTreeObject.getObject();
String sequenceSelectedName = sequenceSelected.getName();
// String sequenceProjectName = sequenceSelected.getProject().getName();
List<String> projectNames = Engine.theApp.databaseObjectsManager.getAllProjectNamesList();
ProjectExplorerView projectExplorerView = ConvertigoPlugin.getDefault().getProjectExplorerView();
treeViewer.setInput(null);
// Get the referencing sequence steps
List<String> referencingSequence = new ArrayList<String>();
RootNode root = new RootNode();
SequenceNode sequenceFolder = new SequenceNode(root, sequenceSelectedName, sequenceSelected);
root.addChild(sequenceFolder);
IsUsedByNode isUsedByNode = new IsUsedByNode(sequenceFolder, "Is used by");
// Searching all objects that reference the selected sequence
for (String projectName : projectNames) {
Project project = getProject(projectName, projectExplorerView);
if (project != null) {
ProjectNode projectFolder = null;
projectFolder = new ProjectNode(isUsedByNode, project.getName(), project);
List<Sequence> sequences = project.getSequencesList();
referencingSequence.clear();
UrlMapper urlMapper = project.getUrlMapper();
if (urlMapper != null) {
MapperNode mapperNode = new MapperNode(projectFolder, urlMapper.getName(), urlMapper);
List<UrlMapping> mappings = urlMapper.getMappingList();
for (UrlMapping mapping : mappings) {
MappingPathNode pathNode = new MappingPathNode(mapperNode, mapping.getPath(), mapping);
List<UrlMappingOperation> operations = mapping.getOperationList();
for (UrlMappingOperation operation : operations) {
String targetRequestable = operation.getTargetRequestable();
if (targetRequestable.equals(projectName + "." + sequenceSelectedName)) {
MappingOperationNode operationNode = new MappingOperationNode(pathNode, operation.getName(), operation);
pathNode.addChild(operationNode);
}
}
if (pathNode.hasChildren()) {
mapperNode.addChild(pathNode);
}
}
if (mapperNode.hasChildren()) {
projectFolder.addChild(mapperNode);
}
}
for (Sequence sequence : sequences) {
List<Step> steps = sequence.getSteps();
for (Step step : steps) {
SequenceNode sequenceNode = new SequenceNode(projectFolder, sequence.getName(), sequence);
getSequenceReferencingIsUsedBy(step, sequenceSelected, sequenceNode);
if (sequenceNode.hasChildren()) {
projectFolder.addChild(sequenceNode);
}
}
}
if (projectFolder.hasChildren()) {
isUsedByNode.addChild(projectFolder);
}
}
}
List<Step> steps = sequenceSelected.getSteps();
RequiresNode requiresNode = new RequiresNode(root, "Requires");
// Searching all objects that are referenced by the selected sequence
List<String> transactionList = new ArrayList<String>();
List<String> sequenceList = new ArrayList<String>();
for (Step step : steps) {
getSequenceReferencingRequires(step, sequenceSelected, projectExplorerView, requiresNode, transactionList, sequenceList);
}
if (requiresNode.hasChildren()) {
sequenceFolder.addChild(requiresNode);
}
if (isUsedByNode.hasChildren()) {
sequenceFolder.addChild(isUsedByNode);
}
if (!sequenceFolder.hasChildren()) {
sequenceFolder.addChild(new InformationNode(sequenceFolder, "This sequence is not used in any sequence"));
}
treeViewer.setInput(root);
treeViewer.expandAll();
}
use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.
the class UrlMappingTreeObject method treeObjectPropertyChanged.
@Override
public void treeObjectPropertyChanged(TreeObjectEvent treeObjectEvent) {
super.treeObjectPropertyChanged(treeObjectEvent);
String propertyName = (String) treeObjectEvent.propertyName;
propertyName = ((propertyName == null) ? "" : propertyName);
TreeObject treeObject = (TreeObject) treeObjectEvent.getSource();
if (treeObject instanceof DatabaseObjectTreeObject) {
DatabaseObject databaseObject = (DatabaseObject) treeObject.getObject();
// Case Mapping path has changed
if ("path".equals(propertyName)) {
if (treeObject.equals(this)) {
try {
UrlMapping urlMapping = (UrlMapping) databaseObject;
String oldPath = (String) treeObjectEvent.oldValue;
List<String> oldPathVariableNames = urlMapping.getPathVariableNames(oldPath);
List<String> newPathVariableNames = urlMapping.getPathVariableNames();
if (!oldPathVariableNames.equals(newPathVariableNames)) {
int oldLen = oldPathVariableNames.size();
int newLen = newPathVariableNames.size();
/*MessageBox messageBox = new MessageBox(viewer.getControl().getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
messageBox.setMessage("Do you really want to update mapping path parameters?");
messageBox.setText("Update parameters\"?");
int ret = messageBox.open();*/
int ret = SWT.YES;
if (ret == SWT.YES) {
// case of deletion
if (newLen < oldLen) {
for (String variableName : oldPathVariableNames) {
if (!newPathVariableNames.contains(variableName)) {
for (UrlMappingOperation operation : urlMapping.getOperationList()) {
for (UrlMappingParameter parameter : operation.getParameterList()) {
if (parameter.getName().equals(variableName)) {
if (parameter.getType().equals(Type.Path)) {
try {
parameter.delete();
operation.remove(parameter);
operation.hasChanged = true;
} catch (EngineException e) {
ConvertigoPlugin.logException(e, "Error when deleting the parameter \"" + variableName + "\"");
}
}
}
}
}
}
}
} else // case of rename
if (newLen == oldLen) {
for (int i = 0; i < oldLen; i++) {
String variableName = oldPathVariableNames.get(i);
for (UrlMappingOperation operation : urlMapping.getOperationList()) {
for (UrlMappingParameter parameter : operation.getParameterList()) {
if (parameter.getName().equals(variableName)) {
if (parameter.getType().equals(Type.Path)) {
try {
parameter.setName(newPathVariableNames.get(i));
parameter.hasChanged = true;
} catch (EngineException e) {
ConvertigoPlugin.logException(e, "Error when renaming the parameter \"" + variableName + "\"");
}
}
}
}
}
}
} else // case of creation
{
for (String variableName : newPathVariableNames) {
for (UrlMappingOperation operation : urlMapping.getOperationList()) {
UrlMappingParameter parameter = null;
try {
parameter = operation.getParameterByName(variableName);
} catch (EngineException e) {
try {
parameter = new PathParameter();
parameter.setName(variableName);
parameter.bNew = true;
operation.add(parameter);
} catch (EngineException ex) {
ConvertigoPlugin.logException(ex, "Error when adding the parameter \"" + variableName + "\"");
}
}
}
}
}
getProjectExplorerView().reloadTreeObject(this);
}
}
} catch (Exception e) {
ConvertigoPlugin.logWarning(e, "Could not reload in tree Mapping \"" + databaseObject.getName() + "\" !");
}
}
}
}
}
use of com.twinsoft.convertigo.beans.core.UrlMapping in project convertigo by convertigo.
the class UrlMappingTreeObject method treeObjectAdded.
@Override
public void treeObjectAdded(TreeObjectEvent treeObjectEvent) {
super.treeObjectAdded(treeObjectEvent);
TreeObject treeObject = (TreeObject) treeObjectEvent.getSource();
if (treeObject instanceof DatabaseObjectTreeObject) {
DatabaseObject databaseObject = (DatabaseObject) treeObject.getObject();
try {
boolean needReload = false;
UrlMapping urlMapping = getObject();
// An UrlMappingOperation has been added : add all path parameters
if (databaseObject instanceof UrlMappingOperation) {
if (urlMapping.equals(databaseObject.getParent())) {
UrlMappingOperation operation = (UrlMappingOperation) databaseObject;
if (operation.bNew) {
for (String variableName : urlMapping.getPathVariableNames()) {
UrlMappingParameter parameter = null;
try {
parameter = operation.getParameterByName(variableName);
} catch (EngineException e) {
try {
parameter = new PathParameter();
parameter.setName(variableName);
parameter.bNew = true;
operation.add(parameter);
operation.hasChanged = true;
needReload = true;
} catch (EngineException ex) {
ConvertigoPlugin.logException(ex, "Error when adding the parameter \"" + variableName + "\"");
}
}
}
}
if (needReload) {
getProjectExplorerView().reloadTreeObject(this);
}
}
}
} catch (Exception e) {
ConvertigoPlugin.logWarning(e, "Could not reload in tree Mapping \"" + databaseObject.getName() + "\" !");
}
}
}
Aggregations