Skip to content

Commit

Permalink
Merge pull request #1086 from rosensilva/main
Browse files Browse the repository at this point in the history
Fix build in mi not working issue
  • Loading branch information
rosensilva authored Mar 25, 2022
2 parents 0faf8f4 + 991d41d commit 72eb9a3
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@

package org.wso2.integrationstudio.artifact.synapse.api.ui.wizard;

import java.awt.PageAttributes.MediaType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
Expand All @@ -36,7 +30,6 @@
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;
Expand All @@ -56,14 +49,12 @@
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.project.MavenProject;
import org.apache.maven.wagon.ConnectionException;
import org.apache.synapse.config.xml.XMLConfigConstants;
import org.apache.synapse.config.xml.endpoints.EndpointSerializer;
import org.apache.synapse.config.xml.rest.APIFactory;
Expand Down Expand Up @@ -92,10 +83,6 @@
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.jface.wizard.IWizardPage;
import org.json.simple.JSONValue;
import org.wso2.carbon.rest.api.APIException;
Expand All @@ -119,7 +106,6 @@
import org.wso2.integrationstudio.general.project.artifact.bean.RegistryItem;
import org.wso2.integrationstudio.gmf.esb.APIVersionType;
import org.wso2.integrationstudio.gmf.esb.ArtifactType;
import org.wso2.integrationstudio.gmf.esb.ContentType;
import org.wso2.integrationstudio.logging.core.IIntegrationStudioLog;
import org.wso2.integrationstudio.logging.core.Logger;
import org.wso2.integrationstudio.maven.util.MavenUtils;
Expand Down Expand Up @@ -162,12 +148,6 @@ public class SynapseAPICreationWizard extends AbstractWSO2ProjectCreationWizard
private static final String WHITE_SPACE = " ";
private static final String METADATA_TYPE = "synapse/metadata";
private static final String REGISTRY_RESOURCE_PATH = "/_system/governance/swagger_files";
private static final String SWAGGER_CONVERTER_API_URL = "https://converter.swagger.io/api/convert";
private static final String SWAGGER_API_CALL_CONSENT_TITLE = "Convert to Swagger version 3?";
private static final String SWAGGER_API_CALL_CONSENT_MESSAGE = "Integration Studio supports Swagger version 3 and above only. The Swagger file you have uploaded is version 2. You can convert this file to version 3 by clicking “Yes”. Note that Integration Studio uses the https://converter.swagger.io API to convert your payload.\n\nYou can continue with the Swagger file you uploaded by clicking “No”, if you feel the detected version is incorrect.\n\nClick “Cancel” to terminate the API creation process.";
private static final String SWAGGER_API_CALL_FAILED_TITLE = "Conversion failed";
private static final String SWAGGER_API_CALL_FAILED_MESSAGE = "Failed to convert swagger 2 content to swagger 3. Please make sure you are connected to the internet and try again.";

private static final String SOAP_BODY_PREFIX = "<soapenv:Envelope xmlns:soapenv"
+ "=\"http://www.w3.org/2003/05/soap-envelope\">\r\n<soapenv:Header/>\r\n<soapenv:Body>\r\n";
private static final String SOAP_BODY_POSTFIX = "</soapenv:Body>\r\n</soapenv:Envelope>\r\n";
Expand Down Expand Up @@ -782,61 +762,18 @@ public static String convertJSONtoYaml(String jsonSource) throws Exception {

private String getSwaggerFileAsYAML(File swaggerFile, String apiName) {
String swaggerContent = "";
boolean terminateFlag = false;
try {
swaggerContent = new String(Files.readAllBytes(Paths.get(swaggerFile.getAbsolutePath())));
if (FilenameUtils.getExtension(swaggerFile.getAbsolutePath()).equals("json")) {
swaggerContent = convertJSONtoYaml(swaggerContent);
}
if (isVersionTwoYamlFile(swaggerContent)) {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();;
MessageBox dialog = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.CANCEL);
dialog.setText(SWAGGER_API_CALL_CONSENT_TITLE);
dialog.setMessage(SWAGGER_API_CALL_CONSENT_MESSAGE);

int returnCode = dialog.open();
if (SWT.YES == returnCode) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(SWAGGER_CONVERTER_API_URL))
.POST(HttpRequest.BodyPublishers.ofString(swaggerContent))
.setHeader("Content-Type", ContentType.YAML.getLiteral())
.build();
HttpResponse<String> response;
try {
response = client.send(request,
HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == HttpStatus.SC_OK) {
swaggerContent = response.body();
} else {
throw new ConnectionException(String.format("Unexpected response code received from api. %s", response.statusCode()));
}
} catch (ConnectException e) {
MessageBox dialogRequestFailed = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
dialogRequestFailed.setText(SWAGGER_API_CALL_FAILED_TITLE);
dialogRequestFailed.setMessage(SWAGGER_API_CALL_FAILED_MESSAGE);
dialogRequestFailed.open();
terminateFlag = true;
log.error(e.getMessage());
throw new Exception(e.getMessage());
}

} else if (SWT.CANCEL == returnCode) {
terminateFlag = true;
throw new Exception("User terminated api creation.");
}
}

RestApiAdmin restAPIAdmin = new RestApiAdmin();
swaggerContent = restAPIAdmin.updateNameInSwagger(apiName, swaggerContent);
} catch (IOException e) {
log.error("Exception occured while reading swagger file", e);
} catch (APIException e) {
log.error("Exception occured while updating swagger name", e);
} catch (Exception e) {
if (terminateFlag) {
throw new RuntimeException(e.getMessage());
}
log.error("Exception occured while converting swagger JSON to YAML", e);
}
return swaggerContent;
Expand Down Expand Up @@ -1042,15 +979,6 @@ public void openEditor(File file) {
log.error("Cannot open the editor", e);
}
}

private boolean isVersionTwoYamlFile(String yamlContent) {
Pattern p = Pattern.compile("([ ]*)(swagger[ ]*:[ ]*[\\\"]?[2][.])(.*)");
Matcher m = p.matcher(yamlContent.toLowerCase());
if (m.find()) {
return true;
}
return false;
}

@Override
public void addPages() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,7 @@ public enum ContentType implements Enumerator {
* @generated
* @ordered
*/
JSON(1, "json", "application/json"),

/**
* The '<em><b>Yaml</b></em>' literal object.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #JSON_VALUE
* @generated
* @ordered
*/
YAML(2, "yaml", "application/yaml");
JSON(1, "json", "application/json");

/**
* The '<em><b>Xml</b></em>' literal value.
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@
<javax.xml.stream.stax.api>1.0-2</javax.xml.stream.stax.api>
<org.codehaus.jettison>1.3.1</org.codehaus.jettison>
<activti.designer.version>5.18.0</activti.designer.version>
<mi.product.version>4.1.0-rc1</mi.product.version>
<mi.product.directory>4.1.0-rc1</mi.product.directory>
<mi.product.version>4.1.0</mi.product.version>
<mi.product.directory>4.1.0</mi.product.directory>
<mi.dashboard.version>4.1.0-rc1</mi.dashboard.version>
<apache.maven.version>3.6.3</apache.maven.version>
<version.sortpom.plugin>2.3.0</version.sortpom.plugin>
Expand Down

0 comments on commit 72eb9a3

Please sign in to comment.