Search in sources :

Example 6 with GatewayContentDTO

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

the class TemplateBuilderUtil method retrieveOperationPolicySequence.

private static GatewayContentDTO retrieveOperationPolicySequence(String pathToAchieve, API api, String flow) throws APIManagementException {
    GatewayContentDTO operationPolicySequenceContentDto = new GatewayContentDTO();
    String policySequence = null;
    String seqExt = APIUtil.getSequenceExtensionName(api) + SynapsePolicyAggregator.getSequenceExtensionFlow(flow);
    try {
        policySequence = SynapsePolicyAggregator.generatePolicySequenceForUriTemplateSet(api.getUriTemplates(), seqExt, flow, pathToAchieve);
    } catch (IOException e) {
        throw new APIManagementException(e);
    }
    if (StringUtils.isNotEmpty(policySequence)) {
        try {
            OMElement omElement = APIUtil.buildOMElement(new ByteArrayInputStream(policySequence.getBytes()));
            if (omElement != null) {
                if (omElement.getAttribute(new QName("name")) != null) {
                    omElement.getAttribute(new QName("name")).setAttributeValue(seqExt);
                }
                operationPolicySequenceContentDto.setName(seqExt);
                operationPolicySequenceContentDto.setContent(APIUtil.convertOMtoString(omElement));
                switch(flow) {
                    case APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST:
                        api.setInSequence(seqExt);
                        break;
                    case APIConstants.OPERATION_SEQUENCE_TYPE_RESPONSE:
                        api.setOutSequence(seqExt);
                        break;
                    case APIConstants.OPERATION_SEQUENCE_TYPE_FAULT:
                        api.setFaultSequence(seqExt);
                        break;
                }
                return operationPolicySequenceContentDto;
            }
        } 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) IOException(java.io.IOException) 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)

Example 7 with GatewayContentDTO

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

the class TemplateBuilderUtil method createAPIGatewayDTOtoPublishAPI.

private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environment, APIProduct apiProduct, APITemplateBuilder builder, String tenantDomain, Map<String, APIDTO> associatedAPIsMap, List<ClientCertificateDTO> clientCertificatesDTOList) throws APITemplateException, XMLStreamException, APIManagementException {
    APIProductIdentifier id = apiProduct.getId();
    GatewayAPIDTO productAPIDto = new GatewayAPIDTO();
    productAPIDto.setProvider(id.getProviderName());
    productAPIDto.setApiId(apiProduct.getUuid());
    productAPIDto.setName(id.getName());
    productAPIDto.setVersion(id.getVersion());
    productAPIDto.setTenantDomain(tenantDomain);
    productAPIDto.setKeyManagers(Collections.singletonList(APIConstants.KeyManager.API_LEVEL_ALL_KEY_MANAGERS));
    String definition = apiProduct.getDefinition();
    productAPIDto.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(apiProduct.getUuid(), productAPIDto.getLocalEntriesToBeRemove()));
    GatewayContentDTO productLocalEntry = new GatewayContentDTO();
    productLocalEntry.setName(apiProduct.getUuid());
    productLocalEntry.setContent("<localEntry key=\"" + apiProduct.getUuid() + "\">" + definition.replaceAll("&(?!amp;)", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;") + "</localEntry>");
    productAPIDto.setLocalEntriesToBeAdd(addGatewayContentToList(productLocalEntry, productAPIDto.getLocalEntriesToBeAdd()));
    setClientCertificatesToBeAdded(tenantDomain, productAPIDto, clientCertificatesDTOList);
    for (Map.Entry<String, APIDTO> apidtoEntry : associatedAPIsMap.entrySet()) {
        String apiExtractedPath = apidtoEntry.getKey();
        APIDTO apidto = apidtoEntry.getValue();
        API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider());
        api.setUuid(apidto.getId());
        GatewayUtils.setCustomSequencesToBeRemoved(apiProduct.getId(), api.getUuid(), productAPIDto);
        APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api, apiProduct);
        addEndpoints(api, apiTemplateBuilder, productAPIDto);
        setCustomSequencesToBeAdded(apiProduct, api, productAPIDto, apiExtractedPath, apidto);
        setAPIFaultSequencesToBeAdded(api, productAPIDto, apiExtractedPath, apidto);
        String prefix = id.getName() + "--v" + id.getVersion();
        setSecureVaultPropertyToBeAdded(prefix, api, productAPIDto);
    }
    productAPIDto.setApiDefinition(builder.getConfigStringForTemplate(environment));
    return productAPIDto;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APITemplateBuilder(org.wso2.carbon.apimgt.impl.template.APITemplateBuilder) API(org.wso2.carbon.apimgt.api.model.API) Map(java.util.Map) HashMap(java.util.HashMap) APITemplateBuilderImpl(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APITemplateBuilderImpl) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Example 8 with GatewayContentDTO

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

