Search in sources :

Example 16 with RestRequest

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);
        }
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Map(java.util.Map) URI(java.net.URI)

Example 17 with RestRequest

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);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest)

Example 18 with RestRequest

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);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) MessagingException(javax.mail.MessagingException) RestResponse(com.linkedin.r2.message.rest.RestResponse) URISyntaxException(java.net.URISyntaxException) URISyntaxException(java.net.URISyntaxException) MessagingException(javax.mail.MessagingException)

Example 19 with RestRequest

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;
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 20 with RestRequest

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);
}
Also used : Path(java.nio.file.Path) StreamCountFilter(com.linkedin.r2.testutils.filter.StreamCountFilter) RestRequest(com.linkedin.r2.message.rest.RestRequest) CaptureLastCallFilter(com.linkedin.r2.testutils.filter.CaptureLastCallFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) FilterChain(com.linkedin.r2.filter.FilterChain) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Aggregations

RestRequest (com.linkedin.r2.message.rest.RestRequest)293 Test (org.testng.annotations.Test)243 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)174 RequestContext (com.linkedin.r2.message.RequestContext)154 RestResponse (com.linkedin.r2.message.rest.RestResponse)147 URI (java.net.URI)124 ByteString (com.linkedin.data.ByteString)65 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)62 FutureCallback (com.linkedin.common.callback.FutureCallback)50 RestException (com.linkedin.r2.message.rest.RestException)49 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)44 ExecutionException (java.util.concurrent.ExecutionException)43 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)38 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)36 HashMap (java.util.HashMap)35 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)33 Map (java.util.Map)28 Callback (com.linkedin.common.callback.Callback)27 URISyntaxException (java.net.URISyntaxException)25 ResourceContext (com.linkedin.restli.server.ResourceContext)24