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.mail.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.model.User;
026: import com.liferay.portal.service.UserLocalServiceUtil;
027: import com.liferay.portal.util.PropsUtil;
028:
029: import java.util.List;
030:
031: import org.apache.commons.httpclient.HttpClient;
032: import org.apache.commons.httpclient.NameValuePair;
033: import org.apache.commons.httpclient.methods.PostMethod;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: /**
038: * <a href="FuseMailHook.java.html"><b><i>View Source</i></b></a>
039: *
040: * @author Brian Wing Shun Chan
041: *
042: */
043: public class FuseMailHook implements Hook {
044:
045: public FuseMailHook() {
046: _client = new HttpClient();
047: }
048:
049: public void addForward(long userId, List filters,
050: List emailAddresses, boolean leaveCopy) {
051: }
052:
053: public void addUser(long userId, String password, String firstName,
054: String middleName, String lastName, String emailAddress) {
055:
056: try {
057: String mailUserId = getMailUserId(userId);
058:
059: PostMethod method = getPostMethod();
060:
061: method.addParameter("request", "order");
062: method.addParameter("user", mailUserId);
063: method.addParameter("password", password);
064: method.addParameter("first_name", firstName);
065: method.addParameter("last_name", lastName);
066: method.addParameter("account_type", _ACCOUNT_TYPE);
067: method.addParameter("group_parent", _GROUP_PARENT);
068: method.addParameter("alias[0]", emailAddress);
069:
070: executeMethod(method);
071: } catch (Exception e) {
072: _log.error(e, e);
073: }
074: }
075:
076: public void addVacationMessage(long userId, String emailAddress,
077: String vacationMessage) {
078: }
079:
080: public void deleteEmailAddress(long userId) {
081: try {
082: User user = UserLocalServiceUtil.getUserById(userId);
083:
084: String mailUserId = getMailUserId(userId);
085:
086: PostMethod method = getPostMethod();
087:
088: method.addParameter("request", "removealias");
089: method.addParameter("user", mailUserId);
090: method.addParameter("alias", user.getEmailAddress());
091:
092: executeMethod(method);
093: } catch (Exception e) {
094: _log.error(e, e);
095: }
096: }
097:
098: public void deleteUser(long userId) {
099: try {
100: String mailUserId = getMailUserId(userId);
101:
102: PostMethod method = getPostMethod();
103:
104: method.addParameter("request", "terminate");
105: method.addParameter("user", mailUserId);
106:
107: executeMethod(method);
108: } catch (Exception e) {
109: _log.error(e, e);
110: }
111: }
112:
113: public void updateBlocked(long userId, List blocked) {
114: }
115:
116: public void updateEmailAddress(long userId, String emailAddress) {
117: try {
118: deleteEmailAddress(userId);
119:
120: String mailUserId = getMailUserId(userId);
121:
122: PostMethod method = getPostMethod();
123:
124: method.addParameter("request", "modify");
125: method.addParameter("user", mailUserId);
126: method.addParameter("alias[0]", emailAddress);
127:
128: executeMethod(method);
129: } catch (Exception e) {
130: _log.error(e, e);
131: }
132: }
133:
134: public void updatePassword(long userId, String password) {
135: try {
136: String mailUserId = getMailUserId(userId);
137:
138: PostMethod method = getPostMethod();
139:
140: method.addParameter("request", "modify");
141: method.addParameter("user", mailUserId);
142: method.addParameter("password", password);
143:
144: executeMethod(method);
145: } catch (Exception e) {
146: _log.error(e, e);
147: }
148: }
149:
150: protected int executeMethod(PostMethod method) throws Exception {
151: HttpClient client = getHttpClient();
152:
153: int status = client.executeMethod(method);
154:
155: if (_log.isDebugEnabled()) {
156: _log.debug("Posting to URI: " + method.getURI());
157:
158: NameValuePair[] pairs = method.getParameters();
159:
160: if (pairs.length > 0) {
161: StringMaker sm = new StringMaker();
162:
163: sm.append("With parameters:\n");
164:
165: for (int i = 0; i < pairs.length; i++) {
166: sm.append("\t");
167: sm.append(pairs[i]);
168: sm.append("\n");
169: }
170:
171: _log.debug(sm.toString());
172: }
173:
174: _log.debug("Status: " + status);
175: _log.debug("Response body: "
176: + method.getResponseBodyAsString());
177: }
178:
179: return status;
180: }
181:
182: protected String getMailUserId(long userId) throws Exception {
183: User user = UserLocalServiceUtil.getUserById(userId);
184:
185: StringMaker sm = new StringMaker();
186:
187: sm.append(user.getCompanyMx());
188: sm.append(StringPool.PERIOD);
189: sm.append(user.getUserId());
190:
191: String mailUserId = sm.toString();
192:
193: if (_log.isDebugEnabled()) {
194: _log.debug("Mail user id " + mailUserId + " for user id "
195: + userId);
196: }
197:
198: return mailUserId;
199: }
200:
201: protected HttpClient getHttpClient() {
202: return _client;
203: }
204:
205: protected PostMethod getPostMethod() {
206: PostMethod post = new PostMethod(_URL);
207:
208: post.addParameter("PlatformUser", _USERNAME);
209: post.addParameter("PlatformPassword", _PASSWORD);
210:
211: return post;
212: }
213:
214: private static final String _URL = PropsUtil
215: .get(PropsUtil.MAIL_HOOK_FUSEMAIL_URL);
216:
217: private static final String _USERNAME = PropsUtil
218: .get(PropsUtil.MAIL_HOOK_FUSEMAIL_USERNAME);
219:
220: private static final String _PASSWORD = PropsUtil
221: .get(PropsUtil.MAIL_HOOK_FUSEMAIL_PASSWORD);
222:
223: private static final String _ACCOUNT_TYPE = PropsUtil
224: .get(PropsUtil.MAIL_HOOK_FUSEMAIL_ACCOUNT_TYPE);
225:
226: private static final String _GROUP_PARENT = PropsUtil
227: .get(PropsUtil.MAIL_HOOK_FUSEMAIL_GROUP_PARENT);
228:
229: private static Log _log = LogFactory.getLog(FuseMailHook.class);
230:
231: private HttpClient _client;
232:
233: }
|