use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestAllPartitionsRequestBuilder method testSendAllPartitionsRequests.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "restliRequestOptions")
public void testSendAllPartitionsRequests(RestliRequestOptions options, RingFactory<URI> ringFactory) throws ServiceUnavailableException, URISyntaxException, RestException, InterruptedException {
final int PARTITION_NUM = 5;
List<URI> expectedUris = new ArrayList<URI>();
ConsistentHashKeyMapper mapper = getKeyToHostMapper(PARTITION_NUM, expectedUris, ringFactory);
AllPartitionsRequestBuilder<Greeting> searchRB = new AllPartitionsRequestBuilder<Greeting>(mapper);
ActionRequestBuilder<Long, Greeting> builder = new ActionRequestBuilder<Long, Greeting>(TEST_URI, Greeting.class, _COLL_SPEC, options);
ActionRequest<Greeting> request = builder.name("updateTone").id(1L).setParam(new FieldDef<Tone>("newTone", Tone.class, DataTemplateUtil.getSchema(Tone.class)), Tone.FRIENDLY).build();
final Map<String, Greeting> results = new ConcurrentHashMap<String, Greeting>();
final CountDownLatch latch = new CountDownLatch(PARTITION_NUM);
final List<Throwable> errors = new ArrayList<Throwable>();
final List<Greeting> responses = new ArrayList<Greeting>();
Callback<Response<Greeting>> cb = new Callback<Response<Greeting>>() {
@Override
public void onError(Throwable e) {
synchronized (errors) {
errors.add(e);
}
latch.countDown();
}
@Override
public void onSuccess(Response<Greeting> response) {
results.put(response.getEntity().toString(), response.getEntity());
synchronized (responses) {
responses.add(response.getEntity());
}
latch.countDown();
}
};
HostSet hostsResult = searchRB.sendRequests(getClient(), request, new RequestContext(), cb);
List<URI> uris = hostsResult.getAllHosts();
Assert.assertTrue(uris.containsAll(expectedUris));
Assert.assertTrue(expectedUris.containsAll(uris));
latch.await();
if (!errors.isEmpty()) {
Assert.fail("I knew it: " + errors.toString());
}
Assert.assertEquals(PARTITION_NUM, responses.size());
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class MultiplexedRequestHandlerImpl method handleRequest.
@Override
public void handleRequest(RestRequest request, RequestContext requestContext, final Callback<RestResponse> callback) {
if (HttpMethod.POST != HttpMethod.valueOf(request.getMethod())) {
_log.error("POST is expected, but " + request.getMethod() + " received");
callback.onError(RestException.forError(HttpStatus.S_405_METHOD_NOT_ALLOWED.getCode(), "Invalid method"));
return;
}
IndividualRequestMap individualRequests;
try {
individualRequests = extractIndividualRequests(request);
} catch (RestException e) {
_log.error("Invalid multiplexed request", e);
callback.onError(e);
return;
} catch (Exception e) {
_log.error("Invalid multiplexed request", e);
callback.onError(RestException.forError(HttpStatus.S_400_BAD_REQUEST.getCode(), e));
return;
}
// prepare the map of individual responses to be collected
final IndividualResponseMap individualResponses = new IndividualResponseMap(individualRequests.size());
final Map<String, HttpCookie> responseCookies = new HashMap<>();
// all tasks are Void and side effect based, that will be useful when we add streaming
Task<?> requestProcessingTask = createParallelRequestsTask(request, requestContext, individualRequests, individualResponses, responseCookies);
Task<Void> responseAggregationTask = Task.action("send aggregated response", () -> {
RestResponse aggregatedResponse = aggregateResponses(individualResponses, responseCookies);
callback.onSuccess(aggregatedResponse);
});
_engine.run(requestProcessingTask.andThen(responseAggregationTask), MUX_PLAN_CLASS);
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestRestLiServer method testRestRequestAttachmentsPresent.
@Test
public void testRestRequestAttachmentsPresent() throws Exception {
//This test verifies that a RestRequest sent to the RestLiServer throws an exception if the content type is multipart/related
RestRequest contentTypeMultiPartRelated = new RestRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_MULTIPART_RELATED).build();
Callback<RestResponse> callback = 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();
assertEquals(restResponse.getStatus(), 415);
assertTrue(restResponse.getEntity().length() > 0);
assertEquals(restResponse.getEntity().asString(Charset.defaultCharset()), "This server cannot handle requests with a content type of multipart/related");
}
};
_server.handleRequest(contentTypeMultiPartRelated, new RequestContext(), callback);
}
use of com.linkedin.r2.message.rest.RestException 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);
}
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestRestLiServer method testPreprocessingError.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testPreprocessingError(final ProtocolVersion protocolVersion, final String errorResponseHeaderName, final RestOrStream restOrStream) throws Exception {
//Bad key type will generate a routing error
final StatusCollectionResource statusResource = _resourceFactory.getMock(StatusCollectionResource.class);
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();
assertEquals(restResponse.getStatus(), 400);
assertTrue(restResponse.getEntity().length() > 0);
assertEquals(restResponse.getHeader(errorResponseHeaderName), RestConstants.HEADER_VALUE_ERROR);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build();
_server.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/abcd")).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);
}
}
Aggregations