Search in sources :

Example 1 with OperationPolicyDataDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO in project carbon-apimgt by wso2.

the class OperationPoliciesApiServiceImpl method getCommonOperationPolicyByPolicyId.

/**
 * Get the common operation policy by providing the policy ID
 *
 * @param operationPolicyId UUID of the operation policy
 * @param messageContext    message context
 * @return Operation policy DTO as response
 */
@Override
public Response getCommonOperationPolicyByPolicyId(String operationPolicyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        OperationPolicyData existingPolicy = apiProvider.getCommonOperationPolicyByPolicyId(operationPolicyId, organization, false);
        if (existingPolicy != null) {
            OperationPolicyDataDTO policyDataDTO = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(existingPolicy);
            return Response.ok().entity(policyDataDTO).build();
        } else {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing common policy with ID: " + operationPolicyId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
        } else {
            String errorMessage = "Error while getting the common operation policy with ID :" + operationPolicyId + " " + e.getMessage();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (Exception e) {
        RestApiUtil.handleInternalServerError("An error has occurred while getting the common operation " + " policy with ID: " + operationPolicyId, e, log);
    }
    return null;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 2 with OperationPolicyDataDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getOperationPolicyForAPIByPolicyId.

/**
 * Get the API specific operation policy specification by providing the policy ID
 *
 * @param apiId             API UUID
 * @param operationPolicyId UUID of the operation policy
 * @param messageContext    message context
 * @return Operation policy DTO as response
 */
@Override
public Response getOperationPolicyForAPIByPolicyId(String apiId, String operationPolicyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // validate whether api exists or not
        validateAPIExistence(apiId);
        OperationPolicyData existingPolicy = apiProvider.getAPISpecificOperationPolicyByPolicyId(operationPolicyId, apiId, organization, false);
        if (existingPolicy != null) {
            OperationPolicyDataDTO policyDataDTO = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(existingPolicy);
            return Response.ok().entity(policyDataDTO).build();
        } else {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing operation policy with ID: " + operationPolicyId + " for API " + apiId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
        } else {
            String errorMessage = "Error while getting an API specific operation policy with ID :" + operationPolicyId + " for API " + apiId + " " + e.getMessage();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (Exception e) {
        RestApiUtil.handleInternalServerError("An Error has occurred while getting the operation policy with ID " + operationPolicyId + "operation policy for API " + apiId, e, log);
    }
    return null;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) JSONException(org.json.JSONException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SdkClientException(com.amazonaws.SdkClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URISyntaxException(java.net.URISyntaxException) BadRequestException(org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException) CryptoException(org.wso2.carbon.core.util.CryptoException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException)

Example 3 with OperationPolicyDataDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method addAPISpecificOperationPolicy.

/**
 * Add an API specific operation policy
 *
 * @param apiId                                  UUID of the API
 * @param policySpecFileInputStream              Input stream of the policy specification file
 * @param policySpecFileDetail                   Operation policy specification
 * @param synapsePolicyDefinitionFileInputStream Input stream of the synapse policy definition file
 * @param synapsePolicyDefinitionFileDetail      Synapse definition of the operation policy
 * @param ccPolicyDefinitionFileInputStream      Input stream of the choreo connect policy definition file
 * @param ccPolicyDefinitionFileDetail           Choreo connect definition of the operation policy
 * @param messageContext                         message context
 * @return Added Operation operation policy DTO as response
 */
@Override
public Response addAPISpecificOperationPolicy(String apiId, InputStream policySpecFileInputStream, Attachment policySpecFileDetail, InputStream synapsePolicyDefinitionFileInputStream, Attachment synapsePolicyDefinitionFileDetail, InputStream ccPolicyDefinitionFileInputStream, Attachment ccPolicyDefinitionFileDetail, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // validate if api exists
        validateAPIExistence(apiId);
        String jsonContent = "";
        OperationPolicyDefinition synapseDefinition = null;
        OperationPolicyDefinition ccPolicyDefinition = null;
        OperationPolicySpecification policySpecification;
        if (policySpecFileInputStream != null) {
            jsonContent = RestApiPublisherUtils.readInputStream(policySpecFileInputStream, policySpecFileDetail);
            String fileName = policySpecFileDetail.getDataHandler().getName();
            String fileContentType = URLConnection.guessContentTypeFromName(fileName);
            if (org.apache.commons.lang3.StringUtils.isBlank(fileContentType)) {
                fileContentType = policySpecFileDetail.getContentType().toString();
            }
            if (APIConstants.YAML_CONTENT_TYPE.equals(fileContentType)) {
                jsonContent = CommonUtil.yamlToJson(jsonContent);
            }
            policySpecification = APIUtil.getValidatedOperationPolicySpecification(jsonContent);
            OperationPolicyData operationPolicyData = new OperationPolicyData();
            operationPolicyData.setOrganization(organization);
            operationPolicyData.setApiUUID(apiId);
            operationPolicyData.setSpecification(policySpecification);
            if (synapsePolicyDefinitionFileInputStream != null) {
                String synapsePolicyDefinition = RestApiPublisherUtils.readInputStream(synapsePolicyDefinitionFileInputStream, synapsePolicyDefinitionFileDetail);
                synapseDefinition = new OperationPolicyDefinition();
                synapseDefinition.setContent(synapsePolicyDefinition);
                synapseDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.Synapse);
                synapseDefinition.setMd5Hash(APIUtil.getMd5OfOperationPolicyDefinition(synapseDefinition));
                operationPolicyData.setSynapsePolicyDefinition(synapseDefinition);
            }
            if (ccPolicyDefinitionFileInputStream != null) {
                String choreoConnectPolicyDefinition = RestApiPublisherUtils.readInputStream(ccPolicyDefinitionFileInputStream, ccPolicyDefinitionFileDetail);
                ccPolicyDefinition = new OperationPolicyDefinition();
                ccPolicyDefinition.setContent(choreoConnectPolicyDefinition);
                ccPolicyDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.ChoreoConnect);
                ccPolicyDefinition.setMd5Hash(APIUtil.getMd5OfOperationPolicyDefinition(ccPolicyDefinition));
                operationPolicyData.setCcPolicyDefinition(ccPolicyDefinition);
            }
            operationPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(operationPolicyData));
            OperationPolicyData existingPolicy = apiProvider.getAPISpecificOperationPolicyByPolicyName(policySpecification.getName(), apiId, null, organization, false);
            String policyID;
            if (existingPolicy == null) {
                policyID = apiProvider.addAPISpecificOperationPolicy(apiId, operationPolicyData, organization);
                if (log.isDebugEnabled()) {
                    log.debug("An API specific operation policy has been added for the API " + apiId + " with id " + policyID);
                }
            } else {
                throw new APIManagementException("An API specific operation policy found for the same name.");
            }
            operationPolicyData.setPolicyId(policyID);
            OperationPolicyDataDTO operationPolicyDataDTO = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(operationPolicyData);
            URI createdPolicyUri = new URI(RestApiConstants.REST_API_PUBLISHER_VERSION + RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + "/" + RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES + "/" + policyID);
            return Response.created(createdPolicyUri).entity(operationPolicyDataDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding an API specific operation policy." + e.getMessage();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (Exception e) {
        RestApiUtil.handleInternalServerError("An error has occurred while adding an API specific " + "operation policy", e, log);
    }
    return null;
}
Also used : OperationPolicyDefinition(org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition) OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) JSONException(org.json.JSONException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SdkClientException(com.amazonaws.SdkClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URISyntaxException(java.net.URISyntaxException) BadRequestException(org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException) CryptoException(org.wso2.carbon.core.util.CryptoException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException)

Example 4 with OperationPolicyDataDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO in project carbon-apimgt by wso2.

the class OperationPolicyMappingUtil method fromOperationPolicyDataListToDTO.

public static OperationPolicyDataListDTO fromOperationPolicyDataListToDTO(List<OperationPolicyData> policyDataList, int offset, int limit) {
    List<OperationPolicyDataDTO> operationPolicyList = new ArrayList<>();
    if (policyDataList == null) {
        policyDataList = new ArrayList<>();
    }
    int size = policyDataList.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = Math.min(offset + limit - 1, size - 1);
    for (int i = start; i <= end; i++) {
        operationPolicyList.add(fromOperationPolicyDataToDTO(policyDataList.get(i)));
    }
    PaginationDTO paginationDTO = new PaginationDTO();
    paginationDTO.setLimit(limit);
    paginationDTO.setOffset(offset);
    paginationDTO.setTotal(size);
    OperationPolicyDataListDTO dataListDTO = new OperationPolicyDataListDTO();
    dataListDTO.setList(operationPolicyList);
    dataListDTO.setCount(operationPolicyList.size());
    dataListDTO.setPagination(paginationDTO);
    return dataListDTO;
}
Also used : OperationPolicyDataListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataListDTO) ArrayList(java.util.ArrayList) PaginationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO)

Example 5 with OperationPolicyDataDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO in project carbon-apimgt by wso2.

the class OperationPolicyMappingUtil method fromOperationPolicyDataToDTO.

public static OperationPolicyDataDTO fromOperationPolicyDataToDTO(OperationPolicyData policyData) {
    OperationPolicyDataDTO policyDataDTO = new OperationPolicyDataDTO();
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    policyDataDTO.setId(policyData.getPolicyId());
    policyDataDTO.setMd5(policyData.getMd5Hash());
    policyDataDTO.setIsAPISpecific(policyData.isApiSpecificPolicy());
    policyDataDTO.setName(policySpecification.getName());
    policyDataDTO.setDisplayName(policySpecification.getDisplayName());
    policyDataDTO.setDescription(policySpecification.getDescription());
    policyDataDTO.setSupportedGateways(policySpecification.getSupportedGateways());
    policyDataDTO.setSupportedApiTypes(policySpecification.getSupportedApiTypes());
    policyDataDTO.setApplicableFlows(policySpecification.getApplicableFlows());
    policyDataDTO.setMultipleAllowed(policySpecification.isMultipleAllowed());
    policyDataDTO.setCategory(policySpecification.getCategory().toString());
    if (policySpecification.getPolicyAttributes() != null) {
        List<OperationPolicySpecAttributeDTO> specAttributeDtoList = new ArrayList<>();
        for (OperationPolicySpecAttribute specAttribute : policySpecification.getPolicyAttributes()) {
            OperationPolicySpecAttributeDTO specAttributeDTO = fromOperationPolicySpecAttributesToDTO(specAttribute);
            specAttributeDtoList.add(specAttributeDTO);
        }
        policyDataDTO.setPolicyAttributes(specAttributeDtoList);
    }
    return policyDataDTO;
}
Also used : OperationPolicySpecAttribute(org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute) ArrayList(java.util.ArrayList) OperationPolicySpecAttributeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicySpecAttributeDTO) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Aggregations

OperationPolicyDataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)4 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)4 OperationPolicySpecification (org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)3 SdkClientException (com.amazonaws.SdkClientException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 ParseException (org.json.simple.parser.ParseException)2 APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException)2 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)2 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)2 OperationPolicyDefinition (org.wso2.carbon.apimgt.api.model.OperationPolicyDefinition)2