Search in sources :

Example 1 with ServiceException

use of org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException 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)

Example 2 with ServiceException

use of org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException in project Openfire by igniterealtime.

the class UserServiceController method addRosterItem.

/**
	 * Adds the roster item.
	 *
	 * @param username
	 *            the username
	 * @param rosterItemEntity
	 *            the roster item entity
	 * @throws ServiceException
	 *             the service exception
	 * @throws UserAlreadyExistsException
	 *             the user already exists exception
	 * @throws SharedGroupException
	 *             the shared group exception
	 * @throws UserNotFoundException
	 *             the user not found exception
	 */
public void addRosterItem(String username, RosterItemEntity rosterItemEntity) throws ServiceException, UserAlreadyExistsException, SharedGroupException, UserNotFoundException {
    Roster roster = getUserRoster(username);
    if (rosterItemEntity.getJid() == null) {
        throw new ServiceException("JID is null", "JID", "IllegalArgumentException", Response.Status.BAD_REQUEST);
    }
    JID jid = new JID(rosterItemEntity.getJid());
    try {
        roster.getRosterItem(jid);
        throw new UserAlreadyExistsException(jid.toBareJID());
    } catch (UserNotFoundException e) {
    // Roster item does not exist. Try to add it.
    }
    if (roster != null) {
        RosterItem rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(), rosterItemEntity.getGroups(), false, true);
        UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
        rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
        roster.updateRosterItem(rosterItem);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Roster(org.jivesoftware.openfire.roster.Roster) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) JID(org.xmpp.packet.JID) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 3 with ServiceException

use of org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException in project Openfire by igniterealtime.

the class UserServiceController method deleteUserFromGroups.

/**
	 * Delete user from groups.
	 *
	 * @param username
	 *            the username
	 * @param userGroupsEntity
	 *            the user groups entity
	 * @throws ServiceException
	 *             the service exception
	 */
public void deleteUserFromGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
    if (userGroupsEntity != null) {
        for (String groupName : userGroupsEntity.getGroupNames()) {
            Group group = null;
            try {
                group = GroupManager.getInstance().getGroup(groupName);
            } catch (GroupNotFoundException e) {
                throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND, Response.Status.NOT_FOUND, e);
            }
            group.getMembers().remove(server.createJID(username, null));
        }
    }
}
Also used : Group(org.jivesoftware.openfire.group.Group) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Example 4 with ServiceException

use of org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException in project Openfire by igniterealtime.

the class UserServiceController method deleteUserFromGroup.

/**
	 * Delete user from group.
	 *
	 * @param username the username
	 * @param groupName the group name
	 * @throws ServiceException the service exception
	 */
public void deleteUserFromGroup(String username, String groupName) throws ServiceException {
    Group group = null;
    try {
        group = GroupManager.getInstance().getGroup(groupName);
    } catch (GroupNotFoundException e) {
        throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND, Response.Status.NOT_FOUND, e);
    }
    group.getMembers().remove(server.createJID(username, null));
}
Also used : Group(org.jivesoftware.openfire.group.Group) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Example 5 with ServiceException

use of org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException in project Openfire by igniterealtime.

the class GroupController method deleteGroup.

/**
	 * Delete group.
	 *
	 * @param groupName
	 *            the group name
	 * @throws ServiceException
	 *             the service exception
	 */
public void deleteGroup(String groupName) throws ServiceException {
    try {
        Group group = GroupManager.getInstance().getGroup(groupName);
        GroupManager.getInstance().deleteGroup(group);
    } catch (GroupNotFoundException e) {
        throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND, Response.Status.NOT_FOUND, e);
    }
}
Also used : Group(org.jivesoftware.openfire.group.Group) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Aggregations

ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)16 Group (org.jivesoftware.openfire.group.Group)7 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)6 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)5 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)5 Roster (org.jivesoftware.openfire.roster.Roster)4 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)3 RosterItem (org.jivesoftware.openfire.roster.RosterItem)3 JID (org.xmpp.packet.JID)3 ArrayList (java.util.ArrayList)2 MUCRoom (org.jivesoftware.openfire.muc.MUCRoom)2 LocalMUCRoom (org.jivesoftware.openfire.muc.spi.LocalMUCRoom)2 User (org.jivesoftware.openfire.user.User)2 UserManager (org.jivesoftware.openfire.user.UserManager)2 Presence (org.xmpp.packet.Presence)2 UnknownHostException (java.net.UnknownHostException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1