Commit 024fed81 authored by Dave Cridland's avatar Dave Cridland

Merge pull request #186 from Redor/openfire

Update to REST API plugin version 1.0.1
parents be20d0f0 090831d3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html> <html>
<head> <head>
<title>REST API Plugin Changelog</title> <title>REST API Plugin Changelog</title>
<style type="text/css"> <style type="text/css">
BODY { BODY {
font-size : 100%; font-size : 100%;
} }
BODY, TD, TH { BODY, TD, TH {
font-family : tahoma, verdana, arial, helvetica, sans-serif; font-family : tahoma, verdana, arial, helvetica, sans-serif;
font-size : 0.8em; font-size : 0.8em;
} }
H2 { H2 {
font-size : 10pt; font-size : 10pt;
font-weight : bold; font-weight : bold;
padding-left : 1em; padding-left : 1em;
} }
A:hover { A:hover {
text-decoration : none; text-decoration : none;
} }
H1 { H1 {
font-family : tahoma, arial, helvetica, sans-serif; font-family : tahoma, arial, helvetica, sans-serif;
font-size : 1.4em; font-size : 1.4em;
font-weight: bold; font-weight: bold;
border-bottom : 1px #ccc solid; border-bottom : 1px #ccc solid;
padding-bottom : 2px; padding-bottom : 2px;
} }
TT { TT {
font-family : courier new; font-family : courier new;
font-weight : bold; font-weight : bold;
color : #060; color : #060;
} }
PRE { PRE {
font-family : courier new; font-family : courier new;
font-size : 100%; font-size : 100%;
} }
</style> </style>
</head> </head>
<body> <body>
<h1> <h1>
REST API Plugin Changelog REST API Plugin Changelog
</h1> </h1>
<p><b>1.0.0</b> -- February 3rd, 2015</p> <p><b>1.0.1</b> -- February 20th, 2015</p>
<ul> <ul>
<li>UserService plugin and MUC Service plugin are merged to the REST API plugin.</li> <li>Added possibility to rename a user (Thanks to JustMarried plugin)</li>
<li>Extended REST API with JSON data format.</li> <li>Adjusted HTTP Codes by conflict to HTTP CODE: 409</li>
</ul> <li>Added subject to Chat room</li>
<li>Disabled jersey logging on startup</li>
<p><b>0.1.0</b> -- November 14th, 2014</p> <li>By create a new chat room the chat room service will be created if it was not there</li>
<ul> </ul>
<li>Initial release of REST API Plugin with possibility to manage system properties.</li>
</ul> <p><b>1.0.0</b> -- February 3rd, 2015</p>
<ul>
</body> <li>UserService plugin and MUC Service plugin are merged to the REST API plugin.</li>
</html> <li>Extended REST API with JSON data format.</li>
</ul>
<p><b>0.1.0</b> -- November 14th, 2014</p>
<ul>
<li>Initial release of REST API Plugin with possibility to manage system properties.</li>
</ul>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<plugin> <plugin>
<class>org.jivesoftware.openfire.plugin.rest.RESTServicePlugin</class> <class>org.jivesoftware.openfire.plugin.rest.RESTServicePlugin</class>
<name>REST API</name> <name>REST API</name>
<description>Allows administration over a RESTful API.</description> <description>Allows administration over a RESTful API.</description>
<author>Roman Soldatow</author> <author>Roman Soldatow</author>
<version>1.0.0</version> <version>1.0.1</version>
<date>02/03/2015</date> <date>02/20/2015</date>
<minServerVersion>3.9.0</minServerVersion> <minServerVersion>3.9.0</minServerVersion>
<adminconsole> <adminconsole>
<tab id="tab-server"> <tab id="tab-server">
<sidebar id="sidebar-server-settings"> <sidebar id="sidebar-server-settings">
<item id="rest-api" name="REST API" url="rest-api.jsp" <item id="rest-api" name="REST API" url="rest-api.jsp"
description="Click to manage the service that allows to configure the Openfire over a RESTFul API" /> description="Click to manage the service that allows to configure the Openfire over a RESTFul API" />
</sidebar> </sidebar>
</tab> </tab>
</adminconsole> </adminconsole>
</plugin> </plugin>
This source diff could not be displayed because it is too large. You can view the blob instead.
package org.jivesoftware.openfire.plugin.rest.controller;
import java.util.List;
import javax.ws.rs.core.Response;
import org.dom4j.Element;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.SharedGroupException;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.AuthFactory;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
import org.jivesoftware.openfire.roster.Roster;
import org.jivesoftware.openfire.roster.RosterItem;
import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.openfire.vcard.VCardManager;
import org.xmpp.packet.StreamError;
/**
* The Class JustMarriedController.
*/
public class JustMarriedController {
/**
* Change name.
*
* @param currentUserName
* the current user name
* @param newUserName
* the new user name
* @param deleteOldUser
* the delete old user
* @param newEmail
* the new email
* @param newRealName
* the new real name
* @return true, if successful
* @throws ServiceException
* the service exception
*/
public static boolean changeName(String currentUserName, String newUserName, boolean deleteOldUser,
String newEmail, String newRealName) throws ServiceException {
UserManager userManager = UserManager.getInstance();
try {
User currentUser = userManager.getUser(currentUserName);
// Old user found, create new one
String password = AuthFactory.getPassword(currentUserName);
String newName = (newRealName == null || newRealName.length() == 0) ? currentUser.getName() : newRealName;
String newMail = (newEmail == null || newEmail.length() == 0) ? currentUser.getEmail() : newEmail;
User newUser = userManager.createUser(newUserName, password, currentUser.getName(), newMail);
newUser.setName(newName);
newUser.setNameVisible(currentUser.isNameVisible());
newUser.setEmailVisible(currentUser.isEmailVisible());
newUser.setCreationDate(currentUser.getCreationDate());
copyRoster(currentUser, newUser, currentUserName);
copyProperties(currentUser, newUser);
copyToGroups(currentUserName, newUserName);
copyVCard(currentUserName, newUserName);
if (deleteOldUser) {
deleteUser(currentUser);
}
} catch (UserNotFoundException e) {
throw new ServiceException("Could not find user", currentUserName, ExceptionType.USER_NOT_FOUND_EXCEPTION,
Response.Status.NOT_FOUND, e);
} catch (UserAlreadyExistsException e) {
throw new ServiceException("Could not create new user", newUserName,
ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
}
return true;
}
/**
* Copy v card.
*
* @param currentUserName
* the current user name
* @param newUserName
* the new user name
* @throws ServiceException
* the service exception
*/
private static void copyVCard(String currentUserName, String newUserName) throws ServiceException {
VCardManager vcardManager = VCardManager.getInstance();
Element vcard = vcardManager.getVCard(currentUserName);
if (vcard != null) {
try {
vcardManager.setVCard(newUserName, vcard);
} catch (Exception e) {
throw new ServiceException("Could not copy vcard to new user", newUserName,
ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST, e);
}
}
}
/**
* Copy to groups.
*
* @param currentUser
* the current user
* @param newUser
* the new user
*/
private static void copyToGroups(String currentUser, String newUser) {
GroupManager groupManager = GroupManager.getInstance();
for (Group group : groupManager.getGroups()) {
if (group.isUser(currentUser)) {
group.getMembers().add(XMPPServer.getInstance().createJID(newUser, null));
}
}
}
/**
* Delete user.
*
* @param oldUser
* the old user
*/
private static void deleteUser(User oldUser) {
UserManager.getInstance().deleteUser(oldUser);
final StreamError error = new StreamError(StreamError.Condition.not_authorized);
for (ClientSession sess : SessionManager.getInstance().getSessions(oldUser.getUsername())) {
sess.deliverRawText(error.toXML());
sess.close();
}
}
/**
* Copy properties.
*
* @param currentUser
* the current user
* @param newUser
* the new user
*/
private static void copyProperties(User currentUser, User newUser) {
for (String key : currentUser.getProperties().keySet()) {
newUser.getProperties().put(key, User.getPropertyValue(currentUser.getUsername(), key));
}
}
/**
* Copy roster.
*
* @param currentUser
* the current user
* @param newUser
* the new user
* @param currentUserName
* the current user name
* @throws ServiceException
* the service exception
*/
private static void copyRoster(User currentUser, User newUser, String currentUserName) throws ServiceException {
Roster newRoster = newUser.getRoster();
Roster currentRoster = currentUser.getRoster();
for (RosterItem item : currentRoster.getRosterItems()) {
try {
List<String> groups = item.getGroups();
RosterItem justCreated = newRoster.createRosterItem(item.getJid(), item.getNickname(), groups, true,
true);
justCreated.setAskStatus(item.getAskStatus());
justCreated.setRecvStatus(item.getRecvStatus());
justCreated.setSubStatus(item.getSubStatus());
for (Group gr : item.getSharedGroups()) {
justCreated.addSharedGroup(gr);
}
for (Group gr : item.getInvisibleSharedGroups()) {
justCreated.addInvisibleSharedGroup(gr);
}
newRoster.updateRosterItem(justCreated);
addNewUserToOthersRoster(newUser, item, currentUserName);
} catch (UserAlreadyExistsException e) {
throw new ServiceException("Could not create roster item for user ", newUser.getUsername(),
ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
} catch (SharedGroupException e) {
throw new ServiceException("Could not create roster item, because it is a contact from a shared group",
newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION,
Response.Status.BAD_REQUEST, e);
} catch (UserNotFoundException e) {
throw new ServiceException("Could not update roster item for user " + newUser.getUsername()
+ " because it was not properly created.", newUser.getUsername(),
ExceptionType.USER_NOT_FOUND_EXCEPTION, Response.Status.NOT_FOUND, e);
}
}
}
/**
* Adds the new user to others roster.
*
* @param newUser
* the new user
* @param otherItem
* the other item
* @param currentUser
* the current user
* @throws ServiceException
* the service exception
*/
private static void addNewUserToOthersRoster(User newUser, RosterItem otherItem, String currentUser)
throws ServiceException {
otherItem.getJid();
UserManager userManager = UserManager.getInstance();
// Is this user registered with our OF server?
String username = otherItem.getJid().getNode();
if (username != null && username.length() > 0 && userManager.isRegisteredUser(username)
&& XMPPServer.getInstance().isLocal(XMPPServer.getInstance().createJID(currentUser, null))) {
try {
User otherUser = userManager.getUser(username);
Roster otherRoster = otherUser.getRoster();
RosterItem oldUserOnOthersRoster = otherRoster.getRosterItem(XMPPServer.getInstance().createJID(
currentUser, null));
try {
if (!oldUserOnOthersRoster.isOnlyShared()) {
RosterItem justCreated = otherRoster.createRosterItem(
XMPPServer.getInstance().createJID(newUser.getUsername(), null),
oldUserOnOthersRoster.getNickname(), oldUserOnOthersRoster.getGroups(), true, true);
justCreated.setAskStatus(oldUserOnOthersRoster.getAskStatus());
justCreated.setRecvStatus(oldUserOnOthersRoster.getRecvStatus());
justCreated.setSubStatus(oldUserOnOthersRoster.getSubStatus());
otherRoster.updateRosterItem(justCreated);
}
} catch (UserAlreadyExistsException e) {
throw new ServiceException("Could not create roster item for user ", newUser.getUsername(),
ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
} catch (SharedGroupException e) {
throw new ServiceException(
"Could not create roster item, because it is a contact from a shared group",
newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION,
Response.Status.BAD_REQUEST, e);
}
} catch (UserNotFoundException e) {
throw new ServiceException("Could not create roster item for user " + newUser.getUsername()
+ " because it is a contact from a shared group.", newUser.getUsername(),
ExceptionType.USER_NOT_FOUND_EXCEPTION, Response.Status.NOT_FOUND, e);
}
}
}
}
package org.jivesoftware.openfire.plugin.rest.controller; package org.jivesoftware.openfire.plugin.rest.controller;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.plugin.rest.entity.MUCChannelType; import org.jivesoftware.openfire.plugin.rest.entity.MUCChannelType;
import org.jivesoftware.openfire.plugin.rest.entity.MUCRoomEntities; import org.jivesoftware.openfire.plugin.rest.entity.MUCRoomEntities;
import org.jivesoftware.openfire.plugin.rest.entity.MUCRoomEntity; import org.jivesoftware.openfire.plugin.rest.entity.MUCRoomEntity;
import org.jivesoftware.openfire.plugin.rest.entity.ParticipantEntities; import org.jivesoftware.openfire.plugin.rest.entity.ParticipantEntities;
import org.jivesoftware.openfire.plugin.rest.entity.ParticipantEntity; import org.jivesoftware.openfire.plugin.rest.entity.ParticipantEntity;
import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType; import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException; import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
import org.jivesoftware.openfire.muc.ConflictException; import org.jivesoftware.openfire.muc.ConflictException;
import org.jivesoftware.openfire.muc.ForbiddenException; import org.jivesoftware.openfire.muc.ForbiddenException;
import org.jivesoftware.openfire.muc.MUCRole; import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.MUCRoom; import org.jivesoftware.openfire.muc.MUCRoom;
import org.jivesoftware.openfire.muc.NotAllowedException; import org.jivesoftware.openfire.muc.NotAllowedException;
import org.jivesoftware.openfire.plugin.rest.utils.MUCRoomUtils; import org.jivesoftware.openfire.plugin.rest.utils.MUCRoomUtils;
import org.jivesoftware.openfire.plugin.rest.utils.UserUtils; import org.jivesoftware.openfire.plugin.rest.utils.UserUtils;
import org.xmpp.packet.JID; import org.jivesoftware.util.AlreadyExistsException;
import org.xmpp.packet.JID;
/**
* The Class MUCRoomController. /**
*/ * The Class MUCRoomController.
public class MUCRoomController { */
/** The Constant INSTANCE. */ public class MUCRoomController {
public static final MUCRoomController INSTANCE = new MUCRoomController(); /** The Constant INSTANCE. */
public static final MUCRoomController INSTANCE = new MUCRoomController();
/**
* Gets the single instance of MUCRoomController. /**
* * Gets the single instance of MUCRoomController.
* @return single instance of MUCRoomController *
*/ * @return single instance of MUCRoomController
public static MUCRoomController getInstance() { */
return INSTANCE; public static MUCRoomController getInstance() {
} return INSTANCE;
}
/**
* Gets the chat rooms. /**
* * Gets the chat rooms.
* @param serviceName *
* the service name * @param serviceName
* @param channelType * the service name
* the channel type * @param channelType
* @param roomSearch * the channel type
* the room search * @param roomSearch
* @return the chat rooms * the room search
*/ * @return the chat rooms
public MUCRoomEntities getChatRooms(String serviceName, String channelType, String roomSearch) { */
List<MUCRoom> rooms = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName) public MUCRoomEntities getChatRooms(String serviceName, String channelType, String roomSearch) {
.getChatRooms(); List<MUCRoom> rooms = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRooms();
List<MUCRoomEntity> mucRoomEntities = new ArrayList<MUCRoomEntity>();
List<MUCRoomEntity> mucRoomEntities = new ArrayList<MUCRoomEntity>();
for (MUCRoom chatRoom : rooms) {
if (roomSearch != null) { for (MUCRoom chatRoom : rooms) {
if (!chatRoom.getName().contains(roomSearch)) { if (roomSearch != null) {
continue; if (!chatRoom.getName().contains(roomSearch)) {
} continue;
} }
}
if (channelType.equals(MUCChannelType.ALL)) {
mucRoomEntities.add(convertToMUCRoomEntity(chatRoom)); if (channelType.equals(MUCChannelType.ALL)) {
} else if (channelType.equals(MUCChannelType.PUBLIC) && chatRoom.isPublicRoom()) { mucRoomEntities.add(convertToMUCRoomEntity(chatRoom));
mucRoomEntities.add(convertToMUCRoomEntity(chatRoom)); } else if (channelType.equals(MUCChannelType.PUBLIC) && chatRoom.isPublicRoom()) {
} mucRoomEntities.add(convertToMUCRoomEntity(chatRoom));
} }
}
return new MUCRoomEntities(mucRoomEntities);
} return new MUCRoomEntities(mucRoomEntities);
}
/**
* Gets the chat room. /**
* * Gets the chat room.
* @param roomName *
* the room name * @param roomName
* @param serviceName * the room name
* the service name * @param serviceName
* @return the chat room * the service name
* @throws ServiceException * @return the chat room
* the service exception * @throws ServiceException
*/ * the service exception
public MUCRoomEntity getChatRoom(String roomName, String serviceName) throws ServiceException { */
MUCRoom chatRoom = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName) public MUCRoomEntity getChatRoom(String roomName, String serviceName) throws ServiceException {
.getChatRoom(roomName); MUCRoom chatRoom = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName);
if (chatRoom == null) {
throw new ServiceException("Could not find the chat room", roomName, ExceptionType.ROOM_NOT_FOUND, Response.Status.NOT_FOUND); if (chatRoom == null) {
} throw new ServiceException("Could not find the chat room", roomName, ExceptionType.ROOM_NOT_FOUND, Response.Status.NOT_FOUND);
}
MUCRoomEntity mucRoomEntity = convertToMUCRoomEntity(chatRoom);
return mucRoomEntity; MUCRoomEntity mucRoomEntity = convertToMUCRoomEntity(chatRoom);
} return mucRoomEntity;
}
/**
* Delete chat room. /**
* * Delete chat room.
* @param roomName *
* the room name * @param roomName
* @param serviceName * the room name
* the service name * @param serviceName
* @throws ServiceException * the service name
* the service exception * @throws ServiceException
*/ * the service exception
public void deleteChatRoom(String roomName, String serviceName) throws ServiceException { */
MUCRoom chatRoom = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName) public void deleteChatRoom(String roomName, String serviceName) throws ServiceException {
.getChatRoom(roomName.toLowerCase()); MUCRoom chatRoom = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName.toLowerCase());
if (chatRoom != null) {
chatRoom.destroyRoom(null, null); if (chatRoom != null) {
} else { chatRoom.destroyRoom(null, null);
throw new ServiceException("Could not remove the channel", roomName, ExceptionType.ROOM_NOT_FOUND, Response.Status.NOT_FOUND); } else {
} throw new ServiceException("Could not remove the channel", roomName, ExceptionType.ROOM_NOT_FOUND, Response.Status.NOT_FOUND);
} }
}
/**
* Creates the chat room. /**
* * Creates the chat room.
* @param serviceName *
* the service name * @param serviceName
* @param mucRoomEntity * the service name
* the MUC room entity * @param mucRoomEntity
* @throws ServiceException * the MUC room entity
* the service exception * @throws ServiceException
*/ * the service exception
public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity) throws ServiceException { */
try { public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity) throws ServiceException {
createRoom(mucRoomEntity, serviceName); try {
} catch (NotAllowedException e) { createRoom(mucRoomEntity, serviceName);
throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(), } catch (NotAllowedException e) {
ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
} catch (ForbiddenException e) { ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(), } catch (ForbiddenException e) {
ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
} catch (ConflictException e) { ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(), } catch (ConflictException e) {
ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
} ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
} } catch (AlreadyExistsException e) {
throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
/** ExceptionType.ALREADY_EXISTS, Response.Status.CONFLICT, e);
* Update chat room. }
* }
* @param roomName
* the room name /**
* @param serviceName * Update chat room.
* the service name *
* @param mucRoomEntity * @param roomName
* the MUC room entity * the room name
* @throws ServiceException * @param serviceName
* the service exception * the service name
*/ * @param mucRoomEntity
public void updateChatRoom(String roomName, String serviceName, MUCRoomEntity mucRoomEntity) * the MUC room entity
throws ServiceException { * @throws ServiceException
try { * the service exception
// If the room name is different throw exception */
if (!roomName.equals(mucRoomEntity.getRoomName())) { public void updateChatRoom(String roomName, String serviceName, MUCRoomEntity mucRoomEntity)
throw new ServiceException( throws ServiceException {
"Could not update the channel. The room name is different to the entity room name.", roomName, try {
ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST); // If the room name is different throw exception
} if (!roomName.equals(mucRoomEntity.getRoomName())) {
createRoom(mucRoomEntity, serviceName); throw new ServiceException(
} catch (NotAllowedException e) { "Could not update the channel. The room name is different to the entity room name.", roomName,
throw new ServiceException("Could not update the channel", roomName, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST);
} catch (ForbiddenException e) { }
throw new ServiceException("Could not update the channel", roomName, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); createRoom(mucRoomEntity, serviceName);
} catch (ConflictException e) { } catch (NotAllowedException e) {
throw new ServiceException("Could not update the channel", roomName, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); throw new ServiceException("Could not update the channel", roomName, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
} } catch (ForbiddenException e) {
} throw new ServiceException("Could not update the channel", roomName, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
} catch (ConflictException e) {
/** throw new ServiceException("Could not update the channel", roomName, ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
* Creates the room. } catch (AlreadyExistsException e) {
* throw new ServiceException("Could not update the channel", mucRoomEntity.getRoomName(),
* @param mucRoomEntity ExceptionType.ALREADY_EXISTS, Response.Status.CONFLICT, e);
* the MUC room entity }
* @param serviceName }
* the service name
* @throws NotAllowedException /**
* the not allowed exception * Creates the room.
* @throws ForbiddenException *
* the forbidden exception * @param mucRoomEntity
* @throws ConflictException * the MUC room entity
* the conflict exception * @param serviceName
*/ * the service name
private void createRoom(MUCRoomEntity mucRoomEntity, String serviceName) throws NotAllowedException, * @throws NotAllowedException
ForbiddenException, ConflictException { * the not allowed exception
* @throws ForbiddenException
// Set owner * the forbidden exception
JID owner = XMPPServer.getInstance().createJID("admin", null); * @throws ConflictException
if (mucRoomEntity.getOwners() != null && mucRoomEntity.getOwners().size() > 0) { * the conflict exception
owner = new JID(mucRoomEntity.getOwners().get(0)); * @throws AlreadyExistsException
} else { */
List<String> owners = new ArrayList<String>(); private void createRoom(MUCRoomEntity mucRoomEntity, String serviceName) throws NotAllowedException,
owners.add(owner.toBareJID()); ForbiddenException, ConflictException, AlreadyExistsException {
mucRoomEntity.setOwners(owners);
} // Set owner
JID owner = XMPPServer.getInstance().createJID("admin", null);
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName) if (mucRoomEntity.getOwners() != null && mucRoomEntity.getOwners().size() > 0) {
.getChatRoom(mucRoomEntity.getRoomName().toLowerCase(), owner); owner = new JID(mucRoomEntity.getOwners().get(0));
} else {
// Set values List<String> owners = new ArrayList<String>();
room.setNaturalLanguageName(mucRoomEntity.getNaturalName()); owners.add(owner.toBareJID());
room.setSubject(mucRoomEntity.getSubject()); mucRoomEntity.setOwners(owners);
room.setDescription(mucRoomEntity.getDescription()); }
room.setPassword(mucRoomEntity.getPassword());
room.setPersistent(mucRoomEntity.isPersistent()); // Check if chat service is available, if not create a new one
room.setPublicRoom(mucRoomEntity.isPublicRoom()); boolean serviceRegistered = XMPPServer.getInstance().getMultiUserChatManager().isServiceRegistered(serviceName);
room.setRegistrationEnabled(mucRoomEntity.isRegistrationEnabled()); if(!serviceRegistered) {
room.setCanAnyoneDiscoverJID(mucRoomEntity.isCanAnyoneDiscoverJID()); XMPPServer.getInstance().getMultiUserChatManager().createMultiUserChatService(serviceName, serviceName, false);
room.setCanOccupantsChangeSubject(mucRoomEntity.isCanOccupantsChangeSubject()); }
room.setCanOccupantsInvite(mucRoomEntity.isCanOccupantsInvite());
room.setChangeNickname(mucRoomEntity.isCanChangeNickname()); MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
room.setModificationDate(mucRoomEntity.getModificationDate()); .getChatRoom(mucRoomEntity.getRoomName().toLowerCase(), owner);
room.setLogEnabled(mucRoomEntity.isLogEnabled());
room.setLoginRestrictedToNickname(mucRoomEntity.isLoginRestrictedToNickname()); // Set values
room.setMaxUsers(mucRoomEntity.getMaxUsers()); room.setNaturalLanguageName(mucRoomEntity.getNaturalName());
room.setMembersOnly(mucRoomEntity.isMembersOnly()); room.setSubject(mucRoomEntity.getSubject());
room.setModerated(mucRoomEntity.isModerated()); room.setDescription(mucRoomEntity.getDescription());
room.setPassword(mucRoomEntity.getPassword());
// Set broadcast presence roles room.setPersistent(mucRoomEntity.isPersistent());
if (mucRoomEntity.getBroadcastPresenceRoles() != null) { room.setPublicRoom(mucRoomEntity.isPublicRoom());
room.setRolesToBroadcastPresence(mucRoomEntity.getBroadcastPresenceRoles()); room.setRegistrationEnabled(mucRoomEntity.isRegistrationEnabled());
} else { room.setCanAnyoneDiscoverJID(mucRoomEntity.isCanAnyoneDiscoverJID());
room.setRolesToBroadcastPresence(new ArrayList<String>()); room.setCanOccupantsChangeSubject(mucRoomEntity.isCanOccupantsChangeSubject());
} room.setCanOccupantsInvite(mucRoomEntity.isCanOccupantsInvite());
// Set all roles room.setChangeNickname(mucRoomEntity.isCanChangeNickname());
setRoles(room, mucRoomEntity); room.setModificationDate(mucRoomEntity.getModificationDate());
room.setLogEnabled(mucRoomEntity.isLogEnabled());
// Set creation date room.setLoginRestrictedToNickname(mucRoomEntity.isLoginRestrictedToNickname());
if (mucRoomEntity.getCreationDate() != null) { room.setMaxUsers(mucRoomEntity.getMaxUsers());
room.setCreationDate(mucRoomEntity.getCreationDate()); room.setMembersOnly(mucRoomEntity.isMembersOnly());
} else { room.setModerated(mucRoomEntity.isModerated());
room.setCreationDate(new Date());
} // Set broadcast presence roles
if (mucRoomEntity.getBroadcastPresenceRoles() != null) {
// Set modification date room.setRolesToBroadcastPresence(mucRoomEntity.getBroadcastPresenceRoles());
if (mucRoomEntity.getModificationDate() != null) { } else {
room.setModificationDate(mucRoomEntity.getModificationDate()); room.setRolesToBroadcastPresence(new ArrayList<String>());
} else { }
room.setModificationDate(new Date()); // Set all roles
} setRoles(room, mucRoomEntity);
// Unlock the room, because the default configuration lock the room. // Set creation date
room.unlock(room.getRole()); if (mucRoomEntity.getCreationDate() != null) {
room.setCreationDate(mucRoomEntity.getCreationDate());
// Save the room to the DB if the room should be persistant } else {
if (room.isPersistent()) { room.setCreationDate(new Date());
room.saveToDB(); }
}
} // Set modification date
if (mucRoomEntity.getModificationDate() != null) {
/** room.setModificationDate(mucRoomEntity.getModificationDate());
* Gets the room participants. } else {
* room.setModificationDate(new Date());
* @param roomName }
* the room name
* @param serviceName // Unlock the room, because the default configuration lock the room.
* the service name room.unlock(room.getRole());
* @return the room participants
*/ // Save the room to the DB if the room should be persistant
public ParticipantEntities getRoomParticipants(String roomName, String serviceName) { if (room.isPersistent()) {
ParticipantEntities participantEntities = new ParticipantEntities(); room.saveToDB();
List<ParticipantEntity> participants = new ArrayList<ParticipantEntity>(); }
}
Collection<MUCRole> serverParticipants = XMPPServer.getInstance().getMultiUserChatManager()
.getMultiUserChatService(serviceName).getChatRoom(roomName).getParticipants(); /**
* Gets the room participants.
for (MUCRole role : serverParticipants) { *
ParticipantEntity participantEntity = new ParticipantEntity(); * @param roomName
participantEntity.setJid(role.getRoleAddress().toFullJID()); * the room name
participantEntity.setRole(role.getRole().name()); * @param serviceName
participantEntity.setAffiliation(role.getAffiliation().name()); * the service name
* @return the room participants
participants.add(participantEntity); */
} public ParticipantEntities getRoomParticipants(String roomName, String serviceName) {
ParticipantEntities participantEntities = new ParticipantEntities();
participantEntities.setParticipants(participants); List<ParticipantEntity> participants = new ArrayList<ParticipantEntity>();
return participantEntities;
} Collection<MUCRole> serverParticipants = XMPPServer.getInstance().getMultiUserChatManager()
.getMultiUserChatService(serviceName).getChatRoom(roomName).getParticipants();
/**
* Convert to MUC room entity. for (MUCRole role : serverParticipants) {
* ParticipantEntity participantEntity = new ParticipantEntity();
* @param room participantEntity.setJid(role.getRoleAddress().toFullJID());
* the room participantEntity.setRole(role.getRole().name());
* @return the MUC room entity participantEntity.setAffiliation(role.getAffiliation().name());
*/
public MUCRoomEntity convertToMUCRoomEntity(MUCRoom room) { participants.add(participantEntity);
MUCRoomEntity mucRoomEntity = new MUCRoomEntity(room.getNaturalLanguageName(), room.getName(), }
room.getDescription());
participantEntities.setParticipants(participants);
mucRoomEntity.setCanAnyoneDiscoverJID(room.canAnyoneDiscoverJID()); return participantEntities;
mucRoomEntity.setCanChangeNickname(room.canChangeNickname()); }
mucRoomEntity.setCanOccupantsChangeSubject(room.canOccupantsChangeSubject());
mucRoomEntity.setCanOccupantsInvite(room.canOccupantsInvite()); /**
* Convert to MUC room entity.
mucRoomEntity.setPublicRoom(room.isPublicRoom()); *
mucRoomEntity.setPassword(room.getPassword()); * @param room
mucRoomEntity.setPersistent(room.isPersistent()); * the room
mucRoomEntity.setRegistrationEnabled(room.isRegistrationEnabled()); * @return the MUC room entity
mucRoomEntity.setLogEnabled(room.isLogEnabled()); */
mucRoomEntity.setLoginRestrictedToNickname(room.isLoginRestrictedToNickname()); public MUCRoomEntity convertToMUCRoomEntity(MUCRoom room) {
mucRoomEntity.setMaxUsers(room.getMaxUsers()); MUCRoomEntity mucRoomEntity = new MUCRoomEntity(room.getNaturalLanguageName(), room.getName(),
mucRoomEntity.setMembersOnly(room.isMembersOnly()); room.getDescription());
mucRoomEntity.setModerated(room.isModerated());
mucRoomEntity.setSubject(room.getSubject());
mucRoomEntity.setOwners(MUCRoomUtils.convertJIDsToStringList(room.getOwners())); mucRoomEntity.setCanAnyoneDiscoverJID(room.canAnyoneDiscoverJID());
mucRoomEntity.setAdmins(MUCRoomUtils.convertJIDsToStringList(room.getAdmins())); mucRoomEntity.setCanChangeNickname(room.canChangeNickname());
mucRoomEntity.setMembers(MUCRoomUtils.convertJIDsToStringList(room.getMembers())); mucRoomEntity.setCanOccupantsChangeSubject(room.canOccupantsChangeSubject());
mucRoomEntity.setOutcasts(MUCRoomUtils.convertJIDsToStringList(room.getOutcasts())); mucRoomEntity.setCanOccupantsInvite(room.canOccupantsInvite());
mucRoomEntity.setBroadcastPresenceRoles(room.getRolesToBroadcastPresence()); mucRoomEntity.setPublicRoom(room.isPublicRoom());
mucRoomEntity.setPassword(room.getPassword());
mucRoomEntity.setCreationDate(room.getCreationDate()); mucRoomEntity.setPersistent(room.isPersistent());
mucRoomEntity.setModificationDate(room.getModificationDate()); mucRoomEntity.setRegistrationEnabled(room.isRegistrationEnabled());
mucRoomEntity.setLogEnabled(room.isLogEnabled());
return mucRoomEntity; mucRoomEntity.setLoginRestrictedToNickname(room.isLoginRestrictedToNickname());
} mucRoomEntity.setMaxUsers(room.getMaxUsers());
mucRoomEntity.setMembersOnly(room.isMembersOnly());
/** mucRoomEntity.setModerated(room.isModerated());
* Reset roles.
* mucRoomEntity.setOwners(MUCRoomUtils.convertJIDsToStringList(room.getOwners()));
* @param room mucRoomEntity.setAdmins(MUCRoomUtils.convertJIDsToStringList(room.getAdmins()));
* the room mucRoomEntity.setMembers(MUCRoomUtils.convertJIDsToStringList(room.getMembers()));
* @param mucRoomEntity mucRoomEntity.setOutcasts(MUCRoomUtils.convertJIDsToStringList(room.getOutcasts()));
* the muc room entity
* @throws ForbiddenException mucRoomEntity.setBroadcastPresenceRoles(room.getRolesToBroadcastPresence());
* the forbidden exception
* @throws NotAllowedException mucRoomEntity.setCreationDate(room.getCreationDate());
* the not allowed exception mucRoomEntity.setModificationDate(room.getModificationDate());
* @throws ConflictException
* the conflict exception return mucRoomEntity;
*/ }
private void setRoles(MUCRoom room, MUCRoomEntity mucRoomEntity) throws ForbiddenException, NotAllowedException,
ConflictException { /**
List<JID> roles = new ArrayList<JID>(); * Reset roles.
Collection<JID> owners = new ArrayList<JID>(); *
Collection<JID> existingOwners = new ArrayList<JID>(); * @param room
* the room
List<JID> mucRoomEntityOwners = MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getOwners()); * @param mucRoomEntity
owners.addAll(room.getOwners()); * the muc room entity
* @throws ForbiddenException
// Find same owners * the forbidden exception
for (JID jid : owners) { * @throws NotAllowedException
if (mucRoomEntityOwners.contains(jid)) { * the not allowed exception
existingOwners.add(jid); * @throws ConflictException
} * the conflict exception
} */
private void setRoles(MUCRoom room, MUCRoomEntity mucRoomEntity) throws ForbiddenException, NotAllowedException,
// Don't delete the same owners ConflictException {
owners.removeAll(existingOwners); List<JID> roles = new ArrayList<JID>();
room.addOwners(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getOwners()), room.getRole()); Collection<JID> owners = new ArrayList<JID>();
Collection<JID> existingOwners = new ArrayList<JID>();
// Collect all roles to reset
roles.addAll(owners); List<JID> mucRoomEntityOwners = MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getOwners());
roles.addAll(room.getAdmins()); owners.addAll(room.getOwners());
roles.addAll(room.getMembers());
roles.addAll(room.getOutcasts()); // Find same owners
for (JID jid : owners) {
for (JID jid : roles) { if (mucRoomEntityOwners.contains(jid)) {
room.addNone(jid, room.getRole()); existingOwners.add(jid);
} }
}
room.addOwners(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getOwners()), room.getRole());
if (mucRoomEntity.getAdmins() != null) { // Don't delete the same owners
room.addAdmins(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getAdmins()), room.getRole()); owners.removeAll(existingOwners);
} room.addOwners(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getOwners()), room.getRole());
if (mucRoomEntity.getMembers() != null) {
for (String memberJid : mucRoomEntity.getMembers()) { // Collect all roles to reset
room.addMember(new JID(memberJid), null, room.getRole()); roles.addAll(owners);
} roles.addAll(room.getAdmins());
} roles.addAll(room.getMembers());
if (mucRoomEntity.getOutcasts() != null) { roles.addAll(room.getOutcasts());
for (String outcastJid : mucRoomEntity.getOutcasts()) {
room.addOutcast(new JID(outcastJid), null, room.getRole()); for (JID jid : roles) {
} room.addNone(jid, room.getRole());
} }
}
room.addOwners(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getOwners()), room.getRole());
/** if (mucRoomEntity.getAdmins() != null) {
* Adds the admin. room.addAdmins(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getAdmins()), room.getRole());
* }
* @param serviceName if (mucRoomEntity.getMembers() != null) {
* the service name for (String memberJid : mucRoomEntity.getMembers()) {
* @param roomName room.addMember(new JID(memberJid), null, room.getRole());
* the room name }
* @param jid }
* the jid if (mucRoomEntity.getOutcasts() != null) {
* @throws ServiceException for (String outcastJid : mucRoomEntity.getOutcasts()) {
* the service exception room.addOutcast(new JID(outcastJid), null, room.getRole());
*/ }
public void addAdmin(String serviceName, String roomName, String jid) throws ServiceException { }
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName) }
.getChatRoom(roomName.toLowerCase());
try { /**
room.addAdmin(UserUtils.checkAndGetJID(jid), room.getRole()); * Adds the admin.
} catch (ForbiddenException e) { *
throw new ServiceException("Could not add admin", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param serviceName
} catch (ConflictException e) { * the service name
throw new ServiceException("Could not add admin", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param roomName
} * the room name
} * @param jid
* the jid
/** * @throws ServiceException
* Adds the owner. * the service exception
* */
* @param serviceName public void addAdmin(String serviceName, String roomName, String jid) throws ServiceException {
* the service name MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
* @param roomName .getChatRoom(roomName.toLowerCase());
* the room name try {
* @param jid room.addAdmin(UserUtils.checkAndGetJID(jid), room.getRole());
* the jid } catch (ForbiddenException e) {
* @throws ServiceException throw new ServiceException("Could not add admin", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
* the service exception } catch (ConflictException e) {
*/ throw new ServiceException("Could not add admin", jid, ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
public void addOwner(String serviceName, String roomName, String jid) throws ServiceException { }
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName) }
.getChatRoom(roomName.toLowerCase());
try { /**
room.addOwner(UserUtils.checkAndGetJID(jid), room.getRole()); * Adds the owner.
} catch (ForbiddenException e) { *
throw new ServiceException("Could not add owner", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param serviceName
} * the service name
} * @param roomName
* the room name
/** * @param jid
* Adds the member. * the jid
* * @throws ServiceException
* @param serviceName * the service exception
* the service name */
* @param roomName public void addOwner(String serviceName, String roomName, String jid) throws ServiceException {
* the room name MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
* @param jid .getChatRoom(roomName.toLowerCase());
* the jid try {
* @throws ServiceException room.addOwner(UserUtils.checkAndGetJID(jid), room.getRole());
* the service exception } catch (ForbiddenException e) {
*/ throw new ServiceException("Could not add owner", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
public void addMember(String serviceName, String roomName, String jid) throws ServiceException { }
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName) }
.getChatRoom(roomName.toLowerCase());
try { /**
room.addMember(UserUtils.checkAndGetJID(jid), null, room.getRole()); * Adds the member.
} catch (ForbiddenException e) { *
throw new ServiceException("Could not add member", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param serviceName
} catch (ConflictException e) { * the service name
throw new ServiceException("Could not add member", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param roomName
} * the room name
} * @param jid
* the jid
/** * @throws ServiceException
* Adds the outcast. * the service exception
* */
* @param serviceName public void addMember(String serviceName, String roomName, String jid) throws ServiceException {
* the service name MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
* @param roomName .getChatRoom(roomName.toLowerCase());
* the room name try {
* @param jid room.addMember(UserUtils.checkAndGetJID(jid), null, room.getRole());
* the jid } catch (ForbiddenException e) {
* @throws ServiceException throw new ServiceException("Could not add member", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
* the service exception } catch (ConflictException e) {
*/ throw new ServiceException("Could not add member", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
public void addOutcast(String serviceName, String roomName, String jid) throws ServiceException { }
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName) }
.getChatRoom(roomName.toLowerCase());
try { /**
room.addOutcast(UserUtils.checkAndGetJID(jid), null, room.getRole()); * Adds the outcast.
} catch (NotAllowedException e) { *
throw new ServiceException("Could not add outcast", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param serviceName
} catch (ForbiddenException e) { * the service name
throw new ServiceException("Could not add outcast", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param roomName
} catch (ConflictException e) { * the room name
throw new ServiceException("Could not add outcast", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param jid
} * the jid
} * @throws ServiceException
* the service exception
/** */
* Delete affiliation. public void addOutcast(String serviceName, String roomName, String jid) throws ServiceException {
* MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
* @param serviceName .getChatRoom(roomName.toLowerCase());
* the service name try {
* @param roomName room.addOutcast(UserUtils.checkAndGetJID(jid), null, room.getRole());
* the room name } catch (NotAllowedException e) {
* @param jid throw new ServiceException("Could not add outcast", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
* the jid } catch (ForbiddenException e) {
* @throws ServiceException throw new ServiceException("Could not add outcast", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
* the service exception } catch (ConflictException e) {
*/ throw new ServiceException("Could not add outcast", jid, ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
public void deleteAffiliation(String serviceName, String roomName, String jid) throws ServiceException { }
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName) }
.getChatRoom(roomName.toLowerCase());
try { /**
room.addNone(UserUtils.checkAndGetJID(jid), room.getRole()); * Delete affiliation.
} catch (ForbiddenException e) { *
throw new ServiceException("Could not delete affiliation", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param serviceName
} catch (ConflictException e) { * the service name
throw new ServiceException("Could not delete affiliation", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e); * @param roomName
} * the room name
} * @param jid
* the jid
* @throws ServiceException
* the service exception
*/
public void deleteAffiliation(String serviceName, String roomName, String jid) throws ServiceException {
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName.toLowerCase());
try {
room.addNone(UserUtils.checkAndGetJID(jid), room.getRole());
} catch (ForbiddenException e) {
throw new ServiceException("Could not delete affiliation", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
} catch (ConflictException e) {
throw new ServiceException("Could not delete affiliation", jid, ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
}
}
} }
\ No newline at end of file
package org.jivesoftware.openfire.plugin.rest.controller; package org.jivesoftware.openfire.plugin.rest.controller;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.jivesoftware.openfire.SharedGroupException; import org.jivesoftware.openfire.SharedGroupException;
import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.plugin.rest.entity.RosterEntities; import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity; import org.jivesoftware.openfire.group.GroupAlreadyExistsException;
import org.jivesoftware.openfire.plugin.rest.entity.UserEntities; import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.plugin.rest.entity.UserEntity; import org.jivesoftware.openfire.group.GroupNotFoundException;
import org.jivesoftware.openfire.plugin.rest.entity.UserGroupsEntity; import org.jivesoftware.openfire.lockout.LockOutManager;
import org.jivesoftware.openfire.plugin.rest.entity.UserProperty; import org.jivesoftware.openfire.plugin.rest.dao.PropertyDAO;
import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType; import org.jivesoftware.openfire.plugin.rest.entity.RosterEntities;
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException; import org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity;
import org.jivesoftware.openfire.group.Group; import org.jivesoftware.openfire.plugin.rest.entity.UserEntities;
import org.jivesoftware.openfire.group.GroupAlreadyExistsException; import org.jivesoftware.openfire.plugin.rest.entity.UserEntity;
import org.jivesoftware.openfire.group.GroupManager; import org.jivesoftware.openfire.plugin.rest.entity.UserGroupsEntity;
import org.jivesoftware.openfire.group.GroupNotFoundException; import org.jivesoftware.openfire.plugin.rest.entity.UserProperty;
import org.jivesoftware.openfire.lockout.LockOutManager; import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
import org.jivesoftware.openfire.plugin.rest.dao.PropertyDAO; import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
import org.jivesoftware.openfire.roster.Roster; import org.jivesoftware.openfire.plugin.rest.utils.UserUtils;
import org.jivesoftware.openfire.roster.RosterItem; import org.jivesoftware.openfire.roster.Roster;
import org.jivesoftware.openfire.roster.RosterManager; import org.jivesoftware.openfire.roster.RosterItem;
import org.jivesoftware.openfire.user.User; import org.jivesoftware.openfire.roster.RosterManager;
import org.jivesoftware.openfire.user.UserAlreadyExistsException; import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserManager; import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserNotFoundException; import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.plugin.rest.utils.UserUtils; import org.jivesoftware.openfire.user.UserNotFoundException;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
/** /**
* The Class UserServiceController. * The Class UserServiceController.
*/ */
public class UserServiceController { public class UserServiceController {
/** The Constant INSTANCE. */ /** The Constant INSTANCE. */
public static final UserServiceController INSTANCE = new UserServiceController(); public static final UserServiceController INSTANCE = new UserServiceController();
/** The user manager. */ /** The user manager. */
private UserManager userManager; private UserManager userManager;
/** The roster manager. */ /** The roster manager. */
private RosterManager rosterManager; private RosterManager rosterManager;
/** The server. */ /** The server. */
private XMPPServer server; private XMPPServer server;
/** /**
* Gets the single instance of UserServiceController. * Gets the single instance of UserServiceController.
* *
* @return single instance of UserServiceController * @return single instance of UserServiceController
*/ */
public static UserServiceController getInstance() { public static UserServiceController getInstance() {
return INSTANCE; return INSTANCE;
} }
/** /**
* Instantiates a new user service controller. * Instantiates a new user service controller.
*/ */
private UserServiceController() { private UserServiceController() {
server = XMPPServer.getInstance(); server = XMPPServer.getInstance();
userManager = server.getUserManager(); userManager = server.getUserManager();
rosterManager = server.getRosterManager(); rosterManager = server.getRosterManager();
} }
/** /**
* Creates the user. * Creates the user.
* *
* @param userEntity * @param userEntity
* the user entity * the user entity
* @throws ServiceException * @throws ServiceException
* the service exception * the service exception
*/ */
public void createUser(UserEntity userEntity) throws ServiceException { public void createUser(UserEntity userEntity) throws ServiceException {
if (userEntity != null && !userEntity.getUsername().isEmpty()) { if (userEntity != null && !userEntity.getUsername().isEmpty()) {
if (userEntity.getPassword() == null) { if (userEntity.getPassword() == null) {
throw new ServiceException("Could not create new user, because password is null", throw new ServiceException("Could not create new user, because password is null",
userEntity.getUsername(), "PasswordIsNull", Response.Status.BAD_REQUEST); userEntity.getUsername(), "PasswordIsNull", Response.Status.BAD_REQUEST);
} }
try { try {
userManager.createUser(userEntity.getUsername(), userEntity.getPassword(), userEntity.getName(), userManager.createUser(userEntity.getUsername(), userEntity.getPassword(), userEntity.getName(),
userEntity.getEmail()); userEntity.getEmail());
} catch (UserAlreadyExistsException e) { } catch (UserAlreadyExistsException e) {
throw new ServiceException("Could not create new user", userEntity.getUsername(), throw new ServiceException("Could not create new user", userEntity.getUsername(),
ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.BAD_REQUEST); ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT);
} }
addProperties(userEntity); addProperties(userEntity.getUsername(), userEntity.getProperties());
} }
} }
/** /**
* Update user. * Update user.
* *
* @param username * @param username
* the username * the username
* @param userEntity * @param userEntity
* the user entity * the user entity
* @throws ServiceException * @throws ServiceException
* the service exception * the service exception
*/ */
public void updateUser(String username, UserEntity userEntity) throws ServiceException { public void updateUser(String username, UserEntity userEntity) throws ServiceException {
if (userEntity != null && !username.isEmpty()) { if (userEntity != null && !username.isEmpty()) {
User user = getAndCheckUser(username); // Payload contains another username than provided over path
if (userEntity.getPassword() != null) { // parameter
user.setPassword(userEntity.getPassword()); if (userEntity.getUsername() != null) {
} if (!userEntity.getUsername().equals(username)) {
if (userEntity.getName() != null) { JustMarriedController.changeName(username, userEntity.getUsername(), true, userEntity.getEmail(),
user.setName(userEntity.getName()); userEntity.getName());
} addProperties(userEntity.getUsername(), userEntity.getProperties());
if (userEntity.getEmail() != null) { return;
user.setEmail(userEntity.getEmail()); }
} }
User user = getAndCheckUser(username);
addProperties(userEntity); if (userEntity.getPassword() != null) {
} user.setPassword(userEntity.getPassword());
} }
if (userEntity.getName() != null) {
/** user.setName(userEntity.getName());
* Delete user. }
* if (userEntity.getEmail() != null) {
* @param username user.setEmail(userEntity.getEmail());
* the username }
* @throws ServiceException
* the service exception addProperties(username, userEntity.getProperties());
*/ }
public void deleteUser(String username) throws ServiceException { }
User user = getAndCheckUser(username);
userManager.deleteUser(user); /**
* Delete user.
rosterManager.deleteRoster(server.createJID(username, null)); *
} * @param username
* the username
/** * @throws ServiceException
* Gets the user entities. * the service exception
* */
* @param userSearch public void deleteUser(String username) throws ServiceException {
* the user search User user = getAndCheckUser(username);
* @param propertyValue userManager.deleteUser(user);
* @param propertyKey
* @return the user entities rosterManager.deleteRoster(server.createJID(username, null));
* @throws ServiceException }
*/
public UserEntities getUserEntities(String userSearch, String propertyKey, String propertyValue) throws ServiceException { /**
if(propertyKey != null) { * Gets the user entities.
return getUserEntitiesByProperty(propertyKey, propertyValue); *
} * @param userSearch
UserEntities userEntities = new UserEntities(); * the user search
userEntities.setUsers(UserUtils.convertUsersToUserEntities(userManager.getUsers(), userSearch)); * @param propertyValue
return userEntities; * @param propertyKey
} * @return the user entities
* @throws ServiceException
/** */
* Gets the user entity. public UserEntities getUserEntities(String userSearch, String propertyKey, String propertyValue)
* throws ServiceException {
* @param username if (propertyKey != null) {
* the username return getUserEntitiesByProperty(propertyKey, propertyValue);
* @return the user entity }
* @throws ServiceException UserEntities userEntities = new UserEntities();
* the service exception userEntities.setUsers(UserUtils.convertUsersToUserEntities(userManager.getUsers(), userSearch));
*/ return userEntities;
public UserEntity getUserEntity(String username) throws ServiceException { }
return UserUtils.convertUserToUserEntity(getAndCheckUser(username));
} /**
* Gets the user entity.
/** *
* Enable user. * @param username
* * the username
* @param username * @return the user entity
* the username * @throws ServiceException
* @throws ServiceException * the service exception
* the service exception */
*/ public UserEntity getUserEntity(String username) throws ServiceException {
public void enableUser(String username) throws ServiceException { return UserUtils.convertUserToUserEntity(getAndCheckUser(username));
getAndCheckUser(username); }
LockOutManager.getInstance().enableAccount(username);
} /**
* Enable user.
/** *
* Disable user. * @param username
* * the username
* @param username * @throws ServiceException
* the username * the service exception
* @throws ServiceException */
* the service exception public void enableUser(String username) throws ServiceException {
*/ getAndCheckUser(username);
public void disableUser(String username) throws ServiceException { LockOutManager.getInstance().enableAccount(username);
getAndCheckUser(username); }
LockOutManager.getInstance().disableAccount(username, null, null);
} /**
* Disable user.
/** *
* Gets the roster entities. * @param username
* * the username
* @param username * @throws ServiceException
* the username * the service exception
* @return the roster entities */
* @throws ServiceException public void disableUser(String username) throws ServiceException {
* the service exception getAndCheckUser(username);
*/ LockOutManager.getInstance().disableAccount(username, null, null);
public RosterEntities getRosterEntities(String username) throws ServiceException { }
Roster roster = getUserRoster(username);
/**
List<RosterItemEntity> rosterEntities = new ArrayList<RosterItemEntity>(); * Gets the roster entities.
for (RosterItem rosterItem : roster.getRosterItems()) { *
RosterItemEntity rosterItemEntity = new RosterItemEntity(rosterItem.getJid().toBareJID(), * @param username
rosterItem.getNickname(), rosterItem.getSubStatus().getValue()); * the username
rosterItemEntity.setGroups(rosterItem.getGroups()); * @return the roster entities
* @throws ServiceException
rosterEntities.add(rosterItemEntity); * the service exception
} */
public RosterEntities getRosterEntities(String username) throws ServiceException {
return new RosterEntities(rosterEntities); Roster roster = getUserRoster(username);
}
List<RosterItemEntity> rosterEntities = new ArrayList<RosterItemEntity>();
/** for (RosterItem rosterItem : roster.getRosterItems()) {
* Adds the roster item. RosterItemEntity rosterItemEntity = new RosterItemEntity(rosterItem.getJid().toBareJID(),
* rosterItem.getNickname(), rosterItem.getSubStatus().getValue());
* @param username rosterItemEntity.setGroups(rosterItem.getGroups());
* the username
* @param rosterItemEntity rosterEntities.add(rosterItemEntity);
* the roster item entity }
* @throws ServiceException
* the service exception return new RosterEntities(rosterEntities);
* @throws UserAlreadyExistsException }
* the user already exists exception
* @throws SharedGroupException /**
* the shared group exception * Adds the roster item.
* @throws UserNotFoundException *
* the user not found exception * @param username
*/ * the username
public void addRosterItem(String username, RosterItemEntity rosterItemEntity) throws ServiceException, * @param rosterItemEntity
UserAlreadyExistsException, SharedGroupException, UserNotFoundException { * the roster item entity
Roster roster = getUserRoster(username); * @throws ServiceException
if (rosterItemEntity.getJid() == null) { * the service exception
throw new ServiceException("JID is null", "JID", "IllegalArgumentException", Response.Status.BAD_REQUEST); * @throws UserAlreadyExistsException
} * the user already exists exception
JID jid = new JID(rosterItemEntity.getJid()); * @throws SharedGroupException
* the shared group exception
try { * @throws UserNotFoundException
roster.getRosterItem(jid); * the user not found exception
throw new UserAlreadyExistsException(jid.toBareJID()); */
} catch (UserNotFoundException e) { public void addRosterItem(String username, RosterItemEntity rosterItemEntity) throws ServiceException,
// Roster item does not exist. Try to add it. UserAlreadyExistsException, SharedGroupException, UserNotFoundException {
} Roster roster = getUserRoster(username);
if (rosterItemEntity.getJid() == null) {
if (roster != null) { throw new ServiceException("JID is null", "JID", "IllegalArgumentException", Response.Status.BAD_REQUEST);
RosterItem rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(), }
rosterItemEntity.getGroups(), false, true); JID jid = new JID(rosterItemEntity.getJid());
UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType())); try {
roster.updateRosterItem(rosterItem); roster.getRosterItem(jid);
} throw new UserAlreadyExistsException(jid.toBareJID());
} } catch (UserNotFoundException e) {
// Roster item does not exist. Try to add it.
/** }
* Update roster item.
* if (roster != null) {
* @param username RosterItem rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(),
* the username rosterItemEntity.getGroups(), false, true);
* @param rosterJid UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
* the roster jid rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
* @param rosterItemEntity roster.updateRosterItem(rosterItem);
* the roster item entity }
* @throws ServiceException }
* the service exception
* @throws UserNotFoundException /**
* the user not found exception * Update roster item.
* @throws UserAlreadyExistsException *
* the user already exists exception * @param username
* @throws SharedGroupException * the username
* the shared group exception * @param rosterJid
*/ * the roster jid
public void updateRosterItem(String username, String rosterJid, RosterItemEntity rosterItemEntity) * @param rosterItemEntity
throws ServiceException, UserNotFoundException, UserAlreadyExistsException, SharedGroupException { * the roster item entity
getAndCheckUser(username); * @throws ServiceException
* the service exception
Roster roster = getUserRoster(username); * @throws UserNotFoundException
JID jid = new JID(rosterJid); * the user not found exception
RosterItem rosterItem = roster.getRosterItem(jid); * @throws UserAlreadyExistsException
* the user already exists exception
if (rosterItemEntity.getNickname() != null) { * @throws SharedGroupException
rosterItem.setNickname(rosterItemEntity.getNickname()); * the shared group exception
} */
if (rosterItemEntity.getGroups() != null) { public void updateRosterItem(String username, String rosterJid, RosterItemEntity rosterItemEntity)
rosterItem.setGroups(rosterItemEntity.getGroups()); throws ServiceException, UserNotFoundException, UserAlreadyExistsException, SharedGroupException {
} getAndCheckUser(username);
UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
Roster roster = getUserRoster(username);
rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType())); JID jid = new JID(rosterJid);
roster.updateRosterItem(rosterItem); RosterItem rosterItem = roster.getRosterItem(jid);
}
if (rosterItemEntity.getNickname() != null) {
/** rosterItem.setNickname(rosterItemEntity.getNickname());
* Delete roster item. }
* if (rosterItemEntity.getGroups() != null) {
* @param username rosterItem.setGroups(rosterItemEntity.getGroups());
* the username }
* @param rosterJid UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
* the roster jid
* @throws SharedGroupException rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
* the shared group exception roster.updateRosterItem(rosterItem);
* @throws ServiceException }
* the service exception
*/ /**
public void deleteRosterItem(String username, String rosterJid) throws SharedGroupException, ServiceException { * Delete roster item.
getAndCheckUser(username); *
Roster roster = getUserRoster(username); * @param username
JID jid = new JID(rosterJid); * the username
* @param rosterJid
if (roster.deleteRosterItem(jid, true) == null) { * the roster jid
throw new ServiceException("Roster Item could not deleted", jid.toBareJID(), "RosterItemNotFound", * @throws SharedGroupException
Response.Status.NOT_FOUND); * the shared group exception
} * @throws ServiceException
} * the service exception
*/
/** public void deleteRosterItem(String username, String rosterJid) throws SharedGroupException, ServiceException {
* Gets the user groups. getAndCheckUser(username);
* Roster roster = getUserRoster(username);
* @param username JID jid = new JID(rosterJid);
* the username
* @return the user groups if (roster.deleteRosterItem(jid, true) == null) {
* @throws ServiceException throw new ServiceException("Roster Item could not deleted", jid.toBareJID(), "RosterItemNotFound",
* the service exception Response.Status.NOT_FOUND);
*/ }
public List<String> getUserGroups(String username) throws ServiceException { }
User user = getAndCheckUser(username);
Collection<Group> groups = GroupManager.getInstance().getGroups(user); /**
List<String> groupNames = new ArrayList<String>(); * Gets the user groups.
for (Group group : groups) { *
groupNames.add(group.getName()); * @param username
} * the username
* @return the user groups
return groupNames; * @throws ServiceException
} * the service exception
*/
/** public List<String> getUserGroups(String username) throws ServiceException {
* Adds the user to group. User user = getAndCheckUser(username);
* Collection<Group> groups = GroupManager.getInstance().getGroups(user);
* @param username the username List<String> groupNames = new ArrayList<String>();
* @param userGroupsEntity the user groups entity for (Group group : groups) {
* @throws ServiceException the service exception groupNames.add(group.getName());
*/ }
public void addUserToGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
if (userGroupsEntity != null) { return groupNames;
Collection<Group> groups = new ArrayList<Group>(); }
for (String groupName : userGroupsEntity.getGroupNames()) { /**
Group group = null; * Adds the user to group.
try { *
group = GroupManager.getInstance().getGroup(groupName); * @param username
} catch (GroupNotFoundException e) { * the username
// Create this group * @param userGroupsEntity
group = createGroup(groupName); * the user groups entity
} * @throws ServiceException
groups.add(group); * the service exception
} */
for (Group group : groups) { public void addUserToGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
group.getMembers().add(server.createJID(username, null)); if (userGroupsEntity != null) {
} Collection<Group> groups = new ArrayList<Group>();
}
} for (String groupName : userGroupsEntity.getGroupNames()) {
Group group = null;
/** try {
* Delete user from groups. group = GroupManager.getInstance().getGroup(groupName);
* } catch (GroupNotFoundException e) {
* @param username // Create this group
* the username group = createGroup(groupName);
* @param userGroupsEntity }
* the user groups entity groups.add(group);
* @throws ServiceException }
* the service exception for (Group group : groups) {
*/ group.getMembers().add(server.createJID(username, null));
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); * Delete user from groups.
} catch (GroupNotFoundException e) { *
throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND, * @param username
Response.Status.NOT_FOUND, e); * the username
} * @param userGroupsEntity
group.getMembers().remove(server.createJID(username, null)); * the user groups entity
} * @throws ServiceException
} * the service exception
} */
public void deleteUserFromGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
/** if (userGroupsEntity != null) {
* Gets the user entities by property key and or value. for (String groupName : userGroupsEntity.getGroupNames()) {
* Group group = null;
* @param propertyKey try {
* the property key group = GroupManager.getInstance().getGroup(groupName);
* @param propertyValue } catch (GroupNotFoundException e) {
* the property value (can be null) throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND,
* @return the user entities by property Response.Status.NOT_FOUND, e);
* @throws ServiceException }
* the service exception group.getMembers().remove(server.createJID(username, null));
*/ }
public UserEntities getUserEntitiesByProperty(String propertyKey, String propertyValue) throws ServiceException { }
List<String> usernames = PropertyDAO.getUsernameByProperty(propertyKey, propertyValue); }
List<UserEntity> users = new ArrayList<UserEntity>();
UserEntities userEntities = new UserEntities(); /**
* Gets the user entities by property key and or value.
for (String username : usernames) { *
users.add(getUserEntity(username)); * @param propertyKey
} * the property key
* @param propertyValue
userEntities.setUsers(users); * the property value (can be null)
return userEntities; * @return the user entities by property
} * @throws ServiceException
* the service exception
/** */
* Adds the properties. public UserEntities getUserEntitiesByProperty(String propertyKey, String propertyValue) throws ServiceException {
* List<String> usernames = PropertyDAO.getUsernameByProperty(propertyKey, propertyValue);
* @param userEntity List<UserEntity> users = new ArrayList<UserEntity>();
* the user entity UserEntities userEntities = new UserEntities();
* @throws ServiceException
* the service exception for (String username : usernames) {
*/ users.add(getUserEntity(username));
private void addProperties(UserEntity userEntity) throws ServiceException { }
User user = getAndCheckUser(userEntity.getUsername());
user.getProperties().clear(); userEntities.setUsers(users);
if (userEntity.getProperties() != null) { return userEntities;
for (UserProperty property : userEntity.getProperties()) { }
user.getProperties().put(property.getKey(), property.getValue());
} /**
} * Adds the properties.
} *
* @param userEntity
/** * the user entity
* Creates the group. * @throws ServiceException
* * the service exception
* @param groupName the group name */
* @return the group private void addProperties(String username, List<UserProperty> properties) throws ServiceException {
* @throws ServiceException the service exception User user = getAndCheckUser(username);
*/ user.getProperties().clear();
private Group createGroup(String groupName) throws ServiceException { if (properties != null) {
Group group = null; for (UserProperty property : properties) {
try { user.getProperties().put(property.getKey(), property.getValue());
group = GroupManager.getInstance().createGroup(groupName); }
group.getProperties().put("sharedRoster.showInRoster", "onlyGroup"); }
group.getProperties().put("sharedRoster.displayName", groupName); }
group.getProperties().put("sharedRoster.groupList", "");
} catch (GroupAlreadyExistsException e) { /**
throw new ServiceException("Could not create group", groupName, ExceptionType.GROUP_ALREADY_EXISTS, * Creates the group.
Response.Status.BAD_REQUEST, e); *
} * @param groupName
return group; * the group name
} * @return the group
* @throws ServiceException
/** * the service exception
* Gets the and check user. */
* private Group createGroup(String groupName) throws ServiceException {
* @param username Group group = null;
* the username try {
* @return the and check user group = GroupManager.getInstance().createGroup(groupName);
* @throws ServiceException group.getProperties().put("sharedRoster.showInRoster", "onlyGroup");
* the service exception group.getProperties().put("sharedRoster.displayName", groupName);
*/ group.getProperties().put("sharedRoster.groupList", "");
private User getAndCheckUser(String username) throws ServiceException { } catch (GroupAlreadyExistsException e) {
JID targetJID = server.createJID(username, null); throw new ServiceException("Could not create group", groupName, ExceptionType.GROUP_ALREADY_EXISTS,
if (targetJID.getNode() == null) { Response.Status.BAD_REQUEST, e);
throw new ServiceException("Could not get user", username, ExceptionType.USER_NOT_FOUND_EXCEPTION, }
Response.Status.NOT_FOUND); return group;
} }
try { /**
return userManager.getUser(targetJID.getNode()); * Gets the and check user.
} catch (UserNotFoundException e) { *
throw new ServiceException("Could not get user", username, ExceptionType.USER_NOT_FOUND_EXCEPTION, * @param username
Response.Status.NOT_FOUND, e); * the username
} * @return the and check user
} * @throws ServiceException
* the service exception
/** */
* Gets the user roster. private User getAndCheckUser(String username) throws ServiceException {
* JID targetJID = server.createJID(username, null);
* @param username if (targetJID.getNode() == null) {
* the username throw new ServiceException("Could not get user", username, ExceptionType.USER_NOT_FOUND_EXCEPTION,
* @return the user roster Response.Status.NOT_FOUND);
* @throws ServiceException }
* the service exception
*/ try {
private Roster getUserRoster(String username) throws ServiceException { return userManager.getUser(targetJID.getNode());
try { } catch (UserNotFoundException e) {
return rosterManager.getRoster(username); throw new ServiceException("Could not get user", username, ExceptionType.USER_NOT_FOUND_EXCEPTION,
} catch (UserNotFoundException e) { Response.Status.NOT_FOUND, e);
throw new ServiceException("Could not get user roster", username, ExceptionType.USER_NOT_FOUND_EXCEPTION, }
Response.Status.NOT_FOUND, e); }
}
} /**
} * Gets the user roster.
*
* @param username
* the username
* @return the user roster
* @throws ServiceException
* the service exception
*/
private Roster getUserRoster(String username) throws ServiceException {
try {
return rosterManager.getRoster(username);
} catch (UserNotFoundException e) {
throw new ServiceException("Could not get user roster", username, ExceptionType.USER_NOT_FOUND_EXCEPTION,
Response.Status.NOT_FOUND, e);
}
}
}
package org.jivesoftware.openfire.plugin.rest.exceptions; package org.jivesoftware.openfire.plugin.rest.exceptions;
/** /**
* The Class ExceptionType. * The Class ExceptionType.
*/ */
public final class ExceptionType { public final class ExceptionType {
/** The Constant ILLEGAL_ARGUMENT_EXCEPTION. */ /** The Constant ILLEGAL_ARGUMENT_EXCEPTION. */
public static final String ILLEGAL_ARGUMENT_EXCEPTION = "IllegalArgumentException"; public static final String ILLEGAL_ARGUMENT_EXCEPTION = "IllegalArgumentException";
/** The Constant SHARED_GROUP_EXCEPTION. */ /** The Constant SHARED_GROUP_EXCEPTION. */
public static final String SHARED_GROUP_EXCEPTION = "SharedGroupException"; public static final String SHARED_GROUP_EXCEPTION = "SharedGroupException";
/** The Constant PROPERTY_NOT_FOUND. */ /** The Constant PROPERTY_NOT_FOUND. */
public static final String PROPERTY_NOT_FOUND = "PropertyNotFoundException"; public static final String PROPERTY_NOT_FOUND = "PropertyNotFoundException";
/** The Constant USER_ALREADY_EXISTS_EXCEPTION. */ /** The Constant USER_ALREADY_EXISTS_EXCEPTION. */
public static final String USER_ALREADY_EXISTS_EXCEPTION = "UserAlreadyExistsException"; public static final String USER_ALREADY_EXISTS_EXCEPTION = "UserAlreadyExistsException";
/** The Constant USER_NOT_FOUND_EXCEPTION. */ /** The Constant USER_NOT_FOUND_EXCEPTION. */
public static final String USER_NOT_FOUND_EXCEPTION = "UserNotFoundException"; public static final String USER_NOT_FOUND_EXCEPTION = "UserNotFoundException";
/** The Constant GROUP_ALREADY_EXISTS. */ /** The Constant GROUP_ALREADY_EXISTS. */
public static final String GROUP_ALREADY_EXISTS = "GroupAlreadyExistsException"; public static final String GROUP_ALREADY_EXISTS = "GroupAlreadyExistsException";
/** The Constant GROUP_NOT_FOUND. */ /** The Constant GROUP_NOT_FOUND. */
public static final String GROUP_NOT_FOUND = "GroupNotFoundException"; public static final String GROUP_NOT_FOUND = "GroupNotFoundException";
/** The Constant ROOM_NOT_FOUND. */ /** The Constant ROOM_NOT_FOUND. */
public static final String ROOM_NOT_FOUND = "RoomNotFoundException"; public static final String ROOM_NOT_FOUND = "RoomNotFoundException";
/** The Constant NOT_ALLOWED. */ /** The Constant NOT_ALLOWED. */
public static final String NOT_ALLOWED = "NotAllowedException"; public static final String NOT_ALLOWED = "NotAllowedException";
/** /** The Constant ALREADY_EXISTS. */
* Instantiates a new exception type. public static final String ALREADY_EXISTS = "AlreadyExistsException";
*/ /**
private ExceptionType() { * Instantiates a new exception type.
} */
} private ExceptionType() {
}
}
...@@ -2,6 +2,8 @@ package org.jivesoftware.openfire.plugin.rest.service; ...@@ -2,6 +2,8 @@ package org.jivesoftware.openfire.plugin.rest.service;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -43,8 +45,12 @@ public class JerseyWrapper extends ServletContainer { ...@@ -43,8 +45,12 @@ public class JerseyWrapper extends ServletContainer {
/** The prc. */ /** The prc. */
private static PackagesResourceConfig prc; private static PackagesResourceConfig prc;
/** The Constant JERSEY_LOGGER. */
private final static Logger JERSEY_LOGGER = Logger.getLogger("com.sun.jersey");
static { static {
JERSEY_LOGGER.setLevel(Level.SEVERE);
config = new HashMap<String, Object>(); config = new HashMap<String, Object>();
config.put(RESOURCE_CONFIG_CLASS_KEY, RESOURCE_CONFIG_CLASS); config.put(RESOURCE_CONFIG_CLASS_KEY, RESOURCE_CONFIG_CLASS);
prc = new PackagesResourceConfig(SCAN_PACKAGE_DEFAULT); prc = new PackagesResourceConfig(SCAN_PACKAGE_DEFAULT);
......
package org.jivesoftware.openfire.plugin.rest.service; package org.jivesoftware.openfire.plugin.rest.service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.jivesoftware.openfire.SharedGroupException; import org.jivesoftware.openfire.SharedGroupException;
import org.jivesoftware.openfire.plugin.rest.controller.UserServiceController; import org.jivesoftware.openfire.plugin.rest.controller.UserServiceController;
import org.jivesoftware.openfire.plugin.rest.entity.RosterEntities; import org.jivesoftware.openfire.plugin.rest.entity.RosterEntities;
import org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity; import org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity;
import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType; import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException; import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
import org.jivesoftware.openfire.user.UserAlreadyExistsException; import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserNotFoundException; import org.jivesoftware.openfire.user.UserNotFoundException;
@Path("restapi/v1/users/{username}/roster") @Path("restapi/v1/users/{username}/roster")
public class UserRosterService { public class UserRosterService {
private static final String COULD_NOT_UPDATE_THE_ROSTER = "Could not update the roster"; private static final String COULD_NOT_UPDATE_THE_ROSTER = "Could not update the roster";
private static final String COULD_NOT_CREATE_ROSTER_ITEM = "Could not create roster item"; private static final String COULD_NOT_CREATE_ROSTER_ITEM = "Could not create roster item";
private UserServiceController plugin; private UserServiceController plugin;
@PostConstruct @PostConstruct
public void init() { public void init() {
plugin = UserServiceController.getInstance(); plugin = UserServiceController.getInstance();
} }
@GET @GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public RosterEntities getUserRoster(@PathParam("username") String username) throws ServiceException { public RosterEntities getUserRoster(@PathParam("username") String username) throws ServiceException {
return plugin.getRosterEntities(username); return plugin.getRosterEntities(username);
} }
@POST @POST
public Response createRoster(@PathParam("username") String username, RosterItemEntity rosterItemEntity) public Response createRoster(@PathParam("username") String username, RosterItemEntity rosterItemEntity)
throws ServiceException { throws ServiceException {
try { try {
plugin.addRosterItem(username, rosterItemEntity); plugin.addRosterItem(username, rosterItemEntity);
} catch (UserNotFoundException e) { } catch (UserNotFoundException e) {
throw new ServiceException(COULD_NOT_CREATE_ROSTER_ITEM, "", ExceptionType.USER_NOT_FOUND_EXCEPTION, throw new ServiceException(COULD_NOT_CREATE_ROSTER_ITEM, "", ExceptionType.USER_NOT_FOUND_EXCEPTION,
Response.Status.NOT_FOUND, e); Response.Status.NOT_FOUND, e);
} catch (UserAlreadyExistsException e) { } catch (UserAlreadyExistsException e) {
throw new ServiceException(COULD_NOT_CREATE_ROSTER_ITEM, "", ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, throw new ServiceException(COULD_NOT_CREATE_ROSTER_ITEM, "", ExceptionType.USER_ALREADY_EXISTS_EXCEPTION,
Response.Status.BAD_REQUEST, e); Response.Status.CONFLICT, e);
} catch (SharedGroupException e) { } catch (SharedGroupException e) {
throw new ServiceException(COULD_NOT_CREATE_ROSTER_ITEM, "", ExceptionType.SHARED_GROUP_EXCEPTION, throw new ServiceException(COULD_NOT_CREATE_ROSTER_ITEM, "", ExceptionType.SHARED_GROUP_EXCEPTION,
Response.Status.BAD_REQUEST, e); Response.Status.BAD_REQUEST, e);
} }
return Response.status(Response.Status.CREATED).build(); return Response.status(Response.Status.CREATED).build();
} }
@DELETE @DELETE
@Path("/{rosterJid}") @Path("/{rosterJid}")
public Response deleteRoster(@PathParam("username") String username, @PathParam("rosterJid") String rosterJid) public Response deleteRoster(@PathParam("username") String username, @PathParam("rosterJid") String rosterJid)
throws ServiceException { throws ServiceException {
try { try {
plugin.deleteRosterItem(username, rosterJid); plugin.deleteRosterItem(username, rosterJid);
} catch (SharedGroupException e) { } catch (SharedGroupException e) {
throw new ServiceException("Could not delete the roster item", rosterJid, throw new ServiceException("Could not delete the roster item", rosterJid,
ExceptionType.SHARED_GROUP_EXCEPTION, Response.Status.BAD_REQUEST, e); ExceptionType.SHARED_GROUP_EXCEPTION, Response.Status.BAD_REQUEST, e);
} }
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
} }
@PUT @PUT
@Path("/{rosterJid}") @Path("/{rosterJid}")
public Response updateRoster(@PathParam("username") String username, @PathParam("rosterJid") String rosterJid, public Response updateRoster(@PathParam("username") String username, @PathParam("rosterJid") String rosterJid,
RosterItemEntity rosterItemEntity) throws ServiceException { RosterItemEntity rosterItemEntity) throws ServiceException {
try { try {
plugin.updateRosterItem(username, rosterJid, rosterItemEntity); plugin.updateRosterItem(username, rosterJid, rosterItemEntity);
} catch (UserNotFoundException e) { } catch (UserNotFoundException e) {
throw new ServiceException(COULD_NOT_UPDATE_THE_ROSTER, rosterJid, ExceptionType.USER_NOT_FOUND_EXCEPTION, throw new ServiceException(COULD_NOT_UPDATE_THE_ROSTER, rosterJid, ExceptionType.USER_NOT_FOUND_EXCEPTION,
Response.Status.NOT_FOUND, e); Response.Status.NOT_FOUND, e);
} catch (SharedGroupException e) { } catch (SharedGroupException e) {
throw new ServiceException(COULD_NOT_UPDATE_THE_ROSTER, rosterJid, ExceptionType.SHARED_GROUP_EXCEPTION, throw new ServiceException(COULD_NOT_UPDATE_THE_ROSTER, rosterJid, ExceptionType.SHARED_GROUP_EXCEPTION,
Response.Status.BAD_REQUEST, e); Response.Status.BAD_REQUEST, e);
} catch (UserAlreadyExistsException e) { } catch (UserAlreadyExistsException e) {
throw new ServiceException(COULD_NOT_UPDATE_THE_ROSTER, rosterJid, throw new ServiceException(COULD_NOT_UPDATE_THE_ROSTER, rosterJid,
ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.BAD_REQUEST, e); ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
} }
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment