Search in sources :

Example 1 with MediationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesMediationMediationPolicyIdGet.

/**
 * Returns a specific global mediation policy by identifier
 *
 * @param mediationPolicyId Mediation policy uuid
 * @param accept            Accept header value
 * @return returns the matched mediation
 */
@Override
public Response policiesMediationMediationPolicyIdGet(String mediationPolicyId, String accept, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // Get given global mediation policy
        Mediation mediation = apiProvider.getGlobalMediationPolicy(mediationPolicyId);
        if (mediation != null) {
            MediationDTO mediationDTO = MediationMappingUtil.fromMediationToDTO(mediation);
            return Response.ok().entity(mediationDTO).build();
        } else {
            // If global mediation policy not exists
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_POLICY, mediationPolicyId, log);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving the global mediation policy with id " + mediationPolicyId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) 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)

Example 2 with MediationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO in project carbon-apimgt by wso2.

the class MediationMappingUtil method fromMediationToDTO.

public static MediationDTO fromMediationToDTO(Mediation mediation) {
    MediationDTO mediationDTO = new MediationDTO();
    mediationDTO.setId(mediation.getUuid());
    mediationDTO.setName(mediation.getName());
    mediationDTO.setType(MediationDTO.TypeEnum.valueOf(mediation.getType().toUpperCase()));
    mediationDTO.setConfig(mediation.getConfig());
    return mediationDTO;
}
Also used : MediationDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO)

Example 3 with MediationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO in project carbon-apimgt by wso2.

the class MediationMappingUtil method fromMediationToDTO.

/**
 * Converts an Mediation object into MediationDTO
 *
 * @param mediation Mediation object
 * @return MediationDTO object correspond to the given Mediation object
 */
public static MediationDTO fromMediationToDTO(Mediation mediation) {
    MediationDTO mediationDTO = new MediationDTO();
    mediationDTO.setId(mediation.getUuid());
    mediationDTO.setName(mediation.getName());
    mediationDTO.setType(MediationDTO.TypeEnum.valueOf(mediation.getType().toUpperCase()));
    return mediationDTO;
}
Also used : MediationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MediationDTO)

Example 4 with MediationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO 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 5 with MediationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO 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

MediationDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)3 Mediation (org.wso2.carbon.apimgt.api.model.Mediation)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)2 IOException (java.io.IOException)1 QName (javax.xml.namespace.QName)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMElement (org.apache.axiom.om.OMElement)1 MediationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MediationDTO)1 RegistryException (org.wso2.carbon.registry.api.RegistryException)1 Resource (org.wso2.carbon.registry.api.Resource)1