use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class ServerStreamCompressionFilter method onStreamResponse.
/**
* Optionally compresses outgoing response
* */
@Override
public void onStreamResponse(final StreamResponse res, final RequestContext requestContext, final Map<String, String> wireAttrs, final NextFilter<StreamRequest, StreamResponse> nextFilter) {
StreamResponse response = res;
try {
String responseCompression = (String) requestContext.getLocalAttr(HttpConstants.ACCEPT_ENCODING);
if (responseCompression == null) {
throw new CompressionException(HttpConstants.ACCEPT_ENCODING + " not in local attribute.");
}
List<AcceptEncoding> parsedEncodings = AcceptEncoding.parseAcceptEncodingHeader(responseCompression, _supportedEncoding);
StreamEncodingType selectedEncoding = AcceptEncoding.chooseBest(parsedEncodings);
//Check if there exists an acceptable encoding
if (selectedEncoding == null) {
//Not acceptable encoding status
response = new StreamResponseBuilder().setStatus(HttpConstants.NOT_ACCEPTABLE).build(EntityStreams.emptyStream());
} else if (selectedEncoding != StreamEncodingType.IDENTITY) {
final int threshold = (Integer) requestContext.getLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD);
final StreamingCompressor compressor = selectedEncoding.getCompressor(_executor);
PartialReader reader = new PartialReader(threshold, new Callback<EntityStream[]>() {
@Override
public void onError(Throwable ex) {
nextFilter.onError(ex, requestContext, wireAttrs);
}
@Override
public void onSuccess(EntityStream[] results) {
if (// entity stream is less than threshold
results.length == 1) {
StreamResponse response = res.builder().build(results[0]);
nextFilter.onResponse(response, requestContext, wireAttrs);
} else {
EntityStream compressedStream = compressor.deflate(EntityStreams.newEntityStream(new CompositeWriter(results)));
StreamResponseBuilder builder = res.builder();
// remove original content-length header if presents.
if (builder.getHeader(HttpConstants.CONTENT_LENGTH) != null) {
Map<String, String> headers = stripHeaders(builder.getHeaders(), HttpConstants.CONTENT_LENGTH);
builder.setHeaders(headers);
}
StreamResponse response = builder.addHeaderValue(HttpConstants.CONTENT_ENCODING, compressor.getContentEncodingName()).build(compressedStream);
nextFilter.onResponse(response, requestContext, wireAttrs);
}
}
});
res.getEntityStream().setReader(reader);
return;
}
} catch (CompressionException e) {
LOG.error(e.getMessage(), e.getCause());
}
nextFilter.onResponse(response, requestContext, wireAttrs);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestStreamRequest method testErrorWriter.
@Test
public void testErrorWriter() throws Exception {
for (Client client : clients()) {
final long totalBytes = SMALL_BYTES_NUM;
EntityStream entityStream = EntityStreams.newEntityStream(new ErrorWriter(totalBytes, BYTE));
StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, FOOBAR_URI));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
error.set(e);
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
latch.countDown();
}
};
client.streamRequest(request, callback);
latch.await();
Assert.assertNotNull(error.get());
}
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestStreamRequest method expectErrorCallback.
private static Callback<StreamResponse> expectErrorCallback(final CountDownLatch latch, final AtomicInteger status) {
return new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
if (e instanceof StreamException) {
StreamResponse errorResponse = ((StreamException) e).getResponse();
status.set(errorResponse.getStatus());
}
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
latch.countDown();
throw new RuntimeException("Should have failed with 404");
}
};
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class MultiplexedRequestBuilder method buildParallel.
private MultiplexedRequest buildParallel() throws RestLiEncodingException {
Map<Integer, Callback<RestResponse>> callbacks = new HashMap<Integer, Callback<RestResponse>>(_requestsWithCallbacks.size());
IndividualRequestMap individualRequests = new IndividualRequestMap(_requestsWithCallbacks.size());
// Dependent requests map is always empty
IndividualRequestMap dependentRequests = new IndividualRequestMap();
for (int i = 0; i < _requestsWithCallbacks.size(); i++) {
RequestWithCallback<?> requestWithCallback = _requestsWithCallbacks.get(i);
IndividualRequest individualRequest = toIndividualRequest(requestWithCallback.getRequest(), dependentRequests);
individualRequests.put(Integer.toString(i), individualRequest);
callbacks.put(i, wrapCallback(requestWithCallback));
}
return toMultiplexedRequest(individualRequests, callbacks);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class MultiplexedRequestBuilder method buildSequential.
private MultiplexedRequest buildSequential() throws RestLiEncodingException {
Map<Integer, Callback<RestResponse>> callbacks = new HashMap<Integer, Callback<RestResponse>>(_requestsWithCallbacks.size());
// Dependent requests - requests which are dependent on the current request (executed after the current request)
IndividualRequestMap dependentRequests = new IndividualRequestMap();
// We start with the last request in the list and proceed backwards because sequential ordering is built using reverse dependencies
for (int i = _requestsWithCallbacks.size() - 1; i >= 0; i--) {
RequestWithCallback<?> requestWithCallback = _requestsWithCallbacks.get(i);
IndividualRequest individualRequest = toIndividualRequest(requestWithCallback.getRequest(), dependentRequests);
dependentRequests = new IndividualRequestMap();
dependentRequests.put(Integer.toString(i), individualRequest);
callbacks.put(i, wrapCallback(requestWithCallback));
}
return toMultiplexedRequest(dependentRequests, callbacks);
}
Aggregations