Search in sources :

Example 1 with SessionEntity

use of org.jivesoftware.openfire.plugin.rest.entity.SessionEntity in project Openfire by igniterealtime.

the class SessionController method convertToSessionEntities.

/**
	 * Convert to session entities.
	 *
	 * @param clientSessions the client sessions
	 * @return the session entities
	 * @throws ServiceException the service exception
	 */
private SessionEntities convertToSessionEntities(Collection<ClientSession> clientSessions) throws ServiceException {
    List<SessionEntity> sessions = new ArrayList<SessionEntity>();
    SessionEntities sessionEntities = new SessionEntities(sessions);
    for (ClientSession clientSession : clientSessions) {
        SessionEntity session = new SessionEntity();
        session.setSessionId(clientSession.getAddress().toString());
        if (!clientSession.isAnonymousUser()) {
            try {
                session.setUsername(clientSession.getUsername());
            } catch (UserNotFoundException e) {
                throw new ServiceException("Could not get user", "", ExceptionType.USER_NOT_FOUND_EXCEPTION, Response.Status.NOT_FOUND, e);
            }
        } else {
            session.setUsername("Anonymous");
        }
        session.setRessource(clientSession.getAddress().getResource());
        if (clientSession instanceof LocalClientSession) {
            session.setNode("Local");
        } else {
            session.setNode("Remote");
        }
        String status = "";
        if (clientSession.getStatus() == Session.STATUS_CLOSED) {
            status = "Closed";
        } else if (clientSession.getStatus() == Session.STATUS_CONNECTED) {
            status = "Connected";
        } else if (clientSession.getStatus() == Session.STATUS_AUTHENTICATED) {
            status = "Authenticated";
        } else {
            status = "Unkown";
        }
        session.setSessionStatus(status);
        if (clientSession.getPresence() != null) {
            session.setPresenceMessage(clientSession.getPresence().getStatus());
            Presence.Show show = clientSession.getPresence().getShow();
            if (show == Presence.Show.away) {
                session.setPresenceStatus("Away");
            } else if (show == Presence.Show.chat) {
                session.setPresenceStatus("Available to Chat");
            } else if (show == Presence.Show.dnd) {
                session.setPresenceStatus("Do Not Disturb");
            } else if (show == Presence.Show.xa) {
                session.setPresenceStatus("Extended Away");
            } else if (show == null) {
                session.setPresenceStatus("Online");
            } else {
                session.setPresenceStatus("Unknown/Not Recognized");
            }
            session.setPriority(clientSession.getPresence().getPriority());
        }
        try {
            session.setHostAddress(clientSession.getHostAddress());
            session.setHostName(clientSession.getHostName());
        } catch (UnknownHostException e) {
            LOG.error("UnknownHostException", e);
        }
        session.setCreationDate(clientSession.getCreationDate());
        session.setLastActionDate(clientSession.getLastActiveDate());
        session.setSecure(clientSession.isSecure());
        sessions.add(session);
    }
    return sessionEntities;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) UnknownHostException(java.net.UnknownHostException) SessionEntity(org.jivesoftware.openfire.plugin.rest.entity.SessionEntity) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession) ArrayList(java.util.ArrayList) Presence(org.xmpp.packet.Presence) SessionEntities(org.jivesoftware.openfire.plugin.rest.entity.SessionEntities)

Aggregations

UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 SessionEntities (org.jivesoftware.openfire.plugin.rest.entity.SessionEntities)1 SessionEntity (org.jivesoftware.openfire.plugin.rest.entity.SessionEntity)1 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)1 ClientSession (org.jivesoftware.openfire.session.ClientSession)1 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)1 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)1 Presence (org.xmpp.packet.Presence)1