use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class AbstractAPIManager method getProductIcon.
public ResourceFile getProductIcon(APIProductIdentifier identifier) throws APIManagementException {
String thumbPath = APIUtil.getProductIconPath(identifier);
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
Registry registry;
boolean isTenantFlowStarted = false;
try {
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
startTenantFlow(tenantDomain);
isTenantFlowStarted = true;
}
/* If the API provider is a tenant, load tenant registry*/
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
int id = getTenantManager().getTenantId(tenantDomain);
registry = getRegistryService().getGovernanceSystemRegistry(id);
} else {
if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
registry = getRegistryService().getGovernanceUserRegistry(identifier.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
} else {
registry = this.registry;
}
}
if (registry.resourceExists(thumbPath)) {
Resource res = registry.get(thumbPath);
return new ResourceFile(res.getContentStream(), res.getMediaType());
}
} catch (RegistryException e) {
String msg = "Error while loading API Product icon of API Product " + identifier.getName() + ":" + identifier.getVersion() + " from the registry";
throw new APIManagementException(msg, e);
} catch (org.wso2.carbon.user.api.UserStoreException e) {
String msg = "Error while loading API Product icon of API Product " + identifier.getName() + ":" + identifier.getVersion();
throw new APIManagementException(msg, e);
} finally {
if (isTenantFlowStarted) {
endTenantFlow();
}
}
return null;
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method saveWSDL.
@Override
public void saveWSDL(Organization org, String apiId, ResourceFile wsdlResourceFile) throws WSDLPersistenceException {
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);
String apiSourcePath = RegistryPersistenceUtil.getAPIBasePath(apiProviderName, apiName, apiVersion);
String wsdlResourcePath = null;
boolean isZip = false;
String wsdlResourcePathArchive = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
String wsdlResourcePathFile = apiSourcePath + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
if (APIConstants.APPLICATION_ZIP.equals(wsdlResourceFile.getContentType())) {
wsdlResourcePath = wsdlResourcePathArchive;
isZip = true;
} else {
wsdlResourcePath = wsdlResourcePathFile;
}
String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
Resource wsdlResource = registry.newResource();
;
wsdlResource.setContentStream(wsdlResourceFile.getContent());
if (wsdlResourceFile.getContentType() != null) {
wsdlResource.setMediaType(wsdlResourceFile.getContentType());
}
registry.put(wsdlResourcePath, wsdlResource);
// set the anonymous role for wsld resource to avoid basicauth security.
String[] visibleRoles = null;
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, visibleRoles, wsdlResourcePath);
if (isZip) {
// Delete any WSDL file if exists
if (registry.resourceExists(wsdlResourcePathFile)) {
registry.delete(wsdlResourcePathFile);
}
} else {
// Delete any WSDL archives if exists
if (registry.resourceExists(wsdlResourcePathArchive)) {
registry.delete(wsdlResourcePathArchive);
}
}
String absoluteWSDLResourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + wsdlResourcePath;
String wsdlRegistryPath;
if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
wsdlRegistryPath = RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
} else {
wsdlRegistryPath = "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
}
apiArtifact.setAttribute(APIConstants.API_OVERVIEW_WSDL, wsdlRegistryPath);
apiArtifactManager.updateGenericArtifact(apiArtifact);
} catch (APIPersistenceException | APIManagementException | RegistryException e) {
throw new WSDLPersistenceException("Error while saving the wsdl 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 RegistryPersistenceImpl method addResourceFile.
protected String addResourceFile(String resourcePath, ResourceFile resourceFile, Registry registry, String tenantDomain) throws PersistenceException {
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 PersistenceException(msg, e);
}
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesMediationPost.
/**
* Add a global mediation policy
*
* @param body Mediation DTO as request body
* @param contentType Content-Type header
* @return created mediation DTO as response
*/
@Override
public Response policiesMediationPost(String contentType, MediationDTO body, MessageContext messageContext) {
InputStream contentStream = null;
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String content = body.getConfig();
contentStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
ResourceFile contentFile = new ResourceFile(contentStream, contentType);
// Extracting mediation policy name from the mediation config
String fileName = this.getMediationNameFromConfig(content);
// constructing the registry resource path
String mediationPolicyPath = APIConstants.API_CUSTOM_SEQUENCE_LOCATION + RegistryConstants.PATH_SEPARATOR + body.getType() + RegistryConstants.PATH_SEPARATOR + fileName;
if (apiProvider.checkIfResourceExists(mediationPolicyPath)) {
RestApiUtil.handleConflict("Mediation policy already exists", log);
}
// Adding new global mediation sequence
// No need to check API permission, hence null as api identifier
String mediationPolicyUrl = apiProvider.addResourceFile(null, mediationPolicyPath, contentFile);
if (StringUtils.isNotBlank(mediationPolicyUrl)) {
// Getting the uuid of the created global mediation policy
String uuid = apiProvider.getCreatedResourceUuid(mediationPolicyPath);
// Getting created mediation policy
Mediation createdMediation = apiProvider.getGlobalMediationPolicy(uuid);
MediationDTO createdPolicy = MediationMappingUtil.fromMediationToDTO(createdMediation);
URI uploadedMediationUri = new URI(mediationPolicyUrl);
return Response.created(uploadedMediationUri).entity(createdPolicy).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while adding the global mediation policy " + body.getName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (URISyntaxException e) {
String errorMessage = "Error while getting location header for created " + "mediation policy " + body.getName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} finally {
IOUtils.closeQuietly(contentStream);
}
return null;
}
use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesMediationMediationPolicyIdPut.
/**
* Updates an existing global mediation policy
*
* @param mediationPolicyId uuid of mediation policy
* @param body updated MediationDTO
* @param contentType Content-Type header
* @return updated mediation DTO as response
*/
@Override
public Response policiesMediationMediationPolicyIdPut(String mediationPolicyId, String contentType, MediationDTO body, MessageContext messageContext) {
InputStream contentStream = null;
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// Get registry resource correspond to given uuid
Resource mediationResource = apiProvider.getCustomMediationResourceFromUuid(mediationPolicyId);
if (mediationResource != null) {
// extracting already existing name of the mediation policy
String contentString = IOUtils.toString(mediationResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
// Get policy name from the mediation config
OMElement omElement = AXIOMUtil.stringToOM(contentString);
OMAttribute attribute = omElement.getAttribute(new QName(PolicyConstants.MEDIATION_NAME_ATTRIBUTE));
String existingMediationPolicyName = attribute.getAttributeValue();
// replacing the name of the body with existing name
body.setName(existingMediationPolicyName);
// Getting mediation config to be update from the body
contentStream = new ByteArrayInputStream(body.getConfig().getBytes(StandardCharsets.UTF_8));
// Creating new resource file
ResourceFile contentFile = new ResourceFile(contentStream, contentType);
// Getting registry path of the existing resource
String resourcePath = mediationResource.getPath();
// Updating the existing global mediation policy
// No need to check API permission, hence null as api identifier
String updatedPolicyUrl = apiProvider.addResourceFile(null, resourcePath, contentFile);
if (StringUtils.isNotBlank(updatedPolicyUrl)) {
// Getting uuid of updated global mediation policy
String uuid = apiProvider.getCreatedResourceUuid(resourcePath);
// Getting updated mediation
Mediation updatedMediation = apiProvider.getGlobalMediationPolicy(uuid);
MediationDTO updatedMediationDTO = MediationMappingUtil.fromMediationToDTO(updatedMediation);
URI uploadedMediationUri = new URI(updatedPolicyUrl);
return Response.ok(uploadedMediationUri).entity(updatedMediationDTO).build();
}
} else {
// If resource not exists
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_POLICY, mediationPolicyId, log);
}
} catch (APIManagementException e) {
String errorMessage = "Error while updating the global mediation policy " + body.getName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (URISyntaxException e) {
String errorMessage = "Error while getting location header for uploaded " + "mediation policy " + body.getName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (XMLStreamException e) {
String errorMessage = "Error occurred while converting the existing content stream of " + " mediation " + "policy to string";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (RegistryException e) {
String errorMessage = "Error occurred while getting the existing content stream ";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (IOException e) {
String errorMessage = "Error occurred while converting content stream in to string ";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} finally {
IOUtils.closeQuietly(contentStream);
}
return null;
}
Aggregations