use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class ExampleD2Client method sendTraffic.
private static void sendTraffic(Map<String, Long> trafficProportion, D2Client d2Client) throws URISyntaxException {
for (Map.Entry<String, Long> entry : trafficProportion.entrySet()) {
URI uri = new URI("d2://" + entry.getKey());
RestRequest request = new RestRequestBuilder(uri).setMethod("get").build();
for (long i = 0; i < entry.getValue(); i++) {
//we don't care about the result from the server after all,
//you can see the traffic hits the echo server from stdout
d2Client.restRequest(request);
}
}
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class ClientQueryTunnelFilter method onRestRequest.
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
final RestRequest newReq;
try {
newReq = QueryTunnelUtil.encode(req, requestContext, _queryPostThreshold);
} catch (Exception e) {
nextFilter.onError(e, requestContext, wireAttrs);
return;
}
nextFilter.onRequest(newReq, requestContext, wireAttrs);
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class ServerQueryTunnelFilter method onRestRequest.
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
final RestRequest newReq;
try {
newReq = QueryTunnelUtil.decode(req, requestContext);
} catch (MessagingException ex) {
RestResponse errorResponse = RestStatus.responseForStatus(RestStatus.BAD_REQUEST, ex.toString());
nextFilter.onResponse(errorResponse, requestContext, wireAttrs);
return;
} catch (URISyntaxException ex) {
RestResponse errorResponse = RestStatus.responseForStatus(RestStatus.BAD_REQUEST, ex.toString());
nextFilter.onResponse(errorResponse, requestContext, wireAttrs);
return;
} catch (Exception ex) {
nextFilter.onError(ex, requestContext, wireAttrs);
return;
}
nextFilter.onRequest(newReq, requestContext, wireAttrs);
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class DirectoryDbSource method loadDb.
private Map<RestRequest, RestResponse> loadDb(File dir, MessageSerializer serializer) throws IOException {
final Map<RestRequest, RestResponse> db = new HashMap<RestRequest, RestResponse>();
final String[] ids = DirectoryDbUtil.listRequestIds(dir);
Arrays.sort(ids);
for (String id : ids) {
InputStream reqIn = null;
InputStream resIn = null;
try {
reqIn = new FileInputStream(DirectoryDbUtil.requestFileName(dir, id));
final RestRequest req = serializer.readRestRequest(reqIn);
resIn = new FileInputStream(DirectoryDbUtil.responseFileName(dir, id));
final RestResponse res = serializer.readRestResponse(resIn);
db.put(canonicalize(req), res);
} catch (IOException e) {
_log.warn("Failed to parse request or response for: " + id, e);
} finally {
closeSilently(reqIn);
closeSilently(resIn);
}
}
return db;
}
use of com.linkedin.r2.message.rest.RestRequest 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);
}
Aggregations