Java Code Examples for org.jivesoftware.database.DbConnectionManager#closeConnection()

The following examples show how to use org.jivesoftware.database.DbConnectionManager#closeConnection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultUserProvider.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
public void setModificationDate(String username, Date modificationDate) throws UserNotFoundException {
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(UPDATE_MODIFICATION_DATE);
        pstmt.setString(1, StringUtils.dateToMillis(modificationDate));
        pstmt.setString(2, username);
        pstmt.executeUpdate();
    }
    catch (SQLException sqle) {
        throw new UserNotFoundException(sqle);
    }
    finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
}
 
Example 2
Source File: JDBCUserProvider.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
public int getUserCount() {
    int count = 0;
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = getConnection();
        pstmt = con.prepareStatement(userCountSQL);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            count = rs.getInt(1);
        }
    }
    catch (SQLException e) {
        Log.error(e.getMessage(), e);
    }
    finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return count;
}
 
Example 3
Source File: DefaultGroupPropertyMap.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * Update the value of an existing group property for the current group
 * 
 * @param key Property name
 * @param value Property value
 * @param originalValue Original property value
 */
private synchronized void updateProperty(String key, String value, String originalValue) {
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(UPDATE_PROPERTY);
        pstmt.setString(1, value);
        pstmt.setString(2, key);
        pstmt.setString(3, group.getName());
        pstmt.executeUpdate();
    }
    catch (SQLException e) {
        logger.error(e.getMessage(), e);
    }
    finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
    Map<String, Object> event = new HashMap<>();
    event.put("propertyKey", key);
    event.put("type", "propertyModified");
    event.put("originalValue", originalValue);
    GroupEventDispatcher.dispatchEvent(group,
            GroupEventDispatcher.EventType.group_modified, event);
}
 
Example 4
Source File: JDBCGroupProvider.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<String> getGroupNames(int start, int num) {
    List<String> groupNames = new ArrayList<>();
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = getConnection();
        pstmt = DbConnectionManager.createScrollablePreparedStatement(con, allGroupsSQL);
        rs = pstmt.executeQuery();
        DbConnectionManager.scrollResultSet(rs, start);
        int count = 0;
        while (rs.next() && count < num) {
            groupNames.add(rs.getString(1));
            count++;
        }
    }
    catch (SQLException e) {
        Log.error(e.getMessage(), e);
    }
    finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return groupNames;
}
 
Example 5
Source File: OfflineMessageStore.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes all offline messages in the store for a user.
 *
 * @param username the username of the user who's messages are going to be deleted.
 */
public void deleteMessages(String username) {
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(DELETE_OFFLINE);
        pstmt.setString(1, username);
        pstmt.executeUpdate();
        
        removeUsernameFromSizeCache(username);
    }
    catch (Exception e) {
        Log.error("Error deleting offline messages of username: " + username, e);
    }
    finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
}
 
Example 6
Source File: DefaultGroupPropertyMap.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * Delete a group property from the database for the current group
 * 
 * @param key Property name
 */
private synchronized void deleteProperty(String key) {
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(DELETE_PROPERTY);
        pstmt.setString(1, group.getName());
        pstmt.setString(2, key);
        pstmt.executeUpdate();
    }
    catch (SQLException e) {
        logger.error(e.getMessage(), e);
    }
    finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
    Map<String, Object> event = new HashMap<>();
    event.put("type", "propertyDeleted");
    event.put("propertyKey", key);
    GroupEventDispatcher.dispatchEvent(group,
        GroupEventDispatcher.EventType.group_modified, event);
}
 
Example 7
Source File: DefaultGroupProvider.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<String> getGroupNames() {
    List<String> groupNames = new ArrayList<>();
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(ALL_GROUPS);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            groupNames.add(rs.getString(1));
        }
    }
    catch (SQLException e) {
        Log.error(e.getMessage(), e);
    }
    finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);       }
    return groupNames;
}
 
Example 8
Source File: AbstractGroupProvider.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<String> search(String key, String value) {
    Set<String> groupNames = new HashSet<>();
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(GROUPS_FOR_PROP);
        pstmt.setString(1, key);
        pstmt.setString(2, value);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            groupNames.add(rs.getString(1));
        }
    }
    catch (SQLException sqle) {
        Log.error(sqle.getMessage(), sqle);
    }
    finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return groupNames;
}
 
Example 9
Source File: RemoteServerManager.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the configuration for a remote server or {@code null} if none was found.
 *
 * @param domain the domain of the remote server.
 * @return the configuration for a remote server or {@code null} if none was found.
 */
