Search in sources :

Example 1 with ApplicationInfoJson

use of com.enonic.xp.impl.server.rest.model.ApplicationInfoJson in project xp by enonic.

the class SseEntryPointTest method testOnEvent.

@Test
public void testOnEvent() {
    listener.setSse(sse);
    final Application application = mock(Application.class);
    when(application.getKey()).thenReturn(ApplicationKey.from("appKey"));
    when(application.getVersion()).thenReturn(Version.emptyVersion);
    when(applicationService.getInstalledApplication(any(ApplicationKey.class))).thenReturn(application);
    when(applicationService.isLocalApplication(any(ApplicationKey.class))).thenReturn(false);
    final ArgumentCaptor<ApplicationInfoJson> applicationInfoCaptor = ArgumentCaptor.forClass(ApplicationInfoJson.class);
    listener.onEvent(Event.create("application.cluster").localOrigin(true).value("id", UUID.randomUUID().toString()).value("eventType", "installed").value("key", "appKey").build());
    verify(sse, times(1)).newEventBuilder();
    verify(applicationService, times(1)).getInstalledApplication(any(ApplicationKey.class));
    verify(applicationService, times(1)).isLocalApplication(any(ApplicationKey.class));
    verify(eventBuilder).data(eq(ApplicationInfoJson.class), applicationInfoCaptor.capture());
    verify(broadcaster, times(1)).broadcast(any(OutboundSseEvent.class));
    final ApplicationInfoJson actualApplication = applicationInfoCaptor.getValue();
    assertNotNull(actualApplication);
    assertEquals("appKey", actualApplication.getKey());
    assertFalse(actualApplication.getLocal());
}
Also used : ApplicationInfoJson(com.enonic.xp.impl.server.rest.model.ApplicationInfoJson) ApplicationKey(com.enonic.xp.app.ApplicationKey) OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) Application(com.enonic.xp.app.Application) Test(org.junit.jupiter.api.Test)

Example 2 with ApplicationInfoJson

use of com.enonic.xp.impl.server.rest.model.ApplicationInfoJson in project xp by enonic.

the class SseEntryPoint method subscribe.

@GET
@Path("events")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void subscribe(@Context final SseEventSink sseEventSink) {
    final SseContextHolder ctx = contextHolder;
    if (ctx == null) {
        throw new IllegalStateException("No SSE context");
    }
    final OutboundSseEvent sseEvent = ctx.sse.newEventBuilder().name("list").id(UUID.randomUUID().toString()).mediaType(MediaType.APPLICATION_JSON_TYPE).data(ListApplicationJson.class, new ListApplicationJson(applicationService.getInstalledApplications().stream().map(application -> new ApplicationInfoJson(application, applicationService.isLocalApplication(application.getKey()))).collect(Collectors.toList()))).build();
    sseEventSink.send(sseEvent);
    ctx.broadcaster.register(sseEventSink);
}
Also used : ApplicationInfoJson(com.enonic.xp.impl.server.rest.model.ApplicationInfoJson) OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) ListApplicationJson(com.enonic.xp.impl.server.rest.model.ListApplicationJson) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with ApplicationInfoJson

use of com.enonic.xp.impl.server.rest.model.ApplicationInfoJson in project xp by enonic.

the class SseEntryPoint method handleEvent.

