Search in sources :

Example 21 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(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 22 with GatewayAPIDTO

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

the class SynapseArtifactGenerator method generateGatewayArtifact.

@Override
public RuntimeArtifactDto generateGatewayArtifact(List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList) throws APIManagementException {
    RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
    List<String> synapseArtifacts = new ArrayList<>();
    for (APIRuntimeArtifactDto runTimeArtifact : apiRuntimeArtifactDtoList) {
        if (runTimeArtifact.isFile()) {
            String tenantDomain = runTimeArtifact.getTenantDomain();
            String label = runTimeArtifact.getLabel();
            Environment environment = APIUtil.getEnvironments(tenantDomain).get(label);
            GatewayAPIDTO gatewayAPIDTO = null;
            if (environment != null) {
                try (InputStream artifact = (InputStream) runTimeArtifact.getArtifact()) {
                    File baseDirectory = CommonUtil.createTempDirectory(null);
                    try {
                        String extractedFolderPath = ImportUtils.getArchivePathOfExtractedDirectory(baseDirectory.getAbsolutePath(), artifact);
                        if (APIConstants.API_PRODUCT.equals(runTimeArtifact.getType())) {
                            APIProductDTO apiProductDTO = ImportUtils.retrieveAPIProductDto(extractedFolderPath);
                            apiProductDTO.setId(runTimeArtifact.getApiId());
                            APIProduct apiProduct = APIMappingUtil.fromDTOtoAPIProduct(apiProductDTO, apiProductDTO.getProvider());
                            APIDefinitionValidationResponse apiDefinitionValidationResponse = ImportUtils.retrieveValidatedSwaggerDefinitionFromArchive(extractedFolderPath);
                            apiProduct.setDefinition(apiDefinitionValidationResponse.getContent());
                            gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(apiProduct, environment, tenantDomain, extractedFolderPath);
                        } else {
                            APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath);
                            API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider());
                            api.setUUID(apidto.getId());
                            if (APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) {
                                APIDefinition parser = new OAS3Parser();
                                SwaggerData swaggerData = new SwaggerData(api);
                                String apiDefinition = parser.generateAPIDefinition(swaggerData);
                                api.setSwaggerDefinition(apiDefinition);
                                GraphqlComplexityInfo graphqlComplexityInfo = APIUtil.getComplexityDetails(api);
                                String graphqlSchema = ImportUtils.retrieveValidatedGraphqlSchemaFromArchive(extractedFolderPath);
                                api.setGraphQLSchema(graphqlSchema);
                                GraphQLSchemaDefinition graphQLSchemaDefinition = new GraphQLSchemaDefinition();
                                graphqlSchema = graphQLSchemaDefinition.buildSchemaWithAdditionalInfo(api, graphqlComplexityInfo);
                                api.setGraphQLSchema(graphqlSchema);
                                gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath);
                            } 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()) || APIConstants.APITransportType.WEBHOOK.toString().equals(api.getType()))) {
                                APIDefinitionValidationResponse apiDefinitionValidationResponse = ImportUtils.retrieveValidatedSwaggerDefinitionFromArchive(extractedFolderPath);
                                api.setSwaggerDefinition(apiDefinitionValidationResponse.getContent());
                                gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath, apiDefinitionValidationResponse);
                            } 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()))) {
                                APIDefinitionValidationResponse asyncApiDefinition = ImportUtils.retrieveValidatedAsyncApiDefinitionFromArchive(extractedFolderPath);
                                api.setAsyncApiDefinition(asyncApiDefinition.getContent());
                                gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDtoForStreamingAPI(api, environment, tenantDomain, apidto, extractedFolderPath);
                            }
                        }
                        if (gatewayAPIDTO != null) {
                            String content = new Gson().toJson(gatewayAPIDTO);
                            synapseArtifacts.add(content);
                        }
                    } finally {
                        FileUtils.deleteQuietly(baseDirectory);
                    }
                } catch (Exception e) {
                    // only do error since we need to continue for other apis
                    log.error("Error while creating Synapse configurations", e);
                }
            }
        }
    }
    runtimeArtifactDto.setFile(false);
    runtimeArtifactDto.setArtifact(synapseArtifacts);
    return runtimeArtifactDto;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) GraphqlComplexityInfo(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo) InputStream(java.io.InputStream) SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) APIProductDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO) ArrayList(java.util.ArrayList) OAS3Parser(org.wso2.carbon.apimgt.impl.definitions.OAS3Parser) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) Gson(com.google.gson.Gson) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) Environment(org.wso2.carbon.apimgt.api.model.Environment) API(org.wso2.carbon.apimgt.api.model.API) File(java.io.File)

Example 23 with GatewayAPIDTO

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

