use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class TestClientStreamCompressionFilter method testRequestCompressionRules.
@Test(dataProvider = "requestData")
public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent, String operation) throws CompressionException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
Executor executor = Executors.newCachedThreadPool();
ClientStreamCompressionFilter clientCompressionFilter = new ClientStreamCompressionFilter(StreamEncodingType.GZIP.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR), executor);
// The entity should be compressible for this test.
int original = 100;
byte[] entity = new byte[original];
Arrays.fill(entity, (byte) 'A');
StreamRequest streamRequest = new StreamRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(entity))));
int compressed = EncodingType.GZIP.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
RequestContext context = new RequestContext();
if (operation != null) {
context.putLocalAttr(R2Constants.OPERATION, operation);
}
context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
int entityLength = headerShouldBePresent ? compressed : original;
FutureCallback<ByteString> callback = new FutureCallback<>();
FullEntityReader reader = new FullEntityReader(callback);
HeaderCaptureFilter captureFilter = new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, headerShouldBePresent, entityLength, reader);
clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), captureFilter);
ByteString entityRead = callback.get(10, TimeUnit.SECONDS);
Assert.assertEquals(entityRead.length(), entityLength);
}
use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class ClientCompressionFilter method onRestResponse.
/**
* Decompresses server response
*/
@Override
public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
Boolean decompressionOff = (Boolean) requestContext.getLocalAttr(R2Constants.RESPONSE_DECOMPRESSION_OFF);
if (decompressionOff == null || !decompressionOff) {
try {
// Check for header encoding
String compressionHeader = res.getHeader(HttpConstants.CONTENT_ENCODING);
// Compress if necessary
if (compressionHeader != null && res.getEntity().length() > 0) {
EncodingType encoding = null;
try {
encoding = EncodingType.get(compressionHeader.trim().toLowerCase());
} catch (IllegalArgumentException e) {
throw new CompressionException(CompressionConstants.SERVER_ENCODING_ERROR + compressionHeader);
}
if (!encoding.hasCompressor()) {
throw new CompressionException(CompressionConstants.SERVER_ENCODING_ERROR + compressionHeader);
}
ByteString inflated = encoding.getCompressor().inflate(res.getEntity());
Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
headers.putAll(res.getHeaders());
headers.remove(HttpConstants.CONTENT_ENCODING);
headers.put(HttpConstants.CONTENT_LENGTH, Integer.toString(inflated.length()));
res = res.builder().setEntity(inflated).setHeaders(headers).build();
}
} catch (CompressionException e) {
nextFilter.onError(e, requestContext, wireAttrs);
return;
}
}
nextFilter.onResponse(res, requestContext, wireAttrs);
}
use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class StreamWriter method doWrite.
/**
* Attempts to write to the entity stream remaining chunks are available. Method must be executed
* by the {@link ChannelHandlerContext}'s executor.
*/
private void doWrite() {
refreshStreamLastActiveTime();
while (_wh.remaining() > 0) {
if (_buffer.isEmpty()) {
break;
}
ByteString data = _buffer.remove(0);
if (data == EOF) {
_wh.done();
_ctx.fireUserEventTriggered(ChannelPipelineEvent.RESPONSE_COMPLETE);
return;
}
_wh.write(data);
_bufferedBytes -= data.length();
if (!_ctx.channel().config().isAutoRead() && _bufferedBytes < BUFFER_LOW_WATER_MARK) {
_ctx.channel().config().setAutoRead(true);
}
refreshStreamLastActiveTime();
}
}
use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class ResponseUtils method buildStreamException.
/**
* @Deprecated: Use buildStreamException(RestLiResponseException, ContentType) method instead
*/
@Deprecated
public static StreamException buildStreamException(RestLiResponseException restLiResponseException, StreamDataCodec codec) {
RestLiResponse restLiResponse = restLiResponseException.getRestLiResponse();
StreamResponseBuilder responseBuilder = new StreamResponseBuilder().setHeaders(restLiResponse.getHeaders()).setCookies(CookieUtil.encodeSetCookies(restLiResponse.getCookies())).setStatus(restLiResponse.getStatus() == null ? HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode() : restLiResponse.getStatus().getCode());
EntityStream<ByteString> entityStream = codec.encodeMap(restLiResponse.getDataMap());
StreamResponse response = responseBuilder.build(EntityStreamAdapters.fromGenericEntityStream(entityStream));
return new StreamException(response, restLiResponseException.getCause());
}
use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class TestRestLiServer method testValidReactiveUnstructuredDataRequest.
@Test(dataProvider = "validClientProtocolVersionDataStreamOnly")
public void testValidReactiveUnstructuredDataRequest(RestLiServer server, ProtocolVersion clientProtocolVersion, String headerConstant) throws URISyntaxException, IOException {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/reactiveFeedDownloads/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
final FeedDownloadResourceReactive resource = getMockResource(FeedDownloadResourceReactive.class);
resource.get(eq(1L), anyObject());
EasyMock.expectLastCall().andDelegateTo(new FeedDownloadResourceReactive()).once();
replay(resource);
@SuppressWarnings("unchecked") Callback<StreamResponse> r2Callback = createMock(Callback.class);
final Capture<StreamResponse> streamResponse = EasyMock.newCapture();
r2Callback.onSuccess(capture(streamResponse));
expectLastCall().once();
replay(r2Callback);
RequestContext requestContext = new RequestContext();
server.handleRequest(streamRequest, requestContext, r2Callback);
verify(resource);
verify(r2Callback);
assertNotNull(streamResponse);
assertEquals(streamResponse.getValue().getHeader(RestConstants.HEADER_CONTENT_TYPE), FeedDownloadResourceReactive.CONTENT_TYPE);
FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
fail("Error inside callback!! Failed to read response data from stream!", e);
}
@Override
public void onSuccess(ByteString result) {
assertEquals(result, FeedDownloadResourceReactive.CONTENT);
}
});
streamResponse.getValue().getEntityStream().setReader(fullEntityReader);
}
Aggregations