Search in sources :

Example 16 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class CrestProtocolEnforcementFilterTest method requestWithIncorrectProtocolMinorVersionShouldReturnBadRequestResponse.

@Test
public void requestWithIncorrectProtocolMinorVersionShouldReturnBadRequestResponse() throws IOException {
    //Given
    Context context = mock(Context.class);
    Request request = new Request();
    Handler next = mock(Handler.class);
    request.getHeaders().put(AcceptApiVersionHeader.valueOf("protocol=1.1"));
    //When
    Response response = filter.filter(context, request, next).getOrThrowUninterruptibly();
    //Then
    assertThat(getUnsupportedMinorVersionExceptionJson(version(1, 1))).isEqualTo(response.getEntity().getJson());
    assertThat(AcceptApiVersionHeader.valueOf(request).getProtocolVersion()).isEqualTo(version(1, 1));
    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 17 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class RestRouterIT method shouldNotBePossibleToReachInternalResourceViaChf.

@Test
public void shouldNotBePossibleToReachInternalResourceViaChf() throws Exception {
    // Given
    Context context = mockContext();
    Request request = newRequest("GET", "/json/internal");
    // When
    Promise<Response, NeverThrowsException> promise = handler.handle(context, request);
    // Then
    Response response = promise.get();
    assertThat(response.getStatus()).isEqualTo(Status.NOT_FOUND);
    verifyZeroInteractions(internalResource);
}
Also used : RootContext(org.forgerock.services.context.RootContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) HttpContext(org.forgerock.json.resource.http.HttpContext) SessionContext(org.forgerock.http.session.SessionContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) AttributesContext(org.forgerock.services.context.AttributesContext) SecurityContext(org.forgerock.services.context.SecurityContext) Context(org.forgerock.services.context.Context) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) Response(org.forgerock.http.protocol.Response) ResourceResponse(org.forgerock.json.resource.ResourceResponse) NeverThrowsException(org.forgerock.util.promise.NeverThrowsException) Request(org.forgerock.http.protocol.Request) ReadRequest(org.forgerock.json.resource.ReadRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.testng.annotations.Test)

Example 18 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class AnnotatedMethod method checkMethod.

static AnnotatedMethod checkMethod(Class<?> annotation, Object requestHandler, Method method) {
    List<ContextParameter> contextParams = new ArrayList<>();
    int requestParam = -1;
    for (int i = 0; i < method.getParameterTypes().length; i++) {
        Class<?> type = method.getParameterTypes()[i];
        for (Annotation paramAnnotation : method.getParameterAnnotations()[i]) {
            if (paramAnnotation instanceof Contextual) {
                if (Context.class.isAssignableFrom(type)) {
                    contextParams.add(new ContextParameter(i, (Class<? extends Context>) type));
                } else if (Request.class.isAssignableFrom(type)) {
                    requestParam = i;
                }
            }
        }
    }
    Function<Object, Promise<Response, NeverThrowsException>, NeverThrowsException> resourceCreator;
    if (Promise.class.equals(method.getReturnType())) {
        resourceCreator = new PromisedResponseCreator();
    } else if (Response.class.equals(method.getReturnType())) {
        resourceCreator = new Function<Object, Promise<Response, NeverThrowsException>, NeverThrowsException>() {

            @Override
            public Promise<Response, NeverThrowsException> apply(Object o) {
                return newResultPromise((Response) o);
            }
        };
    } else {
        resourceCreator = ResponseCreator.forType(method.getReturnType());
    }
    return new AnnotatedMethod(annotation.getSimpleName(), requestHandler, method, contextParams, requestParam, method.getParameterTypes().length, resourceCreator);
}
Also used : Context(org.forgerock.services.context.Context) ArrayList(java.util.ArrayList) Request(org.forgerock.http.protocol.Request) Annotation(java.lang.annotation.Annotation) NeverThrowsException(org.forgerock.util.promise.NeverThrowsException) Response(org.forgerock.http.protocol.Response) Promises.newResultPromise(org.forgerock.util.promise.Promises.newResultPromise) Promise(org.forgerock.util.promise.Promise) Function(org.forgerock.util.Function)

Example 19 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class AuthenticationServiceV1 method handleErrorResponse.

