use of org.apache.sling.distribution.trigger.DistributionRequestHandler in project sling by apache.
the class DistributionTriggerServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String secondsParameter = request.getParameter("sec");
int seconds = secondsParameter != null && secondsParameter.length() > 0 ? Integer.parseInt(secondsParameter) : DEFAULT_NUMBER_OF_SECONDS;
if (seconds > MAX_NUMBER_OF_SECONDS) {
seconds = MAX_NUMBER_OF_SECONDS;
} else if (seconds < 0) {
seconds = DEFAULT_NUMBER_OF_SECONDS;
}
DistributionTrigger distributionTrigger = request.getResource().adaptTo(DistributionTrigger.class);
// setup SSE headers
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
// needed to allow e.g. the JavaScript EventSource API to make a call from author to this server and listen for the events
// response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); // allowed origins should be explicitly configured
// response.setHeader("Access-Control-Allow-Credentials", "true");
final PrintWriter writer = response.getWriter();
DistributionRequestHandler distributionRequestHandler = new DistributionRequestHandler() {
public void handle(@Nullable ResourceResolver resourceResolver, @Nonnull DistributionRequest request) {
writeEvent(writer, request);
}
};
try {
distributionTrigger.register(distributionRequestHandler);
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
log.error("thread interrupted", e);
}
distributionTrigger.unregister(distributionRequestHandler);
} catch (DistributionException e) {
response.setStatus(400);
response.getWriter().write("error while (un)registering trigger " + e.toString());
}
}
use of org.apache.sling.distribution.trigger.DistributionRequestHandler in project sling by apache.
the class PersistingJcrEventDistributionTriggerTest method testProcessEventWithPrivileges.
@Test
public void testProcessEventWithPrivileges() throws Exception {
String nuggetsPath = "/var/nuggets";
String serviceName = "serviceId";
Session session = mock(Session.class);
when(session.nodeExists("/var/nuggets")).thenReturn(true);
Workspace workspace = mock(Workspace.class);
ObservationManager observationManager = mock(ObservationManager.class);
when(workspace.getObservationManager()).thenReturn(observationManager);
when(session.getWorkspace()).thenReturn(workspace);
when(session.hasPermission(nuggetsPath, 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 nuggetsNode = mock(Node.class);
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.trigger.DistributionRequestHandler in project sling by apache.
the class ResourceEventDistributionTriggerTest method testUnregister.
@Test
public void testUnregister() throws Exception {
String path = "/some/path";
BundleContext bundleContext = mock(BundleContext.class);
ResourceEventDistributionTrigger resourceEventdistributionTrigger = new ResourceEventDistributionTrigger(path, bundleContext);
DistributionRequestHandler handler = mock(DistributionRequestHandler.class);
resourceEventdistributionTrigger.unregister(handler);
}
use of org.apache.sling.distribution.trigger.DistributionRequestHandler in project sling by apache.
the class ResourceEventDistributionTriggerTest method testRegister.
@Test
public void testRegister() throws Exception {
String path = "/some/path";
BundleContext bundleContext = mock(BundleContext.class);
ServiceRegistration registration = mock(ServiceRegistration.class);
when(bundleContext.registerService(any(String.class), any(Object.class), any(Dictionary.class))).thenReturn(registration);
ResourceEventDistributionTrigger resourceEventdistributionTrigger = new ResourceEventDistributionTrigger(path, bundleContext);
DistributionRequestHandler handler = mock(DistributionRequestHandler.class);
resourceEventdistributionTrigger.register(handler);
}
use of org.apache.sling.distribution.trigger.DistributionRequestHandler in project sling by apache.
the class ScheduledDistributionTriggerTest method testRegister.
@Test
public void testRegister() throws Exception {
String path = "/path/to/somewhere";
int interval = 10;
DistributionRequestHandler handler = mock(DistributionRequestHandler.class);
Scheduler scheduler = mock(Scheduler.class);
ScheduleOptions options = mock(ScheduleOptions.class);
when(scheduler.NOW(-1, interval)).thenReturn(options);
when(options.name(handler.toString())).thenReturn(options);
ScheduledDistributionTrigger scheduleddistributionTrigger = new ScheduledDistributionTrigger(action.name(), path, interval, null, scheduler, mock(ResourceResolverFactory.class));
scheduleddistributionTrigger.register(handler);
}
Aggregations