Search in sources :

Example 11 with IndividualRequest

use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.

the class TestMultiplexedRequestBuilder method testSequential.

@Test
public void testSequential() throws RestLiEncodingException {
    MultiplexedRequest multiplexedRequest = MultiplexedRequestBuilder.createSequentialRequest().addRequest(request1, callback1).addRequest(request2, callback2).build();
    // verify requests
    IndividualRequest ir2 = fakeIndividualRequest(getUri(ID2));
    IndividualRequest ir1 = fakeIndividualRequest(getUri(ID1), ImmutableMap.of("1", ir2));
    MultiplexedRequestContent expectedRequests = new MultiplexedRequestContent();
    expectedRequests.setRequests(new IndividualRequestMap(ImmutableMap.of("0", ir1)));
    assertMultiplexedRequestContentEquals(multiplexedRequest.getContent(), expectedRequests);
    verifyCallbacks(multiplexedRequest);
}
Also used : IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) MultiplexedRequestContent(com.linkedin.restli.common.multiplexer.MultiplexedRequestContent) Test(org.testng.annotations.Test)

Example 12 with IndividualRequest

use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.

the class MultiplexedRequestBuilder method toIndividualRequest.

private static IndividualRequest toIndividualRequest(Request<?> request, IndividualRequestMap dependantRequests) throws RestLiEncodingException {
    // TODO: Hardcoding RESTLI_PROTOCOL_2_0_0 for now. We need to refactor this code later to get protocol version using the mechanism similar to
    // RestClient.getProtocolVersionForService()
    ProtocolVersion protocolVersion = AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
    String relativeUrl = getRelativeUrl(request, protocolVersion);
    IndividualRequest individualRequest = new IndividualRequest();
    individualRequest.setRelativeUrl(relativeUrl);
    individualRequest.setMethod(request.getMethod().getHttpMethod().name());
    individualRequest.setHeaders(new StringMap(request.getHeaders()));
    List<HttpCookie> cookies = request.getCookies();
    if (cookies != null && !cookies.isEmpty()) {
        throw new IllegalArgumentException(String.format("Cookies for individual request '%s' MUST be added at the envelope request level", relativeUrl));
    }
    individualRequest.setBody(getBody(request, protocolVersion), SetMode.IGNORE_NULL);
    individualRequest.setDependentRequests(dependantRequests);
    return individualRequest;
}
Also used : IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) StringMap(com.linkedin.data.template.StringMap) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) HttpCookie(java.net.HttpCookie)

Example 13 with IndividualRequest

use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.

the class MultiplexedRequestBuilder method buildParallel.

private MultiplexedRequest buildParallel() throws RestLiEncodingException {
    Map<Integer, Callback<RestResponse>> callbacks = new HashMap<>(_requestsWithCallbacks.size());
    IndividualRequestMap individualRequests = new IndividualRequestMap(_requestsWithCallbacks.size());
    // Dependent requests map is always empty
    IndividualRequestMap dependentRequests = new IndividualRequestMap();
    for (int i = 0; i < _requestsWithCallbacks.size(); i++) {
        RequestWithCallback<?> requestWithCallback = _requestsWithCallbacks.get(i);
        IndividualRequest individualRequest = toIndividualRequest(requestWithCallback.getRequest(), dependentRequests);
        individualRequests.put(Integer.toString(i), individualRequest);
        callbacks.put(i, wrapCallback(requestWithCallback));
    }
    return toMultiplexedRequest(individualRequests, callbacks, _requestOptions);
}
Also used : IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) Callback(com.linkedin.common.callback.Callback) HashMap(java.util.HashMap)

Example 14 with IndividualRequest

use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.

the class MultiplexedRequestBuilder method buildSequential.

private MultiplexedRequest buildSequential() throws RestLiEncodingException {
    Map<Integer, Callback<RestResponse>> callbacks = new HashMap<>(_requestsWithCallbacks.size());
    // Dependent requests - requests which are dependent on the current request (executed after the current request)
    IndividualRequestMap dependentRequests = new IndividualRequestMap();
    // We start with the last request in the list and proceed backwards because sequential ordering is built using reverse dependencies
    for (int i = _requestsWithCallbacks.size() - 1; i >= 0; i--) {
        RequestWithCallback<?> requestWithCallback = _requestsWithCallbacks.get(i);
        IndividualRequest individualRequest = toIndividualRequest(requestWithCallback.getRequest(), dependentRequests);
        dependentRequests = new IndividualRequestMap();
        dependentRequests.put(Integer.toString(i), individualRequest);
        callbacks.put(i, wrapCallback(requestWithCallback));
    }
    return toMultiplexedRequest(dependentRequests, callbacks, _requestOptions);
}
Also used : IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) Callback(com.linkedin.common.callback.Callback) HashMap(java.util.HashMap)

Example 15 with IndividualRequest

use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.

the class InheritEnvelopeRequestTask method run.

@Override
protected Promise<? extends IndividualRequest> run(Context context) throws Throwable {
    if (_individualRequest.isFailed()) {
        return Promises.error(_individualRequest.getError());
    }
    IndividualRequest individualRequest = _individualRequest.get();
    inheritHeaders(individualRequest, _envelopeRequest);
    return Promises.value(individualRequest);
}
Also used : IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest)

Aggregations

IndividualRequest (com.linkedin.restli.common.multiplexer.IndividualRequest)21 IndividualRequestMap (com.linkedin.restli.common.multiplexer.IndividualRequestMap)12 Test (org.testng.annotations.Test)12 RequestContext (com.linkedin.r2.message.RequestContext)9 RestRequest (com.linkedin.r2.message.rest.RestRequest)9 FutureCallback (com.linkedin.common.callback.FutureCallback)7 StringMap (com.linkedin.data.template.StringMap)7 RestResponse (com.linkedin.r2.message.rest.RestResponse)7 ByteString (com.linkedin.data.ByteString)5 RestException (com.linkedin.r2.message.rest.RestException)4 MultiplexedResponseContent (com.linkedin.restli.common.multiplexer.MultiplexedResponseContent)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)4 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 HashMap (java.util.HashMap)4 ExecutionException (java.util.concurrent.ExecutionException)4 IndividualResponse (com.linkedin.restli.common.multiplexer.IndividualResponse)3 IndividualResponseMap (com.linkedin.restli.common.multiplexer.IndividualResponseMap)3 MultiplexedRequestContent (com.linkedin.restli.common.multiplexer.MultiplexedRequestContent)3 HttpCookie (java.net.HttpCookie)3