Search in sources :

Example 11 with APIMgtWSDLException

use of org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException in project carbon-apimgt by wso2.

the class WSDL20ProcessorImpl method getUpdatedWSDLPath.

/**
 * Updates the endpoints of all the WSDL files in the path based on the provided API (context) and Label (host).
 *
 * @param api Provided API object
 * @param label Provided label object
 * @return Updated WSDL file path
 * @throws APIMgtWSDLException Error while updating WSDL files
 */
@Override
public String getUpdatedWSDLPath(API api, Label label) throws APIMgtWSDLException {
    if (label != null) {
        for (Map.Entry<String, Description> entry : pathToDescriptionMap.entrySet()) {
            Description description = entry.getValue();
            if (log.isDebugEnabled()) {
                log.debug("Updating endpoints of WSDL: " + entry.getKey());
            }
            updateEndpoints(label.getAccessUrls(), api, description);
            if (log.isDebugEnabled()) {
                log.debug("Successfully updated endpoints of WSDL: " + entry.getKey());
            }
            try (FileOutputStream wsdlFileOutputStream = new FileOutputStream(new File(entry.getKey()))) {
                WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
                writer.writeWSDL(description.toElement(), wsdlFileOutputStream);
            } catch (IOException | WSDLException e) {
                throw new APIMgtWSDLException("Failed to create WSDL archive for API:" + api.getName() + ":" + api.getVersion() + " for label " + label.getName(), ExceptionCodes.ERROR_WHILE_CREATING_WSDL_ARCHIVE);
            }
        }
    }
    return wsdlArchiveExtractedPath;
}
Also used : Description(org.apache.woden.wsdl20.Description) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLException(org.apache.woden.WSDLException) FileOutputStream(java.io.FileOutputStream) WSDLWriter(org.apache.woden.WSDLWriter) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 12 with APIMgtWSDLException

use of org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException in project carbon-apimgt by wso2.

the class WSDL20ProcessorImpl method initPath.

/**
 * {@inheritDoc}
 * Will return true if all the provided WSDL files in the initialized path is of 2.0 and can be successfully
 * parsed by woden.
 */
@Override
public boolean initPath(String path) throws APIMgtWSDLException {
    pathToDescriptionMap = new HashMap<>();
    wsdlArchiveExtractedPath = path;
    try {
        WSDLReader reader = getWsdlFactoryInstance().newWSDLReader();
        reader.setFeature(WSDLReader.FEATURE_VALIDATION, false);
        File folderToImport = new File(path);
        Collection<File> foundWSDLFiles = APIFileUtils.searchFilesWithMatchingExtension(folderToImport, "wsdl");
        if (log.isDebugEnabled()) {
            log.debug("Found " + foundWSDLFiles.size() + " WSDL file(s) in path " + path);
        }
        for (File file : foundWSDLFiles) {
            if (log.isDebugEnabled()) {
                log.debug("Processing WSDL file: " + file.getAbsolutePath());
            }
            Description description = reader.readWSDL(file.getAbsolutePath());
            pathToDescriptionMap.put(file.getAbsolutePath(), description);
        }
        if (foundWSDLFiles.size() > 0) {
            canProcess = true;
        }
        if (log.isDebugEnabled()) {
            log.debug("Successfully processed all WSDL files in path " + path);
        }
    } catch (WSDLException e) {
        // This implementation class cannot process the WSDL.
        log.debug("Cannot process the WSDL by " + this.getClass().getName(), e);
        canProcess = false;
    }
    return canProcess;
}
Also used : Description(org.apache.woden.wsdl20.Description) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLException(org.apache.woden.WSDLException) File(java.io.File) WSDLReader(org.apache.woden.WSDLReader)

Example 13 with APIMgtWSDLException

use of org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException in project carbon-apimgt by wso2.

the class WSDL20ProcessorImpl method getWSDL.

/**
 * {@inheritDoc}
 * @return WSDL 2.0 content bytes
 */
@Override
public byte[] getWSDL() throws APIMgtWSDLException {
    WSDLWriter writer;
    try {
        writer = getWsdlFactoryInstance().newWSDLWriter();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        writer.writeWSDL(wsdlDescription.toElement(), byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    } catch (WSDLException e) {
        throw new APIMgtWSDLException("Error while stringifying WSDL definition", e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    }
}
Also used : APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLException(org.apache.woden.WSDLException) WSDLWriter(org.apache.woden.WSDLWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 14 with APIMgtWSDLException

use of org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException in project carbon-apimgt by wso2.

the class WSDL20ProcessorImpl method updateEndpoints.

/**
 * Updates the endpoints of the {@code description} based on the provided {@code endpointURLs} and {@code api}.
 *
 * @param endpointURLs Endpoint URIs
 * @param api Provided API object
 * @param description WSDL 2.0 description
 * @throws APIMgtWSDLException If an error occurred while updating endpoints
 */
private void updateEndpoints(List<String> endpointURLs, API api, Description description) throws APIMgtWSDLException {
    String context = api.getContext().startsWith("/") ? api.getContext() : "/" + api.getContext();
    String selectedUrl;
    try {
        selectedUrl = APIMWSDLUtils.getSelectedEndpoint(endpointURLs) + context;
        if (log.isDebugEnabled()) {
            log.debug("Selected URL for updating endpoints of WSDL: " + selectedUrl);
        }
    } catch (MalformedURLException e) {
        throw new APIMgtWSDLException("Error while selecting endpoints for WSDL", e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    }
    if (!StringUtils.isBlank(selectedUrl)) {
        updateEndpoints(selectedUrl, description);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)

Example 15 with APIMgtWSDLException

use of org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException in project carbon-apimgt by wso2.

the class WSDLProcessFactory method getWSDLProcessor.

/**
 * Returns the appropriate WSDL 1.1 or 2.0 processor based on the content {@code wsdlContent}.
 *
 * @param wsdlContent Content of the WSDL
 * @return WSDL 1.1 or 2.0 processor for the provided content
 * @throws APIMgtWSDLException If an error occurs while determining the processor
 */
public WSDLProcessor getWSDLProcessor(byte[] wsdlContent) throws APIMgtWSDLException {
    for (String clazz : getWSDLProcessorClasses()) {
        WSDLProcessor processor;
        try {
            processor = (WSDLProcessor) Class.forName(clazz).newInstance();
            boolean canProcess = processor.init(wsdlContent);
            if (canProcess) {
                return processor;
            }
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            throw new APIMgtWSDLException("Error while instantiating " + clazz, e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
        }
    }
    // no processors found if this line reaches
    throw new APIMgtWSDLException("No WSDL processor found to process WSDL content", ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)

Aggregations

APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)24 IOException (java.io.IOException)9 WSDLProcessor (org.wso2.carbon.apimgt.core.api.WSDLProcessor)8 File (java.io.File)6 InputStream (java.io.InputStream)5 FileInputStream (java.io.FileInputStream)4 WSDLException (javax.wsdl.WSDLException)4 WSDLException (org.apache.woden.WSDLException)4 WSDLArchiveInfo (org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 API (org.wso2.carbon.apimgt.core.models.API)3 FileOutputStream (java.io.FileOutputStream)2 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Definition (javax.wsdl.Definition)2