Search in sources :

Example 56 with Operation

use of io.swagger.models.Operation in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method removeScopeFromSwaggerDefinition.

@Override
public String removeScopeFromSwaggerDefinition(String resourceConfigJSON, String name) {
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(resourceConfigJSON);
    Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = swagger.getSecurityDefinitions();
    if (securitySchemeDefinitionMap != null && !securitySchemeDefinitionMap.isEmpty()) {
        OAuth2Definition oAuth2Definition = (OAuth2Definition) securitySchemeDefinitionMap.get(APIMgtConstants.OAUTH2SECURITY);
        if (oAuth2Definition != null) {
            // Removing Scope from Swagger SecurityDefinition
            oAuth2Definition.getScopes().remove(name);
            // Finding Security requirements at root level
            List<SecurityRequirement> securityRequirements = swagger.getSecurity();
            if (securityRequirements != null && !securityRequirements.isEmpty()) {
                // Get List of Security Requirements
                Iterator<SecurityRequirement> securityRequirementIterator = securityRequirements.iterator();
                while (securityRequirementIterator.hasNext()) {
                    SecurityRequirement securityRequirement = securityRequirementIterator.next();
                    Map<String, List<String>> secListMap = securityRequirement.getRequirements();
                    // get Oauth2Security scopes
                    List<String> scopesList = secListMap.get(APIMgtConstants.OAUTH2SECURITY);
                    if (scopesList != null) {
                        // Remove Scope from root level
                        scopesList.remove(name);
                    }
                    // Check root level security Requirements is empty
                    if (securityRequirement.getRequirements().isEmpty()) {
                        // Check root level security Requirements
                        securityRequirementIterator.remove();
                    }
                }
                if (securityRequirements.isEmpty()) {
                    // Remove root level security
                    swagger.setSecurity(null);
                }
            }
            Map<String, Path> pathMap = swagger.getPaths();
            if (pathMap != null && !pathMap.isEmpty()) {
                for (Map.Entry<String, Path> pathEntry : pathMap.entrySet()) {
                    Path path = pathEntry.getValue();
                    List<Operation> operationList = path.getOperations();
                    for (Operation operation : operationList) {
                        List<Map<String, List<String>>> operationSecurityList = operation.getSecurity();
                        if (operationSecurityList != null && !operationSecurityList.isEmpty()) {
                            Iterator<Map<String, List<String>>> securityMapIterator = operationSecurityList.iterator();
                            while (securityMapIterator.hasNext()) {
                                Map<String, List<String>> securityMap = securityMapIterator.next();
                                List<String> scopesList = securityMap.get(APIMgtConstants.OAUTH2SECURITY);
                                scopesList.remove(name);
                                if (scopesList.isEmpty()) {
                                    securityMapIterator.remove();
                                }
                            }
                            if (operationSecurityList.isEmpty()) {
                                operation.setSecurity(null);
                            }
                        }
                    }
                }
            }
        }
    }
    return Json.pretty(swagger);
}
Also used : Path(io.swagger.models.Path) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Operation(io.swagger.models.Operation) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SecurityRequirement(io.swagger.models.SecurityRequirement)

Example 57 with Operation

use of io.swagger.models.Operation in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method populateConfigMapForScopes.

/*
    * This method populates resource to scope mappings into localConfigMap
    *
    * @param swagger swagger oc of the apis
    * @param String namespacee unigue identifier of the api
    *
    * */
private void populateConfigMapForScopes(Swagger swagger, String namespace) {
    Map<String, String> configMap = ServiceReferenceHolder.getInstance().getRestAPIConfigurationMap(namespace);
    // update local cache with configs defined in configuration file(dep.yaml)
    if (!localConfigMap.containsKey(namespace)) {
        localConfigMap.put(namespace, new ConcurrentHashMap<>());
    }
    if (configMap != null) {
        localConfigMap.get(namespace).putAll(configMap);
    }
    // update local cache with the resource to scope mapping read from swagger
    if (swagger != null) {
        for (Map.Entry<String, Path> entry : swagger.getPaths().entrySet()) {
            Path resource = entry.getValue();
            Map<HttpMethod, Operation> operationsMap = resource.getOperationMap();
            for (Map.Entry<HttpMethod, Operation> httpverbEntry : operationsMap.entrySet()) {
                if (httpverbEntry.getValue().getVendorExtensions().size() > 0 && httpverbEntry.getValue().getVendorExtensions().get(APIMgtConstants.SWAGGER_X_SCOPE) != null) {
                    String path = httpverbEntry.getKey() + "_" + entry.getKey();
                    if (!localConfigMap.get(namespace).containsKey(path)) {
                        localConfigMap.get(namespace).put(path, httpverbEntry.getValue().getVendorExtensions().get(APIMgtConstants.SWAGGER_X_SCOPE).toString());
                    }
                }
            }
        }
    }
}
Also used : Path(io.swagger.models.Path) Operation(io.swagger.models.Operation) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HttpMethod(io.swagger.models.HttpMethod)

Example 58 with Operation

