Search in sources :

Example 36 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder 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 37 with RestResponseBuilder

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

the class RestEchoServer method handleRequest.

public void handleRequest(RestRequest request, RequestContext requestContext, Callback<RestResponse> callback) {
    final String msg;
    try {
        msg = IOUtil.toString(request.getEntity().asInputStream(), CHARSET.name());
    } catch (IOException ex) {
        callback.onError(ex);
        return;
    }
    _echoService.echo(msg, new CallbackAdapter<RestResponse, String>(callback) {

        @Override
        protected RestResponse convertResponse(String responseMsg) {
            return new RestResponseBuilder().setEntity(ByteString.copyString(responseMsg, CHARSET)).setHeader("Content-Type", "text/plain").build();
        }
    });
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) ByteString(com.linkedin.data.ByteString) IOException(java.io.IOException)

Example 38 with RestResponseBuilder

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

the class RestLiResponseHandler method buildRestException.

public RestException buildRestException(final Throwable e, PartialRestResponse partialResponse) {
    List<String> cookies = CookieUtil.encodeSetCookies(partialResponse.getCookies());
    RestResponseBuilder builder = new RestResponseBuilder().setHeaders(partialResponse.getHeaders()).setCookies(cookies).setStatus(partialResponse.getStatus().getCode());
    if (partialResponse.hasData()) {
        DataMap dataMap = partialResponse.getDataMap();
        ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
        // partialResponse.getSchema()
        DataMapUtils.write(dataMap, null, baos, true);
        builder.setEntity(baos.toByteArray());
    }
    RestResponse restResponse = builder.build();
    RestException restException = new RestException(restResponse, e);
    return restException;
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataMap(com.linkedin.data.DataMap)

Example 39 with RestResponseBuilder

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

the class RetryTrackerClient method restRequest.

@Override
public void restRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<RestResponse> callback) {
    TransportResponse<RestResponse> response;
    if (_uri.toString().startsWith("http://test.linkedin.com/retry")) {
        RetriableRequestException ex = new RetriableRequestException("Data not available");
        response = TransportResponseImpl.error(ex);
    } else if (_uri.toString().equals("http://test.linkedin.com/bad")) {
        response = TransportResponseImpl.error(RestException.forError(404, "exception happens"), wireAttrs);
    } else {
        response = TransportResponseImpl.success(new RestResponseBuilder().build(), wireAttrs);
    }
    callback.onResponse(response);
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder)

Example 40 with RestResponseBuilder

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

the class Messages method toRestResponse.

/**
   * Converts a StreamResponse to RestResponse
   * @param streamResponse the stream request to be converted
   * @param callback the callback to be invoked when the rest response is constructed
   * @param addContentLengthHeader whether the rest response should have content-length header
   */
public static void toRestResponse(StreamResponse streamResponse, final Callback<RestResponse> callback, final boolean addContentLengthHeader) {
    final RestResponseBuilder builder = new RestResponseBuilder(streamResponse);
    Callback<ByteString> assemblyCallback = new Callback<ByteString>() {

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

        @Override
        public void onSuccess(ByteString result) {
            if (addContentLengthHeader) {
                builder.setHeader(HttpConstants.CONTENT_LENGTH, String.valueOf(result.length()));
            }
            RestResponse restResponse = builder.setEntity(result).build();
            callback.onSuccess(restResponse);
        }
    };
    streamResponse.getEntityStream().setReader(new FullEntityReader(assemblyCallback));
}
Also used : FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) Callback(com.linkedin.common.callback.Callback) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) ByteString(com.linkedin.data.ByteString) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder)

Aggregations

RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)67 Test (org.testng.annotations.Test)45 RestResponse (com.linkedin.r2.message.rest.RestResponse)41 RestException (com.linkedin.r2.message.rest.RestException)23 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)14 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)14 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)14 BeforeTest (org.testng.annotations.BeforeTest)14 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)13 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)11 RoutingException (com.linkedin.restli.server.RoutingException)10 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)10 EmptyRecord (com.linkedin.restli.common.EmptyRecord)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 RestRequest (com.linkedin.r2.message.rest.RestRequest)8 HashMap (java.util.HashMap)8 RequestContext (com.linkedin.r2.message.RequestContext)7 ByteString (com.linkedin.data.ByteString)6 RecordTemplate (com.linkedin.data.template.RecordTemplate)6 Map (java.util.Map)6