use of com.linkedin.r2.message.rest.RestResponseBuilder 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.rest.RestResponseBuilder in project rest.li by linkedin.
the class RestEchoServer method handleRequest.
public void handleRequest(RestRequest request, RequestContext requestContext, Callback<RestResponse> callback) {
final String msg;
try {
msg = IOUtil.toString(request.getEntity().asInputStream(), CHARSET.name());
} catch (IOException ex) {
callback.onError(ex);
return;
}
_echoService.echo(msg, new CallbackAdapter<RestResponse, String>(callback) {
@Override
protected RestResponse convertResponse(String responseMsg) {
return new RestResponseBuilder().setEntity(ByteString.copyString(responseMsg, CHARSET)).setHeader("Content-Type", "text/plain").build();
}
});
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class RestLiResponseHandler method buildRestException.
public RestException buildRestException(final Throwable e, PartialRestResponse partialResponse) {
List<String> cookies = CookieUtil.encodeSetCookies(partialResponse.getCookies());
RestResponseBuilder builder = new RestResponseBuilder().setHeaders(partialResponse.getHeaders()).setCookies(cookies).setStatus(partialResponse.getStatus().getCode());
if (partialResponse.hasData()) {
DataMap dataMap = partialResponse.getDataMap();
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
// partialResponse.getSchema()
DataMapUtils.write(dataMap, null, baos, true);
builder.setEntity(baos.toByteArray());
}
RestResponse restResponse = builder.build();
RestException restException = new RestException(restResponse, e);
return restException;
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class RetryTrackerClient method restRequest.
@Override
public void restRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<RestResponse> callback) {
TransportResponse<RestResponse> response;
if (_uri.toString().startsWith("http://test.linkedin.com/retry")) {
RetriableRequestException ex = new RetriableRequestException("Data not available");
response = TransportResponseImpl.error(ex);
} else if (_uri.toString().equals("http://test.linkedin.com/bad")) {
response = TransportResponseImpl.error(RestException.forError(404, "exception happens"), wireAttrs);
} else {
response = TransportResponseImpl.success(new RestResponseBuilder().build(), wireAttrs);
}
callback.onResponse(response);
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class Messages method toRestResponse.
/**
* Converts a StreamResponse to RestResponse
* @param streamResponse the stream request to be converted
* @param callback the callback to be invoked when the rest response is constructed
* @param addContentLengthHeader whether the rest response should have content-length header
*/
public static void toRestResponse(StreamResponse streamResponse, final Callback<RestResponse> callback, final boolean addContentLengthHeader) {
final RestResponseBuilder builder = new RestResponseBuilder(streamResponse);
Callback<ByteString> assemblyCallback = new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
callback.onError(e);
}
@Override
public void onSuccess(ByteString result) {
if (addContentLengthHeader) {
builder.setHeader(HttpConstants.CONTENT_LENGTH, String.valueOf(result.length()));
}
RestResponse restResponse = builder.setEntity(result).build();
callback.onSuccess(restResponse);
}
};
streamResponse.getEntityStream().setReader(new FullEntityReader(assemblyCallback));
}
Aggregations