Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
Openfire
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Openfire
Commits
090831d3
Commit
090831d3
authored
Feb 20, 2015
by
Roman S
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update to REST API plugin version 1.0.1
parent
4c2804b6
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
5219 additions
and
4962 deletions
+5219
-4962
changelog.html
src/plugins/restAPI/changelog.html
+68
-59
plugin.xml
src/plugins/restAPI/plugin.xml
+21
-21
readme.html
src/plugins/restAPI/readme.html
+3702
-3752
JustMarriedController.java
...penfire/plugin/rest/controller/JustMarriedController.java
+259
-0
MUCRoomController.java
...re/openfire/plugin/rest/controller/MUCRoomController.java
+519
-504
UserServiceController.java
...penfire/plugin/rest/controller/UserServiceController.java
+511
-495
ExceptionType.java
...ftware/openfire/plugin/rest/exceptions/ExceptionType.java
+41
-39
JerseyWrapper.java
...esoftware/openfire/plugin/rest/service/JerseyWrapper.java
+6
-0
UserRosterService.java
...tware/openfire/plugin/rest/service/UserRosterService.java
+92
-92
No files found.
src/plugins/restAPI/changelog.html
View file @
090831d3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
REST API Plugin Changelog
</title>
<style
type=
"text/css"
>
BODY
{
font-size
:
100%
;
}
BODY
,
TD
,
TH
{
font-family
:
tahoma
,
verdana
,
arial
,
helvetica
,
sans-serif
;
font-size
:
0.8em
;
}
H2
{
font-size
:
10pt
;
font-weight
:
bold
;
padding-left
:
1em
;
}
A
:hover
{
text-decoration
:
none
;
}
H1
{
font-family
:
tahoma
,
arial
,
helvetica
,
sans-serif
;
font-size
:
1.4em
;
font-weight
:
bold
;
border-bottom
:
1px
#ccc
solid
;
padding-bottom
:
2px
;
}
TT
{
font-family
:
courier
new
;
font-weight
:
bold
;
color
:
#060
;
}
PRE
{
font-family
:
courier
new
;
font-size
:
100%
;
}
</style>
</head>
<body>
<h1>
REST API Plugin Changelog
</h1>
<p><b>
1.0.0
</b>
-- February 3rd, 2015
</p>
<ul>
<li>
UserService plugin and MUC Service plugin are merged to the REST API plugin.
</li>
<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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
REST API Plugin Changelog
</title>
<style
type=
"text/css"
>
BODY
{
font-size
:
100%
;
}
BODY
,
TD
,
TH
{
font-family
:
tahoma
,
verdana
,
arial
,
helvetica
,
sans-serif
;
font-size
:
0.8em
;
}
H2
{
font-size
:
10pt
;
font-weight
:
bold
;
padding-left
:
1em
;
}
A
:hover
{
text-decoration
:
none
;
}
H1
{
font-family
:
tahoma
,
arial
,
helvetica
,
sans-serif
;
font-size
:
1.4em
;
font-weight
:
bold
;
border-bottom
:
1px
#ccc
solid
;
padding-bottom
:
2px
;
}
TT
{
font-family
:
courier
new
;
font-weight
:
bold
;
color
:
#060
;
}
PRE
{
font-family
:
courier
new
;
font-size
:
100%
;
}
</style>
</head>
<body>
<h1>
REST API Plugin Changelog
</h1>
<p><b>
1.0.1
</b>
-- February 20th, 2015
</p>
<ul>
<li>
Added possibility to rename a user (Thanks to JustMarried plugin)
</li>
<li>
Adjusted HTTP Codes by conflict to HTTP CODE: 409
</li>
<li>
Added subject to Chat room
</li>
<li>
Disabled jersey logging on startup
</li>
<li>
By create a new chat room the chat room service will be created if it was not there
</li>
</ul>
<p><b>
1.0.0
</b>
-- February 3rd, 2015
</p>
<ul>
<li>
UserService plugin and MUC Service plugin are merged to the REST API plugin.
</li>
<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>
src/plugins/restAPI/plugin.xml
View file @
090831d3
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>
org.jivesoftware.openfire.plugin.rest.RESTServicePlugin
</class>
<name>
REST API
</name>
<description>
Allows administration over a RESTful API.
</description>
<author>
Roman Soldatow
</author>
<version>
1.0.
0
</version>
<date>
02/
03/2015
</date>
<minServerVersion>
3.9.0
</minServerVersion>
<adminconsole>
<tab
id=
"tab-server"
>
<sidebar
id=
"sidebar-server-settings"
>
<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"
/>
</sidebar>
</tab>
</adminconsole>
</plugin>
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>
org.jivesoftware.openfire.plugin.rest.RESTServicePlugin
</class>
<name>
REST API
</name>
<description>
Allows administration over a RESTful API.
</description>
<author>
Roman Soldatow
</author>
<version>
1.0.
1
</version>
<date>
02/
20/2015
</date>
<minServerVersion>
3.9.0
</minServerVersion>
<adminconsole>
<tab
id=
"tab-server"
>
<sidebar
id=
"sidebar-server-settings"
>
<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"
/>
</sidebar>
</tab>
</adminconsole>
</plugin>
src/plugins/restAPI/readme.html
View file @
090831d3
This source diff could not be displayed because it is too large. You can
view the blob
instead.
src/plugins/restAPI/src/java/org/jivesoftware/openfire/plugin/rest/controller/JustMarriedController.java
0 → 100644
View file @
090831d3
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
);
}
}
}
}
src/plugins/restAPI/src/java/org/jivesoftware/openfire/plugin/rest/controller/MUCRoomController.java
View file @
090831d3
package
org
.
jivesoftware
.
openfire
.
plugin
.
rest
.
controller
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Date
;
import
java.util.List
;
import
javax.ws.rs.core.Response
;
import
org.jivesoftware.openfire.XMPPServer
;
import
org.jivesoftware.openfire.plugin.rest.entity.MUCChannelType
;
import
org.jivesoftware.openfire.plugin.rest.entity.MUCRoomEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.MUCRoomEntity
;
import
org.jivesoftware.openfire.plugin.rest.entity.ParticipantEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.ParticipantEntity
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException
;
import
org.jivesoftware.openfire.muc.ConflictException
;
import
org.jivesoftware.openfire.muc.ForbiddenException
;
import
org.jivesoftware.openfire.muc.MUCRole
;
import
org.jivesoftware.openfire.muc.MUCRoom
;
import
org.jivesoftware.openfire.muc.NotAllowedException
;
import
org.jivesoftware.openfire.plugin.rest.utils.MUCRoomUtils
;
import
org.jivesoftware.openfire.plugin.rest.utils.UserUtils
;
import
org.xmpp.packet.JID
;
/**
* The Class MUCRoomController.
*/
public
class
MUCRoomController
{
/** The Constant INSTANCE. */
public
static
final
MUCRoomController
INSTANCE
=
new
MUCRoomController
();
/**
* Gets the single instance of MUCRoomController.
*
* @return single instance of MUCRoomController
*/
public
static
MUCRoomController
getInstance
()
{
return
INSTANCE
;
}
/**
* Gets the chat rooms.
*
* @param serviceName
* the service name
* @param channelType
* the channel type
* @param roomSearch
* the room search
* @return the chat rooms
*/
public
MUCRoomEntities
getChatRooms
(
String
serviceName
,
String
channelType
,
String
roomSearch
)
{
List
<
MUCRoom
>
rooms
=
XMPPServer
.
getInstance
().
getMultiUserChatManager
().
getMultiUserChatService
(
serviceName
)
.
getChatRooms
();
List
<
MUCRoomEntity
>
mucRoomEntities
=
new
ArrayList
<
MUCRoomEntity
>();
for
(
MUCRoom
chatRoom
:
rooms
)
{
if
(
roomSearch
!=
null
)
{
if
(!
chatRoom
.
getName
().
contains
(
roomSearch
))
{
continue
;
}
}
if
(
channelType
.
equals
(
MUCChannelType
.
ALL
))
{
mucRoomEntities
.
add
(
convertToMUCRoomEntity
(
chatRoom
));
}
else
if
(
channelType
.
equals
(
MUCChannelType
.
PUBLIC
)
&&
chatRoom
.
isPublicRoom
())
{
mucRoomEntities
.
add
(
convertToMUCRoomEntity
(
chatRoom
));
}
}
return
new
MUCRoomEntities
(
mucRoomEntities
);
}
/**
* Gets the chat room.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @return the chat room
* @throws ServiceException
* the service exception
*/
public
MUCRoomEntity
getChatRoom
(
String
roomName
,
String
serviceName
)
throws
ServiceException
{
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
);
}
MUCRoomEntity
mucRoomEntity
=
convertToMUCRoomEntity
(
chatRoom
);
return
mucRoomEntity
;
}
/**
* Delete chat room.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @throws ServiceException
* the service exception
*/
public
void
deleteChatRoom
(
String
roomName
,
String
serviceName
)
throws
ServiceException
{
MUCRoom
chatRoom
=
XMPPServer
.
getInstance
().
getMultiUserChatManager
().
getMultiUserChatService
(
serviceName
)
.
getChatRoom
(
roomName
.
toLowerCase
());
if
(
chatRoom
!=
null
)
{
chatRoom
.
destroyRoom
(
null
,
null
);
}
else
{
throw
new
ServiceException
(
"Could not remove the channel"
,
roomName
,
ExceptionType
.
ROOM_NOT_FOUND
,
Response
.
Status
.
NOT_FOUND
);
}
}
/**
* Creates the chat room.
*
* @param serviceName
* the service name
* @param mucRoomEntity
* the MUC room entity
* @throws ServiceException
* the service exception
*/
public
void
createChatRoom
(
String
serviceName
,
MUCRoomEntity
mucRoomEntity
)
throws
ServiceException
{
try
{
createRoom
(
mucRoomEntity
,
serviceName
);
}
catch
(
NotAllowedException
e
)
{
throw
new
ServiceException
(
"Could not create the channel"
,
mucRoomEntity
.
getRoomName
(),
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not create the channel"
,
mucRoomEntity
.
getRoomName
(),
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ConflictException
e
)
{
throw
new
ServiceException
(
"Could not create the channel"
,
mucRoomEntity
.
getRoomName
(),
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
}
/**
* Update chat room.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @param mucRoomEntity
* the MUC room entity
* @throws ServiceException
* the service exception
*/
public
void
updateChatRoom
(
String
roomName
,
String
serviceName
,
MUCRoomEntity
mucRoomEntity
)
throws
ServiceException
{
try
{
// If the room name is different throw exception
if
(!
roomName
.
equals
(
mucRoomEntity
.
getRoomName
()))
{
throw
new
ServiceException
(
"Could not update the channel. The room name is different to the entity room name."
,
roomName
,
ExceptionType
.
ILLEGAL_ARGUMENT_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
);
}
createRoom
(
mucRoomEntity
,
serviceName
);
}
catch
(
NotAllowedException
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
.
FORBIDDEN
,
e
);
}
}
/**
* Creates the room.
*
* @param mucRoomEntity
* the MUC room entity
* @param serviceName
* the service name
* @throws NotAllowedException
* the not allowed exception
* @throws ForbiddenException
* the forbidden exception
* @throws ConflictException
* the conflict exception
*/
private
void
createRoom
(
MUCRoomEntity
mucRoomEntity
,
String
serviceName
)
throws
NotAllowedException
,
ForbiddenException
,
ConflictException
{
// Set owner
JID
owner
=
XMPPServer
.
getInstance
().
createJID
(
"admin"
,
null
);
if
(
mucRoomEntity
.
getOwners
()
!=
null
&&
mucRoomEntity
.
getOwners
().
size
()
>
0
)
{
owner
=
new
JID
(
mucRoomEntity
.
getOwners
().
get
(
0
));
}
else
{
List
<
String
>
owners
=
new
ArrayList
<
String
>();
owners
.
add
(
owner
.
toBareJID
());
mucRoomEntity
.
setOwners
(
owners
);
}
MUCRoom
room
=
XMPPServer
.
getInstance
().
getMultiUserChatManager
().
getMultiUserChatService
(
serviceName
)
.
getChatRoom
(
mucRoomEntity
.
getRoomName
().
toLowerCase
(),
owner
);
// Set values
room
.
setNaturalLanguageName
(
mucRoomEntity
.
getNaturalName
());
room
.
setSubject
(
mucRoomEntity
.
getSubject
());
room
.
setDescription
(
mucRoomEntity
.
getDescription
());
room
.
setPassword
(
mucRoomEntity
.
getPassword
());
room
.
setPersistent
(
mucRoomEntity
.
isPersistent
());
room
.
setPublicRoom
(
mucRoomEntity
.
isPublicRoom
());
room
.
setRegistrationEnabled
(
mucRoomEntity
.
isRegistrationEnabled
());
room
.
setCanAnyoneDiscoverJID
(
mucRoomEntity
.
isCanAnyoneDiscoverJID
());
room
.
setCanOccupantsChangeSubject
(
mucRoomEntity
.
isCanOccupantsChangeSubject
());
room
.
setCanOccupantsInvite
(
mucRoomEntity
.
isCanOccupantsInvite
());
room
.
setChangeNickname
(
mucRoomEntity
.
isCanChangeNickname
());
room
.
setModificationDate
(
mucRoomEntity
.
getModificationDate
());
room
.
setLogEnabled
(
mucRoomEntity
.
isLogEnabled
());
room
.
setLoginRestrictedToNickname
(
mucRoomEntity
.
isLoginRestrictedToNickname
());
room
.
setMaxUsers
(
mucRoomEntity
.
getMaxUsers
());
room
.
setMembersOnly
(
mucRoomEntity
.
isMembersOnly
());
room
.
setModerated
(
mucRoomEntity
.
isModerated
());
// Set broadcast presence roles
if
(
mucRoomEntity
.
getBroadcastPresenceRoles
()
!=
null
)
{
room
.
setRolesToBroadcastPresence
(
mucRoomEntity
.
getBroadcastPresenceRoles
());
}
else
{
room
.
setRolesToBroadcastPresence
(
new
ArrayList
<
String
>());
}
// Set all roles
setRoles
(
room
,
mucRoomEntity
);
// Set creation date
if
(
mucRoomEntity
.
getCreationDate
()
!=
null
)
{
room
.
setCreationDate
(
mucRoomEntity
.
getCreationDate
());
}
else
{
room
.
setCreationDate
(
new
Date
());
}
// Set modification date
if
(
mucRoomEntity
.
getModificationDate
()
!=
null
)
{
room
.
setModificationDate
(
mucRoomEntity
.
getModificationDate
());
}
else
{
room
.
setModificationDate
(
new
Date
());
}
// Unlock the room, because the default configuration lock the room.
room
.
unlock
(
room
.
getRole
());
// Save the room to the DB if the room should be persistant
if
(
room
.
isPersistent
())
{
room
.
saveToDB
();
}
}
/**
* Gets the room participants.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @return the room participants
*/
public
ParticipantEntities
getRoomParticipants
(
String
roomName
,
String
serviceName
)
{
ParticipantEntities
participantEntities
=
new
ParticipantEntities
();
List
<
ParticipantEntity
>
participants
=
new
ArrayList
<
ParticipantEntity
>();
Collection
<
MUCRole
>
serverParticipants
=
XMPPServer
.
getInstance
().
getMultiUserChatManager
()
.
getMultiUserChatService
(
serviceName
).
getChatRoom
(
roomName
).
getParticipants
();
for
(
MUCRole
role
:
serverParticipants
)
{
ParticipantEntity
participantEntity
=
new
ParticipantEntity
();
participantEntity
.
setJid
(
role
.
getRoleAddress
().
toFullJID
());
participantEntity
.
setRole
(
role
.
getRole
().
name
());
participantEntity
.
setAffiliation
(
role
.
getAffiliation
().
name
());
participants
.
add
(
participantEntity
);
}
participantEntities
.
setParticipants
(
participants
);
return
participantEntities
;
}
/**
* Convert to MUC room entity.
*
* @param room
* the room
* @return the MUC room entity
*/
public
MUCRoomEntity
convertToMUCRoomEntity
(
MUCRoom
room
)
{
MUCRoomEntity
mucRoomEntity
=
new
MUCRoomEntity
(
room
.
getNaturalLanguageName
(),
room
.
getName
(),
room
.
getDescription
());
mucRoomEntity
.
setCanAnyoneDiscoverJID
(
room
.
canAnyoneDiscoverJID
());
mucRoomEntity
.
setCanChangeNickname
(
room
.
canChangeNickname
());
mucRoomEntity
.
setCanOccupantsChangeSubject
(
room
.
canOccupantsChangeSubject
());
mucRoomEntity
.
setCanOccupantsInvite
(
room
.
canOccupantsInvite
());
mucRoomEntity
.
setPublicRoom
(
room
.
isPublicRoom
());
mucRoomEntity
.
setPassword
(
room
.
getPassword
());
mucRoomEntity
.
setPersistent
(
room
.
isPersistent
());
mucRoomEntity
.
setRegistrationEnabled
(
room
.
isRegistrationEnabled
());
mucRoomEntity
.
setLogEnabled
(
room
.
isLogEnabled
());
mucRoomEntity
.
setLoginRestrictedToNickname
(
room
.
isLoginRestrictedToNickname
());
mucRoomEntity
.
setMaxUsers
(
room
.
getMaxUsers
());
mucRoomEntity
.
setMembersOnly
(
room
.
isMembersOnly
());
mucRoomEntity
.
setModerated
(
room
.
isModerated
());
mucRoomEntity
.
setOwners
(
MUCRoomUtils
.
convertJIDsToStringList
(
room
.
getOwners
()));
mucRoomEntity
.
setAdmins
(
MUCRoomUtils
.
convertJIDsToStringList
(
room
.
getAdmins
()));
mucRoomEntity
.
setMembers
(
MUCRoomUtils
.
convertJIDsToStringList
(
room
.
getMembers
()));
mucRoomEntity
.
setOutcasts
(
MUCRoomUtils
.
convertJIDsToStringList
(
room
.
getOutcasts
()));
mucRoomEntity
.
setBroadcastPresenceRoles
(
room
.
getRolesToBroadcastPresence
());
mucRoomEntity
.
setCreationDate
(
room
.
getCreationDate
());
mucRoomEntity
.
setModificationDate
(
room
.
getModificationDate
());
return
mucRoomEntity
;
}
/**
* Reset roles.
*
* @param room
* the room
* @param mucRoomEntity
* the muc room entity
* @throws ForbiddenException
* the forbidden exception
* @throws NotAllowedException
* the not allowed exception
* @throws ConflictException
* the conflict exception
*/
private
void
setRoles
(
MUCRoom
room
,
MUCRoomEntity
mucRoomEntity
)
throws
ForbiddenException
,
NotAllowedException
,
ConflictException
{
List
<
JID
>
roles
=
new
ArrayList
<
JID
>();
Collection
<
JID
>
owners
=
new
ArrayList
<
JID
>();
Collection
<
JID
>
existingOwners
=
new
ArrayList
<
JID
>();
List
<
JID
>
mucRoomEntityOwners
=
MUCRoomUtils
.
convertStringsToJIDs
(
mucRoomEntity
.
getOwners
());
owners
.
addAll
(
room
.
getOwners
());
// Find same owners
for
(
JID
jid
:
owners
)
{
if
(
mucRoomEntityOwners
.
contains
(
jid
))
{
existingOwners
.
add
(
jid
);
}
}
// Don't delete the same owners
owners
.
removeAll
(
existingOwners
);
room
.
addOwners
(
MUCRoomUtils
.
convertStringsToJIDs
(
mucRoomEntity
.
getOwners
()),
room
.
getRole
());
// Collect all roles to reset
roles
.
addAll
(
owners
);
roles
.
addAll
(
room
.
getAdmins
());
roles
.
addAll
(
room
.
getMembers
());
roles
.
addAll
(
room
.
getOutcasts
());
for
(
JID
jid
:
roles
)
{
room
.
addNone
(
jid
,
room
.
getRole
());
}
room
.
addOwners
(
MUCRoomUtils
.
convertStringsToJIDs
(
mucRoomEntity
.
getOwners
()),
room
.
getRole
());
if
(
mucRoomEntity
.
getAdmins
()
!=
null
)
{
room
.
addAdmins
(
MUCRoomUtils
.
convertStringsToJIDs
(
mucRoomEntity
.
getAdmins
()),
room
.
getRole
());
}
if
(
mucRoomEntity
.
getMembers
()
!=
null
)
{
for
(
String
memberJid
:
mucRoomEntity
.
getMembers
())
{
room
.
addMember
(
new
JID
(
memberJid
),
null
,
room
.
getRole
());
}
}
if
(
mucRoomEntity
.
getOutcasts
()
!=
null
)
{
for
(
String
outcastJid
:
mucRoomEntity
.
getOutcasts
())
{
room
.
addOutcast
(
new
JID
(
outcastJid
),
null
,
room
.
getRole
());
}
}
}
/**
* Adds the admin.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
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
());
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not add admin"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ConflictException
e
)
{
throw
new
ServiceException
(
"Could not add admin"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
}
/**
* Adds the owner.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
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
());
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not add owner"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
}
/**
* Adds the member.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
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
());
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not add member"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ConflictException
e
)
{
throw
new
ServiceException
(
"Could not add member"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
}
/**
* Adds the outcast.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
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
());
}
catch
(
NotAllowedException
e
)
{
throw
new
ServiceException
(
"Could not add outcast"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not add outcast"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ConflictException
e
)
{
throw
new
ServiceException
(
"Could not add outcast"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
}
/**
* Delete affiliation.
*
* @param serviceName
* the service name
* @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
.
FORBIDDEN
,
e
);
}
}
package
org
.
jivesoftware
.
openfire
.
plugin
.
rest
.
controller
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Date
;
import
java.util.List
;
import
javax.ws.rs.core.Response
;
import
org.jivesoftware.openfire.XMPPServer
;
import
org.jivesoftware.openfire.plugin.rest.entity.MUCChannelType
;
import
org.jivesoftware.openfire.plugin.rest.entity.MUCRoomEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.MUCRoomEntity
;
import
org.jivesoftware.openfire.plugin.rest.entity.ParticipantEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.ParticipantEntity
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException
;
import
org.jivesoftware.openfire.muc.ConflictException
;
import
org.jivesoftware.openfire.muc.ForbiddenException
;
import
org.jivesoftware.openfire.muc.MUCRole
;
import
org.jivesoftware.openfire.muc.MUCRoom
;
import
org.jivesoftware.openfire.muc.NotAllowedException
;
import
org.jivesoftware.openfire.plugin.rest.utils.MUCRoomUtils
;
import
org.jivesoftware.openfire.plugin.rest.utils.UserUtils
;
import
org.jivesoftware.util.AlreadyExistsException
;
import
org.xmpp.packet.JID
;
/**
* The Class MUCRoomController.
*/
public
class
MUCRoomController
{
/** The Constant INSTANCE. */
public
static
final
MUCRoomController
INSTANCE
=
new
MUCRoomController
();
/**
* Gets the single instance of MUCRoomController.
*
* @return single instance of MUCRoomController
*/
public
static
MUCRoomController
getInstance
()
{
return
INSTANCE
;
}
/**
* Gets the chat rooms.
*
* @param serviceName
* the service name
* @param channelType
* the channel type
* @param roomSearch
* the room search
* @return the chat rooms
*/
public
MUCRoomEntities
getChatRooms
(
String
serviceName
,
String
channelType
,
String
roomSearch
)
{
List
<
MUCRoom
>
rooms
=
XMPPServer
.
getInstance
().
getMultiUserChatManager
().
getMultiUserChatService
(
serviceName
)
.
getChatRooms
();
List
<
MUCRoomEntity
>
mucRoomEntities
=
new
ArrayList
<
MUCRoomEntity
>();
for
(
MUCRoom
chatRoom
:
rooms
)
{
if
(
roomSearch
!=
null
)
{
if
(!
chatRoom
.
getName
().
contains
(
roomSearch
))
{
continue
;
}
}
if
(
channelType
.
equals
(
MUCChannelType
.
ALL
))
{
mucRoomEntities
.
add
(
convertToMUCRoomEntity
(
chatRoom
));
}
else
if
(
channelType
.
equals
(
MUCChannelType
.
PUBLIC
)
&&
chatRoom
.
isPublicRoom
())
{
mucRoomEntities
.
add
(
convertToMUCRoomEntity
(
chatRoom
));
}
}
return
new
MUCRoomEntities
(
mucRoomEntities
);
}
/**
* Gets the chat room.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @return the chat room
* @throws ServiceException
* the service exception
*/
public
MUCRoomEntity
getChatRoom
(
String
roomName
,
String
serviceName
)
throws
ServiceException
{
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
);
}
MUCRoomEntity
mucRoomEntity
=
convertToMUCRoomEntity
(
chatRoom
);
return
mucRoomEntity
;
}
/**
* Delete chat room.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @throws ServiceException
* the service exception
*/
public
void
deleteChatRoom
(
String
roomName
,
String
serviceName
)
throws
ServiceException
{
MUCRoom
chatRoom
=
XMPPServer
.
getInstance
().
getMultiUserChatManager
().
getMultiUserChatService
(
serviceName
)
.
getChatRoom
(
roomName
.
toLowerCase
());
if
(
chatRoom
!=
null
)
{
chatRoom
.
destroyRoom
(
null
,
null
);
}
else
{
throw
new
ServiceException
(
"Could not remove the channel"
,
roomName
,
ExceptionType
.
ROOM_NOT_FOUND
,
Response
.
Status
.
NOT_FOUND
);
}
}
/**
* Creates the chat room.
*
* @param serviceName
* the service name
* @param mucRoomEntity
* the MUC room entity
* @throws ServiceException
* the service exception
*/
public
void
createChatRoom
(
String
serviceName
,
MUCRoomEntity
mucRoomEntity
)
throws
ServiceException
{
try
{
createRoom
(
mucRoomEntity
,
serviceName
);
}
catch
(
NotAllowedException
e
)
{
throw
new
ServiceException
(
"Could not create the channel"
,
mucRoomEntity
.
getRoomName
(),
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not create the channel"
,
mucRoomEntity
.
getRoomName
(),
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ConflictException
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
* the service name
* @param mucRoomEntity
* the MUC room entity
* @throws ServiceException
* the service exception
*/
public
void
updateChatRoom
(
String
roomName
,
String
serviceName
,
MUCRoomEntity
mucRoomEntity
)
throws
ServiceException
{
try
{
// If the room name is different throw exception
if
(!
roomName
.
equals
(
mucRoomEntity
.
getRoomName
()))
{
throw
new
ServiceException
(
"Could not update the channel. The room name is different to the entity room name."
,
roomName
,
ExceptionType
.
ILLEGAL_ARGUMENT_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
);
}
createRoom
(
mucRoomEntity
,
serviceName
);
}
catch
(
NotAllowedException
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
);
}
catch
(
AlreadyExistsException
e
)
{
throw
new
ServiceException
(
"Could not update the channel"
,
mucRoomEntity
.
getRoomName
(),
ExceptionType
.
ALREADY_EXISTS
,
Response
.
Status
.
CONFLICT
,
e
);
}
}
/**
* Creates the room.
*
* @param mucRoomEntity
* the MUC room entity
* @param serviceName
* the service name
* @throws NotAllowedException
* the not allowed exception
* @throws ForbiddenException
* the forbidden exception
* @throws ConflictException
* the conflict exception
* @throws AlreadyExistsException
*/
private
void
createRoom
(
MUCRoomEntity
mucRoomEntity
,
String
serviceName
)
throws
NotAllowedException
,
ForbiddenException
,
ConflictException
,
AlreadyExistsException
{
// Set owner
JID
owner
=
XMPPServer
.
getInstance
().
createJID
(
"admin"
,
null
);
if
(
mucRoomEntity
.
getOwners
()
!=
null
&&
mucRoomEntity
.
getOwners
().
size
()
>
0
)
{
owner
=
new
JID
(
mucRoomEntity
.
getOwners
().
get
(
0
));
}
else
{
List
<
String
>
owners
=
new
ArrayList
<
String
>();
owners
.
add
(
owner
.
toBareJID
());
mucRoomEntity
.
setOwners
(
owners
);
}
// Check if chat service is available, if not create a new one
boolean
serviceRegistered
=
XMPPServer
.
getInstance
().
getMultiUserChatManager
().
isServiceRegistered
(
serviceName
);
if
(!
serviceRegistered
)
{
XMPPServer
.
getInstance
().
getMultiUserChatManager
().
createMultiUserChatService
(
serviceName
,
serviceName
,
false
);
}
MUCRoom
room
=
XMPPServer
.
getInstance
().
getMultiUserChatManager
().
getMultiUserChatService
(
serviceName
)
.
getChatRoom
(
mucRoomEntity
.
getRoomName
().
toLowerCase
(),
owner
);
// Set values
room
.
setNaturalLanguageName
(
mucRoomEntity
.
getNaturalName
());
room
.
setSubject
(
mucRoomEntity
.
getSubject
());
room
.
setDescription
(
mucRoomEntity
.
getDescription
());
room
.
setPassword
(
mucRoomEntity
.
getPassword
());
room
.
setPersistent
(
mucRoomEntity
.
isPersistent
());
room
.
setPublicRoom
(
mucRoomEntity
.
isPublicRoom
());
room
.
setRegistrationEnabled
(
mucRoomEntity
.
isRegistrationEnabled
());
room
.
setCanAnyoneDiscoverJID
(
mucRoomEntity
.
isCanAnyoneDiscoverJID
());
room
.
setCanOccupantsChangeSubject
(
mucRoomEntity
.
isCanOccupantsChangeSubject
());
room
.
setCanOccupantsInvite
(
mucRoomEntity
.
isCanOccupantsInvite
());
room
.
setChangeNickname
(
mucRoomEntity
.
isCanChangeNickname
());
room
.
setModificationDate
(
mucRoomEntity
.
getModificationDate
());
room
.
setLogEnabled
(
mucRoomEntity
.
isLogEnabled
());
room
.
setLoginRestrictedToNickname
(
mucRoomEntity
.
isLoginRestrictedToNickname
());
room
.
setMaxUsers
(
mucRoomEntity
.
getMaxUsers
());
room
.
setMembersOnly
(
mucRoomEntity
.
isMembersOnly
());
room
.
setModerated
(
mucRoomEntity
.
isModerated
());
// Set broadcast presence roles
if
(
mucRoomEntity
.
getBroadcastPresenceRoles
()
!=
null
)
{
room
.
setRolesToBroadcastPresence
(
mucRoomEntity
.
getBroadcastPresenceRoles
());
}
else
{
room
.
setRolesToBroadcastPresence
(
new
ArrayList
<
String
>());
}
// Set all roles
setRoles
(
room
,
mucRoomEntity
);
// Set creation date
if
(
mucRoomEntity
.
getCreationDate
()
!=
null
)
{
room
.
setCreationDate
(
mucRoomEntity
.
getCreationDate
());
}
else
{
room
.
setCreationDate
(
new
Date
());
}
// Set modification date
if
(
mucRoomEntity
.
getModificationDate
()
!=
null
)
{
room
.
setModificationDate
(
mucRoomEntity
.
getModificationDate
());
}
else
{
room
.
setModificationDate
(
new
Date
());
}
// Unlock the room, because the default configuration lock the room.
room
.
unlock
(
room
.
getRole
());
// Save the room to the DB if the room should be persistant
if
(
room
.
isPersistent
())
{
room
.
saveToDB
();
}
}
/**
* Gets the room participants.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @return the room participants
*/
public
ParticipantEntities
getRoomParticipants
(
String
roomName
,
String
serviceName
)
{
ParticipantEntities
participantEntities
=
new
ParticipantEntities
();
List
<
ParticipantEntity
>
participants
=
new
ArrayList
<
ParticipantEntity
>();
Collection
<
MUCRole
>
serverParticipants
=
XMPPServer
.
getInstance
().
getMultiUserChatManager
()
.
getMultiUserChatService
(
serviceName
).
getChatRoom
(
roomName
).
getParticipants
();
for
(
MUCRole
role
:
serverParticipants
)
{
ParticipantEntity
participantEntity
=
new
ParticipantEntity
();
participantEntity
.
setJid
(
role
.
getRoleAddress
().
toFullJID
());
participantEntity
.
setRole
(
role
.
getRole
().
name
());
participantEntity
.
setAffiliation
(
role
.
getAffiliation
().
name
());
participants
.
add
(
participantEntity
);
}
participantEntities
.
setParticipants
(
participants
);
return
participantEntities
;
}
/**
* Convert to MUC room entity.
*
* @param room
* the room
* @return the MUC room entity
*/
public
MUCRoomEntity
convertToMUCRoomEntity
(
MUCRoom
room
)
{
MUCRoomEntity
mucRoomEntity
=
new
MUCRoomEntity
(
room
.
getNaturalLanguageName
(),
room
.
getName
(),
room
.
getDescription
());
mucRoomEntity
.
setSubject
(
room
.
getSubject
());
mucRoomEntity
.
setCanAnyoneDiscoverJID
(
room
.
canAnyoneDiscoverJID
());
mucRoomEntity
.
setCanChangeNickname
(
room
.
canChangeNickname
());
mucRoomEntity
.
setCanOccupantsChangeSubject
(
room
.
canOccupantsChangeSubject
());
mucRoomEntity
.
setCanOccupantsInvite
(
room
.
canOccupantsInvite
());
mucRoomEntity
.
setPublicRoom
(
room
.
isPublicRoom
());
mucRoomEntity
.
setPassword
(
room
.
getPassword
());
mucRoomEntity
.
setPersistent
(
room
.
isPersistent
());
mucRoomEntity
.
setRegistrationEnabled
(
room
.
isRegistrationEnabled
());
mucRoomEntity
.
setLogEnabled
(
room
.
isLogEnabled
());
mucRoomEntity
.
setLoginRestrictedToNickname
(
room
.
isLoginRestrictedToNickname
());
mucRoomEntity
.
setMaxUsers
(
room
.
getMaxUsers
());
mucRoomEntity
.
setMembersOnly
(
room
.
isMembersOnly
());
mucRoomEntity
.
setModerated
(
room
.
isModerated
());
mucRoomEntity
.
setOwners
(
MUCRoomUtils
.
convertJIDsToStringList
(
room
.
getOwners
()));
mucRoomEntity
.
setAdmins
(
MUCRoomUtils
.
convertJIDsToStringList
(
room
.
getAdmins
()));
mucRoomEntity
.
setMembers
(
MUCRoomUtils
.
convertJIDsToStringList
(
room
.
getMembers
()));
mucRoomEntity
.
setOutcasts
(
MUCRoomUtils
.
convertJIDsToStringList
(
room
.
getOutcasts
()));
mucRoomEntity
.
setBroadcastPresenceRoles
(
room
.
getRolesToBroadcastPresence
());
mucRoomEntity
.
setCreationDate
(
room
.
getCreationDate
());
mucRoomEntity
.
setModificationDate
(
room
.
getModificationDate
());
return
mucRoomEntity
;
}
/**
* Reset roles.
*
* @param room
* the room
* @param mucRoomEntity
* the muc room entity
* @throws ForbiddenException
* the forbidden exception
* @throws NotAllowedException
* the not allowed exception
* @throws ConflictException
* the conflict exception
*/
private
void
setRoles
(
MUCRoom
room
,
MUCRoomEntity
mucRoomEntity
)
throws
ForbiddenException
,
NotAllowedException
,
ConflictException
{
List
<
JID
>
roles
=
new
ArrayList
<
JID
>();
Collection
<
JID
>
owners
=
new
ArrayList
<
JID
>();
Collection
<
JID
>
existingOwners
=
new
ArrayList
<
JID
>();
List
<
JID
>
mucRoomEntityOwners
=
MUCRoomUtils
.
convertStringsToJIDs
(
mucRoomEntity
.
getOwners
());
owners
.
addAll
(
room
.
getOwners
());
// Find same owners
for
(
JID
jid
:
owners
)
{
if
(
mucRoomEntityOwners
.
contains
(
jid
))
{
existingOwners
.
add
(
jid
);
}
}
// Don't delete the same owners
owners
.
removeAll
(
existingOwners
);
room
.
addOwners
(
MUCRoomUtils
.
convertStringsToJIDs
(
mucRoomEntity
.
getOwners
()),
room
.
getRole
());
// Collect all roles to reset
roles
.
addAll
(
owners
);
roles
.
addAll
(
room
.
getAdmins
());
roles
.
addAll
(
room
.
getMembers
());
roles
.
addAll
(
room
.
getOutcasts
());
for
(
JID
jid
:
roles
)
{
room
.
addNone
(
jid
,
room
.
getRole
());
}
room
.
addOwners
(
MUCRoomUtils
.
convertStringsToJIDs
(
mucRoomEntity
.
getOwners
()),
room
.
getRole
());
if
(
mucRoomEntity
.
getAdmins
()
!=
null
)
{
room
.
addAdmins
(
MUCRoomUtils
.
convertStringsToJIDs
(
mucRoomEntity
.
getAdmins
()),
room
.
getRole
());
}
if
(
mucRoomEntity
.
getMembers
()
!=
null
)
{
for
(
String
memberJid
:
mucRoomEntity
.
getMembers
())
{
room
.
addMember
(
new
JID
(
memberJid
),
null
,
room
.
getRole
());
}
}
if
(
mucRoomEntity
.
getOutcasts
()
!=
null
)
{
for
(
String
outcastJid
:
mucRoomEntity
.
getOutcasts
())
{
room
.
addOutcast
(
new
JID
(
outcastJid
),
null
,
room
.
getRole
());
}
}
}
/**
* Adds the admin.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
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
());
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not add admin"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ConflictException
e
)
{
throw
new
ServiceException
(
"Could not add admin"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
CONFLICT
,
e
);
}
}
/**
* Adds the owner.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
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
());
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not add owner"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
}
/**
* Adds the member.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
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
());
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not add member"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ConflictException
e
)
{
throw
new
ServiceException
(
"Could not add member"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
}
/**
* Adds the outcast.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
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
());
}
catch
(
NotAllowedException
e
)
{
throw
new
ServiceException
(
"Could not add outcast"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ForbiddenException
e
)
{
throw
new
ServiceException
(
"Could not add outcast"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
FORBIDDEN
,
e
);
}
catch
(
ConflictException
e
)
{
throw
new
ServiceException
(
"Could not add outcast"
,
jid
,
ExceptionType
.
NOT_ALLOWED
,
Response
.
Status
.
CONFLICT
,
e
);
}
}
/**
* Delete affiliation.
*
* @param serviceName
* the service name
* @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
src/plugins/restAPI/src/java/org/jivesoftware/openfire/plugin/rest/controller/UserServiceController.java
View file @
090831d3
package
org
.
jivesoftware
.
openfire
.
plugin
.
rest
.
controller
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.List
;
import
javax.ws.rs.core.Response
;
import
org.jivesoftware.openfire.SharedGroupException
;
import
org.jivesoftware.openfire.XMPPServer
;
import
org.jivesoftware.openfire.plugin.rest.entity.RosterEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity
;
import
org.jivesoftware.openfire.plugin.rest.entity.UserEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.UserEntity
;
import
org.jivesoftware.openfire.plugin.rest.entity.UserGroupsEntity
;
import
org.jivesoftware.openfire.plugin.rest.entity.UserProperty
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException
;
import
org.jivesoftware.openfire.group.Group
;
import
org.jivesoftware.openfire.group.GroupAlreadyExistsException
;
import
org.jivesoftware.openfire.group.GroupManager
;
import
org.jivesoftware.openfire.group.GroupNotFoundException
;
import
org.jivesoftware.openfire.lockout.LockOutManager
;
import
org.jivesoftware.openfire.plugin.rest.dao.PropertyDAO
;
import
org.jivesoftware.openfire.roster.Roster
;
import
org.jivesoftware.openfire.roster.RosterItem
;
import
org.jivesoftware.openfire.roster.RosterManager
;
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.plugin.rest.utils.UserUtils
;
import
org.xmpp.packet.JID
;
/**
* The Class UserServiceController.
*/
public
class
UserServiceController
{
/** The Constant INSTANCE. */
public
static
final
UserServiceController
INSTANCE
=
new
UserServiceController
();
/** The user manager. */
private
UserManager
userManager
;
/** The roster manager. */
private
RosterManager
rosterManager
;
/** The server. */
private
XMPPServer
server
;
/**
* Gets the single instance of UserServiceController.
*
* @return single instance of UserServiceController
*/
public
static
UserServiceController
getInstance
()
{
return
INSTANCE
;
}
/**
* Instantiates a new user service controller.
*/
private
UserServiceController
()
{
server
=
XMPPServer
.
getInstance
();
userManager
=
server
.
getUserManager
();
rosterManager
=
server
.
getRosterManager
();
}
/**
* Creates the user.
*
* @param userEntity
* the user entity
* @throws ServiceException
* the service exception
*/
public
void
createUser
(
UserEntity
userEntity
)
throws
ServiceException
{
if
(
userEntity
!=
null
&&
!
userEntity
.
getUsername
().
isEmpty
())
{
if
(
userEntity
.
getPassword
()
==
null
)
{
throw
new
ServiceException
(
"Could not create new user, because password is null"
,
userEntity
.
getUsername
(),
"PasswordIsNull"
,
Response
.
Status
.
BAD_REQUEST
);
}
try
{
userManager
.
createUser
(
userEntity
.
getUsername
(),
userEntity
.
getPassword
(),
userEntity
.
getName
(),
userEntity
.
getEmail
());
}
catch
(
UserAlreadyExistsException
e
)
{
throw
new
ServiceException
(
"Could not create new user"
,
userEntity
.
getUsername
(),
ExceptionType
.
USER_ALREADY_EXISTS_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
);
}
addProperties
(
userEntity
);
}
}
/**
* Update user.
*
* @param username
* the username
* @param userEntity
* the user entity
* @throws ServiceException
* the service exception
*/
public
void
updateUser
(
String
username
,
UserEntity
userEntity
)
throws
ServiceException
{
if
(
userEntity
!=
null
&&
!
username
.
isEmpty
())
{
User
user
=
getAndCheckUser
(
username
);
if
(
userEntity
.
getPassword
()
!=
null
)
{
user
.
setPassword
(
userEntity
.
getPassword
());
}
if
(
userEntity
.
getName
()
!=
null
)
{
user
.
setName
(
userEntity
.
getName
());
}
if
(
userEntity
.
getEmail
()
!=
null
)
{
user
.
setEmail
(
userEntity
.
getEmail
());
}
addProperties
(
userEntity
);
}
}
/**
* Delete user.
*
* @param username
* the username
* @throws ServiceException
* the service exception
*/
public
void
deleteUser
(
String
username
)
throws
ServiceException
{
User
user
=
getAndCheckUser
(
username
);
userManager
.
deleteUser
(
user
);
rosterManager
.
deleteRoster
(
server
.
createJID
(
username
,
null
));
}
/**
* Gets the user entities.
*
* @param userSearch
* the user search
* @param propertyValue
* @param propertyKey
* @return the user entities
* @throws ServiceException
*/
public
UserEntities
getUserEntities
(
String
userSearch
,
String
propertyKey
,
String
propertyValue
)
throws
ServiceException
{
if
(
propertyKey
!=
null
)
{
return
getUserEntitiesByProperty
(
propertyKey
,
propertyValue
);
}
UserEntities
userEntities
=
new
UserEntities
();
userEntities
.
setUsers
(
UserUtils
.
convertUsersToUserEntities
(
userManager
.
getUsers
(),
userSearch
));
return
userEntities
;
}
/**
* Gets the user entity.
*
* @param username
* the username
* @return the user entity
* @throws ServiceException
* the service exception
*/
public
UserEntity
getUserEntity
(
String
username
)
throws
ServiceException
{
return
UserUtils
.
convertUserToUserEntity
(
getAndCheckUser
(
username
));
}
/**
* Enable user.
*
* @param username
* the username
* @throws ServiceException
* the service exception
*/
public
void
enableUser
(
String
username
)
throws
ServiceException
{
getAndCheckUser
(
username
);
LockOutManager
.
getInstance
().
enableAccount
(
username
);
}
/**
* Disable user.
*
* @param username
* the username
* @throws ServiceException
* the service exception
*/
public
void
disableUser
(
String
username
)
throws
ServiceException
{
getAndCheckUser
(
username
);
LockOutManager
.
getInstance
().
disableAccount
(
username
,
null
,
null
);
}
/**
* Gets the roster entities.
*
* @param username
* the username
* @return the roster entities
* @throws ServiceException
* the service exception
*/
public
RosterEntities
getRosterEntities
(
String
username
)
throws
ServiceException
{
Roster
roster
=
getUserRoster
(
username
);
List
<
RosterItemEntity
>
rosterEntities
=
new
ArrayList
<
RosterItemEntity
>();
for
(
RosterItem
rosterItem
:
roster
.
getRosterItems
())
{
RosterItemEntity
rosterItemEntity
=
new
RosterItemEntity
(
rosterItem
.
getJid
().
toBareJID
(),
rosterItem
.
getNickname
(),
rosterItem
.
getSubStatus
().
getValue
());
rosterItemEntity
.
setGroups
(
rosterItem
.
getGroups
());
rosterEntities
.
add
(
rosterItemEntity
);
}
return
new
RosterEntities
(
rosterEntities
);
}
/**
* 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
);
}
}
/**
* Update roster item.
*
* @param username
* the username
* @param rosterJid
* the roster jid
* @param rosterItemEntity
* the roster item entity
* @throws ServiceException
* the service exception
* @throws UserNotFoundException
* the user not found exception
* @throws UserAlreadyExistsException
* the user already exists exception
* @throws SharedGroupException
* the shared group exception
*/
public
void
updateRosterItem
(
String
username
,
String
rosterJid
,
RosterItemEntity
rosterItemEntity
)
throws
ServiceException
,
UserNotFoundException
,
UserAlreadyExistsException
,
SharedGroupException
{
getAndCheckUser
(
username
);
Roster
roster
=
getUserRoster
(
username
);
JID
jid
=
new
JID
(
rosterJid
);
RosterItem
rosterItem
=
roster
.
getRosterItem
(
jid
);
if
(
rosterItemEntity
.
getNickname
()
!=
null
)
{
rosterItem
.
setNickname
(
rosterItemEntity
.
getNickname
());
}
if
(
rosterItemEntity
.
getGroups
()
!=
null
)
{
rosterItem
.
setGroups
(
rosterItemEntity
.
getGroups
());
}
UserUtils
.
checkSubType
(
rosterItemEntity
.
getSubscriptionType
());
rosterItem
.
setSubStatus
(
RosterItem
.
SubType
.
getTypeFromInt
(
rosterItemEntity
.
getSubscriptionType
()));
roster
.
updateRosterItem
(
rosterItem
);
}
/**
* Delete roster item.
*
* @param username
* the username
* @param rosterJid
* the roster jid
* @throws SharedGroupException
* the shared group exception
* @throws ServiceException
* the service exception
*/
public
void
deleteRosterItem
(
String
username
,
String
rosterJid
)
throws
SharedGroupException
,
ServiceException
{
getAndCheckUser
(
username
);
Roster
roster
=
getUserRoster
(
username
);
JID
jid
=
new
JID
(
rosterJid
);
if
(
roster
.
deleteRosterItem
(
jid
,
true
)
==
null
)
{
throw
new
ServiceException
(
"Roster Item could not deleted"
,
jid
.
toBareJID
(),
"RosterItemNotFound"
,
Response
.
Status
.
NOT_FOUND
);
}
}
/**
* Gets the user groups.
*
* @param username
* the username
* @return the user groups
* @throws ServiceException
* the service exception
*/
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
>();
for
(
Group
group
:
groups
)
{
groupNames
.
add
(
group
.
getName
());
}
return
groupNames
;
}
/**
* Adds the user to group.
*
* @param username the username
* @param userGroupsEntity the user groups entity
* @throws ServiceException the service exception
*/
public
void
addUserToGroups
(
String
username
,
UserGroupsEntity
userGroupsEntity
)
throws
ServiceException
{
if
(
userGroupsEntity
!=
null
)
{
Collection
<
Group
>
groups
=
new
ArrayList
<
Group
>();
for
(
String
groupName
:
userGroupsEntity
.
getGroupNames
())
{
Group
group
=
null
;
try
{
group
=
GroupManager
.
getInstance
().
getGroup
(
groupName
);
}
catch
(
GroupNotFoundException
e
)
{
// Create this group
group
=
createGroup
(
groupName
);
}
groups
.
add
(
group
);
}
for
(
Group
group
:
groups
)
{
group
.
getMembers
().
add
(
server
.
createJID
(
username
,
null
));
}
}
}
/**
* 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
));
}
}
}
/**
* Gets the user entities by property key and or value.
*
* @param propertyKey
* the property key
* @param propertyValue
* the property value (can be null)
* @return the user entities by property
* @throws ServiceException
* the service exception
*/
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
();
for
(
String
username
:
usernames
)
{
users
.
add
(
getUserEntity
(
username
));
}
userEntities
.
setUsers
(
users
);
return
userEntities
;
}
/**
* Adds the properties.
*
* @param userEntity
* the user entity
* @throws ServiceException
* the service exception
*/
private
void
addProperties
(
UserEntity
userEntity
)
throws
ServiceException
{
User
user
=
getAndCheckUser
(
userEntity
.
getUsername
());
user
.
getProperties
().
clear
();
if
(
userEntity
.
getProperties
()
!=
null
)
{
for
(
UserProperty
property
:
userEntity
.
getProperties
())
{
user
.
getProperties
().
put
(
property
.
getKey
(),
property
.
getValue
());
}
}
}
/**
* Creates the group.
*
* @param groupName the group name
* @return the group
* @throws ServiceException the service exception
*/
private
Group
createGroup
(
String
groupName
)
throws
ServiceException
{
Group
group
=
null
;
try
{
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
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
return
group
;
}
/**
* Gets the and check user.
*
* @param username
* the username
* @return the and check user
* @throws ServiceException
* the service exception
*/
private
User
getAndCheckUser
(
String
username
)
throws
ServiceException
{
JID
targetJID
=
server
.
createJID
(
username
,
null
);
if
(
targetJID
.
getNode
()
==
null
)
{
throw
new
ServiceException
(
"Could not get user"
,
username
,
ExceptionType
.
USER_NOT_FOUND_EXCEPTION
,
Response
.
Status
.
NOT_FOUND
);
}
try
{
return
userManager
.
getUser
(
targetJID
.
getNode
());
}
catch
(
UserNotFoundException
e
)
{
throw
new
ServiceException
(
"Could not get user"
,
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
.
controller
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.List
;
import
javax.ws.rs.core.Response
;
import
org.jivesoftware.openfire.SharedGroupException
;
import
org.jivesoftware.openfire.XMPPServer
;
import
org.jivesoftware.openfire.group.Group
;
import
org.jivesoftware.openfire.group.GroupAlreadyExistsException
;
import
org.jivesoftware.openfire.group.GroupManager
;
import
org.jivesoftware.openfire.group.GroupNotFoundException
;
import
org.jivesoftware.openfire.lockout.LockOutManager
;
import
org.jivesoftware.openfire.plugin.rest.dao.PropertyDAO
;
import
org.jivesoftware.openfire.plugin.rest.entity.RosterEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity
;
import
org.jivesoftware.openfire.plugin.rest.entity.UserEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.UserEntity
;
import
org.jivesoftware.openfire.plugin.rest.entity.UserGroupsEntity
;
import
org.jivesoftware.openfire.plugin.rest.entity.UserProperty
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException
;
import
org.jivesoftware.openfire.plugin.rest.utils.UserUtils
;
import
org.jivesoftware.openfire.roster.Roster
;
import
org.jivesoftware.openfire.roster.RosterItem
;
import
org.jivesoftware.openfire.roster.RosterManager
;
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.xmpp.packet.JID
;
/**
* The Class UserServiceController.
*/
public
class
UserServiceController
{
/** The Constant INSTANCE. */
public
static
final
UserServiceController
INSTANCE
=
new
UserServiceController
();
/** The user manager. */
private
UserManager
userManager
;
/** The roster manager. */
private
RosterManager
rosterManager
;
/** The server. */
private
XMPPServer
server
;
/**
* Gets the single instance of UserServiceController.
*
* @return single instance of UserServiceController
*/
public
static
UserServiceController
getInstance
()
{
return
INSTANCE
;
}
/**
* Instantiates a new user service controller.
*/
private
UserServiceController
()
{
server
=
XMPPServer
.
getInstance
();
userManager
=
server
.
getUserManager
();
rosterManager
=
server
.
getRosterManager
();
}
/**
* Creates the user.
*
* @param userEntity
* the user entity
* @throws ServiceException
* the service exception
*/
public
void
createUser
(
UserEntity
userEntity
)
throws
ServiceException
{
if
(
userEntity
!=
null
&&
!
userEntity
.
getUsername
().
isEmpty
())
{
if
(
userEntity
.
getPassword
()
==
null
)
{
throw
new
ServiceException
(
"Could not create new user, because password is null"
,
userEntity
.
getUsername
(),
"PasswordIsNull"
,
Response
.
Status
.
BAD_REQUEST
);
}
try
{
userManager
.
createUser
(
userEntity
.
getUsername
(),
userEntity
.
getPassword
(),
userEntity
.
getName
(),
userEntity
.
getEmail
());
}
catch
(
UserAlreadyExistsException
e
)
{
throw
new
ServiceException
(
"Could not create new user"
,
userEntity
.
getUsername
(),
ExceptionType
.
USER_ALREADY_EXISTS_EXCEPTION
,
Response
.
Status
.
CONFLICT
);
}
addProperties
(
userEntity
.
getUsername
(),
userEntity
.
getProperties
());
}
}
/**
* Update user.
*
* @param username
* the username
* @param userEntity
* the user entity
* @throws ServiceException
* the service exception
*/
public
void
updateUser
(
String
username
,
UserEntity
userEntity
)
throws
ServiceException
{
if
(
userEntity
!=
null
&&
!
username
.
isEmpty
())
{
// Payload contains another username than provided over path
// parameter
if
(
userEntity
.
getUsername
()
!=
null
)
{
if
(!
userEntity
.
getUsername
().
equals
(
username
))
{
JustMarriedController
.
changeName
(
username
,
userEntity
.
getUsername
(),
true
,
userEntity
.
getEmail
(),
userEntity
.
getName
());
addProperties
(
userEntity
.
getUsername
(),
userEntity
.
getProperties
());
return
;
}
}
User
user
=
getAndCheckUser
(
username
);
if
(
userEntity
.
getPassword
()
!=
null
)
{
user
.
setPassword
(
userEntity
.
getPassword
());
}
if
(
userEntity
.
getName
()
!=
null
)
{
user
.
setName
(
userEntity
.
getName
());
}
if
(
userEntity
.
getEmail
()
!=
null
)
{
user
.
setEmail
(
userEntity
.
getEmail
());
}
addProperties
(
username
,
userEntity
.
getProperties
());
}
}
/**
* Delete user.
*
* @param username
* the username
* @throws ServiceException
* the service exception
*/
public
void
deleteUser
(
String
username
)
throws
ServiceException
{
User
user
=
getAndCheckUser
(
username
);
userManager
.
deleteUser
(
user
);
rosterManager
.
deleteRoster
(
server
.
createJID
(
username
,
null
));
}
/**
* Gets the user entities.
*
* @param userSearch
* the user search
* @param propertyValue
* @param propertyKey
* @return the user entities
* @throws ServiceException
*/
public
UserEntities
getUserEntities
(
String
userSearch
,
String
propertyKey
,
String
propertyValue
)
throws
ServiceException
{
if
(
propertyKey
!=
null
)
{
return
getUserEntitiesByProperty
(
propertyKey
,
propertyValue
);
}
UserEntities
userEntities
=
new
UserEntities
();
userEntities
.
setUsers
(
UserUtils
.
convertUsersToUserEntities
(
userManager
.
getUsers
(),
userSearch
));
return
userEntities
;
}
/**
* Gets the user entity.
*
* @param username
* the username
* @return the user entity
* @throws ServiceException
* the service exception
*/
public
UserEntity
getUserEntity
(
String
username
)
throws
ServiceException
{
return
UserUtils
.
convertUserToUserEntity
(
getAndCheckUser
(
username
));
}
/**
* Enable user.
*
* @param username
* the username
* @throws ServiceException
* the service exception
*/
public
void
enableUser
(
String
username
)
throws
ServiceException
{
getAndCheckUser
(
username
);
LockOutManager
.
getInstance
().
enableAccount
(
username
);
}
/**
* Disable user.
*
* @param username
* the username
* @throws ServiceException
* the service exception
*/
public
void
disableUser
(
String
username
)
throws
ServiceException
{
getAndCheckUser
(
username
);
LockOutManager
.
getInstance
().
disableAccount
(
username
,
null
,
null
);
}
/**
* Gets the roster entities.
*
* @param username
* the username
* @return the roster entities
* @throws ServiceException
* the service exception
*/
public
RosterEntities
getRosterEntities
(
String
username
)
throws
ServiceException
{
Roster
roster
=
getUserRoster
(
username
);
List
<
RosterItemEntity
>
rosterEntities
=
new
ArrayList
<
RosterItemEntity
>();
for
(
RosterItem
rosterItem
:
roster
.
getRosterItems
())
{
RosterItemEntity
rosterItemEntity
=
new
RosterItemEntity
(
rosterItem
.
getJid
().
toBareJID
(),
rosterItem
.
getNickname
(),
rosterItem
.
getSubStatus
().
getValue
());
rosterItemEntity
.
setGroups
(
rosterItem
.
getGroups
());
rosterEntities
.
add
(
rosterItemEntity
);
}
return
new
RosterEntities
(
rosterEntities
);
}
/**
* 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
);
}
}
/**
* Update roster item.
*
* @param username
* the username
* @param rosterJid
* the roster jid
* @param rosterItemEntity
* the roster item entity
* @throws ServiceException
* the service exception
* @throws UserNotFoundException
* the user not found exception
* @throws UserAlreadyExistsException
* the user already exists exception
* @throws SharedGroupException
* the shared group exception
*/
public
void
updateRosterItem
(
String
username
,
String
rosterJid
,
RosterItemEntity
rosterItemEntity
)
throws
ServiceException
,
UserNotFoundException
,
UserAlreadyExistsException
,
SharedGroupException
{
getAndCheckUser
(
username
);
Roster
roster
=
getUserRoster
(
username
);
JID
jid
=
new
JID
(
rosterJid
);
RosterItem
rosterItem
=
roster
.
getRosterItem
(
jid
);
if
(
rosterItemEntity
.
getNickname
()
!=
null
)
{
rosterItem
.
setNickname
(
rosterItemEntity
.
getNickname
());
}
if
(
rosterItemEntity
.
getGroups
()
!=
null
)
{
rosterItem
.
setGroups
(
rosterItemEntity
.
getGroups
());
}
UserUtils
.
checkSubType
(
rosterItemEntity
.
getSubscriptionType
());
rosterItem
.
setSubStatus
(
RosterItem
.
SubType
.
getTypeFromInt
(
rosterItemEntity
.
getSubscriptionType
()));
roster
.
updateRosterItem
(
rosterItem
);
}
/**
* Delete roster item.
*
* @param username
* the username
* @param rosterJid
* the roster jid
* @throws SharedGroupException
* the shared group exception
* @throws ServiceException
* the service exception
*/
public
void
deleteRosterItem
(
String
username
,
String
rosterJid
)
throws
SharedGroupException
,
ServiceException
{
getAndCheckUser
(
username
);
Roster
roster
=
getUserRoster
(
username
);
JID
jid
=
new
JID
(
rosterJid
);
if
(
roster
.
deleteRosterItem
(
jid
,
true
)
==
null
)
{
throw
new
ServiceException
(
"Roster Item could not deleted"
,
jid
.
toBareJID
(),
"RosterItemNotFound"
,
Response
.
Status
.
NOT_FOUND
);
}
}
/**
* Gets the user groups.
*
* @param username
* the username
* @return the user groups
* @throws ServiceException
* the service exception
*/
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
>();
for
(
Group
group
:
groups
)
{
groupNames
.
add
(
group
.
getName
());
}
return
groupNames
;
}
/**
* Adds the user to group.
*
* @param username
* the username
* @param userGroupsEntity
* the user groups entity
* @throws ServiceException
* the service exception
*/
public
void
addUserToGroups
(
String
username
,
UserGroupsEntity
userGroupsEntity
)
throws
ServiceException
{
if
(
userGroupsEntity
!=
null
)
{
Collection
<
Group
>
groups
=
new
ArrayList
<
Group
>();
for
(
String
groupName
:
userGroupsEntity
.
getGroupNames
())
{
Group
group
=
null
;
try
{
group
=
GroupManager
.
getInstance
().
getGroup
(
groupName
);
}
catch
(
GroupNotFoundException
e
)
{
// Create this group
group
=
createGroup
(
groupName
);
}
groups
.
add
(
group
);
}
for
(
Group
group
:
groups
)
{
group
.
getMembers
().
add
(
server
.
createJID
(
username
,
null
));
}
}
}
/**
* 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
));
}
}
}
/**
* Gets the user entities by property key and or value.
*
* @param propertyKey
* the property key
* @param propertyValue
* the property value (can be null)
* @return the user entities by property
* @throws ServiceException
* the service exception
*/
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
();
for
(
String
username
:
usernames
)
{
users
.
add
(
getUserEntity
(
username
));
}
userEntities
.
setUsers
(
users
);
return
userEntities
;
}
/**
* Adds the properties.
*
* @param userEntity
* the user entity
* @throws ServiceException
* the service exception
*/
private
void
addProperties
(
String
username
,
List
<
UserProperty
>
properties
)
throws
ServiceException
{
User
user
=
getAndCheckUser
(
username
);
user
.
getProperties
().
clear
();
if
(
properties
!=
null
)
{
for
(
UserProperty
property
:
properties
)
{
user
.
getProperties
().
put
(
property
.
getKey
(),
property
.
getValue
());
}
}
}
/**
* Creates the group.
*
* @param groupName
* the group name
* @return the group
* @throws ServiceException
* the service exception
*/
private
Group
createGroup
(
String
groupName
)
throws
ServiceException
{
Group
group
=
null
;
try
{
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
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
return
group
;
}
/**
* Gets the and check user.
*
* @param username
* the username
* @return the and check user
* @throws ServiceException
* the service exception
*/
private
User
getAndCheckUser
(
String
username
)
throws
ServiceException
{
JID
targetJID
=
server
.
createJID
(
username
,
null
);
if
(
targetJID
.
getNode
()
==
null
)
{
throw
new
ServiceException
(
"Could not get user"
,
username
,
ExceptionType
.
USER_NOT_FOUND_EXCEPTION
,
Response
.
Status
.
NOT_FOUND
);
}
try
{
return
userManager
.
getUser
(
targetJID
.
getNode
());
}
catch
(
UserNotFoundException
e
)
{
throw
new
ServiceException
(
"Could not get user"
,
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
);
}
}
}
src/plugins/restAPI/src/java/org/jivesoftware/openfire/plugin/rest/exceptions/ExceptionType.java
View file @
090831d3
package
org
.
jivesoftware
.
openfire
.
plugin
.
rest
.
exceptions
;
/**
* The Class ExceptionType.
*/
public
final
class
ExceptionType
{
/** The Constant ILLEGAL_ARGUMENT_EXCEPTION. */
public
static
final
String
ILLEGAL_ARGUMENT_EXCEPTION
=
"IllegalArgumentException"
;
/** The Constant SHARED_GROUP_EXCEPTION. */
public
static
final
String
SHARED_GROUP_EXCEPTION
=
"SharedGroupException"
;
/** The Constant PROPERTY_NOT_FOUND. */
public
static
final
String
PROPERTY_NOT_FOUND
=
"PropertyNotFoundException"
;
/** The Constant USER_ALREADY_EXISTS_EXCEPTION. */
public
static
final
String
USER_ALREADY_EXISTS_EXCEPTION
=
"UserAlreadyExistsException"
;
/** The Constant USER_NOT_FOUND_EXCEPTION. */
public
static
final
String
USER_NOT_FOUND_EXCEPTION
=
"UserNotFoundException"
;
/** The Constant GROUP_ALREADY_EXISTS. */
public
static
final
String
GROUP_ALREADY_EXISTS
=
"GroupAlreadyExistsException"
;
/** The Constant GROUP_NOT_FOUND. */
public
static
final
String
GROUP_NOT_FOUND
=
"GroupNotFoundException"
;
/** The Constant ROOM_NOT_FOUND. */
public
static
final
String
ROOM_NOT_FOUND
=
"RoomNotFoundException"
;
/** The Constant NOT_ALLOWED. */
public
static
final
String
NOT_ALLOWED
=
"NotAllowedException"
;
/**
* Instantiates a new exception type.
*/
private
ExceptionType
()
{
}
}
package
org
.
jivesoftware
.
openfire
.
plugin
.
rest
.
exceptions
;
/**
* The Class ExceptionType.
*/
public
final
class
ExceptionType
{
/** The Constant ILLEGAL_ARGUMENT_EXCEPTION. */
public
static
final
String
ILLEGAL_ARGUMENT_EXCEPTION
=
"IllegalArgumentException"
;
/** The Constant SHARED_GROUP_EXCEPTION. */
public
static
final
String
SHARED_GROUP_EXCEPTION
=
"SharedGroupException"
;
/** The Constant PROPERTY_NOT_FOUND. */
public
static
final
String
PROPERTY_NOT_FOUND
=
"PropertyNotFoundException"
;
/** The Constant USER_ALREADY_EXISTS_EXCEPTION. */
public
static
final
String
USER_ALREADY_EXISTS_EXCEPTION
=
"UserAlreadyExistsException"
;
/** The Constant USER_NOT_FOUND_EXCEPTION. */
public
static
final
String
USER_NOT_FOUND_EXCEPTION
=
"UserNotFoundException"
;
/** The Constant GROUP_ALREADY_EXISTS. */
public
static
final
String
GROUP_ALREADY_EXISTS
=
"GroupAlreadyExistsException"
;
/** The Constant GROUP_NOT_FOUND. */
public
static
final
String
GROUP_NOT_FOUND
=
"GroupNotFoundException"
;
/** The Constant ROOM_NOT_FOUND. */
public
static
final
String
ROOM_NOT_FOUND
=
"RoomNotFoundException"
;
/** The Constant NOT_ALLOWED. */
public
static
final
String
NOT_ALLOWED
=
"NotAllowedException"
;
/** The Constant ALREADY_EXISTS. */
public
static
final
String
ALREADY_EXISTS
=
"AlreadyExistsException"
;
/**
* Instantiates a new exception type.
*/
private
ExceptionType
()
{
}
}
src/plugins/restAPI/src/java/org/jivesoftware/openfire/plugin/rest/service/JerseyWrapper.java
View file @
090831d3
...
...
@@ -2,6 +2,8 @@ package org.jivesoftware.openfire.plugin.rest.service;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
javax.servlet.ServletConfig
;
import
javax.servlet.ServletException
;
...
...
@@ -43,8 +45,12 @@ public class JerseyWrapper extends ServletContainer {
/** The prc. */
private
static
PackagesResourceConfig
prc
;
/** The Constant JERSEY_LOGGER. */
private
final
static
Logger
JERSEY_LOGGER
=
Logger
.
getLogger
(
"com.sun.jersey"
);
static
{
JERSEY_LOGGER
.
setLevel
(
Level
.
SEVERE
);
config
=
new
HashMap
<
String
,
Object
>();
config
.
put
(
RESOURCE_CONFIG_CLASS_KEY
,
RESOURCE_CONFIG_CLASS
);
prc
=
new
PackagesResourceConfig
(
SCAN_PACKAGE_DEFAULT
);
...
...
src/plugins/restAPI/src/java/org/jivesoftware/openfire/plugin/rest/service/UserRosterService.java
View file @
090831d3
package
org
.
jivesoftware
.
openfire
.
plugin
.
rest
.
service
;
import
javax.annotation.PostConstruct
;
import
javax.ws.rs.DELETE
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.POST
;
import
javax.ws.rs.PUT
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
org.jivesoftware.openfire.SharedGroupException
;
import
org.jivesoftware.openfire.plugin.rest.controller.UserServiceController
;
import
org.jivesoftware.openfire.plugin.rest.entity.RosterEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException
;
import
org.jivesoftware.openfire.user.UserAlreadyExistsException
;
import
org.jivesoftware.openfire.user.UserNotFoundException
;
@Path
(
"restapi/v1/users/{username}/roster"
)
public
class
UserRosterService
{
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
UserServiceController
plugin
;
@PostConstruct
public
void
init
()
{
plugin
=
UserServiceController
.
getInstance
();
}
@GET
@Produces
({
MediaType
.
APPLICATION_XML
,
MediaType
.
APPLICATION_JSON
})
public
RosterEntities
getUserRoster
(
@PathParam
(
"username"
)
String
username
)
throws
ServiceException
{
return
plugin
.
getRosterEntities
(
username
);
}
@POST
public
Response
createRoster
(
@PathParam
(
"username"
)
String
username
,
RosterItemEntity
rosterItemEntity
)
throws
ServiceException
{
try
{
plugin
.
addRosterItem
(
username
,
rosterItemEntity
);
}
catch
(
UserNotFoundException
e
)
{
throw
new
ServiceException
(
COULD_NOT_CREATE_ROSTER_ITEM
,
""
,
ExceptionType
.
USER_NOT_FOUND_EXCEPTION
,
Response
.
Status
.
NOT_FOUND
,
e
);
}
catch
(
UserAlreadyExistsException
e
)
{
throw
new
ServiceException
(
COULD_NOT_CREATE_ROSTER_ITEM
,
""
,
ExceptionType
.
USER_ALREADY_EXISTS_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
catch
(
SharedGroupException
e
)
{
throw
new
ServiceException
(
COULD_NOT_CREATE_ROSTER_ITEM
,
""
,
ExceptionType
.
SHARED_GROUP_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
return
Response
.
status
(
Response
.
Status
.
CREATED
).
build
();
}
@DELETE
@Path
(
"/{rosterJid}"
)
public
Response
deleteRoster
(
@PathParam
(
"username"
)
String
username
,
@PathParam
(
"rosterJid"
)
String
rosterJid
)
throws
ServiceException
{
try
{
plugin
.
deleteRosterItem
(
username
,
rosterJid
);
}
catch
(
SharedGroupException
e
)
{
throw
new
ServiceException
(
"Could not delete the roster item"
,
rosterJid
,
ExceptionType
.
SHARED_GROUP_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
return
Response
.
status
(
Response
.
Status
.
OK
).
build
();
}
@PUT
@Path
(
"/{rosterJid}"
)
public
Response
updateRoster
(
@PathParam
(
"username"
)
String
username
,
@PathParam
(
"rosterJid"
)
String
rosterJid
,
RosterItemEntity
rosterItemEntity
)
throws
ServiceException
{
try
{
plugin
.
updateRosterItem
(
username
,
rosterJid
,
rosterItemEntity
);
}
catch
(
UserNotFoundException
e
)
{
throw
new
ServiceException
(
COULD_NOT_UPDATE_THE_ROSTER
,
rosterJid
,
ExceptionType
.
USER_NOT_FOUND_EXCEPTION
,
Response
.
Status
.
NOT_FOUND
,
e
);
}
catch
(
SharedGroupException
e
)
{
throw
new
ServiceException
(
COULD_NOT_UPDATE_THE_ROSTER
,
rosterJid
,
ExceptionType
.
SHARED_GROUP_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
catch
(
UserAlreadyExistsException
e
)
{
throw
new
ServiceException
(
COULD_NOT_UPDATE_THE_ROSTER
,
rosterJid
,
ExceptionType
.
USER_ALREADY_EXISTS_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
return
Response
.
status
(
Response
.
Status
.
OK
).
build
();
}
}
package
org
.
jivesoftware
.
openfire
.
plugin
.
rest
.
service
;
import
javax.annotation.PostConstruct
;
import
javax.ws.rs.DELETE
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.POST
;
import
javax.ws.rs.PUT
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
org.jivesoftware.openfire.SharedGroupException
;
import
org.jivesoftware.openfire.plugin.rest.controller.UserServiceController
;
import
org.jivesoftware.openfire.plugin.rest.entity.RosterEntities
;
import
org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType
;
import
org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException
;
import
org.jivesoftware.openfire.user.UserAlreadyExistsException
;
import
org.jivesoftware.openfire.user.UserNotFoundException
;
@Path
(
"restapi/v1/users/{username}/roster"
)
public
class
UserRosterService
{
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
UserServiceController
plugin
;
@PostConstruct
public
void
init
()
{
plugin
=
UserServiceController
.
getInstance
();
}
@GET
@Produces
({
MediaType
.
APPLICATION_XML
,
MediaType
.
APPLICATION_JSON
})
public
RosterEntities
getUserRoster
(
@PathParam
(
"username"
)
String
username
)
throws
ServiceException
{
return
plugin
.
getRosterEntities
(
username
);
}
@POST
public
Response
createRoster
(
@PathParam
(
"username"
)
String
username
,
RosterItemEntity
rosterItemEntity
)
throws
ServiceException
{
try
{
plugin
.
addRosterItem
(
username
,
rosterItemEntity
);
}
catch
(
UserNotFoundException
e
)
{
throw
new
ServiceException
(
COULD_NOT_CREATE_ROSTER_ITEM
,
""
,
ExceptionType
.
USER_NOT_FOUND_EXCEPTION
,
Response
.
Status
.
NOT_FOUND
,
e
);
}
catch
(
UserAlreadyExistsException
e
)
{
throw
new
ServiceException
(
COULD_NOT_CREATE_ROSTER_ITEM
,
""
,
ExceptionType
.
USER_ALREADY_EXISTS_EXCEPTION
,
Response
.
Status
.
CONFLICT
,
e
);
}
catch
(
SharedGroupException
e
)
{
throw
new
ServiceException
(
COULD_NOT_CREATE_ROSTER_ITEM
,
""
,
ExceptionType
.
SHARED_GROUP_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
return
Response
.
status
(
Response
.
Status
.
CREATED
).
build
();
}
@DELETE
@Path
(
"/{rosterJid}"
)
public
Response
deleteRoster
(
@PathParam
(
"username"
)
String
username
,
@PathParam
(
"rosterJid"
)
String
rosterJid
)
throws
ServiceException
{
try
{
plugin
.
deleteRosterItem
(
username
,
rosterJid
);
}
catch
(
SharedGroupException
e
)
{
throw
new
ServiceException
(
"Could not delete the roster item"
,
rosterJid
,
ExceptionType
.
SHARED_GROUP_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
return
Response
.
status
(
Response
.
Status
.
OK
).
build
();
}
@PUT
@Path
(
"/{rosterJid}"
)
public
Response
updateRoster
(
@PathParam
(
"username"
)
String
username
,
@PathParam
(
"rosterJid"
)
String
rosterJid
,
RosterItemEntity
rosterItemEntity
)
throws
ServiceException
{
try
{
plugin
.
updateRosterItem
(
username
,
rosterJid
,
rosterItemEntity
);
}
catch
(
UserNotFoundException
e
)
{
throw
new
ServiceException
(
COULD_NOT_UPDATE_THE_ROSTER
,
rosterJid
,
ExceptionType
.
USER_NOT_FOUND_EXCEPTION
,
Response
.
Status
.
NOT_FOUND
,
e
);
}
catch
(
SharedGroupException
e
)
{
throw
new
ServiceException
(
COULD_NOT_UPDATE_THE_ROSTER
,
rosterJid
,
ExceptionType
.
SHARED_GROUP_EXCEPTION
,
Response
.
Status
.
BAD_REQUEST
,
e
);
}
catch
(
UserAlreadyExistsException
e
)
{
throw
new
ServiceException
(
COULD_NOT_UPDATE_THE_ROSTER
,
rosterJid
,
ExceptionType
.
USER_ALREADY_EXISTS_EXCEPTION
,
Response
.
Status
.
CONFLICT
,
e
);
}
return
Response
.
status
(
Response
.
Status
.
OK
).
build
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment