Search in sources :

Example 21 with StreamException

use of com.linkedin.r2.message.stream.StreamException 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 22 with StreamException

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

the class ServletHelper method writeResponseHeadersToServletResponse.

static StreamResponse writeResponseHeadersToServletResponse(TransportResponse<StreamResponse> response, HttpServletResponse resp) {
    Map<String, String> wireAttrs = response.getWireAttributes();
    for (Map.Entry<String, String> e : WireAttributeHelper.toWireAttributes(wireAttrs).entrySet()) {
        resp.setHeader(e.getKey(), e.getValue());
    }
    StreamResponse streamResponse;
    if (response.hasError()) {
        Throwable e = response.getError();
        if (e instanceof StreamException) {
            streamResponse = ((StreamException) e).getResponse();
        } else {
            streamResponse = Messages.toStreamResponse(RestStatus.responseForError(RestStatus.INTERNAL_SERVER_ERROR, e));
        }
    } else {
        streamResponse = response.getResponse();
    }
    resp.setStatus(streamResponse.getStatus());
    Map<String, String> headers = streamResponse.getHeaders();
    for (Map.Entry<String, String> e : headers.entrySet()) {
        // TODO multi-valued headers
        resp.setHeader(e.getKey(), e.getValue());
    }
    for (String cookie : streamResponse.getCookies()) {
        resp.addHeader(HttpConstants.RESPONSE_COOKIE_HEADER_NAME, cookie);
    }
    return streamResponse;
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) Map(java.util.Map) StreamException(com.linkedin.r2.message.stream.StreamException)

Example 23 with StreamException

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

