Search in sources :

Example 11 with DistributionPackage

use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.

the class DistributionPackageExporterServlet method exportOnePackage.

private void exportOnePackage(final SlingHttpServletRequest request, final SlingHttpServletResponse response, final boolean delete) throws ServletException, IOException {
    DistributionPackageExporter distributionPackageExporter = request.getResource().adaptTo(DistributionPackageExporter.class);
    final long start = System.currentTimeMillis();
    response.setContentType(ContentType.APPLICATION_OCTET_STREAM.toString());
    DistributionRequest distributionRequest = RequestUtils.fromServletRequest(request);
    ResourceResolver resourceResolver = request.getResourceResolver();
    final AtomicInteger fetched = new AtomicInteger(0);
    try {
        // get all items
        distributionPackageExporter.exportPackages(resourceResolver, distributionRequest, new DistributionPackageProcessor() {

            @Override
            public void process(DistributionPackage distributionPackage) {
                fetched.incrementAndGet();
                InputStream inputStream = null;
                int bytesCopied = -1;
                try {
                    inputStream = DistributionPackageUtils.createStreamWithHeader(distributionPackage);
                    bytesCopied = IOUtils.copy(inputStream, response.getOutputStream());
                } catch (IOException e) {
                    log.error("cannot process package", e);
                } finally {
                    IOUtils.closeQuietly(inputStream);
                }
                String packageId = distributionPackage.getId();
                if (delete) {
                    // delete the package permanently
                    distributionPackage.delete();
                }
                // everything ok
                response.setStatus(200);
                log.debug("exported package {} was sent (and deleted={}), bytes written {}", new Object[] { packageId, delete, bytesCopied });
            }

            @Override
            public List<DistributionResponse> getAllResponses() {
                return null;
            }

            @Override
            public int getPackagesCount() {
                return 0;
            }

            @Override
            public long getPackagesSize() {
                return 0;
            }
        });
        if (fetched.get() > 0) {
            long end = System.currentTimeMillis();
            log.info("Processed distribution export request in {} ms: : fetched {}", new Object[] { end - start, fetched });
        } else {
            response.setStatus(204);
            log.debug("nothing to fetch");
        }
    } catch (Throwable e) {
        response.setStatus(503);
        log.error("error while exporting package", e);
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionRequest(org.apache.sling.distribution.DistributionRequest) DistributionPackageProcessor(org.apache.sling.distribution.packaging.DistributionPackageProcessor) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) List(java.util.List)

Example 12 with DistributionPackage

use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.

the class DistributionPackageExporterServlet method deletePackage.

private void deletePackage(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws DistributionException {
    DistributionPackageExporter distributionPackageExporter = request.getResource().adaptTo(DistributionPackageExporter.class);
    ResourceResolver resourceResolver = request.getResourceResolver();
    String id = request.getParameter("id");
    DistributionPackage distributionPackage = distributionPackageExporter.getPackage(resourceResolver, id);
    if (distributionPackage != null) {
        distributionPackage.delete();
        log.debug("exported package {} was deleted", distributionPackage.getId());
        response.setStatus(200);
    } else {
        response.setStatus(204);
        log.debug("nothing to delete {}", id);
    }
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter)

Example 13 with DistributionPackage

use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.

the class DistributionAgentQueueServlet method addItems.

private void addItems(ResourceResolver resourceResolver, DistributionQueue targetQueue, DistributionQueue sourceQueue, String[] ids) {
    for (String id : ids) {
        DistributionQueueEntry entry = sourceQueue.getItem(id);
        if (entry != null) {
            targetQueue.add(entry.getItem());
            DistributionPackage distributionPackage = getPackage(resourceResolver, entry.getItem());
            DistributionPackageUtils.acquire(distributionPackage, targetQueue.getName());
        }
    }
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry)

Example 14 with DistributionPackage

use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.

the class AvroContentSerializerTest method testBuildAndInstallOnSingleDeepPath.

@Test
public void testBuildAndInstallOnSingleDeepPath() throws Exception {
    String type = "avro";
    DistributionContentSerializer contentSerializer = new AvroContentSerializer(type);
    String tempFilesFolder = "target";
    String[] nodeFilters = new String[0];
    String[] propertyFilters = new String[0];
    DistributionPackageBuilder packageBuilder = new FileDistributionPackageBuilder(type, contentSerializer, tempFilesFolder, null, nodeFilters, propertyFilters);
    DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, true, "/libs");
    DistributionPackage distributionPackage = packageBuilder.createPackage(resourceResolver, request);
    Resource resource = resourceResolver.getResource("/libs/sub");
    resourceResolver.delete(resource);
    resourceResolver.commit();
    assertTrue(packageBuilder.installPackage(resourceResolver, distributionPackage));
    assertNotNull(resourceResolver.getResource("/libs"));
    assertNotNull(resourceResolver.getResource("/libs/sub"));
    assertNotNull(resourceResolver.getResource("/libs/sameLevel"));
}
Also used : FileDistributionPackageBuilder(org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) FileDistributionPackageBuilder(org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder) DistributionPackageBuilder(org.apache.sling.distribution.packaging.DistributionPackageBuilder) Resource(org.apache.sling.api.resource.Resource) DistributionContentSerializer(org.apache.sling.distribution.serialization.DistributionContentSerializer) Test(org.junit.Test)

Example 15 with DistributionPackage

use of org.apache.sling.distribution.packaging.DistributionPackage in project sling by apache.

the class QueueingDistributionPackageProcessorTest method testProcess.

@Test
public void testProcess() throws Exception {
    String callingUser = "mr-who-cares";
    String requestId = "id231";
    long startTime = System.currentTimeMillis();
    DistributionEventFactory eventFactory = mock(DistributionEventFactory.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionPackage distributionPackage = mock(DistributionPackage.class);
    DistributionQueueDispatchingStrategy scheduleQueueStrategy = mock(DistributionQueueDispatchingStrategy.class);
    // assume scheduling works
    List<DistributionQueueItemStatus> statuses = new LinkedList<DistributionQueueItemStatus>();
    DistributionQueueItemStatus qis = new DistributionQueueItemStatus(DistributionQueueItemState.QUEUED, "queue-1");
    statuses.add(qis);
    when(scheduleQueueStrategy.add(distributionPackage, queueProvider)).thenReturn(statuses);
    DefaultDistributionLog log = mock(DefaultDistributionLog.class);
    String agentName = "dummy";
    QueueingDistributionPackageProcessor exporterProcessor = new QueueingDistributionPackageProcessor(callingUser, requestId, startTime, eventFactory, scheduleQueueStrategy, queueProvider, log, agentName);
    DistributionPackageInfo info = new DistributionPackageInfo("type-a", new HashMap<String, Object>());
    when(distributionPackage.getInfo()).thenReturn(info);
    exporterProcessor.process(distributionPackage);
}
Also used : DistributionQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) DistributionEventFactory(org.apache.sling.distribution.event.impl.DistributionEventFactory) LinkedList(java.util.LinkedList) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) Test(org.junit.Test)

Aggregations

DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)59 Test (org.junit.Test)41 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)25 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)21 DistributionRequest (org.apache.sling.distribution.DistributionRequest)17 SimpleDistributionRequest (org.apache.sling.distribution.SimpleDistributionRequest)17 DistributionPackageBuilder (org.apache.sling.distribution.packaging.DistributionPackageBuilder)16 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)12 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)12 HashMap (java.util.HashMap)11 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)10 DistributionException (org.apache.sling.distribution.common.DistributionException)9 DistributionPackageProcessor (org.apache.sling.distribution.packaging.DistributionPackageProcessor)9 SlingRepository (org.apache.sling.jcr.api.SlingRepository)9 InputStream (java.io.InputStream)8 DistributionEventFactory (org.apache.sling.distribution.event.impl.DistributionEventFactory)8 FileDistributionPackageBuilder (org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder)8 Resource (org.apache.sling.api.resource.Resource)7 DistributionPackageExporter (org.apache.sling.distribution.packaging.DistributionPackageExporter)7 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)7