Search in sources :

Example 31 with TransportClientAdapter

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

the class TestResponseCompression method testResponseCompression.

@Test(dataProvider = "requestData")
public void testResponseCompression(Boolean useResponseCompression, CompressionConfig responseCompressionConfig, RestliRequestOptions restliRequestOptions, int idCount, String expectedAcceptEncoding, String expectedCompressionThreshold, boolean responseShouldBeCompressed) throws RemoteInvocationException, CloneNotSupportedException {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
    Map<String, CompressionConfig> responseCompressionConfigs = new HashMap<String, CompressionConfig>();
    if (responseCompressionConfig != null) {
        responseCompressionConfigs.put(SERVICE_NAME, responseCompressionConfig);
    }
    HttpClientFactory httpClientFactory = new HttpClientFactory(FilterChains.empty(), new NioEventLoopGroup(0, /* use default settings */
    new NamedThreadFactory("R2 Nio Event Loop")), true, executor, true, executor, false, AbstractJmxManager.NULL_JMX_MANAGER, Integer.MAX_VALUE, Collections.<String, CompressionConfig>emptyMap(), responseCompressionConfigs, true);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
    if (useResponseCompression != null) {
        properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, String.valueOf(useResponseCompression));
    }
    TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
    RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
    Long[] ids = new Long[idCount];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = (long) i;
    }
    BatchGetRequestBuilder<Long, Greeting> builder = new GreetingsBuilders(restliRequestOptions).batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding);
    if (expectedCompressionThreshold != null) {
        builder.setHeader(EXPECTED_COMPRESSION_THRESHOLD, expectedCompressionThreshold);
    }
    Request<BatchResponse<Greeting>> request = builder.build();
    Response<BatchResponse<Greeting>> response = client.sendRequest(request).getResponse();
    if (responseShouldBeCompressed) {
        Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), EncodingType.GZIP.getHttpName());
    } else {
        Assert.assertNull(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED));
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HashMap(java.util.HashMap) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) BatchResponse(com.linkedin.restli.common.BatchResponse) RestClient(com.linkedin.restli.client.RestClient) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 32 with TransportClientAdapter

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

the class RestLiExampleBasicClient method main.

/**
   * This is a stand-alone app to demo the use of client-side Pegasus API. To run in,
   * com.linkedin.restli.example.RestLiExamplesServer has to be running.
   *
   * The only argument is the path to the resource on the photo server, e.g. /photos/1
   */
public static void main(String[] args) throws Exception {
    // create HTTP Netty client with default properties
    final HttpClientFactory http = new HttpClientFactory();
    final TransportClient transportClient = http.getClient(Collections.<String, String>emptyMap());
    // create an abstraction layer over the actual client, which supports both REST and RPC
    final Client r2Client = new TransportClientAdapter(transportClient);
    // REST client wrapper that simplifies the interface
    final StringBuilder serverUrlBuilder = new StringBuilder("http://").append(SERVER_HOSTNAME).append(":").append(SERVER_PORT).append("/");
    final RestClient restClient = new RestClient(r2Client, serverUrlBuilder.toString());
    final RestLiExampleBasicClient photoClient = new RestLiExampleBasicClient(restClient);
    String pathInfo = args.length == 0 ? "" : args[0];
    photoClient.sendRequest(pathInfo, new PrintWriter(System.out));
    photoClient.shutdown();
    http.shutdown(new FutureCallback<None>());
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestClient(com.linkedin.restli.client.RestClient) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) None(com.linkedin.common.util.None) PrintWriter(java.io.PrintWriter)

Example 33 with TransportClientAdapter

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

the class RestLiFortunesClient method main.

/**
   * This stand-alone app demos the client-side Pegasus API.
   * To see the demo, run RestLiFortuneServer, then start the client
   */
public static void main(String[] args) throws Exception {
    // Create an HttpClient and wrap it in an abstraction layer
    final HttpClientFactory http = new HttpClientFactory();
    final Client r2Client = new TransportClientAdapter(http.getClient(Collections.<String, String>emptyMap()));
    // Create a RestClient to talk to localhost:8080
    RestClient restClient = new RestClient(r2Client, "http://localhost:8080/");
    // Generate a random ID for a fortune cookie, in the range 0-5
    long fortuneId = (long) (Math.random() * 5);
    // Construct a request for the specified fortune
    FortunesGetBuilder getBuilder = _fortuneBuilder.get();
    Request<Fortune> getReq = getBuilder.id(fortuneId).build();
    // Send the request and wait for a response
    final ResponseFuture<Fortune> getFuture = restClient.sendRequest(getReq);
    final Response<Fortune> resp = getFuture.getResponse();
    // Print the response
    System.out.println(resp.getEntity().getFortune());
    // shutdown
    restClient.shutdown(new FutureCallback<None>());
    http.shutdown(new FutureCallback<None>());
}
Also used : RestClient(com.linkedin.restli.client.RestClient) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) None(com.linkedin.common.util.None)

Aggregations

TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)33 HashMap (java.util.HashMap)18 Client (com.linkedin.r2.transport.common.Client)16 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)16 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)14 RestRequest (com.linkedin.r2.message.rest.RestRequest)11 Test (org.testng.annotations.Test)11 None (com.linkedin.common.util.None)10 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)9 FutureCallback (com.linkedin.common.callback.FutureCallback)8 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)7 TransportDispatcherBuilder (com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder)7 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)7 URI (java.net.URI)7 CompressionConfig (com.linkedin.r2.filter.CompressionConfig)6 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)6 BeforeClass (org.testng.annotations.BeforeClass)6 RequestContext (com.linkedin.r2.message.RequestContext)5