Search in sources :

Example 11 with APIOperationsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIOperationsDTO in project carbon-apimgt by wso2.

the class PublisherCommonUtils method validateGraphQLSchema.

/**
 * Validate GraphQL Schema.
 *
 * @param filename file name of the schema
 * @param schema   GraphQL schema
 */
public static GraphQLValidationResponseDTO validateGraphQLSchema(String filename, String schema) throws APIManagementException {
    String errorMessage;
    GraphQLValidationResponseDTO validationResponse = new GraphQLValidationResponseDTO();
    boolean isValid = false;
    try {
        if (filename.endsWith(".graphql") || filename.endsWith(".txt") || filename.endsWith(".sdl")) {
            if (schema.isEmpty()) {
                throw new APIManagementException("GraphQL Schema cannot be empty or null to validate it", ExceptionCodes.GRAPHQL_SCHEMA_CANNOT_BE_NULL);
            }
            SchemaParser schemaParser = new SchemaParser();
            TypeDefinitionRegistry typeRegistry = schemaParser.parse(schema);
            GraphQLSchema graphQLSchema = UnExecutableSchemaGenerator.makeUnExecutableSchema(typeRegistry);
            SchemaValidator schemaValidation = new SchemaValidator();
            Set<SchemaValidationError> validationErrors = schemaValidation.validateSchema(graphQLSchema);
            if (validationErrors.toArray().length > 0) {
                errorMessage = "InValid Schema";
                validationResponse.isValid(Boolean.FALSE);
                validationResponse.errorMessage(errorMessage);
            } else {
                validationResponse.setIsValid(Boolean.TRUE);
                GraphQLValidationResponseGraphQLInfoDTO graphQLInfo = new GraphQLValidationResponseGraphQLInfoDTO();
                GraphQLSchemaDefinition graphql = new GraphQLSchemaDefinition();
                List<URITemplate> operationList = graphql.extractGraphQLOperationList(typeRegistry, null);
                List<APIOperationsDTO> operationArray = APIMappingUtil.fromURITemplateListToOprationList(operationList);
                graphQLInfo.setOperations(operationArray);
                GraphQLSchemaDTO schemaObj = new GraphQLSchemaDTO();
                schemaObj.setSchemaDefinition(schema);
                graphQLInfo.setGraphQLSchema(schemaObj);
                validationResponse.setGraphQLInfo(graphQLInfo);
            }
        } else {
            throw new APIManagementException("Unsupported extension type of file: " + filename, ExceptionCodes.UNSUPPORTED_GRAPHQL_FILE_EXTENSION);
        }
        isValid = validationResponse.isIsValid();
        errorMessage = validationResponse.getErrorMessage();
    } catch (SchemaProblem e) {
        errorMessage = e.getMessage();
    }
    if (!isValid) {
        validationResponse.setIsValid(isValid);
        validationResponse.setErrorMessage(errorMessage);
    }
    return validationResponse;
}
Also used : TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaValidator(graphql.schema.validation.SchemaValidator) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) GraphQLValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLValidationResponseDTO) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) SchemaValidationError(graphql.schema.validation.SchemaValidationError) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GraphQLValidationResponseGraphQLInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLValidationResponseGraphQLInfoDTO) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) GraphQLSchemaDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLSchemaDTO)

Example 12 with APIOperationsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIOperationsDTO in project carbon-apimgt by wso2.

the class ImportUtils method extractAndDropOperationPoliciesFromURITemplate.

public static Map<String, List<OperationPolicy>> extractAndDropOperationPoliciesFromURITemplate(List<APIOperationsDTO> operationsDTO) {
    Map<String, List<OperationPolicy>> operationPoliciesMap = new HashMap<>();
    for (APIOperationsDTO dto : operationsDTO) {
        String key = dto.getVerb() + ":" + dto.getTarget();
        List<OperationPolicy> operationPolicies = OperationPolicyMappingUtil.fromDTOToAPIOperationPoliciesList(dto.getOperationPolicies());
        if (!operationPolicies.isEmpty()) {
            operationPoliciesMap.put(key, operationPolicies);
        }
        dto.setOperationPolicies(null);
    }
    return operationPoliciesMap;
}
Also used : HashMap(java.util.HashMap) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) ArrayList(java.util.ArrayList) List(java.util.List) NodeList(org.w3c.dom.NodeList)

Example 13 with APIOperationsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIOperationsDTO in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method addWebsocketTopicMappings.

