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
020package org.apache.james.user.jdbc;
021
022import java.sql.PreparedStatement;
023import java.sql.ResultSet;
024import java.sql.SQLException;
025
026import org.apache.james.user.api.AlreadyExistInUsersRepositoryException;
027import org.apache.james.user.api.UsersRepositoryException;
028import org.apache.james.user.api.model.User;
029import org.apache.james.user.lib.model.DefaultUser;
030
031/**
032 * A Jdbc-backed UserRepository which handles User instances of the
033 * <code>DefaultUser</code> class.<br>
034 * Although this repository can handle subclasses of DefaultUser, like
035 * <code>DefaultJamesUser</code>, only properties from the DefaultUser class are
036 * persisted.
037 * <p/>
038 * TODO Please note that default configuration uses JamesUsersJdbcRepository
039 * instead of this class. So we could also delete this implementation.
040 */
041@Deprecated
042public class DefaultUsersJdbcRepository extends AbstractJdbcUsersRepository {
043
044    @Override
045    protected User readUserFromResultSet(ResultSet rsUsers) throws SQLException {
046        // Get the username, and build a DefaultUser with it.
047        String username = rsUsers.getString(1);
048        String passwordHash = rsUsers.getString(2);
049        String passwordAlg = rsUsers.getString(3);
050        return new DefaultUser(username, passwordHash, passwordAlg);
051    }
052
053    @Override
054    protected void setUserForInsertStatement(User user, PreparedStatement userInsert) throws SQLException {
055        DefaultUser defUser = (DefaultUser) user;
056        userInsert.setString(1, defUser.getUserName());
057        userInsert.setString(2, defUser.getHashedPassword());
058        userInsert.setString(3, defUser.getHashAlgorithm());
059    }
060
061    @Override
062    protected void setUserForUpdateStatement(User user, PreparedStatement userUpdate) throws SQLException {
063        DefaultUser defUser = (DefaultUser) user;
064        userUpdate.setString(1, defUser.getHashedPassword());
065        userUpdate.setString(2, defUser.getHashAlgorithm());
066        userUpdate.setString(3, defUser.getUserName());
067    }
068
069    @Override
070    public void addUser(String username, String password) throws UsersRepositoryException {
071        if (contains(username)) {
072            throw new AlreadyExistInUsersRepositoryException("User " + username + " already exist");
073        }
074        isValidUsername(username);
075        User newbie = new DefaultUser(username, "SHA");
076        newbie.setPassword(password);
077        doAddUser(newbie);
078    }
079
080}