Search in sources :

Example 1 with ResultHandler

use of org.forgerock.util.promise.ResultHandler in project OpenAM by OpenRock.

the class PolicyResourceDelegate method createPolicies.

/**
     * <p>Creates the underlying backend policies.</p>
     *
     * <p>NOTE: if the creation of the underlying policies fails, any successfully
     * created underlying policies will be attempted to be deleted but if the deletion
     * fails, then the underlying policies may be in an inconsistent state.</p>
     *
     * @param context The request context.
     * @param policies The underlying policies to create.
     * @return A promise containing the list of created underlying policies or a {@code ResourceException} if
     * the creation fails.
     */
public Promise<List<ResourceResponse>, ResourceException> createPolicies(Context context, Set<JsonValue> policies) {
    final List<String> policyIds = new ArrayList<String>();
    List<Promise<ResourceResponse, ResourceException>> promises = new ArrayList<>();
    for (JsonValue policy : policies) {
        promises.add(policyResource.handleCreate(context, Requests.newCreateRequest("", policy)).thenOnResult(new ResultHandler<ResourceResponse>() {

            @Override
            public void handleResult(ResourceResponse result) {
                //Save ids of created policies, in case a latter policy fails to be created,
                // so we can roll back.
                policyIds.add(result.getId());
            }
        }));
    }
    return Promises.when(promises).thenAsync(new AsyncFunction<List<ResourceResponse>, List<ResourceResponse>, ResourceException>() {

        @Override
        public Promise<List<ResourceResponse>, ResourceException> apply(List<ResourceResponse> value) {
            return newResultPromise(value);
        }
    }, new UmaPolicyCreateFailureHandler(context, policyIds));
}
Also used : ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) ResultHandler(org.forgerock.util.promise.ResultHandler) Promises.newExceptionPromise(org.forgerock.util.promise.Promises.newExceptionPromise) Promises.newResultPromise(org.forgerock.util.promise.Promises.newResultPromise) Promise(org.forgerock.util.promise.Promise) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ArrayList(java.util.ArrayList) List(java.util.List) ResourceException(org.forgerock.json.resource.ResourceException)

Example 2 with ResultHandler

use of org.forgerock.util.promise.ResultHandler in project OpenAM by OpenRock.

the class UmaPolicyServiceImpl method updatePolicy.

/**
     * {@inheritDoc}
     */
@Override
public //TODO need to check if need to delete backend policies
Promise<UmaPolicy, ResourceException> updatePolicy(//TODO need to check if need to delete backend policies
final Context context, //TODO need to check if need to delete backend policies
final String resourceSetId, JsonValue policy) {
    final UmaPolicy updatedUmaPolicy;
    final ResourceSetDescription resourceSet;
    try {
        resourceSet = getResourceSet(getRealm(context), resourceSetId);
        updatedUmaPolicy = UmaPolicy.valueOf(resourceSet, resolveUsernameToUID(context, policy));
        boolean canShare = canUserShareResourceSet(resourceSet.getResourceOwnerId(), contextHelper.getUserId(context), resourceSet.getClientId(), getRealm(context), resourceSet.getId(), updatedUmaPolicy.getScopes());
        if (!canShare) {
            return new ForbiddenException().asPromise();
        }
        validateScopes(resourceSet, updatedUmaPolicy.getScopes());
    } catch (ResourceException e) {
        return e.asPromise();
    }
    return internalReadPolicy(context, resourceSetId).thenAsync(beforeResourceSharedModified(updatedUmaPolicy)).thenOnResult(new ResultHandler<UmaPolicy>() {

        @Override
        public void handleResult(UmaPolicy currentUmaPolicy) {
            Set<String> modifiedScopes = new HashSet<>(updatedUmaPolicy.getScopes());
            modifiedScopes.retainAll(currentUmaPolicy.getScopes());
            Set<String> removedScopes = new HashSet<>(currentUmaPolicy.getScopes());
            removedScopes.removeAll(modifiedScopes);
            for (JsonValue policy : currentUmaPolicy.asUnderlyingPolicies(contextHelper.getUserId(context))) {
                for (String scope : removedScopes) {
                    if (policy.get("actionValues").isDefined(scope)) {
                        policyResourceDelegate.queryPolicies(context, Requests.newQueryRequest("").setQueryFilter(QueryFilter.and(QueryFilter.equalTo(new JsonPointer("createdBy"), contextHelper.getUserUid(context)), QueryFilter.equalTo(new JsonPointer("name"), policy.get("name").asString())))).thenAsync(new DeleteOldPolicyFunction(context));
                    }
                }
            }
        }
    }).thenOnResult(new ResultHandler<UmaPolicy>() {

        @Override
        public void handleResult(UmaPolicy currentUmaPolicy) {
            Set<String> modifiedScopes = new HashSet<>(currentUmaPolicy.getScopes());
            modifiedScopes.retainAll(updatedUmaPolicy.getScopes());
            Set<String> deletedScopes = new HashSet<>(updatedUmaPolicy.getScopes());
            deletedScopes.removeAll(modifiedScopes);
            for (JsonValue policy : updatedUmaPolicy.asUnderlyingPolicies(contextHelper.getUserId(context))) {
                for (String scope : deletedScopes) {
                    if (policy.get("actionValues").isDefined(scope)) {
                        policyResourceDelegate.createPolicies(context, singleton(policy));
                    }
                }
            }
        }
    }).thenOnResult(new ResultHandler<UmaPolicy>() {

        @Override
        public void handleResult(UmaPolicy currentUmaPolicy) {
            String uid = contextHelper.getUserId(context);
            Set<String> underlyingPolicyIds = new HashSet<>(currentUmaPolicy.getUnderlyingPolicyIds());
            Set<JsonValue> newUnderlyingPolicies = updatedUmaPolicy.asUnderlyingPolicies(uid);
            for (JsonValue value : newUnderlyingPolicies) {
                underlyingPolicyIds.remove(value.get("name").asString());
            }
            policyResourceDelegate.deletePolicies(context, underlyingPolicyIds);
        }
    }).thenAsync(new UpdatePolicyGraphStatesFunction<UmaPolicy>(resourceSet, context)).thenAsync(new UpdateUmaPolicyFunction(context, updatedUmaPolicy, resourceSetId, resourceSet));
}
Also used : ForbiddenException(org.forgerock.json.resource.ForbiddenException) Set(java.util.Set) HashSet(java.util.HashSet) JsonValue(org.forgerock.json.JsonValue) JsonPointer(org.forgerock.json.JsonPointer) ResultHandler(org.forgerock.util.promise.ResultHandler) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet)

Aggregations

JsonValue (org.forgerock.json.JsonValue)2 ResourceException (org.forgerock.json.resource.ResourceException)2 ResultHandler (org.forgerock.util.promise.ResultHandler)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 JsonPointer (org.forgerock.json.JsonPointer)1 ForbiddenException (org.forgerock.json.resource.ForbiddenException)1 ResourceResponse (org.forgerock.json.resource.ResourceResponse)1 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)1 UmaPolicy (org.forgerock.openam.uma.UmaPolicy)1 Promise (org.forgerock.util.promise.Promise)1 Promises.newExceptionPromise (org.forgerock.util.promise.Promises.newExceptionPromise)1 Promises.newResultPromise (org.forgerock.util.promise.Promises.newResultPromise)1