Search in sources :

Example 16 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpoint method updateResourceSet.

@Put
public Representation updateResourceSet(JsonRepresentation entity) throws NotFoundException, ServerException, BadRequestException {
    if (!isConditionalRequest()) {
        throw new ResourceException(512, "precondition_failed", "Require If-Match header to update Resource Set", null);
    }
    final Map<String, Object> resourceSetDescriptionAttributes = validator.validate(toMap(entity));
    final String resourceSetId = getResourceSetId();
    ResourceSetStore store = providerSettingsFactory.get(requestFactory.create(getRequest())).getResourceSetStore();
    ResourceSetDescription resourceSetDescription = store.read(resourceSetId, getResourceOwnerId()).update(resourceSetDescriptionAttributes);
    JsonValue labels = resourceSetDescription.getDescription().get(OAuth2Constants.ResourceSets.LABELS);
    resourceSetDescription.getDescription().remove(OAuth2Constants.ResourceSets.LABELS);
    store.update(resourceSetDescription);
    if (labels.isNotNull()) {
        resourceSetDescription.getDescription().add(OAuth2Constants.ResourceSets.LABELS, labels.asSet());
    } else {
        resourceSetDescription.getDescription().add(OAuth2Constants.ResourceSets.LABELS, new HashSet<String>());
    }
    labelRegistration.updateLabelsForExistingResourceSet(resourceSetDescription);
    return createJsonResponse(resourceSetDescription, false, true);
}
Also used : ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.restlet.resource.ResourceException) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Put(org.restlet.resource.Put)

Example 17 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpoint method listResourceSets.

private Representation listResourceSets() throws ServerException, NotFoundException {
    ResourceSetStore store = providerSettingsFactory.get(requestFactory.create(getRequest())).getResourceSetStore();
    QueryFilter<String> query = QueryFilter.and(QueryFilter.equalTo(ResourceSetTokenField.CLIENT_ID, getClientId()), QueryFilter.equalTo(ResourceSetTokenField.RESOURCE_OWNER_ID, getResourceOwnerId()));
    Set<ResourceSetDescription> resourceSetDescriptions = store.query(query);
    Set<String> resourceSetIds = new HashSet<String>();
    for (ResourceSetDescription resourceSetDescription : resourceSetDescriptions) {
        resourceSetIds.add(resourceSetDescription.getId());
    }
    return jacksonRepresentationFactory.create(resourceSetIds);
}
Also used : ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) HashSet(java.util.HashSet)

Example 18 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpoint method readResourceSet.

private Representation readResourceSet(String resourceSetId) throws NotFoundException, ServerException {
    ResourceSetStore store = providerSettingsFactory.get(requestFactory.create(getRequest())).getResourceSetStore();
    ResourceSetDescription resourceSetDescription = store.read(resourceSetId, getResourceOwnerId());
    Set<String> labels = new HashSet<String>();
    try {
        Set<ResourceSetLabel> labelSet = umaLabelsStore.forResourceSet(resourceSetDescription.getRealm(), resourceSetDescription.getResourceOwnerId(), resourceSetDescription.getId(), false);
        for (ResourceSetLabel label : labelSet) {
            labels.add(label.getName());
        }
    } catch (org.forgerock.json.resource.ResourceException e) {
        throw new ServerException(e);
    }
    resourceSetDescription.getDescription().put("labels", labels);
    return createJsonResponse(resourceSetDescription, true, true);
}
Also used : ServerException(org.forgerock.oauth2.core.exceptions.ServerException) ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) HashSet(java.util.HashSet) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)

Example 19 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetResource method updateInstance.

/**
     * Update the none system labels on a resource set only
     *
     * @param context {@inheritDoc}
     * @param request {@inheritDoc}
     */
