Search in sources :

Example 1 with APITemplateBuilder

use of org.wso2.carbon.apimgt.impl.template.APITemplateBuilder in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method retrieveGatewayAPIDto.

public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environment, String tenantDomain, APIDTO apidto, String extractedFolderPath) throws APIManagementException, XMLStreamException, APITemplateException {
    List<ClientCertificateDTO> clientCertificatesDTOList = ImportUtils.retrieveClientCertificates(extractedFolderPath);
    List<SoapToRestMediationDto> soapToRestInMediationDtoList = ImportUtils.retrieveSoapToRestFlowMediations(extractedFolderPath, ImportUtils.IN);
    List<SoapToRestMediationDto> soapToRestOutMediationDtoList = ImportUtils.retrieveSoapToRestFlowMediations(extractedFolderPath, ImportUtils.OUT);
    JSONObject originalProperties = api.getAdditionalProperties();
    // add new property for entires that has a __display suffix
    JSONObject modifiedProperties = getModifiedProperties(originalProperties);
    api.setAdditionalProperties(modifiedProperties);
    APITemplateBuilder apiTemplateBuilder = TemplateBuilderUtil.getAPITemplateBuilder(api, tenantDomain, clientCertificatesDTOList, soapToRestInMediationDtoList, soapToRestOutMediationDtoList);
    GatewayAPIDTO gatewaAPIDto = createAPIGatewayDTOtoPublishAPI(environment, api, apiTemplateBuilder, tenantDomain, extractedFolderPath, apidto, clientCertificatesDTOList);
    // Reset the additional properties to the original values
    if (originalProperties != null) {
        api.setAdditionalProperties(originalProperties);
    }
    return gatewaAPIDto;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) JSONObject(org.json.simple.JSONObject) SoapToRestMediationDto(org.wso2.carbon.apimgt.impl.dto.SoapToRestMediationDto) APITemplateBuilder(org.wso2.carbon.apimgt.impl.template.APITemplateBuilder) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)

Example 2 with APITemplateBuilder

use of org.wso2.carbon.apimgt.impl.template.APITemplateBuilder in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method createAPIGatewayDTOtoPublishAPI.

