use of org.wso2.carbon.apimgt.api.model.ResourceFile 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.apimgt.api.model.ResourceFile in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdThumbnailGet.
@Override
public Response apisApiIdThumbnailGet(String apiId, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) {
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
// this will fail if user does not have access to the API or the API does not exist
apiConsumer.getLightweightAPIByUUID(apiId, organization);
ResourceFile thumbnailResource = apiConsumer.getIcon(apiId, organization);
if (thumbnailResource != null) {
return Response.ok(thumbnailResource.getContent(), MediaType.valueOf(thumbnailResource.getContentType())).build();
} else {
return Response.noContent().build();
}
} catch (APIManagementException e) {
// existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while retrieving thumbnail of API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.ResourceFile in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getWSDLOfAPI.
@Override
public Response getWSDLOfAPI(String apiId, String environmentName, String ifNoneMatch, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
API api = apiConsumer.getLightweightAPIByUUID(apiId, organization);
APIIdentifier apiIdentifier = api.getId();
List<Environment> environments = APIUtil.getEnvironmentsOfAPI(api);
if (environments != null && environments.size() > 0) {
if (StringUtils.isEmpty(environmentName)) {
environmentName = api.getEnvironments().iterator().next();
}
Environment selectedEnvironment = null;
for (Environment environment : environments) {
if (environment.getName().equals(environmentName)) {
selectedEnvironment = environment;
break;
}
}
if (selectedEnvironment == null) {
throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.INVALID_GATEWAY_ENVIRONMENT, environmentName));
}
ResourceFile wsdl = apiConsumer.getWSDL(api, selectedEnvironment.getName(), selectedEnvironment.getType(), organization);
return RestApiUtil.getResponseFromResourceFile(apiIdentifier.toString(), wsdl);
} else {
throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.NO_GATEWAY_ENVIRONMENTS_ADDED, apiIdentifier.toString()));
}
}
use of org.wso2.carbon.apimgt.api.model.ResourceFile in project carbon-apimgt by wso2.
the class AbstractAPIManager method addResourceFile.
public String addResourceFile(Identifier identifier, String resourcePath, ResourceFile resourceFile) throws APIManagementException {
try {
Resource thumb = registry.newResource();
thumb.setContentStream(resourceFile.getContent());
thumb.setMediaType(resourceFile.getContentType());
registry.put(resourcePath, thumb);
if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
return RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + RegistryConstants.PATH_SEPARATOR + "_system" + RegistryConstants.PATH_SEPARATOR + "governance" + resourcePath;
} else {
return "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + RegistryConstants.PATH_SEPARATOR + "_system" + RegistryConstants.PATH_SEPARATOR + "governance" + resourcePath;
}
} catch (RegistryException e) {
String msg = "Error while adding the resource to the registry";
throw new APIManagementException(msg, e);
}
}
use of org.wso2.carbon.apimgt.api.model.ResourceFile in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testAddResourceFile.
@Test
public void testAddResourceFile() throws APIManagementException, RegistryException, IOException {
Identifier identifier = Mockito.mock(Identifier.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
Resource resource = new ResourceImpl();
Mockito.when(registry.newResource()).thenReturn(resource);
String resourcePath = "/test";
String contentType = "sampleType";
ResourceFile resourceFile = new ResourceFile(null, contentType);
try {
abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Error while adding the resource to the registry"));
}
InputStream in = IOUtils.toInputStream("sample content", "UTF-8");
resourceFile = new ResourceFile(in, contentType);
String returnedPath = abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
Assert.assertTrue(returnedPath.contains(resourcePath) && returnedPath.contains("/t/"));
abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN;
returnedPath = abstractAPIManager.addResourceFile(identifier, resourcePath, resourceFile);
Assert.assertTrue(returnedPath.contains(resourcePath) && !returnedPath.contains("/t/"));
}
Aggregations