Search in sources :

Example 11 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class PersistingJcrEventDistributionTriggerTest method testProcessEventWithPrivilegesAndNoNuggetsNode.

@Test
public void testProcessEventWithPrivilegesAndNoNuggetsNode() throws Exception {
    String nuggetsPath = "/var/nuggets";
    String serviceName = "serviceId";
    Session session = mock(Session.class);
    // first time it doesn't exist and should be created
    when(session.nodeExists("/var/nuggets")).thenReturn(false);
    // second time it should exist
    when(session.nodeExists("/var/nuggets")).thenReturn(true);
    Node rootNode = mock(Node.class);
    Node varNode = mock(Node.class);
    Node nuggetsNode = mock(Node.class);
    when(varNode.addNode("nuggets", "sling:Folder")).thenReturn(nuggetsNode);
    when(rootNode.addNode("var", "sling:Folder")).thenReturn(varNode);
    when(session.getRootNode()).thenReturn(rootNode);
    Workspace workspace = mock(Workspace.class);
    ObservationManager observationManager = mock(ObservationManager.class);
    when(workspace.getObservationManager()).thenReturn(observationManager);
    when(session.getWorkspace()).thenReturn(workspace);
    when(session.hasPermission(any(String.class), eq(Session.ACTION_ADD_NODE))).thenReturn(true);
    SlingRepository repository = mock(SlingRepository.class);
    Scheduler scheduler = mock(Scheduler.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    when(repository.loginService(serviceName, null)).thenReturn(session);
    String path = "/some/path";
    PersistedJcrEventDistributionTrigger persistingJcrEventdistributionTrigger = new PersistedJcrEventDistributionTrigger(repository, scheduler, resolverFactory, path, serviceName, nuggetsPath);
    DistributionRequestHandler handler = mock(DistributionRequestHandler.class);
    persistingJcrEventdistributionTrigger.register(handler);
    Node eventNode = mock(Node.class);
    when(nuggetsNode.addNode(any(String.class), any(String.class))).thenReturn(eventNode);
    when(session.getNode(nuggetsPath)).thenReturn(nuggetsNode);
    Event event = mock(Event.class);
    when(event.getPath()).thenReturn("/some/path/generating/event");
    DistributionRequest distributionRequest = persistingJcrEventdistributionTrigger.processEvent(event);
    assertNotNull(distributionRequest);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionRequestHandler(org.apache.sling.distribution.trigger.DistributionRequestHandler) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) Session(javax.jcr.Session) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Example 12 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class JcrEventDistributionTriggerTest method testProcessEventWithPathProperty.

@Test
public void testProcessEventWithPathProperty() throws Exception {
    SlingRepository repository = mock(SlingRepository.class);
    Scheduler scheduler = mock(Scheduler.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    String path = "/some/path";
    String serviceName = "serviceId";
    JcrEventDistributionTrigger jcrEventdistributionTrigger = new JcrEventDistributionTrigger(repository, scheduler, resolverFactory, path, false, serviceName, null);
    Event event = mock(Event.class);
    when(event.getPath()).thenReturn("/some/path/generating/event");
    DistributionRequest distributionRequest = jcrEventdistributionTrigger.processEvent(event);
    assertNotNull(distributionRequest);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Event(javax.jcr.observation.Event) Test(org.junit.Test)

Example 13 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest in project sling by apache.

the class PersistingJcrEventDistributionTriggerTest method testProcessEventWithoutPrivileges.

@Test
public void testProcessEventWithoutPrivileges() throws Exception {
    String serviceName = "serviceId";
    Session session = mock(Session.class);
    when(session.nodeExists("/var/nuggets")).thenReturn(true);
    SlingRepository repository = mock(SlingRepository.class);
    Scheduler scheduler = mock(Scheduler.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    when(repository.loginService(serviceName, null)).thenReturn(session);
    String path = "/some/path";
    String nuggetsPath = "/var/nuggets";
    PersistedJcrEventDistributionTrigger persistingJcrEventdistributionTrigger = new PersistedJcrEventDistributionTrigger(repository, scheduler, resolverFactory, path, serviceName, nuggetsPath);
    Event event = mock(Event.class);
    DistributionRequest distributionRequest = persistingJcrEventdistributionTrigger.processEvent(event);
    assertNull(distributionRequest);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Event(javax.jcr.observation.Event) Session(javax.jcr.Session) Test(org.junit.Test)

Example 14 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest 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 15 with DistributionRequest

use of org.apache.sling.distribution.DistributionRequest 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)

Aggregations

DistributionRequest (org.apache.sling.distribution.DistributionRequest)52 Test (org.junit.Test)42 SimpleDistributionRequest (org.apache.sling.distribution.SimpleDistributionRequest)31 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)22 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)16 SlingRepository (org.apache.sling.jcr.api.SlingRepository)14 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)13 Session (javax.jcr.Session)11 List (java.util.List)10 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)10 DistributionPackageBuilder (org.apache.sling.distribution.packaging.DistributionPackageBuilder)10 Event (javax.jcr.observation.Event)9 Scheduler (org.apache.sling.commons.scheduler.Scheduler)9 DistributionPackageProcessor (org.apache.sling.distribution.packaging.DistributionPackageProcessor)9 DistributionResponse (org.apache.sling.distribution.DistributionResponse)7 TreeMap (java.util.TreeMap)6 Resource (org.apache.sling.api.resource.Resource)6 DistributionPackageExporter (org.apache.sling.distribution.packaging.DistributionPackageExporter)6 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)6 FileDistributionPackageBuilder (org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder)6