Search in sources :

Example 6 with DistributionRequestHandler

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());
    }
}
Also used : DistributionRequestHandler(org.apache.sling.distribution.trigger.DistributionRequestHandler) DistributionRequest(org.apache.sling.distribution.DistributionRequest) Nonnull(javax.annotation.Nonnull) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionTrigger(org.apache.sling.distribution.trigger.DistributionTrigger) DistributionException(org.apache.sling.distribution.common.DistributionException) Nullable(javax.annotation.Nullable) PrintWriter(java.io.PrintWriter)

Example 7 with DistributionRequestHandler

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);
}
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 8 with DistributionRequestHandler

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);
}
Also used : DistributionRequestHandler(org.apache.sling.distribution.trigger.DistributionRequestHandler) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 9 with DistributionRequestHandler

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);
}
Also used : DistributionRequestHandler(org.apache.sling.distribution.trigger.DistributionRequestHandler) Dictionary(java.util.Dictionary) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 10 with DistributionRequestHandler

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);
}
Also used : DistributionRequestHandler(org.apache.sling.distribution.trigger.DistributionRequestHandler) ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Test(org.junit.Test)

Aggregations

DistributionRequestHandler (org.apache.sling.distribution.trigger.DistributionRequestHandler)11 Test (org.junit.Test)10 Scheduler (org.apache.sling.commons.scheduler.Scheduler)6 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)4 BundleContext (org.osgi.framework.BundleContext)4 DistributionRequest (org.apache.sling.distribution.DistributionRequest)3 Node (javax.jcr.Node)2 Session (javax.jcr.Session)2 Workspace (javax.jcr.Workspace)2 Event (javax.jcr.observation.Event)2 ObservationManager (javax.jcr.observation.ObservationManager)2 ScheduleOptions (org.apache.sling.commons.scheduler.ScheduleOptions)2 DistributionTransportSecretProvider (org.apache.sling.distribution.transport.DistributionTransportSecretProvider)2 SlingRepository (org.apache.sling.jcr.api.SlingRepository)2 PrintWriter (java.io.PrintWriter)1 Dictionary (java.util.Dictionary)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 DistributionException (org.apache.sling.distribution.common.DistributionException)1