Search in sources :

Example 6 with DistributionException

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");
    }
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) BufferedInputStream(java.io.BufferedInputStream) DistributionException(org.apache.sling.distribution.common.DistributionException) Nonnull(javax.annotation.Nonnull)

Example 7 with DistributionException

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);
        }
    }
}
Also used : DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) InputStream(java.io.InputStream) Request(org.apache.http.client.fluent.Request) DistributionRequest(org.apache.sling.distribution.DistributionRequest) HttpResponseException(org.apache.http.client.HttpResponseException) URI(java.net.URI) DistributionException(org.apache.sling.distribution.common.DistributionException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) RecoverableDistributionException(org.apache.sling.distribution.common.RecoverableDistributionException) HttpResponseException(org.apache.http.client.HttpResponseException) Response(org.apache.http.client.fluent.Response) Executor(org.apache.http.client.fluent.Executor) RecoverableDistributionException(org.apache.sling.distribution.common.RecoverableDistributionException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) DistributionException(org.apache.sling.distribution.common.DistributionException) RecoverableDistributionException(org.apache.sling.distribution.common.RecoverableDistributionException) AbstractDistributionPackage(org.apache.sling.distribution.packaging.impl.AbstractDistributionPackage)

Example 8 with DistributionException

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);
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DistributionException(org.apache.sling.distribution.common.DistributionException) RepositoryException(javax.jcr.RepositoryException) PackageManager(org.apache.jackrabbit.vault.packaging.PackageManager) FileOutputStream(java.io.FileOutputStream) DistributionException(org.apache.sling.distribution.common.DistributionException) File(java.io.File) FileDistributionPackage(org.apache.sling.distribution.packaging.impl.FileDistributionPackage) BufferedOutputStream(java.io.BufferedOutputStream) ImportOptions(org.apache.jackrabbit.vault.fs.io.ImportOptions) VaultPackage(org.apache.jackrabbit.vault.packaging.VaultPackage) Session(javax.jcr.Session)

Example 9 with DistributionException

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;
}
Also used : DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) DistributionPackageBuilder(org.apache.sling.distribution.packaging.DistributionPackageBuilder) DistributionException(org.apache.sling.distribution.common.DistributionException)

Example 10 with DistributionException

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();
        }
    }
}
Also used : InputStream(java.io.InputStream) Node(javax.jcr.Node) DistributionException(org.apache.sling.distribution.common.DistributionException) DistributionException(org.apache.sling.distribution.common.DistributionException) Session(javax.jcr.Session)

Aggregations

DistributionException (org.apache.sling.distribution.common.DistributionException)35 IOException (java.io.IOException)8 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)8 Session (javax.jcr.Session)6 Nonnull (javax.annotation.Nonnull)5 RepositoryException (javax.jcr.RepositoryException)5 PersistenceException (org.apache.sling.api.resource.PersistenceException)5 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)5 InputStream (java.io.InputStream)4 Resource (org.apache.sling.api.resource.Resource)4 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 DigestOutputStream (java.security.DigestOutputStream)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 LoginException (org.apache.sling.api.resource.LoginException)3 SimpleDistributionResponse (org.apache.sling.distribution.impl.SimpleDistributionResponse)3 DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)3