use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.
the class ClientQueryTunnelFilter method onRestRequest.
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
final RestRequest newReq;
try {
newReq = QueryTunnelUtil.encode(req, requestContext, _queryPostThreshold);
} catch (Exception e) {
nextFilter.onError(e, requestContext, wireAttrs);
return;
}
nextFilter.onRequest(newReq, requestContext, wireAttrs);
}
use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.
the class ServerQueryTunnelFilter method onRestRequest.
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
final RestRequest newReq;
try {
newReq = QueryTunnelUtil.decode(req, requestContext);
} catch (MessagingException ex) {
RestResponse errorResponse = RestStatus.responseForStatus(RestStatus.BAD_REQUEST, ex.toString());
nextFilter.onResponse(errorResponse, requestContext, wireAttrs);
return;
} catch (URISyntaxException ex) {
RestResponse errorResponse = RestStatus.responseForStatus(RestStatus.BAD_REQUEST, ex.toString());
nextFilter.onResponse(errorResponse, requestContext, wireAttrs);
return;
} catch (Exception ex) {
nextFilter.onError(ex, requestContext, wireAttrs);
return;
}
nextFilter.onRequest(newReq, requestContext, wireAttrs);
}
use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.
the class ServerQueryTunnelFilter method onStreamRequest.
@Override
public void onStreamRequest(final StreamRequest req, final RequestContext requestContext, final Map<String, String> wireAttrs, final NextFilter<StreamRequest, StreamResponse> nextFilter) {
Callback<StreamRequest> callback = new Callback<StreamRequest>() {
@Override
public void onError(Throwable e) {
if (e instanceof MessagingException || e instanceof URISyntaxException) {
RestResponse errorResponse = RestStatus.responseForStatus(RestStatus.BAD_REQUEST, e.toString());
nextFilter.onResponse(Messages.toStreamResponse(errorResponse), requestContext, wireAttrs);
} else {
nextFilter.onError(e, requestContext, wireAttrs);
}
}
@Override
public void onSuccess(StreamRequest newReq) {
nextFilter.onRequest(newReq, requestContext, wireAttrs);
}
};
// the entire request would be buffered in memory if decoding is needed
// this usually is not a problem as request with extremely query parameters is usually get request with no body
QueryTunnelUtil.decode(req, requestContext, callback);
}
use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.
the class LoggingFilter method onStreamResponse.
@Override
public void onStreamResponse(StreamResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
EntityStream entityStream = res.getEntityStream();
entityStream.addObserver(new Observer() {
private long startTime;
private long bytesNum = 0;
@Override
public void onDataAvailable(ByteString data) {
if (bytesNum == 0) {
startTime = System.nanoTime();
}
bytesNum += data.length();
}
@Override
public void onDone() {
long stopTime = System.nanoTime();
_log.info("Status: success. Total bytes streamed: " + bytesNum + ". Total stream time: " + (stopTime - startTime) + " nano seconds.");
}
@Override
public void onError(Throwable e) {
long stopTime = System.nanoTime();
_log.error("Status: failed. Total bytes streamed: " + bytesNum + ". Total stream time before failure: " + (stopTime - startTime) + " nano seconds.");
}
});
}
use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.
the class TestRequestCompression method initClass.
@BeforeClass
public void initClass() throws Exception {
class CheckRequestCompressionFilter implements RestFilter {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
Map<String, String> requestHeaders = req.getHeaders();
if (requestHeaders.containsKey(TEST_HELP_HEADER)) {
String contentEncodingHeader = requestHeaders.get(HttpConstants.CONTENT_ENCODING);
if (requestHeaders.get(TEST_HELP_HEADER).equals(EXPECT_COMPRESSION)) {
if (contentEncodingHeader == null) {
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is not compressed when it should be.");
} else if (!contentEncodingHeader.equals("x-snappy-framed")) {
// which is always snappy in this test.
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is compressed with " + contentEncodingHeader + " instead of x-snappy-framed.");
}
} else {
if (contentEncodingHeader != null) {
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is compressed when it shouldn't be.");
}
}
}
nextFilter.onRequest(req, requestContext, wireAttrs);
}
}
// Check that Content-Encoding and Content-Length headers are set correctly by ServerCompressionFilter.
class CheckHeadersFilter implements RestFilter {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
if (req.getHeaders().containsKey(HttpConstants.CONTENT_ENCODING)) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Content-Encoding header not removed.");
}
if (req.getEntity().length() != Integer.parseInt(req.getHeader(HttpConstants.CONTENT_LENGTH))) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Content-Length header incorrect.");
}
nextFilter.onRequest(req, requestContext, wireAttrs);
}
}
final FilterChain fc = FilterChains.empty().addLastRest(new CheckRequestCompressionFilter()).addLastRest(new ServerCompressionFilter(RestLiIntTestServer.supportedCompression)).addLastRest(new CheckHeadersFilter()).addLastRest(new SimpleLoggingFilter());
super.init(null, fc, false);
}
Aggregations