Search in sources :

Example 96 with Response

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

the class MockFailedResponseFutureBuilder method buildWithErrorResponse.

private ResponseFuture<V> buildWithErrorResponse(ProtocolVersion protocolVersion) {
    int status = (_errorResponse.hasStatus()) ? _errorResponse.getStatus() : DEFAULT_HTTP_STATUS;
    // create a RestLiResponseException and wrap it in an ExecutionException that will be thrown by the ResponseFuture
    RestLiResponseException restLiResponseException = new MockRestliResponseExceptionBuilder().setErrorResponse(_errorResponse).setStatus(HttpStatus.fromCode(status)).setCookies(getCookies() == null ? Collections.emptyList() : getCookies()).setHeaders(getHeaders() == null ? new HashMap<>() : getHeaders()).build();
    ExecutionException executionException = new ExecutionException(restLiResponseException);
    Future<Response<V>> responseFuture = buildFuture(null, executionException);
    return new ResponseFutureImpl<V>(responseFuture, _errorHandlingBehavior);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) Response(com.linkedin.restli.client.Response) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ExecutionException(java.util.concurrent.ExecutionException) ResponseFutureImpl(com.linkedin.restli.internal.client.ResponseFutureImpl)

Example 97 with Response

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

the class MockFailedResponseFutureBuilder method buildWithEntity.

private ResponseFuture<V> buildWithEntity() {
    int status = getStatus();
    byte[] entity = mapToBytes(getEntity().data());
    Response<V> decodedResponse = new MockResponseBuilder<K, V>().setEntity(getEntity()).setStatus(status).setHeaders(getHeaders()).setCookies(getCookies()).setProtocolVersion(getProtocolVersion()).build();
    RestResponse restResponse = new RestResponseBuilder().setEntity(entity).setStatus(status).setHeaders(decodedResponse.getHeaders()).setCookies(CookieUtil.encodeCookies(decodedResponse.getCookies())).build();
    RestLiResponseException restLiResponseException = new RestLiResponseException(restResponse, decodedResponse, new ErrorResponse());
    ExecutionException executionException = new ExecutionException(restLiResponseException);
    Future<Response<V>> responseFuture = buildFuture(null, executionException);
    return new ResponseFutureImpl<V>(responseFuture, _errorHandlingBehavior);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestResponse(com.linkedin.r2.message.rest.RestResponse) Response(com.linkedin.restli.client.Response) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ExecutionException(java.util.concurrent.ExecutionException) ResponseFutureImpl(com.linkedin.restli.internal.client.ResponseFutureImpl)

Example 98 with Response

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

the class TestMockFailedResponseFutureBuilder method testBuildWithEntityTreatServerErrorAsSuccess.

@Test
public void testBuildWithEntityTreatServerErrorAsSuccess() {
    ResponseFuture<Greeting> future = buildWithEntity(ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS);
    try {
        Response<Greeting> response = future.getResponse();
        Assert.assertEquals(response.getEntity(), new Greeting().setId(1L).setMessage("foo"));
        Assert.assertEquals(response.getStatus(), 500);
        Assert.assertEquals(response.getError().getStatus(), 500);
    } catch (RemoteInvocationException e) {
        Assert.fail("No exception should have been thrown!");
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) Test(org.testng.annotations.Test)

Example 99 with Response

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

the class RestEchoClient method echo.

public void echo(String msg, Callback<String> callback) {
    final RestRequest req = new RestRequestBuilder(_uri).setEntity(ByteString.copyString(msg, RestEchoServer.CHARSET)).setMethod(RestMethod.POST).build();
    _client.restRequest(req, new CallbackAdapter<String, RestResponse>(callback) {

        @Override
        protected String convertResponse(RestResponse response) throws Exception {
            return response.getEntity().asString(RestEchoServer.CHARSET);
        }
    });
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString)

Example 100 with Response

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

the class FilterUtil method fireRestRequestResponse.

// Fires a request, saving the local attributes, and then fires a response with the local
// attributes.
public static void fireRestRequestResponse(FilterChain fc, RestRequest req, RestResponse res) {
    final RequestContext context = new RequestContext();
    fc.onRestRequest(req, context, emptyWireAttrs());
    fc.onRestResponse(res, context, emptyWireAttrs());
}
Also used : RequestContext(com.linkedin.r2.message.RequestContext)

Aggregations

RestResponse (com.linkedin.r2.message.rest.RestResponse)100 Test (org.testng.annotations.Test)95 RestRequest (com.linkedin.r2.message.rest.RestRequest)63 RequestContext (com.linkedin.r2.message.RequestContext)51 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)47 URI (java.net.URI)45 ByteString (com.linkedin.data.ByteString)42 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)37 RestException (com.linkedin.r2.message.rest.RestException)32 HashMap (java.util.HashMap)29 FutureCallback (com.linkedin.common.callback.FutureCallback)25 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)25 ExecutionException (java.util.concurrent.ExecutionException)25 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)24 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)22 CountDownLatch (java.util.concurrent.CountDownLatch)22 URISyntaxException (java.net.URISyntaxException)21 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)18 IOException (java.io.IOException)18 Map (java.util.Map)17