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;
}
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;
}
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;
}
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();
}
}
}
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;
}
Aggregations