@Override
public Promise<ResourceResponse, ResourceException> updateInstance(Context context, String resourceId, UpdateRequest request) {
    final Map<String, Object> resourceSetDescriptionAttributes;
    try {
        resourceSetDescriptionAttributes = validator.validate(request.getContent().asMap());
        final String realm = getRealm(context);
        final String userId = getUserId(context);
        //remove this resource set id from all labels
        Set<ResourceSetLabel> labels = umaLabelsStore.forResourceSet(realm, userId, resourceId, true);
        for (ResourceSetLabel label : labels) {
            if (!isSystemLabel(label)) {
                label.removeResourceSetId(resourceId);
                umaLabelsStore.update(realm, userId, label);
            }
        }
        //add resource set id to new labels
        for (String labelId : (List<String>) resourceSetDescriptionAttributes.get("labels")) {
            ResourceSetLabel label = umaLabelsStore.read(realm, userId, labelId);
            label.addResourceSetId(resourceId);
            umaLabelsStore.update(realm, userId, label);
        }
        return resourceSetService.getResourceSet(context, realm, resourceId, userId, augmentWithPolicies(request)).thenAsync(new AsyncFunction<ResourceSetDescription, ResourceResponse, ResourceException>() {

            @Override
            public Promise<ResourceResponse, ResourceException> apply(ResourceSetDescription result) {
                try {
                    JsonValue content = null;
                    content = getResourceSetJson(result, userId);
                    return newResultPromise(newResource(result.getId(), content));
                } catch (ResourceException e) {
                    return e.asPromise();
                }
            }
        });
    } catch (ResourceException e) {
        return e.asPromise();
    } catch (org.forgerock.oauth2.core.exceptions.BadRequestException e) {
        return new BadRequestException("Error retrieving labels.", e).asPromise();
    }
}
Also used : JsonValue(org.forgerock.json.JsonValue) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel) Promises.newResultPromise(org.forgerock.util.promise.Promises.newResultPromise) Promise(org.forgerock.util.promise.Promise) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) BadRequestException(org.forgerock.json.resource.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) ResourceException(org.forgerock.json.resource.ResourceException)

Example 20 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetService method getResourceSets.

/**
     * Queries resource sets across the resource set store and UMA policy store.
     *
     * @param context             The context.
     * @param realm               The realm.
     * @param query               The aggregated query.
     * @param resourceOwnerId     The resource owner id.
     * @param augmentWithPolicies {@code true} to pull in UMA policies into the resource set.
     * @return A Promise containing the Resource Sets or a ResourceException.
     */
