Search in sources :

Example 11 with WSDLArchiveInfo

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLArchiveInfo in project carbon-apimgt by wso2.

the class APIConsumerImpl method getWSDLDocument.

// Remove this method once the jaggery store app is removed.
@Deprecated
@Override
public String getWSDLDocument(String username, String tenantDomain, String resourceUrl, Map environmentDetails, Map apiDetails) throws APIManagementException {
    if (username == null) {
        username = APIConstants.END_USER_ANONYMOUS;
    }
    if (tenantDomain == null) {
        tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    Map<String, Object> docResourceMap = APIUtil.getDocument(username, resourceUrl, tenantDomain);
    String wsdlContent = "";
    if (log.isDebugEnabled()) {
        log.debug("WSDL document resource availability: " + docResourceMap.isEmpty());
    }
    if (!docResourceMap.isEmpty()) {
        try {
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            IOUtils.copy((InputStream) docResourceMap.get("Data"), arrayOutputStream);
            String apiName = (String) apiDetails.get(API_NAME);
            String apiVersion = (String) apiDetails.get(API_VERSION);
            String apiProvider = (String) apiDetails.get(API_PROVIDER);
            String environmentName = (String) environmentDetails.get(ENVIRONMENT_NAME);
            String environmentType = (String) environmentDetails.get(ENVIRONMENT_TYPE);
            if (log.isDebugEnabled()) {
                log.debug("Published SOAP api gateway environment name: " + environmentName + " environment type: " + environmentType);
            }
            if (resourceUrl.endsWith(APIConstants.ZIP_FILE_EXTENSION)) {
                WSDLArchiveInfo archiveInfo = APIMWSDLReader.extractAndValidateWSDLArchive((InputStream) docResourceMap.get("Data")).getWsdlArchiveInfo();
                File folderToImport = new File(archiveInfo.getLocation() + File.separator + APIConstants.API_WSDL_EXTRACTED_DIRECTORY);
                Collection<File> wsdlFiles = APIFileUtil.searchFilesWithMatchingExtension(folderToImport, APIFileUtil.WSDL_FILE_EXTENSION);
                Collection<File> xsdFiles = APIFileUtil.searchFilesWithMatchingExtension(folderToImport, APIFileUtil.XSD_FILE_EXTENSION);
                if (wsdlFiles != null) {
                    for (File foundWSDLFile : wsdlFiles) {
                        Path fileLocation = Paths.get(foundWSDLFile.getAbsolutePath());
                        byte[] updatedWSDLContent = this.getUpdatedWSDLByEnvironment(resourceUrl, Files.readAllBytes(fileLocation), environmentName, environmentType, apiName, apiVersion, apiProvider);
                        File updatedWSDLFile = new File(foundWSDLFile.getPath());
                        wsdlFiles.remove(foundWSDLFile);
                        FileUtils.writeByteArrayToFile(updatedWSDLFile, updatedWSDLContent);
                        wsdlFiles.add(updatedWSDLFile);
                    }
                    wsdlFiles.addAll(xsdFiles);
                    ZIPUtils.zipFiles(folderToImport.getCanonicalPath() + APIConstants.UPDATED_WSDL_ZIP, wsdlFiles);
                    wsdlContent = folderToImport.getCanonicalPath() + APIConstants.UPDATED_WSDL_ZIP;
                }
            } else {
                arrayOutputStream = new ByteArrayOutputStream();
                IOUtils.copy((InputStream) docResourceMap.get("Data"), arrayOutputStream);
                byte[] updatedWSDLContent = this.getUpdatedWSDLByEnvironment(resourceUrl, arrayOutputStream.toByteArray(), environmentName, environmentType, apiName, apiVersion, apiProvider);
                wsdlContent = new String(updatedWSDLContent);
            }
        } catch (IOException e) {
            handleException("Error occurred while copying wsdl content into byte array stream for resource: " + resourceUrl, e);
        }
    } else {
        handleException("No wsdl resource found for resource path: " + resourceUrl);
    }
    JSONObject data = new JSONObject();
    data.put(APIConstants.DOCUMENTATION_RESOURCE_MAP_CONTENT_TYPE, docResourceMap.get(APIConstants.DOCUMENTATION_RESOURCE_MAP_CONTENT_TYPE));
    data.put(APIConstants.DOCUMENTATION_RESOURCE_MAP_NAME, docResourceMap.get(APIConstants.DOCUMENTATION_RESOURCE_MAP_NAME));
    data.put(APIConstants.DOCUMENTATION_RESOURCE_MAP_DATA, wsdlContent);
    if (log.isDebugEnabled()) {
        log.debug("Updated wsdl content details for wsdl resource: " + docResourceMap.get("name") + " is " + data.toJSONString());
    }
    return data.toJSONString();
}
Also used : Path(java.nio.file.Path) JSONObject(org.json.simple.JSONObject) InputStream(java.io.InputStream) JSONObject(org.json.simple.JSONObject) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) WSDLArchiveInfo(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLArchiveInfo) IOException(java.io.IOException) File(java.io.File) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile)

Aggregations

WSDLArchiveInfo (org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo)9 IOException (java.io.IOException)5 File (java.io.File)4 InputStream (java.io.InputStream)4 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)4 FileInputStream (java.io.FileInputStream)3 Response (javax.ws.rs.core.Response)3 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)3 WSDLProcessor (org.wso2.carbon.apimgt.core.api.WSDLProcessor)3 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)2 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 API (org.wso2.carbon.apimgt.core.models.API)2 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)2 Label (org.wso2.carbon.apimgt.core.models.Label)2 WSDLInfo (org.wso2.carbon.apimgt.core.models.WSDLInfo)2 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)2