the class InMemoryAPIDeployer method unDeployAPI.

private void unDeployAPI(APIGatewayAdmin apiGatewayAdmin, DeployAPIInGatewayEvent gatewayEvent) throws AxisFault {
    if (gatewayArtifactSynchronizerProperties.isRetrieveFromStorageEnabled()) {
        GatewayAPIDTO gatewayAPIDTO = new GatewayAPIDTO();
        gatewayAPIDTO.setName(gatewayEvent.getName());
        gatewayAPIDTO.setVersion(gatewayEvent.getVersion());
        gatewayAPIDTO.setProvider(gatewayEvent.getProvider());
        gatewayAPIDTO.setTenantDomain(gatewayEvent.getTenantDomain());
        gatewayAPIDTO.setApiId(gatewayEvent.getUuid());
        setClientCertificatesToRemoveIntoGatewayDTO(gatewayAPIDTO);
        if (APIConstants.API_PRODUCT.equals(gatewayEvent.getApiType())) {
            APIProductIdentifier apiProductIdentifier = new APIProductIdentifier(gatewayEvent.getProvider(), gatewayEvent.getName(), gatewayEvent.getVersion());
            Set<APIEvent> associatedApis = gatewayEvent.getAssociatedApis();
            for (APIEvent associatedApi : associatedApis) {
                GatewayUtils.setCustomSequencesToBeRemoved(apiProductIdentifier, associatedApi.getUuid(), gatewayAPIDTO);
                GatewayUtils.setEndpointsToBeRemoved(apiProductIdentifier, associatedApi.getUuid(), gatewayAPIDTO);
            }
        } else {
            API api = new API(new APIIdentifier(gatewayEvent.getProvider(), gatewayEvent.getName(), gatewayEvent.getVersion()));
            if (APIConstants.APITransportType.GRAPHQL.toString().equalsIgnoreCase(gatewayEvent.getApiType())) {
                gatewayAPIDTO.setLocalEntriesToBeRemove(org.wso2.carbon.apimgt.impl.utils.GatewayUtils.addStringToList(gatewayEvent.getUuid().concat("_graphQL"), gatewayAPIDTO.getLocalEntriesToBeRemove()));
                DataHolder.getInstance().getApiToGraphQLSchemaDTOMap().remove(gatewayEvent.getUuid());
            }
            if (APIConstants.APITransportType.WS.toString().equalsIgnoreCase(gatewayEvent.getApiType())) {
                org.wso2.carbon.apimgt.gateway.utils.GatewayUtils.setWebsocketEndpointsToBeRemoved(gatewayAPIDTO, gatewayEvent.getTenantDomain());
            } else {
                GatewayUtils.setEndpointsToBeRemoved(gatewayAPIDTO.getName(), gatewayAPIDTO.getVersion(), gatewayAPIDTO);
            }
            GatewayUtils.setCustomSequencesToBeRemoved(api, gatewayAPIDTO);
        }
        gatewayAPIDTO.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(gatewayEvent.getUuid(), gatewayAPIDTO.getLocalEntriesToBeRemove()));
        apiGatewayAdmin.unDeployAPI(gatewayAPIDTO);
        DataHolder.getInstance().getApiToCertificatesMap().remove(gatewayEvent.getUuid());
        DataHolder.getInstance().removeKeyManagerToAPIMapping(gatewayAPIDTO.getApiId());
    }
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 24 with GatewayAPIDTO

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

the class InMemoryAPIDeployer method retrieveArtifact.

private GatewayAPIDTO retrieveArtifact(String apiId, Set<String> gatewayLabels) throws ArtifactSynchronizerException {
    GatewayAPIDTO result;
    String labelString = String.join("|", gatewayLabels);
    String encodedString = Base64.encodeBase64URLSafeString(labelString.getBytes());
    if (artifactRetriever != null) {
        try {
            String gatewayRuntimeArtifact = artifactRetriever.retrieveArtifact(apiId, encodedString);
            if (StringUtils.isNotEmpty(gatewayRuntimeArtifact)) {
                result = new Gson().fromJson(gatewayRuntimeArtifact, GatewayAPIDTO.class);
            } else {
                String msg = "Error retrieving artifacts for API " + apiId + ". Storage returned null";
                log.error(msg);
                throw new ArtifactSynchronizerException(msg);
            }
        } catch (ArtifactSynchronizerException e) {
            String msg = "Error deploying " + apiId + " in Gateway";
            log.error(msg, e);
            throw new ArtifactSynchronizerException(msg, e);
        }
    } else {
        String msg = "Artifact retriever not found";
        log.error(msg);
        throw new ArtifactSynchronizerException(msg);
    }
    return result;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) Gson(com.google.gson.Gson)

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