Search in sources :

Example 41 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.

the class AbstractWSDLProcessor method getWSDLArchive.

/**
 * Creates a zip for from the files of the provided path and returns an InputStream for the created zip file.
 *
 * @param wsdlArchiveFilePath file path where the WSDL archive is extracted
 * @return ByteArrayInputStream object of the WSDL archive zip file
 * @throws APIMgtWSDLException when error occurred when creating the zip and returning the InputStream
 */
ByteArrayInputStream getWSDLArchive(String wsdlArchiveFilePath) throws APIMgtWSDLException {
    try {
        Collection<File> allFiles = FileUtils.listFiles(new File(wsdlArchiveFilePath), null, true);
        String outputZipFilePath = wsdlArchiveFilePath + File.separator + APIConstants.WSDL_ARCHIVE_UPDATED_ZIP_FILE;
        ZIPUtils.zipFiles(outputZipFilePath, allFiles);
        File outputZipFile = new File(outputZipFilePath);
        return new ByteArrayInputStream(FileUtils.readFileToByteArray(outputZipFile));
    } catch (APIManagementException | IOException e) {
        throw new APIMgtWSDLException("General error occurred while creating WSDL archive", e, ExceptionCodes.ERROR_WHILE_CREATING_WSDL_ARCHIVE);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) File(java.io.File)

Example 42 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.

the class AbstractWSDLProcessor method getSecuredParsedDocumentFromURL.

/**
 * Returns an "XXE safe" built DOM XML object by reading the content from the provided URL.
 *
 * @param url URL to fetch the content
 * @return an "XXE safe" built DOM XML object by reading the content from the provided URL
 * @throws APIMgtWSDLException When error occurred while reading from URL
 */
Document getSecuredParsedDocumentFromURL(URL url) throws APIMgtWSDLException {
    InputStream inputStream = null;
    try {
        DocumentBuilderFactory factory = getSecuredDocumentBuilder();
        DocumentBuilder builder = factory.newDocumentBuilder();
        inputStream = url.openStream();
        return builder.parse(inputStream);
    } catch (ParserConfigurationException | IOException | SAXException e) {
        throw new APIMgtWSDLException("Error while reading WSDL document", e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 43 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.

the class WSDL11ProcessorImpl method init.

@Override
public boolean init(byte[] wsdlContent) throws APIMgtWSDLException {
    setMode(Mode.SINGLE);
    WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
    // switch off the verbose mode
    wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
    wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
    try {
        wsdlDefinition = wsdlReader.readWSDL(null, getSecuredParsedDocumentFromContent(wsdlContent));
        if (log.isDebugEnabled()) {
            log.debug("Successfully initialized an instance of " + this.getClass().getSimpleName() + " with a single WSDL.");
        }
    } catch (WSDLException | APIManagementException e) {
        // This implementation class cannot process the WSDL.
        log.debug("Cannot process the WSDL by " + this.getClass().getName(), e);
        setError(new ErrorItem(ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorMessage(), e.getMessage(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorCode(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getHttpStatusCode()));
    }
    return !hasError;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) ErrorItem(org.wso2.carbon.apimgt.api.ErrorItem) WSDLReader(javax.wsdl.xml.WSDLReader) APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader)

Example 44 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.

the class WSDL11ProcessorImpl method initPath.

@Override
public boolean initPath(String path) throws APIMgtWSDLException {
    setMode(Mode.ARCHIVE);
    pathToDefinitionMap = new HashMap<>();
    wsdlArchiveExtractedPath = path;
    WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
    try {
        // switch off the verbose mode
        wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
        wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
        File folderToImport = new File(path);
        Collection<File> foundWSDLFiles = APIFileUtil.searchFilesWithMatchingExtension(folderToImport, "wsdl");
        if (log.isDebugEnabled()) {
            log.debug("Found " + foundWSDLFiles.size() + " WSDL file(s) in path " + path);
        }
        for (File file : foundWSDLFiles) {
            String absWSDLPath = file.getAbsolutePath();
            if (log.isDebugEnabled()) {
                log.debug("Processing WSDL file: " + absWSDLPath);
            }
            Definition definition = wsdlReader.readWSDL(path, getSecuredParsedDocumentFromPath(absWSDLPath));
            pathToDefinitionMap.put(absWSDLPath, definition);
            // set the first found WSDL as wsdlDefinition variable assuming that it is the root WSDL
            if (wsdlDefinition == null) {
                wsdlDefinition = definition;
            }
        }
        if (foundWSDLFiles.isEmpty()) {
            setError(ExceptionCodes.NO_WSDL_FOUND_IN_WSDL_ARCHIVE);
        }
        if (log.isDebugEnabled()) {
            log.debug("Successfully processed all WSDL files in path " + path);
        }
    } catch (WSDLException | APIManagementException e) {
        // This implementation class cannot process the WSDL. Continuing after setting canProcess = false
        log.debug(this.getClass().getName() + " was unable to process the WSDL Files for the path: " + path, e);
        setError(new ErrorItem(ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorMessage(), e.getMessage(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorCode(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getHttpStatusCode()));
    }
    return !hasError;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) Definition(javax.wsdl.Definition) ErrorItem(org.wso2.carbon.apimgt.api.ErrorItem) File(java.io.File) WSDLReader(javax.wsdl.xml.WSDLReader) APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader)

Example 45 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.

the class WSDL11ProcessorImpl method updateEndpointsOfSingleWSDL.

/**
 * Update the endpoint information of the provided WSDL definition when an API and the environment details are
 * provided
 *
 * @param api API
 * @param environmentName name of the gateway environment
 * @param environmentType type of the gateway environment
 * @param wsdlDefinition WSDL 1.1 definition
 * @throws APIMgtWSDLException when error occurred while updating the endpoints
 */
private void updateEndpointsOfSingleWSDL(API api, String environmentName, String environmentType, Definition wsdlDefinition) throws APIMgtWSDLException {
    Map serviceMap = wsdlDefinition.getAllServices();
    URL addressURI;
    String organization = api.getOrganization();
    for (Object entry : serviceMap.entrySet()) {
        Map.Entry svcEntry = (Map.Entry) entry;
        Service svc = (Service) svcEntry.getValue();
        Map portMap = svc.getPorts();
        for (Object o : portMap.entrySet()) {
            Map.Entry portEntry = (Map.Entry) o;
            Port port = (Port) portEntry.getValue();
            List<ExtensibilityElement> extensibilityElementList = port.getExtensibilityElements();
            String endpointTransport;
            for (ExtensibilityElement extensibilityElement : extensibilityElementList) {
                try {
                    addressURI = new URL(getAddressUrl(extensibilityElement));
                    endpointTransport = determineURLTransport(addressURI.getProtocol(), api.getTransports());
                    if (log.isDebugEnabled()) {
                        log.debug("Address URI for the port:" + port.getName() + " is " + addressURI.toString());
                    }
                } catch (MalformedURLException e) {
                    if (log.isDebugEnabled()) {
                        log.debug("Error occurred while getting the wsdl address location [" + getAddressUrl(extensibilityElement) + "]");
                    }
                    endpointTransport = determineURLTransport("https", api.getTransports());
                // This string to URL conversion done in order to identify URL transport eg - http or https.
                // Here if there is a conversion failure , consider "https" as default protocol
                }
                try {
                    setAddressUrl(extensibilityElement, endpointTransport, api.getContext(), environmentName, environmentType, organization);
                } catch (APIManagementException e) {
                    throw new APIMgtWSDLException("Error while setting gateway access URLs in the WSDL", e);
                }
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Port(javax.wsdl.Port) Service(javax.wsdl.Service) URL(java.net.URL) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)64 IOException (java.io.IOException)42 APIMgtWSDLException (org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException)33 Test (org.junit.Test)30 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 API (org.wso2.carbon.apimgt.core.models.API)28 File (java.io.File)27 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)24 ByteArrayInputStream (java.io.ByteArrayInputStream)23 FileInputStream (java.io.FileInputStream)23 InputStream (java.io.InputStream)22 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)22 WSDLException (javax.wsdl.WSDLException)21 HashMap (java.util.HashMap)20 Resource (org.wso2.carbon.registry.core.Resource)20 MalformedURLException (java.net.MalformedURLException)18 Map (java.util.Map)16 Response (javax.ws.rs.core.Response)16 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)16 SAXException (org.xml.sax.SAXException)16