private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environment, API api, APITemplateBuilder builder, String tenantDomain, String extractedPath, APIDTO apidto, List<ClientCertificateDTO> clientCertificatesDTOList) throws APIManagementException, APITemplateException, XMLStreamException {
    GatewayAPIDTO gatewayAPIDTO = new GatewayAPIDTO();
    gatewayAPIDTO.setName(api.getId().getName());
    gatewayAPIDTO.setVersion(api.getId().getVersion());
    gatewayAPIDTO.setProvider(api.getId().getProviderName());
    gatewayAPIDTO.setApiId(api.getUUID());
    gatewayAPIDTO.setTenantDomain(tenantDomain);
    gatewayAPIDTO.setKeyManagers(api.getKeyManagers());
    String definition;
    boolean isGraphQLSubscriptionAPI = false;
    if (api.getType() != null && APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) {
        // Build schema with additional info
        gatewayAPIDTO.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(api.getUUID() + "_graphQL", gatewayAPIDTO.getLocalEntriesToBeRemove()));
        GatewayContentDTO graphqlLocalEntry = new GatewayContentDTO();
        graphqlLocalEntry.setName(api.getUUID() + "_graphQL");
        graphqlLocalEntry.setContent("<localEntry key=\"" + api.getUUID() + "_graphQL" + "\">" + "<![CDATA[" + api.getGraphQLSchema() + "]]>" + "</localEntry>");
        gatewayAPIDTO.setLocalEntriesToBeAdd(addGatewayContentToList(graphqlLocalEntry, gatewayAPIDTO.getLocalEntriesToBeAdd()));
        gatewayAPIDTO.setGraphQLSchema(api.getGraphQLSchema());
        Set<URITemplate> uriTemplates = new HashSet<>();
        URITemplate template = new URITemplate();
        template.setAuthType("Any");
        template.setHTTPVerb("POST");
        template.setHttpVerbs("POST");
        template.setUriTemplate("/*");
        uriTemplates.add(template);
        api.setUriTemplates(uriTemplates);
        GraphQLSchemaDefinition graphql = new GraphQLSchemaDefinition();
        if (graphql.isSubscriptionAvailable(api.getGraphQLSchema())) {
            isGraphQLSubscriptionAPI = true;
            // if subscriptions are available add new URI template with wild card resource without http verb.
            template = new URITemplate();
            template.setUriTemplate("/*");
            uriTemplates.add(template);
            api.setUriTemplates(uriTemplates);
            api.setEndpointConfig(populateSubscriptionEndpointConfig(api.getEndpointConfig()));
            addGqlWebSocketTopicMappings(api);
        }
    } else if (api.getType() != null && (APIConstants.APITransportType.HTTP.toString().equals(api.getType()) || APIConstants.API_TYPE_SOAP.equals(api.getType()) || APIConstants.API_TYPE_SOAPTOREST.equals(api.getType()))) {
        definition = api.getSwaggerDefinition();
        gatewayAPIDTO.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(api.getUUID(), gatewayAPIDTO.getLocalEntriesToBeRemove()));
        GatewayContentDTO apiLocalEntry = new GatewayContentDTO();
        apiLocalEntry.setName(api.getUUID());
        apiLocalEntry.setContent("<localEntry key=\"" + api.getUUID() + "\">" + definition.replaceAll("&(?!amp;)", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;") + "</localEntry>");
        gatewayAPIDTO.setLocalEntriesToBeAdd(addGatewayContentToList(apiLocalEntry, gatewayAPIDTO.getLocalEntriesToBeAdd()));
    } else if (api.getType() != null && (APIConstants.APITransportType.WS.toString().equals(api.getType()) || APIConstants.APITransportType.SSE.toString().equals(api.getType()) || APIConstants.APITransportType.WEBSUB.toString().equals(api.getType()))) {
        gatewayAPIDTO.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(api.getUUID(), gatewayAPIDTO.getLocalEntriesToBeRemove()));
        definition = api.getAsyncApiDefinition();
        GatewayContentDTO apiLocalEntry = new GatewayContentDTO();
        apiLocalEntry.setName(api.getUUID());
        apiLocalEntry.setContent("<localEntry key=\"" + api.getUUID() + "\">" + definition.replaceAll("&(?!amp;)", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;") + "</localEntry>");
        gatewayAPIDTO.setLocalEntriesToBeAdd(addGatewayContentToList(apiLocalEntry, gatewayAPIDTO.getLocalEntriesToBeAdd()));
    }
    if ((APIConstants.GATEWAY_ENV_TYPE_PRODUCTION.equals(environment.getType()) && !APIUtil.isProductionEndpointsExists(api.getEndpointConfig())) || (APIConstants.GATEWAY_ENV_TYPE_SANDBOX.equals(environment.getType()) && !APIUtil.isSandboxEndpointsExists(api.getEndpointConfig()))) {
        if (log.isDebugEnabled()) {
            log.debug("Not adding API to environment " + environment.getName() + " since its endpoint URL " + "cannot be found");
        }
        return null;
    }
    GatewayUtils.setCustomSequencesToBeRemoved(api, gatewayAPIDTO);
    setAPIFaultSequencesToBeAdded(api, gatewayAPIDTO, extractedPath, apidto);
    setCustomSequencesToBeAdded(api, gatewayAPIDTO, extractedPath, apidto);
    setClientCertificatesToBeAdded(tenantDomain, gatewayAPIDTO, clientCertificatesDTOList);
    boolean isWsApi = APIConstants.APITransportType.WS.toString().equals(api.getType());
    if (isWsApi) {
        addWebsocketTopicMappings(api, apidto);
    }
    // Add the API
    if (APIConstants.IMPLEMENTATION_TYPE_INLINE.equalsIgnoreCase(api.getImplementation())) {
        String prototypeScriptAPI = builder.getConfigStringForPrototypeScriptAPI(environment);
        gatewayAPIDTO.setApiDefinition(prototypeScriptAPI);
    } else if (APIConstants.IMPLEMENTATION_TYPE_ENDPOINT.equalsIgnoreCase(api.getImplementation())) {
        String apiConfig = builder.getConfigStringForTemplate(environment);
        gatewayAPIDTO.setApiDefinition(apiConfig);
        org.json.JSONObject endpointConfig = new org.json.JSONObject(api.getEndpointConfig());
        if (!endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).equals(APIConstants.ENDPOINT_TYPE_AWSLAMBDA)) {
            if (!isWsApi) {
                addEndpoints(api, builder, gatewayAPIDTO);
            }
            if (isWsApi || isGraphQLSubscriptionAPI) {
                addWebSocketResourceEndpoints(api, builder, gatewayAPIDTO);
            }
        }
    }
    setSecureVaultPropertyToBeAdded(null, api, gatewayAPIDTO);
    return gatewayAPIDTO;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) JSONObject(org.json.simple.JSONObject) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) HashSet(java.util.HashSet)