use of io.swagger.models.Operation in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method setApiResourceBuilderProperties.

/**
 * Extract properties in Operation entry and assign them to api resource builder properties.
 *
 * @param operationEntry     Map entry to be extracted properties
 * @param uriTemplateBuilder Uri template builder to assign related properties
 * @param resourcePath       resource path
 * @return APIResource.Builder object
 */
private APIResource.Builder setApiResourceBuilderProperties(Map.Entry<HttpMethod, Operation> operationEntry, UriTemplate.UriTemplateBuilder uriTemplateBuilder, String resourcePath) {
    Operation operation = operationEntry.getValue();
    APIResource.Builder apiResourceBuilder = new APIResource.Builder();
    List<String> producesList = operation.getProduces();
    if (producesList != null) {
        String produceSeparatedString = "\"";
        produceSeparatedString += String.join("\",\"", producesList) + "\"";
        apiResourceBuilder.produces(produceSeparatedString);
    }
    List<String> consumesList = operation.getConsumes();
    if (consumesList != null) {
        String consumesSeparatedString = "\"";
        consumesSeparatedString += String.join("\",\"", consumesList) + "\"";
        apiResourceBuilder.consumes(consumesSeparatedString);
    }
    if (operation.getOperationId() != null) {
        uriTemplateBuilder.templateId(operation.getOperationId());
    } else {
        uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(resourcePath, operationEntry.getKey().name()));
    }
    uriTemplateBuilder.httpVerb(operationEntry.getKey().name());
    apiResourceBuilder.uriTemplate(uriTemplateBuilder.build());
    return apiResourceBuilder;
}
Also used : APIResource(org.wso2.carbon.apimgt.core.models.APIResource) Operation(io.swagger.models.Operation)

Example 59 with Operation

use of io.swagger.models.Operation in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method generateSwaggerFromResources.

@Override
public String generateSwaggerFromResources(CompositeAPI.Builder api) {
    Swagger swagger = new Swagger();
    Info info = new Info();
    info.setTitle(api.getName());
    info.setDescription(api.getDescription());
    info.setVersion(api.getVersion());
    swagger.setInfo(info);
    Map<String, Path> stringPathMap = new HashMap();
    for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
        String uriTemplateString = uriTemplate.getUriTemplate();
        List<Parameter> parameterList = getParameters(uriTemplateString);
        if (!HttpMethod.GET.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(uriTemplate.getHttpVerb())) {
            parameterList.add(getDefaultBodyParameter());
        }
        Operation operation = new Operation();
        operation.setParameters(parameterList);
        operation.setOperationId(uriTemplate.getTemplateId());
        operation.addResponse("200", getDefaultResponse());
        if (stringPathMap.containsKey(uriTemplateString)) {
            Path path = stringPathMap.get(uriTemplateString);
            path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
        } else {
            Path path = new Path();
            path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
            stringPathMap.put(uriTemplateString, path);
        }
    }
    swagger.setPaths(stringPathMap);
    swagger.setPaths(stringPathMap);
    return Json.pretty(swagger);
}
Also used : Path(io.swagger.models.Path) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Swagger(io.swagger.models.Swagger) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 60 with Operation

use of io.swagger.models.Operation in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method isOperationNotExist.

private boolean isOperationNotExist(Map.Entry<HttpMethod, Operation> entry, String uri, Map<String, UriTemplate> uriTemplateMap, String securityName) {
    HttpMethod httpMethod = entry.getKey();
    Operation operation = entry.getValue();
    String operationId = APIUtils.generateOperationIdFromPath(uri, httpMethod.name());
    if (uriTemplateMap.containsKey(operationId)) {
        assignScopesToOperation(operation, securityName, uriTemplateMap.get(operationId).getScopes());
        uriTemplateMap.remove(operationId);
        return false;
    } else {
        return true;
    }
}
Also used : Operation(io.swagger.models.Operation) HttpMethod(io.swagger.models.HttpMethod)

Aggregations

Operation (io.swagger.models.Operation)118 Test (org.testng.annotations.Test)80 Swagger (io.swagger.models.Swagger)45 Path (io.swagger.models.Path)39 BodyParameter (io.swagger.models.parameters.BodyParameter)27 Property (io.swagger.models.properties.Property)24 Response (io.swagger.models.Response)23 Parameter (io.swagger.models.parameters.Parameter)20 RefProperty (io.swagger.models.properties.RefProperty)16 ArrayModel (io.swagger.models.ArrayModel)15 Model (io.swagger.models.Model)14 QueryParameter (io.swagger.models.parameters.QueryParameter)13 StringProperty (io.swagger.models.properties.StringProperty)13 HashMap (java.util.HashMap)13 ArrayProperty (io.swagger.models.properties.ArrayProperty)12 ReaderContext (io.swagger.servlet.ReaderContext)12 PathParameter (io.swagger.models.parameters.PathParameter)11 ArrayList (java.util.ArrayList)11 MapProperty (io.swagger.models.properties.MapProperty)10 NicknamedOperation (io.swagger.resources.NicknamedOperation)10