List of usage examples for org.apache.http.client.fluent Request bodyStream
public Request bodyStream(final InputStream instream, final ContentType contentType)
From source file:com.qwazr.utils.json.client.JsonClientAbstract.java
private Request setBodyString(Request request, Object bodyObject) throws JsonProcessingException { if (bodyObject == null) return request; if (bodyObject instanceof String) return request.bodyString(bodyObject.toString(), ContentType.TEXT_PLAIN); else if (bodyObject instanceof InputStream) return request.bodyStream((InputStream) bodyObject, ContentType.APPLICATION_OCTET_STREAM); else/* w w w . jav a2 s .c o m*/ return request.bodyString(JsonMapper.MAPPER.writeValueAsString(bodyObject), ContentType.APPLICATION_JSON); }
From source file:org.apache.sling.replication.transport.impl.HttpTransportHandler.java
private void deliverPackage(Executor executor, ReplicationPackage replicationPackage, ReplicationEndpoint replicationEndpoint) throws IOException { String type = replicationPackage.getType(); Request req = Request.Post(replicationEndpoint.getUri()).useExpectContinue(); if (useCustomHeaders) { String[] customizedHeaders = getCustomizedHeaders(customHeaders, replicationPackage.getAction(), replicationPackage.getPaths()); for (String header : customizedHeaders) { addHeader(req, header);//ww w .j ava 2 s . c o m } } else { req.addHeader(ReplicationHeader.TYPE.toString(), type); } InputStream inputStream = null; Response response = null; try { if (useCustomBody) { String body = customBody == null ? "" : customBody; inputStream = new ByteArrayInputStream(body.getBytes()); } else { inputStream = replicationPackage.createInputStream(); } if (inputStream != null) { req = req.bodyStream(inputStream, ContentType.APPLICATION_OCTET_STREAM); } response = executor.execute(req); } finally { IOUtils.closeQuietly(inputStream); } if (response != null) { Content content = response.returnContent(); if (log.isInfoEnabled()) { log.info("Replication content of type {} for {} delivered: {}", new Object[] { type, Arrays.toString(replicationPackage.getPaths()), content }); } } else { throw new IOException("response is empty"); } }
From source file:org.apache.sling.distribution.transport.impl.SimpleHttpDistributionTransport.java
public void deliverPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionPackage distributionPackage, @Nonnull DistributionTransportContext distributionContext) throws DistributionException { String hostAndPort = getHostAndPort(distributionEndpoint.getUri()); DistributionPackageInfo info = distributionPackage.getInfo(); URI packageOrigin = info.get(PACKAGE_INFO_PROPERTY_ORIGIN_URI, URI.class); if (packageOrigin != null && hostAndPort.equals(getHostAndPort(packageOrigin))) { log.debug("skipping distribution of package {}to same origin {}", distributionPackage.getId(), hostAndPort);/*from w w w .ja v a 2s . co m*/ } else { try { Executor executor = getExecutor(distributionContext); Request req = Request.Post(distributionEndpoint.getUri()).useExpectContinue(); // add the message body digest, see https://tools.ietf.org/html/rfc3230#section-4.3.2 if (distributionPackage instanceof AbstractDistributionPackage) { AbstractDistributionPackage adb = (AbstractDistributionPackage) distributionPackage; if (adb.getDigestAlgorithm() != null && adb.getDigestMessage() != null) { req.addHeader(DIGEST_HEADER, String.format("%s=%s", adb.getDigestAlgorithm(), adb.getDigestMessage())); } } InputStream inputStream = null; try { inputStream = DistributionPackageUtils.createStreamWithHeader(distributionPackage); req = req.bodyStream(inputStream, ContentType.APPLICATION_OCTET_STREAM); Response response = executor.execute(req); response.returnContent(); // throws an error if HTTP status is >= 300 } finally { IOUtils.closeQuietly(inputStream); } log.debug("delivered packageId={}, endpoint={}", distributionPackage.getId(), distributionEndpoint.getUri()); } catch (HttpHostConnectException e) { throw new RecoverableDistributionException( "endpoint not available " + distributionEndpoint.getUri(), e); } catch (HttpResponseException e) { int statusCode = e.getStatusCode(); if (statusCode == 404 || statusCode == 401) { throw new RecoverableDistributionException( "not enough rights for " + distributionEndpoint.getUri(), e); } throw new DistributionException(e); } catch (Exception e) { throw new DistributionException(e); } } }