Search in sources :

Example 1 with DistributionPackageProcessor

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

the class SimpleDistributionAgent method exportPackages.

private CompositeDistributionResponse exportPackages(ResourceResolver agentResourceResolver, DistributionRequest distributionRequest, String callingUser, String requestId) throws DistributionException {
    final long startTime = System.currentTimeMillis();
    // callback function
    DistributionPackageProcessor packageProcessor;
    if (DistributionRequestType.TEST.equals(distributionRequest.getRequestType())) {
        // test packages do not get passed to the queues and get imported immediately
        packageProcessor = new ImportingDistributionPackageProcessor(distributionPackageImporter, agentAuthenticationInfo, callingUser, requestId, log);
    } else {
        packageProcessor = new QueueingDistributionPackageProcessor(callingUser, requestId, startTime, distributionEventFactory, scheduleQueueStrategy, queueProvider, log, name);
    }
    // export packages
    distributionPackageExporter.exportPackages(agentResourceResolver, distributionRequest, packageProcessor);
    final long endTime = System.currentTimeMillis();
    generatePackageEvent(DistributionEventTopics.AGENT_PACKAGE_CREATED);
    // create response
    List<DistributionResponse> distributionResponses = packageProcessor.getAllResponses();
    int packagesCount = packageProcessor.getPackagesCount();
    long packagesSize = packageProcessor.getPackagesSize();
    return new CompositeDistributionResponse(distributionResponses, packagesCount, packagesSize, endTime - startTime);
}
Also used : DistributionPackageProcessor(org.apache.sling.distribution.packaging.DistributionPackageProcessor) CompositeDistributionResponse(org.apache.sling.distribution.impl.CompositeDistributionResponse) DistributionResponse(org.apache.sling.distribution.DistributionResponse) SimpleDistributionResponse(org.apache.sling.distribution.impl.SimpleDistributionResponse) CompositeDistributionResponse(org.apache.sling.distribution.impl.CompositeDistributionResponse)

Example 2 with DistributionPackageProcessor

use of org.apache.sling.distribution.packaging.DistributionPackageProcessor 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 3 with DistributionPackageProcessor

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

the class SimpleDistributionAgentTest method testDistributionWithDisallowedRoot.

@Test
public void testDistributionWithDisallowedRoot() throws Exception {
    String name = "sample-agent";
    DistributionPackageImporter packageImporter = mock(DistributionPackageImporter.class);
    DistributionPackageExporter packageExporter = mock(DistributionPackageExporter.class);
    DistributionRequestAuthorizationStrategy packageExporterStrategy = mock(DistributionRequestAuthorizationStrategy.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueueDispatchingStrategy queueDistributionStrategy = mock(DistributionQueueDispatchingStrategy.class);
    DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    when(queueDistributionStrategy.add(any(DistributionPackage.class), any(DistributionQueueProvider.class))).thenReturn(Collections.singletonList(new DistributionQueueItemStatus(DistributionQueueItemState.QUEUED, "default")));
    SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "serviceName", packageImporter, packageExporter, packageExporterStrategy, queueProvider, queueDistributionStrategy, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, new String[] { "/content" }, 0);
    DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, "/home");
    final DistributionPackage distributionPackage = mock(DistributionPackage.class);
    DistributionPackageInfo packageInfo = new DistributionPackageInfo("type");
    when(distributionPackage.getInfo()).thenReturn(packageInfo);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    queueDistributionStrategy.add(distributionPackage, queueProvider);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            ((DistributionPackageProcessor) args[2]).process(distributionPackage);
            return null;
        }
    }).when(packageExporter).exportPackages(any(ResourceResolver.class), any(DistributionRequest.class), any(DistributionPackageProcessor.class));
    when(queueProvider.getQueue(DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME)).thenReturn(new SimpleDistributionQueue(name, "name"));
    DistributionResponse response = agent.execute(resourceResolver, request);
    assertFalse(response.isSuccessful());
}
Also used : DistributionQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) SimpleDistributionQueue(org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue) SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionPackageProcessor(org.apache.sling.distribution.packaging.DistributionPackageProcessor) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionEventFactory(org.apache.sling.distribution.event.impl.DistributionEventFactory) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) DistributionResponse(org.apache.sling.distribution.DistributionResponse) Test(org.junit.Test)

Example 4 with DistributionPackageProcessor

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

the class SimpleDistributionAgentTest method testDistributionWithAllowedRoot.

