Search in sources :

Example 56 with Wsdl

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

the class APIProviderImpl method createNewAPIVersion.

public API createNewAPIVersion(String existingApiId, String newVersion, Boolean isDefaultVersion, String organization) throws APIManagementException {
    API existingAPI = getAPIbyUUID(existingApiId, organization);
    if (existingAPI == null) {
        throw new APIMgtResourceNotFoundException("API not found for id " + existingApiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, existingApiId));
    }
    if (newVersion.equals(existingAPI.getId().getVersion())) {
        throw new APIMgtResourceAlreadyExistsException("Version " + newVersion + " exists for api " + existingAPI.getId().getApiName());
    }
    existingAPI.setOrganization(organization);
    APIIdentifier existingAPIId = existingAPI.getId();
    String existingAPICreatedTime = existingAPI.getCreatedTime();
    String existingAPIStatus = existingAPI.getStatus();
    boolean isExsitingAPIdefaultVersion = existingAPI.isDefaultVersion();
    String existingContext = existingAPI.getContext();
    String existingVersionTimestamp = existingAPI.getVersionTimestamp();
    APIIdentifier newApiId = new APIIdentifier(existingAPI.getId().getProviderName(), existingAPI.getId().getApiName(), newVersion);
    existingAPI.setUuid(null);
    existingAPI.setId(newApiId);
    existingAPI.setStatus(APIConstants.CREATED);
    existingAPI.setDefaultVersion(isDefaultVersion);
    existingAPI.setVersionTimestamp("");
    // We need to change the context by setting the new version
    // This is a change that is coming with the context version strategy
    String existingAPIContextTemplate = existingAPI.getContextTemplate();
    existingAPI.setContext(existingAPIContextTemplate.replace("{version}", newVersion));
    Map<String, List<OperationPolicy>> operationPoliciesMap = extractAndDropOperationPoliciesFromURITemplate(existingAPI.getUriTemplates());
    API newAPI = addAPI(existingAPI);
    String newAPIId = newAPI.getUuid();
    if (!operationPoliciesMap.isEmpty()) {
        // clone common or API specific operation policy.
        Map<String, String> clonedOperationPolicyMap = cloneOperationPoliciesToAPI(existingApiId, newAPI, operationPoliciesMap);
        // attach policy to uri template.
        attachOperationPoliciesToAPI(newAPI, clonedOperationPolicyMap, operationPoliciesMap);
    }
    // copy docs
    List<Documentation> existingDocs = getAllDocumentation(existingApiId, organization);
    if (existingDocs != null) {
        for (Documentation documentation : existingDocs) {
            Documentation newDoc = addDocumentation(newAPIId, documentation, organization);
            DocumentationContent content = getDocumentationContent(existingApiId, documentation.getId(), // TODO see whether we can optimize this
            organization);
            if (content != null) {
                addDocumentationContent(newAPIId, newDoc.getId(), organization, content);
            }
        }
    }
    // copy icon
    ResourceFile icon = getIcon(existingApiId, organization);
    if (icon != null) {
        setThumbnailToAPI(newAPIId, icon, organization);
    }
    // copy sequences
    List<Mediation> mediationPolicies = getAllApiSpecificMediationPolicies(existingApiId, organization);
    if (mediationPolicies != null) {
        for (Mediation mediation : mediationPolicies) {
            Mediation policy = getApiSpecificMediationPolicyByPolicyId(existingApiId, mediation.getUuid(), organization);
            addApiSpecificMediationPolicy(newAPIId, policy, organization);
        }
    }
    // copy wsdl
    if (!APIConstants.API_TYPE_SOAPTOREST.equals(existingAPI.getType()) && existingAPI.getWsdlUrl() != null) {
        ResourceFile wsdl = getWSDL(existingApiId, organization);
        if (wsdl != null) {
            addWSDLResource(newAPIId, wsdl, null, organization);
        }
    }
    // copy graphql definition
    String graphQLSchema = getGraphqlSchemaDefinition(existingApiId, organization);
    if (graphQLSchema != null) {
        saveGraphqlSchemaDefinition(newAPIId, graphQLSchema, organization);
    }
    // update old api
    // revert back to old values before update.
    existingAPI.setUuid(existingApiId);
    existingAPI.setStatus(existingAPIStatus);
    existingAPI.setId(existingAPIId);
    existingAPI.setContext(existingContext);
    existingAPI.setCreatedTime(existingAPICreatedTime);
    // update existing api with the original timestamp
    existingAPI.setVersionTimestamp(existingVersionTimestamp);
    if (isDefaultVersion) {
        existingAPI.setDefaultVersion(false);
    } else {
        existingAPI.setDefaultVersion(isExsitingAPIdefaultVersion);
    }
    try {
        apiPersistenceInstance.updateAPI(new Organization(organization), APIMapper.INSTANCE.toPublisherApi(existingAPI));
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Error while updating API details", e);
    }
    return getAPIbyUUID(newAPIId, organization);
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ArrayList(java.util.ArrayList) List(java.util.List)

