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());
}
});
}
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());
}
});
}
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();
}
}
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));
}
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();
}
}
Aggregations