/** * Add payload to request. By default, payload is sent as application/json */ protected int addPayload(RestRequestBuilder builder, String payload) { if (payload == null || payload.length() == 0) { return 0; } builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON); try { DataMap data = JACKSON_DATA_CODEC.stringToMap(payload); byte[] bytes = JACKSON_DATA_CODEC.mapToBytes(data); builder.setEntity(bytes); return bytes.length; } catch (IOException e) { throw new RuntimeException("Fail to convert payload: " + payload, e); } }
requestBuilder.setEntity(payload); requestBuilder.setHeader(CONTENT_TYPE, "binary"); requestBuilder.setHeader(CONTENT_LENGTH, "" + payload.length);
rb.setEntity(payload); rb.setHeader(CONTENT_TYPE, "binary"); rb.setHeader(CONTENT_LENGTH, "" + payload.length);
expected.setEntity(JACKSON_DATA_CODEC.mapToBytes(data));
headers.remove(HttpConstants.CONTENT_ENCODING); headers.put(HttpConstants.CONTENT_LENGTH, Integer.toString(decompressedContent.length)); req = req.builder().setEntity(decompressedContent).setHeaders(headers).build();
@Test(dataProvider = "requestCompressionData") public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent) throws CompressionException, URISyntaxException { ClientCompressionFilter clientCompressionFilter = new ClientCompressionFilter(EncodingType.SNAPPY.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Collections.<String>emptyList()); // The entity should be compressible for this test. int original = 100; byte[] entity = new byte[original]; Arrays.fill(entity, (byte)'A'); RestRequest restRequest = new RestRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).setEntity(entity).build(); int compressed = EncodingType.SNAPPY.getCompressor().deflate(new ByteArrayInputStream(entity)).length; RequestContext context = new RequestContext(); context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride); int entityLength = headerShouldBePresent ? compressed : original; String expectedContentEncoding = headerShouldBePresent ? EncodingType.SNAPPY.getHttpName() : null; clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, expectedContentEncoding, entityLength)); }
req = req.builder().setEntity(compressed).setHeader(HttpConstants.CONTENT_ENCODING, compressor.getContentEncodingName()).build();
@Override public void onSuccess(ByteString result) { RestRequest restRequest = builder.setEntity(result).build(); completable.complete(restRequest); } }));
@Override public void onSuccess(ByteString result) { RestRequest restRequest = builder.setEntity(result).build(); callback.onSuccess(restRequest); } };
@SuppressWarnings("unchecked") public static <T extends RecordTemplate> PatchRequest<T> buildPatchFromString(String patch) { PatchRequest<T> patchRequest = null; try { RestRequest restRequest = new RestRequestBuilder(new URI("/test")).setEntity(patch.getBytes()).build(); patchRequest = (PatchRequest<T>) ArgumentBuilder.extractEntity(restRequest, PatchRequest.class); } catch (URISyntaxException e) { } return patchRequest; } }
/** * Add payload to request. By default, payload is sent as application/json */ protected int addPayload(RestRequestBuilder builder, String payload) { if (payload == null || payload.length() == 0) { return 0; } builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON); try { DataMap data = JACKSON_DATA_CODEC.stringToMap(payload); byte[] bytes = JACKSON_DATA_CODEC.mapToBytes(data); builder.setEntity(bytes); return bytes.length; } catch (IOException e) { throw new RuntimeException("Fail to convert payload: " + payload, e); } }
private static void writeEntity(Request<?> request, ProtocolVersion protocolVersion, RestRequestBuilder messageBuilder) { try { DataMap dataMap = RequestBodyTransformer.transform(request, protocolVersion); messageBuilder.setEntity(CODEC.mapToBytes(dataMap)); } catch (IOException e) { throw new ExampleGenerationException("Unable to serializing data", e); } }
@Override public RestRequest readRestRequest(InputStream in) throws IOException { final RestRequestBuilder builder = new RestRequestBuilder(URI.create("")); readReqLine(builder, in); readHeaders(builder, in, HttpConstants.REQUEST_COOKIE_HEADER_NAME); builder.setEntity(readEntity(in)); return builder.build(); }
public String sendRequest(DynamicClient client, ZKConnection zkclient, String zkserver, String d2path, String cluster, String service, String method, String request, String requestType, boolean performShutdown) throws Exception { String responseString = null; URI uri = URI.create("d2://" + service + method); try { RestRequest restRequest = new RestRequestBuilder(uri).setEntity(request.getBytes("UTF-8")).build(); Future<RestResponse> response = client.restRequest(restRequest, new RequestContext()); responseString = response.get().getEntity().asString("UTF-8"); } finally { if (performShutdown) { LoadBalancerUtil.syncShutdownClient(_client, _log); zkclient.shutdown(); } } return responseString; }
builder.setEntity(entity);
private static RestRequest createSyntheticRequest(RestRequest envelopeRequest, IndividualRequest individualRequest) throws MimeTypeParseException, IOException { URI uri = URI.create(individualRequest.getRelativeUrl()); ByteString entity = getBodyAsByteString(individualRequest); return new RestRequestBuilder(uri) .setMethod(individualRequest.getMethod()) .setHeaders(individualRequest.getHeaders()) .setCookies(envelopeRequest.getCookies()) .setEntity(entity) .build(); }
@Override public void onFinished() { //Verify we actually had some parts. User attachments do not have to be present but for multipart/related //there must be atleast some payload. if (_requestPayload == null) { _streamResponseCallback.onError(Messages.toStreamException(RestException.forError(400, "Did not receive any parts in the multipart mime request!"))); return; } //At this point, this means that the multipart mime envelope didn't have any attachments (apart from the //json/pson payload). Technically the rest.li client would not create a payload like this, but to keep the protocol //somewhat flexible we will allow it. //If there had been more attachments, then onNewPart() above would be invoked and we would have passed the //attachment reader onto the framework. //It is also important to note that this callback (TopLevelReaderCallback) will no longer be used. We provide //null to the application developer since there are no attachments present. Therefore it is not possible for this //callback to ever be used again. This is a bit different then the onNewPart() case above because in that case //there is a valid non-null attachment reader provided to the resource method. In that case application developers //could call drainAllAttachments() without registering a callback which would then lead to onDrainComplete() being //invoked. _restRequestBuilder.setEntity(_requestPayload); RestRequest restRequest = _restRequestBuilder.build(); //We have no attachments so we pass null for the reader. // Debug request should have already handled by one of the request handlers. _fallback.handleResourceRequest(restRequest, _routingResult, toRestResponseCallback(_streamResponseCallback, _routingResult.getContext())); }
private RestRequest buildMultiplexedRequest(MultiplexedRequest multiplexedRequest) throws IOException { URI requestUri = new MultiplexerUriBuilder(_uriPrefix).build(); RestRequestBuilder requestBuilder = new RestRequestBuilder(requestUri).setMethod(HttpMethod.POST.toString()); addAcceptHeaders(requestBuilder, multiplexedRequest.getRequestOptions().getAcceptTypes(), false); final DataMap multiplexedPayload = multiplexedRequest.getContent().data(); final ContentType type = resolveContentType( requestBuilder, multiplexedPayload, multiplexedRequest.getRequestOptions().getContentType()); assert (type != null); requestBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey()); requestBuilder.setEntity(type.getCodec().mapToBytes(multiplexedPayload)); requestBuilder.setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion().toString()); return requestBuilder.build(); }
private RestRequest buildRestRequest(URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, ProtocolVersion protocolVersion, ContentType contentType, List<ContentType> acceptTypes, boolean acceptResponseAttachments) throws Exception { RestRequestBuilder requestBuilder = new RestRequestBuilder(uri).setMethod(method.getHttpMethod().toString()); requestBuilder.setHeaders(headers); requestBuilder.setCookies(cookies); addAcceptHeaders(requestBuilder, acceptTypes, acceptResponseAttachments); final ContentType type = resolveContentType(requestBuilder, dataMap, contentType); if (type != null) { requestBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey()); // Use unsafe wrap to avoid copying the bytes when request builder creates ByteString. requestBuilder.setEntity(ByteString.unsafeWrap(type.getCodec().mapToBytes(dataMap))); } addProtocolVersionHeader(requestBuilder, protocolVersion); if (method.getHttpMethod() == HttpMethod.POST) { requestBuilder.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, method.toString()); } return requestBuilder.build(); }