Search in sources :

Example 16 with Request

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

the class PendingRequestEmailTemplate method resolveScope.

private String resolveScope(String scope, Locale locale) {
    if (URI.create(scope).getScheme() != null) {
        Request request = new Request().setMethod("GET").setUri(URI.create(scope));
        request.getHeaders().put("Accept-Language", locale.toLanguageTag());
        Response response = client.send(request).getOrThrowUninterruptibly();
        if (Status.OK.equals(response.getStatus())) {
            try {
                JsonValue json = json(response.getEntity().getJson());
                if (json.isDefined("name") && json.get("name").isString()) {
                    return json.get("name").asString();
                }
            } catch (IOException e) {
                debug.warning("Failed to parse Scope description JSON", e);
            }
        }
    }
    return scope;
}
Also used : Response(org.forgerock.http.protocol.Response) Request(org.forgerock.http.protocol.Request) JsonValue(org.forgerock.json.JsonValue) IOException(java.io.IOException)

Example 17 with Request

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

the class HandlerProviderTest method shouldNotCallInjectorHolderTwice.

@Test
public void shouldNotCallInjectorHolderTwice() {
    //Given
    Context context = new RootContext();
    Request request = new Request();
    //When
    handlerProvider.handle(context, request);
    handlerProvider.handle(context, request);
    //Then
    assertThat(handlerReturnCount.get()).isEqualTo(1);
    verify(handler, times(2)).handle(context, request);
}
Also used : Context(org.forgerock.services.context.Context) RootContext(org.forgerock.services.context.RootContext) RootContext(org.forgerock.services.context.RootContext) Request(org.forgerock.http.protocol.Request) Test(org.testng.annotations.Test)

Example 18 with Request

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

the class HandlerProviderTest method shouldCallInjectorHolderOnFirstCallToHandler.

@Test
public void shouldCallInjectorHolderOnFirstCallToHandler() {
    //Given
    Context context = new RootContext();
    Request request = new Request();
    //When
    handlerProvider.handle(context, request);
    //Then
    assertThat(handlerReturnCount.get()).isEqualTo(1);
    verify(handler).handle(context, request);
}
Also used : Context(org.forgerock.services.context.Context) RootContext(org.forgerock.services.context.RootContext) RootContext(org.forgerock.services.context.RootContext) Request(org.forgerock.http.protocol.Request) Test(org.testng.annotations.Test)

Example 19 with Request

use of org.forgerock.http.protocol.Request 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 20 with Request

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

the class AuthenticationServiceV2Test method shouldReturnResponseContainingUnauthorizedCodeWithJsonErrorMessage.

@Test
public void shouldReturnResponseContainingUnauthorizedCodeWithJsonErrorMessage() throws IOException {
    // given
    Request httpRequest = new Request();
    RestAuthException testException = new RestAuthException(401, "Invalid Password!!");
    testException.setFailureUrl("http://localhost:8080");
    // when
    Response response = authServiceV2.handleErrorResponse(httpRequest, Status.valueOf(401), testException);
    // then
    assertThat(response.getStatus()).isEqualToComparingFieldByField(Status.UNAUTHORIZED);
    JsonValue responseBody = json(response.getEntity().getJson());
    assertThat(responseBody).integerAt("code").isEqualTo(401);
    assertThat(responseBody).stringAt("reason").isEqualTo("Unauthorized");
    assertThat(responseBody).stringAt("message").isEqualTo("Invalid Password!!");
    assertThat(responseBody).stringAt("detail/failureUrl").isEqualTo("http://localhost:8080");
}
Also used : RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) Response(org.forgerock.http.protocol.Response) Request(org.forgerock.http.protocol.Request) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Aggregations

Request (org.forgerock.http.protocol.Request)51 Test (org.testng.annotations.Test)45 Context (org.forgerock.services.context.Context)36 RootContext (org.forgerock.services.context.RootContext)33 AttributesContext (org.forgerock.services.context.AttributesContext)30 ReadRequest (org.forgerock.json.resource.ReadRequest)29 ActionRequest (org.forgerock.json.resource.ActionRequest)21 CreateRequest (org.forgerock.json.resource.CreateRequest)21 DeleteRequest (org.forgerock.json.resource.DeleteRequest)21 PatchRequest (org.forgerock.json.resource.PatchRequest)21 QueryRequest (org.forgerock.json.resource.QueryRequest)21 UpdateRequest (org.forgerock.json.resource.UpdateRequest)21 UriRouterContext (org.forgerock.http.routing.UriRouterContext)19 Response (org.forgerock.http.protocol.Response)14 SessionContext (org.forgerock.http.session.SessionContext)11 Handler (org.forgerock.http.Handler)10 RequestAuditContext (org.forgerock.services.context.RequestAuditContext)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 HttpContext (org.forgerock.json.resource.http.HttpContext)7 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)7