Search in sources :

Example 1 with ServerSession

use of org.cometd.bayeux.server.ServerSession in project ddf by codice.

the class ActivityControllerTest method testGetServerSessionByUserId.

/**
     * Test method for {@link ActivityController#getSessionByUserId(java.lang.String)}
     */
@Test
public void testGetServerSessionByUserId() {
    activityController.userSessionMap.put(MOCK_SESSION_ID, mockServerSession);
    ServerSession serverSession = activityController.getSessionByUserId(MOCK_SESSION_ID);
    assertNotNull(ActivityController.class.getName() + " returned null for user ID: " + MOCK_SESSION_ID, serverSession);
    String serverSessionId = serverSession.getId();
    assertNotNull("ServerSession ID is null", serverSessionId);
    assertEquals(ActivityController.class.getName() + " did not return the expected " + ServerSession.class.getName() + " object", serverSessionId, mockServerSession.getId());
}
Also used : ServerSession(org.cometd.bayeux.server.ServerSession) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 2 with ServerSession

use of org.cometd.bayeux.server.ServerSession in project ddf by codice.

the class NotificationController method handleEvent.

/**
     * Implementation of {@link org.osgi.service.event.EventHandler#handleEvent(Event)} that
     * receives notifications published on the {@link Notification#NOTIFICATION_TOPIC_ROOT} topic
     * from the OSGi eventing framework and forwards them to their intended recipients.
     *
     * @throws IllegalArgumentException when any of the following required properties are either
     *                                  missing from the Event or contain empty values:
     *                                  <p>
     *                                  <ul>
     *                                  <li>{@link Notification#NOTIFICATION_KEY_APPLICATION}</li>
     *                                  <li>{@link Notification#NOTIFICATION_KEY_MESSAGE}</li>
     *                                  <li>{@link Notification#NOTIFICATION_KEY_TIMESTAMP}</li
     *                                  <li>{@link Notification#NOTIFICATION_KEY_TITLE}</li>
     *                                  <li>{@link Notification#NOTIFICATION_KEY_USER_ID}</li>
     *                                  </ul>
     */
@Override
public void handleEvent(Event event) throws IllegalArgumentException {
    if (null == event.getProperty(Notification.NOTIFICATION_KEY_APPLICATION) || event.getProperty(Notification.NOTIFICATION_KEY_APPLICATION).toString().isEmpty()) {
        String message = String.format("Event [%s] property is null or empty", Notification.NOTIFICATION_KEY_APPLICATION);
        throw new IllegalArgumentException(message);
    }
    if (null == event.getProperty(Notification.NOTIFICATION_KEY_MESSAGE) || event.getProperty(Notification.NOTIFICATION_KEY_MESSAGE).toString().isEmpty()) {
        String message = String.format("Event [%s] property is null or empty", Notification.NOTIFICATION_KEY_MESSAGE);
        throw new IllegalArgumentException(message);
    }
    if (null == event.getProperty(Notification.NOTIFICATION_KEY_TIMESTAMP)) {
        String message = String.format("Event [%s] property is null", Notification.NOTIFICATION_KEY_TIMESTAMP);
        throw new IllegalArgumentException(message);
    }
    if (null == event.getProperty(Notification.NOTIFICATION_KEY_TITLE) || event.getProperty(Notification.NOTIFICATION_KEY_TITLE).toString().isEmpty()) {
        String message = String.format("Event [%s] property is null or empty", Notification.NOTIFICATION_KEY_TITLE);
        throw new IllegalArgumentException(message);
    }
    String sessionId = (String) event.getProperty(Notification.NOTIFICATION_KEY_SESSION_ID);
    String userId = (String) event.getProperty(Notification.NOTIFICATION_KEY_USER_ID);
    if (StringUtils.isBlank(userId) && StringUtils.isBlank(sessionId)) {
        String message = "No user information was provided in the event object. userId and sessionId properties were null";
        throw new IllegalArgumentException(message);
    }
    ServerSession recipient;
    LOGGER.debug("Getting ServerSession for userId/sessionId [{}]/[{}]", userId, sessionId);
    recipient = getSessionById(userId, sessionId);
    if (null != recipient) {
        Map<String, Object> propMap = new HashMap<>();
        for (String key : event.getPropertyNames()) {
            if (!EventConstants.EVENT_TOPIC.equals(key) && !Notification.NOTIFICATION_KEY_USER_ID.equals(key)) {
                propMap.put(key, event.getProperty(key));
            }
        }
        recipient.deliver(controllerServerSession, NOTIFICATION_TOPIC_DOWNLOADS_COMETD, propMap);
    } else {
        LOGGER.debug("Session with ID [{}] is not connected to the server. " + "Ignoring notification", sessionId);
    }
}
Also used : ServerSession(org.cometd.bayeux.server.ServerSession) HashMap(java.util.HashMap)