public static RemoteServerConfiguration getConfiguration(String domain) {
    Object value = configurationsCache.get(domain);
    if ("null".equals(value)) {
        return null;
    }
    RemoteServerConfiguration configuration = (RemoteServerConfiguration) value;
    if (configuration == null) {
        java.sql.Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_CONFIGURATION);
            pstmt.setString(1, domain);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                configuration = new RemoteServerConfiguration(domain);
                configuration.setRemotePort(rs.getInt(1));
                configuration.setPermission(Permission.valueOf(rs.getString(2)));
            }
        }
        catch (SQLException sqle) {
            Log.error(sqle.getMessage(), sqle);
        }
        finally {
            DbConnectionManager.closeConnection(rs, pstmt, con);
        }
        if (configuration != null) {
            configurationsCache.put(domain, configuration);
        }
        else {
            configurationsCache.put(domain, "null");
        }
    }
    return configuration;
}
 
Example 10
Source File: DefaultRosterItemProvider.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteItem(String username, long rosterItemID) {
    // Only try to remove the user if they exist in the roster already:
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        rosterItemCache.remove( username );

        con = DbConnectionManager.getConnection();
        // Remove roster groups
        pstmt = con.prepareStatement(DELETE_ROSTER_ITEM_GROUPS);

        pstmt.setLong(1, rosterItemID);
        pstmt.executeUpdate();
        // Close now the statement (do not wait to be GC'ed)
        DbConnectionManager.fastcloseStmt(pstmt);

        // Remove roster
        pstmt = con.prepareStatement(DELETE_ROSTER_ITEM);

        pstmt.setLong(1, rosterItemID);
        pstmt.executeUpdate();
    }
    catch (SQLException e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    }
    finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
}
 
Example 11
Source File: ExternalComponentManager.java    From Openfire with Apache License 2.0 5 votes vote down vote up
private static Collection<ExternalComponentConfiguration> getConfigurations(
        Permission permission) {
    Collection<ExternalComponentConfiguration> answer =
            new ArrayList<>();
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(LOAD_CONFIGURATIONS);
        pstmt.setString(1, permission.toString());
        rs = pstmt.executeQuery();
        ExternalComponentConfiguration configuration;
        while (rs.next()) {
            String subdomain = rs.getString(1);
            boolean wildcard = rs.getInt(2) == 1;
            // Remove the trailing % if using wildcards
            subdomain = wildcard ? subdomain.substring(0, subdomain.length()-1) : subdomain;
            configuration = new ExternalComponentConfiguration(subdomain, wildcard, permission,
                    rs.getString(3));
            answer.add(configuration);
        }
    }
    catch (SQLException sqle) {
        Log.error(sqle.getMessage(), sqle);
    }
    finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return answer;
}
 
Example 12
Source File: JDBCAuthProvider.java    From Openfire with Apache License 2.0 5 votes vote down vote up
private void setPasswordValue(String username, String password) throws UserNotFoundException {
    Connection con = null;
    PreparedStatement pstmt = null;
    if (username.contains("@")) {
        // Check that the specified domain matches the server's domain
        int index = username.indexOf("@");
        String domain = username.substring(index + 1);
        if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
            username = username.substring(0, index);
        } else {
            // Unknown domain.
            throw new UserNotFoundException();
        }
    }
    try {
        con = getConnection();
        pstmt = con.prepareStatement(setPasswordSQL);

        // OF-1837: When the database does not hold escaped data, our query should use unescaped values in the 'where' clause.
        final String queryValue = assumePersistedDataIsEscaped() ? username : JID.unescapeNode( username );
        pstmt.setString(2, queryValue);

        password = hashPassword(password);
        pstmt.setString(1, password);
        pstmt.executeQuery();
    }
    catch (SQLException e) {
        Log.error("Exception in JDBCAuthProvider", e);
        throw new UserNotFoundException();
    }
    finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
    
}
 
