Search in sources :

Example 41 with RestException

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

the class TestMessages method testToStreamTransportCallbackStreamException.

@Test
public void testToStreamTransportCallbackStreamException() {
    TransportCallback<RestResponse> restCallback = response -> {
        Assert.assertTrue(response.hasError());
        Assert.assertNotNull(response.getError());
        Assert.assertTrue(response.getError() instanceof RestException);
        Assert.assertNotNull(response.getWireAttributes());
        Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
    };
    TransportCallback<StreamResponse> streamCallback = Messages.toStreamTransportCallback(restCallback);
    StreamResponseBuilder builder = new StreamResponseBuilder();
    StreamResponse streamResponse = builder.build(EntityStreams.newEntityStream(new ByteStringWriter(DATA)));
    streamCallback.onResponse(TransportResponseImpl.error(new StreamException(streamResponse, new IllegalStateException()), WIRE_ATTR));
}
Also used : ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Callback(com.linkedin.common.callback.Callback) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) Messages(com.linkedin.r2.message.Messages) RestResponse(com.linkedin.r2.message.rest.RestResponse) EntityStreams(com.linkedin.r2.message.stream.entitystream.EntityStreams) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) Assert(org.testng.Assert) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Map(java.util.Map) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RestException(com.linkedin.r2.message.rest.RestException) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamException(com.linkedin.r2.message.stream.StreamException) Test(org.testng.annotations.Test)

Example 42 with RestException

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

the class TestServerRetryFilter method testNestedException.

@Test
public void testNestedException() {
    String retryMessage = "this is a retry";
    ServerRetryFilter retryFilter = new ServerRetryFilter();
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
        }
    };
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, retryFilter);
    Throwable nestedException = new RetriableRequestException(retryMessage);
    for (int i = 0; i < 5; i++) {
        nestedException = new RuntimeException(nestedException);
    }
    FilterUtil.fireRestError(filterChain, new RestException(null, nestedException), new HashMap<String, String>());
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) ServerRetryFilter(com.linkedin.r2.filter.transport.ServerRetryFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) RetriableRequestException(com.linkedin.r2.RetriableRequestException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 43 with RestException

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

the class ServerCompressionFilter method onRestRequest.

/**
   * Handles compression tasks for incoming requests
   */
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
    try {
        //Check if the request is compressed, if so, decompress
        String requestContentEncoding = req.getHeader(HttpConstants.CONTENT_ENCODING);
        if (requestContentEncoding != null) {
            //This must be a specific compression type other than *
            EncodingType encoding;
            try {
                encoding = EncodingType.get(requestContentEncoding.trim().toLowerCase());
            } catch (IllegalArgumentException ex) {
                throw new CompressionException(CompressionConstants.UNSUPPORTED_ENCODING + requestContentEncoding);
            }
            if (encoding == EncodingType.ANY) {
                throw new CompressionException(CompressionConstants.REQUEST_ANY_ERROR + requestContentEncoding);
            }
            //Process the correct compression types only
            if (encoding.hasCompressor()) {
                byte[] decompressedContent = encoding.getCompressor().inflate(req.getEntity().asInputStream());
                Map<String, String> headers = new HashMap<String, String>(req.getHeaders());
                headers.remove(HttpConstants.CONTENT_ENCODING);
                headers.put(HttpConstants.CONTENT_LENGTH, Integer.toString(decompressedContent.length));
                req = req.builder().setEntity(decompressedContent).setHeaders(headers).build();
            }
        }
        //Get client support for compression and flag compress if need be
        String responseAcceptedEncodings = req.getHeader(HttpConstants.ACCEPT_ENCODING);
        if (responseAcceptedEncodings == null) {
            //Only permit identity
            responseAcceptedEncodings = EMPTY;
        }
        requestContext.putLocalAttr(HttpConstants.ACCEPT_ENCODING, responseAcceptedEncodings);
        if (!responseAcceptedEncodings.isEmpty()) {
            requestContext.putLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, _serverCompressionHelper.getResponseCompressionThreshold(req));
        }
        nextFilter.onRequest(req, requestContext, wireAttrs);
    } catch (CompressionException e) {
        //If we can't decompress the client's request, we can't do much more with it
        LOG.error(e.getMessage(), e.getCause());
        RestResponse restResponse = new RestResponseBuilder().setStatus(HttpConstants.UNSUPPORTED_MEDIA_TYPE).build();
        nextFilter.onError(new RestException(restResponse, e), requestContext, wireAttrs);
    }
}
Also used : HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException)

Example 44 with RestException

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

the class TestServerTimeout method testFilterNotCancelButShouldNotTimeout.

@Test
public void testFilterNotCancelButShouldNotTimeout() throws Exception {
    RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, STREAM_EXCEPTION_FILTER_URI)).setEntity(new byte[10240]).build();
    _client.restRequest(request);
    Future<RestResponse> futureResponse = _client.restRequest(request);
    // if server times out, our second request would fail with TimeoutException because it's blocked by first one
    try {
        futureResponse.get(SERVER_IOHANDLER_TIMEOUT / 2, TimeUnit.MILLISECONDS);
        Assert.fail("Should fail with ExecutionException");
    } catch (ExecutionException ex) {
        Assert.assertTrue(ex.getCause() instanceof RestException);
        RestException restException = (RestException) ex.getCause();
        Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("StreamException in filter."));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) 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) Test(org.testng.annotations.Test)

Example 45 with RestException

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

the class TestServerTimeoutAsyncEvent method testFilterThrowButShouldNotTimeout.

@Test
public void testFilterThrowButShouldNotTimeout() throws Exception {
    RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, BUGGY_FILTER_URI)).setEntity(new byte[10240]).build();
    _client.restRequest(request);
    Future<RestResponse> futureResponse = _client.restRequest(request);
    // if server times out, our second request would fail with TimeoutException because it's blocked by first one
    try {
        futureResponse.get(ASYNC_EVENT_TIMEOUT / 2, TimeUnit.MILLISECONDS);
        Assert.fail("Should fail with ExecutionException");
    } catch (ExecutionException ex) {
        Assert.assertTrue(ex.getCause() instanceof RestException);
        RestException restException = (RestException) ex.getCause();
        Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("Buggy filter throws."));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) 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) Test(org.testng.annotations.Test)

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