Search in sources :

Example 11 with Response

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

the class AuthenticationServiceV1 method createResponse.

/**
     * Creates a response from the given JsonValue.
     *
     * @param jsonResponse The Json response body.
     * @return a response.
     * @throws IOException If there is a problem creating the response.
     */
private Response createResponse(final JsonValue jsonResponse) throws IOException {
    Response response = new Response(Status.OK);
    response.getHeaders().put(CACHE_CONTROL_HEADER_NAME, NO_CACHE_CACHE_CONTROL_HEADER);
    response.getHeaders().put(PRAGMA_HEADER_NAME, PRAGMA_NO_CACHE_HEADER);
    response.getHeaders().put(EXPIRES_HEADER_NAME, ALWAYS_EXPIRE_HEADER);
    response.getHeaders().put(CONTENT_TYPE_HEADER_NAME, "application/json");
    response.setEntity(jsonResponse.getObject());
    return response;
}
Also used : Response(org.forgerock.http.protocol.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 12 with Response

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

the class AuthenticationServiceV1Test method shouldReturnUnauthorizedCodeWithJsonErrorMessage.

@Test
public void shouldReturnUnauthorizedCodeWithJsonErrorMessage() throws IOException {
    // given
    Request httpRequest = new Request();
    RestAuthException exception = new RestAuthException(401, "Invalid Password!!");
    exception.setFailureUrl("http://localhost:8080");
    // when
    Response response = authServiceV1.handleErrorResponse(httpRequest, Status.valueOf(401), exception);
    // then
    assertThat(response.getStatus()).isEqualTo(Status.UNAUTHORIZED);
    JsonValue responseBody = json(response.getEntity().getJson());
    assertThat(responseBody).stringAt("errorMessage").isEqualTo("Invalid Password!!");
    assertThat(responseBody).stringAt("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)

Example 13 with Response

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

the class RealmContextFilterTest method filterShouldConsumeRealmFromRequestWithInvalidDnsAlias.

@Test
public void filterShouldConsumeRealmFromRequestWithInvalidDnsAlias() throws Exception {
    //Given
    Context context = mockContext(ENDPOINT_PATH_ELEMENT);
    Request request = createRequest(INVALID_DNS_ALIAS_HOSTNAME, ENDPOINT_PATH_ELEMENT);
    mockInvalidDnsAlias(INVALID_DNS_ALIAS_HOSTNAME);
    //When
    Response response = filter.filter(context, request, handler).getOrThrowUninterruptibly();
    //Then
    assertThat(response.getStatus()).isSameAs(Status.BAD_REQUEST);
}
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) Response(org.forgerock.http.protocol.Response) ActionResponse(org.forgerock.json.resource.ActionResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) 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) Test(org.testng.annotations.Test)

Example 14 with Response

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

the class RealmContextFilterTest method filterShouldConsumeRealmFromRequestWithInvalidOverrideRealm.

@Test
public void filterShouldConsumeRealmFromRequestWithInvalidOverrideRealm() throws Exception {
    //Given
    Context context = mockContext(ENDPOINT_PATH_ELEMENT);
    Request request = createRequest(HOSTNAME, ENDPOINT_PATH_ELEMENT + "?realm=" + INVALID_OVERRIDE_REALM);
    mockDnsAlias(HOSTNAME, "/");
    mockInvalidRealmAlias(INVALID_OVERRIDE_REALM);
    //When
    Response response = filter.filter(context, request, handler).getOrThrowUninterruptibly();
    //Then
    verifyInvalidRealmResponse(response, INVALID_OVERRIDE_REALM);
}
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) Response(org.forgerock.http.protocol.Response) ActionResponse(org.forgerock.json.resource.ActionResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) 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) Test(org.testng.annotations.Test)

Example 15 with Response

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

the class RealmContextFilterTest method filterShouldFailToConsumeRealmFromRequestOnExceptionWhenResolvingServerName.

@Test
public void filterShouldFailToConsumeRealmFromRequestOnExceptionWhenResolvingServerName() throws Exception {
    //Given
    Context context = mockContext(ENDPOINT_PATH_ELEMENT);
    Request request = createRequest(HOSTNAME, ENDPOINT_PATH_ELEMENT);
    IdRepoException exception = mock(IdRepoException.class);
    given(exception.getMessage()).willReturn("EXCEPTION_MESSAGE");
    doThrow(exception).when(coreWrapper).getOrganization(any(SSOToken.class), eq(HOSTNAME));
    //When
    Response response = filter.filter(context, request, handler).getOrThrowUninterruptibly();
    //Then
    assertThat(response.getStatus()).isSameAs(Status.BAD_REQUEST);
    assertThat(response.getEntity().getJson()).isEqualTo(new BadRequestException("FQDN \"HOSTNAME\" is not valid.").toJsonValue().getObject());
}
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) Response(org.forgerock.http.protocol.Response) ActionResponse(org.forgerock.json.resource.ActionResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) SSOToken(com.iplanet.sso.SSOToken) 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) IdRepoException(com.sun.identity.idm.IdRepoException) BadRequestException(org.forgerock.json.resource.BadRequestException) Test(org.testng.annotations.Test)

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