Example 57 with Wsdl

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

the class APIUtil method saveWSDLResource.

/**
 * Save the provided wsdl archive file to the registry for the api
 *
 * @param registry Governance Registry space to save the WSDL
 * @param api      API instance
 * @return
 * @throws RegistryException
 * @throws APIManagementException
 */
public static String saveWSDLResource(Registry registry, API api) throws RegistryException, APIManagementException {
    ResourceFile wsdlResource = api.getWsdlResource();
    String wsdlResourcePath;
    boolean isZip = false;
    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 wsdlResourcePathArchive = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + api.getId().getProviderName() + APIConstants.WSDL_PROVIDER_SEPERATOR + api.getId().getApiName() + api.getId().getVersion() + APIConstants.ZIP_FILE_EXTENSION;
    String wsdlResourcePathFile = apiSourcePath + RegistryConstants.PATH_SEPARATOR + createWsdlFileName(api.getId().getProviderName(), api.getId().getApiName(), api.getId().getVersion());
    if (wsdlResource.getContentType().equals(APIConstants.APPLICATION_ZIP)) {
        wsdlResourcePath = wsdlResourcePathArchive;
        isZip = true;
    } else {
        wsdlResourcePath = wsdlResourcePathFile;
    }
    String absoluteWSDLResourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + wsdlResourcePath;
    try {
        Resource wsdlResourceToUpdate = registry.newResource();
        wsdlResourceToUpdate.setContentStream(api.getWsdlResource().getContent());
        wsdlResourceToUpdate.setMediaType(api.getWsdlResource().getContentType());
        registry.put(wsdlResourcePath, wsdlResourceToUpdate);
        String[] visibleRoles = null;
        if (api.getVisibleRoles() != null) {
            visibleRoles = api.getVisibleRoles().split(",");
        }
        setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), visibleRoles, wsdlResourcePath);
        if (isZip) {
            // Delete any WSDL file if exists
            if (registry.resourceExists(wsdlResourcePathFile)) {
                registry.delete(wsdlResourcePathFile);
            }
        } else {
            // Delete any WSDL archives if exists
            if (registry.resourceExists(wsdlResourcePathArchive)) {
                registry.delete(wsdlResourcePathArchive);
            }
        }
        api.setWsdlUrl(getRegistryResourceHTTPPermlink(absoluteWSDLResourcePath));
    } catch (RegistryException e) {
        String msg = "Failed to add WSDL Archive " + api.getWsdlUrl() + " to the registry";
        log.error(msg, e);
        throw new RegistryException(msg, e);
    } catch (APIManagementException e) {
        String msg = "Failed to process the WSDL Archive: " + api.getWsdlUrl();
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    }
    return wsdlResourcePath;
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)

Example 58 with Wsdl

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

