Search in sources :

Example 16 with QueryFilter

use of org.forgerock.util.query.QueryFilter in project OpenAM by OpenRock.

the class ApplicationV1Filter method findOrCreateResourceType.

/**
     * Attempts to first find a resource type that has the same set of
     * actions and resources. If none is found a new resource type is created.
     *
     * @param actions
     *         the map of resource type actions
     * @param resources
     *         the set of resource type patterns
     * @param context
     *         the filter chain context
     * @param request
     *         the create request
     *
     * @return a resource type that matches the passed actions and resources
     *
     * @throws EntitlementException
     *         should some error occur finding or creating a resource type
     */
private ResourceType findOrCreateResourceType(final Map<String, Boolean> actions, Set<String> resources, final Context context, CreateRequest request) throws EntitlementException {
    final Subject callingSubject = contextHelper.getSubject(context);
    final String realm = contextHelper.getRealm(context);
    final Set<QueryFilter<SmsAttribute>> actionFilters = transformSet(actions.entrySet(), new ActionsToQuery());
    final Set<QueryFilter<SmsAttribute>> patternFilters = transformSet(resources, new ResourcesToQuery());
    final Set<ResourceType> resourceTypes = resourceTypeService.getResourceTypes(QueryFilter.and(QueryFilter.and(actionFilters), QueryFilter.and(patternFilters)), callingSubject, realm);
    if (!resourceTypes.isEmpty()) {
        // Some matching resource types have been found, return the first one.
        return resourceTypes.iterator().next();
    }
    final String resourceTypeName = generateResourceTypeName(request);
    final ResourceType resourceType = ResourceType.builder().setName(resourceTypeName).setActions(actions).setPatterns(resources).setDescription("Generated resource type").generateUUID().build();
    // Create and return new resource type.
    return resourceTypeService.saveResourceType(callingSubject, realm, resourceType);
}
Also used : QueryFilter(org.forgerock.util.query.QueryFilter) ResourceType(org.forgerock.openam.entitlement.ResourceType) Subject(javax.security.auth.Subject)

Aggregations

QueryFilter (org.forgerock.util.query.QueryFilter)16 Test (org.testng.annotations.Test)10 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)9 HashSet (java.util.HashSet)8 ResourceException (org.forgerock.json.resource.ResourceException)7 Collection (java.util.Collection)6 QueryResponse (org.forgerock.json.resource.QueryResponse)6 Responses.newQueryResponse (org.forgerock.json.resource.Responses.newQueryResponse)6 UmaPolicy (org.forgerock.openam.uma.UmaPolicy)6 Pair (org.forgerock.util.Pair)6 RealmContext (org.forgerock.openam.rest.RealmContext)5 Context (org.forgerock.services.context.Context)5 RootContext (org.forgerock.services.context.RootContext)5 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JsonValue (org.forgerock.json.JsonValue)3 TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)3 ResourceType (org.forgerock.openam.entitlement.ResourceType)3 Filter (org.forgerock.opendj.ldap.Filter)3