Search in sources :

Example 61 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class RestClient method sendStreamRequestImpl.

/**
   * Sends an untyped stream request using a callback.
   *
   * @param requestContext context for the request
   * @param uri for resource
   * @param method to perform
   * @param dataMap request body entity
   * @param headers additional headers to be added to the request
   * @param cookies the cookies to be sent with the request
   * @param methodName the method name (used for finders and actions)
   * @param protocolVersion the version of the Rest.li protocol used to build this request
   * @param requestOptions contains compression force on/off overrides, request content type and accept types
   * @param callback to call on request completion. In the event of an error, the callback
   *                 will receive a {@link com.linkedin.r2.RemoteInvocationException}. If a valid
   *                 error response was received from the remote server, the callback will receive
   *                 a {@link com.linkedin.r2.message.rest.RestException} containing the error details.
   */
private void sendStreamRequestImpl(RequestContext requestContext, URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, String methodName, ProtocolVersion protocolVersion, RestliRequestOptions requestOptions, List<Object> streamingAttachments, Callback<StreamResponse> callback) {
    try {
        final StreamRequest request = buildStreamRequest(uri, method, dataMap, headers, cookies, protocolVersion, requestOptions.getContentType(), requestOptions.getAcceptTypes(), requestOptions.getAcceptResponseAttachments(), streamingAttachments);
        String operation = OperationNameGenerator.generate(method, methodName);
        requestContext.putLocalAttr(R2Constants.OPERATION, operation);
        requestContext.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestOptions.getRequestCompressionOverride());
        requestContext.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, requestOptions.getResponseCompressionOverride());
        _client.streamRequest(request, requestContext, callback);
    } catch (Exception e) {
        // No need to wrap the exception; RestLiCallbackAdapter.onError() will take care of that
        callback.onError(e);
    }
}
Also used : ByteString(com.linkedin.data.ByteString) URISyntaxException(java.net.URISyntaxException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 62 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class RestClient method sendRestRequestImpl.

/**
   * Sends an untyped REST request using a callback.
   *
   * @param requestContext context for the request
   * @param uri for resource
   * @param method to perform
   * @param dataMap request body entity
   * @param headers additional headers to be added to the request
   * @param cookies the cookies to be sent with the request
   * @param methodName the method name (used for finders and actions)
   * @param protocolVersion the version of the Rest.li protocol used to build this request
   * @param requestOptions contains compression force on/off overrides, request content type and accept types
   * @param callback to call on request completion. In the event of an error, the callback
   *                 will receive a {@link com.linkedin.r2.RemoteInvocationException}. If a valid
   *                 error response was received from the remote server, the callback will receive
   *                 a {@link com.linkedin.r2.message.rest.RestException} containing the error details.
   */
private void sendRestRequestImpl(RequestContext requestContext, URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, String methodName, ProtocolVersion protocolVersion, RestliRequestOptions requestOptions, Callback<RestResponse> callback) {
    try {
        RestRequest request = buildRestRequest(uri, method, dataMap, headers, cookies, protocolVersion, requestOptions.getContentType(), requestOptions.getAcceptTypes(), false);
        String operation = OperationNameGenerator.generate(method, methodName);
        requestContext.putLocalAttr(R2Constants.OPERATION, operation);
        requestContext.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestOptions.getRequestCompressionOverride());
        requestContext.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, requestOptions.getResponseCompressionOverride());
        _client.restRequest(request, requestContext, callback);
    } catch (Exception e) {
        // No need to wrap the exception; RestLiCallbackAdapter.onError() will take care of that
        callback.onError(e);
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ByteString(com.linkedin.data.ByteString) URISyntaxException(java.net.URISyntaxException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException)

Example 63 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class MultiplexedCallback method buildIndividualRestResponse.

private static RestResponse buildIndividualRestResponse(Response<?> envelopeResponse, IndividualResponse individualResponse) throws IOException, MimeTypeParseException {
    IndividualBody body = individualResponse.getBody(GetMode.NULL);
    ByteString entity = (body != null) ? DataMapConverter.dataMapToByteString(individualResponse.getHeaders(), body.data()) : ByteString.empty();
    return new RestResponseBuilder().setStatus(individualResponse.getStatus()).setHeaders(inheritHeaders(individualResponse, envelopeResponse)).setCookies(CookieUtil.encodeSetCookies(envelopeResponse.getCookies())).setEntity(entity).build();
}
Also used : IndividualBody(com.linkedin.restli.common.multiplexer.IndividualBody) ByteString(com.linkedin.data.ByteString) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder)

Example 64 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class MultiplexedCallback method notifyIndividualCallbacks.

private void notifyIndividualCallbacks(Response<MultiplexedResponseContent> muxResponse) {
    IndividualResponseMap individualResponses = muxResponse.getEntity().getResponses();
    for (IndividualResponseMap.Entry<String, IndividualResponse> individualResponseMapEntry : individualResponses.entrySet()) {
        Integer id = Integer.valueOf(individualResponseMapEntry.getKey());
        IndividualResponse individualResponse = individualResponseMapEntry.getValue();
        Callback<RestResponse> callback = _callbacks.get(id);
        RestResponse individualRestResponse;
        try {
            individualRestResponse = buildIndividualRestResponse(muxResponse, individualResponse);
        } catch (MimeTypeParseException e) {
            callback.onError(new RestLiDecodingException("Could not convert IndividualResponse to individual RestRestponse due to an invalid content type, id=" + id, e));
            return;
        } catch (IOException e) {
            callback.onError(new RestLiDecodingException("Could not convert IndividualResponse to individual RestRestponse, id=" + id, e));
            return;
        }
        if (RestStatus.isOK(individualResponse.getStatus())) {
            callback.onSuccess(individualRestResponse);
        } else {
            RestException exception = new RestException(individualRestResponse, "Received error " + individualRestResponse.getStatus());
            callback.onError(exception);
        }
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) ByteString(com.linkedin.data.ByteString) IOException(java.io.IOException) IndividualResponseMap(com.linkedin.restli.common.multiplexer.IndividualResponseMap) IndividualResponse(com.linkedin.restli.common.multiplexer.IndividualResponse)

Example 65 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class FilterUtil method fireStreamRequestResponse.

// Fires a request, saving the local attributes, and then fires a response with the local
// attributes.
public static void fireStreamRequestResponse(FilterChain fc, StreamRequest req, StreamResponse res) {
    final RequestContext context = new RequestContext();
    wrapFilterChain(fc).onStreamRequest(req, context, emptyWireAttrs());
    wrapFilterChain(fc).onStreamResponse(res, context, emptyWireAttrs());
}
Also used : RequestContext(com.linkedin.r2.message.RequestContext)

Aggregations

RestResponse (com.linkedin.r2.message.rest.RestResponse)100 Test (org.testng.annotations.Test)95 RestRequest (com.linkedin.r2.message.rest.RestRequest)63 RequestContext (com.linkedin.r2.message.RequestContext)51 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)47 URI (java.net.URI)45 ByteString (com.linkedin.data.ByteString)42 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)37 RestException (com.linkedin.r2.message.rest.RestException)32 HashMap (java.util.HashMap)29 FutureCallback (com.linkedin.common.callback.FutureCallback)25 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)25 ExecutionException (java.util.concurrent.ExecutionException)25 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)24 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)22 CountDownLatch (java.util.concurrent.CountDownLatch)22 URISyntaxException (java.net.URISyntaxException)21 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)18 IOException (java.io.IOException)18 Map (java.util.Map)17