Search in sources :

Example 1 with HttpProtocolVersion

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

the class HttpClientFactory method getRawClient.

/**
   * Testing aid.
   */
TransportClient getRawClient(Map<String, ? extends Object> properties, SSLContext sslContext, SSLParameters sslParameters) {
    Integer poolSize = chooseNewOverDefault(getIntValue(properties, HTTP_POOL_SIZE), DEFAULT_POOL_SIZE);
    Integer idleTimeout = chooseNewOverDefault(getIntValue(properties, HTTP_IDLE_TIMEOUT), DEFAULT_IDLE_TIMEOUT);
    Integer shutdownTimeout = chooseNewOverDefault(getIntValue(properties, HTTP_SHUTDOWN_TIMEOUT), DEFAULT_SHUTDOWN_TIMEOUT);
    long maxResponseSize = chooseNewOverDefault(getLongValue(properties, HTTP_MAX_RESPONSE_SIZE), DEFAULT_MAX_RESPONSE_SIZE);
    Integer requestTimeout = chooseNewOverDefault(getIntValue(properties, HTTP_REQUEST_TIMEOUT), DEFAULT_REQUEST_TIMEOUT);
    Integer poolWaiterSize = chooseNewOverDefault(getIntValue(properties, HTTP_POOL_WAITER_SIZE), DEFAULT_POOL_WAITER_SIZE);
    String clientName = null;
    if (properties != null && properties.containsKey(HTTP_SERVICE_NAME)) {
        clientName = properties.get(HTTP_SERVICE_NAME) + "Client";
    }
    clientName = chooseNewOverDefault(clientName, DEFAULT_CLIENT_NAME);
    AsyncPoolImpl.Strategy strategy = chooseNewOverDefault(getStrategy(properties), DEFAULT_POOL_STRATEGY);
    Integer poolMinSize = chooseNewOverDefault(getIntValue(properties, HTTP_POOL_MIN_SIZE), DEFAULT_POOL_MIN_SIZE);
    Integer maxHeaderSize = chooseNewOverDefault(getIntValue(properties, HTTP_MAX_HEADER_SIZE), DEFAULT_MAX_HEADER_SIZE);
    Integer maxChunkSize = chooseNewOverDefault(getIntValue(properties, HTTP_MAX_CHUNK_SIZE), DEFAULT_MAX_CHUNK_SIZE);
    Integer maxConcurrentConnections = chooseNewOverDefault(getIntValue(properties, HTTP_MAX_CONCURRENT_CONNECTIONS), Integer.MAX_VALUE);
    HttpProtocolVersion httpProtocolVersion = chooseNewOverDefault(getHttpProtocolVersion(properties, HTTP_PROTOCOL_VERSION), _defaultHttpVersion);
    TransportClient streamClient;
    switch(httpProtocolVersion) {
        case HTTP_1_1:
            streamClient = new HttpNettyStreamClient(_eventLoopGroup, _executor, poolSize, requestTimeout, idleTimeout, shutdownTimeout, maxResponseSize, sslContext, sslParameters, _callbackExecutorGroup, poolWaiterSize, clientName + "-Stream", /* to distinguish channel pool metrics from rest client during transition period */
            _jmxManager, strategy, poolMinSize, maxHeaderSize, maxChunkSize, maxConcurrentConnections, _tcpNoDelay);
            break;
        case HTTP_2:
            streamClient = new Http2NettyStreamClient(_eventLoopGroup, _executor, requestTimeout, idleTimeout, shutdownTimeout, maxResponseSize, sslContext, sslParameters, _callbackExecutorGroup, poolWaiterSize, clientName + "-HTTP/2-Stream", /* to distinguish channel pool metrics from rest client during transition period */
            _jmxManager, maxHeaderSize, maxChunkSize, maxConcurrentConnections, _tcpNoDelay);
            break;
        default:
            throw new IllegalArgumentException("Unrecognized HTTP protocol version " + httpProtocolVersion);
    }
    HttpNettyClient legacyClient = new HttpNettyClient(_eventLoopGroup, _executor, poolSize, requestTimeout, idleTimeout, shutdownTimeout, (int) maxResponseSize, sslContext, sslParameters, _callbackExecutorGroup, poolWaiterSize, clientName, _jmxManager, strategy, poolMinSize, maxHeaderSize, maxChunkSize, maxConcurrentConnections);
    return new MixedClient(legacyClient, streamClient);
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HttpProtocolVersion(com.linkedin.r2.transport.http.common.HttpProtocolVersion)

Example 2 with HttpProtocolVersion

use of com.linkedin.r2.transport.http.common.HttpProtocolVersion 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 3 with HttpProtocolVersion

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

the class TestHttpNettyStreamClient method testRequestContextAttributes.

@Test(dataProvider = "remoteClientAddressClients")
public void testRequestContextAttributes(AbstractNettyStreamClient client) throws InterruptedException, IOException, TimeoutException {
    RestRequest r = new RestRequestBuilder(URI.create("http://localhost")).build();
    FutureCallback<StreamResponse> cb = new FutureCallback<>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
    RequestContext requestContext = new RequestContext();
    client.streamRequest(Messages.toStreamRequest(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" + actualRemoteAddress);
    if (client instanceof HttpNettyStreamClient) {
        Assert.assertEquals(actualProtocolVersion, HttpProtocolVersion.HTTP_1_1);
    } else if (client instanceof Http2NettyStreamClient) {
        Assert.assertEquals(actualProtocolVersion, HttpProtocolVersion.HTTP_2);
    } else {
        Assert.fail("Unexpected client instance type");
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) HttpProtocolVersion(com.linkedin.r2.transport.http.common.HttpProtocolVersion) 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) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 4 with HttpProtocolVersion

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

the class AbstractR2Servlet method readRequestContext.

/**
   * Read HTTP-specific properties from the servlet request into the request context. We'll read
   * properties that many clients might be interested in, such as the caller's IP address.
   * @param req The HTTP servlet request
   * @return The request context
   */
protected RequestContext readRequestContext(HttpServletRequest req) {
    RequestContext context = new RequestContext();
    context.putLocalAttr(R2Constants.REMOTE_ADDR, req.getRemoteAddr());
    HttpProtocolVersion protocol = HttpProtocolVersion.parse(req.getProtocol());
    context.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, protocol);
    if (req.isSecure()) {
        // attribute name documented in ServletRequest API:
        // http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getAttribute%28java.lang.String%29
        Object[] certs = (Object[]) req.getAttribute("javax.servlet.request.X509Certificate");
        if (certs != null && certs.length > 0) {
            context.putLocalAttr(R2Constants.CLIENT_CERT, certs[0]);
        }
        context.putLocalAttr(R2Constants.IS_SECURE, true);
    } else {
        context.putLocalAttr(R2Constants.IS_SECURE, false);
    }
    return context;
}
Also used : HttpProtocolVersion(com.linkedin.r2.transport.http.common.HttpProtocolVersion) RequestContext(com.linkedin.r2.message.RequestContext)

Example 5 with HttpProtocolVersion

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

the class ServletHelper method readRequestContext.

/**
   * Read HTTP-specific properties from the servlet request into the request context. We'll read
   * properties that many clients might be interested in, such as the caller's IP address.
   * @param req The HTTP servlet request
   * @return The request context
   */
static RequestContext readRequestContext(HttpServletRequest req) {
    RequestContext context = new RequestContext();
    context.putLocalAttr(R2Constants.REMOTE_ADDR, req.getRemoteAddr());
    HttpProtocolVersion protocol = HttpProtocolVersion.parse(req.getProtocol());
    context.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, protocol);
    if (req.isSecure()) {
        // attribute name documented in ServletRequest API:
        // http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getAttribute%28java.lang.String%29
        Object[] certs = (Object[]) req.getAttribute("javax.servlet.request.X509Certificate");
        if (certs != null && certs.length > 0) {
            context.putLocalAttr(R2Constants.CLIENT_CERT, certs[0]);
        }
        context.putLocalAttr(R2Constants.IS_SECURE, true);
    } else {
        context.putLocalAttr(R2Constants.IS_SECURE, false);
    }
    return context;
}
Also used : HttpProtocolVersion(com.linkedin.r2.transport.http.common.HttpProtocolVersion) RequestContext(com.linkedin.r2.message.RequestContext)

Aggregations

HttpProtocolVersion (com.linkedin.r2.transport.http.common.HttpProtocolVersion)5 RequestContext (com.linkedin.r2.message.RequestContext)4 FutureCallback (com.linkedin.common.callback.FutureCallback)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)2 Test (org.testng.annotations.Test)2 ByteString (com.linkedin.data.ByteString)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)1 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)1 AsciiString (io.netty.util.AsciiString)1