1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.mail.util.multiaccount;
24  
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.PropsUtil;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.mail.AccountNotFoundException;
31  import com.liferay.portlet.mail.MailAccountsException;
32  
33  import java.util.ArrayList;
34  import java.util.Collection;
35  
36  import javax.portlet.ActionRequest;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpSession;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="MailAccounts.java.html"><b><i>View Source</i></b></a>
46   *
47   * <p>
48   * Handles all the mail accounts that belong to the current user.
49   * </p>
50   *
51   * @author Jorge Ferrer
52   *
53   */
54  public class MailAccounts {
55  
56      public static final String ACCOUNT_FINDER_PASSWORD = PropsUtil.get(
57          PropsUtil.MAIL_ACCOUNT_FINDER_PASSWORD);
58  
59      public static MailAccount getCurrentAccount(HttpServletRequest req)
60          throws MailAccountsException {
61  
62          HttpSession ses = req.getSession();
63  
64          MailAccount previousAccount =
65              (MailAccount)ses.getAttribute(WebKeys.MAIL_CURRENT_ACCOUNT);
66  
67          if (previousAccount == null) {
68              MailAccount defaultAccount =
69                  _getAccount(req, _getDefaultAccountName());
70  
71              ses.setAttribute(WebKeys.MAIL_CURRENT_ACCOUNT, defaultAccount);
72  
73              previousAccount = defaultAccount;
74          }
75  
76          return previousAccount;
77      }
78  
79      public static MailAccount getCurrentAccount(ActionRequest req)
80          throws MailAccountsException {
81  
82          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
83  
84          return getCurrentAccount(httpReq);
85      }
86  
87      public static void setAccount(HttpServletRequest req, String accountName)
88          throws MailAccountsException {
89  
90          HttpSession ses = req.getSession();
91  
92          MailAccount account = _getAccount(req, accountName);
93  
94          if (_log.isInfoEnabled()) {
95              _log.info("Switched to account " + account);
96          }
97  
98          ses.setAttribute(WebKeys.MAIL_CURRENT_ACCOUNT, account);
99      }
100 
101     private static MailAccount _getAccount(
102             HttpServletRequest req, String accountName)
103         throws MailAccountsException {
104 
105         HttpSession ses = req.getSession();
106 
107         MailAccount account = null;
108 
109         AccountFinder accountFinder =
110             AccountFinderLocator.getAccountFinderInstance();
111 
112         User user = null;
113         String password = null;
114 
115         try {
116             user = PortalUtil.getUser(req);
117 
118             if (Validator.isNull(ACCOUNT_FINDER_PASSWORD)) {
119                 password = PortalUtil.getUserPassword(ses);
120 
121                 if (password == null) {
122                     password = user.getPassword();
123                 }
124             }
125             else {
126                 password = ACCOUNT_FINDER_PASSWORD;
127             }
128 
129             account = accountFinder.findAccount(user, password, accountName);
130         }
131         catch (AccountNotFoundException anfe) {
132             try {
133                 account = accountFinder.findAccount(
134                     user, password, accountFinder.getDefaultAccountName());
135             }
136             catch (Exception e1) {
137                 throw new MailAccountsException(
138                     "Could not read the default account", e1);
139             }
140         }
141         catch (Exception e2) {
142             throw new MailAccountsException(
143                 "Could not read the account " + accountName, e2);
144         }
145 
146         // Call find all accounts to cache the list of accounts associated with
147         // a user. If this method is not called, then when a user logs off,
148         // there will not be any accounts associated with that user, and so it
149         // becomes impossible to close the stores associated with the accounts
150         // that are associated with that user. Closing the stores dramatically
151         // improves performance because it closes all associated JavaMail
152         // connections to the IMAP server.
153 
154         _findAllAccounts(user, password);
155 
156         return account;
157     }
158 
159     private static String _getDefaultAccountName() {
160         AccountFinder accountFinder =
161             AccountFinderLocator.getAccountFinderInstance();
162 
163         return accountFinder.getDefaultAccountName();
164     }
165 
166     private static Collection _findAllAccounts(User user, String password) {
167         Collection cachedAccounts = MailCache.getUserAccounts(user.getUserId());
168 
169         if (cachedAccounts != null) {
170             return cachedAccounts;
171         }
172 
173         try {
174             AccountFinder accountFinder =
175                 AccountFinderLocator.getAccountFinderInstance();
176 
177             Collection accounts = accountFinder.findAllAccounts(user, password);
178 
179             MailCache.putUserAccounts(user.getUserId(), accounts);
180 
181             return accounts;
182         }
183         catch (MailAccountsException mae) {
184             _log.error(
185                 "Error trying to get all accounts for user " + user.getUserId(),
186                 mae);
187 
188             return new ArrayList();
189         }
190     }
191 
192     private static Log _log = LogFactory.getLog(MailAccounts.class);
193 
194 }