Search in sources :

Example 51 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class ApplicationThrottleController method lookup.

private static OMNode lookup(String key, int tenantId) {
    try {
        Resource resource = getResource(key, tenantId);
        if (resource instanceof Collection || resource == null) {
            return null;
        }
        ByteArrayInputStream inputStream = null;
        Object content = resource.getContent();
        if (content instanceof String) {
            inputStream = new ByteArrayInputStream(content.toString().getBytes(Charset.defaultCharset()));
        } else if (content instanceof byte[]) {
            inputStream = new ByteArrayInputStream((byte[]) content);
        }
        OMNode result = null;
        try {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
            XMLStreamReader parser = factory.createXMLStreamReader(inputStream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            result = builder.getDocumentElement();
        } catch (OMException ignored) {
            result = readNonXML(resource);
        } catch (XMLStreamException ignored) {
            result = readNonXML(resource);
        } catch (Exception e) {
            // a more general exception(e.g. a Runtime exception if the XML doc has an
            // external DTD deceleration and if not connected to internet) which in case
            // just log for debugging
            log.error("Error while reading the resource '" + key + '\'', e);
        } finally {
            try {
                resource.discard();
                if (result != null && result.getParent() != null) {
                    result.detach();
                    OMDocumentImpl parent = new OMDocumentImpl(OMAbstractFactory.getOMFactory());
                    parent.addChild(result);
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                log.error("Error while closing the input stream", e);
            }
        }
        return result;
    } catch (RegistryException e) {
        handleException("Error while fetching the resource " + key, e);
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Resource(org.wso2.carbon.registry.core.Resource) OMDocumentImpl(org.apache.axiom.om.impl.llom.OMDocumentImpl) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) ThrottleException(org.apache.synapse.commons.throttle.core.ThrottleException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) Collection(org.wso2.carbon.registry.core.Collection) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 52 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException 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 53 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException 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 54 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException 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)

Example 55 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getAsyncDefinition.

@Override
public String getAsyncDefinition(Organization org, String apiId) throws AsyncSpecPersistenceException {
    String apiTenantDomain = org.getName();
    String definition = null;
    boolean tenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(apiTenantDomain);
        Registry registryType = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted;
        GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registryType, APIConstants.API_KEY);
        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiId);
        if (apiArtifact != null) {
            String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
            String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
            String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
            String apiPath = GovernanceUtils.getArtifactPath(registryType, apiId);
            int prependIndex = apiPath.lastIndexOf("/api");
            String apiSourcePath = apiPath.substring(0, prependIndex);
            String definitionPath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ASYNC_API_DEFINITION_RESOURCE_NAME;
            if (registryType.resourceExists(definitionPath)) {
                Resource apiDocResource = registryType.get(definitionPath);
                definition = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
                return definition;
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Failed to get specification of API : " + apiId;
        throw new AsyncSpecPersistenceException(msg, e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
    }
    return definition;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Resource(org.wso2.carbon.registry.core.Resource) 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)235 Resource (org.wso2.carbon.registry.core.Resource)196 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)167 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)145 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)104 Registry (org.wso2.carbon.registry.core.Registry)95 Test (org.junit.Test)81 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)81 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)67 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)61 UserStoreException (org.wso2.carbon.user.api.UserStoreException)60 API (org.wso2.carbon.apimgt.api.model.API)58 IOException (java.io.IOException)57 ArrayList (java.util.ArrayList)55 QName (javax.xml.namespace.QName)42 APIResource (org.wso2.carbon.apimgt.api.doc.model.APIResource)40 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)40 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)40