/**
     * Processes the given Exception into a Restlet response representation or wrap it into
     * a ResourceException, which will be thrown.
     *
     * @param status The status to set the response to.
     * @param exception The Exception to be handled.
     * @return The Restlet Response Representation.
     * @throws ResourceException If the given exception is wrapped in a ResourceException.
     */
protected Response handleErrorResponse(Request request, Status status, Exception exception) {
    Reject.ifNull(status);
    Response response = new Response(status);
    final Map<String, Object> rep = new HashMap<>();
    if (exception instanceof RestAuthResponseException) {
        final RestAuthResponseException authResponseException = (RestAuthResponseException) exception;
        for (Map.Entry<String, String> entry : authResponseException.getResponseHeaders().entrySet()) {
            response.getHeaders().put(entry.getKey(), entry.getValue());
        }
        rep.putAll(authResponseException.getJsonResponse().asMap());
    } else if (exception instanceof RestAuthException) {
        final RestAuthException authException = (RestAuthException) exception;
        if (authException.getFailureUrl() != null) {
            rep.put("failureUrl", authException.getFailureUrl());
        }
        rep.put("errorMessage", getLocalizedMessage(request, exception));
    } else if (exception == null) {
        rep.put("errorMessage", status.getReasonPhrase());
    } else {
        rep.put("errorMessage", getLocalizedMessage(request, exception));
    }
    response.setEntity(rep);
    return response;
}
Also used : Response(org.forgerock.http.protocol.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) HashMap(java.util.HashMap) RestAuthResponseException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class AuthenticationServiceV2 method handleErrorResponse.

@Override
protected Response handleErrorResponse(Request request, Status status, Exception exception) {
    Reject.ifNull(status);
    Response response = new Response(status);
    if (exception instanceof RestAuthResponseException) {
        final RestAuthResponseException authResponseException = (RestAuthResponseException) exception;
        for (Map.Entry<String, String> entry : authResponseException.getResponseHeaders().entrySet()) {
            response.getHeaders().add(entry.getKey(), entry.getValue());
        }
        response.setEntity(authResponseException.getJsonResponse().asMap());
        return response;
    } else if (exception instanceof RestAuthException) {
        final RestAuthException rae = (RestAuthException) exception;
        ResourceException cause = ResourceException.getException(rae.getStatusCode(), getLocalizedMessage(request, rae));
        if (rae.getFailureUrl() != null) {
            cause.setDetail(json(object(field("failureUrl", rae.getFailureUrl()))));
        }
        return createExceptionResponse(response, cause);
    } else if (exception == null) {
        return createExceptionResponse(response, ResourceException.getException(status.getCode()));
    } else {
        return createExceptionResponse(response, ResourceException.getException(status.getCode(), exception.getMessage(), exception));
    }
}
Also used : Response(org.forgerock.http.protocol.Response) RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) RestAuthResponseException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException) ResourceException(org.forgerock.json.resource.ResourceException) Map(java.util.Map)

Aggregations

Response (org.forgerock.http.protocol.Response)22 Request (org.forgerock.http.protocol.Request)15 Test (org.testng.annotations.Test)14 Context (org.forgerock.services.context.Context)8 JsonValue (org.forgerock.json.JsonValue)6 AttributesContext (org.forgerock.services.context.AttributesContext)6 RootContext (org.forgerock.services.context.RootContext)6 Handler (org.forgerock.http.Handler)4 ReadRequest (org.forgerock.json.resource.ReadRequest)4 ResourceResponse (org.forgerock.json.resource.ResourceResponse)4 RestAuthException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthException)4 RestAuthResponseException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException)4 NeverThrowsException (org.forgerock.util.promise.NeverThrowsException)4 Map (java.util.Map)3 UriRouterContext (org.forgerock.http.routing.UriRouterContext)3 SessionContext (org.forgerock.http.session.SessionContext)3 ActionRequest (org.forgerock.json.resource.ActionRequest)3 ActionResponse (org.forgerock.json.resource.ActionResponse)3 CreateRequest (org.forgerock.json.resource.CreateRequest)3 DeleteRequest (org.forgerock.json.resource.DeleteRequest)3