Search in sources :

Example 11 with GatewayContentDTO

use of org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method retrieveSequence.

private static GatewayContentDTO retrieveSequence(APIProduct apiProduct, String pathToAchieve, List<MediationPolicyDTO> mediationPolicies, String type, API api) throws APIManagementException {
    APIProductIdentifier apiProductIdentifier = apiProduct.getId();
    MediationPolicyDTO mediationPolicyDTO = null;
    for (MediationPolicyDTO mediationPolicy : mediationPolicies) {
        if (type.equalsIgnoreCase(mediationPolicy.getType())) {
            mediationPolicyDTO = mediationPolicy;
            break;
        }
    }
    if (mediationPolicyDTO != null) {
        GatewayContentDTO sequenceContentDto = new GatewayContentDTO();
        String sequenceContent = ImportUtils.retrieveSequenceContent(pathToAchieve, !mediationPolicyDTO.isShared(), type.toLowerCase(), mediationPolicyDTO.getName());
        if (StringUtils.isNotEmpty(sequenceContent)) {
            try {
                OMElement omElement = APIUtil.buildOMElement(new ByteArrayInputStream(sequenceContent.getBytes()));
                if (omElement != null) {
                    String seqExt = APIUtil.getSequenceExtensionName(apiProductIdentifier.getName(), apiProductIdentifier.getVersion()).concat("--").concat(api.getUuid());
                    if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT.equalsIgnoreCase(type)) {
                        seqExt = seqExt + APIConstants.API_CUSTOM_SEQ_FAULT_EXT;
                    } else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT.equalsIgnoreCase(type)) {
                        seqExt = seqExt + APIConstants.API_CUSTOM_SEQ_OUT_EXT;
                    } else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN.equalsIgnoreCase(type)) {
                        seqExt = seqExt + APIConstants.API_CUSTOM_SEQ_IN_EXT;
                    }
                    if (omElement.getAttribute(new QName("name")) != null) {
                        omElement.getAttribute(new QName("name")).setAttributeValue(seqExt);
                    }
                    sequenceContentDto.setName(seqExt);
                    sequenceContentDto.setContent(APIUtil.convertOMtoString(omElement));
                    return sequenceContentDto;
                }
            } catch (Exception e) {
                throw new APIManagementException(e);
            }
        }
    }
    return null;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) JSONException(org.json.JSONException) XMLStreamException(javax.xml.stream.XMLStreamException) CertificateManagementException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MediationPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MediationPolicyDTO)

Example 12 with GatewayContentDTO

use of org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method addWebSocketResourceEndpoints.

public static void addWebSocketResourceEndpoints(API api, APITemplateBuilder builder, GatewayAPIDTO gatewayAPIDTO) throws APITemplateException, XMLStreamException {
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    Map<String, Map<String, String>> topicMappings = api.getWebSocketTopicMappingConfiguration().getMappings();
    List<GatewayContentDTO> endpointsToAdd = new ArrayList<>();
    for (URITemplate resource : uriTemplates) {
        String topic = resource.getUriTemplate();
        Map<String, String> endpoints = topicMappings.get(topic);
        // Production and Sandbox endpoints
        for (Map.Entry<String, String> endpointData : endpoints.entrySet()) {
            if (!"resourceKey".equals(endpointData.getKey())) {
                String endpointType = endpointData.getKey();
                String endpointUrl = endpointData.getValue();
                String endpointConfigContext = builder.getConfigStringForWebSocketEndpointTemplate(endpointType, getWebsocketResourceKey(topic), endpointUrl);
                GatewayContentDTO endpoint = new GatewayContentDTO();
                // For WS APIs, resource type is not applicable,
                // so we can just use the uriTemplate/uriMapping to identify the resource
                endpoint.setName(getEndpointName(endpointConfigContext));
                endpoint.setContent(endpointConfigContext);
                endpointsToAdd.add(endpoint);
            }
        }
        // once through resources is enough.
        if (APIConstants.GRAPHQL_API.equals(api.getType())) {
            break;
        }
    }
    gatewayAPIDTO.setEndpointEntriesToBeAdd(addGatewayContentsToList(endpointsToAdd, gatewayAPIDTO.getEndpointEntriesToBeAdd()));
}
Also used : URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Example 13 with GatewayContentDTO

use of org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method setAPIFaultSequencesToBeAdded.

private static void setAPIFaultSequencesToBeAdded(API api, GatewayAPIDTO gatewayAPIDTO, String extractedPath, APIDTO apidto) throws APIManagementException {
    String faultSeqExt = APIUtil.getSequenceExtensionName(api) + APIConstants.API_CUSTOM_SEQ_FAULT_EXT;
    if (!APIConstants.APITransportType.HTTP.toString().equals(api.getType())) {
        gatewayAPIDTO.setSequencesToBeRemove(GatewayUtils.addStringToList(faultSeqExt, gatewayAPIDTO.getSequencesToBeRemove()));
        List<MediationPolicyDTO> mediationPolicies = apidto.getMediationPolicies();
        GatewayContentDTO faultSequenceContent = retrieveSequence(extractedPath, mediationPolicies, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, api);
        if (faultSequenceContent != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(faultSequenceContent, gatewayAPIDTO.getSequenceToBeAdd()));
        }
    }
    gatewayAPIDTO.setSequencesToBeRemove(GatewayUtils.addStringToList(faultSeqExt, gatewayAPIDTO.getSequencesToBeRemove()));
}
Also used : GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) MediationPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MediationPolicyDTO)

