Search in sources :

Example 1 with ServerMessageImpl

use of org.cometd.server.ServerMessageImpl in project ddf by codice.

the class WorkspaceService method getWorkspaces.

@SuppressWarnings("unchecked")
@Listener("/service/workspaces")
public void getWorkspaces(final ServerSession remote, Message message) {
    ServerMessage.Mutable reply = new ServerMessageImpl();
    Map<String, Object> data = message.getDataAsMap();
    Subject subject = (Subject) bayeux.getContext().getRequestAttribute(SecurityConstants.SECURITY_SUBJECT);
    String username = SubjectUtils.getName(subject);
    // No workspaces persisted for a guest user (whose username="")
    if (StringUtils.isNotBlank(username)) {
        if (data == null || data.isEmpty() || data.get("workspaces") == null) {
            List<Map<String, Object>> workspacesList = new ArrayList<Map<String, Object>>();
            try {
                workspacesList = persistentStore.get(PersistentStore.WORKSPACE_TYPE, "user = '" + username + "'");
                if (workspacesList.size() == 1) {
                    // Convert workspace's JSON representation back to nested maps of Map<String, Object>
                    Map<String, Object> workspaces = (Map<String, Object>) workspacesList.get(0);
                    JSONContext.Client jsonContext = new Jackson1JSONContextClient();
                    String json = (String) workspaces.get("workspaces_json_txt");
                    LOGGER.debug("workspaces extracted JSON text:\n {}", json);
                    Map<String, Object> workspacesMap;
                    try {
                        workspacesMap = jsonContext.getParser().parse(new StringReader(json), Map.class);
                        reply.putAll(workspacesMap);
                    } catch (ParseException e) {
                        LOGGER.info("ParseException while trying to convert persisted workspaces's for user {} from JSON", username, e);
                    }
                }
            } catch (PersistenceException e) {
                LOGGER.info("PersistenceException while trying to retrieve persisted workspaces for user {}", username, e);
            }
            reply.put(Search.SUCCESSFUL, true);
            remote.deliver(serverSession, "/service/workspaces", reply);
        } else {
            LOGGER.debug("Persisting workspaces for username = {}", username);
            // Use JSON serializer so that only "data" component is serialized, not entire Message
            JSONContext.Server jsonContext = new Jackson1JSONContextServer();
            String json = jsonContext.getGenerator().generate(data);
            LOGGER.debug("workspaces JSON text:\n {}", json);
            PersistentItem item = new PersistentItem();
            item.addIdProperty(username);
            item.addProperty("user", username);
            item.addProperty("workspaces_json", json);
            try {
                persistentStore.add(PersistentStore.WORKSPACE_TYPE, item);
            } catch (PersistenceException e) {
                LOGGER.info("PersistenceException while trying to persist workspaces for user {}", username, e);
            }
            reply.put(Search.SUCCESSFUL, true);
            remote.deliver(serverSession, "/service/workspaces", reply);
        }
    }
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) ServerMessage(org.cometd.bayeux.server.ServerMessage) ArrayList(java.util.ArrayList) Subject(ddf.security.Subject) JSONContext(org.cometd.common.JSONContext) ServerMessageImpl(org.cometd.server.ServerMessageImpl) StringReader(java.io.StringReader) PersistenceException(org.codice.ddf.persistence.PersistenceException) ParseException(java.text.ParseException) Jackson1JSONContextServer(org.cometd.server.Jackson1JSONContextServer) Jackson1JSONContextClient(org.cometd.common.Jackson1JSONContextClient) Map(java.util.Map) Listener(org.cometd.annotation.Listener)

Example 2 with ServerMessageImpl

use of org.cometd.server.ServerMessageImpl in project ddf by codice.

the class SearchService method processQuery.

/**
     * Service method called by Cometd when something arrives on the service channel
     *
     * @param remote
     *            - Client session
     * @param message
     *            - JSON message
     */
@Listener("/service/query")
public void processQuery(final ServerSession remote, Message message) {
    ServerMessage.Mutable reply = new ServerMessageImpl();
    Map<String, Object> queryMessage = message.getDataAsMap();
    if (queryMessage != null && queryMessage.containsKey(Search.ID)) {
        bayeux.createChannelIfAbsent("/" + queryMessage.get(Search.ID), new ConfigurableServerChannel.Initializer() {

            public void configureChannel(ConfigurableServerChannel channel) {
                channel.setPersistent(true);
            }
        });
        BayeuxContext context = bayeux.getContext();
        Subject subject = null;
        if (context != null) {
            subject = (Subject) context.getRequestAttribute(SecurityConstants.SECURITY_SUBJECT);
        }
        // kick off the query
        executeQuery(queryMessage, subject);
        reply.put(Search.SUCCESSFUL, true);
        remote.deliver(serverSession, reply);
    } else {
        reply.put(Search.SUCCESSFUL, false);
        reply.put("status", "ERROR: unable to return results, no id in query request");
        remote.deliver(serverSession, reply);
    }
}
Also used : ServerMessageImpl(org.cometd.server.ServerMessageImpl) ServerMessage(org.cometd.bayeux.server.ServerMessage) ConfigurableServerChannel(org.cometd.bayeux.server.ConfigurableServerChannel) BayeuxContext(org.cometd.bayeux.server.BayeuxContext) Subject(ddf.security.Subject) Listener(org.cometd.annotation.Listener)

