use of org.apache.sling.distribution.common.DistributionException in project sling by apache.
the class DistributionTriggerServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String secondsParameter = request.getParameter("sec");
int seconds = secondsParameter != null && secondsParameter.length() > 0 ? Integer.parseInt(secondsParameter) : DEFAULT_NUMBER_OF_SECONDS;
if (seconds > MAX_NUMBER_OF_SECONDS) {
seconds = MAX_NUMBER_OF_SECONDS;
} else if (seconds < 0) {
seconds = DEFAULT_NUMBER_OF_SECONDS;
}
DistributionTrigger distributionTrigger = request.getResource().adaptTo(DistributionTrigger.class);
// setup SSE headers
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
// needed to allow e.g. the JavaScript EventSource API to make a call from author to this server and listen for the events
// response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); // allowed origins should be explicitly configured
// response.setHeader("Access-Control-Allow-Credentials", "true");
final PrintWriter writer = response.getWriter();
DistributionRequestHandler distributionRequestHandler = new DistributionRequestHandler() {
public void handle(@Nullable ResourceResolver resourceResolver, @Nonnull DistributionRequest request) {
writeEvent(writer, request);
}
};
try {
distributionTrigger.register(distributionRequestHandler);
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
log.error("thread interrupted", e);
}
distributionTrigger.unregister(distributionRequestHandler);
} catch (DistributionException e) {
response.setStatus(400);
response.getWriter().write("error while (un)registering trigger " + e.toString());
}
}
use of org.apache.sling.distribution.common.DistributionException in project sling by apache.
the class FileVaultContentSerializer method exportToStream.
@Override
public void exportToStream(ResourceResolver resourceResolver, DistributionExportOptions exportOptions, OutputStream outputStream) throws DistributionException {
Session session = null;
try {
session = getSession(resourceResolver);
String packageGroup = PACKAGE_GROUP;
String packageName = TYPE + "_" + System.currentTimeMillis() + "_" + UUID.randomUUID();
WorkspaceFilter filter = VltUtils.createFilter(exportOptions.getRequest(), nodeFilters, propertyFilters);
ExportOptions opts = VltUtils.getExportOptions(filter, packageRoots, packageGroup, packageName, VERSION, useBinaryReferences);
log.debug("assembling package {} user {}", packageGroup + '/' + packageName + "-" + VERSION, resourceResolver.getUserID());
packaging.getPackageManager().assemble(session, opts, outputStream);
} catch (Exception e) {
throw new DistributionException(e);
} finally {
ungetSession(session);
}
}
use of org.apache.sling.distribution.common.DistributionException in project sling by apache.
the class SimpleHttpDistributionTransport method retrievePackage.
@Nullable
public RemoteDistributionPackage retrievePackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest distributionRequest, @Nonnull DistributionTransportContext distributionContext) throws DistributionException {
log.debug("pulling from {}", distributionEndpoint.getUri());
try {
URI distributionURI = RequestUtils.appendDistributionRequest(distributionEndpoint.getUri(), distributionRequest);
Executor executor = getExecutor(distributionContext);
// TODO : add queue parameter
InputStream inputStream = HttpTransportUtils.fetchNextPackage(executor, distributionURI, httpConfiguration);
if (inputStream == null) {
return null;
}
try {
final DistributionPackage responsePackage = packageBuilder.readPackage(resourceResolver, inputStream);
responsePackage.getInfo().put(PACKAGE_INFO_PROPERTY_ORIGIN_URI, distributionURI);
log.debug("pulled package with info {}", responsePackage.getInfo());
return new DefaultRemoteDistributionPackage(responsePackage, executor, distributionURI);
} finally {
IOUtils.closeQuietly(inputStream);
}
} catch (HttpHostConnectException e) {
log.debug("could not connect to {} - skipping", distributionEndpoint.getUri());
} catch (Exception ex) {
log.error("cannot retrieve packages", ex);
}
return null;
}
use of org.apache.sling.distribution.common.DistributionException in project sling by apache.
the class AbstractJcrEventTrigger method unregister.
public void unregister(@Nonnull DistributionRequestHandler requestHandler) throws DistributionException {
JcrEventDistributionTriggerListener listener = registeredListeners.get(requestHandler.toString());
if (listener != null) {
Session session;
try {
session = getSession();
session.getWorkspace().getObservationManager().removeEventListener(listener);
} catch (RepositoryException e) {
throw new DistributionException("unable to unregister handler " + requestHandler, e);
}
}
}
use of org.apache.sling.distribution.common.DistributionException in project sling by apache.
the class RemoteEventDistributionTrigger method register.
public void register(@Nonnull DistributionRequestHandler requestHandler) throws DistributionException {
try {
log.info("applying remote event distribution trigger");
ScheduleOptions options = scheduler.NOW();
options.name(getJobName(requestHandler));
options.canRunConcurrently(false);
options.onLeaderOnly(true);
scheduler.schedule(new EventBasedDistribution(requestHandler), options);
} catch (Exception e) {
throw new DistributionException("unable to register handler " + requestHandler, e);
}
}
Aggregations