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);
}
}
}
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);
}
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;
}
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));
}
}
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;
}
Aggregations