Example 3 with APITemplateBuilder

use of org.wso2.carbon.apimgt.impl.template.APITemplateBuilder in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method retrieveGatewayAPIDto.

public static GatewayAPIDTO retrieveGatewayAPIDto(APIProduct apiProduct, Environment environment, String tenantDomain, String extractedFolderPath) throws APIManagementException, XMLStreamException, APITemplateException {
    List<ClientCertificateDTO> clientCertificatesDTOList = ImportUtils.retrieveClientCertificates(extractedFolderPath);
    Map<String, APIDTO> apidtoMap = retrieveAssociatedApis(extractedFolderPath);
    Map<String, APIDTO> associatedAPIsMap = convertAPIIdToDto(apidtoMap.values());
    for (APIProductResource productResource : apiProduct.getProductResources()) {
        String apiId = productResource.getApiId();
        APIDTO apidto = associatedAPIsMap.get(apiId);
        if (apidto != null) {
            API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider());
            productResource.setApiIdentifier(api.getId());
            if (api.isAdvertiseOnly()) {
                productResource.setEndpointConfig(APIUtil.generateEndpointConfigForAdvertiseOnlyApi(api));
            } else {
                productResource.setEndpointConfig(api.getEndpointConfig());
            }
            if (StringUtils.isNotEmpty(api.getInSequence())) {
                String sequenceName = APIUtil.getSequenceExtensionName(apiProduct.getId().getName(), apiProduct.getId().getVersion()).concat("--").concat(productResource.getApiId()) + APIConstants.API_CUSTOM_SEQ_IN_EXT;
                productResource.setInSequenceName(sequenceName);
            }
            if (StringUtils.isNotEmpty(api.getOutSequence())) {
                String sequenceName = APIUtil.getSequenceExtensionName(apiProduct.getId().getName(), apiProduct.getId().getVersion()).concat("--").concat(productResource.getApiId()) + APIConstants.API_CUSTOM_SEQ_OUT_EXT;
                productResource.setOutSequenceName(sequenceName);
            }
            if (StringUtils.isNotEmpty(api.getFaultSequence())) {
                String sequenceName = APIUtil.getSequenceExtensionName(apiProduct.getId().getName(), apiProduct.getId().getVersion()).concat("--").concat(productResource.getApiId()) + APIConstants.API_CUSTOM_SEQ_FAULT_EXT;
                productResource.setFaultSequenceName(sequenceName);
            }
            productResource.setProductIdentifier(apiProduct.getId());
            productResource.setEndpointSecurityMap(APIUtil.setEndpointSecurityForAPIProduct(api));
        }
    }
    APITemplateBuilder apiTemplateBuilder = TemplateBuilderUtil.getAPITemplateBuilder(apiProduct, tenantDomain, clientCertificatesDTOList, convertAPIIdToDto(associatedAPIsMap.values()));
    return createAPIGatewayDTOtoPublishAPI(environment, apiProduct, apiTemplateBuilder, tenantDomain, apidtoMap, clientCertificatesDTOList);
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APITemplateBuilder(org.wso2.carbon.apimgt.impl.template.APITemplateBuilder) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) API(org.wso2.carbon.apimgt.api.model.API)

Example 4 with APITemplateBuilder

use of org.wso2.carbon.apimgt.impl.template.APITemplateBuilder 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 5 with APITemplateBuilder

use of org.wso2.carbon.apimgt.impl.template.APITemplateBuilder 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)

Aggregations

GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)5 GatewayContentDTO (org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)5 API (org.wso2.carbon.apimgt.api.model.API)5 APITemplateBuilder (org.wso2.carbon.apimgt.impl.template.APITemplateBuilder)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.Test)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)3 APITemplateBuilderImpl (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APITemplateBuilderImpl)3 HashSet (java.util.HashSet)2 JSONObject (org.json.simple.JSONObject)2 ClientCertificateDTO (org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)2 Environment (org.wso2.carbon.apimgt.api.model.Environment)2 WebsubSubscriptionConfiguration (org.wso2.carbon.apimgt.api.model.WebsubSubscriptionConfiguration)2 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)2 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)2 APIManagerConfigurationServiceImpl (org.wso2.carbon.apimgt.impl.APIManagerConfigurationServiceImpl)2 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO)2 ArrayList (java.util.ArrayList)1