Search in sources :

Example 1 with RecoverableDistributionException

use of org.apache.sling.distribution.common.RecoverableDistributionException 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 2 with RecoverableDistributionException

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

the class SimpleDistributionAgentQueueProcessor method processQueueItem.

private boolean processQueueItem(String queueName, DistributionQueueEntry queueEntry) throws DistributionException {
    boolean removeItemFromQueue = false;
    ResourceResolver agentResourceResolver = null;
    DistributionPackage distributionPackage = null;
    DistributionQueueItem queueItem = queueEntry.getItem();
    DistributionQueueItemStatus queueItemStatus = queueEntry.getStatus();
    try {
        String callingUser = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_USER, String.class);
        String requestId = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_ID, String.class);
        Long globalStartTime = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_START_TIME, Long.class);
        agentResourceResolver = DistributionUtils.getResourceResolver(callingUser, authenticationInfo.getAgentService(), authenticationInfo.getSlingRepository(), authenticationInfo.getSubServiceName(), authenticationInfo.getResourceResolverFactory());
        final long startTime = System.currentTimeMillis();
        distributionPackage = distributionPackageExporter.getPackage(agentResourceResolver, queueItem.getPackageId());
        if (distributionPackage != null) {
            final long packageSize = distributionPackage.getSize();
            DistributionPackageUtils.mergeQueueEntry(distributionPackage.getInfo(), queueEntry);
            final DistributionRequestType requestType = distributionPackage.getInfo().getRequestType();
            final String[] paths = distributionPackage.getInfo().getPaths();
            try {
                // import package
                distributionPackageImporter.importPackage(agentResourceResolver, distributionPackage);
                // generated event
                distributionEventFactory.generatePackageEvent(DistributionEventTopics.AGENT_PACKAGE_DISTRIBUTED, DistributionComponentKind.AGENT, agentName, distributionPackage.getInfo());
                removeItemFromQueue = true;
                final long endTime = System.currentTimeMillis();
                distributionLog.info("[{}] PACKAGE-DELIVERED {}: {} paths={}, importTime={}ms, execTime={}ms, size={}B", queueName, requestId, requestType, paths, endTime - startTime, endTime - globalStartTime, packageSize);
            } catch (RecoverableDistributionException e) {
                distributionLog.error("[{}] PACKAGE-FAIL {}: could not deliver {}, {}", queueName, requestId, distributionPackage.getId(), e.getMessage());
                distributionLog.debug("could not deliver package {}", distributionPackage.getId(), e);
                log.error("could not deliver package {}", distributionPackage.getId(), e);
            } catch (Throwable e) {
                distributionLog.error("[{}] PACKAGE-FAIL {}: could not deliver package {} {}", queueName, requestId, distributionPackage.getId(), e.getMessage(), e);
                log.error("could not deliver package {} from queue {}", new Object[] { distributionPackage.getId(), queueName }, e);
                if (errorQueueStrategy != null && queueItemStatus.getAttempts() > retryAttempts) {
                    removeItemFromQueue = reEnqueuePackage(distributionPackage);
                    distributionLog.info("[{}] PACKAGE-QUEUED {}: distribution package {} was enqueued to an error queue", queueName, requestId, distributionPackage.getId());
                }
            }
        } else {
            // return success if package does not exist in order to clear the queue.
            removeItemFromQueue = true;
            distributionLog.error("distribution package with id {} does not exist. the package will be skipped.", queueItem.getPackageId());
        }
    } finally {
        if (removeItemFromQueue) {
            DistributionPackageUtils.releaseOrDelete(distributionPackage, queueName);
        } else {
            DistributionPackageUtils.closeSafely(distributionPackage);
        }
        DistributionUtils.ungetResourceResolver(agentResourceResolver);
    }
    // return true if item should be removed from queue
    return removeItemFromQueue;
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionRequestType(org.apache.sling.distribution.DistributionRequestType) RecoverableDistributionException(org.apache.sling.distribution.common.RecoverableDistributionException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Aggregations

RecoverableDistributionException (org.apache.sling.distribution.common.RecoverableDistributionException)2 InputStream (java.io.InputStream)1 URI (java.net.URI)1 HttpResponseException (org.apache.http.client.HttpResponseException)1 Executor (org.apache.http.client.fluent.Executor)1 Request (org.apache.http.client.fluent.Request)1 Response (org.apache.http.client.fluent.Response)1 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 DistributionRequest (org.apache.sling.distribution.DistributionRequest)1 DistributionRequestType (org.apache.sling.distribution.DistributionRequestType)1 DistributionException (org.apache.sling.distribution.common.DistributionException)1 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)1 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)1 AbstractDistributionPackage (org.apache.sling.distribution.packaging.impl.AbstractDistributionPackage)1 DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)1 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)1