the class TemplateBuilderUtil method addEndpoints.

private static void addEndpoints(API api, APITemplateBuilder builder, GatewayAPIDTO gatewayAPIDTO) throws APITemplateException, XMLStreamException {
    ArrayList<String> arrayListToAdd = getEndpointType(api);
    for (String type : arrayListToAdd) {
        String endpointConfigContext = builder.getConfigStringForEndpointTemplate(type);
        GatewayContentDTO endpoint = new GatewayContentDTO();
        endpoint.setName(getEndpointName(endpointConfigContext));
        endpoint.setContent(endpointConfigContext);
        gatewayAPIDTO.setEndpointEntriesToBeAdd(addGatewayContentToList(endpoint, gatewayAPIDTO.getEndpointEntriesToBeAdd()));
    }
}
Also used : GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Example 9 with GatewayContentDTO

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

the class APIGatewayAdmin method deployAPI.

public boolean deployAPI(GatewayAPIDTO gatewayAPIDTO) throws AxisFault {
    CertificateManager certificateManager = CertificateManagerImpl.getInstance();
    SequenceAdminServiceProxy sequenceAdminServiceProxy = getSequenceAdminServiceClient(gatewayAPIDTO.getTenantDomain());
    RESTAPIAdminServiceProxy restapiAdminServiceProxy = getRestapiAdminClient(gatewayAPIDTO.getTenantDomain());
    LocalEntryServiceProxy localEntryServiceProxy = new LocalEntryServiceProxy(gatewayAPIDTO.getTenantDomain());
    EndpointAdminServiceProxy endpointAdminServiceProxy = new EndpointAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
    MediationSecurityAdminServiceProxy mediationSecurityAdminServiceProxy = new MediationSecurityAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
    if (log.isDebugEnabled()) {
        log.debug("Start to undeploy API" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    unDeployAPI(certificateManager, sequenceAdminServiceProxy, restapiAdminServiceProxy, localEntryServiceProxy, endpointAdminServiceProxy, gatewayAPIDTO, mediationSecurityAdminServiceProxy);
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " undeployed");
        log.debug("Start to deploy Local entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add Local Entries
    if (gatewayAPIDTO.getLocalEntriesToBeAdd() != null) {
        for (GatewayContentDTO localEntry : gatewayAPIDTO.getLocalEntriesToBeAdd()) {
            if (localEntryServiceProxy.isEntryExists(localEntry.getName())) {
                localEntryServiceProxy.deleteEntry(localEntry.getName());
                localEntryServiceProxy.addLocalEntry(localEntry.getContent());
            } else {
                localEntryServiceProxy.addLocalEntry(localEntry.getContent());
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Local Entries deployed");
        log.debug("Start to deploy Endpoint entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add Endpoints
    if (gatewayAPIDTO.getEndpointEntriesToBeAdd() != null) {
        for (GatewayContentDTO endpointEntry : gatewayAPIDTO.getEndpointEntriesToBeAdd()) {
            if (endpointAdminServiceProxy.isEndpointExist(endpointEntry.getName())) {
                endpointAdminServiceProxy.deleteEndpoint(endpointEntry.getName());
                endpointAdminServiceProxy.addEndpoint(endpointEntry.getContent());
            } else {
                endpointAdminServiceProxy.addEndpoint(endpointEntry.getContent());
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Endpoints deployed");
        log.debug("Start to deploy Client certificates" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add Client Certificates
    if (gatewayAPIDTO.getClientCertificatesToBeAdd() != null) {
        for (GatewayContentDTO certificate : gatewayAPIDTO.getClientCertificatesToBeAdd()) {
            certificateManager.addClientCertificateToGateway(certificate.getContent(), certificate.getName());
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " client certificates deployed");
        log.debug("Start to add vault entries " + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add vault entries
    if (gatewayAPIDTO.getCredentialsToBeAdd() != null) {
        for (CredentialDto certificate : gatewayAPIDTO.getCredentialsToBeAdd()) {
            try {
                String encryptedValue = mediationSecurityAdminServiceProxy.doEncryption(certificate.getPassword());
                if (mediationSecurityAdminServiceProxy.isAliasExist(certificate.getAlias())) {
                    setRegistryProperty(gatewayAPIDTO.getTenantDomain(), certificate.getAlias(), encryptedValue);
                } else {
                    setRegistryProperty(gatewayAPIDTO.getTenantDomain(), certificate.getAlias(), encryptedValue);
                }
            } catch (APIManagementException e) {
                log.error("Exception occurred while encrypting password.", e);
                throw new AxisFault(e.getMessage());
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Vault Entries Added successfully");
        log.debug("Start to deploy custom sequences" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add Sequences
    if (gatewayAPIDTO.getSequenceToBeAdd() != null) {
        for (GatewayContentDTO sequence : gatewayAPIDTO.getSequenceToBeAdd()) {
            OMElement element;
            try {
                element = AXIOMUtil.stringToOM(sequence.getContent());
            } catch (XMLStreamException e) {
                log.error("Exception occurred while converting String to an OM.", e);
                throw new AxisFault(e.getMessage());
            }
            if (sequenceAdminServiceProxy.isExistingSequence(sequence.getName())) {
                sequenceAdminServiceProxy.deleteSequence(sequence.getName());
                sequenceAdminServiceProxy.addSequence(element);
            } else {
                sequenceAdminServiceProxy.addSequence(element);
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " custom sequences deployed");
        log.debug("Start to deploy API Definition" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add API
    if (StringUtils.isNotEmpty(gatewayAPIDTO.getApiDefinition())) {
        restapiAdminServiceProxy.addApi(gatewayAPIDTO.getApiDefinition());
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " API Definition deployed");
        log.debug("Start to deploy Default API Definition" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Default API Definition deployed");
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + "Deployed successfully");
    }
    return true;
}
Also used : AxisFault(org.apache.axis2.AxisFault) MediationSecurityAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.MediationSecurityAdminServiceProxy) CertificateManager(org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager) OMElement(org.apache.axiom.om.OMElement) RESTAPIAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.RESTAPIAdminServiceProxy) CredentialDto(org.wso2.carbon.apimgt.api.gateway.CredentialDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) XMLStreamException(javax.xml.stream.XMLStreamException) LocalEntryServiceProxy(org.wso2.carbon.apimgt.gateway.utils.LocalEntryServiceProxy) EndpointAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.EndpointAdminServiceProxy) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) SequenceAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.SequenceAdminServiceProxy)

Example 10 with GatewayContentDTO

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

the class TemplateBuilderUtilTest method addAddGqlWebSocketResourceEndpoints.

@Test
public void addAddGqlWebSocketResourceEndpoints() throws Exception {
    APITemplateBuilder apiTemplateBuilder = Mockito.mock(APITemplateBuilder.class);
    API api = new API(new APIIdentifier("admin", "API", "1.0"));
    Set<URITemplate> uriTemplates = new HashSet<>();
    URITemplate template = new URITemplate();
    template.setAuthType("Any");
    template.setHTTPVerb("POST");
    template.setHttpVerbs("POST");
    template.setUriTemplate(wildCardResource);
    uriTemplates.add(template);
    template = new URITemplate();
    template.setUriTemplate(wildCardResource);
    uriTemplates.add(template);
    api.setUriTemplates(uriTemplates);
    Map<String, Map<String, String>> perTopicMappings = new HashMap<>();
    Map<String, String> endpoints = new HashMap<>();
    endpoints.put(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, wsProdEndpoint);
    endpoints.put(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, wsSandEndpoint);
    perTopicMappings.put(wildCardResource, endpoints);
    WebSocketTopicMappingConfiguration webSocketTopicMappingConfiguration = new WebSocketTopicMappingConfiguration(perTopicMappings);
    webSocketTopicMappingConfiguration.setResourceKey(wildCardResource, mappingWildCard);
    api.setWebSocketTopicMappingConfiguration(webSocketTopicMappingConfiguration);
    api.setType(APIConstants.GRAPHQL_API);
    GatewayAPIDTO gatewayAPIDTO = new GatewayAPIDTO();
    GatewayContentDTO dummyHttpProdEpContentDTO = new GatewayContentDTO();
    dummyHttpProdEpContentDTO.setName("API--v1.0_APIproductionEndpoint");
    dummyHttpProdEpContentDTO.setContent("dummy content");
    GatewayContentDTO[] gatewayContentDTOS = new GatewayContentDTO[1];
    gatewayContentDTOS[0] = dummyHttpProdEpContentDTO;
    gatewayAPIDTO.setEndpointEntriesToBeAdd(gatewayContentDTOS);
    String dummyProdEndpointConfig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><endpoint name=\"" + wsProdEpName + "\">dummy prod content</endpoint>";
    String dummySandboxEndpointConfig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><endpoint name=\"" + wsSandEpName + "\">dummy sandbox content</endpoint>";
    Mockito.when(apiTemplateBuilder.getConfigStringForWebSocketEndpointTemplate(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, mappingWildCard, wsProdEndpoint)).thenReturn(dummyProdEndpointConfig);
    Mockito.when(apiTemplateBuilder.getConfigStringForWebSocketEndpointTemplate(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, mappingWildCard, wsSandEndpoint)).thenReturn(dummySandboxEndpointConfig);
    TemplateBuilderUtil.addWebSocketResourceEndpoints(api, apiTemplateBuilder, gatewayAPIDTO);
    GatewayContentDTO[] endpointEntries = gatewayAPIDTO.getEndpointEntriesToBeAdd();
    Assert.assertEquals(endpointEntries.length, 3);
    boolean httpEpConfigPresent = false;
    boolean wsProdEpConfigPresent = false;
    boolean wsSandEPConfigPresent = false;
    for (int i = 0; i < 3; i++) {
        if (dummyHttpProdEpContentDTO.getName().equals(endpointEntries[i].getName())) {
            httpEpConfigPresent = true;
            Assert.assertEquals(endpointEntries[i].getContent(), dummyHttpProdEpContentDTO.getContent());
        }
        if (wsProdEpName.equals(endpointEntries[i].getName())) {
            wsProdEpConfigPresent = true;
            Assert.assertEquals(endpointEntries[i].getContent(), dummyProdEndpointConfig);
        }
        if (wsSandEpName.equals(endpointEntries[i].getName())) {
            wsSandEPConfigPresent = true;
            Assert.assertEquals(endpointEntries[i].getContent(), dummySandboxEndpointConfig);
        }
    }
    Assert.assertTrue(wsProdEpConfigPresent);
    Assert.assertTrue(wsSandEPConfigPresent);
    Assert.assertTrue(httpEpConfigPresent);
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) HashMap(java.util.HashMap) WebSocketTopicMappingConfiguration(org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APITemplateBuilder(org.wso2.carbon.apimgt.impl.template.APITemplateBuilder) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashMap(java.util.HashMap) Map(java.util.Map) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

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