Search in sources :

Example 11 with TransportCallback

use of com.linkedin.r2.transport.common.bridge.common.TransportCallback 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 12 with TransportCallback

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

the class TestHttpNettyClient method testRequestContextAttributes.

@Test
public void testRequestContextAttributes() throws InterruptedException, IOException, TimeoutException {
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildRest();
    RestRequest r = new RestRequestBuilder(URI.create("http://localhost")).build();
    FutureCallback<RestResponse> cb = new FutureCallback<>();
    TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
    RequestContext requestContext = new RequestContext();
    client.restRequest(r, requestContext, new HashMap<>(), callback);
    final String actualRemoteAddress = (String) requestContext.getLocalAttr(R2Constants.REMOTE_SERVER_ADDR);
    final HttpProtocolVersion actualProtocolVersion = (HttpProtocolVersion) requestContext.getLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION);
    Assert.assertTrue("127.0.0.1".equals(actualRemoteAddress) || "0:0:0:0:0:0:0:1".equals(actualRemoteAddress), "Actual remote client address is not expected. " + "The local attribute field must be IP address in string type");
    Assert.assertEquals(actualProtocolVersion, HttpProtocolVersion.HTTP_1_1);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) HttpProtocolVersion(com.linkedin.r2.transport.http.common.HttpProtocolVersion) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 13 with TransportCallback

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

the class TestHttpNettyClient method testMakingOutboundHttpsRequest.

@Test(enabled = false)
public void testMakingOutboundHttpsRequest() throws NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
    SSLContext context = SSLContext.getDefault();
    SSLParameters sslParameters = context.getDefaultSSLParameters();
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setSSLContext(context).setSSLParameters(sslParameters).buildRest();
    RestRequest r = new RestRequestBuilder(URI.create("https://www.howsmyssl.com/a/check")).build();
    FutureCallback<RestResponse> cb = new FutureCallback<RestResponse>();
    TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(cb);
    client.restRequest(r, new RequestContext(), new HashMap<String, String>(), callback);
    cb.get(30, TimeUnit.SECONDS);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestResponse(com.linkedin.r2.message.rest.RestResponse) SSLContext(javax.net.ssl.SSLContext) RestRequest(com.linkedin.r2.message.rest.RestRequest) SSLParameters(javax.net.ssl.SSLParameters) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 14 with TransportCallback

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

the class TestHttpNettyStreamClient method testNoChannelTimeout.

@Test
public void testNoChannelTimeout() throws InterruptedException {
    HttpNettyStreamClient client = new HttpNettyStreamClient(new NoCreations(_scheduler), _scheduler, 500, 500, 1024 * 1024 * 2);
    RestRequest r = new RestRequestBuilder(URI.create("http://localhost/")).build();
    FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
    client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
    try {
        // This timeout needs to be significantly larger than the getTimeout of the netty client;
        // we're testing that the client will generate its own timeout
        cb.get(30, TimeUnit.SECONDS);
        Assert.fail("Get was supposed to time out");
    } catch (TimeoutException e) {
        // TimeoutException means the timeout for Future.get() elapsed and nothing happened.
        // Instead, we are expecting our callback to be invoked before the future timeout
        // with a timeout generated by the HttpNettyClient.
        Assert.fail("Unexpected TimeoutException, should have been ExecutionException", e);
    } catch (ExecutionException e) {
        verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 15 with TransportCallback

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

the class TestHttpNettyStreamClient method testMakingOutboundHttpsRequest.

@Test(enabled = false)
public void testMakingOutboundHttpsRequest() throws NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
    SSLContext context = SSLContext.getDefault();
    SSLParameters sslParameters = context.getDefaultSSLParameters();
    HttpNettyStreamClient client = new HttpClientBuilder(_eventLoop, _scheduler).setSSLContext(context).setSSLParameters(sslParameters).buildStream();
    RestRequest r = new RestRequestBuilder(URI.create("https://www.howsmyssl.com/a/check")).build();
    FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
    client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
    cb.get(30, TimeUnit.SECONDS);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) SSLContext(javax.net.ssl.SSLContext) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) RestRequest(com.linkedin.r2.message.rest.RestRequest) SSLParameters(javax.net.ssl.SSLParameters) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

RestRequest (com.linkedin.r2.message.rest.RestRequest)32 RequestContext (com.linkedin.r2.message.RequestContext)30 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)29 RestResponse (com.linkedin.r2.message.rest.RestResponse)26 FutureCallback (com.linkedin.common.callback.FutureCallback)22 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)22 Test (org.testng.annotations.Test)22 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)21 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)19 ByteString (com.linkedin.data.ByteString)16 ExecutionException (java.util.concurrent.ExecutionException)16 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)14 TimeoutException (java.util.concurrent.TimeoutException)14 HashMap (java.util.HashMap)13 URI (java.net.URI)12 Map (java.util.Map)12 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)11 RestException (com.linkedin.r2.message.rest.RestException)10 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)10 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)9