Search in sources :

Example 16 with WSDLProcessor

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

the class APIPublisherImpl method updateAPIWSDL.

@Override
public String updateAPIWSDL(String apiId, InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
    byte[] wsdlContent;
    try {
        wsdlContent = IOUtils.toByteArray(inputStream);
        WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
        if (!processor.canProcess()) {
            throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
        }
        if (log.isDebugEnabled()) {
            log.debug("Successfully validated the content of WSDL. API uuid: " + apiId);
        }
        getApiDAO().addOrUpdateWSDL(apiId, wsdlContent, getUsername());
        if (log.isDebugEnabled()) {
            log.debug("Successfully added WSDL to the DB. API uuid: " + apiId);
        }
        return new String(wsdlContent, APIMgtConstants.ENCODING_UTF_8);
    } catch (IOException e) {
        throw new APIMgtWSDLException("Error while updating WSDL of API " + apiId, e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    }
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) IOException(java.io.IOException)

Example 17 with WSDLProcessor

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

the class APIProviderImpl method addWSDLResource.

@Override
public void addWSDLResource(String apiId, ResourceFile resource, String url, String organization) throws APIManagementException {
    if (!StringUtils.isEmpty(url)) {
        URL wsdlUrl;
        try {
            wsdlUrl = new URL(url);
        } catch (MalformedURLException e) {
            throw new APIManagementException("Invalid/Malformed WSDL URL : " + url, e, ExceptionCodes.INVALID_WSDL_URL_EXCEPTION);
        }
        // Get the WSDL 1.1 or 2.0 processor and process the content based on the version
        WSDLProcessor wsdlProcessor = APIMWSDLReader.getWSDLProcessorForUrl(wsdlUrl);
        InputStream wsdlContent = wsdlProcessor.getWSDL();
        // wsdlResource.setContentStream(wsdlContent);
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(wsdlContent, null);
        try {
            apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
        } catch (WSDLPersistenceException e) {
            throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
        }
    } else if (resource != null) {
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(resource.getContent(), resource.getContentType());
        try {
            apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
        } catch (WSDLPersistenceException e) {
            throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) URL(java.net.URL) WSDLProcessor(org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 18 with WSDLProcessor

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

the class APIUtil method createWSDL.

/**
 * Crate an WSDL from given wsdl url. Reset the endpoint details to gateway node
 * *
 *
 * @param registry - Governance Registry space to save the WSDL
 * @param api      -API instance
 * @return Path of the created resource
 * @throws APIManagementException If an error occurs while adding the WSDL
 */
public static String createWSDL(Registry registry, API api) throws RegistryException, APIManagementException {
    try {
        APIIdentifier apiId = api.getId();
        String apiPath = APIUtil.getAPIPath(apiId);
        int prependIndex = apiPath.indexOf(apiId.getVersion()) + apiId.getVersion().length();
        String apiSourcePath = apiPath.substring(0, prependIndex);
        String wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + createWsdlFileName(api.getId().getProviderName(), api.getId().getApiName(), api.getId().getVersion());
        String absoluteWSDLResourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + wsdlResourcePath;
        APIMWSDLReader wsdlReader = new APIMWSDLReader();
        OMElement wsdlContentEle;
        String wsdlRegistryPath;
        String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        if (org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
            wsdlRegistryPath = RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
        } else {
            wsdlRegistryPath = "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
        }
        Resource wsdlResource = registry.newResource();
        // Hence if this is a registry url, should not go in to the following if block
        if (!api.getWsdlUrl().matches(wsdlRegistryPath) && (api.getWsdlUrl().startsWith("http:") || api.getWsdlUrl().startsWith("https:") || api.getWsdlUrl().startsWith("file:") || api.getWsdlUrl().startsWith("/t"))) {
            URL wsdlUrl;
            try {
                wsdlUrl = new URL(api.getWsdlUrl());
            } catch (MalformedURLException e) {
                throw new APIManagementException("Invalid/Malformed WSDL URL : " + api.getWsdlUrl(), e, ExceptionCodes.INVALID_WSDL_URL_EXCEPTION);
            }
            // Get the WSDL 1.1 or 2.0 processor and process the content based on the version
            WSDLProcessor wsdlProcessor = APIMWSDLReader.getWSDLProcessorForUrl(wsdlUrl);
            InputStream wsdlContent = wsdlProcessor.getWSDL();
            wsdlResource.setContentStream(wsdlContent);
        } else {
            byte[] wsdl = (byte[]) registry.get(wsdlResourcePath).getContent();
            if (isWSDL2Resource(wsdl)) {
                wsdlContentEle = wsdlReader.updateWSDL2(wsdl, api);
                wsdlResource.setContent(wsdlContentEle.toString());
            } else {
                wsdlContentEle = wsdlReader.updateWSDL(wsdl, api);
                wsdlResource.setContent(wsdlContentEle.toString());
            }
        }
        registry.put(wsdlResourcePath, wsdlResource);
        // set the anonymous role for wsld resource to avoid basicauth security.
        String[] visibleRoles = null;
        if (api.getVisibleRoles() != null) {
            visibleRoles = api.getVisibleRoles().split(",");
        }
        setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), visibleRoles, wsdlResourcePath);
        // Delete any WSDL archives if exists
        String wsdlArchivePath = APIUtil.getWsdlArchivePath(api.getId());
        if (registry.resourceExists(wsdlArchivePath)) {
            registry.delete(wsdlArchivePath);
        }
        // set the wsdl resource permlink as the wsdlURL.
        api.setWsdlUrl(getRegistryResourceHTTPPermlink(absoluteWSDLResourcePath));
        return wsdlRegistryPath;
    } catch (RegistryException e) {
        String msg = "Failed to add WSDL " + api.getWsdlUrl() + " to the registry";
        log.error(msg, e);
        throw new RegistryException(msg, e);
    } catch (APIManagementException e) {
        String msg = "Failed to process the WSDL : " + api.getWsdlUrl();
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) OMElement(org.apache.axiom.om.OMElement) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint) URL(java.net.URL) WSDLProcessor(org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 19 with WSDLProcessor

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

the class APIMWSDLReader method getWSDLProcessorForUrl.

/**
 * Returns the appropriate WSDL 1.1/WSDL 2.0 based on the url {@code url}.
 *
 * @param url WSDL url
 * @return WSDL 1.1/2.0 processor for the provided content
 * @throws APIManagementException If an error occurs while determining the processor
 */
public static WSDLProcessor getWSDLProcessorForUrl(URL url) throws APIManagementException {
    WSDLProcessor wsdl11Processor = new WSDL11ProcessorImpl();
    WSDLProcessor wsdl20Processor = new WSDL20ProcessorImpl();
    try {
        if (wsdl11Processor.canProcess(url)) {
            wsdl11Processor.init(url);
            return wsdl11Processor;
        } else if (wsdl20Processor.canProcess(url)) {
            wsdl20Processor.init(url);
            return wsdl20Processor;
        } else {
            // no processors found if this line reaches
            throw new APIManagementException("No WSDL processor found to process WSDL url: " + url, ExceptionCodes.URL_NOT_RECOGNIZED_AS_WSDL);
        }
    } catch (APIMgtWSDLException e) {
        throw new APIManagementException("Error while instantiating wsdl processor class", e);
    }
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDL20ProcessorImpl(org.wso2.carbon.apimgt.impl.wsdl.WSDL20ProcessorImpl) WSDL11ProcessorImpl(org.wso2.carbon.apimgt.impl.wsdl.WSDL11ProcessorImpl)

Example 20 with WSDLProcessor

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

the class APIMWSDLReader method getWSDLProcessor.

/**
 * Returns the appropriate WSDL 1.1/WSDL 2.0 based on the file path {@code wsdlPath}.
 *
 * @param wsdlPath File path containing WSDL files and dependant files
 * @return WSDL 1.1/2.0 processor for the provided content
 * @throws APIManagementException If an error occurs while determining the processor
 */
public static WSDLProcessor getWSDLProcessor(String wsdlPath) throws APIManagementException {
    WSDLProcessor wsdl11Processor = new WSDL11ProcessorImpl();
    WSDLProcessor wsdl20Processor = new WSDL20ProcessorImpl();
    byte[] wsdlContent;
    APIMWSDLReader wsdlReader;
    if (wsdlPath.endsWith(".wsdl") || wsdlPath.endsWith("?wsdl")) {
        wsdlReader = new APIMWSDLReader(wsdlPath);
        wsdlContent = wsdlReader.getWSDL();
        return getWSDLProcessor(wsdlContent);
    } else {
        try {
            if (wsdl11Processor.canProcess(wsdlPath)) {
                wsdl11Processor.initPath(wsdlPath);
                return wsdl11Processor;
            } else if (wsdl20Processor.canProcess(wsdlPath)) {
                wsdl20Processor.initPath(wsdlPath);
                return wsdl20Processor;
            } else {
                // no processors found if this line reaches
                throw new APIManagementException("No WSDL processor found to process WSDL content.", ExceptionCodes.CONTENT_NOT_RECOGNIZED_AS_WSDL);
            }
        } catch (APIMgtWSDLException e) {
            throw new APIManagementException("Error while instantiating wsdl processor class", e);
        }
    }
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDL20ProcessorImpl(org.wso2.carbon.apimgt.impl.wsdl.WSDL20ProcessorImpl) WSDL11ProcessorImpl(org.wso2.carbon.apimgt.impl.wsdl.WSDL11ProcessorImpl)

Aggregations

WSDLProcessor (org.wso2.carbon.apimgt.core.api.WSDLProcessor)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)8 WSDLProcessor (org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor)8 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 WSDLValidationResponse (org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 WSDLArchiveInfo (org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo)3 WSDL11ProcessorImpl (org.wso2.carbon.apimgt.impl.wsdl.WSDL11ProcessorImpl)3 WSDL20ProcessorImpl (org.wso2.carbon.apimgt.impl.wsdl.WSDL20ProcessorImpl)3 APIMgtWSDLException (org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Test (org.testng.annotations.Test)2 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)2 APINotFoundException (org.wso2.carbon.apimgt.core.exception.APINotFoundException)2 LabelException (org.wso2.carbon.apimgt.core.exception.LabelException)2 API (org.wso2.carbon.apimgt.core.models.API)2 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)2