Example 13
Source File: DefaultPubSubPersistenceProvider.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
   public PublishedItem getPublishedItem(LeafNode node, PublishedItem.UniqueIdentifier itemIdentifier)
   {
       // fetch item from DB
       Connection con = null;
       PreparedStatement pstmt = null;
       ResultSet rs;
       try {
           con = DbConnectionManager.getConnection();
           pstmt = con.prepareStatement(LOAD_ITEM);
           pstmt.setString(1, node.getUniqueIdentifier().getServiceIdentifier().getServiceId());
           pstmt.setString(2, node.getNodeID());
           pstmt.setString(3, itemIdentifier.getItemId());
           rs = pstmt.executeQuery();

           // Add to each node the corresponding subscriptions
           if (rs.next()) {
               JID publisher = new JID(rs.getString(1));
               Date creationDate = new Date(Long.parseLong(rs.getString(2).trim()));
               // Create the item
               final PublishedItem result = new PublishedItem(node, publisher, itemIdentifier.getItemId(), creationDate);
               // Add the extra fields to the published item
               if (rs.getString(3) != null) {
                   result.setPayloadXML(rs.getString(3));
               }
               log.debug("Loaded item from DB");
               return result;
           }
       } catch (Exception exc) {
           log.error("An exception occurred while trying to obtain item {} from node {}", itemIdentifier.getItemId(), node.getUniqueIdentifier(), exc);
       } finally {
           DbConnectionManager.closeConnection(pstmt, con);
       }
       return null;
}
 
Example 14
Source File: DefaultRosterItemProvider.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public RosterItem createItem(String username, RosterItem item)
        throws UserAlreadyExistsException
{
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        rosterItemCache.remove( username );

        long rosterID = SequenceManager.nextID(JiveConstants.ROSTER);
        con = DbConnectionManager.getConnection();
        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) {
        Log.warn("Error trying to insert a new row in ofRoster", e);
        throw new UserAlreadyExistsException(item.getJid().toBareJID());
    }
    finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
    return item;
}
 
Example 15
Source File: JDBCGroupProvider.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public Group getGroup(String name) throws GroupNotFoundException {
    String description = null;

    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = getConnection();
        pstmt = con.prepareStatement(descriptionSQL);
        pstmt.setString(1, name);
        rs = pstmt.executeQuery();
        if (!rs.next()) {
            throw new GroupNotFoundException("Group with name "
                    + name + " not found.");
        }
        description = rs.getString(1);
    }
    catch (SQLException e) {
        Log.error(e.getMessage(), e);
    }
    finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    Collection<JID> members = getMembers(name, false);
    Collection<JID> administrators = getMembers(name, true);
    return new Group(name, description, members, administrators);
}
 
Example 16
Source File: DefaultPubSubPersistenceProvider.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public PEPService loadPEPServiceFromDB(JID jid) {
    PEPService pepService = null;

    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        // Get all PEP services
        pstmt = con.prepareStatement(GET_PEP_SERVICE);
        pstmt.setString(1, jid.toString());
        rs = pstmt.executeQuery();
        // Restore old PEPService
        while (rs.next()) {
            String serviceID = rs.getString(1);
            if ( !jid.toString().equals( serviceID )) {
                log.warn( "Loading a PEP service for {} that has a different name: {}", jid, serviceID );
            }
            // Create a new PEPService
            pepService = new PEPService(XMPPServer.getInstance(), jid);
        }
    } catch (SQLException sqle) {
        log.error(sqle.getMessage(), sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }

    return pepService;
}
 
Example 17
Source File: DefaultAuthProvider.java    From Openfire with Apache License 2.0 4 votes vote down vote up
@Override
public void setPassword(String username, String password) throws UserNotFoundException {
    // Determine if the password should be stored as plain text or encrypted.
    boolean usePlainPassword = JiveGlobals.getBooleanProperty("user.usePlainPassword");
    boolean scramOnly = JiveGlobals.getBooleanProperty("user.scramHashedPasswordOnly");
    String encryptedPassword = null;
    if (username.contains("@")) {
        // Check that the specified domain matches the server's domain
        int index = username.indexOf("@");
        String domain = username.substring(index + 1);
        if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
            username = username.substring(0, index);
        } else {
            // Unknown domain.
            throw new UserNotFoundException();
        }
    }
    
    // Store the salt and salted password so SCRAM-SHA-1 SASL auth can be used later.
    byte[] saltShaker = new byte[24];
    random.nextBytes(saltShaker);
    String salt = DatatypeConverter.printBase64Binary(saltShaker);

    
    final int iterations = ScramSha1SaslServer.ITERATION_COUNT.getValue();
    byte[] saltedPassword = null, clientKey = null, storedKey = null, serverKey = null;
