Search in sources :

Example 26 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class LocalDistributionPackageImporter method importStream.

@Override
@Nonnull
public DistributionPackageInfo importStream(@Nonnull ResourceResolver resourceResolver, @Nonnull InputStream stream) throws DistributionException {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }
    Map<String, Object> headerInfo = new HashMap<String, Object>();
    DistributionPackageUtils.readInfo(stream, headerInfo);
    log.debug("header info: {}", headerInfo);
    Object o = headerInfo.get(DistributionPackageUtils.PROPERTY_REMOTE_PACKAGE_ID);
    String reference = o != null ? String.valueOf(o) : null;
    if (reference != null) {
        if (ReferencePackage.isReference(reference)) {
            String actualPackageId = ReferencePackage.idFromReference(reference);
            if (actualPackageId != null) {
                log.info("installing from reference {}", actualPackageId);
                DistributionPackage distributionPackage = packageBuilder.getPackage(resourceResolver, actualPackageId);
                if (distributionPackage != null) {
                    if (packageBuilder.installPackage(resourceResolver, distributionPackage)) {
                        DistributionPackageInfo info = distributionPackage.getInfo();
                        log.info("package installed {}", info);
                        eventFactory.generatePackageEvent(DistributionEventTopics.IMPORTER_PACKAGE_IMPORTED, DistributionComponentKind.IMPORTER, name, info);
                        return info;
                    } else {
                        throw new DistributionException("could not install package {}" + distributionPackage);
                    }
                } else {
                    throw new DistributionException("could not install package from reference " + actualPackageId);
                }
            } else {
                throw new DistributionException("could not install package from invalid reference " + reference);
            }
        } else {
            try {
                // TODO : see if this can be removed entirely
                stream.reset();
            } catch (IOException e) {
            // do nothing
            }
            DistributionPackageInfo packageInfo;
            Object rr = headerInfo.get("reference-required");
            boolean store = rr != null && Boolean.valueOf(rr.toString());
            if (store) {
                log.debug("storing actual package");
                DistributionPackage distributionPackage = packageBuilder.readPackage(resourceResolver, stream);
                packageInfo = distributionPackage.getInfo();
                log.info("package stored {}", packageInfo);
            } else {
                packageInfo = packageBuilder.installPackage(resourceResolver, stream);
                log.info("package installed {}", packageInfo);
            }
            eventFactory.generatePackageEvent(DistributionEventTopics.IMPORTER_PACKAGE_IMPORTED, DistributionComponentKind.IMPORTER, name, packageInfo);
            return packageInfo;
        }
    } else {
        DistributionPackageInfo packageInfo = packageBuilder.installPackage(resourceResolver, stream);
        log.info("package installed");
        eventFactory.generatePackageEvent(DistributionEventTopics.IMPORTER_PACKAGE_IMPORTED, DistributionComponentKind.IMPORTER, name, packageInfo);
        return packageInfo;
    }
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) BufferedInputStream(java.io.BufferedInputStream) HashMap(java.util.HashMap) DistributionException(org.apache.sling.distribution.common.DistributionException) IOException(java.io.IOException) Nonnull(javax.annotation.Nonnull)

Example 27 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class AbstractDistributionPackageBuilder method installPackage.

public boolean installPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionPackage distributionPackage) throws DistributionException {
    DistributionRequestType actionType = distributionPackage.getInfo().getRequestType();
    if (!type.equals(distributionPackage.getType())) {
        throw new DistributionException("not supported package type" + distributionPackage.getType());
    }
    boolean installed = false;
    if (DistributionRequestType.DELETE.equals(actionType)) {
        installed = installDeletePackage(resourceResolver, distributionPackage);
    } else if (DistributionRequestType.TEST.equals(actionType)) {
        // do nothing for test packages
        installed = true;
    } else if (DistributionRequestType.ADD.equals(actionType)) {
        installed = installAddPackage(resourceResolver, distributionPackage);
    }
    return installed;
}
Also used : DistributionRequestType(org.apache.sling.distribution.DistributionRequestType) DistributionException(org.apache.sling.distribution.common.DistributionException)

Example 28 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class AbstractDistributionPackageBuilder method createPackage.

@Nonnull
public DistributionPackage createPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest request) throws DistributionException {
    DistributionPackage distributionPackage;
    request = VltUtils.sanitizeRequest(request);
    if (DistributionRequestType.ADD.equals(request.getRequestType())) {
        distributionPackage = createPackageForAdd(resourceResolver, request);
    } else if (DistributionRequestType.DELETE.equals(request.getRequestType())) {
        distributionPackage = new SimpleDistributionPackage(request, type);
    } else if (DistributionRequestType.PULL.equals(request.getRequestType())) {
        distributionPackage = new SimpleDistributionPackage(request, type);
    } else if (DistributionRequestType.TEST.equals(request.getRequestType())) {
        distributionPackage = new SimpleDistributionPackage(request, type);
    } else {
        throw new DistributionException("unknown action type " + request.getRequestType());
    }
    DistributionPackageUtils.fillInfo(distributionPackage.getInfo(), request);
    return distributionPackage;
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionException(org.apache.sling.distribution.common.DistributionException) Nonnull(javax.annotation.Nonnull)

Example 29 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class ScheduledDistributionTrigger method register.

public void register(@Nonnull DistributionRequestHandler requestHandler) throws DistributionException {
    try {
        ScheduleOptions options = scheduler.NOW(-1, secondsInterval);
        String jobName = getJobName(requestHandler);
        options.name(jobName);
        options.canRunConcurrently(false);
        options.onLeaderOnly(true);
        boolean success = scheduler.schedule(new ScheduledDistribution(requestHandler), options);
        if (success) {
            registeredJobs.add(jobName);
        }
        log.info("handler registered {} {}", jobName, success);
    } catch (Exception e) {
        throw new DistributionException("unable to register handler " + requestHandler, e);
    }
}
Also used : ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) DistributionException(org.apache.sling.distribution.common.DistributionException) DistributionException(org.apache.sling.distribution.common.DistributionException) LoginException(org.apache.sling.api.resource.LoginException)

Example 30 with DistributionException

use of org.apache.sling.distribution.common.DistributionException in project sling by apache.

the class DistributionUtils method getResourceResolver.

public static ResourceResolver getResourceResolver(String user, String service, SlingRepository slingRepository, String subServiceName, ResourceResolverFactory resourceResolverFactory) throws DistributionException {
    ResourceResolver resourceResolver;
    try {
        Map<String, Object> authenticationInfo = new HashMap<String, Object>();
        if (subServiceName == null && user != null) {
            try {
                Session session = slingRepository.impersonateFromService(service, new SimpleCredentials(user, new char[0]), null);
                authenticationInfo.put(AUTHENTICATION_INFO_SESSION, session);
            } catch (NoSuchMethodError nsme) {
                log.warn("without sling.jcr.api 2.3.0 content will be aggregated using service {}", service);
                Session session = slingRepository.loginService(service, null);
                authenticationInfo.put(AUTHENTICATION_INFO_SESSION, session);
            }
        } else {
            authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, subServiceName);
        }
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(authenticationInfo);
        return resourceResolver;
    } catch (LoginException le) {
        throw new DistributionException(le);
    } catch (RepositoryException re) {
        throw new DistributionException(re);
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) DistributionException(org.apache.sling.distribution.common.DistributionException) RepositoryException(javax.jcr.RepositoryException) 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