Java Code Examples for org.wso2.carbon.apimgt.api.model.URITemplate#setScopes()

The following examples show how to use org.wso2.carbon.apimgt.api.model.URITemplate#setScopes() . 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: APIMgtDAOTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
private URITemplate getUriTemplate(String resourceString,String httpVerb,String authType,String scope, String throtlingTier){
    URITemplate uriTemplate = new URITemplate();
    uriTemplate.setUriTemplate(resourceString);
    uriTemplate.setHTTPVerb(httpVerb);
    uriTemplate.setThrottlingTier(throtlingTier);
    uriTemplate.setAuthType(authType);
    uriTemplate.setMediationScript("abcd defgh fff");
    if (scope!= null){
        Scope scope1 = new Scope();
        scope1.setId("0");
        scope1.setDescription("");
        scope1.setKey(scope);
        scope1.setName(scope);
        scope1.setRoles("admin");
        uriTemplate.setScope(scope1);
        uriTemplate.setScopes(scope1);
    }
    return uriTemplate;
}
 
Example 2
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 3
Source File: OASTestBase.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public void testGetURITemplates(APIDefinition parser, String content) throws Exception {
    JSONObject jsonObject = new JSONObject(content);

    URITemplate exUriTemplate = new URITemplate();
    exUriTemplate.setUriTemplate("/pets");
    exUriTemplate.setAuthType("Application & Application User");
    exUriTemplate.setAuthTypes("Application & Application User");
    exUriTemplate.setHTTPVerb("GET");
    exUriTemplate.setHttpVerbs("GET");
    exUriTemplate.setThrottlingTier("Unlimited");
    exUriTemplate.setThrottlingTiers("Unlimited");
    exUriTemplate.setScope(extensionScope);
    exUriTemplate.setScopes(extensionScope);

    String scopesOnlyInSecurity = jsonObject.getJSONObject("scopesOnlyInSecurity").toString();
    Set<URITemplate> uriTemplates = parser.getURITemplates(scopesOnlyInSecurity);
    Assert.assertEquals(1, uriTemplates.size());
    Assert.assertTrue(uriTemplates.contains(petGet));

    String scopesOnlyInExtension = jsonObject.getJSONObject("scopesOnlyInExtension").toString();
    uriTemplates = parser.getURITemplates(scopesOnlyInExtension);
    Assert.assertEquals(1, uriTemplates.size());
    Assert.assertTrue(uriTemplates.contains(exUriTemplate));

    String scopesInExtensionAndSec = jsonObject.getJSONObject("scopesInExtensionAndSec").toString();
    uriTemplates = parser.getURITemplates(scopesInExtensionAndSec);
    Assert.assertEquals(1, uriTemplates.size());
    Assert.assertTrue(uriTemplates.contains(petGet));
}
 
Example 4
Source File: OASTestBase.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public String testGenerateAPIDefinitionWithExtension(APIDefinition parser, String content) throws Exception {
    JSONObject jsonObject = new JSONObject(content);
    String equalNoOfResourcesWithExtension = jsonObject.getJSONObject("equalNoOfResourcesWithExtension").toString();

    Scope newScope = new Scope();
    newScope.setName("newScope");
    newScope.setKey("newScope");
    newScope.setRoles("admin");
    newScope.setDescription("newScopeDescription");

    URITemplate updatedItemGet = new URITemplate();
    updatedItemGet.setUriTemplate("/items");
    updatedItemGet.setAuthType("Application & Application User");
    updatedItemGet.setAuthTypes("Application & Application User");
    updatedItemGet.setHTTPVerb("GET");
    updatedItemGet.setHttpVerbs("GET");
    updatedItemGet.setThrottlingTier("Unlimited");
    updatedItemGet.setThrottlingTiers("Unlimited");
    updatedItemGet.setScope(newScope);
    updatedItemGet.setScopes(newScope);

    APIIdentifier identifier = new APIIdentifier("admin", "simple", "1.0.0");
    API api = new API(identifier);
    api.setScopes(new HashSet<>(Arrays.asList(sampleScope, extensionScope, newScope)));
    api.setUriTemplates(new HashSet<>(Arrays.asList(petPost, updatedItemGet)));

    String definition = parser.generateAPIDefinition(new SwaggerData(api), equalNoOfResourcesWithExtension);
    APIDefinitionValidationResponse response = parser.validateAPIDefinition(definition, false);
    Assert.assertTrue(response.isValid());
    return definition;
}
 
Example 5
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 6
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;
}
 
Example 7
Source File: OASTestBase.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public OASTestBase() {
    sampleScope = new Scope();
    sampleScope.setName("sample");
    sampleScope.setKey("sample");
    sampleScope.setRoles("admin");
    sampleScope.setDescription("sample description");

    extensionScope = new Scope();
    extensionScope.setName("extensionScope");
    extensionScope.setKey("extensionScope");
    extensionScope.setRoles("admin");
    extensionScope.setDescription("extensionScope description");

    petGet = new URITemplate();
    petGet.setUriTemplate("/pets");
    petGet.setAuthType("Application & Application User");
    petGet.setAuthTypes("Application & Application User");
    petGet.setHTTPVerb("GET");
    petGet.setHttpVerbs("GET");
    petGet.setThrottlingTier("Unlimited");
    petGet.setThrottlingTiers("Unlimited");
    petGet.setScope(sampleScope);
    petGet.setScopes(sampleScope);

    petPost = new URITemplate();
    petPost.setUriTemplate("/pets");
    petPost.setAuthType("Application & Application User");
    petPost.setAuthTypes("Application & Application User");
    petPost.setHTTPVerb("POST");
    petPost.setHttpVerbs("POST");
    petPost.setThrottlingTier("Unlimited");
    petPost.setThrottlingTiers("Unlimited");
    petPost.setScope(sampleScope);
    petPost.setScopes(sampleScope);

    itemPost = new URITemplate();
    itemPost.setUriTemplate("/items");
    itemPost.setAuthType("Application & Application User");
    itemPost.setAuthTypes("Application & Application User");
    itemPost.setHTTPVerb("POST");
    itemPost.setHttpVerbs("POST");
    itemPost.setThrottlingTier("Unlimited");
    itemPost.setThrottlingTiers("Unlimited");
    itemPost.setScope(sampleScope);
    itemPost.setScopes(sampleScope);

    itemGet = new URITemplate();
    itemGet.setUriTemplate("/items");
    itemGet.setAuthType("Application & Application User");
    itemGet.setAuthTypes("Application & Application User");
    itemGet.setHTTPVerb("GET");
    itemGet.setHttpVerbs("GET");
    itemGet.setThrottlingTier("Unlimited");
    itemGet.setThrottlingTiers("Unlimited");
    itemGet.setScope(sampleScope);
    itemGet.setScopes(sampleScope);
}