use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.
the class TestRestLiResponseHandler method invokeResponseHandler.
private RestResponse invokeResponseHandler(String uri, ResourceMethodDescriptor methodDescriptor, Object result, Map<String, String> headers, ProtocolVersion protocolVersion) throws IOException, URISyntaxException, RestLiSyntaxException {
RestRequest request = buildRequest(uri, headers, protocolVersion);
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
RestUtils.validateRequestHeadersAndUpdateResourceContext(headers, context);
RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
RestResponse response;
response = _responseHandler.buildResponse(request, routingResult, result);
return response;
}
use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.
the class TestCapRepFilter method testStreamResponse.
@Test
public void testStreamResponse() throws IOException {
Path dirPath = Files.createTempDirectory("caprep-test");
StreamCountFilter after = new StreamCountFilter();
StreamCountFilter before = new StreamCountFilter();
CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
FilterChain fc = FilterChains.createStreamChain(StreamFilterAdapters.adaptRestFilter(lastCallFilter), before, _filter, after);
RestRequest myRequest = new RestRequestBuilder(URI.create("/req1")).setEntity("123".getBytes()).build();
RestResponse myResponse = new RestResponseBuilder().setStatus(201).setEntity("321".getBytes()).build();
_filter.capture(dirPath.toString());
RequestContext requestContext = new RequestContext();
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), requestContext, FilterUtil.emptyWireAttrs());
FilterUtil.fireStreamResponse(fc, Messages.toStreamResponse(myResponse), requestContext, FilterUtil.emptyWireAttrs());
Assert.assertEquals(after.getStreamReqCount(), 1);
Assert.assertEquals(after.getStreamResCount(), 1);
Assert.assertEquals(before.getStreamReqCount(), 1);
Assert.assertEquals(before.getStreamResCount(), 1);
lastCallFilter.reset();
_filter.passThrough();
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest));
Assert.assertEquals(after.getStreamReqCount(), 2);
Assert.assertEquals(after.getStreamResCount(), 1);
Assert.assertEquals(before.getStreamReqCount(), 2);
Assert.assertEquals(before.getStreamResCount(), 1);
Assert.assertNull(lastCallFilter.getLastRes());
_filter.replay(dirPath.toString());
FilterUtil.fireSimpleStreamRequest(fc);
Assert.assertNull(lastCallFilter.getLastRes());
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), new RequestContext(), FilterUtil.emptyWireAttrs());
Assert.assertEquals(lastCallFilter.getLastRes(), myResponse);
Assert.assertEquals(after.getStreamReqCount(), 3);
Assert.assertEquals(after.getStreamResCount(), 1);
Assert.assertEquals(before.getStreamReqCount(), 4);
Assert.assertEquals(before.getStreamResCount(), 2);
}
use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.
the class TestCapRepFilter method testStreamException.
@Test
public void testStreamException() throws IOException {
Path dirPath = Files.createTempDirectory("caprep-test");
CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
FilterChain fc = FilterChains.createStreamChain(StreamFilterAdapters.adaptRestFilter(lastCallFilter), _filter);
RestRequest myRequest = new RestRequestBuilder(URI.create("/req1")).setEntity("123".getBytes()).build();
RestResponse myErrorResponse = new RestResponseBuilder().setStatus(400).setEntity("321".getBytes()).build();
RestException myRestException = new RestException(myErrorResponse);
_filter.capture(dirPath.toString());
RequestContext requestContext = new RequestContext();
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), requestContext, FilterUtil.emptyWireAttrs());
FilterUtil.fireStreamError(fc, Messages.toStreamException(myRestException), requestContext, FilterUtil.emptyWireAttrs());
lastCallFilter.reset();
_filter.replay(dirPath.toString());
FilterUtil.fireSimpleStreamRequest(fc);
Assert.assertNull(lastCallFilter.getLastErr());
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest));
Assert.assertTrue(lastCallFilter.getLastErr() instanceof RestException);
Assert.assertEquals(((RestException) lastCallFilter.getLastErr()).getResponse(), myErrorResponse);
}
use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.
the class TestReplaceableFilter method testStreamingPassThrough.
@Test
public void testStreamingPassThrough() {
ReplaceableFilter filter = getFilter();
CountRequestChunksFilter countRequestChunksFilter = new CountRequestChunksFilter();
CountResponseChunksFilter countResponseChunksFilter = new CountResponseChunksFilter();
FilterChain filterChain = FilterChains.empty().addLast(countResponseChunksFilter).addLast(filter).addLast(countRequestChunksFilter);
int requestChunks = 100;
int responseChunks = 200;
StreamRequest streamRequest = new StreamRequestBuilder(URI.create("/test")).build(EntityStreams.newEntityStream(new Writer() {
private WriteHandle _wh;
int _count = 0;
@Override
public void onInit(WriteHandle wh) {
_wh = wh;
}
@Override
public void onWritePossible() {
while (_wh.remaining() > 0) {
if (_count < requestChunks) {
_wh.write(ByteString.copy(new byte[10]));
_count++;
} else {
_wh.done();
}
}
}
@Override
public void onAbort(Throwable e) {
}
}));
StreamResponse streamResponse = new StreamResponseBuilder().build(EntityStreams.newEntityStream(new Writer() {
private WriteHandle _wh;
int _count = 0;
@Override
public void onInit(WriteHandle wh) {
_wh = wh;
}
@Override
public void onWritePossible() {
while (_wh.remaining() > 0) {
if (_count < responseChunks) {
_wh.write(ByteString.copy(new byte[10]));
_count++;
} else {
_wh.done();
}
}
}
@Override
public void onAbort(Throwable e) {
}
}));
FilterUtil.fireStreamRequest(filterChain, streamRequest);
FilterUtil.fireStreamResponse(filterChain, streamResponse, new RequestContext(), new HashMap<>());
Assert.assertEquals(countRequestChunksFilter.getRequestChunks(), requestChunks);
Assert.assertEquals(countResponseChunksFilter.getResponseChunks(), responseChunks);
}
use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.
the class AbstractClient method restRequest.
@Override
public void restRequest(RestRequest request, RequestContext requestContext, Callback<RestResponse> callback) {
StreamRequest streamRequest = Messages.toStreamRequest(request);
// IS_FULL_REQUEST flag, if set true, would result in the request being sent without using chunked transfer encoding
// This is needed as the legacy R2 server (before 2.8.0) does not support chunked transfer encoding.
requestContext.putLocalAttr(R2Constants.IS_FULL_REQUEST, true);
// here we add back the content-length header for the response because some client code depends on this header
streamRequest(streamRequest, requestContext, Messages.toStreamCallback(callback, true));
}
Aggregations