Search in sources :

Example 11 with RestResponseBuilder

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

the class MockRestliResponseExceptionBuilder method build.

public RestLiResponseException build() {
    String errorHeaderName = _version.equals(AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion()) ? RestConstants.HEADER_LINKEDIN_ERROR_RESPONSE : RestConstants.HEADER_RESTLI_ERROR_RESPONSE;
    Map<String, String> headers = new HashMap<String, String>();
    headers.put(errorHeaderName, "true");
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, _version.toString());
    headers.putAll(_headers);
    RestResponse restResponse = new RestResponseBuilder().setEntity(mapToBytes(_errorResponse.data())).setStatus(_errorResponse.hasStatus() ? _errorResponse.getStatus() : DEFAULT_HTTP_STATUS).setHeaders(Collections.unmodifiableMap(headers)).setCookies(Collections.unmodifiableList(CookieUtil.encodeCookies(_cookies.isEmpty() ? Collections.emptyList() : _cookies))).build();
    return new RestLiResponseException(restResponse, null, _errorResponse);
}
Also used : HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException)

Example 12 with RestResponseBuilder

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

the class MultiplexedRequestHandlerImpl method aggregateResponses.

private static RestResponse aggregateResponses(IndividualResponseMap responses, Map<String, HttpCookie> responseCookies) {
    MultiplexedResponseContent aggregatedResponseContent = new MultiplexedResponseContent();
    aggregatedResponseContent.setResponses(responses);
    byte[] aggregatedResponseData = DataMapUtils.mapToBytes(aggregatedResponseContent.data());
    return new RestResponseBuilder().setStatus(HttpStatus.S_200_OK.getCode()).setEntity(aggregatedResponseData).setCookies(CookieUtil.encodeSetCookies(new ArrayList(responseCookies.values()))).build();
}
Also used : MultiplexedResponseContent(com.linkedin.restli.common.multiplexer.MultiplexedResponseContent) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) ArrayList(java.util.ArrayList)

Example 13 with RestResponseBuilder

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

the class RestLiResponseHandler method buildResponse.

/**
   * Build a RestResponse from PartialRestResponse and RoutingResult.
   *
   * @param routingResult
   *          {@link RoutingResult}
   * @param partialResponse
   *          {@link PartialRestResponse}
   * @return
   */
public RestResponse buildResponse(final RoutingResult routingResult, 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();
        String mimeType = ((ServerResourceContext) routingResult.getContext()).getResponseMimeType();
        builder = encodeResult(mimeType, builder, dataMap);
    }
    return builder.build();
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) DataMap(com.linkedin.data.DataMap)

Example 14 with RestResponseBuilder

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

the class TestMultiplexedRequestHandlerImpl method fakeMuxRestResponse.

private static RestResponse fakeMuxRestResponse(Map<Integer, IndividualResponse> responses) throws IOException {
    IndividualResponseMap individualResponseMap = new IndividualResponseMap();
    for (Map.Entry<Integer, IndividualResponse> responseMapEntry : responses.entrySet()) {
        individualResponseMap.put(Integer.toString(responseMapEntry.getKey()), responseMapEntry.getValue());
    }
    MultiplexedResponseContent content = new MultiplexedResponseContent();
    content.setResponses(individualResponseMap);
    return new RestResponseBuilder().setStatus(HttpStatus.S_200_OK.getCode()).setEntity(CODEC.mapToBytes(content.data())).build();
}
Also used : MultiplexedResponseContent(com.linkedin.restli.common.multiplexer.MultiplexedResponseContent) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) IndividualResponseMap(com.linkedin.restli.common.multiplexer.IndividualResponseMap) Map(java.util.Map) IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) StringMap(com.linkedin.data.template.StringMap) IndividualResponseMap(com.linkedin.restli.common.multiplexer.IndividualResponseMap) TreeMap(java.util.TreeMap) IndividualResponse(com.linkedin.restli.common.multiplexer.IndividualResponse)

Example 15 with RestResponseBuilder

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

the class TestBuilders method testChainBuildRestResponseFromRestResponseBuilder.

@Test
public void testChainBuildRestResponseFromRestResponseBuilder() {
    final RestResponse res = new RestResponseBuilder().setEntity(new byte[] { 1, 2, 3, 4 }).setHeader("k1", "v1").setStatus(300).build().builder().setEntity(new byte[] { 5, 6, 7, 8 }).setHeader("k2", "v2").setStatus(400).build();
    Assert.assertEquals(new byte[] { 5, 6, 7, 8 }, res.getEntity().copyBytes());
    Assert.assertEquals("v1", res.getHeader("k1"));
    Assert.assertEquals("v2", res.getHeader("k2"));
    Assert.assertEquals(400, res.getStatus());
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) Test(org.testng.annotations.Test)

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