Search in sources :

Example 31 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.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;
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 32 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.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();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 33 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.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);
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 34 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.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;
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) MediationDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) URI(java.net.URI)

Example 35 with ResourceFile

use of org.wso2.carbon.apimgt.api.model.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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.api.Resource) OMElement(org.apache.axiom.om.OMElement) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) MediationDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) URI(java.net.URI) RegistryException(org.wso2.carbon.registry.api.RegistryException) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)27 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)24 Resource (org.wso2.carbon.registry.core.Resource)13 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)11 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)10 Registry (org.wso2.carbon.registry.core.Registry)9 InputStream (java.io.InputStream)8 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)8 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)7 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)6 WSDLPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException)6 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)5 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)5 ResourceFile (org.wso2.carbon.apimgt.persistence.dto.ResourceFile)4 ThumbnailPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException)4 IOException (java.io.IOException)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3