try {
       saltedPassword = ScramUtils.createSaltedPassword(saltShaker, password, iterations);
           clientKey = ScramUtils.computeHmac(saltedPassword, "Client Key");
           storedKey = MessageDigest.getInstance("SHA-1").digest(clientKey);
           serverKey = ScramUtils.computeHmac(saltedPassword, "Server Key");
   } catch (SaslException | NoSuchAlgorithmException e) {
       Log.warn("Unable to persist values for SCRAM authentication.");
   }
   
    if (!scramOnly && !usePlainPassword) {
        try {
            encryptedPassword = AuthFactory.encryptPassword(password);
            // Set password to null so that it's inserted that way.
            password = null;
        }
        catch (UnsupportedOperationException uoe) {
            // Encryption may fail. In that case, ignore the error and
            // the plain password will be stored.
        }
    }
    if (scramOnly) {
        encryptedPassword = null;
        password = null;
    }

    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(UPDATE_PASSWORD);
        if (password == null) {
            pstmt.setNull(1, Types.VARCHAR);
        }
        else {
            pstmt.setString(1, password);
        }
        if (encryptedPassword == null) {
            pstmt.setNull(2, Types.VARCHAR);
        }
        else {
            pstmt.setString(2, encryptedPassword);
        }
        if (storedKey == null) {
            pstmt.setNull(3, Types.VARCHAR);
        }
        else {
            pstmt.setString(3, DatatypeConverter.printBase64Binary(storedKey));
        }
        if (serverKey == null) {
            pstmt.setNull(4, Types.VARCHAR);
        }
        else {
            pstmt.setString(4, DatatypeConverter.printBase64Binary(serverKey));
        }
        pstmt.setString(5, salt);
        pstmt.setInt(6, iterations);
        pstmt.setString(7, username);
        pstmt.executeUpdate();
    }
    catch (SQLException sqle) {
        throw new UserNotFoundException(sqle);
    }
    finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
}
 
Example 18
Source File: DefaultPubSubPersistenceProvider.java    From Openfire with Apache License 2.0 4 votes vote down vote up
@Override
public void createSubscription(Node node, NodeSubscription subscription)
{
    log.trace( "Creating node subscription: {} {} (write to database)", node.getUniqueIdentifier(), subscription.getID() );
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        con = DbConnectionManager.getConnection();
        // Add the subscription of the user to the database
        pstmt = con.prepareStatement(ADD_SUBSCRIPTION);
        pstmt.setString(1, node.getUniqueIdentifier().getServiceIdentifier().getServiceId());
        pstmt.setString(2, encodeNodeID(node.getNodeID()));
        pstmt.setString(3, subscription.getID());
        pstmt.setString(4, subscription.getJID().toString());
        pstmt.setString(5, subscription.getOwner().toString());
        pstmt.setString(6, subscription.getState().name());
        pstmt.setInt(7, (subscription.shouldDeliverNotifications() ? 1 : 0));
        pstmt.setInt(8, (subscription.isUsingDigest() ? 1 : 0));
        pstmt.setInt(9, subscription.getDigestFrequency());
        Date expireDate = subscription.getExpire();
        if (expireDate == null) {
            pstmt.setString(10, null);
        }
        else {
            pstmt.setString(10, StringUtils.dateToMillis(expireDate));
        }
        pstmt.setInt(11, (subscription.isIncludingBody() ? 1 : 0));
        pstmt.setString(12, encodeWithComma(subscription.getPresenceStates()));
        pstmt.setString(13, subscription.getType().name());
        pstmt.setInt(14, subscription.getDepth());
        pstmt.setString(15, subscription.getKeyword());
        pstmt.executeUpdate();
        // Indicate the subscription that is has been saved to the database
        subscription.setSavedToDB(true);
    }
    catch (SQLException sqle) {
        log.error("An exception occurred while creating a subscription ({}) to a node ({}) in the database.", subscription, node.getUniqueIdentifier(), sqle);
    }
    finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
}
 
