/** * 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); } } }
/* (non-Javadoc) * @see org.jivesoftware.openfire.roster.RosterItemProvider#createItem(java.lang.String, org.jivesoftware.openfire.roster.RosterItem) */ public RosterItem createItem(String username, RosterItem item) throws UserAlreadyExistsException { Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); long rosterID = SequenceManager.nextID(JiveConstants.ROSTER); pstmt = con.prepareStatement(CREATE_ROSTER_ITEM); pstmt.setString(1, username); pstmt.setLong(2, rosterID); pstmt.setString(3, item.getJid().toBareJID()); pstmt.setInt(4, item.getSubStatus().getValue()); pstmt.setInt(5, item.getAskStatus().getValue()); pstmt.setInt(6, item.getRecvStatus().getValue()); pstmt.setString(7, item.getNickname()); pstmt.executeUpdate(); item.setID(rosterID); insertGroups(rosterID, item.getGroups().iterator(), con); } catch (SQLException e) { throw new UserAlreadyExistsException(item.getJid().toBareJID()); } finally { try { if (pstmt != null) { pstmt.close(); } } catch (Exception e) { Log.error(e); } try { if (con != null) { con.close(); } } catch (Exception e) { Log.error(e); } } return item; }
/** * 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); } } }