Search in sources :

Example 1 with PolicyRequest

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();
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) NotSupportedException(org.forgerock.json.resource.NotSupportedException) Entitlement(com.sun.identity.entitlement.Entitlement) PolicyRequest(org.forgerock.openam.entitlement.rest.model.json.PolicyRequest)

Example 2 with PolicyRequest

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);
}
Also used : Context(org.forgerock.services.context.Context) ClientContext(org.forgerock.services.context.ClientContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) RealmContext(org.forgerock.openam.rest.RealmContext) HashMap(java.util.HashMap) BatchPolicyRequest(org.forgerock.openam.entitlement.rest.model.json.BatchPolicyRequest) TreePolicyRequest(org.forgerock.openam.entitlement.rest.model.json.TreePolicyRequest) PolicyRequest(org.forgerock.openam.entitlement.rest.model.json.PolicyRequest) BatchPolicyRequest(org.forgerock.openam.entitlement.rest.model.json.BatchPolicyRequest) Test(org.testng.annotations.Test)

Example 3 with PolicyRequest

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);
}
Also used : Context(org.forgerock.services.context.Context) ClientContext(org.forgerock.services.context.ClientContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) RealmContext(org.forgerock.openam.rest.RealmContext) HashMap(java.util.HashMap) TreePolicyRequest(org.forgerock.openam.entitlement.rest.model.json.TreePolicyRequest) TreePolicyRequest(org.forgerock.openam.entitlement.rest.model.json.TreePolicyRequest) PolicyRequest(org.forgerock.openam.entitlement.rest.model.json.PolicyRequest) BatchPolicyRequest(org.forgerock.openam.entitlement.rest.model.json.BatchPolicyRequest) Test(org.testng.annotations.Test)

Aggregations

PolicyRequest (org.forgerock.openam.entitlement.rest.model.json.PolicyRequest)3 HashMap (java.util.HashMap)2 BatchPolicyRequest (org.forgerock.openam.entitlement.rest.model.json.BatchPolicyRequest)2 TreePolicyRequest (org.forgerock.openam.entitlement.rest.model.json.TreePolicyRequest)2 RealmContext (org.forgerock.openam.rest.RealmContext)2 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)2 ClientContext (org.forgerock.services.context.ClientContext)2 Context (org.forgerock.services.context.Context)2 Test (org.testng.annotations.Test)2 Entitlement (com.sun.identity.entitlement.Entitlement)1 EntitlementException (com.sun.identity.entitlement.EntitlementException)1 NotSupportedException (org.forgerock.json.resource.NotSupportedException)1