Search in sources :

Example 1 with Handler

use of org.forgerock.http.Handler in project OpenAM by OpenRock.

the class CrestProtocolEnforcementFilterTest method requestWithCorrectProtocolVersionShouldCallHandler.

@Test
public void requestWithCorrectProtocolVersionShouldCallHandler() {
    //Given
    Context context = mock(Context.class);
    Request request = new Request();
    Handler next = mock(Handler.class);
    request.getHeaders().put(AcceptApiVersionHeader.valueOf("protocol=1"));
    //When
    filter.filter(context, request, next);
    //Then
    assertThat(AcceptApiVersionHeader.valueOf(request).getProtocolVersion()).isEqualTo(version(1));
    verify(next).handle(context, request);
}
Also used : Context(org.forgerock.services.context.Context) Request(org.forgerock.http.protocol.Request) Handler(org.forgerock.http.Handler) Test(org.testng.annotations.Test)

Example 2 with Handler

use of org.forgerock.http.Handler in project OpenAM by OpenRock.

the class CrestProtocolEnforcementFilterTest method requestWithIncorrectProtocolMajorVersionShouldReturnBadRequestResponse.

@Test
public void requestWithIncorrectProtocolMajorVersionShouldReturnBadRequestResponse() throws IOException {
    //Given
    Context context = mock(Context.class);
    Request request = new Request();
    Handler next = mock(Handler.class);
    request.getHeaders().put(AcceptApiVersionHeader.valueOf("protocol=2"));
    //When
    Response response = filter.filter(context, request, next).getOrThrowUninterruptibly();
    //Then
    assertThat(getUnsupportedMajorVersionExceptionJson(version(2))).isEqualTo(response.getEntity().getJson());
    assertThat(AcceptApiVersionHeader.valueOf(request).getProtocolVersion()).isEqualTo(version(2));
    verify(next, never()).handle(context, request);
}
Also used : Context(org.forgerock.services.context.Context) Response(org.forgerock.http.protocol.Response) Request(org.forgerock.http.protocol.Request) Handler(org.forgerock.http.Handler) Test(org.testng.annotations.Test)

Example 3 with Handler

use of org.forgerock.http.Handler in project OpenAM by OpenRock.

the class RealmContextFilterTest method filterShouldConsumeRealmFromCRUDPAQRequestWithSubrealm.

@Test(dataProvider = "CRUDPAQ")
public void filterShouldConsumeRealmFromCRUDPAQRequestWithSubrealm(Request request, String postURIString) throws Exception {
    //Given
    RequestHandler requestHandler = mock(RequestHandler.class);
    String path = SUB_REALM + "/" + ENDPOINT_PATH_ELEMENT;
    Context context = mockContext(path);
    request.setUri(createRequestURI(HOSTNAME, path, postURIString));
    mockDnsAlias(HOSTNAME, "/");
    mockRealmAlias("/" + SUB_REALM, "/" + SUB_REALM);
    //When
    Handler httpHandler = getHttpHandler(requestHandler);
    httpHandler.handle(context, request).getOrThrowUninterruptibly();
    //Then
    ArgumentCaptor<Context> contextCaptor = ArgumentCaptor.forClass(Context.class);
    ArgumentCaptor<org.forgerock.json.resource.Request> requestCaptor = ArgumentCaptor.forClass(org.forgerock.json.resource.Request.class);
    collectCRUDPAQArguments(requestHandler, contextCaptor, requestCaptor);
    verifyRealmContext(contextCaptor.getValue(), "", "/" + SUB_REALM, null);
    verifyUriRouterContext(contextCaptor.getValue(), SUB_REALM);
    verifyResolvedResourcePath(requestCaptor.getValue(), ENDPOINT_PATH_ELEMENT);
}
Also used : RootContext(org.forgerock.services.context.RootContext) UriRouterContext(org.forgerock.http.routing.UriRouterContext) Context(org.forgerock.services.context.Context) AttributesContext(org.forgerock.services.context.AttributesContext) RequestHandler(org.forgerock.json.resource.RequestHandler) CreateRequest(org.forgerock.json.resource.CreateRequest) ActionRequest(org.forgerock.json.resource.ActionRequest) ReadRequest(org.forgerock.json.resource.ReadRequest) DeleteRequest(org.forgerock.json.resource.DeleteRequest) UpdateRequest(org.forgerock.json.resource.UpdateRequest) PatchRequest(org.forgerock.json.resource.PatchRequest) Request(org.forgerock.http.protocol.Request) QueryRequest(org.forgerock.json.resource.QueryRequest) RequestHandler(org.forgerock.json.resource.RequestHandler) QueryResponseHandler(org.forgerock.openam.rest.query.QueryResponseHandler) Handler(org.forgerock.http.Handler) Test(org.testng.annotations.Test)

Example 4 with Handler

use of org.forgerock.http.Handler in project OpenAM by OpenRock.

the class Endpoints method from.