Example 19
Source File: MUCPersistenceManager.java    From Openfire with Apache License 2.0 4 votes vote down vote up
private static Map<Long, LocalMUCRoom> loadRooms(Long serviceID, Date cleanupDate, MultiUserChatService chatserver, PacketRouter packetRouter) throws SQLException {
    final Map<Long, LocalMUCRoom> rooms = new HashMap<>();

    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
        connection = DbConnectionManager.getConnection();
        if (cleanupDate!=null) 
        {
            statement = connection.prepareStatement(RELOAD_ALL_ROOMS_WITH_RECENT_ACTIVITY);
            statement.setLong(1, serviceID);
            statement.setString(2, StringUtils.dateToMillis(cleanupDate));
        }
        else
        {
            statement = connection.prepareStatement(LOAD_ALL_ROOMS);
            statement.setLong(1, serviceID);
        }
        resultSet = statement.executeQuery();

        while (resultSet.next()) {
            try {
                LocalMUCRoom room = new LocalMUCRoom(chatserver, resultSet.getString(4), packetRouter);
                room.setID(resultSet.getLong(1));
                room.setCreationDate(new Date(Long.parseLong(resultSet.getString(2).trim()))); // creation date
                room.setModificationDate(new Date(Long.parseLong(resultSet.getString(3).trim()))); // modification date
                room.setNaturalLanguageName(resultSet.getString(5));
                room.setDescription(resultSet.getString(6));
                room.setLockedDate(new Date(Long.parseLong(resultSet.getString(7).trim())));
                if (resultSet.getString(8) != null) {
                    room.setEmptyDate(new Date(Long.parseLong(resultSet.getString(8).trim())));
                }
                else {
                    room.setEmptyDate(null);
                }
                room.setCanOccupantsChangeSubject(resultSet.getInt(9) == 1);
                room.setMaxUsers(resultSet.getInt(10));
                room.setPublicRoom(resultSet.getInt(11) == 1);
                room.setModerated(resultSet.getInt(12) == 1);
                room.setMembersOnly(resultSet.getInt(13) == 1);
                room.setCanOccupantsInvite(resultSet.getInt(14) == 1);
                room.setPassword(resultSet.getString(15));
                room.setCanAnyoneDiscoverJID(resultSet.getInt(16) == 1);
                room.setLogEnabled(resultSet.getInt(17) == 1);
                room.setSubject(resultSet.getString(18));
                List<String> rolesToBroadcast = new ArrayList<>();
                String roles = StringUtils.zeroPadString(Integer.toBinaryString(resultSet.getInt(19)), 3);
                if (roles.charAt(0) == '1') {
                    rolesToBroadcast.add("moderator");
                }
                if (roles.charAt(1) == '1') {
                    rolesToBroadcast.add("participant");
                }
                if (roles.charAt(2) == '1') {
                    rolesToBroadcast.add("visitor");
                }
                room.setRolesToBroadcastPresence(rolesToBroadcast);
                room.setLoginRestrictedToNickname(resultSet.getInt(20) == 1);
                room.setChangeNickname(resultSet.getInt(21) == 1);
                room.setRegistrationEnabled(resultSet.getInt(22) == 1);
                switch (resultSet.getInt(23)) // null returns 0.
                {
                    default:
                    case 0: room.setCanSendPrivateMessage( "anyone"       ); break;
                    case 1: room.setCanSendPrivateMessage( "participants" ); break;
                    case 2: room.setCanSendPrivateMessage( "moderators"   ); break;
                    case 3: room.setCanSendPrivateMessage( "none"         ); break;
                }
                room.setPersistent(true);
                rooms.put(room.getID(), room);
            } catch (SQLException e) {
                Log.error("A database exception prevented one particular MUC room to be loaded from the database.", e);
            }
        }
    } finally {
        DbConnectionManager.closeConnection(resultSet, statement, connection);
    }

    return rooms;
}
 
Example 20
Source File: DefaultAuthProvider.java    From Openfire with Apache License 2.0 4 votes vote down vote up
private UserInfo getUserInfo(String username, boolean recurse) throws UnsupportedOperationException, UserNotFoundException {
    if (!isScramSupported()) {
        // Reject the operation since the provider  does not support SCRAM
        throw new UnsupportedOperationException();
    }
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(TEST_PASSWORD);
        pstmt.setString(1, username);
        rs = pstmt.executeQuery();
        if (!rs.next()) {
            throw new UserNotFoundException(username);
        }
        UserInfo userInfo = new UserInfo();
        userInfo.plainText = rs.getString(1);
        userInfo.encrypted = rs.getString(2);
        userInfo.iterations = rs.getInt(3);
        userInfo.salt = rs.getString(4);
        userInfo.storedKey = rs.getString(5);
        userInfo.serverKey = rs.getString(6);
        if (userInfo.encrypted != null) {
            try {
                userInfo.plainText = AuthFactory.decryptPassword(userInfo.encrypted);
            }
            catch (UnsupportedOperationException uoe) {
                // Ignore and return plain password instead.
            }
        }
        if (!recurse) {
            if (userInfo.plainText != null) {
                boolean scramOnly = JiveGlobals.getBooleanProperty("user.scramHashedPasswordOnly");
                if (scramOnly || userInfo.salt == null) {
                    // If we have a password here, but we're meant to be scramOnly, we should reset it.
                    setPassword(username, userInfo.plainText);
                    // RECURSE
                    return getUserInfo(username, true);
                }
            }
        }
        // Good to go.
        return userInfo;
    }
    catch (SQLException sqle) {
        Log.error("User SQL failure:", sqle);
        throw new UserNotFoundException(sqle);
    }
    finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
}