Search in sources :

Example 1 with ResourcePolicyInfoDTO

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

the class APIMappingUtil method fromResourcePolicyStrToInfoDTO.

/**
 * Creates a DTO consisting a single conversion policy.
 *
 * @param conversionPolicyStr conversion policy string
 * @return ConversionPolicyInfoDTO consisting given conversion policy string
 * @throws APIManagementException
 */
public static ResourcePolicyInfoDTO fromResourcePolicyStrToInfoDTO(String conversionPolicyStr) throws APIManagementException {
    ResourcePolicyInfoDTO policyInfoDTO = new ResourcePolicyInfoDTO();
    if (StringUtils.isNotEmpty(conversionPolicyStr)) {
        try {
            JSONObject conversionPolicyObj = (JSONObject) new JSONParser().parse(conversionPolicyStr);
            for (Object key : conversionPolicyObj.keySet()) {
                JSONObject policyInfo = (JSONObject) conversionPolicyObj.get(key);
                String keyStr = ((String) key);
                policyInfoDTO.setId(policyInfo.get(RestApiConstants.SEQUENCE_ARTIFACT_ID).toString());
                policyInfoDTO.setHttpVerb(policyInfo.get(RestApiConstants.HTTP_METHOD).toString());
                if (keyStr.contains("_")) {
                    policyInfoDTO.setResourcePath(keyStr.substring(0, keyStr.lastIndexOf("_")));
                } else {
                    policyInfoDTO.setResourcePath(keyStr);
                }
                policyInfoDTO.setContent(policyInfo.get(RestApiConstants.SEQUENCE_CONTENT).toString());
            }
        } catch (ParseException e) {
            throw new APIManagementException("Couldn't parse the conversion policy string.", e);
        }
    }
    return policyInfoDTO;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ResourcePolicyInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO) ParseException(org.json.simple.parser.ParseException)

Example 2 with ResourcePolicyInfoDTO

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

the class ExportUtils method writeSOAPToRESTSequencesToArchive.

/**
 * Retrieve SOAP to REST mediation logic for the exporting API for a particular type (in/out) and store it
 * in the archive directory.
 *
 * @param api                   API
 * @param sequencePathInArchive Path to the SOAP to REST sequences in the archive
 * @param type                  Seqeunce type
 * @throws APIManagementException   If an error occurs while reading/writing SOAP to REST sequences
 * @throws APIImportExportException If an error occurs while creating the directory
 */
private static void writeSOAPToRESTSequencesToArchive(API api, String sequencePathInArchive, String type) throws APIManagementException, APIImportExportException {
    String resourcePolicy = SequenceUtils.getRestToSoapConvertedSequence(api, type);
    ResourcePolicyListDTO resourcePolicyInListDTO = APIMappingUtil.fromResourcePolicyStrToDTO(resourcePolicy);
    String individualSequencePathInArchive = sequencePathInArchive + File.separator + type;
    CommonUtil.createDirectory(individualSequencePathInArchive);
    for (ResourcePolicyInfoDTO resourcePolicyInfoDTO : resourcePolicyInListDTO.getList()) {
        String sequenceContent = resourcePolicyInfoDTO.getContent();
        String sequenceName = resourcePolicyInfoDTO.getResourcePath() + "_" + resourcePolicyInfoDTO.getHttpVerb();
        writeSequenceToArchive(sequenceContent, individualSequencePathInArchive, sequenceName);
    }
}
Also used : ResourcePolicyInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO) ResourcePolicyListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyListDTO)

Example 3 with ResourcePolicyInfoDTO

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

the class ApisApiServiceImpl method getAPIResourcePoliciesByPolicyId.

/**
 * Get the resource policy given the resource id.
 *
 * @param apiId           API ID
 * @param resourcePolicyId      resource policy id
 * @param ifNoneMatch     If-None-Match header value
 * @return json response of the resource policy for the resource id given
 */
