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