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));
}
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));
}
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);
}
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);
}
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();
}
Aggregations