@Test
public void testDistributionWithAllowedRoot() throws Exception {
    String name = "sample-agent";
    DistributionPackageImporter packageImporter = mock(DistributionPackageImporter.class);
    DistributionPackageExporter packageExporter = mock(DistributionPackageExporter.class);
    DistributionRequestAuthorizationStrategy packageExporterStrategy = mock(DistributionRequestAuthorizationStrategy.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueueDispatchingStrategy queueDistributionStrategy = mock(DistributionQueueDispatchingStrategy.class);
    DistributionEventFactory distributionEventFactory = mock(DistributionEventFactory.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    when(queueDistributionStrategy.add(any(DistributionPackage.class), any(DistributionQueueProvider.class))).thenReturn(Collections.singletonList(new DistributionQueueItemStatus(DistributionQueueItemState.QUEUED, "default")));
    SimpleDistributionAgent agent = new SimpleDistributionAgent(name, false, null, "serviceName", packageImporter, packageExporter, packageExporterStrategy, queueProvider, queueDistributionStrategy, null, distributionEventFactory, resolverFactory, mock(SlingRepository.class), mock(DefaultDistributionLog.class), null, new String[] { "/content" }, 0);
    DistributionRequest request = new SimpleDistributionRequest(DistributionRequestType.ADD, "/content");
    final DistributionPackage distributionPackage = mock(DistributionPackage.class);
    DistributionPackageInfo packageInfo = new DistributionPackageInfo("type");
    when(distributionPackage.getInfo()).thenReturn(packageInfo);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    queueDistributionStrategy.add(distributionPackage, queueProvider);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            ((DistributionPackageProcessor) args[2]).process(distributionPackage);
            return null;
        }
    }).when(packageExporter).exportPackages(any(ResourceResolver.class), any(DistributionRequest.class), any(DistributionPackageProcessor.class));
    when(queueProvider.getQueue(DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME)).thenReturn(new SimpleDistributionQueue(name, "name"));
    DistributionResponse response = agent.execute(resourceResolver, request);
    assertTrue(response.isSuccessful());
}
Also used : DistributionQueueDispatchingStrategy(org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) SimpleDistributionQueue(org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue) SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionPackageProcessor(org.apache.sling.distribution.packaging.DistributionPackageProcessor) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionEventFactory(org.apache.sling.distribution.event.impl.DistributionEventFactory) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) DistributionResponse(org.apache.sling.distribution.DistributionResponse) Test(org.junit.Test)

Example 5 with DistributionPackageProcessor

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

the class AgentDistributionPackageExporterTest method testTestExport.

@Test
public void testTestExport() throws Exception {
    AgentDistributionPackageExporter distributionPackageExporter = new AgentDistributionPackageExporter(null, mock(DistributionAgent.class), mock(DistributionPackageBuilderProvider.class), null);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    // vargarg doesn't match and causes compiler warning
    String[] args = new String[0];
    DistributionRequest distributionRequest = new SimpleDistributionRequest(DistributionRequestType.TEST, args);
    final List<DistributionPackage> distributionPackages = new ArrayList<DistributionPackage>();
    distributionPackageExporter.exportPackages(resourceResolver, distributionRequest, new DistributionPackageProcessor() {

        @Override
        public void process(DistributionPackage distributionPackage) {
            distributionPackages.add(distributionPackage);
        }

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

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

        @Override
        public long getPackagesSize() {
            return 0;
        }
    });
    assertNotNull(distributionPackages);
    assertEquals(1, distributionPackages.size());
}
Also used : DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) ArrayList(java.util.ArrayList) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionPackageProcessor(org.apache.sling.distribution.packaging.DistributionPackageProcessor) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ArrayList(java.util.ArrayList) List(java.util.List) DistributionPackageBuilderProvider(org.apache.sling.distribution.packaging.DistributionPackageBuilderProvider) Test(org.junit.Test)

Aggregations

DistributionPackageProcessor (org.apache.sling.distribution.packaging.DistributionPackageProcessor)10 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)9 DistributionRequest (org.apache.sling.distribution.DistributionRequest)9 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)9 SimpleDistributionRequest (org.apache.sling.distribution.SimpleDistributionRequest)8 Test (org.junit.Test)8 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)6 DistributionPackageExporter (org.apache.sling.distribution.packaging.DistributionPackageExporter)6 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)5 DistributionResponse (org.apache.sling.distribution.DistributionResponse)5 DistributionEventFactory (org.apache.sling.distribution.event.impl.DistributionEventFactory)5 DistributionPackageImporter (org.apache.sling.distribution.packaging.DistributionPackageImporter)5 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)5 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)5 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)5 DistributionQueueDispatchingStrategy (org.apache.sling.distribution.queue.impl.DistributionQueueDispatchingStrategy)5 SimpleDistributionQueue (org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueue)5 SlingRepository (org.apache.sling.jcr.api.SlingRepository)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 List (java.util.List)4