Search in sources :

Example 1 with StreamRequestHandler

use of com.linkedin.r2.transport.common.StreamRequestHandler in project rest.li by linkedin.

the class TransportDispatcherImpl method handleStreamRequest.

@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
    final URI address = req.getURI();
    final StreamRequestHandler handler = _streamHandlers.get(address);
    if (handler == null) {
        final RestResponse response = RestStatus.responseForStatus(RestStatus.NOT_FOUND, "No resource for URI: " + address);
        callback.onResponse(TransportResponseImpl.success(Messages.toStreamResponse(response)));
        req.getEntityStream().setReader(new DrainReader());
        return;
    }
    try {
        handler.handleRequest(req, requestContext, new TransportCallbackAdapter<StreamResponse>(callback));
    } catch (Exception e) {
        final Exception ex = RestException.forError(RestStatus.INTERNAL_SERVER_ERROR, e);
        callback.onResponse(TransportResponseImpl.<StreamResponse>error(ex));
    }
}
Also used : StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) URI(java.net.URI) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) RestException(com.linkedin.r2.message.rest.RestException)

Example 2 with StreamRequestHandler

use of com.linkedin.r2.transport.common.StreamRequestHandler in project rest.li by linkedin.

the class TestServerTimeout 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(SERVER_IOHANDLER_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(BUGGY_SERVER_URI, new BuggyRequestHandler());
    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).createRAPServer(PORT, transportDispatcher, SERVER_IOHANDLER_TIMEOUT, true);
    _server.start();
}
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 3 with StreamRequestHandler

use of com.linkedin.r2.transport.common.StreamRequestHandler 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 4 with StreamRequestHandler

use of com.linkedin.r2.transport.common.StreamRequestHandler in project rest.li by linkedin.

the class TestStreamEcho method getHandlers.

protected Map<URI, StreamRequestHandler> getHandlers() {
    Map<URI, StreamRequestHandler> handlers = new HashMap<URI, StreamRequestHandler>();
    handlers.put(ECHO_URI, new SteamEchoHandler());
    handlers.put(ASYNC_ECHO_URI, new SteamAsyncEchoHandler(_scheduler));
    handlers.put(DELAYED_ECHO_URI, new StreamRequestHandlerAdapter(new DelayedStoreAndForwardEchoHandler()));
    return handlers;
}
Also used : StreamRequestHandlerAdapter(com.linkedin.r2.transport.common.StreamRequestHandlerAdapter) StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) HashMap(java.util.HashMap) URI(java.net.URI)

Example 5 with StreamRequestHandler

use of com.linkedin.r2.transport.common.StreamRequestHandler in project rest.li by linkedin.

the class TestQueryTunnel method setUp.

@BeforeClass
protected void setUp() throws Exception {
    Map<String, String> clientProperties = new HashMap<String, String>();
    clientProperties.put(HttpClientFactory.HTTP_QUERY_POST_THRESHOLD, String.valueOf(QUERY_TUNNEL_THRESHOLD));
    clientProperties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, _httpProtocolVersion);
    _clientFactory = new HttpClientFactory();
    final TransportClient transportClient = _clientFactory.getClient(clientProperties);
    _client = new TransportClientAdapter(transportClient, _clientROS);
    final RestRequestHandler restHandler = new CheckQueryTunnelHandler();
    final StreamRequestHandler streamHandler = new StreamRequestHandlerAdapter(restHandler);
    TransportDispatcher dispatcher = new TransportDispatcher() {

        @Override
        public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
            restHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<RestResponse>(callback));
        }

        @Override
        public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
            streamHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<StreamResponse>(callback));
        }
    };
    _server = new HttpServerFactory(_servletType).createH2cServer(_port, dispatcher, _serverROS);
    _server.start();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) RestRequestHandler(com.linkedin.r2.transport.common.RestRequestHandler) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) StreamRequestHandlerAdapter(com.linkedin.r2.transport.common.StreamRequestHandlerAdapter) StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) RestRequest(com.linkedin.r2.message.rest.RestRequest) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RequestContext(com.linkedin.r2.message.RequestContext) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) HashMap(java.util.HashMap) Map(java.util.Map) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

StreamRequestHandler (com.linkedin.r2.transport.common.StreamRequestHandler)7 RequestContext (com.linkedin.r2.message.RequestContext)5 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)5 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)5 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)5 RestRequest (com.linkedin.r2.message.rest.RestRequest)4 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)4 URI (java.net.URI)4 HashMap (java.util.HashMap)4 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 DrainReader (com.linkedin.r2.message.stream.entitystream.DrainReader)3 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)3 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)3 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)3 Map (java.util.Map)3 BeforeClass (org.testng.annotations.BeforeClass)3 ByteString (com.linkedin.data.ByteString)2 FilterChain (com.linkedin.r2.filter.FilterChain)2 RestException (com.linkedin.r2.message.rest.RestException)2 StreamRequestHandlerAdapter (com.linkedin.r2.transport.common.StreamRequestHandlerAdapter)2