headers.remove(HttpConstants.CONTENT_ENCODING); headers.put(HttpConstants.CONTENT_LENGTH, Integer.toString(inflated.length)); res = res.builder().setEntity(inflated).setHeaders(headers).build();
@Test(dataProvider = "headersData") public void testResponseCompressionRules(String acceptEncoding, int compressionThreshold, EncodingType expectedContentEncoding) throws CompressionException, URISyntaxException { ServerCompressionFilter serverCompressionFilter = new ServerCompressionFilter(ACCEPT_COMPRESSIONS); RequestContext context = new RequestContext(); context.putLocalAttr(HttpConstants.ACCEPT_ENCODING, acceptEncoding); context.putLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, compressionThreshold); int originalLength = 100; byte[] entity = new byte[originalLength]; Arrays.fill(entity, (byte) 'A'); int compressedLength = (expectedContentEncoding == null) ? originalLength : expectedContentEncoding.getCompressor().deflate(new ByteArrayInputStream(entity)).length; String expectedContentEncodingName = (expectedContentEncoding == null) ? null : expectedContentEncoding.getHttpName(); RestResponse restResponse = new RestResponseBuilder().setEntity(entity).build(); serverCompressionFilter.onRestResponse(restResponse, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, expectedContentEncodingName, compressedLength)); } }
@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); } };
private static RestResponse createErrorRestResponse(ErrorResponse errorResponse) { RestResponseBuilder builder = new RestResponseBuilder().setStatus(errorResponse.getStatus()); String errorMessage = errorResponse.getMessage(); if (errorMessage != null) { builder.setEntity(errorMessage.getBytes()); } return builder.build(); } }
/** * Return a new {@link RestResponse} message with the specified status code. * * @param status the status code to use for the response message. * @param detail a detail message to be included as the body of the {@link RestResponse}. * @return a new {@link RestResponse} constructed as described above. */ public static RestResponse responseForStatus(int status, String detail) { if (detail == null) { detail = "No detailed message"; } // TODO: set charset in Content-Type header return new RestResponseBuilder() .setStatus(status) .setEntity(detail.getBytes(UTF8)) .setHeader("Content-Type", "text/plain") .build(); }
/** * @return {@link RestResponse} based on this exception */ public RestResponse buildRestResponse() { RestResponseBuilder builder = new RestResponseBuilder().setStatus(_status); if (getMessage() != null) { builder.setEntity(ByteString.copyString(getMessage(), Charset.defaultCharset())); } return builder.build(); } }
builder.setEntity(entity);
@Override public RestResponse readRestResponse(InputStream in) throws IOException { final RestResponseBuilder builder = new RestResponseBuilder(); readResLine(builder, in); readHeaders(builder, in, HttpConstants.RESPONSE_COOKIE_HEADER_NAME); builder.setEntity(readEntity(in)); return builder.build(); }
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(); } }
private static RestResponseBuilder encodeResult(String mimeType, RestResponseBuilder builder, DataMap dataMap) { try { ContentType type = ContentType.getContentType(mimeType).orElseThrow( () -> new RestLiServiceException(HttpStatus.S_406_NOT_ACCEPTABLE, "Requested mime type for encoding is not supported. Mimetype: " + mimeType)); assert type != null; builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey()); // Use unsafe wrap to avoid copying the bytes when request builder creates ByteString. builder.setEntity(ByteString.unsafeWrap(DataMapUtils.mapToBytes(dataMap, type.getCodec()))); } catch (MimeTypeParseException e) { throw new RestLiServiceException(HttpStatus.S_406_NOT_ACCEPTABLE, "Invalid mime type: " + mimeType); } return builder; }
headers.remove(HttpConstants.CONTENT_ENCODING); headers.put(HttpConstants.CONTENT_LENGTH, Integer.toString(inflated.length)); res = res.builder().setEntity(inflated).setHeaders(headers).build();
setStatus(HttpStatus.S_200_OK.getCode()). setHeader(RestConstants.HEADER_CONTENT_TYPE, renderer.getMIMEType()). setEntity(out.toByteArray()). build();
public static RestException buildRestException(RestLiResponseException restLiResponseException) { RestLiResponse restLiResponse = restLiResponseException.getRestLiResponse(); RestResponseBuilder responseBuilder = new RestResponseBuilder() .setHeaders(restLiResponse.getHeaders()) .setCookies(CookieUtil.encodeSetCookies(restLiResponse.getCookies())) .setStatus(restLiResponse.getStatus().getCode()); if (restLiResponse.hasData()) { DataMap dataMap = restLiResponse.getDataMap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); DataMapUtils.write(dataMap, null, baos, true); responseBuilder.setEntity(ByteString.unsafeWrap(baos.toByteArray())); // TODO: Error response not always built in requested content type. responseBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, ContentType.JSON.getHeaderKey()); } RestResponse restResponse = responseBuilder.build(); return new RestException(restResponse, restLiResponseException.getCause()); }
private static RestResponse buildIndividualRestResponse(Response<?> envelopeResponse, IndividualResponse individualResponse) throws IOException, MimeTypeParseException { IndividualBody body = individualResponse.getBody(GetMode.NULL); ByteString entity = (body != null) ? DataMapConverter.dataMapToByteString(individualResponse.getHeaders(), body.data()) : ByteString.empty(); return new RestResponseBuilder() .setStatus(individualResponse.getStatus()) .setHeaders(inheritHeaders(individualResponse, envelopeResponse)) .setCookies(CookieUtil.encodeSetCookies(envelopeResponse.getCookies())) .setEntity(entity) .build(); }