use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class RewriteLoadBalancerClientTestStreamRequest method testWithEverything.
@Test
public void testWithEverything() {
URI uri = URI.create("http://username:password@test.linkedin.com:9876/test");
String serviceName = "HistoryService";
TestClient wrappedClient = new TestClient();
RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
assertEquals(client.getUri(), uri);
assertEquals(client.getServiceName(), serviceName);
StreamRequest streamRequest = getRequest("d2://HistoryService/getCube?bar=baz#fragId");
Map<String, String> restWireAttrs = new HashMap<>();
TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<>();
client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
assertFalse(restCallback.response.hasError());
assertEquals(wrappedClient.streamRequest.getHeaders(), streamRequest.getHeaders());
assertEquals(wrappedClient.streamRequest.getMethod(), streamRequest.getMethod());
assertEquals(wrappedClient.streamRequest.getURI(), URI.create("http://username:password@test.linkedin.com:9876/test/getCube?bar=baz#fragId"));
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class RewriteLoadBalancerClientTestStreamRequest method testWithQueryAndFragment.
@Test
public void testWithQueryAndFragment() {
URI uri = URI.create("http://test.linkedin.com/test");
String serviceName = "HistoryService";
TestClient wrappedClient = new TestClient();
RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
assertEquals(client.getUri(), uri);
assertEquals(client.getServiceName(), serviceName);
StreamRequest streamRequest = getRequest("d2://HistoryService/getCube?bar=baz#fragId");
Map<String, String> restWireAttrs = new HashMap<>();
TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<>();
client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
assertFalse(restCallback.response.hasError());
assertEquals(wrappedClient.streamRequest.getHeaders(), streamRequest.getHeaders());
assertEquals(wrappedClient.streamRequest.getMethod(), streamRequest.getMethod());
assertEquals(wrappedClient.streamRequest.getURI(), URI.create("http://test.linkedin.com/test/getCube?bar=baz#fragId"));
}
use of com.linkedin.r2.message.stream.StreamRequest 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.message.stream.StreamRequest in project rest.li by linkedin.
the class QueryTunnelUtil method encode.
/**
* @param request a StreamRequest object to be encoded as a tunneled POST
* @param requestContext a RequestContext object associated with the request
* @param threshold the size of the query params above which the request will be encoded
* @param callback the callback to be executed with the encoded request
*/
public static void encode(final StreamRequest request, RequestContext requestContext, int threshold, final Callback<StreamRequest> callback) {
URI uri = request.getURI();
// Check to see if we should tunnel this request by testing the length of the query
// if the query is NULL, we won't bother to encode.
// 0 length is a special case that could occur with a url like http://www.foo.com?
// which we don't want to encode, because we'll lose the "?" in the process
// Otherwise only encode queries whose length is greater than or equal to the
// threshold value.
String query = uri.getRawQuery();
boolean forceQueryTunnel = requestContext.getLocalAttr(R2Constants.FORCE_QUERY_TUNNEL) != null && (Boolean) requestContext.getLocalAttr(R2Constants.FORCE_QUERY_TUNNEL);
if (query == null || query.length() == 0 || (query.length() < threshold && !forceQueryTunnel)) {
callback.onSuccess(request);
} else {
// If we need to encode, we'll fully buffer the request first. See class doc for the reasoning.
Messages.toRestRequest(request, new Callback<RestRequest>() {
@Override
public void onError(Throwable e) {
callback.onError(e);
}
@Override
public void onSuccess(RestRequest result) {
RestRequest encodedRequest;
try {
encodedRequest = doEncode(result);
} catch (Exception ex) {
callback.onError(ex);
return;
}
callback.onSuccess(Messages.toStreamRequest(encodedRequest));
}
});
}
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class AbstractClient method restRequest.
@Override
public void restRequest(RestRequest request, RequestContext requestContext, Callback<RestResponse> callback) {
StreamRequest streamRequest = Messages.toStreamRequest(request);
// IS_FULL_REQUEST flag, if set true, would result in the request being sent without using chunked transfer encoding
// This is needed as the legacy R2 server (before 2.8.0) does not support chunked transfer encoding.
requestContext.putLocalAttr(R2Constants.IS_FULL_REQUEST, true);
// here we add back the content-length header for the response because some client code depends on this header
streamRequest(streamRequest, requestContext, Messages.toStreamCallback(callback, true));
}
Aggregations