Search in sources :

Example 6 with RestException

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

the class AbstractEchoServiceTest method testBadRestURI.

@Test
public void testBadRestURI() {
    final EchoService client = getEchoClient(_client, URI.create("/unknown-service"));
    if (!(client instanceof RestEchoClient)) {
        return;
    }
    final String msg = "This is a simple echo message";
    final FutureCallback<String> callback = new FutureCallback<String>();
    client.echo(msg, callback);
    try {
        callback.get();
        Assert.fail("Should have thrown an exception");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof ExecutionException);
        Assert.assertTrue(e.getCause() instanceof RestException);
        RestException re = (RestException) e.getCause();
        Assert.assertEquals(re.getResponse().getStatus(), RestStatus.NOT_FOUND);
    }
}
Also used : EchoService(com.linkedin.r2.sample.echo.EchoService) RestEchoClient(com.linkedin.r2.sample.echo.rest.RestEchoClient) RestException(com.linkedin.r2.message.rest.RestException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test)

Example 7 with RestException

use of com.linkedin.r2.message.rest.RestException 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 8 with RestException

use of com.linkedin.r2.message.rest.RestException 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 9 with RestException

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

the class TestGreetingsClientProtocolVersionHeader method testNoProtocolVersionHeaderFail.

@Test
public void testNoProtocolVersionHeaderFail() throws InterruptedException {
    final TransportClientAdapter client = new TransportClientAdapter(CLIENT_FACTORY.getClient(Collections.<String, String>emptyMap()));
    final RestRequestBuilder requestBuilder = new RestRequestBuilder(URI.create(URI_PREFIX));
    final RestRequest request = requestBuilder.build();
    Assert.assertTrue(request.getHeaders().isEmpty());
    try {
        client.restRequest(request).get();
    } catch (ExecutionException e) {
        final RestException exception = (RestException) e.getCause();
        final RestResponse response = exception.getResponse();
        Assert.assertEquals(response.getStatus(), HttpStatus.S_404_NOT_FOUND.getCode());
        Assert.assertEquals(response.getHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION), AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion().toString());
        final DataMap exceptionDetail = DataMapUtils.readMap(response.getEntity().asInputStream());
        Assert.assertEquals(exceptionDetail.getString("exceptionClass"), RestLiServiceException.class.getName());
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) DataMap(com.linkedin.data.DataMap) RestLiIntegrationTest(com.linkedin.restli.examples.RestLiIntegrationTest) Test(org.testng.annotations.Test)

Example 10 with RestException

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

the class RestLiServer method respondWithPreRoutingError.

private void respondWithPreRoutingError(Throwable th, RestRequest request, RestLiAttachmentReader attachmentReader, RequestExecutionCallback<RestResponse> callback) {
    RestLiFilterResponseContextFactory<Object> filterResponseContextFactory = new RestLiFilterResponseContextFactory<Object>(request, null, _responseHandler);
    RestLiResponseData responseData = filterResponseContextFactory.fromThrowable(th).getResponseData();
    RestException restException = _responseHandler.buildRestException(th, _responseHandler.buildPartialResponse(null, responseData));
    callback.onError(restException, createEmptyExecutionReport(), attachmentReader, null);
}
Also used : RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) RestException(com.linkedin.r2.message.rest.RestException)

Aggregations

RestException (com.linkedin.r2.message.rest.RestException)62 RestResponse (com.linkedin.r2.message.rest.RestResponse)48 Test (org.testng.annotations.Test)44 RestRequest (com.linkedin.r2.message.rest.RestRequest)33 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)31 URI (java.net.URI)23 BeforeTest (org.testng.annotations.BeforeTest)21 ExecutionException (java.util.concurrent.ExecutionException)20 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)19 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)19 RequestContext (com.linkedin.r2.message.RequestContext)18 Callback (com.linkedin.common.callback.Callback)16 URISyntaxException (java.net.URISyntaxException)16 ByteString (com.linkedin.data.ByteString)15 AfterTest (org.testng.annotations.AfterTest)12 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)11 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)11 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)11 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)11 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)11