Java Code Examples for org.wso2.carbon.apimgt.impl.utils.APIUtil#findScopeByKey()

The following examples show how to use org.wso2.carbon.apimgt.impl.utils.APIUtil#findScopeByKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OASParserUtil.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the scopes to the URL template object using the given list of scopes
 *
 * @param template URL template
 * @param resourceScopes   list of scopes of the resource
 * @param apiScopes set of scopes defined for the API
 * @return URL template after setting the scopes
 */
public static URITemplate setScopesToTemplate(URITemplate template, List<String> resourceScopes,
                                              Set<Scope> apiScopes) throws APIManagementException {

    for (String scopeName : resourceScopes) {
        Scope scope = APIUtil.findScopeByKey(apiScopes, scopeName);
        if (scope == null) {
            throw new APIManagementException("Resource Scope '" + scopeName + "' not found.");
        }
        template.setScopes(scope);
    }
    return template;
}
 
Example 2
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * This method returns URI templates according to the given swagger file
 *
 * @param resourceConfigsJSON swaggerJSON
 * @return URI Templates
 * @throws APIManagementException
 */
@Override
public Set<URITemplate> getURITemplates(String resourceConfigsJSON) throws APIManagementException {
    Swagger swagger = getSwagger(resourceConfigsJSON);
    Set<URITemplate> urlTemplates = new LinkedHashSet<>();
    Set<Scope> scopes = getScopes(resourceConfigsJSON);
    String oauth2SchemeKey = getOAuth2SecuritySchemeKey(swagger);

    for (String pathString : swagger.getPaths().keySet()) {
        Path path = swagger.getPath(pathString);
        Map<HttpMethod, Operation> operationMap = path.getOperationMap();
        for (Map.Entry<HttpMethod, Operation> entry : operationMap.entrySet()) {
            Operation operation = entry.getValue();
            URITemplate template = new URITemplate();
            template.setHTTPVerb(entry.getKey().name().toUpperCase());
            template.setHttpVerbs(entry.getKey().name().toUpperCase());
            template.setUriTemplate(pathString);
            List<String> opScopes = getScopeOfOperations(oauth2SchemeKey, operation);
            if (!opScopes.isEmpty()) {
                if (opScopes.size() == 1) {
                    String firstScope = opScopes.get(0);
                    Scope scope = APIUtil.findScopeByKey(scopes, firstScope);
                    if (scope == null) {
                        throw new APIManagementException("Scope '" + firstScope + "' not found.");
                    }
                    template.setScope(scope);
                    template.setScopes(scope);
                } else {
                    template = OASParserUtil.setScopesToTemplate(template, opScopes, scopes);
                }
            }
            Map<String, Object> extensions = operation.getVendorExtensions();
            if (extensions != null) {
                if (extensions.containsKey(APIConstants.SWAGGER_X_AUTH_TYPE)) {
                    String authType = (String) extensions.get(APIConstants.SWAGGER_X_AUTH_TYPE);
                    template.setAuthType(authType);
                    template.setAuthTypes(authType);
                } else {
                    template.setAuthType("Any");
                    template.setAuthTypes("Any");
                }
                if (extensions.containsKey(APIConstants.SWAGGER_X_THROTTLING_TIER)) {
                    String throttlingTier = (String) extensions.get(APIConstants.SWAGGER_X_THROTTLING_TIER);
                    template.setThrottlingTier(throttlingTier);
                    template.setThrottlingTiers(throttlingTier);
                }
                if (extensions.containsKey(APIConstants.SWAGGER_X_MEDIATION_SCRIPT)) {
                    String mediationScript = (String) extensions.get(APIConstants.SWAGGER_X_MEDIATION_SCRIPT);
                    template.setMediationScript(mediationScript);
                    template.setMediationScripts(template.getHTTPVerb(), mediationScript);
                }
            }
            urlTemplates.add(template);
        }
    }
    return urlTemplates;
}
 
Example 3
Source File: OAS3Parser.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * This method returns URI templates according to the given swagger file
 *
 * @param resourceConfigsJSON swaggerJSON
 * @return URI Templates
 * @throws APIManagementException
 */
@Override
public Set<URITemplate> getURITemplates(String resourceConfigsJSON) throws APIManagementException {
    OpenAPI openAPI = getOpenAPI(resourceConfigsJSON);
    Set<URITemplate> urlTemplates = new LinkedHashSet<>();
    Set<Scope> scopes = getScopes(resourceConfigsJSON);

    for (String pathKey : openAPI.getPaths().keySet()) {
        PathItem pathItem = openAPI.getPaths().get(pathKey);
        for (Map.Entry<PathItem.HttpMethod, Operation> entry : pathItem.readOperationsMap().entrySet()) {
            Operation operation = entry.getValue();
            URITemplate template = new URITemplate();
            if (APIConstants.SUPPORTED_METHODS.contains(entry.getKey().name().toLowerCase())) {
                template.setHTTPVerb(entry.getKey().name().toUpperCase());
                template.setHttpVerbs(entry.getKey().name().toUpperCase());
                template.setUriTemplate(pathKey);
                List<String> opScopes = getScopeOfOperations(OPENAPI_SECURITY_SCHEMA_KEY, operation);
                if (!opScopes.isEmpty()) {
                    if (opScopes.size() == 1) {
                        String firstScope = opScopes.get(0);
                        Scope scope = APIUtil.findScopeByKey(scopes, firstScope);
                        if (scope == null) {
                            throw new APIManagementException("Scope '" + firstScope + "' not found.");
                        }
                        template.setScope(scope);
                        template.setScopes(scope);
                    } else {
                        template = OASParserUtil.setScopesToTemplate(template, opScopes, scopes);
                    }
                }
                Map<String, Object> extensios = operation.getExtensions();
                if (extensios != null) {
                    if (extensios.containsKey(APIConstants.SWAGGER_X_AUTH_TYPE)) {
                        String scopeKey = (String) extensios.get(APIConstants.SWAGGER_X_AUTH_TYPE);
                        template.setAuthType(scopeKey);
                        template.setAuthTypes(scopeKey);
                    } else {
                        template.setAuthType("Any");
                        template.setAuthTypes("Any");
                    }
                    if (extensios.containsKey(APIConstants.SWAGGER_X_THROTTLING_TIER)) {
                        String throttlingTier = (String) extensios.get(APIConstants.SWAGGER_X_THROTTLING_TIER);
                        template.setThrottlingTier(throttlingTier);
                        template.setThrottlingTiers(throttlingTier);
                    }
                    if (extensios.containsKey(APIConstants.SWAGGER_X_MEDIATION_SCRIPT)) {
                        String mediationScript = (String) extensios.get(APIConstants.SWAGGER_X_MEDIATION_SCRIPT);
                        template.setMediationScript(mediationScript);
                        template.setMediationScripts(template.getHTTPVerb(), mediationScript);
                    }
                }
                urlTemplates.add(template);
            }
        }
    }
    return urlTemplates;
}