Search in sources :

Example 1 with IndividualResponse

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

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

the class TestMultiplexedCallback method testMixed.

@Test
public void testMixed() throws Exception {
    FutureCallback<RestResponse> callback1 = new FutureCallback<RestResponse>();
    FutureCallback<RestResponse> callback2 = new FutureCallback<RestResponse>();
    ImmutableMap<Integer, Callback<RestResponse>> individualCallbacks = ImmutableMap.<Integer, Callback<RestResponse>>of(ID1, callback1, ID2, callback2);
    FutureCallback<MultiplexedResponse> aggregatedCallback = new FutureCallback<MultiplexedResponse>();
    TestRecord entity1 = fakeEntity(ID1);
    IndividualResponse ir1 = fakeIndividualResponse(entity1);
    IndividualResponse ir2 = fakeIndividualErrorResponse();
    MultiplexedResponseContent responseContent = new MultiplexedResponseContent().setResponses(new IndividualResponseMap(ImmutableMap.of(Integer.toString(ID1), ir1, Integer.toString(ID2), ir2)));
    MultiplexedCallback multiplexedCallback = new MultiplexedCallback(individualCallbacks, aggregatedCallback);
    multiplexedCallback.onSuccess(fakeRestResponse(responseContent));
    assertRestResponseEquals(callback1.get(), fakeRestResponse(entity1));
    RestException actualError = (RestException) getError(callback2);
    assertRestResponseEquals(actualError.getResponse(), fakeRestErrorResponse());
    MultiplexedResponse multiplexedResponse = aggregatedCallback.get();
    Assert.assertEquals(multiplexedResponse.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals(multiplexedResponse.getHeaders(), HEADERS);
}
Also used : MultiplexedResponseContent(com.linkedin.restli.common.multiplexer.MultiplexedResponseContent) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) IndividualResponse(com.linkedin.restli.common.multiplexer.IndividualResponse) FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) TestRecord(com.linkedin.restli.client.test.TestRecord) IndividualResponseMap(com.linkedin.restli.common.multiplexer.IndividualResponseMap) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 3 with IndividualResponse

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

the class IndividualResponseConversionTask method toIndividualResponse.

private static IndividualResponse toIndividualResponse(String id, RestResponse restResponse) throws MimeTypeParseException, IOException {
    IndividualResponse individualResponse = new IndividualResponse();
    individualResponse.setStatus(restResponse.getStatus());
    individualResponse.setHeaders(new StringMap(restResponse.getHeaders()));
    ByteString entity = restResponse.getEntity();
    if (!entity.isEmpty()) {
        individualResponse.setBody(new IndividualBody(DataMapConverter.bytesToDataMap(restResponse.getHeaders(), entity)));
    }
    return individualResponse;
}
Also used : StringMap(com.linkedin.data.template.StringMap) IndividualBody(com.linkedin.restli.common.multiplexer.IndividualBody) ByteString(com.linkedin.data.ByteString) IndividualResponse(com.linkedin.restli.common.multiplexer.IndividualResponse)

Example 4 with IndividualResponse

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

the class IndividualResponseConversionTask method run.

@Override
protected Promise<? extends IndividualResponseWithCookies> run(Context context) throws Throwable {
    if (_restResponse.isFailed()) {
        return Promises.value(toErrorIndividualResponse(_restResponse.getError()));
    }
    try {
        RestResponse restResponse = _restResponse.get();
        IndividualResponse response = toIndividualResponse(_restResponseId, restResponse);
        return Promises.value(new IndividualResponseWithCookies(response, restResponse.getCookies()));
    } catch (MimeTypeParseException e) {
        return Promises.value(createInternalServerErrorResponse("Invalid content type for individual response: " + _restResponseId));
    } catch (IOException e) {
        return Promises.value(createInternalServerErrorResponse("Unable to set body for individual response: " + _restResponseId));
    } catch (Exception e) {
        return Promises.value(toErrorIndividualResponse(e));
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) RestResponse(com.linkedin.r2.message.rest.RestResponse) IOException(java.io.IOException) IndividualResponse(com.linkedin.restli.common.multiplexer.IndividualResponse) MimeTypeParseException(javax.activation.MimeTypeParseException) IOException(java.io.IOException)

Example 5 with IndividualResponse

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

the class IndividualResponseException method createErrorIndividualResponse.

private static IndividualResponse createErrorIndividualResponse(HttpStatus status, ErrorResponse errorResponse) {
    IndividualResponse response = new IndividualResponse();
    response.setStatus(status.getCode());
    if (errorResponse != null) {
        response.setBody(new IndividualBody(errorResponse.data()));
    }
    return response;
}
Also used : IndividualBody(com.linkedin.restli.common.multiplexer.IndividualBody) IndividualResponse(com.linkedin.restli.common.multiplexer.IndividualResponse)

Aggregations

IndividualResponse (com.linkedin.restli.common.multiplexer.IndividualResponse)12 RestResponse (com.linkedin.r2.message.rest.RestResponse)7 IndividualResponseMap (com.linkedin.restli.common.multiplexer.IndividualResponseMap)7 MultiplexedResponseContent (com.linkedin.restli.common.multiplexer.MultiplexedResponseContent)6 FutureCallback (com.linkedin.common.callback.FutureCallback)5 ByteString (com.linkedin.data.ByteString)5 RestException (com.linkedin.r2.message.rest.RestException)5 IOException (java.io.IOException)5 Test (org.testng.annotations.Test)5 StringMap (com.linkedin.data.template.StringMap)4 RequestContext (com.linkedin.r2.message.RequestContext)3 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 IndividualBody (com.linkedin.restli.common.multiplexer.IndividualBody)3 IndividualRequest (com.linkedin.restli.common.multiplexer.IndividualRequest)3 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)3 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Callback (com.linkedin.common.callback.Callback)2