Search in sources :

Example 11 with APIMWSDLReader

use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader in project carbon-apimgt by wso2.

the class WSDLSOAPOperationExtractorImplTestCase method testGetSwaggerModelForImportedSchemas.

@Test
public void testGetSwaggerModelForImportedSchemas() throws Exception {
    APIMWSDLReader wsdlReader = new APIMWSDLReader(Thread.currentThread().getContextClassLoader().getResource("wsdls/import-schemas/sampleservice.wsdl").toExternalForm());
    byte[] wsdlContent = wsdlReader.getWSDL();
    WSDL11SOAPOperationExtractor processor = SOAPOperationBindingUtils.getWSDL11SOAPOperationExtractor(wsdlContent, wsdlReader);
    Set<WSDLSOAPOperation> operations = processor.getWsdlInfo().getSoapBindingOperations();
    Assert.assertNotNull(operations);
    Map<String, ModelImpl> parameterModelMap = processor.getWsdlInfo().getParameterModelMap();
    Assert.assertNotNull(parameterModelMap);
}
Also used : APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader) WSDLSOAPOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation) ModelImpl(io.swagger.models.ModelImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 12 with APIMWSDLReader

use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader in project carbon-apimgt by wso2.

the class WSDLSOAPOperationExtractorImplTestCase method setup.

@Before
public void setup() throws Exception {
    APIMWSDLReader wsdlReader = new APIMWSDLReader(Thread.currentThread().getContextClassLoader().getResource("wsdls/phoneverify.wsdl").toExternalForm());
    byte[] wsdlContent = wsdlReader.getWSDL();
    WSDL11SOAPOperationExtractor processor = SOAPOperationBindingUtils.getWSDL11SOAPOperationExtractor(wsdlContent, wsdlReader);
    operations = processor.getWsdlInfo().getSoapBindingOperations();
}
Also used : APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader) Before(org.junit.Before)

Example 13 with APIMWSDLReader

use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader in project carbon-apimgt by wso2.

the class WSDL11SOAPOperationExtractor method loadXSDsfromNamespaces.

/**
 * Load the schemas into the list of based schemas from the namespaces.
 *
 * @param ns namespace
 * @return document
 * @throws APIManagementException
 */
public Document loadXSDsfromNamespaces(String ns) throws APIManagementException {
    Collection<File> foundXSDFiles = new java.util.LinkedList<>();
    foundXSDFiles.addAll(getStandardBaseXSDs());
    Document doc = null;
    APIMWSDLReader reader = new APIMWSDLReader(ns + ".xsd");
    for (File file : foundXSDFiles) {
        String absWSDLPath = file.getAbsolutePath();
        if (log.isDebugEnabled()) {
            log.debug("Processing xsd file: " + absWSDLPath);
        }
        doc = reader.getSecuredParsedDocument(absWSDLPath);
        Node namespace = doc.getDocumentElement().getAttributes().getNamedItem("targetNamespace");
        if (namespace != null) {
            basedSchemas.put(namespace.getNodeValue(), doc);
        }
    }
    return doc;
}
Also used : APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) File(java.io.File)

Example 14 with APIMWSDLReader

use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader in project carbon-apimgt by wso2.

the class APIConsumerImpl method getUpdatedWSDLByEnvironment.

/**
 * This method is used to get the updated wsdl with the respective environment apis are published
 *
 * @param wsdlResourcePath registry resource path to the wsdl
 * @param wsdlContent      wsdl resource content as byte array
 * @param environmentType  gateway environment type
 * @return updated wsdl content with environment endpoints
 * @throws APIManagementException
 */
private byte[] getUpdatedWSDLByEnvironment(String wsdlResourcePath, byte[] wsdlContent, String environmentName, String environmentType, String apiName, String apiVersion, String apiProvider) throws APIManagementException {
    APIMWSDLReader apimwsdlReader = new APIMWSDLReader(wsdlResourcePath);
    Definition definition = apimwsdlReader.getWSDLDefinitionFromByteContent(wsdlContent, false);
    byte[] updatedWSDLContent = null;
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(apiProvider));
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        }
        RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
        int tenantId;
        UserRegistry registry;
        try {
            tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
            APIUtil.loadTenantRegistry(tenantId);
            registry = registryService.getGovernanceSystemRegistry(tenantId);
            API api = null;
            if (!StringUtils.isEmpty(apiName) && !StringUtils.isEmpty(apiVersion)) {
                APIIdentifier apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(apiProvider), apiName, apiVersion);
                if (log.isDebugEnabled()) {
                    log.debug("Api identifier for the soap api artifact: " + apiIdentifier + "for api name: " + apiName + ", version: " + apiVersion);
                }
                GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
                api = APIUtil.getAPI(apiArtifact);
                if (log.isDebugEnabled()) {
                    if (api != null) {
                        log.debug("Api context for the artifact with id:" + api.getId() + " is " + api.getContext());
                    } else {
                        log.debug("Api does not exist for api name: " + apiIdentifier.getApiName());
                    }
                }
            } else {
                handleException("Artifact does not exist in the registry for api name: " + apiName + " and version: " + apiVersion);
            }
            if (api != null) {
                try {
                    apimwsdlReader.setServiceDefinition(definition, api, environmentName, environmentType);
                    if (log.isDebugEnabled()) {
                        log.debug("Soap api with context:" + api.getContext() + " in " + environmentName + " with environment type" + environmentType);
                    }
                    updatedWSDLContent = apimwsdlReader.getWSDL(definition);
                } catch (APIManagementException e) {
                    handleException("Error occurred while processing the wsdl for api: [" + api.getId() + "]", e);
                }
            } else {
                handleException("Error while getting API object for wsdl artifact");
            }
        } catch (UserStoreException e) {
            handleException("Error while reading tenant information", e);
        } catch (RegistryException e) {
            handleException("Error when create registry instance", e);
        }
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return updatedWSDLContent;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Definition(javax.wsdl.Definition) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader) UserStoreException(org.wso2.carbon.user.api.UserStoreException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService)

Example 15 with APIMWSDLReader

use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader 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)

Aggregations

APIMWSDLReader (org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader)11 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 ModelImpl (io.swagger.models.ModelImpl)4 Definition (javax.wsdl.Definition)3 API (org.wso2.carbon.apimgt.api.model.API)3 WSDL11SOAPOperationExtractor (org.wso2.carbon.apimgt.impl.wsdl.WSDL11SOAPOperationExtractor)3 Document (org.w3c.dom.Document)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 WSDLProcessor (org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor)2 WSDLSOAPOperation (org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation)2 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 OMElement (org.apache.axiom.om.OMElement)1