Search in sources :

Example 46 with Response

use of org.restlet.Response in project OpenAM by OpenRock.

the class TokenEndpointResourceTest method shouldThrowServerErrorForExceptionsThatAreNotOAuth2RestletExceptions.

@Test
public void shouldThrowServerErrorForExceptionsThatAreNotOAuth2RestletExceptions() {
    //Given
    Context context = new Context();
    Request request = new Request();
    Response response = new Response(request);
    tokenEndpointResource.init(context, request, response);
    //When
    tokenEndpointResource.doCatch(new NullPointerException());
    //Then
    assertEquals(response.getStatus(), Status.CLIENT_ERROR_BAD_REQUEST);
    assertEquals(response.getEntityAsText(), "{\"error\":\"server_error\"}");
}
Also used : Context(org.restlet.Context) Response(org.restlet.Response) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Test(org.testng.annotations.Test)

Example 47 with Response

use of org.restlet.Response in project OpenAM by OpenRock.

the class ResourceSetRegistrationExceptionFilterTest method shouldSet412ExceptionResponse.

@Test
@SuppressWarnings("unchecked")
public void shouldSet412ExceptionResponse() throws Exception {
    //Given
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    Status status = new Status(412);
    given(response.getStatus()).willReturn(status);
    //When
    exceptionFilter.afterHandle(request, response);
    //Then
    ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class);
    verify(response).setEntity(exceptionResponseCaptor.capture());
    Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject();
    assertThat(responseBody).containsOnly(entry("error", "precondition_failed"));
}
Also used : Response(org.restlet.Response) Status(org.restlet.data.Status) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Request(org.restlet.Request) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 48 with Response

use of org.restlet.Response in project OpenAM by OpenRock.

the class ResourceSetRegistrationExceptionFilterTest method shouldSetAnyOtherExceptionResponse.

@Test
@SuppressWarnings("unchecked")
public void shouldSetAnyOtherExceptionResponse() throws Exception {
    //Given
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    Exception exception = new Exception("MESSAGE");
    Status status = new Status(444, exception);
    given(response.getStatus()).willReturn(status);
    //When
    exceptionFilter.afterHandle(request, response);
    //Then
    ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class);
    verify(response).setEntity(exceptionResponseCaptor.capture());
    Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject();
    assertThat(responseBody).containsOnly(entry("error", "server_error"), entry("error_description", "MESSAGE"));
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(response).setStatus(statusCaptor.capture());
    assertThat(statusCaptor.getValue().getCode()).isEqualTo(500);
    assertThat(statusCaptor.getValue().getThrowable()).isEqualTo(exception);
}
Also used : Response(org.restlet.Response) Status(org.restlet.data.Status) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Request(org.restlet.Request) Map(java.util.Map) BadRequestException(org.forgerock.oauth2.core.exceptions.BadRequestException) Test(org.testng.annotations.Test)

Example 49 with Response

use of org.restlet.Response in project camel by apache.

the class RestletProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    RestletEndpoint endpoint = (RestletEndpoint) getEndpoint();
    final RestletBinding binding = endpoint.getRestletBinding();
    Request request;
    String resourceUri = buildUri(endpoint, exchange);
    URI uri = new URI(resourceUri);
    request = new Request(endpoint.getRestletMethod(), resourceUri);
    binding.populateRestletRequestFromExchange(request, exchange);
    loadCookies(exchange, uri, request);
    LOG.debug("Sending request synchronously: {} for exchangeId: {}", request, exchange.getExchangeId());
    Response response = client.handle(request);
    LOG.debug("Received response synchronously: {} for exchangeId: {}", response, exchange.getExchangeId());
    if (response != null) {
        Integer respCode = response.getStatus().getCode();
        storeCookies(exchange, uri, response);
        if (respCode > 207 && throwException) {
            exchange.setException(populateRestletProducerException(exchange, response, respCode));
        } else {
            binding.populateExchangeFromRestletResponse(exchange, response);
        }
    }
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) URI(java.net.URI)

Example 50 with Response

use of org.restlet.Response in project camel by apache.

the class RestletRequestAndResponseAPITest method testRestletProducer2.

@Test
public void testRestletProducer2() throws Exception {
    final Map<String, Object> headers = new HashMap<String, Object>();
    headers.put("id", 123);
    headers.put("beverage.beer", "Carlsberg");
    Exchange out = template.request("direct:start", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeaders(headers);
        }
    });
    assertNotNull(out);
    assertEquals("text/xml", out.getOut().getHeader(Exchange.CONTENT_TYPE));
    assertEquals(200, out.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
    assertEquals("<response>Beer is Good</response>", out.getOut().getBody(String.class));
    // the restlet response should be accessible if needed
    Response response = out.getOut().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
    assertNotNull(response);
    assertEquals(200, response.getStatus().getCode());
}
Also used : Exchange(org.apache.camel.Exchange) Response(org.restlet.Response) Processor(org.apache.camel.Processor) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Response (org.restlet.Response)62 Request (org.restlet.Request)44 Test (org.testng.annotations.Test)26 Status (org.restlet.data.Status)17 ChallengeResponse (org.restlet.data.ChallengeResponse)15 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)12 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)12 Map (java.util.Map)10 Test (org.junit.Test)10 Client (org.restlet.Client)9 Representation (org.restlet.representation.Representation)9 Reference (org.restlet.data.Reference)8 ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)7 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)7 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)7 AccessToken (org.forgerock.oauth2.core.AccessToken)6 ResourceException (org.restlet.resource.ResourceException)6 Resource (org.qi4j.library.rest.common.Resource)5 Link (org.qi4j.library.rest.common.link.Link)5 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)5