Example 3 with ServerSession

use of org.cometd.bayeux.server.ServerSession in project ddf by codice.

the class NotificationControllerTest method testGetServerSessionByUserId.

/**
     * Test method for {@link NotificationController#getSessionByUserId(java.lang.String)}
     */
@Test
public void testGetServerSessionByUserId() {
    notificationController.userSessionMap.put(MOCK_SESSION_ID, mockServerSession);
    ServerSession serverSession = notificationController.getSessionByUserId(MOCK_SESSION_ID);
    assertNotNull(NotificationController.class.getName() + " returned null for user ID: " + MOCK_SESSION_ID, serverSession);
    String serverSessionId = serverSession.getId();
    assertNotNull("ServerSession ID is null", serverSessionId);
    assertEquals(NotificationController.class.getName() + " did not return the expected " + ServerSession.class.getName() + " object", serverSessionId, mockServerSession.getId());
}
Also used : ServerSession(org.cometd.bayeux.server.ServerSession) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 4 with ServerSession

use of org.cometd.bayeux.server.ServerSession in project ddf by codice.

the class CometdEndpoint method init.

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    bayeuxServer = (BayeuxServer) config.getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
    if (bayeuxServer != null) {
        ServerAnnotationProcessor cometdAnnotationProcessor = new ServerAnnotationProcessor(bayeuxServer);
        //TODO: don't do this, we need some sort of policy
        bayeuxServer.setSecurityPolicy(new DefaultSecurityPolicy() {

            @Override
            public boolean canCreate(BayeuxServer server, ServerSession session, String channelId, ServerMessage message) {
                return true;
            }

            @Override
            public boolean canHandshake(BayeuxServer server, ServerSession session, ServerMessage message) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("canHandshake ServerSession: {}   canHandshake ServerMessage: {}", session, message);
                }
                notificationController.registerUserSession(session, message);
                activityController.registerUserSession(session, message);
                return true;
            }

            @Override
            public boolean canPublish(BayeuxServer server, ServerSession session, ServerChannel channel, ServerMessage message) {
                return true;
            }

            @Override
            public boolean canSubscribe(BayeuxServer server, ServerSession session, ServerChannel channel, ServerMessage message) {
                return true;
            }
        });
        searchController.setBayeuxServer(bayeuxServer);
        searchService = new SearchService(filterBuilder, searchController);
        UserService userService = new UserService(persistentStore);
        WorkspaceService workspaceService = new WorkspaceService(persistentStore);
        cometdAnnotationProcessor.process(userService);
        cometdAnnotationProcessor.process(workspaceService);
        cometdAnnotationProcessor.process(searchService);
        cometdAnnotationProcessor.process(notificationController);
        cometdAnnotationProcessor.process(activityController);
    }
}
Also used : DefaultSecurityPolicy(org.cometd.server.DefaultSecurityPolicy) ServerSession(org.cometd.bayeux.server.ServerSession) ServerAnnotationProcessor(org.cometd.annotation.ServerAnnotationProcessor) UserService(org.codice.ddf.ui.searchui.query.service.UserService) BayeuxServer(org.cometd.bayeux.server.BayeuxServer) WorkspaceService(org.codice.ddf.ui.searchui.query.service.WorkspaceService) SearchService(org.codice.ddf.ui.searchui.query.service.SearchService) ServerMessage(org.cometd.bayeux.server.ServerMessage) ServerChannel(org.cometd.bayeux.server.ServerChannel)

Example 5 with ServerSession

