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