the class APIMWSDLReader method updateWSDL.

/**
 * Update WSDL 1.0 service definitions saved in registry
 *
 * @param wsdl 	byte array of registry content
 * @param api 	API object
 * @return 		the OMElemnt of the new WSDL content
 * @throws APIManagementException
 */
public OMElement updateWSDL(byte[] wsdl, API api) throws APIManagementException {
    try {
        // Generate wsdl document from registry data
        WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
        // switch off the verbose mode
        wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
        wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
        if (wsdlReader instanceof WSDLReaderImpl) {
            ((WSDLReaderImpl) wsdlReader).setIgnoreSchemaContent(true);
        }
        Definition wsdlDefinition = wsdlReader.readWSDL(null, getSecuredParsedDocumentFromContent(wsdl));
        // Update transports
        setServiceDefinition(wsdlDefinition, api);
        WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        writer.writeWSDL(wsdlDefinition, byteArrayOutputStream);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        return APIUtil.buildOMElement(byteArrayInputStream);
    } catch (Exception e) {
        String msg = " Error occurs when updating WSDL ";
        log.error(msg);
        throw new APIManagementException(msg, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) Definition(javax.wsdl.Definition) WSDLWriter(javax.wsdl.xml.WSDLWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WSDLReader(javax.wsdl.xml.WSDLReader) WSDLReaderImpl(com.ibm.wsdl.xml.WSDLReaderImpl) 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 59 with Wsdl

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

the class APIMWSDLReader method getWSDLProcessor.

/**
 * Gets WSDL processor WSDL 1.1/WSDL 2.0 based on the content {@code content}.
 *
 * @param content WSDL content
 * @return {@link WSDLProcessor}
 * @throws APIManagementException
 */
public static WSDLProcessor getWSDLProcessor(byte[] content) throws APIManagementException {
    WSDLProcessor wsdl11Processor = new WSDL11ProcessorImpl();
    WSDLProcessor wsdl20Processor = new WSDL20ProcessorImpl();
    try {
        if (wsdl11Processor.canProcess(content)) {
            wsdl11Processor.init(content);
            return wsdl11Processor;
        } else if (wsdl20Processor.canProcess(content)) {
            wsdl20Processor.init(content);
            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)

Example 60 with Wsdl

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

the class APIMWSDLReader method validateBaseURI.

/**
 * Validate the base URI of the WSDL reader
 *
 * @throws APIManagementException When error occurred while parsing the content from the URL
 */
@Deprecated
public void validateBaseURI() throws APIManagementException {
    if (baseURI.startsWith(APIConstants.WSDL_REGISTRY_LOCATION_PREFIX)) {
        baseURI = APIUtil.getServerURL() + baseURI;
    }
    boolean isWsdl20 = false;
    boolean isWsdl11 = false;
    BufferedReader in = null;
    try {
        String inputLine;
        StringBuilder urlContent = new StringBuilder();
        URL wsdl = new URL(baseURI);
        in = new BufferedReader(new InputStreamReader(wsdl.openStream(), Charset.defaultCharset()));
        while ((inputLine = in.readLine()) != null) {
            urlContent.append(inputLine);
            isWsdl20 = urlContent.indexOf(WSDL20_NAMESPACE) > 0;
            isWsdl11 = urlContent.indexOf(WSDL11_NAMESPACE) > 0;
        }
    } catch (IOException e) {
        throw new APIManagementException("Error while reading WSDL from base URI " + baseURI, e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    try {
        if (isWsdl11) {
            readAndValidateWSDL11();
        } else if (isWsdl20) {
            readAndValidateWSDL20();
        } else {
            throw new APIManagementException("URL is not in format of wsdl1.1 or wsdl2.0");
        }
    } catch (WSDLException e) {
        throw new APIManagementException("Error while parsing WSDL content", e);
    } catch (org.apache.woden.WSDLException e) {
        throw new APIManagementException("Error while parsing WSDL content", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL)

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