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\"}");
}
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"));
}
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);
}
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);
}
}
}
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());
}
Aggregations