use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class StreamRestLiServer method handleStructuredDataResourceRequest.
private void handleStructuredDataResourceRequest(StreamRequest request, RoutingResult routingResult, Callback<?> callback, Function<ContentType, Callback<RestLiResponse>> restLiResponseCallbackConstructor, Consumer<RestRequest> fallbackRequestProcessor) {
ContentType reqContentType, respContentType;
try {
// TODO: We should throw exception instead of defaulting to JSON when the request content type is non-null and
// unrecognized. This behavior was inadvertently changed in commit d149605e4181349b64180bdfe0b4d24a294dc6f6
// when this logic was moved from DataMapUtils.readMapWithExceptions() to DataMapConverter.dataMapToByteString().
reqContentType = ContentType.getContentType(request.getHeader(RestConstants.HEADER_CONTENT_TYPE)).orElse(ContentType.JSON);
String respMimeType = routingResult.getContext().getResponseMimeType();
respContentType = ContentType.getResponseContentType(respMimeType, request.getURI(), request.getHeaders()).orElseThrow(() -> new RestLiServiceException(HttpStatus.S_406_NOT_ACCEPTABLE, "Requested mime type for encoding is not supported. Mimetype: " + respMimeType));
} catch (MimeTypeParseException e) {
callback.onError(e);
return;
}
StreamDataCodec reqCodec = reqContentType.getStreamCodec();
StreamDataCodec respCodec = respContentType.getStreamCodec();
if (_useStreamCodec && reqCodec != null && respCodec != null) {
final RequestContext requestContext = routingResult.getContext().getRawRequestContext();
TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.SERVER_REQUEST_RESTLI_DESERIALIZATION.key());
reqCodec.decodeMap(EntityStreamAdapters.toGenericEntityStream(request.getEntityStream())).handle((dataMap, e) -> {
TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.SERVER_REQUEST_RESTLI_DESERIALIZATION.key());
Throwable error = null;
if (e == null) {
try {
handleResourceRequest(request, routingResult, dataMap, restLiResponseCallbackConstructor.apply(respContentType));
} catch (Throwable throwable) {
error = throwable;
}
} else {
error = buildPreRoutingStreamException(new RoutingException("Cannot parse request entity", HttpStatus.S_400_BAD_REQUEST.getCode(), e), request);
}
if (error != null) {
log.error("Fail to handle structured stream request", error);
callback.onError(error);
}
// handle function requires a return statement although there is no more completion stage.
return null;
});
} else {
// Fallback to fully-buffered request and response processing.
Messages.toRestRequest(request).handle((restRequest, e) -> {
if (e == null) {
try {
fallbackRequestProcessor.accept(restRequest);
} catch (Throwable throwable) {
e = throwable;
}
}
if (e != null) {
log.error("Fail to handle structured toRest request", e);
callback.onError(e);
}
// handle function requires a return statement although there is no more completion stage.
return null;
});
}
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TransportDispatcherImpl method handleStreamRequest.
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
final URI address = req.getURI();
final StreamRequestHandler handler = _streamHandlers.get(address);
if (handler == null) {
final RestResponse response = RestStatus.responseForStatus(RestStatus.NOT_FOUND, "No resource for URI: " + address);
callback.onResponse(TransportResponseImpl.success(Messages.toStreamResponse(response)));
req.getEntityStream().setReader(new DrainReader());
return;
}
try {
handler.handleRequest(req, requestContext, new TransportCallbackAdapter<>(callback));
} catch (Exception e) {
final Exception ex = RestException.forError(RestStatus.INTERNAL_SERVER_ERROR, e);
callback.onResponse(TransportResponseImpl.<StreamResponse>error(ex));
}
}
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 TransportClientAdapter method streamRequest.
@Override
public void streamRequest(StreamRequest request, RequestContext requestContext, Callback<StreamResponse> callback) {
final Map<String, String> wireAttrs = new HashMap<>();
// make a copy of the caller's RequestContext to ensure that we have a unique instance per-request
_client.streamRequest(request, new RequestContext(requestContext), wireAttrs, new TransportCallbackAdapter<>(callback));
}
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