use of org.apache.sling.distribution.common.DistributionException in project sling by apache.
the class AbstractDistributionPackageBuilder method installPackage.
@Nonnull
@Override
public DistributionPackageInfo installPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull InputStream stream) throws DistributionException {
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
DistributionPackageInfo packageInfo = new DistributionPackageInfo(type);
DistributionPackageUtils.readInfo(stream, packageInfo);
DistributionPackage distributionPackage = SimpleDistributionPackage.fromStream(stream, type);
boolean installed;
// not a simple package
if (distributionPackage == null) {
installed = installPackageInternal(resourceResolver, stream);
} else {
installed = installPackage(resourceResolver, distributionPackage);
packageInfo.putAll(distributionPackage.getInfo());
}
if (installed) {
return packageInfo;
} else {
throw new DistributionException("could not install package from stream");
}
}
use of org.apache.sling.distribution.common.DistributionException 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);
}
}
}
use of org.apache.sling.distribution.common.DistributionException in project sling by apache.
the class FileVaultContentSerializer method importFromStream.
@Override
public void importFromStream(ResourceResolver resourceResolver, InputStream inputStream) throws DistributionException {
Session session = null;
OutputStream outputStream = null;
File file = null;
boolean isTmp = true;
try {
session = getSession(resourceResolver);
ImportOptions importOptions = VltUtils.getImportOptions(aclHandling, importMode, autosaveThreshold);
if (inputStream instanceof FileDistributionPackage.PackageInputStream) {
file = ((FileDistributionPackage.PackageInputStream) inputStream).getFile();
isTmp = false;
} else {
file = File.createTempFile("distrpck-tmp-" + System.nanoTime(), "." + TYPE);
}
outputStream = new BufferedOutputStream(new FileOutputStream(file));
IOUtils.copy(inputStream, outputStream);
IOUtils.closeQuietly(outputStream);
PackageManager packageManager = packaging.getPackageManager();
VaultPackage vaultPackage = packageManager.open(file);
vaultPackage.extract(session, importOptions);
vaultPackage.close();
} catch (Exception e) {
throw new DistributionException(e);
} finally {
IOUtils.closeQuietly(outputStream);
if (isTmp) {
FileUtils.deleteQuietly(file);
}
ungetSession(session);
}
}
use of org.apache.sling.distribution.common.DistributionException in project sling by apache.
the class DistributionAgentQueueServlet method getPackage.
private DistributionPackage getPackage(ResourceResolver resourceResolver, DistributionQueueItem item) {
DistributionPackageInfo info = DistributionPackageUtils.fromQueueItem(item);
String type = info.getType();
DistributionPackageBuilder packageBuilder = packageBuilderProvider.getPackageBuilder(type);
if (packageBuilder != null) {
try {
return packageBuilder.getPackage(resourceResolver, item.getPackageId());
} catch (DistributionException e) {
log.error("cannot get package", e);
}
}
return null;
}
use of org.apache.sling.distribution.common.DistributionException in project sling by apache.
the class RepositoryDistributionPackageImporter method importPackage.
public void importPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionPackage distributionPackage) throws DistributionException {
Session session = null;
try {
session = authenticate();
int lastSlash = distributionPackage.getId().lastIndexOf('/');
String nodeName = Text.escape(lastSlash < 0 ? distributionPackage.getId() : distributionPackage.getId().substring(lastSlash + 1));
log.debug("importing package {} in {}", distributionPackage.getId(), nodeName);
if (session != null) {
Node addedNode = session.getNode(path).addNode(nodeName, NodeType.NT_FILE);
Node contentNode = addedNode.addNode(JcrConstants.JCR_CONTENT, NodeType.NT_RESOURCE);
if (contentNode != null) {
InputStream inputStream = null;
try {
inputStream = distributionPackage.createInputStream();
contentNode.setProperty(JcrConstants.JCR_DATA, session.getValueFactory().createBinary(inputStream));
contentNode.setProperty("package.type", distributionPackage.getType());
session.save();
} finally {
IOUtils.closeQuietly(inputStream);
}
}
log.debug("package {} imported into the repository as node {} ", distributionPackage.getId(), addedNode.getPath());
} else {
throw new Exception("could not get a Session to deliver package to the repository");
}
} catch (Exception e) {
throw new DistributionException(e);
} finally {
if (session != null) {
session.logout();
}
}
}
Aggregations