@Override protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers); if (this.httpRequest instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest; HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput); entityEnclosingRequest.setEntity(requestEntity); } final HttpResponseFutureCallback callback = new HttpResponseFutureCallback(); final Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback); return new ClientHttpResponseFuture(futureResponse, callback); }
public HttpResponse post(ChukwaRequest chukwaRequest) throws IOException { httpClient.start(); byte[] payload = chukwaRequest.toByteArray(); final HttpPost httpPost = new HttpPost(uri); final NByteArrayEntity nByteArrayEntity = new NByteArrayEntity(payload, ContentType.APPLICATION_OCTET_STREAM); httpPost.setEntity(nByteArrayEntity); final Future<HttpResponse> execute = httpClient.execute(httpPost, null); try { return execute.get(); } catch (InterruptedException | ExecutionException e) { throw new IOException(e); } }
/** * Makes an HTTP PUT request using the given URI and optional byte array body and headers. * * @param uri the URI to use for the PUT call * @param contentType The content-type. Use helper function org.apache.http.entity.ContentType.create() to generate * @param body Optional, can be null. An byte array that will be PUTed as is * @param headers Optional, can be null. One or more HTTP headers, typically created by instancing org.apache.http.message.BasicHeader * @return the resulting HttpFuture instance */ public HttpFuture doPut( URI uri, ContentType contentType, byte[] body, Header[] headers ) { HttpPut httpPut = new HttpPut( uri ); if( body != null ) { httpPut.setEntity( new NByteArrayEntity( body, contentType ) ); } if( headers != null && headers.length > 0 ) httpPut.setHeaders( headers ); return new HttpFuture( this, httpPut, asyncClient.execute( httpPut, null ), null ); }
public static HttpEntity createEntity(String json) { try { byte[] bytes = SMILE_MAPPER.writeValueAsBytes(SMILE_MAPPER.readValue(json, Object.class)); return new NByteArrayEntity(bytes, ContentType.create(APPLICATION_JACKSON_SMILE)); } catch (Exception e) { LOG.error("Cannot create entity, due to {}!", e); throw new RuntimeException("Cannot create entity!", e); } }
private static NByteArrayEntity byteBody(HttpReq config) { NByteArrayEntity entity = new NByteArrayEntity(config.body()); if (config.contentType() != null) { entity.setContentType(config.contentType()); } return entity; }
private HttpPost buildMultipartRequest(URI uri, MultiPartRequest request) throws RestEndpointIOException { HttpPost post = new HttpPost(uri); try { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (MultiPartRequest.MultiPartSerialized<?> serializedPart : request.getSerializedRQs()) { Serializer serializer = getSupportedSerializer(serializedPart); builder.addPart(serializedPart.getPartName(), new StringBody(new String(serializer.serialize(serializedPart.getRequest()), Charsets.UTF_8), ContentType.parse(serializer.getMimeType()))); } for (MultiPartRequest.MultiPartBinary partBinaty : request.getBinaryRQs()) { builder.addPart( partBinaty.getPartName(), new InputStreamBody(partBinaty.getData().openBufferedStream(), ContentType.parse(partBinaty.getContentType()), partBinaty .getFilename()) ); } /* Here is some dirty hack to avoid problem with MultipartEntity and asynchronous http client * Details can be found here: http://comments.gmane.org/gmane.comp.apache.httpclient.user/2426 * * The main idea is to replace MultipartEntity with NByteArrayEntity once first * doesn't support #getContent method * which is required for async client implementation. So, we are copying response * body as byte array to NByteArrayEntity to * leave it unmodified. * * Alse we need to add boundary value to content type header. Details are here: * http://en.wikipedia.org/wiki/Delimiter#Content_boundary * MultipartEntity generates correct header by yourself, but we need to put it * manually once we replaced entity type to NByteArrayEntity */ String boundary = "-------------" + UUID.randomUUID().toString(); builder.setBoundary(boundary); ByteArrayOutputStream baos = new ByteArrayOutputStream(); builder.build().writeTo(baos); post.setEntity(new NByteArrayEntity(baos.toByteArray(), ContentType.MULTIPART_FORM_DATA)); post.setHeader("Content-Type", "multipart/form-data;boundary=" + boundary); } catch (Exception e) { throw new RestEndpointIOException("Unable to build post multipart request", e); } return post; }