Search in sources :

Example 1 with RequestWithCallback

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

the class Http2AlpnHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    _alpnPromise.addListener(f -> {
        ChannelFuture future = (ChannelFuture) f;
        if (future.isSuccess()) {
            ctx.write(msg, promise);
        } else {
            @SuppressWarnings("unchecked") TimeoutAsyncPoolHandle<?> handle = ((RequestWithCallback<?, ?, TimeoutAsyncPoolHandle<?>>) msg).handle();
            handle.error().release();
            TransportCallback<?> callback = ((RequestWithCallback) msg).callback();
            callback.onResponse(TransportResponseImpl.error(future.cause()));
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback)

Example 2 with RequestWithCallback

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

the class Http2InitializerHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    if (_setupComplete) {
        ctx.write(msg);
    } else {
        Request request = ((RequestWithCallback) msg).request();
        URI uri = request.getURI();
        String scheme = uri.getScheme();
        if (scheme.equalsIgnoreCase(HttpScheme.HTTPS.toString())) {
            configureHttpsPipeline(ctx);
        } else if (scheme.equalsIgnoreCase(HttpScheme.HTTP.toString())) {
            configureHttpPipeline(ctx, request);
        } else {
            throw new IllegalArgumentException("Invalid scheme " + scheme + ", expected either http or https");
        }
        ctx.write(msg);
    }
}
Also used : RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback) Request(com.linkedin.r2.message.Request) URI(java.net.URI)

Example 3 with RequestWithCallback

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

the class Http2SchemeHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    Request request = ((RequestWithCallback) msg).request();
    URI uri = request.getURI();
    String scheme = uri.getScheme();
    if (!scheme.equalsIgnoreCase(_scheme)) {
        // Specified scheme does not match the existing scheme for the pipeline. Returns channel back to the pool
        // and throws exception to the caller.
        ((RequestWithCallback) msg).handle().release();
        throw new IllegalStateException(String.format("Cannot switch scheme from %s to %s for %s", _scheme, scheme, ctx.channel().remoteAddress()));
    }
    ctx.write(msg, promise);
}
Also used : RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback) Request(com.linkedin.r2.message.Request) URI(java.net.URI)

Example 4 with RequestWithCallback

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

the class Http2StreamCodec method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    Request request = ((RequestWithCallback) msg).request();
    Http2ConnectionEncoder encoder = encoder();
    int streamId = connection().local().incrementAndGetNextStreamId();
    if (request instanceof StreamRequest) {
        LOG.debug("Writing StreamRequest...");
        StreamRequest streamRequest = (StreamRequest) request;
        Http2Headers http2Headers = NettyRequestAdapter.toHttp2Headers(streamRequest);
        BufferedReader reader = new BufferedReader(ctx, encoder, streamId, ((RequestWithCallback) msg).handle());
        streamRequest.getEntityStream().setReader(reader);
        encoder.writeHeaders(ctx, streamId, http2Headers, NO_PADDING, NOT_END_STREAM, promise).addListener(future -> reader.request());
        LOG.debug("Sent HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, NOT_END_STREAM, http2Headers.size(), NO_PADDING });
    } else if (request instanceof RestRequest) {
        LOG.debug("Writing RestRequest...");
        PromiseCombiner promiseCombiner = new PromiseCombiner();
        ChannelPromise headersPromise = ctx.channel().newPromise();
        ChannelPromise dataPromise = ctx.channel().newPromise();
        promiseCombiner.add(headersPromise);
        promiseCombiner.add(dataPromise);
        promiseCombiner.finish(promise);
        RestRequest restRequest = (RestRequest) request;
        Http2Headers headers = NettyRequestAdapter.toHttp2Headers(restRequest);
        encoder.writeHeaders(ctx, streamId, headers, NO_PADDING, NOT_END_STREAM, headersPromise);
        LOG.debug("Sent HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, NOT_END_STREAM, headers.size(), NO_PADDING });
        ByteBuf data = Unpooled.wrappedBuffer(restRequest.getEntity().asByteBuffer());
        encoder.writeData(ctx, streamId, data, NO_PADDING, END_STREAM, dataPromise);
        LOG.debug("Sent HTTP/2 DATA frame, stream={}, end={}, data={}bytes, padding={}bytes", new Object[] { streamId, END_STREAM, data.readableBytes(), NO_PADDING });
    } else {
        // Request type is not supported. Returns channel back to the pool and throws exception.
        ctx.fireChannelRead(((RequestWithCallback) msg).handle());
        throw new IllegalArgumentException("Request is neither StreamRequest or RestRequest");
    }
    // Sets TransportCallback as a stream property to be retrieved later
    TransportCallback<?> callback = ((RequestWithCallback) msg).callback();
    Http2Connection.PropertyKey callbackKey = ctx.channel().attr(Http2ClientPipelineInitializer.CALLBACK_ATTR_KEY).get();
    connection().stream(streamId).setProperty(callbackKey, callback);
    // Sets AsyncPoolHandle as a stream property to be retrieved later
    AsyncPoolHandle<?> handle = ((RequestWithCallback) msg).handle();
    Http2Connection.PropertyKey handleKey = ctx.channel().attr(Http2ClientPipelineInitializer.CHANNEL_POOL_HANDLE_ATTR_KEY).get();
    connection().stream(streamId).setProperty(handleKey, handle);
}
Also used : PromiseCombiner(io.netty.util.concurrent.PromiseCombiner) Http2Connection(io.netty.handler.codec.http2.Http2Connection) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Request(com.linkedin.r2.message.Request) RestRequest(com.linkedin.r2.message.rest.RestRequest) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback) Http2Headers(io.netty.handler.codec.http2.Http2Headers) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder)

Example 5 with RequestWithCallback

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

the class Http2UpgradeHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    _upgradePromise.addListener(f -> {
        ChannelFuture future = (ChannelFuture) f;
        if (future.isSuccess()) {
            ctx.write(msg, promise);
        } else {
            @SuppressWarnings("unchecked") TimeoutAsyncPoolHandle<?> handle = ((RequestWithCallback<?, ?, TimeoutAsyncPoolHandle<?>>) msg).handle();
            handle.error().release();
            TransportCallback<?> callback = ((RequestWithCallback) msg).callback();
            callback.onResponse(TransportResponseImpl.error(future.cause()));
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback)

Aggregations

RequestWithCallback (com.linkedin.r2.transport.common.bridge.common.RequestWithCallback)5 Request (com.linkedin.r2.message.Request)3 Callback (com.linkedin.common.callback.Callback)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 IndividualRequest (com.linkedin.restli.common.multiplexer.IndividualRequest)2 IndividualRequestMap (com.linkedin.restli.common.multiplexer.IndividualRequestMap)2 ChannelFuture (io.netty.channel.ChannelFuture)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelPromise (io.netty.channel.ChannelPromise)1 Http2Connection (io.netty.handler.codec.http2.Http2Connection)1 Http2ConnectionEncoder (io.netty.handler.codec.http2.Http2ConnectionEncoder)1 Http2Headers (io.netty.handler.codec.http2.Http2Headers)1 PromiseCombiner (io.netty.util.concurrent.PromiseCombiner)1