PageRenderTime 66ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/openfire/src/plugins/restAPI/src/java/org/jivesoftware/openfire/plugin/rest/controller/UserServiceController.java

https://bitbucket.org/belinskiy/openfire4v2
Java | 543 lines | 278 code | 48 blank | 217 comment | 47 complexity | 48694c36543a35232178bea24379768a MD5 | raw file
Possible License(s): MIT
  1. package org.jivesoftware.openfire.plugin.rest.controller;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import javax.ws.rs.core.Response;
  6. import org.jivesoftware.openfire.SessionManager;
  7. import org.jivesoftware.openfire.SharedGroupException;
  8. import org.jivesoftware.openfire.XMPPServer;
  9. import org.jivesoftware.openfire.group.Group;
  10. import org.jivesoftware.openfire.group.GroupManager;
  11. import org.jivesoftware.openfire.group.GroupNotFoundException;
  12. import org.jivesoftware.openfire.lockout.LockOutManager;
  13. import org.jivesoftware.openfire.plugin.rest.dao.PropertyDAO;
  14. import org.jivesoftware.openfire.plugin.rest.entity.GroupEntity;
  15. import org.jivesoftware.openfire.plugin.rest.entity.RosterEntities;
  16. import org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity;
  17. import org.jivesoftware.openfire.plugin.rest.entity.UserEntities;
  18. import org.jivesoftware.openfire.plugin.rest.entity.UserEntity;
  19. import org.jivesoftware.openfire.plugin.rest.entity.UserGroupsEntity;
  20. import org.jivesoftware.openfire.plugin.rest.entity.UserProperty;
  21. import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
  22. import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
  23. import org.jivesoftware.openfire.plugin.rest.utils.UserUtils;
  24. import org.jivesoftware.openfire.roster.Roster;
  25. import org.jivesoftware.openfire.roster.RosterItem;
  26. import org.jivesoftware.openfire.roster.RosterManager;
  27. import org.jivesoftware.openfire.session.ClientSession;
  28. import org.jivesoftware.openfire.user.User;
  29. import org.jivesoftware.openfire.user.UserAlreadyExistsException;
  30. import org.jivesoftware.openfire.user.UserManager;
  31. import org.jivesoftware.openfire.user.UserNotFoundException;
  32. import org.xmpp.packet.JID;
  33. import org.xmpp.packet.StreamError;
  34. /**
  35. * The Class UserServiceController.
  36. */
  37. public class UserServiceController {
  38. /** The Constant INSTANCE. */
  39. public static final UserServiceController INSTANCE = new UserServiceController();
  40. /** The user manager. */
  41. private UserManager userManager;
  42. /** The roster manager. */
  43. private RosterManager rosterManager;
  44. /** The server. */
  45. private XMPPServer server;
  46. /** The lock out manager. */
  47. private LockOutManager lockOutManager;
  48. /**
  49. * Gets the single instance of UserServiceController.
  50. *
  51. * @return single instance of UserServiceController
  52. */
  53. public static UserServiceController getInstance() {
  54. return INSTANCE;
  55. }
  56. /**
  57. * Instantiates a new user service controller.
  58. */
  59. private UserServiceController() {
  60. server = XMPPServer.getInstance();
  61. userManager = server.getUserManager();
  62. rosterManager = server.getRosterManager();
  63. lockOutManager = server.getLockOutManager();
  64. }
  65. /**
  66. * Creates the user.
  67. *
  68. * @param userEntity
  69. * the user entity
  70. * @throws ServiceException
  71. * the service exception
  72. */
  73. public void createUser(UserEntity userEntity) throws ServiceException {
  74. if (userEntity != null && !userEntity.getUsername().isEmpty()) {
  75. if (userEntity.getPassword() == null) {
  76. throw new ServiceException("Could not create new user, because password is null",
  77. userEntity.getUsername(), "PasswordIsNull", Response.Status.BAD_REQUEST);
  78. }
  79. try {
  80. userManager.createUser(userEntity.getUsername(), userEntity.getPassword(), userEntity.getName(),
  81. userEntity.getEmail());
  82. } catch (UserAlreadyExistsException e) {
  83. throw new ServiceException("Could not create new user", userEntity.getUsername(),
  84. ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT);
  85. }
  86. addProperties(userEntity.getUsername(), userEntity.getProperties());
  87. } else {
  88. throw new ServiceException("Could not create new user",
  89. "users", ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST);
  90. }
  91. }
  92. /**
  93. * Update user.
  94. *
  95. * @param username
  96. * the username
  97. * @param userEntity
  98. * the user entity
  99. * @throws ServiceException
  100. * the service exception
  101. */
  102. public void updateUser(String username, UserEntity userEntity) throws ServiceException {
  103. if (userEntity != null && !username.isEmpty()) {
  104. // Payload contains another username than provided over path
  105. // parameter
  106. if (userEntity.getUsername() != null) {
  107. if (!userEntity.getUsername().equals(username)) {
  108. JustMarriedController.changeName(username, userEntity.getUsername(), true, userEntity.getEmail(),
  109. userEntity.getName());
  110. addProperties(userEntity.getUsername(), userEntity.getProperties());
  111. return;
  112. }
  113. }
  114. User user = getAndCheckUser(username);
  115. if (userEntity.getPassword() != null) {
  116. user.setPassword(userEntity.getPassword());
  117. }
  118. if (userEntity.getName() != null) {
  119. user.setName(userEntity.getName());
  120. }
  121. if (userEntity.getEmail() != null) {
  122. user.setEmail(userEntity.getEmail());
  123. }
  124. addProperties(username, userEntity.getProperties());
  125. }
  126. }
  127. /**
  128. * Delete user.
  129. *
  130. * @param username
  131. * the username
  132. * @throws ServiceException
  133. * the service exception
  134. */
  135. public void deleteUser(String username) throws ServiceException {
  136. User user = getAndCheckUser(username);
  137. userManager.deleteUser(user);
  138. rosterManager.deleteRoster(server.createJID(username, null));
  139. }
  140. /**
  141. * Gets the user entities.
  142. *
  143. * @param userSearch
  144. * the user search
  145. * @param propertyValue
  146. * @param propertyKey
  147. * @return the user entities
  148. * @throws ServiceException
  149. */
  150. public UserEntities getUserEntities(String userSearch, String propertyKey, String propertyValue)
  151. throws ServiceException {
  152. if (propertyKey != null) {
  153. return getUserEntitiesByProperty(propertyKey, propertyValue);
  154. }
  155. UserEntities userEntities = new UserEntities();
  156. userEntities.setUsers(UserUtils.convertUsersToUserEntities(userManager.getUsers(), userSearch));
  157. return userEntities;
  158. }
  159. /**
  160. * Gets the user entity.
  161. *
  162. * @param username
  163. * the username
  164. * @return the user entity
  165. * @throws ServiceException
  166. * the service exception
  167. */
  168. public UserEntity getUserEntity(String username) throws ServiceException {
  169. return UserUtils.convertUserToUserEntity(getAndCheckUser(username));
  170. }
  171. /**
  172. * Enable user.
  173. *
  174. * @param username
  175. * the username
  176. * @throws ServiceException
  177. * the service exception
  178. */
  179. public void enableUser(String username) throws ServiceException {
  180. getAndCheckUser(username);
  181. lockOutManager.enableAccount(username);
  182. }
  183. /**
  184. * Disable user.
  185. *
  186. * @param username
  187. * the username
  188. * @throws ServiceException
  189. * the service exception
  190. */
  191. public void disableUser(String username) throws ServiceException {
  192. getAndCheckUser(username);
  193. lockOutManager.disableAccount(username, null, null);
  194. if (lockOutManager.isAccountDisabled(username)) {
  195. final StreamError error = new StreamError(StreamError.Condition.not_authorized);
  196. for (ClientSession sess : SessionManager.getInstance().getSessions(username)) {
  197. sess.deliverRawText(error.toXML());
  198. sess.close();
  199. }
  200. }
  201. }
  202. /**
  203. * Gets the roster entities.
  204. *
  205. * @param username
  206. * the username
  207. * @return the roster entities
  208. * @throws ServiceException
  209. * the service exception
  210. */
  211. public RosterEntities getRosterEntities(String username) throws ServiceException {
  212. Roster roster = getUserRoster(username);
  213. List<RosterItemEntity> rosterEntities = new ArrayList<RosterItemEntity>();
  214. for (RosterItem rosterItem : roster.getRosterItems()) {
  215. RosterItemEntity rosterItemEntity = new RosterItemEntity(rosterItem.getJid().toBareJID(),
  216. rosterItem.getNickname(), rosterItem.getSubStatus().getValue());
  217. rosterItemEntity.setGroups(rosterItem.getGroups());
  218. rosterEntities.add(rosterItemEntity);
  219. }
  220. return new RosterEntities(rosterEntities);
  221. }
  222. /**
  223. * Adds the roster item.
  224. *
  225. * @param username
  226. * the username
  227. * @param rosterItemEntity
  228. * the roster item entity
  229. * @throws ServiceException
  230. * the service exception
  231. * @throws UserAlreadyExistsException
  232. * the user already exists exception
  233. * @throws SharedGroupException
  234. * the shared group exception
  235. * @throws UserNotFoundException
  236. * the user not found exception
  237. */
  238. public void addRosterItem(String username, RosterItemEntity rosterItemEntity) throws ServiceException,
  239. UserAlreadyExistsException, SharedGroupException, UserNotFoundException {
  240. Roster roster = getUserRoster(username);
  241. if (rosterItemEntity.getJid() == null) {
  242. throw new ServiceException("JID is null", "JID", "IllegalArgumentException", Response.Status.BAD_REQUEST);
  243. }
  244. JID jid = new JID(rosterItemEntity.getJid());
  245. try {
  246. roster.getRosterItem(jid);
  247. throw new UserAlreadyExistsException(jid.toBareJID());
  248. } catch (UserNotFoundException e) {
  249. // Roster item does not exist. Try to add it.
  250. }
  251. if (roster != null) {
  252. RosterItem rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(),
  253. rosterItemEntity.getGroups(), false, true);
  254. UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
  255. rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
  256. roster.updateRosterItem(rosterItem);
  257. }
  258. }
  259. /**
  260. * Update roster item.
  261. *
  262. * @param username
  263. * the username
  264. * @param rosterJid
  265. * the roster jid
  266. * @param rosterItemEntity
  267. * the roster item entity
  268. * @throws ServiceException
  269. * the service exception
  270. * @throws UserNotFoundException
  271. * the user not found exception
  272. * @throws UserAlreadyExistsException
  273. * the user already exists exception
  274. * @throws SharedGroupException
  275. * the shared group exception
  276. */
  277. public void updateRosterItem(String username, String rosterJid, RosterItemEntity rosterItemEntity)
  278. throws ServiceException, UserNotFoundException, UserAlreadyExistsException, SharedGroupException {
  279. getAndCheckUser(username);
  280. Roster roster = getUserRoster(username);
  281. JID jid = new JID(rosterJid);
  282. RosterItem rosterItem = roster.getRosterItem(jid);
  283. if (rosterItemEntity.getNickname() != null) {
  284. rosterItem.setNickname(rosterItemEntity.getNickname());
  285. }
  286. if (rosterItemEntity.getGroups() != null) {
  287. rosterItem.setGroups(rosterItemEntity.getGroups());
  288. }
  289. UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
  290. rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
  291. roster.updateRosterItem(rosterItem);
  292. }
  293. /**
  294. * Delete roster item.
  295. *
  296. * @param username
  297. * the username
  298. * @param rosterJid
  299. * the roster jid
  300. * @throws SharedGroupException
  301. * the shared group exception
  302. * @throws ServiceException
  303. * the service exception
  304. */
  305. public void deleteRosterItem(String username, String rosterJid) throws SharedGroupException, ServiceException {
  306. getAndCheckUser(username);
  307. Roster roster = getUserRoster(username);
  308. JID jid = new JID(rosterJid);
  309. if (roster.deleteRosterItem(jid, true) == null) {
  310. throw new ServiceException("Roster Item could not deleted", jid.toBareJID(), "RosterItemNotFound",
  311. Response.Status.NOT_FOUND);
  312. }
  313. }
  314. /**
  315. * Gets the user groups.
  316. *
  317. * @param username
  318. * the username
  319. * @return the user groups
  320. * @throws ServiceException
  321. * the service exception
  322. */
  323. public List<String> getUserGroups(String username) throws ServiceException {
  324. User user = getAndCheckUser(username);
  325. Collection<Group> groups = GroupManager.getInstance().getGroups(user);
  326. List<String> groupNames = new ArrayList<String>();
  327. for (Group group : groups) {
  328. groupNames.add(group.getName());
  329. }
  330. return groupNames;
  331. }
  332. /**
  333. * Adds the user to group.
  334. *
  335. * @param username
  336. * the username
  337. * @param userGroupsEntity
  338. * the user groups entity
  339. * @throws ServiceException
  340. * the service exception
  341. */
  342. public void addUserToGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
  343. if (userGroupsEntity != null) {
  344. Collection<Group> groups = new ArrayList<Group>();
  345. for (String groupName : userGroupsEntity.getGroupNames()) {
  346. Group group = null;
  347. try {
  348. group = GroupManager.getInstance().getGroup(groupName);
  349. } catch (GroupNotFoundException e) {
  350. // Create this group
  351. group = GroupController.getInstance().createGroup(new GroupEntity(groupName, ""));
  352. }
  353. groups.add(group);
  354. }
  355. for (Group group : groups) {
  356. group.getMembers().add(server.createJID(username, null));
  357. }
  358. }
  359. }
  360. /**
  361. * Adds the user to group.
  362. *
  363. * @param username the username
  364. * @param groupName the group name
  365. * @throws ServiceException the service exception
  366. */
  367. public void addUserToGroup(String username, String groupName) throws ServiceException {
  368. Group group = null;
  369. try {
  370. group = GroupManager.getInstance().getGroup(groupName);
  371. } catch (GroupNotFoundException e) {
  372. // Create this group
  373. group = GroupController.getInstance().createGroup(new GroupEntity(groupName, ""));
  374. }
  375. group.getMembers().add(server.createJID(username, null));
  376. }
  377. /**
  378. * Delete user from groups.
  379. *
  380. * @param username
  381. * the username
  382. * @param userGroupsEntity
  383. * the user groups entity
  384. * @throws ServiceException
  385. * the service exception
  386. */
  387. public void deleteUserFromGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
  388. if (userGroupsEntity != null) {
  389. for (String groupName : userGroupsEntity.getGroupNames()) {
  390. Group group = null;
  391. try {
  392. group = GroupManager.getInstance().getGroup(groupName);
  393. } catch (GroupNotFoundException e) {
  394. throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND,
  395. Response.Status.NOT_FOUND, e);
  396. }
  397. group.getMembers().remove(server.createJID(username, null));
  398. }
  399. }
  400. }
  401. /**
  402. * Delete user from group.
  403. *
  404. * @param username the username
  405. * @param groupName the group name
  406. * @throws ServiceException the service exception
  407. */
  408. public void deleteUserFromGroup(String username, String groupName) throws ServiceException {
  409. Group group = null;
  410. try {
  411. group = GroupManager.getInstance().getGroup(groupName);
  412. } catch (GroupNotFoundException e) {
  413. throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND,
  414. Response.Status.NOT_FOUND, e);
  415. }
  416. group.getMembers().remove(server.createJID(username, null));
  417. }
  418. /**
  419. * Gets the user entities by property key and or value.
  420. *
  421. * @param propertyKey
  422. * the property key
  423. * @param propertyValue
  424. * the property value (can be null)
  425. * @return the user entities by property
  426. * @throws ServiceException
  427. * the service exception
  428. */
  429. public UserEntities getUserEntitiesByProperty(String propertyKey, String propertyValue) throws ServiceException {
  430. List<String> usernames = PropertyDAO.getUsernameByProperty(propertyKey, propertyValue);
  431. List<UserEntity> users = new ArrayList<UserEntity>();
  432. UserEntities userEntities = new UserEntities();
  433. for (String username : usernames) {
  434. users.add(getUserEntity(username));
  435. }
  436. userEntities.setUsers(users);
  437. return userEntities;
  438. }
  439. /**
  440. * Adds the properties.
  441. *
  442. * @param userEntity
  443. * the user entity
  444. * @throws ServiceException
  445. * the service exception
  446. */
  447. private void addProperties(String username, List<UserProperty> properties) throws ServiceException {
  448. User user = getAndCheckUser(username);
  449. user.getProperties().clear();
  450. if (properties != null) {
  451. for (UserProperty property : properties) {
  452. user.getProperties().put(property.getKey(), property.getValue());
  453. }
  454. }
  455. }
  456. /**
  457. * Gets the and check user.
  458. *
  459. * @param username
  460. * the username
  461. * @return the and check user
  462. * @throws ServiceException
  463. * the service exception
  464. */
  465. private User getAndCheckUser(String username) throws ServiceException {
  466. JID targetJID = server.createJID(username, null);
  467. if (targetJID.getNode() == null) {
  468. throw new ServiceException("Could not get user", username, ExceptionType.USER_NOT_FOUND_EXCEPTION,
  469. Response.Status.NOT_FOUND);
  470. }
  471. try {
  472. return userManager.getUser(targetJID.getNode());
  473. } catch (UserNotFoundException e) {
  474. throw new ServiceException("Could not get user", username, ExceptionType.USER_NOT_FOUND_EXCEPTION,
  475. Response.Status.NOT_FOUND, e);
  476. }
  477. }
  478. /**
  479. * Gets the user roster.
  480. *
  481. * @param username
  482. * the username
  483. * @return the user roster
  484. * @throws ServiceException
  485. * the service exception
  486. */
  487. private Roster getUserRoster(String username) throws ServiceException {
  488. try {
  489. return rosterManager.getRoster(username);
  490. } catch (UserNotFoundException e) {
  491. throw new ServiceException("Could not get user roster", username, ExceptionType.USER_NOT_FOUND_EXCEPTION,
  492. Response.Status.NOT_FOUND, e);
  493. }
  494. }
  495. }