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 org.apache.james.user.api.model.User; 023import org.apache.james.user.lib.model.DefaultJamesUser; 024import org.apache.james.user.lib.model.DefaultUser; 025import org.apache.mailet.MailAddress; 026 027import java.sql.PreparedStatement; 028import java.sql.ResultSet; 029import java.sql.SQLException; 030 031/** 032 * A Jdbc-backed UserRepository which handles User instances of the 033 * <code>DefaultJamesUser</code> class, or any superclass. 034 */ 035@Deprecated 036public class JamesUsersJdbcRepository extends AbstractJdbcUsersRepository { 037 038 /** 039 * @see org.apache.james.user.jdbc.AbstractJdbcUsersRepository#readUserFromResultSet(java.sql.ResultSet) 040 */ 041 protected User readUserFromResultSet(ResultSet rsUsers) throws SQLException { 042 // Get the column values 043 String username = rsUsers.getString(1); 044 String pwdHash = rsUsers.getString(2); 045 String pwdAlgorithm = rsUsers.getString(3); 046 boolean useForwarding = rsUsers.getBoolean(4); 047 String forwardingDestination = rsUsers.getString(5); 048 boolean useAlias = rsUsers.getBoolean(6); 049 String alias = rsUsers.getString(7); 050 051 MailAddress forwardAddress = null; 052 if (forwardingDestination != null) { 053 try { 054 forwardAddress = new MailAddress(forwardingDestination); 055 } catch (javax.mail.internet.ParseException pe) { 056 String exceptionBuffer = "Invalid mail address in database: " + forwardingDestination + ", for user " + username + "."; 057 throw new RuntimeException(exceptionBuffer); 058 } 059 } 060 061 // Build a DefaultJamesUser with these values, and add to the list. 062 DefaultJamesUser user = new DefaultJamesUser(username, pwdHash, pwdAlgorithm); 063 user.setForwarding(useForwarding); 064 user.setForwardingDestination(forwardAddress); 065 user.setAliasing(useAlias); 066 user.setAlias(alias); 067 068 return user; 069 } 070 071 /** 072 * @see org.apache.james.user.jdbc.AbstractJdbcUsersRepository#setUserForInsertStatement(org.apache.james.user.api.model.User, 073 * java.sql.PreparedStatement) 074 */ 075 protected void setUserForInsertStatement(User user, PreparedStatement userInsert) throws SQLException { 076 setUserForStatement(user, userInsert, false); 077 } 078 079 /** 080 * @see org.apache.james.user.jdbc.AbstractJdbcUsersRepository#setUserForUpdateStatement(org.apache.james.user.api.model.User, 081 * java.sql.PreparedStatement) 082 */ 083 protected void setUserForUpdateStatement(User user, PreparedStatement userUpdate) throws SQLException { 084 setUserForStatement(user, userUpdate, true); 085 } 086 087 /** 088 * Sets the data for the prepared statement to match the information in the 089 * user object. 090 * 091 * @param user 092 * the user whose data is to be stored in the PreparedStatement. 093 * @param stmt 094 * the PreparedStatement to be modified. 095 * @param userNameLast 096 * whether the user id is the last or the first column 097 */ 098 private void setUserForStatement(User user, PreparedStatement stmt, boolean userNameLast) throws SQLException { 099 // Determine column offsets to use, based on username column pos. 100 int nameIndex = 1; 101 int colOffset = 1; 102 if (userNameLast) { 103 nameIndex = 7; 104 colOffset = 0; 105 } 106 107 // Can handle instances of DefaultJamesUser and DefaultUser. 108 DefaultJamesUser jamesUser; 109 if (user instanceof DefaultJamesUser) { 110 jamesUser = (DefaultJamesUser) user; 111 } else if (user instanceof DefaultUser) { 112 DefaultUser aUser = (DefaultUser) user; 113 jamesUser = new DefaultJamesUser(aUser.getUserName(), aUser.getHashedPassword(), aUser.getHashAlgorithm()); 114 } 115 // Can't handle any other implementations. 116 else { 117 throw new RuntimeException("An unknown implementation of User was " + "found. This implementation cannot be " + "persisted to a UsersJDBCRepsitory."); 118 } 119 120 // Get the user details to save. 121 stmt.setString(nameIndex, jamesUser.getUserName()); 122 stmt.setString(1 + colOffset, jamesUser.getHashedPassword()); 123 stmt.setString(2 + colOffset, jamesUser.getHashAlgorithm()); 124 stmt.setInt(3 + colOffset, (jamesUser.getForwarding() ? 1 : 0)); 125 126 MailAddress forwardAddress = jamesUser.getForwardingDestination(); 127 String forwardDestination = null; 128 if (forwardAddress != null) { 129 forwardDestination = forwardAddress.toString(); 130 } 131 stmt.setString(4 + colOffset, forwardDestination); 132 stmt.setInt(5 + colOffset, (jamesUser.getAliasing() ? 1 : 0)); 133 stmt.setString(6 + colOffset, jamesUser.getAlias()); 134 } 135 136}