/**
     * Produce a {@code Handler} from the annotated methods on the provided object.
     * <p>
     * This method currently only distinguishes requests by their method type. In future this
     * should be extended to support selection by request and response media types, and request
     * path.
     * @param obj The object containing annotated methods.
     * @return A new {@code Handler}.
     */
public static Handler from(final Object obj) {
    final Map<String, AnnotatedMethod> methods = new HashMap<>();
    methods.put("DELETE", AnnotatedMethod.findMethod(obj, Delete.class));
    methods.put("GET", AnnotatedMethod.findMethod(obj, Get.class));
    methods.put("POST", AnnotatedMethod.findMethod(obj, Post.class));
    methods.put("PUT", AnnotatedMethod.findMethod(obj, Put.class));
    return new Handler() {

        @Override
        public Promise<Response, NeverThrowsException> handle(Context context, Request request) {
            AnnotatedMethod method = methods.get(getMethod(request));
            if (method == null) {
                Response response = new Response(Status.METHOD_NOT_ALLOWED);
                response.setEntity(new NotSupportedException().toJsonValue().getObject());
                return newResultPromise(response);
            }
            return method.invoke(context, request);
        }
    };
}
Also used : Context(org.forgerock.services.context.Context) HashMap(java.util.HashMap) Request(org.forgerock.http.protocol.Request) Handler(org.forgerock.http.Handler) Response(org.forgerock.http.protocol.Response) NeverThrowsException(org.forgerock.util.promise.NeverThrowsException) NotSupportedException(org.forgerock.json.resource.NotSupportedException)

Example 5 with Handler

use of org.forgerock.http.Handler in project OpenAM by OpenRock.

the class AbstractHttpAccessAuditFilterTest method shouldAuditAccessAttemptAndResult.

@Test(dataProvider = "handlerResponses")
public void shouldAuditAccessAttemptAndResult(Status responseStatus) throws AuditException {
    //Given
    Context context = new RequestAuditContext(mockContext());
    Request request = new Request().setMethod("GET").setUri(URI.create("http://example.com:8080?query=value"));
    request.getHeaders().put(ContentTypeHeader.valueOf("CONTENT_TYPE"));
    enableAccessTopicAuditing();
    Handler handler = mockHandler(context, request, responseStatus);
    //When
    auditFilter.filter(context, request, handler);
    //Then
    ArgumentCaptor<AuditEvent> auditEventCaptor = ArgumentCaptor.forClass(AuditEvent.class);
    verify(eventPublisher, times(2)).tryPublish(eq(AuditConstants.ACCESS_TOPIC), auditEventCaptor.capture());
    verifyAccessAttemptAuditEvent(auditEventCaptor.getAllValues().get(0).getValue());
    if (responseStatus.isSuccessful()) {
        verifyAccessSuccessAuditEvent(auditEventCaptor.getAllValues().get(1).getValue());
    } else {
        verifyAccessFailedAuditEvent(auditEventCaptor.getAllValues().get(1).getValue());
    }
}
Also used : RootContext(org.forgerock.services.context.RootContext) ClientContext(org.forgerock.services.context.ClientContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Context(org.forgerock.services.context.Context) AuditRequestContext(org.forgerock.openam.audit.context.AuditRequestContext) SessionContext(org.forgerock.http.session.SessionContext) AttributesContext(org.forgerock.services.context.AttributesContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Request(org.forgerock.http.protocol.Request) Handler(org.forgerock.http.Handler) AuditEvent(org.forgerock.audit.events.AuditEvent) Test(org.testng.annotations.Test)

Aggregations

Handler (org.forgerock.http.Handler)13 Request (org.forgerock.http.protocol.Request)10 Context (org.forgerock.services.context.Context)10 Test (org.testng.annotations.Test)9 AttributesContext (org.forgerock.services.context.AttributesContext)5 RootContext (org.forgerock.services.context.RootContext)5 Response (org.forgerock.http.protocol.Response)4 UriRouterContext (org.forgerock.http.routing.UriRouterContext)3 ActionRequest (org.forgerock.json.resource.ActionRequest)3 CreateRequest (org.forgerock.json.resource.CreateRequest)3 DeleteRequest (org.forgerock.json.resource.DeleteRequest)3 PatchRequest (org.forgerock.json.resource.PatchRequest)3 QueryRequest (org.forgerock.json.resource.QueryRequest)3 ReadRequest (org.forgerock.json.resource.ReadRequest)3 RequestHandler (org.forgerock.json.resource.RequestHandler)3 UpdateRequest (org.forgerock.json.resource.UpdateRequest)3 QueryResponseHandler (org.forgerock.openam.rest.query.QueryResponseHandler)3 AuditEvent (org.forgerock.audit.events.AuditEvent)2 SessionContext (org.forgerock.http.session.SessionContext)2 AuditRequestContext (org.forgerock.openam.audit.context.AuditRequestContext)2