use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method saveThumbnail.
@Override
public void saveThumbnail(Organization org, String apiId, ResourceFile resourceFile) throws ThumbnailPersistenceException {
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);
if (apiArtifact == null) {
throw new ThumbnailPersistenceException("API not found. ", 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 artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
String filePath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
String savedFilePath = addResourceFile(filePath, resourceFile, registry, tenantDomain);
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, null, null, filePath);
apiArtifact.setAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL, savedFilePath);
apiArtifactManager.updateGenericArtifact(apiArtifact);
} catch (APIPersistenceException | GovernanceException | PersistenceException | APIManagementException e) {
throw new ThumbnailPersistenceException("Error while saving thumbnail for api " + apiId, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method updateWSDLOfAPI.
/**
* Update the WSDL of an API
*
* @param apiId UUID of the API
* @param fileInputStream file data as input stream
* @param fileDetail file details
* @param url URL of the WSDL
* @return 200 OK response if the operation is successful. 400 if the provided inputs are invalid. 500 if a server
* error occurred.
* @throws APIManagementException when error occurred while trying to retrieve the WSDL
*/
@Override
public Response updateWSDLOfAPI(String apiId, String ifMatch, InputStream fileInputStream, Attachment fileDetail, String url, MessageContext messageContext) throws APIManagementException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// validate if api exists
APIInfo apiInfo = validateAPIExistence(apiId);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(apiInfo.getStatus().toString());
WSDLValidationResponse validationResponse = validateWSDLAndReset(fileInputStream, fileDetail, url);
if (StringUtils.isNotBlank(url)) {
apiProvider.addWSDLResource(apiId, null, url, organization);
} else {
ResourceFile wsdlResource;
if (APIConstants.APPLICATION_ZIP.equals(fileDetail.getContentType().toString()) || APIConstants.APPLICATION_X_ZIP_COMPRESSED.equals(fileDetail.getContentType().toString())) {
wsdlResource = new ResourceFile(validationResponse.getWsdlProcessor().getWSDL(), APIConstants.APPLICATION_ZIP);
} else {
wsdlResource = new ResourceFile(validationResponse.getWsdlProcessor().getWSDL(), fileDetail.getContentType().toString());
}
apiProvider.addWSDLResource(apiId, wsdlResource, null, organization);
}
return Response.ok().build();
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class APIProviderImpl method setThumbnailToAPI.
@Override
public void setThumbnailToAPI(String apiId, ResourceFile resource, String organization) throws APIManagementException {
try {
org.wso2.carbon.apimgt.persistence.dto.ResourceFile iconResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(resource.getContent(), resource.getContentType());
apiPersistenceInstance.saveThumbnail(new Organization(organization), apiId, iconResourceFile);
} catch (ThumbnailPersistenceException e) {
if (e.getErrorHandler() == ExceptionCodes.API_NOT_FOUND) {
throw new APIMgtResourceNotFoundException(e);
} else {
throw new APIManagementException("Error while saving thumbnail ", e);
}
}
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class APIProviderImpl method addWSDLResource.
@Override
public void addWSDLResource(String apiId, ResourceFile resource, String url, String organization) throws APIManagementException {
if (!StringUtils.isEmpty(url)) {
URL wsdlUrl;
try {
wsdlUrl = new URL(url);
} catch (MalformedURLException e) {
throw new APIManagementException("Invalid/Malformed WSDL URL : " + url, e, ExceptionCodes.INVALID_WSDL_URL_EXCEPTION);
}
// Get the WSDL 1.1 or 2.0 processor and process the content based on the version
WSDLProcessor wsdlProcessor = APIMWSDLReader.getWSDLProcessorForUrl(wsdlUrl);
InputStream wsdlContent = wsdlProcessor.getWSDL();
// wsdlResource.setContentStream(wsdlContent);
org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(wsdlContent, null);
try {
apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
} catch (WSDLPersistenceException e) {
throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
}
} else if (resource != null) {
org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(resource.getContent(), resource.getContentType());
try {
apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
} catch (WSDLPersistenceException e) {
throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
}
}
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class APIProviderImpl method addFileToProductDocumentation.
/**
* Add a file to a product document of source type FILE
*
* @param productId APIProduct identifier the document belongs to
* @param documentation document
* @param filename name of the file
* @param content content of the file as an Input Stream
* @param contentType content type of the file
* @throws APIManagementException if failed to add the file
*/
public void addFileToProductDocumentation(APIProductIdentifier productId, Documentation documentation, String filename, InputStream content, String contentType) throws APIManagementException {
if (Documentation.DocumentSourceType.FILE.equals(documentation.getSourceType())) {
contentType = "application/force-download";
ResourceFile icon = new ResourceFile(content, contentType);
String filePath = APIUtil.getDocumentationFilePath(productId, filename);
APIProduct apiProduct;
try {
apiProduct = getAPIProduct(productId);
String visibleRolesList = apiProduct.getVisibleRoles();
String[] visibleRoles = new String[0];
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
APIUtil.setResourcePermissions(apiProduct.getId().getProviderName(), apiProduct.getVisibility(), visibleRoles, filePath, registry);
documentation.setFilePath(addResourceFile(productId, filePath, icon));
APIUtil.setFilePermission(filePath);
} catch (APIManagementException e) {
handleException("Failed to add file to product document " + documentation.getName(), e);
}
} else {
String errorMsg = "Cannot add file to the Product Document. Document " + documentation.getName() + "'s Source type is not FILE.";
handleException(errorMsg);
}
}
Aggregations