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