Search in sources :

Example 1 with NextFilter

use of com.linkedin.r2.filter.NextFilter 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 2 with NextFilter

use of com.linkedin.r2.filter.NextFilter 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 3 with NextFilter

use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.

the class ServerQueryTunnelFilter method onStreamRequest.

@Override
public void onStreamRequest(final StreamRequest req, final RequestContext requestContext, final Map<String, String> wireAttrs, final NextFilter<StreamRequest, StreamResponse> nextFilter) {
    Callback<StreamRequest> callback = new Callback<StreamRequest>() {

        @Override
        public void onError(Throwable e) {
            if (e instanceof MessagingException || e instanceof URISyntaxException) {
                RestResponse errorResponse = RestStatus.responseForStatus(RestStatus.BAD_REQUEST, e.toString());
                nextFilter.onResponse(Messages.toStreamResponse(errorResponse), requestContext, wireAttrs);
            } else {
                nextFilter.onError(e, requestContext, wireAttrs);
            }
        }

        @Override
        public void onSuccess(StreamRequest newReq) {
            nextFilter.onRequest(newReq, requestContext, wireAttrs);
        }
    };
    // the entire request would be buffered in memory if decoding is needed
    // this usually is not a problem as request with extremely query parameters is usually get request with no body
    QueryTunnelUtil.decode(req, requestContext, callback);
}
Also used : Callback(com.linkedin.common.callback.Callback) MessagingException(javax.mail.MessagingException) RestResponse(com.linkedin.r2.message.rest.RestResponse) URISyntaxException(java.net.URISyntaxException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 4 with NextFilter

use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.

the class LoggingFilter method onStreamResponse.

@Override
public void onStreamResponse(StreamResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
    EntityStream entityStream = res.getEntityStream();
    entityStream.addObserver(new Observer() {

        private long startTime;

        private long bytesNum = 0;

        @Override
        public void onDataAvailable(ByteString data) {
            if (bytesNum == 0) {
                startTime = System.nanoTime();
            }
            bytesNum += data.length();
        }

        @Override
        public void onDone() {
            long stopTime = System.nanoTime();
            _log.info("Status: success. Total bytes streamed: " + bytesNum + ". Total stream time: " + (stopTime - startTime) + " nano seconds.");
        }

        @Override
        public void onError(Throwable e) {
            long stopTime = System.nanoTime();
            _log.error("Status: failed. Total bytes streamed: " + bytesNum + ". Total stream time before failure: " + (stopTime - startTime) + " nano seconds.");
        }
    });
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) ByteString(com.linkedin.data.ByteString) Observer(com.linkedin.r2.message.stream.entitystream.Observer)

Example 5 with NextFilter

use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.

the class TestRequestCompression method initClass.

@BeforeClass
public void initClass() throws Exception {
    class CheckRequestCompressionFilter implements RestFilter {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Map<String, String> requestHeaders = req.getHeaders();
            if (requestHeaders.containsKey(TEST_HELP_HEADER)) {
                String contentEncodingHeader = requestHeaders.get(HttpConstants.CONTENT_ENCODING);
                if (requestHeaders.get(TEST_HELP_HEADER).equals(EXPECT_COMPRESSION)) {
                    if (contentEncodingHeader == null) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is not compressed when it should be.");
                    } else if (!contentEncodingHeader.equals("x-snappy-framed")) {
                        // which is always snappy in this test.
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is compressed with " + contentEncodingHeader + " instead of x-snappy-framed.");
                    }
                } else {
                    if (contentEncodingHeader != null) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is compressed when it shouldn't be.");
                    }
                }
            }
            nextFilter.onRequest(req, requestContext, wireAttrs);
        }
    }
    // Check that Content-Encoding and Content-Length headers are set correctly by ServerCompressionFilter.
    class CheckHeadersFilter implements RestFilter {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            if (req.getHeaders().containsKey(HttpConstants.CONTENT_ENCODING)) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Content-Encoding header not removed.");
            }
            if (req.getEntity().length() != Integer.parseInt(req.getHeader(HttpConstants.CONTENT_LENGTH))) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Content-Length header incorrect.");
            }
            nextFilter.onRequest(req, requestContext, wireAttrs);
        }
    }
    final FilterChain fc = FilterChains.empty().addLastRest(new CheckRequestCompressionFilter()).addLastRest(new ServerCompressionFilter(RestLiIntTestServer.supportedCompression)).addLastRest(new CheckHeadersFilter()).addLastRest(new SimpleLoggingFilter());
    super.init(null, fc, false);
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) NextFilter(com.linkedin.r2.filter.NextFilter) ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterChain(com.linkedin.r2.filter.FilterChain) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

RequestContext (com.linkedin.r2.message.RequestContext)23 Test (org.testng.annotations.Test)22 Map (java.util.Map)18 NextFilter (com.linkedin.r2.filter.NextFilter)17 FilterChain (com.linkedin.r2.filter.FilterChain)15 RestResponse (com.linkedin.r2.message.rest.RestResponse)15 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)14 RestRequest (com.linkedin.r2.message.rest.RestRequest)14 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)13 Callback (com.linkedin.common.callback.Callback)10 HashMap (java.util.HashMap)10 ByteString (com.linkedin.data.ByteString)9 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)8 URI (java.net.URI)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 StreamException (com.linkedin.r2.message.stream.StreamException)7 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)7 RestException (com.linkedin.r2.message.rest.RestException)6