Search in sources :

Example 66 with Wsdl

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

the class APIMWSDLReader method handleExceptionDuringValidation.

/**
 * Handles the provided exception occurred during validation and return a validation response or the exception.
 *
 * @param e exception object
 * @return a validation response if the exception contains an ErrorHandler
 * @throws APIManagementException if the exception doesn't contains an ErrorHandler. Throws the same error as 'e'
 */
private static WSDLValidationResponse handleExceptionDuringValidation(APIManagementException e) throws APIManagementException {
    if (e.getErrorHandler() != null && e.getErrorHandler().getHttpStatusCode() < 500) {
        log.debug("Validation error occurred due to invalid WSDL", e);
        WSDLValidationResponse validationResponse = new WSDLValidationResponse();
        validationResponse.setError(e.getErrorHandler());
        return validationResponse;
    } else {
        throw e;
    }
}
Also used : WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse)

Example 67 with Wsdl

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

the class APIMWSDLReader method extractAndValidateWSDLArchive.

/**
 * Extract the WSDL archive zip and validates it
 *
 * @param inputStream zip input stream
 * @return Validation information
 * @throws APIManagementException Error occurred during validation
 */
public static WSDLValidationResponse extractAndValidateWSDLArchive(InputStream inputStream) throws APIManagementException {
    String path = System.getProperty(APIConstants.JAVA_IO_TMPDIR) + File.separator + APIConstants.WSDL_ARCHIVES_TEMP_FOLDER + File.separator + UUID.randomUUID().toString();
    String archivePath = path + File.separator + APIConstants.WSDL_ARCHIVE_ZIP_FILE;
    String extractedLocation = APIFileUtil.extractUploadedArchive(inputStream, APIConstants.API_WSDL_EXTRACTED_DIRECTORY, archivePath, path);
    if (log.isDebugEnabled()) {
        log.debug("Successfully extracted WSDL archive. Location: " + extractedLocation);
    }
    WSDLProcessor processor;
    try {
        processor = getWSDLProcessor(extractedLocation);
    } catch (APIManagementException e) {
        return handleExceptionDuringValidation(e);
    }
    WSDLValidationResponse wsdlValidationResponse = new WSDLValidationResponse();
    if (processor.hasError()) {
        wsdlValidationResponse.setValid(false);
        wsdlValidationResponse.setError(processor.getError());
    } else {
        wsdlValidationResponse.setValid(true);
        WSDLArchiveInfo wsdlArchiveInfo = new WSDLArchiveInfo(path, APIConstants.WSDL_ARCHIVE_ZIP_FILE, processor.getWsdlInfo());
        wsdlValidationResponse.setWsdlArchiveInfo(wsdlArchiveInfo);
        wsdlValidationResponse.setWsdlInfo(processor.getWsdlInfo());
        wsdlValidationResponse.setWsdlProcessor(processor);
    }
    return wsdlValidationResponse;
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) WSDLArchiveInfo(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLArchiveInfo)

Example 68 with Wsdl

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

the class APIMWSDLReader method getWSDL.

/**
 * Gets WSDL definition as a byte array
 *
 * @return converted WSDL definition as byte array
 * @throws APIManagementException
 */
@Deprecated
public byte[] getWSDL() throws APIManagementException {
    try {
        Definition wsdlDefinition = readWSDLFile();
        WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        writer.writeWSDL(wsdlDefinition, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    } catch (Exception e) {
        String msg = "Error occurs when change the address URL of the WSDL";
        throw new APIManagementException(msg, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Definition(javax.wsdl.Definition) WSDLWriter(javax.wsdl.xml.WSDLWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) APIUtil.handleException(org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) WSDLException(javax.wsdl.WSDLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 69 with Wsdl

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

the class APIMWSDLReader method updateWSDL2.

/**
 * Update WSDL 2.0 service definitions saved in registry
 *
 * @param wsdl 	byte array of wsdl definition saved in registry
 * @param api 	API object
 * @return 		the OMElemnt of the new WSDL content
 * @throws APIManagementException
 */
public OMElement updateWSDL2(byte[] wsdl, API api) throws APIManagementException {
    try {
        // Generate wsdl document from registry data
        DocumentBuilderFactory factory = getSecuredDocumentBuilder();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.apache.woden.WSDLFactory wsdlFactory = org.apache.woden.WSDLFactory.newInstance();
        org.apache.woden.WSDLReader reader = wsdlFactory.newWSDLReader();
        reader.setFeature(org.apache.woden.WSDLReader.FEATURE_VALIDATION, false);
        Document dom = builder.parse(new ByteArrayInputStream(wsdl));
        Element domElement = dom.getDocumentElement();
        WSDLSource wsdlSource = reader.createWSDLSource();
        wsdlSource.setSource(domElement);
        org.apache.woden.wsdl20.Description wsdlDefinition = reader.readWSDL(wsdlSource);
        // Update transports
        setServiceDefinitionForWSDL2(wsdlDefinition, api);
        org.apache.woden.WSDLWriter writer = org.apache.woden.WSDLFactory.newInstance().newWSDLWriter();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        writer.writeWSDL(wsdlDefinition.toElement(), byteArrayOutputStream);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        return APIUtil.buildOMElement(byteArrayInputStream);
    } catch (Exception e) {
        String msg = " Error occurs when updating WSDL ";
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) EndpointElement(org.apache.woden.wsdl20.xml.EndpointElement) OMElement(org.apache.axiom.om.OMElement) UnknownExtensibilityElement(javax.wsdl.extensions.UnknownExtensibilityElement) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) APIUtil.handleException(org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) WSDLException(javax.wsdl.WSDLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) WSDLSource(org.apache.woden.WSDLSource)

Example 70 with Wsdl

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

the class APIMWSDLReader method getWSDL.

/**
 * Gets WSDL definition as a byte array given the WSDL definition
 *
 * @param wsdlDefinition generated WSDL definition
 * @return converted WSDL definition as byte array
 * @throws APIManagementException
 */
public byte[] getWSDL(Definition wsdlDefinition) throws APIManagementException {
    try {
        WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        writer.writeWSDL(wsdlDefinition, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    } catch (WSDLException e) {
        throw new APIManagementException("Error occurs when change the address URL of the WSDL", e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) WSDLWriter(javax.wsdl.xml.WSDLWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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