private void handleEvent(final Event event, final String eventSubType) {
    final SseContextHolder ctx = contextHolder;
    if (ctx == null) {
        LOG.debug("Skipping {} {}. Please to subscribe to send SSE events.", EVENT_TYPE, eventSubType);
        return;
    }
    if (event.getValue(APPLICATION_KEY_PARAM).isPresent()) {
        final OutboundSseEvent.Builder eventBuilder = ctx.sse.newEventBuilder().name(eventSubType).id(UUID.randomUUID().toString()).mediaType(MediaType.APPLICATION_JSON_TYPE);
        if (UNINSTALLED.equals(eventSubType)) {
            eventBuilder.data(ApplicationUninstalledJson.class, new ApplicationUninstalledJson(event.getValue(APPLICATION_KEY_PARAM).get().toString()));
        } else {
            final ApplicationKey applicationKey = ApplicationKey.from(event.getValue(APPLICATION_KEY_PARAM).get().toString());
            final Application application = applicationService.getInstalledApplication(applicationKey);
            if (application == null) {
                LOG.warn("Application \"{}\" not found", applicationKey);
                return;
            }
            final boolean localApplication = applicationService.isLocalApplication(application.getKey());
            eventBuilder.data(ApplicationInfoJson.class, new ApplicationInfoJson(application, localApplication));
        }
        ctx.broadcaster.broadcast(eventBuilder.build());
    }
}
Also used : ApplicationInfoJson(com.enonic.xp.impl.server.rest.model.ApplicationInfoJson) OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) ApplicationKey(com.enonic.xp.app.ApplicationKey) ApplicationUninstalledJson(com.enonic.xp.impl.server.rest.model.ApplicationUninstalledJson) Application(com.enonic.xp.app.Application)

Example 4 with ApplicationInfoJson

use of com.enonic.xp.impl.server.rest.model.ApplicationInfoJson in project xp by enonic.

the class SseEntryPointTest method testSubscribe.

@Test
public void testSubscribe() {
    listener.setSse(sse);
    final SseEventSink eventSink = mock(SseEventSink.class);
    doNothing().when(broadcaster).register(eventSink);
    final Application application = mock(Application.class);
    when(application.getKey()).thenReturn(ApplicationKey.from("appKey"));
    when(application.getVersion()).thenReturn(Version.emptyVersion);
    final Applications applications = Applications.from(application);
    when(applicationService.getInstalledApplications()).thenReturn(applications);
    when(applicationService.isLocalApplication(any(ApplicationKey.class))).thenReturn(false);
    final ArgumentCaptor<ListApplicationJson> listApplicationCaptor = ArgumentCaptor.forClass(ListApplicationJson.class);
    listener.subscribe(eventSink);
    verify(broadcaster, times(1)).register(eventSink);
    verify(sse, times(1)).newEventBuilder();
    verify(eventBuilder).data(eq(ListApplicationJson.class), listApplicationCaptor.capture());
    verify(applicationService, times(1)).getInstalledApplications();
    verify(applicationService, times(1)).isLocalApplication(any(ApplicationKey.class));
    final List<ApplicationInfoJson> actualApplications = listApplicationCaptor.getValue().getApplications();
    assertNotNull(actualApplications);
    assertFalse(actualApplications.isEmpty());
    assertEquals("appKey", actualApplications.get(0).getKey());
    assertFalse(actualApplications.get(0).getLocal());
}
Also used : ApplicationInfoJson(com.enonic.xp.impl.server.rest.model.ApplicationInfoJson) ApplicationKey(com.enonic.xp.app.ApplicationKey) ListApplicationJson(com.enonic.xp.impl.server.rest.model.ListApplicationJson) Applications(com.enonic.xp.app.Applications) SseEventSink(javax.ws.rs.sse.SseEventSink) Application(com.enonic.xp.app.Application) Test(org.junit.jupiter.api.Test)

Aggregations

ApplicationInfoJson (com.enonic.xp.impl.server.rest.model.ApplicationInfoJson)4 Application (com.enonic.xp.app.Application)3 ApplicationKey (com.enonic.xp.app.ApplicationKey)3 OutboundSseEvent (javax.ws.rs.sse.OutboundSseEvent)3 ListApplicationJson (com.enonic.xp.impl.server.rest.model.ListApplicationJson)2 Test (org.junit.jupiter.api.Test)2 Applications (com.enonic.xp.app.Applications)1 ApplicationUninstalledJson (com.enonic.xp.impl.server.rest.model.ApplicationUninstalledJson)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 SseEventSink (javax.ws.rs.sse.SseEventSink)1