the class TestRestLiServer method testPostProcessingException.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testPostProcessingException(final ProtocolVersion protocolVersion, final String errorResponseHeaderName, final RestOrStream restOrStream) throws Exception {
    //request for nested projection within string field will generate error
    final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
    EasyMock.replay(statusResource);
    Callback<RestResponse> restResponseCallback = new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse restResponse) {
            fail();
        }

        @Override
        public void onError(Throwable e) {
            assertTrue(e instanceof RestException);
            RestException restException = (RestException) e;
            RestResponse restResponse = restException.getResponse();
            try {
                assertEquals(restResponse.getStatus(), 500);
                assertTrue(restResponse.getEntity().length() > 0);
                assertEquals(restResponse.getHeader(errorResponseHeaderName), RestConstants.HEADER_VALUE_ERROR);
                EasyMock.verify(statusResource);
                EasyMock.reset(statusResource);
            } catch (Exception e2) {
                fail(e2.toString());
            }
        }
    };
    if (restOrStream == RestOrStream.REST) {
        RestRequest request = new RestRequestBuilder(new URI("/statuses/1?fields=text:(invalid)")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build();
        _server.handleRequest(request, new RequestContext(), restResponseCallback);
    } else {
        StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1?fields=text:(invalid)")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build(EntityStreams.emptyStream());
        Callback<StreamResponse> callback = new Callback<StreamResponse>() {

            @Override
            public void onSuccess(StreamResponse streamResponse) {
                fail();
            }

            @Override
            public void onError(Throwable e) {
                Messages.toRestException((StreamException) e, new Callback<RestException>() {

                    @Override
                    public void onError(Throwable e) {
                        Assert.fail();
                    }

                    @Override
                    public void onSuccess(RestException result) {
                        restResponseCallback.onError(result);
                    }
                });
            }
        };
        _server.handleRequest(streamRequest, new RequestContext(), callback);
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RestException(com.linkedin.r2.message.rest.RestException) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URISyntaxException(java.net.URISyntaxException) StreamException(com.linkedin.r2.message.stream.StreamException) ParseException(javax.mail.internet.ParseException) RestException(com.linkedin.r2.message.rest.RestException) IOException(java.io.IOException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 24 with StreamException

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

the class TestRestLiServer method testMultipartRelatedNoAttachmentsAtAll.

@Test
public void testMultipartRelatedNoAttachmentsAtAll() throws Exception {
    //This test verifies the server's ability to throw an exception if there are absolutely no attachments at all
    //in the request. The protocol allows no user attachments to be required, but there must always be at least a rest.li
    //payload in the first part.
    final MultiPartMIMEWriter.Builder builder = new MultiPartMIMEWriter.Builder();
    final StreamRequest streamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(new URI("/doesNotMatter"), "related", builder.build(), Collections.<String, String>emptyMap(), "POST", ImmutableMap.of(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_MULTIPART_RELATED), Collections.emptyList());
    final Callback<StreamResponse> callback = new Callback<StreamResponse>() {

        @Override
        public void onSuccess(StreamResponse streamResponse) {
            Assert.fail();
        }

        @Override
        public void onError(Throwable e) {
            //Verify the exception.
            assertTrue(e instanceof StreamException);
            StreamException streamException = (StreamException) e;
            StreamResponse streamResponse = streamException.getResponse();
            assertEquals(streamResponse.getStatus(), 400);
            final FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {

                @Override
                public void onError(Throwable e) {
                    Assert.fail();
                }

                @Override
                public void onSuccess(ByteString result) {
                    //We have the body so assert that the exception made it.
                    assertTrue(result.length() > 0);
                    assertTrue(result.asString(Charset.defaultCharset()).contains("Did not receive any parts in the multipart mime request"));
                }
            });
            streamResponse.getEntityStream().setReader(fullEntityReader);
        }
    };
    _server.handleRequest(streamRequest, new RequestContext(), callback);
}
Also used : ByteString(com.linkedin.data.ByteString) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ErrorResponseBuilder(com.linkedin.restli.internal.server.response.ErrorResponseBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) StreamException(com.linkedin.r2.message.stream.StreamException) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 25 with StreamException

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

the class TestRestLiServer method testSyncNullObject404.

@Test(dataProvider = "restOrStream")
public void testSyncNullObject404(final RestOrStream restOrStream) throws Exception {
    final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.get(eq(1L))).andReturn(null).once();
    EasyMock.replay(statusResource);
    Callback<RestResponse> restResponseCallback = new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse restResponse) {
            fail("We should not get a success here. The server should have returned a 404!");
        }

        @Override
        public void onError(Throwable e) {
            RestException restException = (RestException) e;
            assertEquals(restException.getResponse().getStatus(), 404, "We should get a 404 back here!");
            EasyMock.verify(statusResource);
            EasyMock.reset(statusResource);
        }
    };
    if (restOrStream == RestOrStream.REST) {
        RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
        _server.handleRequest(request, new RequestContext(), restResponseCallback);
    } else {
        StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
        Callback<StreamResponse> callback = new Callback<StreamResponse>() {

            @Override
            public void onSuccess(StreamResponse streamResponse) {
                fail("We should not get a success here. The server should have returned a 404!");
            }

            @Override
            public void onError(Throwable e) {
                Messages.toRestException((StreamException) e, new Callback<RestException>() {

                    @Override
                    public void onError(Throwable e) {
                        Assert.fail();
                    }

                    @Override
                    public void onSuccess(RestException result) {
                        restResponseCallback.onError(result);
                    }
                });
            }
        };
        _server.handleRequest(streamRequest, new RequestContext(), callback);
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RestException(com.linkedin.r2.message.rest.RestException) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

StreamException (com.linkedin.r2.message.stream.StreamException)24 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)24 Test (org.testng.annotations.Test)23 Callback (com.linkedin.common.callback.Callback)19 RequestContext (com.linkedin.r2.message.RequestContext)17 RestResponse (com.linkedin.r2.message.rest.RestResponse)17 RestRequest (com.linkedin.r2.message.rest.RestRequest)15 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)15 RestException (com.linkedin.r2.message.rest.RestException)14 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)14 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)13 ByteString (com.linkedin.data.ByteString)12 URI (java.net.URI)12 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)11 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)11 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)11 AfterTest (org.testng.annotations.AfterTest)10 BeforeTest (org.testng.annotations.BeforeTest)10 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)9 AsyncStatusCollectionResource (com.linkedin.restli.server.twitter.AsyncStatusCollectionResource)8