Example 14 with GatewayContentDTO

use of org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method setCustomSequencesToBeAdded.

private static void setCustomSequencesToBeAdded(APIProduct apiProduct, API api, GatewayAPIDTO gatewayAPIDTO, String extractedPath, APIDTO apidto) throws APIManagementException {
    if (APIConstants.APITransportType.HTTP.toString().equals(api.getType())) {
        GatewayContentDTO gatewayInContentDTO = retrieveOperationPolicySequenceForProducts(apiProduct, api, extractedPath, APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST);
        if (gatewayInContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayInContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
        GatewayContentDTO gatewayOutContentDTO = retrieveOperationPolicySequenceForProducts(apiProduct, api, extractedPath, APIConstants.OPERATION_SEQUENCE_TYPE_RESPONSE);
        if (gatewayOutContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayOutContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
        GatewayContentDTO gatewayFaultContentDTO = retrieveOperationPolicySequenceForProducts(apiProduct, api, extractedPath, APIConstants.OPERATION_SEQUENCE_TYPE_FAULT);
        if (gatewayFaultContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayFaultContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
    } else {
        GatewayContentDTO gatewayInContentDTO = retrieveSequence(apiProduct, extractedPath, apidto.getMediationPolicies(), APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, api);
        if (gatewayInContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayInContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
        GatewayContentDTO gatewayOutContentDTO = retrieveSequence(apiProduct, extractedPath, apidto.getMediationPolicies(), APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, api);
        if (gatewayOutContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayOutContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
    }
}
Also used : GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Example 15 with GatewayContentDTO

use of org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method retrieveSequence.

private static GatewayContentDTO retrieveSequence(String pathToAchieve, List<MediationPolicyDTO> mediationPolicies, String type, API api) throws APIManagementException {
    MediationPolicyDTO mediationPolicyDTO = null;
    for (MediationPolicyDTO mediationPolicy : mediationPolicies) {
        if (type.equalsIgnoreCase(mediationPolicy.getType())) {
            mediationPolicyDTO = mediationPolicy;
            break;
        }
    }
    if (mediationPolicyDTO != null) {
        GatewayContentDTO sequenceContentDto = new GatewayContentDTO();
        String sequenceContent = ImportUtils.retrieveSequenceContent(pathToAchieve, !mediationPolicyDTO.isShared(), type.toLowerCase(), mediationPolicyDTO.getName());
        if (StringUtils.isNotEmpty(sequenceContent)) {
            try {
                OMElement omElement = APIUtil.buildOMElement(new ByteArrayInputStream(sequenceContent.getBytes()));
                if (omElement != null) {
                    String seqExt = APIUtil.getSequenceExtensionName(api);
                    if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT.equalsIgnoreCase(type)) {
                        seqExt = seqExt + APIConstants.API_CUSTOM_SEQ_FAULT_EXT;
                    } else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT.equalsIgnoreCase(type)) {
                        seqExt = seqExt + APIConstants.API_CUSTOM_SEQ_OUT_EXT;
                    } else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN.equalsIgnoreCase(type)) {
                        seqExt = seqExt + APIConstants.API_CUSTOM_SEQ_IN_EXT;
                    }
                    if (omElement.getAttribute(new QName("name")) != null) {
                        omElement.getAttribute(new QName("name")).setAttributeValue(seqExt);
                    }
                    sequenceContentDto.setName(seqExt);
                    sequenceContentDto.setContent(APIUtil.convertOMtoString(omElement));
                    return sequenceContentDto;
                }
            } catch (Exception e) {
                throw new APIManagementException(e);
            }
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) JSONException(org.json.JSONException) XMLStreamException(javax.xml.stream.XMLStreamException) CertificateManagementException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MediationPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MediationPolicyDTO)

Aggregations

GatewayContentDTO (org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)15 XMLStreamException (javax.xml.stream.XMLStreamException)5 OMElement (org.apache.axiom.om.OMElement)5 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 QName (javax.xml.namespace.QName)4 JSONException (org.json.JSONException)4 ParseException (org.json.simple.parser.ParseException)4 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)4 CertificateManagementException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException)4 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)3 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)3 MediationPolicyDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MediationPolicyDTO)3 ArrayList (java.util.ArrayList)2 API (org.wso2.carbon.apimgt.api.model.API)2