Search in sources :

Example 31 with DistributionException

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());
    }
}
Also used : DistributionRequestHandler(org.apache.sling.distribution.trigger.DistributionRequestHandler) DistributionRequest(org.apache.sling.distribution.DistributionRequest) Nonnull(javax.annotation.Nonnull) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionTrigger(org.apache.sling.distribution.trigger.DistributionTrigger) DistributionException(org.apache.sling.distribution.common.DistributionException) Nullable(javax.annotation.Nullable) PrintWriter(java.io.PrintWriter)

Example 32 with DistributionException

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);
    }
}
Also used : DistributionExportOptions(org.apache.sling.distribution.serialization.DistributionExportOptions) ExportOptions(org.apache.jackrabbit.vault.packaging.ExportOptions) WorkspaceFilter(org.apache.jackrabbit.vault.fs.api.WorkspaceFilter) DistributionException(org.apache.sling.distribution.common.DistributionException) DistributionException(org.apache.sling.distribution.common.DistributionException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Example 33 with DistributionException

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;
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) AbstractDistributionPackage(org.apache.sling.distribution.packaging.impl.AbstractDistributionPackage) Executor(org.apache.http.client.fluent.Executor) InputStream(java.io.InputStream) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) 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) Nullable(javax.annotation.Nullable)

Example 34 with DistributionException

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

Example 35 with DistributionException

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);
    }
}
Also used : ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) DistributionException(org.apache.sling.distribution.common.DistributionException) DistributionException(org.apache.sling.distribution.common.DistributionException) IOException(java.io.IOException)

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