use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class MockFailedResponseFutureBuilder method buildWithErrorResponse.
private ResponseFuture<V> buildWithErrorResponse(ProtocolVersion protocolVersion) {
int status = (_errorResponse.hasStatus()) ? _errorResponse.getStatus() : DEFAULT_HTTP_STATUS;
// create a RestLiResponseException and wrap it in an ExecutionException that will be thrown by the ResponseFuture
RestLiResponseException restLiResponseException = new MockRestliResponseExceptionBuilder().setErrorResponse(_errorResponse).setStatus(HttpStatus.fromCode(status)).setCookies(getCookies() == null ? Collections.emptyList() : getCookies()).setHeaders(getHeaders() == null ? new HashMap<>() : getHeaders()).build();
ExecutionException executionException = new ExecutionException(restLiResponseException);
Future<Response<V>> responseFuture = buildFuture(null, executionException);
return new ResponseFutureImpl<V>(responseFuture, _errorHandlingBehavior);
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class MockFailedResponseFutureBuilder method buildWithEntity.
private ResponseFuture<V> buildWithEntity() {
int status = getStatus();
byte[] entity = mapToBytes(getEntity().data());
Response<V> decodedResponse = new MockResponseBuilder<K, V>().setEntity(getEntity()).setStatus(status).setHeaders(getHeaders()).setCookies(getCookies()).setProtocolVersion(getProtocolVersion()).build();
RestResponse restResponse = new RestResponseBuilder().setEntity(entity).setStatus(status).setHeaders(decodedResponse.getHeaders()).setCookies(CookieUtil.encodeCookies(decodedResponse.getCookies())).build();
RestLiResponseException restLiResponseException = new RestLiResponseException(restResponse, decodedResponse, new ErrorResponse());
ExecutionException executionException = new ExecutionException(restLiResponseException);
Future<Response<V>> responseFuture = buildFuture(null, executionException);
return new ResponseFutureImpl<V>(responseFuture, _errorHandlingBehavior);
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestMockFailedResponseFutureBuilder method testBuildWithEntityTreatServerErrorAsSuccess.
@Test
public void testBuildWithEntityTreatServerErrorAsSuccess() {
ResponseFuture<Greeting> future = buildWithEntity(ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS);
try {
Response<Greeting> response = future.getResponse();
Assert.assertEquals(response.getEntity(), new Greeting().setId(1L).setMessage("foo"));
Assert.assertEquals(response.getStatus(), 500);
Assert.assertEquals(response.getError().getStatus(), 500);
} catch (RemoteInvocationException e) {
Assert.fail("No exception should have been thrown!");
}
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class RestEchoClient method echo.
public void echo(String msg, Callback<String> callback) {
final RestRequest req = new RestRequestBuilder(_uri).setEntity(ByteString.copyString(msg, RestEchoServer.CHARSET)).setMethod(RestMethod.POST).build();
_client.restRequest(req, new CallbackAdapter<String, RestResponse>(callback) {
@Override
protected String convertResponse(RestResponse response) throws Exception {
return response.getEntity().asString(RestEchoServer.CHARSET);
}
});
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class FilterUtil method fireRestRequestResponse.
// Fires a request, saving the local attributes, and then fires a response with the local
// attributes.
public static void fireRestRequestResponse(FilterChain fc, RestRequest req, RestResponse res) {
final RequestContext context = new RequestContext();
fc.onRestRequest(req, context, emptyWireAttrs());
fc.onRestResponse(res, context, emptyWireAttrs());
}
Aggregations