@Override
public Response getAPIResourcePoliciesByPolicyId(String apiId, String resourcePolicyId, String ifNoneMatch, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider provider = RestApiCommonUtil.getLoggedInUserProvider();
        API api = provider.getLightweightAPIByUUID(apiId, organization);
        if (APIConstants.API_TYPE_SOAPTOREST.equals(api.getType())) {
            if (StringUtils.isEmpty(resourcePolicyId)) {
                String errorMessage = "Resource id should not be empty to update a resource policy.";
                RestApiUtil.handleBadRequest(errorMessage, log);
            }
            String policyContent = SequenceUtils.getResourcePolicyFromRegistryResourceId(api, resourcePolicyId);
            ResourcePolicyInfoDTO resourcePolicyInfoDTO = APIMappingUtil.fromResourcePolicyStrToInfoDTO(policyContent);
            return Response.ok().entity(resourcePolicyInfoDTO).build();
        } else {
            String errorMessage = "The provided api with id: " + apiId + " is not a soap to rest converted api.";
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving the API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ResourcePolicyInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 4 with ResourcePolicyInfoDTO

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

the class ApisApiServiceImpl method updateAPIResourcePoliciesByPolicyId.

/**
 * Update the resource policies(inflow/outflow) given the resource id.
 *
 * @param apiId  API ID
 * @param resourcePolicyId resource policy id
 * @param body resource policy content
 * @param ifMatch If-Match header value
 * @return json response of the updated sequence content
 */
@Override
public Response updateAPIResourcePoliciesByPolicyId(String apiId, String resourcePolicyId, ResourcePolicyInfoDTO body, String ifMatch, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider provider = RestApiCommonUtil.getLoggedInUserProvider();
        API api = provider.getAPIbyUUID(apiId, organization);
        if (api == null) {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
        }
        // validate API update operation permitted based on the LC state
        validateAPIOperationsPerLC(api.getStatus());
        if (APIConstants.API_TYPE_SOAPTOREST.equals(api.getType())) {
            if (StringUtils.isEmpty(resourcePolicyId)) {
                String errorMessage = "Resource id should not be empty to update a resource policy.";
                RestApiUtil.handleBadRequest(errorMessage, log);
            }
            boolean isValidSchema = RestApiPublisherUtils.validateXMLSchema(body.getContent());
            if (isValidSchema) {
                List<SOAPToRestSequence> sequence = api.getSoapToRestSequences();
                for (SOAPToRestSequence soapToRestSequence : sequence) {
                    if (soapToRestSequence.getUuid().equals(resourcePolicyId)) {
                        soapToRestSequence.setContent(body.getContent());
                        break;
                    }
                }
                API originalAPI = provider.getAPIbyUUID(apiId, organization);
                provider.updateAPI(api, originalAPI);
                SequenceUtils.updateResourcePolicyFromRegistryResourceId(api.getId(), resourcePolicyId, body.getContent());
                String updatedPolicyContent = SequenceUtils.getResourcePolicyFromRegistryResourceId(api, resourcePolicyId);
                ResourcePolicyInfoDTO resourcePolicyInfoDTO = APIMappingUtil.fromResourcePolicyStrToInfoDTO(updatedPolicyContent);
                return Response.ok().entity(resourcePolicyInfoDTO).build();
            } else {
                String errorMessage = "Error while validating the resource policy xml content for the API : " + apiId;
                RestApiUtil.handleInternalServerError(errorMessage, log);
            }
        } else {
            String errorMessage = "The provided api with id: " + apiId + " is not a soap to rest converted api.";
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
    } catch (APIManagementException | FaultGatewaysException e) {
        String errorMessage = "Error while retrieving the API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ResourcePolicyInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Example 5 with ResourcePolicyInfoDTO

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

the class APIMappingUtil method fromResourcePolicyStrToDTO.

/**
 * Creates  a list of conversion policies into a DTO.
 *
 * @param conversionPolicyStr conversion policies
 * @return ConversionPolicyListDTO object containing ConversionPolicyInfoDTOs
 * @throws APIManagementException
 */
public static ResourcePolicyListDTO fromResourcePolicyStrToDTO(String conversionPolicyStr) throws APIManagementException {
    ResourcePolicyListDTO policyListDTO = new ResourcePolicyListDTO();
    List<ResourcePolicyInfoDTO> policyInfoDTOs = policyListDTO.getList();
    if (StringUtils.isNotEmpty(conversionPolicyStr)) {
        try {
            JSONObject conversionPolicyObj = (JSONObject) new JSONParser().parse(conversionPolicyStr);
            for (Object key : conversionPolicyObj.keySet()) {
                JSONObject policyInfo = (JSONObject) conversionPolicyObj.get(key);
                String keyStr = ((String) key);
                ResourcePolicyInfoDTO policyInfoDTO = new ResourcePolicyInfoDTO();
                policyInfoDTO.setId(policyInfo.get(RestApiConstants.SEQUENCE_ARTIFACT_ID).toString());
                policyInfoDTO.setHttpVerb(policyInfo.get(RestApiConstants.HTTP_METHOD).toString());
                if (keyStr.contains("_")) {
                    policyInfoDTO.setResourcePath(keyStr.substring(0, keyStr.lastIndexOf("_")));
                } else {
                    policyInfoDTO.setResourcePath(keyStr);
                }
                policyInfoDTO.setContent(policyInfo.get(RestApiConstants.SEQUENCE_CONTENT).toString());
                policyInfoDTOs.add(policyInfoDTO);
            }
        } catch (ParseException e) {
            throw new APIManagementException("Couldn't parse the conversion policy string.", e);
        }
    }
    policyListDTO.setCount(policyInfoDTOs.size());
    return policyListDTO;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ResourcePolicyInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO) ParseException(org.json.simple.parser.ParseException) ResourcePolicyListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyListDTO)

Aggregations

ResourcePolicyInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO)5 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 JSONObject (org.json.simple.JSONObject)2 JSONParser (org.json.simple.parser.JSONParser)2 ParseException (org.json.simple.parser.ParseException)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 API (org.wso2.carbon.apimgt.api.model.API)2 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)2 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)2 ResourcePolicyListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyListDTO)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)1 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)1 SOAPToRestSequence (org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)1