Search in sources :

Example 1 with ResourcePolicyListDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyListDTO 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 2 with ResourcePolicyListDTO

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

the class ApisApiServiceImpl method getAPIResourcePolicies.

/**
 * Get the resource policies(inflow/outflow).
 *
 * @param apiId           API ID
 * @param sequenceType    sequence type('in' or 'out')
 * @param resourcePath    api resource path
 * @param verb            http verb
 * @param ifNoneMatch     If-None-Match header value
 * @return json response of the resource policies according to the resource path
 */
@Override
public Response getAPIResourcePolicies(String apiId, String sequenceType, String resourcePath, String verb, 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(sequenceType) || !(RestApiConstants.IN_SEQUENCE.equals(sequenceType) || RestApiConstants.OUT_SEQUENCE.equals(sequenceType))) {
                String errorMessage = "Sequence type should be either of the values from 'in' or 'out'";
                RestApiUtil.handleBadRequest(errorMessage, log);
            }
            String resourcePolicy = SequenceUtils.getRestToSoapConvertedSequence(api, sequenceType);
            if (StringUtils.isEmpty(resourcePath) && StringUtils.isEmpty(verb)) {
                ResourcePolicyListDTO resourcePolicyListDTO = APIMappingUtil.fromResourcePolicyStrToDTO(resourcePolicy);
                return Response.ok().entity(resourcePolicyListDTO).build();
            }
            if (StringUtils.isNotEmpty(resourcePath) && StringUtils.isNotEmpty(verb)) {
                JSONObject sequenceObj = (JSONObject) new JSONParser().parse(resourcePolicy);
                JSONObject resultJson = new JSONObject();
                String key = resourcePath + "_" + verb;
                JSONObject sequenceContent = (JSONObject) sequenceObj.get(key);
                if (sequenceContent == null) {
                    String errorMessage = "Cannot find any resource policy for Resource path : " + resourcePath + " with type: " + verb;
                    RestApiUtil.handleResourceNotFoundError(errorMessage, log);
                }
                resultJson.put(key, sequenceObj.get(key));
                ResourcePolicyListDTO resourcePolicyListDTO = APIMappingUtil.fromResourcePolicyStrToDTO(resultJson.toJSONString());
                return Response.ok().entity(resourcePolicyListDTO).build();
            } else if (StringUtils.isEmpty(resourcePath)) {
                String errorMessage = "Resource path cannot be empty for the defined verb: " + verb;
                RestApiUtil.handleBadRequest(errorMessage, log);
            } else if (StringUtils.isEmpty(verb)) {
                String errorMessage = "HTTP verb cannot be empty for the defined resource path: " + resourcePath;
                RestApiUtil.handleBadRequest(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 e) {
        String errorMessage = "Error while retrieving the API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (ParseException e) {
        String errorMessage = "Error while retrieving the resource policies for the API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) 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) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) ResourcePolicyListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyListDTO)

Example 3 with ResourcePolicyListDTO

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

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