Search in sources :

Example 36 with Identifier

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

the class AbstractAPIManager method getIcon.

public ResourceFile getIcon(APIIdentifier identifier) throws APIManagementException {
    String artifactOldPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
    String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
    String tenantDomain = getTenantDomain(identifier);
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            startTenantFlow(tenantDomain);
            isTenantFlowStarted = true;
        }
        /* If the API provider is a tenant, load tenant registry*/
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            int id = getTenantManager().getTenantId(tenantDomain);
            registry = getRegistryService().getGovernanceSystemRegistry(id);
        } else {
            if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
                registry = getRegistryService().getGovernanceUserRegistry(identifier.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
            } else {
                registry = this.registry;
            }
        }
        String oldThumbPath = artifactOldPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        if (registry.resourceExists(thumbPath)) {
            Resource res = registry.get(thumbPath);
            return new ResourceFile(res.getContentStream(), res.getMediaType());
        } else if (registry.resourceExists(oldThumbPath)) {
            Resource res = registry.get(oldThumbPath);
            return new ResourceFile(res.getContentStream(), res.getMediaType());
        }
    } catch (RegistryException e) {
        String msg = "Error while loading API icon of API " + identifier.getApiName() + ":" + identifier.getVersion() + " from the registry";
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Error while loading API icon of API " + identifier.getApiName() + ":" + identifier.getVersion();
        throw new APIManagementException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
    return null;
}
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) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 37 with Identifier

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

the class AbstractAPIManager method getDocumentationContent.

public String getDocumentationContent(Identifier identifier, String documentationName) throws APIManagementException {
    String contentPath = StringUtils.EMPTY;
    String identifierType = StringUtils.EMPTY;
    if (identifier instanceof APIIdentifier) {
        contentPath = APIUtil.getAPIDocPath((APIIdentifier) identifier) + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + documentationName;
        identifierType = APIConstants.API_IDENTIFIER_TYPE;
    } else if (identifier instanceof APIProductIdentifier) {
        contentPath = APIUtil.getProductDocPath((APIProductIdentifier) identifier) + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + documentationName;
        identifierType = APIConstants.API_PRODUCT_IDENTIFIER_TYPE;
    }
    String tenantDomain = getTenantDomain(identifier);
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            startTenantFlow(tenantDomain);
            isTenantFlowStarted = true;
        }
        /* If the API provider is a tenant, load tenant registry*/
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            int id = getTenantManager().getTenantId(tenantDomain);
            registry = getRegistryService().getGovernanceSystemRegistry(id);
        } else {
            if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
                registry = getRegistryService().getGovernanceUserRegistry(identifier.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
            } else {
                registry = this.registry;
            }
        }
        if (registry.resourceExists(contentPath)) {
            Resource docContent = registry.get(contentPath);
            Object content = docContent.getContent();
            if (content != null) {
                return new String((byte[]) docContent.getContent(), Charset.defaultCharset());
            }
        }
    } catch (RegistryException e) {
        String msg = "No document content found for documentation: " + documentationName + " of " + identifierType + " : " + identifier.getName();
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Failed to get document content found for documentation: " + documentationName + " of " + identifierType + " : " + identifier.getName();
        throw new APIManagementException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
    return null;
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) JSONObject(org.json.simple.JSONObject)

Example 38 with Identifier

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

the class AbstractAPIManager method getOpenAPIDefinition.

/**
 * Returns the swagger 2.0 definition of the given API
 *
 * @param apiId id of the APIIdentifier
 * @param organization identifier of the organization
 * @return An String containing the swagger 2.0 definition
 * @throws APIManagementException
 */
@Override
public String getOpenAPIDefinition(Identifier apiId, String organization) throws APIManagementException {
    String definition = null;
    String id;
    if (apiId.getUUID() != null) {
        id = apiId.getUUID();
    } else {
        id = apiMgtDAO.getUUIDFromIdentifier(apiId.getProviderName(), apiId.getName(), apiId.getVersion(), organization);
    }
    try {
        definition = apiPersistenceInstance.getOASDefinition(new Organization(organization), id);
    } catch (OASPersistenceException e) {
        throw new APIManagementException("Error while retrieving OAS definition from the persistance location", e);
    }
    return definition;
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 39 with Identifier

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

the class AbstractAPIManager method isDocumentationExist.

/**
 * Checks whether the given document already exists for the given api/product
 *
 * @param identifier API/Product Identifier
 * @param docName    Name of the document
 * @return true if document already exists for the given api/product
 * @throws APIManagementException if failed to check existence of the documentation
 */
public boolean isDocumentationExist(Identifier identifier, String docName) throws APIManagementException {
    String docPath = "";
    docPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR + docName;
    try {
        return registry.resourceExists(docPath);
    } catch (RegistryException e) {
        String msg = "Failed to check existence of the document :" + docPath;
        throw new APIManagementException(msg, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 40 with Identifier

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

the class AbstractAPIManager method getCustomMediationResourceFromUuid.

/**
 * Returns the mediation policy registry resource correspond to the given identifier
 *
 * @param mediationPolicyId uuid of the mediation resource
 * @return Registry resource of given identifier or null
 * @throws APIManagementException If failed to get the registry resource of given uuid
 */
@Override
public Resource getCustomMediationResourceFromUuid(String mediationPolicyId) throws APIManagementException {
    String resourcePath = APIConstants.API_CUSTOM_SEQUENCE_LOCATION;
    try {
        Resource resource = registry.get(resourcePath);
        // resource : customsequences
        if (resource instanceof Collection) {
            Collection typeCollection = (Collection) resource;
            String[] typeArray = typeCollection.getChildren();
            for (String type : typeArray) {
                Resource typeResource = registry.get(type);
                // typeResource: in/ out/ fault
                if (typeResource instanceof Collection) {
                    String[] policyArray = ((Collection) typeResource).getChildren();
                    if (policyArray.length > 0) {
                        for (String policy : policyArray) {
                            Resource mediationResource = registry.get(policy);
                            // mediationResource: eg .log_in_msg.xml
                            String resourceId = mediationResource.getUUID();
                            if (resourceId.equals(mediationPolicyId)) {
                                // registry resource
                                return mediationResource;
                            }
                        }
                    }
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Error while accessing registry objects";
        throw new APIManagementException(msg, e);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) 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

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)118 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)83 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)66 API (org.wso2.carbon.apimgt.api.model.API)42 Resource (org.wso2.carbon.registry.core.Resource)40 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)39 Test (org.junit.Test)36 PreparedStatement (java.sql.PreparedStatement)34 SQLException (java.sql.SQLException)34 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)34 Connection (java.sql.Connection)33 UserStoreException (org.wso2.carbon.user.core.UserStoreException)31 ResultSet (java.sql.ResultSet)29 ArrayList (java.util.ArrayList)29 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)29 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)27 IOException (java.io.IOException)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)26 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)25 HumanTaskException (org.wso2.carbon.humantask.core.engine.HumanTaskException)24