Example 3 with ServerMessageImpl

use of org.cometd.server.ServerMessageImpl in project ddf by codice.

the class UserService method getUser.

@Listener("/service/user")
public void getUser(final ServerSession remote, Message message) {
    ServerMessage.Mutable reply = new ServerMessageImpl();
    Map<String, Object> data = message.getDataAsMap();
    Subject subject = (Subject) bayeux.getContext().getRequestAttribute(SecurityConstants.SECURITY_SUBJECT);
    if (subject != null) {
        if (data == null || data.isEmpty()) {
            Map<String, Object> userMap = new HashMap<>();
            String username = SubjectUtils.getName(subject);
            userMap.put("username", username);
            userMap.put("isGuest", String.valueOf(subject.isGuest()));
            List<Map<String, Object>> preferencesList;
            try {
                preferencesList = persistentStore.get(PersistentStore.PREFERENCES_TYPE, "user = '" + username + "'");
                if (preferencesList.size() == 1) {
                    Map<String, Object> preferences = preferencesList.get(0);
                    JSONContext.Client jsonContext = new Jackson1JSONContextClient();
                    String json = (String) preferences.get("preferences_json_txt");
                    LOGGER.debug("preferences extracted JSON text:\n {}", json);
                    Map preferencesMap;
                    try {
                        preferencesMap = jsonContext.getParser().parse(new StringReader(json), Map.class);
                        userMap.put("preferences", preferencesMap);
                    } catch (ParseException e) {
                        LOGGER.info("ParseException while trying to convert persisted preferences for user {} from JSON", username, e);
                    }
                }
            } catch (PersistenceException e) {
                LOGGER.info("PersistenceException while trying to retrieve persisted preferences for user {}", username, e);
            }
            reply.put("user", userMap);
            reply.put(Search.SUCCESSFUL, true);
            remote.deliver(serverSession, "/service/user", reply);
        } else {
            JSONContext.Server jsonContext = new Jackson1JSONContextServer();
            String json = jsonContext.getGenerator().generate(data);
            LOGGER.debug("preferences JSON text:\n {}", json);
            String username = SubjectUtils.getName(subject);
            PersistentItem item = new PersistentItem();
            item.addIdProperty(username);
            item.addProperty("user", username);
            item.addProperty("preferences_json", json);
            try {
                persistentStore.add(PersistentStore.PREFERENCES_TYPE, item);
            } catch (PersistenceException e) {
                LOGGER.info("PersistenceException while trying to persist preferences for user {}", username, e);
            }
        }
    } else {
        reply.put(Search.SUCCESSFUL, false);
        remote.deliver(serverSession, "/service/user", reply);
    }
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) HashMap(java.util.HashMap) ServerMessage(org.cometd.bayeux.server.ServerMessage) Subject(ddf.security.Subject) JSONContext(org.cometd.common.JSONContext) ServerMessageImpl(org.cometd.server.ServerMessageImpl) StringReader(java.io.StringReader) PersistenceException(org.codice.ddf.persistence.PersistenceException) ParseException(java.text.ParseException) Jackson1JSONContextServer(org.cometd.server.Jackson1JSONContextServer) Jackson1JSONContextClient(org.cometd.common.Jackson1JSONContextClient) HashMap(java.util.HashMap) Map(java.util.Map) Listener(org.cometd.annotation.Listener)

Aggregations

Subject (ddf.security.Subject)3 Listener (org.cometd.annotation.Listener)3 ServerMessage (org.cometd.bayeux.server.ServerMessage)3 ServerMessageImpl (org.cometd.server.ServerMessageImpl)3 StringReader (java.io.StringReader)2 ParseException (java.text.ParseException)2 Map (java.util.Map)2 PersistenceException (org.codice.ddf.persistence.PersistenceException)2 PersistentItem (org.codice.ddf.persistence.PersistentItem)2 JSONContext (org.cometd.common.JSONContext)2 Jackson1JSONContextClient (org.cometd.common.Jackson1JSONContextClient)2 Jackson1JSONContextServer (org.cometd.server.Jackson1JSONContextServer)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 BayeuxContext (org.cometd.bayeux.server.BayeuxContext)1 ConfigurableServerChannel (org.cometd.bayeux.server.ConfigurableServerChannel)1