001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.user.jdbc;
021    
022    import org.apache.james.user.api.UsersRepositoryException;
023    import org.apache.james.user.api.model.User;
024    import org.apache.james.user.lib.model.DefaultUser;
025    
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    import java.sql.SQLException;
029    
030    /**
031     * A Jdbc-backed UserRepository which handles User instances of the
032     * <code>DefaultUser</code> class.<br>
033     * Although this repository can handle subclasses of DefaultUser, like
034     * <code>DefaultJamesUser</code>, only properties from the DefaultUser class are
035     * persisted.
036     * 
037     * TODO Please note that default configuration uses JamesUsersJdbcRepository
038     * instead of this class. So we could also delete this implementation.
039     * 
040     */
041    @Deprecated
042    public class DefaultUsersJdbcRepository extends AbstractJdbcUsersRepository {
043    
044        /**
045         * @see org.apache.james.user.jdbc.AbstractJdbcUsersRepository#readUserFromResultSet(java.sql.ResultSet)
046         */
047        protected User readUserFromResultSet(ResultSet rsUsers) throws SQLException {
048            // Get the username, and build a DefaultUser with it.
049            String username = rsUsers.getString(1);
050            String passwordHash = rsUsers.getString(2);
051            String passwordAlg = rsUsers.getString(3);
052            DefaultUser user = new DefaultUser(username, passwordHash, passwordAlg);
053            return user;
054        }
055    
056        /**
057         * @see org.apache.james.user.jdbc.AbstractJdbcUsersRepository#setUserForInsertStatement(org.apache.james.user.api.model.User,
058         *      java.sql.PreparedStatement)
059         */
060        protected void setUserForInsertStatement(User user, PreparedStatement userInsert) throws SQLException {
061            DefaultUser defUser = (DefaultUser) user;
062            userInsert.setString(1, defUser.getUserName());
063            userInsert.setString(2, defUser.getHashedPassword());
064            userInsert.setString(3, defUser.getHashAlgorithm());
065        }
066    
067        /**
068         * @see org.apache.james.user.jdbc.AbstractJdbcUsersRepository#setUserForUpdateStatement(org.apache.james.user.api.model.User,
069         *      java.sql.PreparedStatement)
070         */
071        protected void setUserForUpdateStatement(User user, PreparedStatement userUpdate) throws SQLException {
072            DefaultUser defUser = (DefaultUser) user;
073            userUpdate.setString(1, defUser.getHashedPassword());
074            userUpdate.setString(2, defUser.getHashAlgorithm());
075            userUpdate.setString(3, defUser.getUserName());
076        }
077    
078        /**
079         * @see org.apache.james.user.api.UsersRepository#addUser(java.lang.String,
080         *      java.lang.String)
081         */
082        public void addUser(String username, String password) throws UsersRepositoryException {
083            if (contains(username) == true) {
084                throw new UsersRepositoryException("User " + username + " already exist");
085            }
086            isValidUsername(username);
087            User newbie = new DefaultUser(username, "SHA");
088            newbie.setPassword(password);
089            doAddUser(newbie);
090        }
091    
092    }