Search in sources :

Example 71 with ResourcePath

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

the class AbstractAPIManager method getWsdlById.

/**
 * Return Wsdl specify by identifier
 *
 * @param wsdlId uuid of the wsdl resource
 * @return A Wsdl object related to the given identifier or null
 * @throws APIManagementException If failed to get specified wsdl
 */
@Override
public Wsdl getWsdlById(String wsdlId) throws APIManagementException {
    Wsdl wsdl = null;
    // Get registry resource correspond to identifier
    Resource wsdlResource = this.getWsdlResourceFromUuid(wsdlId);
    if (wsdlResource != null) {
        try {
            // extracting content stream of wsdl in to  string
            String contentString = IOUtils.toString(wsdlResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
            wsdl = new Wsdl();
            String resourcePath = wsdlResource.getPath();
            String wsdlName = resourcePath.substring(resourcePath.lastIndexOf("/") + 1);
            wsdl.setUuid(wsdlResource.getUUID());
            wsdl.setName(wsdlName);
            wsdl.setConfig(contentString);
        } catch (RegistryException e) {
            log.error("Error occurred while getting content stream of the wsdl " + wsdlResource.getPath(), e);
        } catch (IOException e) {
            log.error("Error occurred while converting content stream of wsdl " + wsdlResource.getPath() + " into string ", e);
        }
    }
    return wsdl;
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) IOException(java.io.IOException) Wsdl(org.wso2.carbon.apimgt.api.model.Wsdl) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 72 with ResourcePath

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

the class AbstractAPIManager method getGlobalMediationPolicy.

/**
 * Return mediation policy corresponds to the given identifier
 *
 * @param mediationPolicyId uuid of the registry resource
 * @return Mediation object related to the given identifier or null
 * @throws APIManagementException If failed to get specified mediation policy
 */
@Override
public Mediation getGlobalMediationPolicy(String mediationPolicyId) throws APIManagementException {
    Mediation mediation = null;
    // Get registry resource correspond to identifier
    Resource mediationResource = this.getCustomMediationResourceFromUuid(mediationPolicyId);
    if (mediationResource != null) {
        // Get mediation config details
        try {
            // extracting content stream of mediation policy in to  string
            String contentString = IOUtils.toString(mediationResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
            // Get policy name from the mediation config
            OMElement omElement = AXIOMUtil.stringToOM(contentString);
            OMAttribute attribute = omElement.getAttribute(new QName(PolicyConstants.MEDIATION_NAME_ATTRIBUTE));
            String mediationPolicyName = attribute.getAttributeValue();
            mediation = new Mediation();
            mediation.setUuid(mediationResource.getUUID());
            mediation.setName(mediationPolicyName);
            String resourcePath = mediationResource.getPath();
            // Extracting mediation type from the registry resource path
            String[] path = resourcePath.split(RegistryConstants.PATH_SEPARATOR);
            String resourceType = path[(path.length - 2)];
            mediation.setType(resourceType);
            mediation.setConfig(contentString);
        } catch (RegistryException e) {
            log.error("Error occurred while getting content stream of the ,mediation policy ", e);
        } catch (IOException e) {
            log.error("Error occurred while converting content stream of mediation policy " + "into string ", e);
        } catch (XMLStreamException e) {
            log.error("Error occurred while getting omElement out of mediation content ", e);
        }
    }
    return mediation;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) OMElement(org.apache.axiom.om.OMElement) IOException(java.io.IOException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) OMAttribute(org.apache.axiom.om.OMAttribute) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 73 with ResourcePath

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

the class AbstractAPIManager method updateWsdl.

/**
 * Update a existing wsdl in the path specified
 *
 * @param resourcePath   Registry path of the resource
 * @param wsdlDefinition wsdl content
 */
@Override
public void updateWsdl(String resourcePath, String wsdlDefinition) throws APIManagementException {
    try {
        Resource resource = registry.get(resourcePath);
        resource.setContent(wsdlDefinition);
        registry.put(resourcePath, resource);
    } catch (RegistryException e) {
        String msg = "Error while updating the existing wsdl ";
        throw new APIManagementException(msg, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 74 with ResourcePath

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

the class AbstractAPIManager method getAPIDefinitionOfAPIProduct.

@Override
public String getAPIDefinitionOfAPIProduct(APIProduct product) throws APIManagementException {
    String resourcePath = APIUtil.getAPIProductOpenAPIDefinitionFilePath(product.getId());
    JSONParser parser = new JSONParser();
    String apiDocContent = null;
    try {
        if (registry.resourceExists(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME)) {
            Resource apiDocResource = registry.get(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME);
            apiDocContent = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
            parser.parse(apiDocContent);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Resource " + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME + " not found at " + resourcePath);
            }
        }
    } catch (RegistryException e) {
        handleException("Error while retrieving OpenAPI v2.0 or v3.0.0 Definition for " + product.getId().getName() + '-' + product.getId().getProviderName(), e);
    } catch (ParseException e) {
        handleException("Error while parsing OpenAPI v2.0 or v3.0.0 Definition for " + product.getId().getName() + '-' + product.getId().getProviderName() + " in " + resourcePath, e);
    }
    return apiDocContent;
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 75 with ResourcePath

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

the class AbstractAPIManager method getDocPaths.

/**
 * Get API Documents within the provided registry collection
 * In case the document names contained '/' character, need to get only leaf node documents within them
 *
 * @param docCollection          registry collection
 * @param apiOrAPIProductDocPath base api/api product document path
 * @return
 * @throws APIManagementException
 */
private List<String> getDocPaths(org.wso2.carbon.registry.core.Collection docCollection, String apiOrAPIProductDocPath) throws APIManagementException {
    List<String> docPaths = new ArrayList<>();
    String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
    String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
    try {
        String[] resourcePaths = docCollection.getChildren();
        for (String resourcePath : resourcePaths) {
            if (!(resourcePath.equals(pathToContent) || resourcePath.equals(pathToDocFile))) {
                Resource resource = registry.get(resourcePath);
                if (resource instanceof org.wso2.carbon.registry.core.Collection) {
                    docPaths.addAll(getDocPaths((org.wso2.carbon.registry.core.Collection) resource, apiOrAPIProductDocPath));
                } else {
                    docPaths.add(resourcePath);
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Failed to get documents for api/product";
        throw new APIManagementException(msg, e);
    }
    return docPaths;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

Resource (org.wso2.carbon.registry.core.Resource)51 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)48 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)44 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)28 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)25 IOException (java.io.IOException)18 Registry (org.wso2.carbon.registry.core.Registry)16 Collection (org.wso2.carbon.registry.core.Collection)15 UserStoreException (org.wso2.carbon.user.api.UserStoreException)14 Test (org.junit.Test)13 Resource (org.wso2.carbon.registry.api.Resource)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 ArrayList (java.util.ArrayList)11 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)11 RegistryException (org.wso2.carbon.registry.api.RegistryException)11 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)11 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)11 JSONParser (org.json.simple.parser.JSONParser)10 ParseException (org.json.simple.parser.ParseException)10 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)10