Promise<Collection<ResourceSetDescription>, ResourceException> getResourceSets(final Context context, String realm, final ResourceSetWithPolicyQuery query, final String resourceOwnerId, final boolean augmentWithPolicies) {
    final Set<ResourceSetDescription> resourceSets;
    try {
        resourceSets = new ResourceSetSharedFilter(this, resourceOwnerId, realm).filter(resourceSetStoreFactory.create(realm).query(query.getResourceSetQuery()));
    } catch (ServerException e) {
        return new InternalServerErrorException(e).asPromise();
    }
    QueryRequest policyQuery = newQueryRequest("").setQueryId("searchAll");
    policyQuery.setQueryFilter(QueryFilter.<JsonPointer>alwaysTrue());
    return getSharedResourceSets(context, policyQuery, resourceOwnerId).thenAsync(new AsyncFunction<Set<ResourceSetDescription>, Collection<ResourceSetDescription>, ResourceException>() {

        @Override
        public Promise<Collection<ResourceSetDescription>, ResourceException> apply(final Set<ResourceSetDescription> sharedResourceSets) {
            //combine the owned ResourceSets with the shared ones, then filter based on the query
            sharedResourceSets.addAll(resourceSets);
            final Collection<ResourceSetDescription> filteredResourceSets = filterPolicies(resourceSets, query);
            Promise<Collection<ResourceSetDescription>, ResourceException> resourceSetsPromise;
            if (query.getPolicyQuery() != null) {
                QueryRequest policyQuery = newQueryRequest("").setQueryFilter(query.getPolicyQuery());
                resourceSetsPromise = policyService.queryPolicies(context, policyQuery).thenAsync(new AsyncFunction<Pair<QueryResponse, Collection<UmaPolicy>>, Collection<ResourceSetDescription>, ResourceException>() {

                    @Override
                    public Promise<Collection<ResourceSetDescription>, ResourceException> apply(Pair<QueryResponse, Collection<UmaPolicy>> result) throws ResourceException {
                        try {
                            return newResultPromise(combine(context, query, filteredResourceSets, result.getSecond(), augmentWithPolicies, resourceOwnerId));
                        } catch (org.forgerock.oauth2.core.exceptions.NotFoundException e) {
                            return new InternalServerErrorException(e).asPromise();
                        } catch (ServerException e) {
                            return new InternalServerErrorException(e).asPromise();
                        }
                    }
                });
            } else {
                if (augmentWithPolicies) {
                    List<Promise<ResourceSetDescription, ResourceException>> promises = new ArrayList<>();
                    PromiseImpl<ResourceSetDescription, ResourceException> kicker = PromiseImpl.create();
                    promises.add(kicker);
                    for (ResourceSetDescription resourceSet : filteredResourceSets) {
                        promises.add(augmentWithPolicy(context, resourceSet.getId(), resourceSet));
                    }
                    resourceSetsPromise = Promises.when(promises).thenAsync(new AsyncFunction<List<ResourceSetDescription>, Collection<ResourceSetDescription>, ResourceException>() {

                        @Override
                        public Promise<Collection<ResourceSetDescription>, ResourceException> apply(List<ResourceSetDescription> resourceSets) {
                            Collection<ResourceSetDescription> resourceSetDescriptions = new HashSet<>();
                            for (ResourceSetDescription rs : filteredResourceSets) {
                                if (rs != null) {
                                    resourceSetDescriptions.add(rs);
                                }
                            }
                            return newResultPromise(resourceSetDescriptions);
                        }
                    });
                    kicker.handleResult(null);
                } else {
                    resourceSetsPromise = newResultPromise(filteredResourceSets);
                }
            }
            return resourceSetsPromise;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceException(org.forgerock.json.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Pair(org.forgerock.util.Pair) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) Requests.newQueryRequest(org.forgerock.json.resource.Requests.newQueryRequest) QueryRequest(org.forgerock.json.resource.QueryRequest) PromiseImpl(org.forgerock.util.promise.PromiseImpl) ResourceSetSharedFilter(org.forgerock.openam.uma.ResourceSetSharedFilter) Promises.newResultPromise(org.forgerock.util.promise.Promises.newResultPromise) Promise(org.forgerock.util.promise.Promise) QueryResponse(org.forgerock.json.resource.QueryResponse) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) Collection(java.util.Collection)

Aggregations

ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)59 Test (org.testng.annotations.Test)33 ResourceException (org.forgerock.json.resource.ResourceException)19 HashSet (java.util.HashSet)15 UmaPolicy (org.forgerock.openam.uma.UmaPolicy)15 Context (org.forgerock.services.context.Context)14 JsonValue (org.forgerock.json.JsonValue)12 QueryResponse (org.forgerock.json.resource.QueryResponse)12 Collection (java.util.Collection)11 ResourceSetStore (org.forgerock.oauth2.resources.ResourceSetStore)11 RealmContext (org.forgerock.openam.rest.RealmContext)11 HashMap (java.util.HashMap)10 Responses.newQueryResponse (org.forgerock.json.resource.Responses.newQueryResponse)10 RootContext (org.forgerock.services.context.RootContext)10 Pair (org.forgerock.util.Pair)10 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)9 QueryFilter (org.forgerock.util.query.QueryFilter)9 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)9 List (java.util.List)8 ResourceSetLabel (org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)8