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);
}
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);
}
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);
}
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);
}
}
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"));
}
Aggregations