Search in sources :

Example 81 with StreamRequest

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;
        });
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) ContentType(com.linkedin.restli.common.ContentType) StreamDataCodec(com.linkedin.data.codec.entitystream.StreamDataCodec) ByteString(com.linkedin.data.ByteString) RequestContext(com.linkedin.r2.message.RequestContext)

Example 82 with StreamRequest

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));
    }
}
Also used : StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) URI(java.net.URI) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) RestException(com.linkedin.r2.message.rest.RestException)

Example 83 with StreamRequest

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));
            }
        });
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 84 with StreamRequest

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));
}
Also used : HashMap(java.util.HashMap) RequestContext(com.linkedin.r2.message.RequestContext)

Example 85 with StreamRequest

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));
}
Also used : StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Aggregations

StreamRequest (com.linkedin.r2.message.stream.StreamRequest)173 Test (org.testng.annotations.Test)132 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)121 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)116 URI (java.net.URI)84 RequestContext (com.linkedin.r2.message.RequestContext)82 CountDownLatch (java.util.concurrent.CountDownLatch)51 ByteString (com.linkedin.data.ByteString)46 RestRequest (com.linkedin.r2.message.rest.RestRequest)45 Callback (com.linkedin.common.callback.Callback)38 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)34 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)32 HashMap (java.util.HashMap)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)29 BeforeTest (org.testng.annotations.BeforeTest)26 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)25 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)24 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)23 RestResponse (com.linkedin.r2.message.rest.RestResponse)23 AfterTest (org.testng.annotations.AfterTest)23