Search in sources :

Example 1 with APIIdentifier

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

the class AbstractAPIManager method getAllDocumentation.

public List<Documentation> getAllDocumentation(Identifier id) throws APIManagementException {
    List<Documentation> documentationList = new ArrayList<Documentation>();
    String docArtifactKeyType = StringUtils.EMPTY;
    docArtifactKeyType = APIConstants.DOCUMENTATION_KEY;
    String apiOrAPIProductDocPath;
    APIRevision apiRevision = apiMgtDAO.checkAPIUUIDIsARevisionUUID(id.getUUID());
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        apiOrAPIProductDocPath = APIUtil.getAPIOrAPIProductRevisionDocPath(apiRevision.getApiUUID(), apiRevision.getId());
    } else {
        apiOrAPIProductDocPath = APIUtil.getAPIOrAPIProductDocPath(id);
    }
    String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
    String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
    try {
        if (registry.resourceExists(apiOrAPIProductDocPath)) {
            Resource resource = registry.get(apiOrAPIProductDocPath);
            if (resource instanceof org.wso2.carbon.registry.core.Collection) {
                List<String> docPaths = getDocPaths((org.wso2.carbon.registry.core.Collection) resource, apiOrAPIProductDocPath);
                for (String docPath : docPaths) {
                    if (!(docPath.equalsIgnoreCase(pathToContent) || docPath.equalsIgnoreCase(pathToDocFile))) {
                        Resource docResource = registry.get(docPath);
                        GenericArtifactManager artifactManager = getAPIGenericArtifactManager(registry, docArtifactKeyType);
                        GenericArtifact docArtifact = artifactManager.getGenericArtifact(docResource.getUUID());
                        Documentation doc = APIUtil.getDocumentation(docArtifact);
                        Date contentLastModifiedDate;
                        Date docLastModifiedDate = docResource.getLastModified();
                        if (Documentation.DocumentSourceType.INLINE.equals(doc.getSourceType()) || Documentation.DocumentSourceType.MARKDOWN.equals(doc.getSourceType())) {
                            String contentPath = StringUtils.EMPTY;
                            if (id instanceof APIIdentifier) {
                                contentPath = APIUtil.getAPIDocContentPath((APIIdentifier) id, doc.getName());
                            } else if (id instanceof APIProductIdentifier) {
                                contentPath = APIUtil.getProductDocContentPath((APIProductIdentifier) id, doc.getName());
                            }
                            contentLastModifiedDate = registry.get(contentPath).getLastModified();
                            doc.setLastUpdated((contentLastModifiedDate.after(docLastModifiedDate) ? contentLastModifiedDate : docLastModifiedDate));
                        } else {
                            doc.setLastUpdated(docLastModifiedDate);
                        }
                        documentationList.add(doc);
                    }
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Failed to get documentations for api/product " + id.getName();
        throw new APIManagementException(msg, e);
    }
    return documentationList;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Date(java.util.Date) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 2 with APIIdentifier

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

the class AbstractAPIManager method getSwaggerDefinitionTimeStamps.

/**
 * gets the swagger definition timestamps as a map
 *
 * @param apiIdentifier
 * @return
 * @throws APIManagementException
 */
public Map<String, String> getSwaggerDefinitionTimeStamps(APIIdentifier apiIdentifier) throws APIManagementException {
    String apiTenantDomain = getTenantDomain(apiIdentifier);
    try {
        Registry registryType;
        // Tenant store anonymous mode if current tenant and the required tenant is not matching
        if (this.tenantDomain == null || isTenantDomainNotMatching(apiTenantDomain)) {
            int tenantId = getTenantManager().getTenantId(apiTenantDomain);
            registryType = getRegistryService().getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId);
        } else {
            registryType = registry;
        }
        return OASParserUtil.getAPIOpenAPIDefinitionTimeStamps(apiIdentifier, registryType);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        log.error("Error while getting the lastUpdated time due to " + e.getMessage(), e);
    } catch (RegistryException e) {
        log.debug("Error while getting the lastUpdated time due to " + e.getMessage(), e);
    }
    return null;
}
Also used : UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 3 with APIIdentifier

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

the class AbstractAPIManager method getWSDL.

/**
 * Returns the wsdl content in registry specified by the wsdl name
 *
 * @param apiId Api Identifier
 * @return wsdl content matching name if exist else null
 */
@Override
public ResourceFile getWSDL(APIIdentifier apiId) throws APIManagementException {
    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 + APIUtil.createWsdlFileName(apiId.getProviderName(), apiId.getApiName(), apiId.getVersion());
    String wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + APIUtil.createWsdlFileName(apiId.getProviderName(), apiId.getApiName(), apiId.getVersion());
    String tenantDomain = getTenantDomain(apiId);
    boolean isTenantFlowStarted = false;
    try {
        Registry registry;
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            startTenantFlow(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(apiId.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
            } else {
                registry = this.registry;
            }
        }
        if (registry.resourceExists(wsdlResourcePath)) {
            Resource resource = registry.get(wsdlResourcePath);
            return new ResourceFile(resource.getContentStream(), resource.getMediaType());
        } else if (registry.resourceExists(wsdlResourcePathOld)) {
            Resource resource = registry.get(wsdlResourcePathOld);
            return new ResourceFile(resource.getContentStream(), resource.getMediaType());
        } else {
            wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiId.getProviderName() + APIConstants.WSDL_PROVIDER_SEPERATOR + apiId.getApiName() + apiId.getVersion() + APIConstants.ZIP_FILE_EXTENSION;
            wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiId.getProviderName() + APIConstants.WSDL_PROVIDER_SEPERATOR + apiId.getApiName() + apiId.getVersion() + APIConstants.ZIP_FILE_EXTENSION;
            if (registry.resourceExists(wsdlResourcePath)) {
                Resource resource = registry.get(wsdlResourcePath);
                return new ResourceFile(resource.getContentStream(), resource.getMediaType());
            } else if (registry.resourceExists(wsdlResourcePathOld)) {
                Resource resource = registry.get(wsdlResourcePathOld);
                return new ResourceFile(resource.getContentStream(), resource.getMediaType());
            } else {
                throw new APIManagementException("No WSDL found for the API: " + apiId, ExceptionCodes.from(ExceptionCodes.NO_WSDL_AVAILABLE_FOR_API, apiId.getApiName(), apiId.getVersion()));
            }
        }
    } catch (RegistryException | org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Error while getting wsdl file from the registry for API: " + apiId.toString();
        throw new APIManagementException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
}
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) UserStoreException(org.wso2.carbon.user.core.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 4 with APIIdentifier

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

the class AbstractAPIManager method getGraphqlSchemaDefinition.

/**
 * Returns the graphQL content in registry specified by the wsdl name
 *
 * @param apiId Api Identifier
 * @return graphQL content matching name if exist else null
 */
@Override
public String getGraphqlSchemaDefinition(APIIdentifier apiId) throws APIManagementException {
    String apiTenantDomain = getTenantDomain(apiId);
    String schema;
    try {
        Registry registryType;
        // Tenant store anonymous mode if current tenant and the required tenant is not matching
        if (this.tenantDomain == null || isTenantDomainNotMatching(apiTenantDomain)) {
            int tenantId = getTenantManager().getTenantId(apiTenantDomain);
            registryType = getRegistryService().getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId);
        } else {
            registryType = registry;
        }
        schema = schemaDef.getGraphqlSchemaDefinition(apiId, registryType);
    } catch (org.wso2.carbon.user.api.UserStoreException | RegistryException e) {
        String msg = "Failed to get graphql schema definition of Graphql API : " + apiId;
        throw new APIManagementException(msg, e);
    }
    return schema;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 5 with APIIdentifier

use of org.wso2.carbon.apimgt.api.model.APIIdentifier 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)

Aggregations

APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)305 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)171 API (org.wso2.carbon.apimgt.api.model.API)160 Test (org.junit.Test)155 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)119 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)105 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)85 Resource (org.wso2.carbon.registry.core.Resource)79 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)69 ArrayList (java.util.ArrayList)68 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)56 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)48 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)46 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)46 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)44 HashSet (java.util.HashSet)41 HashMap (java.util.HashMap)40 IOException (java.io.IOException)37 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)37 JSONObject (org.json.simple.JSONObject)36