use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class PublisherCommonUtils method updateThumbnail.
/**
* Update thumbnail of an API/API Product
*
* @param fileInputStream Input stream
* @param fileContentType The content type of the image
* @param apiProvider API Provider
* @param apiId API/API Product UUID
* @param tenantDomain Tenant domain of the API
* @throws APIManagementException If an error occurs while updating the thumbnail
*/
public static void updateThumbnail(InputStream fileInputStream, String fileContentType, APIProvider apiProvider, String apiId, String tenantDomain) throws APIManagementException {
ResourceFile apiImage = new ResourceFile(fileInputStream, fileContentType);
apiProvider.setThumbnailToAPI(apiId, apiImage, tenantDomain);
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method getAPIProductThumbnail.
@Override
public Response getAPIProductThumbnail(String apiProductId, String accept, String ifNoneMatch, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
// this will fail if user does not have access to the API or the API does not exist
APIProductIdentifier productIdentifier = APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, tenantDomain);
ResourceFile thumbnailResource = apiProvider.getIcon(apiProductId, tenantDomain);
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_PRODUCT, apiProductId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving thumbnail of API Product : " + apiProductId, e, log);
} else {
String errorMessage = "Error while retrieving thumbnail of API Product : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method updateAPIProductThumbnail.
@Override
public Response updateAPIProductThumbnail(String apiProductId, InputStream fileInputStream, Attachment fileDetail, String ifMatch, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
String fileName = fileDetail.getDataHandler().getName();
String extension = FilenameUtils.getExtension(fileName);
if (!RestApiConstants.ALLOWED_THUMBNAIL_EXTENSIONS.contains(extension.toLowerCase())) {
RestApiUtil.handleBadRequest("Unsupported Thumbnail File Extension. Supported extensions are .jpg, .png, .jpeg .svg " + "and .gif", log);
}
String fileContentType = URLConnection.guessContentTypeFromName(fileName);
if (org.apache.commons.lang3.StringUtils.isBlank(fileContentType)) {
fileContentType = fileDetail.getContentType().toString();
}
// this will fail if user does not have access to the API or the API does not exist
APIProduct apiProduct = apiProvider.getAPIProductbyUUID(apiProductId, tenantDomain);
ResourceFile apiImage = new ResourceFile(fileInputStream, fileContentType);
apiProvider.setThumbnailToAPI(apiProductId, apiImage, tenantDomain);
/*
String thumbPath = APIUtil.getProductIconPath(apiProduct.getId());
String thumbnailUrl = apiProvider.addProductResourceFile(apiProduct.getId(), thumbPath, apiImage);
apiProduct.setThumbnailUrl(APIUtil.prependTenantPrefix(thumbnailUrl, apiProduct.getId().getProviderName()));
APIUtil.setResourcePermissions(apiProduct.getId().getProviderName(), null, null, thumbPath);
//need to set product resource mappings before updating product, otherwise existing mappings will be lost
List<APIProductResource> resources = apiProvider.getResourcesOfAPIProduct(apiProduct.getId());
apiProduct.setProductResources(resources);
apiProvider.updateAPIProduct(apiProduct);
*/
String uriString = RestApiConstants.RESOURCE_PATH_THUMBNAIL.replace(RestApiConstants.APIID_PARAM, apiProductId);
URI uri = new URI(uriString);
FileInfoDTO infoDTO = new FileInfoDTO();
infoDTO.setRelativePath(uriString);
infoDTO.setMediaType(apiImage.getContentType());
return Response.created(uri).entity(infoDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while updating API Product : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving thumbnail location of API Product : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method addDocumentationContent.
@Override
public DocumentContent addDocumentationContent(Organization org, String apiId, String docId, DocumentContent content) throws DocumentationPersistenceException {
boolean isTenantFlowStarted = false;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
Registry registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
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);
GenericArtifactManager docArtifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registry);
GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docId);
Documentation doc = RegistryPersistenceDocUtil.getDocumentation(docArtifact);
if (DocumentContent.ContentSourceType.FILE.equals(content.getSourceType())) {
ResourceFile resource = content.getResourceFile();
String filePath = RegistryPersistenceDocUtil.getDocumentFilePath(apiProviderName, apiName, apiVersion, resource.getName());
String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
String[] visibleRoles = new String[0];
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
RegistryPersistenceUtil.setResourcePermissions(RegistryPersistenceUtil.replaceEmailDomain(apiProviderName), visibility, visibleRoles, filePath, registry);
// documentation.setFilePath(addResourceFile(apiId, filePath, icon));
String savedFilePath = addResourceFile(filePath, resource, registry, tenantDomain);
// doc.setFilePath(savedFilePath);
docArtifact.setAttribute(APIConstants.DOC_FILE_PATH, savedFilePath);
docArtifactManager.updateGenericArtifact(docArtifact);
RegistryPersistenceUtil.setFilePermission(filePath);
} else {
String contentPath = RegistryPersistenceDocUtil.getDocumentContentPath(apiProviderName, apiName, apiVersion, doc.getName());
Resource docContent;
if (!registry.resourceExists(contentPath)) {
docContent = registry.newResource();
} else {
docContent = registry.get(contentPath);
}
String text = content.getTextContent();
if (!APIConstants.NO_CONTENT_UPDATE.equals(text)) {
docContent.setContent(text);
}
docContent.setMediaType(APIConstants.DOCUMENTATION_INLINE_CONTENT_TYPE);
registry.put(contentPath, docContent);
// Set resource permission
String apiPath = RegistryPersistenceUtil.getAPIPath(apiName, apiVersion, apiProviderName);
String docVisibility = doc.getVisibility().name();
String[] authorizedRoles = RegistryPersistenceUtil.getAuthorizedRoles(apiPath, tenantDomain);
String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
if (docVisibility != null) {
if (APIConstants.DOC_SHARED_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_SHARED_VISIBILITY;
} else if (APIConstants.DOC_OWNER_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_OWNER_VISIBILITY;
}
}
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, contentPath, registry);
GenericArtifact updateDocArtifact = RegistryPersistenceDocUtil.createDocArtifactContent(docArtifact, apiProviderName, apiName, apiVersion, doc);
Boolean toggle = Boolean.parseBoolean(updateDocArtifact.getAttribute("toggle"));
updateDocArtifact.setAttribute("toggle", Boolean.toString(!toggle));
docArtifactManager.updateGenericArtifact(updateDocArtifact);
}
} catch (APIPersistenceException | RegistryException | APIManagementException | PersistenceException | UserStoreException e) {
throw new DocumentationPersistenceException("Error while adding document content", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return null;
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getWSDL.
@Override
public ResourceFile getWSDL(Organization org, String apiId) throws WSDLPersistenceException {
boolean isTenantFlowStarted = false;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
Registry registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
GenericArtifact apiArtifact = getAPIArtifact(apiId, registry);
if (apiArtifact == null) {
return null;
}
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 apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
int prependIndex = apiPath.lastIndexOf("/api");
String apiSourcePath = apiPath.substring(0, prependIndex);
String wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
String wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
String resourceFileName = apiProviderName + "-" + apiName + "-" + apiVersion;
if (registry.resourceExists(wsdlResourcePath)) {
Resource resource = registry.get(wsdlResourcePath);
ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
returnResource.setName(resourceFileName);
return returnResource;
} else if (registry.resourceExists(wsdlResourcePathOld)) {
Resource resource = registry.get(wsdlResourcePathOld);
ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
returnResource.setName(resourceFileName);
return returnResource;
} else {
wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
if (registry.resourceExists(wsdlResourcePath)) {
Resource resource = registry.get(wsdlResourcePath);
ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
returnResource.setName(resourceFileName);
return returnResource;
} else if (registry.resourceExists(wsdlResourcePathOld)) {
Resource resource = registry.get(wsdlResourcePathOld);
ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
returnResource.setName(resourceFileName);
return returnResource;
} else {
throw new WSDLPersistenceException("No WSDL found for the API: " + apiId, ExceptionCodes.from(ExceptionCodes.NO_WSDL_AVAILABLE_FOR_API, apiName, apiVersion));
}
}
} catch (RegistryException | APIPersistenceException e) {
String msg = "Error while getting wsdl file from the registry for API: " + apiId.toString();
throw new WSDLPersistenceException(msg, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
Aggregations