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