use of org.forgerock.openam.entitlement.rest.model.json.PolicyRequest in project OpenAM by OpenRock.
the class PolicyResource method actionCollection.
/**
* {@inheritDoc}
*/
@Override
public Promise<ActionResponse, ResourceException> actionCollection(Context context, ActionRequest actionRequest) {
final String actionString = actionRequest.getAction();
final PolicyAction action = PolicyAction.getAction(actionString);
if (!PolicyAction.isEvaluateAction(action)) {
final String errorMsg = "Action '" + actionString + "' not implemented for this resource";
final NotSupportedException nsE = new NotSupportedException(errorMsg);
DEBUG.error(errorMsg, nsE);
return nsE.asPromise();
}
try {
if (DEBUG.messageEnabled()) {
DEBUG.message("Rendering policy request for action " + actionString);
}
final PolicyRequest request = requestFactory.buildRequest(action, context, actionRequest);
final PolicyEvaluator evaluator = factory.getEvaluator(request.getRestSubject(), request.getApplication());
if (DEBUG.messageEnabled()) {
final StringBuilder builder = new StringBuilder();
builder.append("Evaluating policy request for action ");
builder.append(actionString);
builder.append(" under realm ");
builder.append(request.getRealm());
builder.append(" within the application context ");
builder.append(request.getApplication());
DEBUG.message(builder.toString());
}
final List<Entitlement> entitlements = evaluator.routePolicyRequest(request);
return newResultPromise(newActionResponse(policyParser.printEntitlements(entitlements)));
} catch (final EntitlementException eE) {
DEBUG.error("Error evaluating policy request", eE);
return resourceErrorHandler.handleError(context, actionRequest, eE).asPromise();
}
}
use of org.forgerock.openam.entitlement.rest.model.json.PolicyRequest in project OpenAM by OpenRock.
the class PolicyRequestFactoryTest method shouldRetrieveBatchRequest.
@Test
public void shouldRetrieveBatchRequest() throws EntitlementException {
// When...
given(subjectContext.getCallerSubject()).willReturn(restSubject);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("resources", Arrays.asList("/resource/a", "/resource/b"));
given(actionRequest.getContent()).willReturn(JsonValue.json(properties));
// Given...
Context context = buildContextStructure("/abc");
PolicyRequest request = factory.buildRequest(PolicyAction.EVALUATE, context, actionRequest);
// Then...
assertThat(request).isNotNull();
assertThat(request.getRealm()).isEqualTo("/abc");
assertThat(request).isInstanceOfAny(BatchPolicyRequest.class);
BatchPolicyRequest batchRequest = (BatchPolicyRequest) request;
assertThat(batchRequest.getResources()).containsOnly("/resource/a", "/resource/b");
verify(subjectContext).getCallerSubject();
verify(actionRequest, times(2)).getContent();
verifyNoMoreInteractions(subjectContext, actionRequest);
}
use of org.forgerock.openam.entitlement.rest.model.json.PolicyRequest in project OpenAM by OpenRock.
the class PolicyRequestFactoryTest method shouldRetrieveTreeRequest.
@Test
public void shouldRetrieveTreeRequest() throws EntitlementException {
// When...
given(subjectContext.getCallerSubject()).willReturn(restSubject);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("resource", "/resource/a");
given(actionRequest.getContent()).willReturn(JsonValue.json(properties));
// Given...
Context context = buildContextStructure("/abc");
PolicyRequest request = factory.buildRequest(PolicyAction.TREE_EVALUATE, context, actionRequest);
// Then...
assertThat(request).isNotNull();
assertThat(request.getRealm()).isEqualTo("/abc");
assertThat(request).isInstanceOfAny(TreePolicyRequest.class);
TreePolicyRequest treeRequest = (TreePolicyRequest) request;
assertThat(treeRequest.getResource()).isEqualTo("/resource/a");
verify(subjectContext).getCallerSubject();
verify(actionRequest, times(2)).getContent();
verifyNoMoreInteractions(subjectContext, actionRequest);
}
Aggregations