use of org.cometd.bayeux.server.ServerSession in project ddf by codice.

the class ActivityController method handleEvent.

/**
     * Implementation of {@link org.osgi.service.event.EventHandler#handleEvent(Event)} that receives
     * notifications published on the {@link ActivityEvent#EVENT_TOPIC} topic
     * from the OSGi eventing framework and forwards them to their intended
     * recipients.
     *
     * @throws IllegalArgumentException when any of the following required properties are either
     *                                  missing from the Event or contain empty values:
     *                                  <p>
     *                                  <ul>
     *                                  <li>{@link ActivityEvent#ID_KEY}</li>
     *                                  <li>{@link ActivityEvent#MESSAGE_KEY}</li>
     *                                  <li>{@link ActivityEvent#TIMESTAMP_KEY}</li
     *                                  <li>{@link ActivityEvent#STATUS_KEY}</li>
     *                                  <li>{@link ActivityEvent#USER_ID_KEY}</li>
     *                                  </ul>
     */
@Override
public void handleEvent(Event event) throws IllegalArgumentException {
    if (null == event.getProperty(ActivityEvent.ID_KEY) || event.getProperty(ActivityEvent.ID_KEY).toString().isEmpty()) {
        throw new IllegalArgumentException("Activity Event \"" + ActivityEvent.ID_KEY + "\" property is null or empty");
    }
    if (null == event.getProperty(ActivityEvent.MESSAGE_KEY) || event.getProperty(ActivityEvent.MESSAGE_KEY).toString().isEmpty()) {
        throw new IllegalArgumentException("Activity Event \"" + ActivityEvent.MESSAGE_KEY + "\" property is null or empty");
    }
    if (null == event.getProperty(ActivityEvent.TIMESTAMP_KEY)) {
        throw new IllegalArgumentException("Activity Event \"" + ActivityEvent.TIMESTAMP_KEY + "\" property is null");
    }
    if (null == event.getProperty(ActivityEvent.STATUS_KEY) || event.getProperty(ActivityEvent.STATUS_KEY).toString().isEmpty()) {
        throw new IllegalArgumentException("Activity Event \"" + ActivityEvent.STATUS_KEY + "\" property is null or empty");
    }
    String sessionId = (String) event.getProperty(ActivityEvent.SESSION_ID_KEY);
    String userId = (String) event.getProperty(ActivityEvent.USER_ID_KEY);
    if (StringUtils.isBlank(userId) && StringUtils.isBlank(sessionId)) {
        throw new IllegalArgumentException("No user information was provided in the Event object. userId and sessionId properties were null");
    }
    ServerSession recipient = null;
    LOGGER.debug("Getting ServerSession for userId/sessionId {}", userId);
    recipient = getSessionById(userId, sessionId);
    if (null != recipient) {
        Map<String, Object> propMap = new HashMap<>();
        for (String key : event.getPropertyNames()) {
            if (!EventConstants.EVENT_TOPIC.equals(key) && !ActivityEvent.USER_ID_KEY.equals(key) && event.getProperty(key) != null) {
                propMap.put(key, event.getProperty(key));
            }
        }
        recipient.deliver(controllerServerSession, ACTIVITY_TOPIC_COMETD_NEW, propMap);
    } else {
        LOGGER.debug("Session with ID \"{}\" is not connected to the server. Ignoring activity", sessionId);
    }
}
Also used : ServerSession(org.cometd.bayeux.server.ServerSession) HashMap(java.util.HashMap)

Aggregations

ServerSession (org.cometd.bayeux.server.ServerSession)5 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 SearchService (org.codice.ddf.ui.searchui.query.service.SearchService)1 UserService (org.codice.ddf.ui.searchui.query.service.UserService)1 WorkspaceService (org.codice.ddf.ui.searchui.query.service.WorkspaceService)1 ServerAnnotationProcessor (org.cometd.annotation.ServerAnnotationProcessor)1 BayeuxServer (org.cometd.bayeux.server.BayeuxServer)1 ServerChannel (org.cometd.bayeux.server.ServerChannel)1 ServerMessage (org.cometd.bayeux.server.ServerMessage)1 DefaultSecurityPolicy (org.cometd.server.DefaultSecurityPolicy)1