Search in sources :

Example 6 with FilterChain

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

the class TestServerTimeoutAsyncEvent method setup.

@BeforeClass
public void setup() throws IOException {
    _clientFactory = new HttpClientFactory();
    Map<String, Object> clientProperties = new HashMap<String, Object>();
    clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(ASYNC_EVENT_TIMEOUT * 20));
    clientProperties.put(HttpClientFactory.HTTP_POOL_MIN_SIZE, "1");
    clientProperties.put(HttpClientFactory.HTTP_POOL_SIZE, "1");
    _client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), true);
    final Map<URI, StreamRequestHandler> handlers = new HashMap<URI, StreamRequestHandler>();
    handlers.put(TIMEOUT_BEFORE_SENDING_RESPONSE_SERVER_URI, new TimeoutBeforeRespondingRequestHandler());
    handlers.put(TIMEOUT_AFTER_SENDING_RESPONSE_SERVER_URI, new TimeoutAfterRespondingRequestHandler());
    handlers.put(THROW_BUT_SHOULD_NOT_TIMEOUT_URI, new ThrowHandler());
    handlers.put(BUGGY_FILTER_URI, new NormalHandler());
    TransportDispatcher transportDispatcher = new TransportDispatcher() {

        @Override
        public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
            throw new UnsupportedOperationException("This dispatcher only supports stream");
        }

        @Override
        public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
            StreamRequestHandler handler = handlers.get(req.getURI());
            if (handler != null) {
                handler.handleRequest(req, requestContext, new TransportCallbackAdapter<StreamResponse>(callback));
            } else {
                req.getEntityStream().setReader(new DrainReader());
                callback.onResponse(TransportResponseImpl.<StreamResponse>error(new IllegalStateException("Handler not found for URI " + req.getURI())));
            }
        }
    };
    FilterChain filterChain = FilterChains.createStreamChain(new BuggyFilter());
    _server = new HttpServerFactory(filterChain, HttpJettyServer.ServletType.ASYNC_EVENT).createServer(PORT, transportDispatcher, ASYNC_EVENT_TIMEOUT, true);
    _server.start();
    _asyncExecutor = Executors.newSingleThreadExecutor();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) HashMap(java.util.HashMap) FilterChain(com.linkedin.r2.filter.FilterChain) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RequestContext(com.linkedin.r2.message.RequestContext) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) RestRequest(com.linkedin.r2.message.rest.RestRequest) Map(java.util.Map) HashMap(java.util.HashMap) BeforeClass(org.testng.annotations.BeforeClass)

Example 7 with FilterChain

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

the class FilterUtil method fireStreamRequestResponse.

// Fires a request, saving the local attributes, and then fires a response with the local
// attributes.
public static void fireStreamRequestResponse(FilterChain fc, StreamRequest req, StreamResponse res) {
    final RequestContext context = new RequestContext();
    wrapFilterChain(fc).onStreamRequest(req, context, emptyWireAttrs());
    wrapFilterChain(fc).onStreamResponse(res, context, emptyWireAttrs());
}
Also used : RequestContext(com.linkedin.r2.message.RequestContext)

Example 8 with FilterChain

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

the class Bootstrap method createHttpClient.

public static Client createHttpClient(FilterChain filters, boolean restOverStream) {
    HashMap<String, String> properties = new HashMap<>();
    properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_1_1.name());
    final TransportClient client = new HttpClientFactory.Builder().setFilterChain(filters).build().getClient(properties);
    return new TransportClientAdapter(client, restOverStream);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory)

Example 9 with FilterChain

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

the class RestLiIntegrationTest method init.

public void init(List<? extends Filter> filters) throws IOException {
    final FilterChain fc = FilterChains.empty().addLastRest(new ServerCompressionFilter(RestLiIntTestServer.supportedCompression, new CompressionConfig(0))).addLastRest(new SimpleLoggingFilter());
    init(filters, fc, false);
}
Also used : ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterChain(com.linkedin.r2.filter.FilterChain) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) CompressionConfig(com.linkedin.r2.filter.CompressionConfig)

Example 10 with FilterChain

use of com.linkedin.r2.filter.FilterChain 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

FilterChain (com.linkedin.r2.filter.FilterChain)45 Test (org.testng.annotations.Test)38 RequestContext (com.linkedin.r2.message.RequestContext)28 RestRequest (com.linkedin.r2.message.rest.RestRequest)20 RestResponse (com.linkedin.r2.message.rest.RestResponse)17 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)15 HashMap (java.util.HashMap)12 ByteString (com.linkedin.data.ByteString)11 Map (java.util.Map)11 RestException (com.linkedin.r2.message.rest.RestException)10 RestCountFilter (com.linkedin.r2.testutils.filter.RestCountFilter)10 StreamCountFilter (com.linkedin.r2.testutils.filter.StreamCountFilter)10 NextFilter (com.linkedin.r2.filter.NextFilter)9 Callback (com.linkedin.common.callback.Callback)8 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)8 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)8 CaptureLastCallFilter (com.linkedin.r2.testutils.filter.CaptureLastCallFilter)7 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)5