private static void addWebsocketTopicMappings(API api, APIDTO apidto) {
    org.json.JSONObject endpointConfiguration = new org.json.JSONObject(api.getEndpointConfig());
    String sandboxEndpointUrl = !endpointConfiguration.isNull(APIConstants.API_DATA_SANDBOX_ENDPOINTS) ? endpointConfiguration.getJSONObject(APIConstants.API_DATA_SANDBOX_ENDPOINTS).getString("url") : null;
    String productionEndpointUrl = !endpointConfiguration.isNull(APIConstants.API_DATA_PRODUCTION_ENDPOINTS) ? endpointConfiguration.getJSONObject(APIConstants.API_DATA_PRODUCTION_ENDPOINTS).getString("url") : null;
    Map<String, Map<String, String>> perTopicMappings = new HashMap<>();
    for (APIOperationsDTO operation : apidto.getOperations()) {
        String key = operation.getTarget();
        String mapping = operation.getUriMapping() == null ? "" : Paths.get("/", operation.getUriMapping()).toString();
        Map<String, String> endpoints = new HashMap<>();
        if (sandboxEndpointUrl != null) {
            endpoints.put(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, sandboxEndpointUrl + mapping);
        }
        if (productionEndpointUrl != null) {
            endpoints.put(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, productionEndpointUrl + mapping);
        }
        perTopicMappings.put(key, endpoints);
    }
    api.setWebSocketTopicMappingConfiguration(new WebSocketTopicMappingConfiguration(perTopicMappings));
    addWebsocketTopicResourceKeys(api);
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) WebSocketTopicMappingConfiguration(org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with APIOperationsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIOperationsDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method getOperationsFromAPI.

/**
 * Returns a set of operations from a API.
 *
 * @param api API object
 * @return a set of operations from a given swagger definition
 */
private static List<APIOperationsDTO> getOperationsFromAPI(API api) {
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    List<APIOperationsDTO> operationsDTOList = new ArrayList<>();
    for (URITemplate uriTemplate : uriTemplates) {
        APIOperationsDTO operationsDTO = getOperationFromURITemplate(uriTemplate);
        if (api.getType().equals(APIConstants.API_TYPE_WS)) {
            Map<String, String> wsUriMappings = api.getWsUriMapping();
            if (wsUriMappings != null) {
                String wsUriMapping = wsUriMappings.get(operationsDTO.getVerb() + "_" + operationsDTO.getTarget());
                if (wsUriMapping != null) {
                    operationsDTO.setUriMapping(wsUriMapping);
                }
            }
        }
        operationsDTOList.add(operationsDTO);
    }
    return operationsDTOList;
}
Also used : URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) ArrayList(java.util.ArrayList)

Example 15 with APIOperationsDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIOperationsDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method getOperationListWithOldData.

/**
 * This method returns graphQL operations with the old data.
 *
 * @param uriTemplates uriTemplates
 * @param operations   operations
 * @return operations
 */
public static List<APIOperationsDTO> getOperationListWithOldData(Set<URITemplate> uriTemplates, List<APIOperationsDTO> operations) {
    for (APIOperationsDTO operation : operations) {
        for (URITemplate uriTemplate : uriTemplates) {
            if (operation.getVerb().equalsIgnoreCase(uriTemplate.getHTTPVerb()) && operation.getTarget().equalsIgnoreCase(uriTemplate.getUriTemplate())) {
                operation.setThrottlingPolicy(uriTemplate.getThrottlingTier());
                operation.setAuthType(uriTemplate.getAuthType());
                operation.setScopes(uriTemplate.retrieveAllScopes().stream().map(Scope::getKey).collect(Collectors.toList()));
            }
            if (operation.getThrottlingPolicy() == null) {
                operation.setThrottlingPolicy(APIConstants.UNLIMITED_TIER);
            }
        }
    }
    return operations;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Aggregations

APIOperationsDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO)19 ArrayList (java.util.ArrayList)15 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)15 HashMap (java.util.HashMap)9 Scope (org.wso2.carbon.apimgt.api.model.Scope)7 JSONObject (org.json.simple.JSONObject)6 Tier (org.wso2.carbon.apimgt.api.model.Tier)6 CORSConfiguration (org.wso2.carbon.apimgt.api.model.CORSConfiguration)4 APICorsConfigurationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APICorsConfigurationDTO)4 APIInfoAdditionalPropertiesDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesDTO)4 Timestamp (java.sql.Timestamp)3 Date (java.util.Date)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 APICategory (org.wso2.carbon.apimgt.api.model.APICategory)3 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)3 ProductAPIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO)3 JsonObject (com.google.gson.JsonObject)2 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)2