Search in sources :

Example 1 with UpdateRequest

use of org.forgerock.json.resource.UpdateRequest in project OpenAM by OpenRock.

the class AuditRestRouteProvider method addResourceRoutes.

@Override
public void addResourceRoutes(ResourceRouter rootRouter, ResourceRouter realmRouter) {
    rootRouter.route("global-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).toRequestHandler(STARTS_WITH, auditServiceProvider.getDefaultAuditService());
    rootRouter.route("realm-audit").auditAs(AUDIT, AuditEndpointAuditFilter.class).authorizeWith(SpecialOrAdminOrAgentAuthzModule.class).forVersion(1).through(RealmContextFilter.class).toRequestHandler(STARTS_WITH, new RequestHandler() {

        @Override
        public Promise<ActionResponse, ResourceException> handleAction(Context context, ActionRequest actionRequest) {
            return getAuditService(context).handleAction(context, actionRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleCreate(Context context, CreateRequest createRequest) {
            return getAuditService(context).handleCreate(context, createRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleDelete(Context context, DeleteRequest deleteRequest) {
            return getAuditService(context).handleDelete(context, deleteRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handlePatch(Context context, PatchRequest patchRequest) {
            return getAuditService(context).handlePatch(context, patchRequest);
        }

        @Override
        public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest queryRequest, QueryResourceHandler queryResourceHandler) {
            return getAuditService(context).handleQuery(context, queryRequest, queryResourceHandler);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleRead(Context context, ReadRequest readRequest) {
            return getAuditService(context).handleRead(context, readRequest);
        }

        @Override
        public Promise<ResourceResponse, ResourceException> handleUpdate(Context context, UpdateRequest updateRequest) {
            return getAuditService(context).handleUpdate(context, updateRequest);
        }

        private AMAuditService getAuditService(Context context) {
            String realm = context.asContext(RealmContext.class).getResolvedRealm();
            if (StringUtils.isEmpty(realm)) {
                logger.warn("Context contained RealmContext but had an empty resolved realm");
                return auditServiceProvider.getDefaultAuditService();
            }
            return auditServiceProvider.getAuditService(realm);
        }
    });
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) AuditEndpointAuditFilter(org.forgerock.openam.rest.fluent.AuditEndpointAuditFilter) UpdateRequest(org.forgerock.json.resource.UpdateRequest) CreateRequest(org.forgerock.json.resource.CreateRequest) AMAuditService(org.forgerock.openam.audit.AMAuditService) PatchRequest(org.forgerock.json.resource.PatchRequest) Promise(org.forgerock.util.promise.Promise) RequestHandler(org.forgerock.json.resource.RequestHandler) ActionRequest(org.forgerock.json.resource.ActionRequest) RealmContextFilter(org.forgerock.openam.rest.RealmContextFilter) SpecialOrAdminOrAgentAuthzModule(org.forgerock.openam.rest.authz.SpecialOrAdminOrAgentAuthzModule) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) DeleteRequest(org.forgerock.json.resource.DeleteRequest) ReadRequest(org.forgerock.json.resource.ReadRequest)

Example 2 with UpdateRequest

use of org.forgerock.json.resource.UpdateRequest in project OpenAM by OpenRock.

the class PolicyV1FilterTest method resourceTypeAssociationOnUpdate.

/**
     * Verifies that the appropriate resource type is associated with the policy being updated.
     */
@Test
public void resourceTypeAssociationOnUpdate() throws Exception {
    // Given
    given(contextHelper.getRealm(context)).willReturn("/abc");
    given(contextHelper.getSubject(context)).willReturn(subject);
    UpdateRequest updateRequest = mock(UpdateRequest.class);
    JsonValue jsonValue = json(object(field("applicationName", "testApp")));
    given(updateRequest.getContent()).willReturn(jsonValue);
    given(applicationServiceFactory.create(subject, "/abc")).willReturn(applicationService);
    Application application = mock(Application.class);
    given(applicationService.getApplication("testApp")).willReturn(application);
    Set<String> resourceTypeUUIDs = new HashSet<>(CollectionUtils.asSet("abc-def-hij"));
    given(application.getResourceTypeUuids()).willReturn(resourceTypeUUIDs);
    // When
    Promise<ResourceResponse, ResourceException> promise = filter.filterUpdate(context, updateRequest, requestHandler);
    // Then
    assertThat(promise).succeeded();
    verify(applicationServiceFactory).create(subject, "/abc");
    verify(applicationService).getApplication("testApp");
    assertThat(jsonValue.get("resourceTypeUuid").asString()).isEqualTo("abc-def-hij");
    assertThat(promise.get().getContent().contains("resourceTypeUuid")).isFalse();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) UpdateRequest(org.forgerock.json.resource.UpdateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Application(com.sun.identity.entitlement.Application) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 3 with UpdateRequest

use of org.forgerock.json.resource.UpdateRequest in project OpenAM by OpenRock.

the class RealmContextFilter method filterUpdate.

@Override
public Promise<ResourceResponse, ResourceException> filterUpdate(Context context, UpdateRequest request, RequestHandler next) {
    try {
        Context newContext = evaluate(context, request);
        UpdateRequest evaluatedRequest = wasEvaluated(context, newContext) ? copyOfUpdateRequest(request).setResourcePath(newContext.asContext(UriRouterContext.class).getRemainingUri()) : request;
        return next.handleUpdate(newContext, evaluatedRequest);
    } catch (ResourceException e) {
        return newExceptionPromise(e);
    }
}
Also used : UriRouterContext(org.forgerock.http.routing.UriRouterContext) Context(org.forgerock.services.context.Context) HttpContext(org.forgerock.json.resource.http.HttpContext) UpdateRequest(org.forgerock.json.resource.UpdateRequest) UriRouterContext(org.forgerock.http.routing.UriRouterContext) ResourceException(org.forgerock.json.resource.ResourceException)

Example 4 with UpdateRequest

use of org.forgerock.json.resource.UpdateRequest in project OpenAM by OpenRock.

the class ApplicationsResourceTest method updateInstanceShouldReturnServerInternalExceptionWhenApplicationToJson.

@Test(expectedExceptions = InternalServerErrorException.class)
public void updateInstanceShouldReturnServerInternalExceptionWhenApplicationToJson() throws EntitlementException, ResourceException {
    //Given
    SSOTokenContext subjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(subjectContext);
    realmContext.setSubRealm("REALM", "REALM");
    Context context = ClientContext.newInternalClientContext(realmContext);
    String resourceId = "iPlanetAMWebAgentService";
    UpdateRequest request = mock(UpdateRequest.class);
    Subject subject = new Subject();
    JsonValue content = mock(JsonValue.class);
    Application application = mock(Application.class);
    Application newApplication = mock(Application.class);
    given(subjectContext.getCallerSubject()).willReturn(subject);
    given(request.getContent()).willReturn(content);
    given(applicationManagerWrapper.getApplication(subject, "/REALM", resourceId)).willReturn(application);
    given(applicationWrapper.getName()).willReturn("APP_NAME");
    given(applicationWrapper.getApplication()).willReturn(newApplication);
    given(newApplication.getLastModifiedDate()).willReturn(1000L);
    doThrow(new EntitlementException(1)).when(applicationWrapper).toJsonValue();
    //When
    Promise<ResourceResponse, ResourceException> result = applicationsResource.updateInstance(context, resourceId, request);
    //Then
    verify(applicationManagerWrapper).updateApplication(application, applicationWrapper.getApplication(), subject, "/REALM");
    result.getOrThrowUninterruptibly();
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) EntitlementException(com.sun.identity.entitlement.EntitlementException) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) UpdateRequest(org.forgerock.json.resource.UpdateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 5 with UpdateRequest

use of org.forgerock.json.resource.UpdateRequest in project OpenAM by OpenRock.

the class ApplicationsResourceTest method shouldNotUpdateInstanceWhenSubjectIsNull.

@Test(expectedExceptions = BadRequestException.class)
public void shouldNotUpdateInstanceWhenSubjectIsNull() throws EntitlementException, ResourceException {
    //Given
    SSOTokenContext subjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(subjectContext);
    realmContext.setSubRealm("REALM", "REALM");
    Context context = ClientContext.newInternalClientContext(realmContext);
    String resourceId = "RESOURCE_ID";
    UpdateRequest request = mock(UpdateRequest.class);
    Subject subject = null;
    given(subjectContext.getCallerSubject()).willReturn(subject);
    //When
    Promise<ResourceResponse, ResourceException> result = applicationsResource.updateInstance(context, resourceId, request);
    //Then
    result.getOrThrowUninterruptibly();
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) UpdateRequest(org.forgerock.json.resource.UpdateRequest) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Aggregations

UpdateRequest (org.forgerock.json.resource.UpdateRequest)36 Test (org.testng.annotations.Test)33 ResourceResponse (org.forgerock.json.resource.ResourceResponse)27 JsonValue (org.forgerock.json.JsonValue)26 ResourceException (org.forgerock.json.resource.ResourceException)26 Context (org.forgerock.services.context.Context)16 Matchers.anyString (org.mockito.Matchers.anyString)13 Subject (javax.security.auth.Subject)11 Application (com.sun.identity.entitlement.Application)9 RealmContext (org.forgerock.openam.rest.RealmContext)9 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)7 ClientContext (org.forgerock.services.context.ClientContext)7 HashSet (java.util.HashSet)6 ResourceType (org.forgerock.openam.entitlement.ResourceType)5 EntitlementException (com.sun.identity.entitlement.EntitlementException)4 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)2 Router (org.forgerock.json.resource.Router)2 UmaPolicy (org.forgerock.openam.uma.UmaPolicy)2 DelegationPermission (com.sun.identity.delegation.DelegationPermission)1 Privilege (com.sun.identity.entitlement.Privilege)1