Search in sources :

Example 46 with Server

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

the class TestGeneralEchoServiceTest method testFilterChain.

@Test
public void testFilterChain() throws Exception {
    final EchoService client = getEchoClient(_client, Bootstrap.getEchoURI());
    final String msg = "This is a simple echo message";
    final FutureCallback<String> callback = new FutureCallback<>();
    client.echo(msg, callback);
    callback.get();
    // Make sure the server got its wire attribute
    Assert.assertEquals(_serverCaptureFilter.getRequest().get(_toServerKey), _toServerValue);
    Assert.assertEquals(_serverCaptureFilter.getResponse().get(_toClientKey), _toClientValue);
    // Make sure the client got its wire attribute, but not the server's wire attribute
    Assert.assertEquals(_clientCaptureFilter.getResponse().get(_toClientKey), _toClientValue);
    Assert.assertNull(_clientCaptureFilter.getResponse().get(_toServerKey));
}
Also used : EchoService(com.linkedin.r2.sample.echo.EchoService) FutureCallback(com.linkedin.common.callback.FutureCallback) AbstractEchoServiceTest(test.r2.integ.clientserver.providers.AbstractEchoServiceTest) Test(org.testng.annotations.Test)

Example 47 with Server

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

the class TestServerCompressionFilter method testResponseCompressionRules.

// Test response compression rules where the server has a default threshold of Integer.MAX_VALUE.
@Test(dataProvider = "headersData")
public void testResponseCompressionRules(String acceptEncoding, int compressionThreshold, EncodingType expectedContentEncoding) throws CompressionException, URISyntaxException {
    ServerCompressionFilter serverCompressionFilter = new ServerCompressionFilter(ACCEPT_COMPRESSIONS);
    RequestContext context = new RequestContext();
    context.putLocalAttr(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
    context.putLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, compressionThreshold);
    int originalLength = 100;
    byte[] entity = new byte[originalLength];
    Arrays.fill(entity, (byte) 'A');
    int compressedLength = (expectedContentEncoding == null) ? originalLength : expectedContentEncoding.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    String expectedContentEncodingName = (expectedContentEncoding == null) ? null : expectedContentEncoding.getHttpName();
    RestResponse restResponse = new RestResponseBuilder().setEntity(entity).build();
    serverCompressionFilter.onRestResponse(restResponse, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, expectedContentEncodingName, compressedLength));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 48 with Server

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

the class ClientStreamCompressionFilter method onStreamResponse.

/**
 *  Decompresses server response
 */
@Override
public void onStreamResponse(StreamResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
    Boolean decompressionOff = (Boolean) requestContext.getLocalAttr(R2Constants.RESPONSE_DECOMPRESSION_OFF);
    if (decompressionOff == null || !decompressionOff) {
        // Check for header encoding
        String compressionHeader = res.getHeader(HttpConstants.CONTENT_ENCODING);
        // decompress if necessary
        if (compressionHeader != null) {
            final StreamEncodingType encoding = StreamEncodingType.get(compressionHeader.trim().toLowerCase());
            if (encoding == null) {
                nextFilter.onError(new IllegalArgumentException("Server returned unrecognized content encoding: " + compressionHeader), requestContext, wireAttrs);
                return;
            }
            final StreamingCompressor compressor = encoding.getCompressor(_executor);
            EntityStream uncompressedStream = compressor.inflate(res.getEntityStream());
            StreamResponseBuilder builder = res.builder();
            Map<String, String> headers = stripHeaders(builder.getHeaders(), HttpConstants.CONTENT_ENCODING, HttpConstants.CONTENT_LENGTH);
            res = builder.setHeaders(headers).build(uncompressedStream);
        }
    }
    nextFilter.onResponse(res, requestContext, wireAttrs);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) StreamEncodingType(com.linkedin.r2.filter.compression.streaming.StreamEncodingType)

Example 49 with Server

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

the class RestLiExampleD2Server method main.

public static void main(String[] args) throws Exception {
    // write D2-related configuration to ZooKeeper
    // all the configuration here are permanent, no need to re-write as long as ZooKeeper still has the data
    // therefore in real case, do this "discovery" step separately
    final HttpServer server = RestLiExampleBasicServer.createServer();
    final ZooKeeperConnectionManager zkConn = createZkConn();
    startServer(server, zkConn);
    System.out.println("Example server with D2 is running with URL " + RestLiExampleBasicServer.getServerUrl() + ". Press any key to stop server.");
    System.in.read();
    stopServer(server, zkConn);
}
Also used : ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager) HttpServer(com.linkedin.r2.transport.http.server.HttpServer)

Example 50 with Server

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

the class RestLiExampleBasicClient method sendRequest.

public void sendRequest(String pathInfo, PrintWriter respWriter) {
    /* Supported URLs
     *  /fail: just fail
     *  /album/<album_id>: display album
     *  all others: make photos, get photos, purge photos
     */
    try {
        if (pathInfo.equals("/fail")) {
            getNonPhoto();
        } else {
            if (pathInfo.startsWith("/album/")) {
                getAlbum(respWriter, Long.parseLong(pathInfo.substring("/album/".length())));
            } else {
                // this track does not make any assumption on server
                // we need to create photo first, find it and clean them up
                // we use both sync and async approaches for creating
                final long newPhotoId = createPhoto(respWriter);
                final CountDownLatch latch = new CountDownLatch(1);
                createPhotoAsync(respWriter, latch, newPhotoId);
                getPhoto(respWriter, newPhotoId);
                findPhoto(respWriter);
                partialUpdatePhoto(respWriter, newPhotoId);
                // photos and albums have IDs starting from 1
                getAlbumSummary(respWriter, (long) new Random().nextInt(10) + 1);
                purgeAllPhotos(respWriter);
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    respWriter.println(e.getMessage());
                }
            }
        }
    } catch (RemoteInvocationException e) {
        respWriter.println("Error in example client: " + e.getMessage());
    }
    respWriter.flush();
}
Also used : Random(java.util.Random) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Test (org.testng.annotations.Test)82 RestRequest (com.linkedin.r2.message.rest.RestRequest)52 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)50 RequestContext (com.linkedin.r2.message.RequestContext)49 URI (java.net.URI)43 RestResponse (com.linkedin.r2.message.rest.RestResponse)41 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)32 FutureCallback (com.linkedin.common.callback.FutureCallback)30 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)30 CountDownLatch (java.util.concurrent.CountDownLatch)24 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)23 RestException (com.linkedin.r2.message.rest.RestException)21 ExecutionException (java.util.concurrent.ExecutionException)21 None (com.linkedin.common.util.None)20 Server (org.eclipse.jetty.server.Server)20 ByteString (com.linkedin.data.ByteString)19 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)19 HashMap (java.util.HashMap)15 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)13 ArrayList (java.util.ArrayList)13