use of org.apache.sling.distribution.packaging.impl.AbstractDistributionPackage in project sling by apache.
the class SimpleHttpDistributionTransport method deliverPackage.
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);
} else {
try {
Executor executor = getExecutor(distributionContext);
Request req = Request.Post(distributionEndpoint.getUri()).connectTimeout(httpConfiguration.getConnectTimeout()).socketTimeout(httpConfiguration.getSocketTimeout()).addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE).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);
// throws an error if HTTP status is >= 300
response.returnContent();
} 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);
}
}
}
Aggregations