Search in sources :

Example 1 with Promise

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

the class UmaPolicyServiceImplDelegationTest method mockPolicyResourceDelegateForNewPolicy.

private void mockPolicyResourceDelegateForNewPolicy() {
    final List<ResourceResponse> createdPolicies = new ArrayList<>();
    ResourceResponse createdPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
    ResourceResponse createdPolicy2 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectTwoPolicyJson());
    createdPolicies.add(createdPolicy1);
    createdPolicies.add(createdPolicy2);
    Promise<List<ResourceResponse>, ResourceException> createPolicyPromise = newResultPromise(createdPolicies);
    given(policyResourceDelegate.createPolicies(any(Context.class), anySetOf(JsonValue.class))).willReturn(createPolicyPromise);
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = new NotFoundException().asPromise();
    given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class))).willReturn(queryPromise);
    given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class), any(QueryResourceHandler.class))).willAnswer(new Answer<Promise<QueryResponse, ResourceException>>() {

        @Override
        public Promise<QueryResponse, ResourceException> answer(InvocationOnMock invocation) throws Throwable {
            final PolicyGraph policyGraph = (PolicyGraph) invocation.getArguments()[2];
            for (ResourceResponse r : createdPolicies) {
                policyGraph.handleResource(r);
            }
            policyGraph.handleResult(newQueryResponse());
            return newResultPromise(newQueryResponse());
        }
    });
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException) Promise(org.forgerock.util.promise.Promise) ResourceResponse(org.forgerock.json.resource.ResourceResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) ArrayList(java.util.ArrayList) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Pair(org.forgerock.util.Pair)

Example 2 with Promise

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

the class UmaPolicyServiceImplDelegationTest method mockPolicyResourceDelegateForUpdatedPolicy.

private void mockPolicyResourceDelegateForUpdatedPolicy() {
    List<ResourceResponse> currentPolicies = new ArrayList<>();
    ResourceResponse currentPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
    ResourceResponse currentPolicy2 = newResourceResponse("ID_2", "REVISION_1", createBackendSubjectTwoPolicyJson());
    currentPolicies.add(currentPolicy1);
    currentPolicies.add(currentPolicy2);
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = newResultPromise(Pair.of((QueryResponse) null, currentPolicies));
    given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class))).willReturn(queryPromise);
    final List<ResourceResponse> updatedPolicies = new ArrayList<>();
    ResourceResponse updatedPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
    ResourceResponse updatedPolicy3 = newResourceResponse("ID_3", "REVISION_1", createBackendSubjectTwoPolicyJson());
    updatedPolicies.add(updatedPolicy1);
    updatedPolicies.add(updatedPolicy3);
    Promise<List<ResourceResponse>, ResourceException> updatePolicyPromise = newResultPromise(updatedPolicies);
    given(policyResourceDelegate.updatePolicies(any(Context.class), Matchers.<Set<JsonValue>>anyObject())).willReturn(updatePolicyPromise);
    given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class), any(QueryResourceHandler.class))).willAnswer(new Answer<Promise<QueryResponse, ResourceException>>() {

        @Override
        public Promise<QueryResponse, ResourceException> answer(InvocationOnMock invocation) throws Throwable {
            final PolicyGraph policyGraph = (PolicyGraph) invocation.getArguments()[2];
            for (ResourceResponse r : updatedPolicies) {
                policyGraph.handleResource(r);
            }
            policyGraph.handleResult(newQueryResponse());
            return newResultPromise(newQueryResponse());
        }
    });
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) Promise(org.forgerock.util.promise.Promise) ResourceResponse(org.forgerock.json.resource.ResourceResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Pair(org.forgerock.util.Pair)

Example 3 with Promise

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

the class PendingRequestResource method actionCollection.

@Override
public Promise<ActionResponse, ResourceException> actionCollection(Context context, ActionRequest request) {
    try {
        if (APPROVE_ACTION_ID.equalsIgnoreCase(request.getAction())) {
            List<Promise<Void, ResourceException>> promises = new ArrayList<>();
            JsonValue content = request.getContent();
            for (UmaPendingRequest pendingRequest : queryResourceOwnerPendingRequests(context)) {
                promises.add(service.approvePendingRequest(context, pendingRequest.getId(), content.get(pendingRequest.getId()), ServerContextUtils.getRealm(context)));
            }
            return handlePendingRequestApproval(promises);
        } else if (DENY_ACTION_ID.equalsIgnoreCase(request.getAction())) {
            for (UmaPendingRequest pendingRequest : queryResourceOwnerPendingRequests(context)) {
                service.denyPendingRequest(pendingRequest.getId(), ServerContextUtils.getRealm(context));
            }
            return newResultPromise(newActionResponse((json(object()))));
        } else {
            return new NotSupportedException("Action, " + request.getAction() + ", is not supported.").asPromise();
        }
    } catch (ResourceException e) {
        return e.asPromise();
    }
}
Also used : Promise(org.forgerock.util.promise.Promise) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) UmaPendingRequest(org.forgerock.openam.sm.datalayer.impl.uma.UmaPendingRequest) ResourceException(org.forgerock.json.resource.ResourceException) NotSupportedException(org.forgerock.json.resource.NotSupportedException)

Example 4 with Promise

use of org.forgerock.util.promise.Promise 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 5 with Promise

use of org.forgerock.util.promise.Promise 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)

Aggregations

Promise (org.forgerock.util.promise.Promise)21 ResourceException (org.forgerock.json.resource.ResourceException)18 JsonValue (org.forgerock.json.JsonValue)14 Promises.newResultPromise (org.forgerock.util.promise.Promises.newResultPromise)14 ArrayList (java.util.ArrayList)11 ResourceResponse (org.forgerock.json.resource.ResourceResponse)9 List (java.util.List)8 BadRequestException (org.forgerock.json.resource.BadRequestException)8 ActionResponse (org.forgerock.json.resource.ActionResponse)7 QueryRequest (org.forgerock.json.resource.QueryRequest)7 SSOToken (com.iplanet.sso.SSOToken)6 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)6 NotFoundException (org.forgerock.json.resource.NotFoundException)6 RealmContext (org.forgerock.openam.rest.RealmContext)6 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)5 QueryResponse (org.forgerock.json.resource.QueryResponse)5 Context (org.forgerock.services.context.Context)5 Pair (org.forgerock.util.Pair)5 IdentityDetails (com.sun.identity.idsvcs.IdentityDetails)4 QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)4