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;
}
}
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;
}
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;
}
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);
}
}
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);
}
}
Aggregations