Search in sources :

Example 86 with APIDefinition

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

the class SettingsMappingUtil method getScopeList.

private List<String> getScopeList() throws APIManagementException {
    String definition = null;
    try {
        definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream("/service-catalog-api.yaml"), "UTF-8");
    } catch (IOException e) {
        log.error("Error while reading the swagger definition", e);
    }
    APIDefinition oasParser = OASParserUtil.getOASParser(definition);
    Set<Scope> scopeSet = oasParser.getScopes(definition);
    List<String> scopeList = new ArrayList<>();
    for (Scope entry : scopeSet) {
        scopeList.add(entry.getKey());
    }
    return scopeList;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 87 with APIDefinition

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

the class ImportUtils method updateApiProductSwagger.

/**
 * This method updates the API Product and the swagger with the correct scopes.
 *
 * @param pathToArchive      Path to the extracted folder
 * @param importedApiProduct Imported API Product
 * @param apiProvider        API Provider
 * @throws APIManagementException If an error occurs when retrieving the parser and updating the API Product
 * @throws FaultGatewaysException If an error occurs when updating the API to overwrite
 * @throws IOException            If an error occurs when loading the swagger file
 */
private static APIProduct updateApiProductSwagger(String pathToArchive, String apiProductId, APIProduct importedApiProduct, APIProvider apiProvider, String orgId) throws APIManagementException, FaultGatewaysException, IOException {
    String swaggerContent = loadSwaggerFile(pathToArchive);
    // Load required properties from swagger to the API Product
    APIDefinition apiDefinition = OASParserUtil.getOASParser(swaggerContent);
    Set<Scope> scopes = apiDefinition.getScopes(swaggerContent);
    importedApiProduct.setScopes(scopes);
    importedApiProduct.setOrganization(orgId);
    // This is required to make scopes get effected
    Map<API, List<APIProductResource>> apiToProductResourceMapping = apiProvider.updateAPIProduct(importedApiProduct);
    apiProvider.updateAPIProductSwagger(apiProductId, apiToProductResourceMapping, importedApiProduct, orgId);
    return importedApiProduct;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) API(org.wso2.carbon.apimgt.api.model.API) ArrayList(java.util.ArrayList) List(java.util.List) NodeList(org.w3c.dom.NodeList)

Example 88 with APIDefinition

use of org.wso2.carbon.apimgt.api.APIDefinition 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 89 with APIDefinition

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

the class APIKeyValidator method getAPIProductURITemplates.

@MethodStats
protected ArrayList<URITemplate> getAPIProductURITemplates(MessageContext messageContext, String context, String apiVersion) throws APISecurityException {
    if (uriTemplates == null) {
        synchronized (this) {
            if (uriTemplates == null) {
                String swagger = (String) messageContext.getProperty(APIMgtGatewayConstants.OPEN_API_STRING);
                if (swagger != null) {
                    APIDefinition oasParser;
                    try {
                        oasParser = OASParserUtil.getOASParser(swagger);
                        uriTemplates = new ArrayList<>();
                        uriTemplates.addAll(oasParser.getURITemplates(swagger));
                        return uriTemplates;
                    } catch (APIManagementException e) {
                        log.error("Error while parsing swagger content to get URI Templates", e);
                    }
                }
                uriTemplates = dataStore.getAPIProductURITemplates(context, apiVersion);
            }
        }
    }
    return uriTemplates;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) MethodStats(org.wso2.carbon.apimgt.gateway.MethodStats)

Example 90 with APIDefinition

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

the class APIMappingUtil method getOperationsFromSwaggerDef.

/**
 * Returns a set of operations from a API
 * Returns a set of operations from a given swagger definition
 *
 * @param api               API object
 * @param swaggerDefinition Swagger definition
 * @return a set of operations from a given swagger definition
 * @throws APIManagementException error while trying to retrieve URI templates of the given API
 */
private static List<APIOperationsDTO> getOperationsFromSwaggerDef(API api, String swaggerDefinition) throws APIManagementException {
    APIDefinition apiDefinition = OASParserUtil.getOASParser(swaggerDefinition);
    Set<URITemplate> uriTemplates;
    if (APIConstants.GRAPHQL_API.equals(api.getType())) {
        uriTemplates = api.getUriTemplates();
    } else {
        uriTemplates = apiDefinition.getURITemplates(swaggerDefinition);
    }
    List<APIOperationsDTO> operationsDTOList = new ArrayList<>();
    if (!StringUtils.isEmpty(swaggerDefinition)) {
        for (URITemplate uriTemplate : uriTemplates) {
            APIOperationsDTO operationsDTO = getOperationFromURITemplate(uriTemplate);
            operationsDTOList.add(operationsDTO);
        }
    }
    return operationsDTOList;
}
Also used : APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) ArrayList(java.util.ArrayList)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)32 HashMap (java.util.HashMap)30 APIDefinition (org.wso2.carbon.apimgt.api.APIDefinition)30 API (org.wso2.carbon.apimgt.core.models.API)30 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)25 ArrayList (java.util.ArrayList)23 API (org.wso2.carbon.apimgt.api.model.API)20 Map (java.util.Map)19 IOException (java.io.IOException)18 Test (org.testng.annotations.Test)18 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)18 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)16 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)15 Scope (org.wso2.carbon.apimgt.api.model.Scope)15 HashSet (java.util.HashSet)13 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)13 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)12 SwaggerData (org.wso2.carbon.apimgt.api.model.SwaggerData)12 BusinessInformation (org.wso2.carbon.apimgt.core.models.BusinessInformation)12 CorsConfiguration (org.wso2.carbon.apimgt.core.models.CorsConfiguration)12