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();
}
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());
}
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);
}
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);
}
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);
}
Aggregations