Search in sources :

Example 11 with IndividualRequestMap

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

the class TestMultiplexerRunMode method muxRestRequest.

private RestRequest muxRestRequest(Map<String, IndividualRequest> requests) throws URISyntaxException, IOException {
    MultiplexedRequestContent content = new MultiplexedRequestContent();
    content.setRequests(new IndividualRequestMap(requests));
    return muxRequestBuilder().setMethod(HttpMethod.POST.name()).setEntity(CODEC.mapToBytes(content.data())).setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON).build();
}
Also used : IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) MultiplexedRequestContent(com.linkedin.restli.common.multiplexer.MultiplexedRequestContent)

Example 12 with IndividualRequestMap

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

the class MultiplexerTestBase method fakeIndividualRequest.

protected static IndividualRequest fakeIndividualRequest(String url, Map<String, IndividualRequest> dependentCalls) {
    IndividualRequest request = new IndividualRequest();
    request.setMethod(HttpMethod.GET.name());
    request.setHeaders(new StringMap(HEADERS));
    request.setRelativeUrl(url);
    request.setDependentRequests(new IndividualRequestMap(dependentCalls));
    return request;
}
Also used : IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) StringMap(com.linkedin.data.template.StringMap)

Example 13 with IndividualRequestMap

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

the class MultiplexerTestBase method assertMultiplexedRequestContentEquals.

protected static void assertMultiplexedRequestContentEquals(MultiplexedRequestContent actual, MultiplexedRequestContent expected) {
    IndividualRequestMap actualRequests = actual.getRequests();
    IndividualRequestMap expectedRequests = expected.getRequests();
    Assert.assertEquals(actualRequests.size(), expectedRequests.size());
    for (String id : expectedRequests.keySet()) {
        assertIndividualRequestEquals(actualRequests.get(id), expectedRequests.get(id));
    }
}
Also used : IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) ByteString(com.linkedin.data.ByteString)

Example 14 with IndividualRequestMap

use of com.linkedin.restli.common.multiplexer.IndividualRequestMap 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 15 with IndividualRequestMap

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

the class MultiplexedRequestHandlerImpl method handleRequest.

@Override
public void handleRequest(RestRequest request, RequestContext requestContext, final Callback<RestResponse> callback) {
    if (HttpMethod.POST != HttpMethod.valueOf(request.getMethod())) {
        _log.error("POST is expected, but " + request.getMethod() + " received");
        callback.onError(RestException.forError(HttpStatus.S_405_METHOD_NOT_ALLOWED.getCode(), "Invalid method"));
        return;
    }
    IndividualRequestMap individualRequests;
    try {
        individualRequests = extractIndividualRequests(request);
    } catch (RestException e) {
        _log.error("Invalid multiplexed request", e);
        callback.onError(e);
        return;
    } catch (Exception e) {
        _log.error("Invalid multiplexed request", e);
        callback.onError(RestException.forError(HttpStatus.S_400_BAD_REQUEST.getCode(), e));
        return;
    }
    // prepare the map of individual responses to be collected
    final IndividualResponseMap individualResponses = new IndividualResponseMap(individualRequests.size());
    final Map<String, HttpCookie> responseCookies = new HashMap<>();
    // all tasks are Void and side effect based, that will be useful when we add streaming
    Task<?> requestProcessingTask = createParallelRequestsTask(request, requestContext, individualRequests, individualResponses, responseCookies);
    Task<Void> responseAggregationTask = Task.action("send aggregated response", () -> {
        RestResponse aggregatedResponse = aggregateResponses(individualResponses, responseCookies);
        callback.onSuccess(aggregatedResponse);
    });
    _engine.run(requestProcessingTask.andThen(responseAggregationTask), MUX_PLAN_CLASS);
}
Also used : IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) IndividualResponseMap(com.linkedin.restli.common.multiplexer.IndividualResponseMap) HttpCookie(java.net.HttpCookie) RestException(com.linkedin.r2.message.rest.RestException) MimeTypeParseException(javax.activation.MimeTypeParseException)

Aggregations

IndividualRequestMap (com.linkedin.restli.common.multiplexer.IndividualRequestMap)16 IndividualRequest (com.linkedin.restli.common.multiplexer.IndividualRequest)10 MultiplexedRequestContent (com.linkedin.restli.common.multiplexer.MultiplexedRequestContent)7 StringMap (com.linkedin.data.template.StringMap)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 HashMap (java.util.HashMap)3 Test (org.testng.annotations.Test)3 Callback (com.linkedin.common.callback.Callback)2 ByteString (com.linkedin.data.ByteString)1 DataMap (com.linkedin.data.DataMap)1 Task (com.linkedin.parseq.Task)1 RestException (com.linkedin.r2.message.rest.RestException)1 TestRecord (com.linkedin.restli.client.test.TestRecord)1 EmptyRecord (com.linkedin.restli.common.EmptyRecord)1 IndividualBody (com.linkedin.restli.common.multiplexer.IndividualBody)1 IndividualResponseMap (com.linkedin.restli.common.multiplexer.IndividualResponseMap)1 HttpCookie (java.net.HttpCookie)1 ArrayList (java.util.ArrayList)1 MimeTypeParseException (javax.activation.MimeTypeParseException)1