001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.mail.util.multiaccount;
022:
023: import com.liferay.portal.kernel.util.Validator;
024: import com.liferay.portal.model.User;
025: import com.liferay.portal.util.PortalUtil;
026: import com.liferay.portal.util.PropsUtil;
027: import com.liferay.portal.util.WebKeys;
028: import com.liferay.portlet.mail.AccountNotFoundException;
029: import com.liferay.portlet.mail.MailAccountsException;
030:
031: import java.util.ArrayList;
032: import java.util.Collection;
033:
034: import javax.portlet.ActionRequest;
035:
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpSession;
038:
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: /**
043: * <a href="MailAccounts.java.html"><b><i>View Source</i></b></a>
044: *
045: * <p>
046: * Handles all the mail accounts that belong to the current user.
047: * </p>
048: *
049: * @author Jorge Ferrer
050: *
051: */
052: public class MailAccounts {
053:
054: public static final String ACCOUNT_FINDER_PASSWORD = PropsUtil
055: .get(PropsUtil.MAIL_ACCOUNT_FINDER_PASSWORD);
056:
057: public static MailAccount getCurrentAccount(HttpServletRequest req)
058: throws MailAccountsException {
059:
060: HttpSession ses = req.getSession();
061:
062: MailAccount previousAccount = (MailAccount) ses
063: .getAttribute(WebKeys.MAIL_CURRENT_ACCOUNT);
064:
065: if (previousAccount == null) {
066: MailAccount defaultAccount = _getAccount(req,
067: _getDefaultAccountName());
068:
069: ses.setAttribute(WebKeys.MAIL_CURRENT_ACCOUNT,
070: defaultAccount);
071:
072: previousAccount = defaultAccount;
073: }
074:
075: return previousAccount;
076: }
077:
078: public static MailAccount getCurrentAccount(ActionRequest req)
079: throws MailAccountsException {
080:
081: HttpServletRequest httpReq = PortalUtil
082: .getHttpServletRequest(req);
083:
084: return getCurrentAccount(httpReq);
085: }
086:
087: public static void setAccount(HttpServletRequest req,
088: String accountName) throws MailAccountsException {
089:
090: HttpSession ses = req.getSession();
091:
092: MailAccount account = _getAccount(req, accountName);
093:
094: if (_log.isInfoEnabled()) {
095: _log.info("Switched to account " + account);
096: }
097:
098: ses.setAttribute(WebKeys.MAIL_CURRENT_ACCOUNT, account);
099: }
100:
101: private static MailAccount _getAccount(HttpServletRequest req,
102: String accountName) throws MailAccountsException {
103:
104: HttpSession ses = req.getSession();
105:
106: MailAccount account = null;
107:
108: AccountFinder accountFinder = AccountFinderLocator
109: .getAccountFinderInstance();
110:
111: User user = null;
112: String password = null;
113:
114: try {
115: user = PortalUtil.getUser(req);
116:
117: if (Validator.isNull(ACCOUNT_FINDER_PASSWORD)) {
118: password = PortalUtil.getUserPassword(ses);
119:
120: if (password == null) {
121: password = user.getPassword();
122: }
123: } else {
124: password = ACCOUNT_FINDER_PASSWORD;
125: }
126:
127: account = accountFinder.findAccount(user, password,
128: accountName);
129: } catch (AccountNotFoundException anfe) {
130: try {
131: account = accountFinder.findAccount(user, password,
132: accountFinder.getDefaultAccountName());
133: } catch (Exception e1) {
134: throw new MailAccountsException(
135: "Could not read the default account", e1);
136: }
137: } catch (Exception e2) {
138: throw new MailAccountsException(
139: "Could not read the account " + accountName, e2);
140: }
141:
142: // Call find all accounts to cache the list of accounts associated with
143: // a user. If this method is not called, then when a user logs off,
144: // there will not be any accounts associated with that user, and so it
145: // becomes impossible to close the stores associated with the accounts
146: // that are associated with that user. Closing the stores dramatically
147: // improves performance because it closes all associated JavaMail
148: // connections to the IMAP server.
149:
150: _findAllAccounts(user, password);
151:
152: return account;
153: }
154:
155: private static String _getDefaultAccountName() {
156: AccountFinder accountFinder = AccountFinderLocator
157: .getAccountFinderInstance();
158:
159: return accountFinder.getDefaultAccountName();
160: }
161:
162: private static Collection _findAllAccounts(User user,
163: String password) {
164: Collection cachedAccounts = MailCache.getUserAccounts(user
165: .getUserId());
166:
167: if (cachedAccounts != null) {
168: return cachedAccounts;
169: }
170:
171: try {
172: AccountFinder accountFinder = AccountFinderLocator
173: .getAccountFinderInstance();
174:
175: Collection accounts = accountFinder.findAllAccounts(user,
176: password);
177:
178: MailCache.putUserAccounts(user.getUserId(), accounts);
179:
180: return accounts;
181: } catch (MailAccountsException mae) {
182: _log.error("Error trying to get all accounts for user "
183: + user.getUserId(), mae);
184:
185: return new ArrayList();
186: }
187: }
188:
189: private static Log _log = LogFactory.getLog(MailAccounts.class);
190:
191: }
|