View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.userrepository;
23  
24  import org.apache.james.api.user.User;
25  import org.apache.james.impl.user.DefaultJamesUser;
26  import org.apache.james.impl.user.DefaultUser;
27  import org.apache.mailet.MailAddress;
28  
29  import java.sql.PreparedStatement;
30  import java.sql.ResultSet;
31  import java.sql.SQLException;
32  
33  /**
34   * A Jdbc-backed UserRepository which handles User instances of the <CODE>DefaultJamesUser</CODE>
35   * class, or any superclass.
36   */
37  public class JamesUsersJdbcRepository extends AbstractJdbcUsersRepository {
38      /**
39       * @see org.apache.james.userrepository.AbstractJdbcUsersRepository#readUserFromResultSet(java.sql.ResultSet)
40       */
41      protected User readUserFromResultSet(ResultSet rsUsers) throws SQLException {
42          // Get the column values
43          String username = rsUsers.getString(1);
44          String pwdHash = rsUsers.getString(2);
45          String pwdAlgorithm = rsUsers.getString(3);
46          boolean useForwarding = rsUsers.getBoolean(4);
47          String forwardingDestination = rsUsers.getString(5);
48          boolean useAlias = rsUsers.getBoolean(6);
49          String alias = rsUsers.getString(7);
50  
51          MailAddress forwardAddress = null;
52          if ( forwardingDestination != null ) {
53              try {
54                  forwardAddress = new MailAddress(forwardingDestination);
55              } catch (javax.mail.internet.ParseException pe) {
56                  StringBuffer exceptionBuffer = new StringBuffer(256).append(
57                          "Invalid mail address in database: ").append(
58                          forwardingDestination).append(", for user ").append(
59                          username).append(".");
60                  throw new RuntimeException(exceptionBuffer.toString());
61              }
62          }
63  
64          // Build a DefaultJamesUser with these values, and add to the list.
65          DefaultJamesUser user = new DefaultJamesUser(username, pwdHash,
66                  pwdAlgorithm);
67          user.setForwarding(useForwarding);
68          user.setForwardingDestination(forwardAddress);
69          user.setAliasing(useAlias);
70          user.setAlias(alias);
71  
72          return user;
73      }
74  
75  
76      /**
77       * @see org.apache.james.userrepository.AbstractJdbcUsersRepository#setUserForInsertStatement(org.apache.james.api.user.User,
78       *      java.sql.PreparedStatement)
79       */
80      protected void setUserForInsertStatement(User user, 
81              PreparedStatement userInsert) throws SQLException {
82          setUserForStatement(user, userInsert, false);
83      }
84  
85      /**
86       * @see org.apache.james.userrepository.AbstractJdbcUsersRepository#setUserForUpdateStatement(org.apache.james.api.user.User,
87       *      java.sql.PreparedStatement)
88       */
89      protected void setUserForUpdateStatement(User user, 
90              PreparedStatement userUpdate) throws SQLException {
91          setUserForStatement(user, userUpdate, true);
92      }
93  
94      /**
95       * Sets the data for the prepared statement to match the information in the
96       * user object.
97       *
98       * @param user
99       *            the user whose data is to be stored in the PreparedStatement.
100      * @param stmt
101      *            the PreparedStatement to be modified.
102      * @param userNameLast
103      *            whether the user id is the last or the first column
104      */
105     private void setUserForStatement(User user, PreparedStatement stmt,
106                                      boolean userNameLast) throws SQLException {
107         // Determine column offsets to use, based on username column pos.
108         int nameIndex = 1;
109         int colOffset = 1;
110         if ( userNameLast ) {
111             nameIndex = 7;
112             colOffset = 0;
113         }
114 
115         // Can handle instances of DefaultJamesUser and DefaultUser.
116         DefaultJamesUser jamesUser;
117         if (user instanceof DefaultJamesUser) {
118             jamesUser = (DefaultJamesUser)user;
119         } else if (user instanceof DefaultUser) {
120             DefaultUser aUser = (DefaultUser)user;
121             jamesUser = new DefaultJamesUser(aUser.getUserName(), aUser
122                     .getHashedPassword(), aUser.getHashAlgorithm());
123         } 
124         // Can't handle any other implementations.
125         else {
126             throw new RuntimeException("An unknown implementation of User was "
127                     + "found. This implementation cannot be "
128                     + "persisted to a UsersJDBCRepsitory.");
129         }
130 
131         // Get the user details to save.
132         stmt.setString(nameIndex, jamesUser.getUserName());
133         stmt.setString(1 + colOffset, jamesUser.getHashedPassword());
134         stmt.setString(2 + colOffset, jamesUser.getHashAlgorithm());
135         stmt.setInt(3 + colOffset, (jamesUser.getForwarding() ? 1 : 0));
136 
137         MailAddress forwardAddress = jamesUser.getForwardingDestination();
138         String forwardDestination = null;
139         if ( forwardAddress != null ) {
140             forwardDestination = forwardAddress.toString();
141         }
142         stmt.setString(4 + colOffset, forwardDestination);
143         stmt.setInt(5 + colOffset, (jamesUser.getAliasing() ? 1 : 0));
144         stmt.setString(6 + colOffset, jamesUser.getAlias());
145     }
146     
147     
148     
149     /**
150      * @see org.apache.james.api.user.UsersRepository#addUser(java.lang.String,
151      *      java.lang.String)
152      */
153     public boolean addUser(String username, String password)  {
154         User newbie = new DefaultJamesUser(username, "SHA");
155         newbie.setPassword(password);
156         return addUser(newbie);
157     }
158     
159 }