Search in sources :

Example 56 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class ExportUtils method addThumbnailToArchive.

/**
 * Retrieve thumbnail image for the exporting API or API Product and store it in the archive directory.
 *
 * @param archivePath File path to export the thumbnail image
 * @param identifier  ID of the requesting API or API Product
 * @param apiProvider API Provider
 * @throws APIImportExportException If an error occurs while retrieving image from the registry or
 *                                  storing in the archive directory
 */
public static void addThumbnailToArchive(String archivePath, Identifier identifier, APIProvider apiProvider) throws APIImportExportException, APIManagementException {
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    String localImagePath = archivePath + File.separator + ImportExportConstants.IMAGE_RESOURCE;
    try {
        ResourceFile thumbnailResource = apiProvider.getIcon(identifier.getUUID(), tenantDomain);
        if (thumbnailResource != null) {
            String mediaType = thumbnailResource.getContentType();
            String extension = ImportExportConstants.fileExtensionMapping.get(mediaType);
            if (extension != null) {
                CommonUtil.createDirectory(localImagePath);
                try (InputStream imageDataStream = thumbnailResource.getContent();
                    OutputStream outputStream = new FileOutputStream(localImagePath + File.separator + APIConstants.API_ICON_IMAGE + APIConstants.DOT + extension)) {
                    IOUtils.copy(imageDataStream, outputStream);
                    if (log.isDebugEnabled()) {
                        log.debug("Thumbnail image retrieved successfully for API/API Product: " + identifier.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + identifier.getVersion());
                    }
                }
            } else {
                // api gets imported without thumbnail
                log.error("Unsupported media type for icon " + mediaType + ". Skipping thumbnail export.");
            }
        } else if (log.isDebugEnabled()) {
            log.debug("Thumbnail URL does not exists in registry for API/API Product: " + identifier.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + identifier.getVersion() + ". Skipping thumbnail export.");
        }
    } catch (IOException e) {
        // Exception is ignored by logging due to the reason that Thumbnail is not essential for
        // an API to be recreated.
        log.error("I/O error while writing API/API Product Thumbnail to file", e);
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 57 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class ApplicationThrottleController method getResource.

private static Resource getResource(String path, int tenantId) {
    RegistryService registryService = RegistryServiceHolder.getInstance().getRegistryService();
    Registry registry = null;
    try {
        registry = registryService.getGovernanceSystemRegistry(tenantId);
    } catch (RegistryException e) {
        log.error("Error while fetching Governance Registry of Super Tenant", e);
        return null;
    }
    String key = resolvePath(path);
    try {
        if (registry.resourceExists(key)) {
            return registry.get(key);
        }
    } catch (RegistryException e) {
        handleException("Error while fetching the resource " + path, e);
    }
    return null;
}
Also used : Registry(org.wso2.carbon.registry.core.Registry) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 58 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class GraphQLSchemaDefinition 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
 */
public String getGraphqlSchemaDefinition(APIIdentifier apiId, Registry registry) throws APIManagementException {
    String apiName = apiId.getApiName();
    String apiVersion = apiId.getVersion();
    String apiProviderName = apiId.getProviderName();
    APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiId.getUUID());
    String resourcePath;
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        resourcePath = APIUtil.getRevisionPath(apiRevision.getApiUUID(), apiRevision.getId());
    } else {
        resourcePath = APIUtil.getGraphqlDefinitionFilePath(apiName, apiVersion, apiProviderName);
    }
    String schemaDoc = null;
    String schemaName = apiId.getProviderName() + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + apiId.getApiName() + apiId.getVersion() + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION;
    String schemaResourePath = resourcePath + schemaName;
    try {
        if (registry.resourceExists(schemaResourePath)) {
            Resource schemaResource = registry.get(schemaResourePath);
            schemaDoc = IOUtils.toString(schemaResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
        }
    } catch (RegistryException e) {
        String msg = "Error while getting schema file from the registry " + schemaResourePath;
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    } catch (IOException e) {
        String error = "Error occurred while getting the content of schema: " + schemaName;
        log.error(error);
        throw new APIManagementException(error, e);
    }
    return schemaDoc;
}
Also used : APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.api.Resource) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 59 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class GraphQLSchemaDefinition method extractGraphQLOperationList.

/**
 * Extract GraphQL Operations from given schema.
 *
 * @param typeRegistry graphQL Schema Type Registry
 * @param type operation type string
 * @return the arrayList of APIOperationsDTO
 */
public List<URITemplate> extractGraphQLOperationList(TypeDefinitionRegistry typeRegistry, String type) {
    List<URITemplate> operationArray = new ArrayList<>();
    Map<java.lang.String, TypeDefinition> operationList = typeRegistry.types();
    for (Map.Entry<String, TypeDefinition> entry : operationList.entrySet()) {
        Optional<SchemaDefinition> schemaDefinition = typeRegistry.schemaDefinition();
        if (schemaDefinition.isPresent()) {
            List<OperationTypeDefinition> operationTypeList = schemaDefinition.get().getOperationTypeDefinitions();
            for (OperationTypeDefinition operationTypeDefinition : operationTypeList) {
                if (entry.getValue().getName().equalsIgnoreCase(operationTypeDefinition.getTypeName().getName())) {
                    if (type == null) {
                        addOperations(entry, operationTypeDefinition.getName().toUpperCase(), operationArray);
                    } else if (type.equals(operationTypeDefinition.getName().toUpperCase())) {
                        addOperations(entry, operationTypeDefinition.getName().toUpperCase(), operationArray);
                    }
                }
            }
        } else {
            if (entry.getValue().getName().equalsIgnoreCase(APIConstants.GRAPHQL_QUERY) || entry.getValue().getName().equalsIgnoreCase(APIConstants.GRAPHQL_MUTATION) || entry.getValue().getName().equalsIgnoreCase(APIConstants.GRAPHQL_SUBSCRIPTION)) {
                if (type == null) {
                    addOperations(entry, entry.getKey(), operationArray);
                } else if (type.equals(entry.getValue().getName().toUpperCase())) {
                    addOperations(entry, entry.getKey(), operationArray);
                }
            }
        }
    }
    return operationArray;
}
Also used : SchemaDefinition(graphql.language.SchemaDefinition) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) OperationTypeDefinition(graphql.language.OperationTypeDefinition) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition)

Example 60 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method deleteThumbnail.

@Override
public void deleteThumbnail(Organization org, String apiId) throws ThumbnailPersistenceException {
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifact apiArtifact = getAPIArtifact(apiId, registry);
        if (apiArtifact == null) {
            throw new ThumbnailPersistenceException("API not found for id " + apiId, ExceptionCodes.API_NOT_FOUND);
        }
        String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
        String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        String artifactOldPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
        String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
        String oldThumbPath = artifactOldPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        if (registry.resourceExists(thumbPath)) {
            registry.delete(thumbPath);
        }
        if (registry.resourceExists(oldThumbPath)) {
            registry.delete(oldThumbPath);
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error while loading API icon of API " + apiId + " from the registry";
        throw new ThumbnailPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)230 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)225 Resource (org.wso2.carbon.registry.core.Resource)210 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)197 Registry (org.wso2.carbon.registry.core.Registry)125 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)114 Test (org.junit.Test)111 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)107 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)91 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)89 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)80 IOException (java.io.IOException)75 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)74 UserStoreException (org.wso2.carbon.user.api.UserStoreException)71 API (org.wso2.carbon.apimgt.api.model.API)70 ArrayList (java.util.ArrayList)67 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)58 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)51 HashMap (java.util.HashMap)48 OMElement (org.apache.axiom.om.OMElement)48