Search in sources :

Example 1 with GatewayAPIDTO

use of org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO 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 GatewayAPIDTO

use of org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO 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 GatewayAPIDTO

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

the class TemplateBuilderUtil method setCustomSequencesToBeAdded.

private static void setCustomSequencesToBeAdded(API api, GatewayAPIDTO gatewayAPIDTO, String extractedPath, APIDTO apidto) throws APIManagementException {
    if (APIConstants.APITransportType.HTTP.toString().equals(api.getType())) {
        GatewayContentDTO gatewayInContentDTO = retrieveOperationPolicySequence(extractedPath, api, APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST);
        if (gatewayInContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayInContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
        GatewayContentDTO gatewayOutContentDTO = retrieveOperationPolicySequence(extractedPath, api, APIConstants.OPERATION_SEQUENCE_TYPE_RESPONSE);
        if (gatewayOutContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayOutContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
        GatewayContentDTO gatewayFaultContentDTO = retrieveOperationPolicySequence(extractedPath, api, APIConstants.OPERATION_SEQUENCE_TYPE_FAULT);
        if (gatewayFaultContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayFaultContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
    } else {
        GatewayContentDTO gatewayInContentDTO = retrieveSequence(extractedPath, apidto.getMediationPolicies(), APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, api);
        if (gatewayInContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayInContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
        GatewayContentDTO gatewayOutContentDTO = retrieveSequence(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 4 with GatewayAPIDTO

use of org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO 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, APIDefinitionValidationResponse apiDefinitionValidationResponse) throws APIManagementException, XMLStreamException, APITemplateException, CertificateManagementException {
    if (apiDefinitionValidationResponse.isValid()) {
        APIDefinition parser = apiDefinitionValidationResponse.getParser();
        String definition = apiDefinitionValidationResponse.getJsonContent();
        if (parser != null) {
            Set<URITemplate> uriTemplates = parser.getURITemplates(definition);
            for (URITemplate uriTemplate : uriTemplates) {
                for (URITemplate template : api.getUriTemplates()) {
                    if (template.getHTTPVerb().equalsIgnoreCase(uriTemplate.getHTTPVerb()) && template.getUriTemplate().equals(uriTemplate.getUriTemplate())) {
                        template.setMediationScript(uriTemplate.getMediationScript());
                        template.setMediationScripts(uriTemplate.getHTTPVerb(), uriTemplate.getMediationScript());
                        template.setAmznResourceName(uriTemplate.getAmznResourceName());
                        template.setAmznResourceTimeout(uriTemplate.getAmznResourceTimeout());
                        break;
                    }
                }
            }
        }
    }
    return retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath);
}
Also used : APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Example 5 with GatewayAPIDTO

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

the class TemplateBuilderUtil method addCredentialsToList.

private static void addCredentialsToList(String prefix, API api, GatewayAPIDTO gatewayAPIDTO, org.json.JSONObject endpointSecurity, String type) {
    if (APIConstants.ENDPOINT_SECURITY_TYPE_OAUTH.equalsIgnoreCase((String) endpointSecurity.get(APIConstants.ENDPOINT_SECURITY_TYPE))) {
        CredentialDto clientSecretDto = new CredentialDto();
        if (StringUtils.isNotEmpty(prefix)) {
            clientSecretDto.setAlias(prefix.concat("--").concat(GatewayUtils.retrieveOauthClientSecretAlias(api.getId().getApiName(), api.getId().getVersion(), type)));
        } else {
            clientSecretDto.setAlias(GatewayUtils.retrieveOauthClientSecretAlias(api.getId().getApiName(), api.getId().getVersion(), type));
        }
        clientSecretDto.setPassword((String) endpointSecurity.get(APIConstants.ENDPOINT_SECURITY_CLIENT_SECRET));
        gatewayAPIDTO.setCredentialsToBeAdd(addCredentialsToList(clientSecretDto, gatewayAPIDTO.getCredentialsToBeAdd()));
        if (endpointSecurity.has(APIConstants.ENDPOINT_SECURITY_PASSWORD)) {
            CredentialDto passwordDto = new CredentialDto();
            if (StringUtils.isNotEmpty(prefix)) {
                passwordDto.setAlias(prefix.concat("--").concat(GatewayUtils.retrieveOAuthPasswordAlias(api.getId().getApiName(), api.getId().getVersion(), type)));
            } else {
                passwordDto.setAlias(GatewayUtils.retrieveOAuthPasswordAlias(api.getId().getApiName(), api.getId().getVersion(), type));
            }
            passwordDto.setPassword((String) endpointSecurity.get(APIConstants.ENDPOINT_SECURITY_PASSWORD));
            gatewayAPIDTO.setCredentialsToBeAdd(addCredentialsToList(passwordDto, gatewayAPIDTO.getCredentialsToBeAdd()));
        }
    } else if (APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.equalsIgnoreCase((String) endpointSecurity.get(APIConstants.ENDPOINT_SECURITY_TYPE))) {
        CredentialDto credentialDto = new CredentialDto();
        if (StringUtils.isNotEmpty(prefix)) {
            credentialDto.setAlias(prefix.concat("--").concat(GatewayUtils.retrieveBasicAuthAlias(api.getId().getApiName(), api.getId().getVersion(), type)));
        } else {
            credentialDto.setAlias(GatewayUtils.retrieveBasicAuthAlias(api.getId().getApiName(), api.getId().getVersion(), type));
        }
        credentialDto.setPassword((String) endpointSecurity.get(APIConstants.ENDPOINT_SECURITY_PASSWORD));
        gatewayAPIDTO.setCredentialsToBeAdd(addCredentialsToList(credentialDto, gatewayAPIDTO.getCredentialsToBeAdd()));
    }
}
Also used : CredentialDto(org.wso2.carbon.apimgt.api.gateway.CredentialDto)

Aggregations

GatewayContentDTO (org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)11 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)10 API (org.wso2.carbon.apimgt.api.model.API)5 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)4 APITemplateBuilder (org.wso2.carbon.apimgt.impl.template.APITemplateBuilder)4 Gson (com.google.gson.Gson)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 AxisFault (org.apache.axis2.AxisFault)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 ClientCertificateDTO (org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)3 CredentialDto (org.wso2.carbon.apimgt.api.gateway.CredentialDto)3 ArtifactSynchronizerException (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException)3 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO)3 HashSet (java.util.HashSet)2 JSONObject (org.json.simple.JSONObject)2 APIDefinition (org.wso2.carbon.apimgt.api.APIDefinition)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)2