001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.mail.util;
016    
017    import com.liferay.mail.model.Filter;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Company;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.CompanyLocalServiceUtil;
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    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class FuseMailHook implements Hook {
039    
040            public FuseMailHook() {
041                    _client = new HttpClient();
042            }
043    
044            public void addForward(
045                    long companyId, long userId, List<Filter> filters,
046                    List<String> emailAddresses, boolean leaveCopy) {
047            }
048    
049            public void addUser(
050                    long companyId, long userId, String password, String firstName,
051                    String middleName, String lastName, String emailAddress) {
052    
053                    try {
054                            String mailUserId = getMailUserId(companyId, userId);
055    
056                            PostMethod method = getPostMethod();
057    
058                            method.addParameter("request", "order");
059                            method.addParameter("user", mailUserId);
060                            method.addParameter("password", password);
061                            method.addParameter("first_name", firstName);
062                            method.addParameter("last_name", lastName);
063                            method.addParameter("account_type", _ACCOUNT_TYPE);
064                            method.addParameter("group_parent", _GROUP_PARENT);
065                            method.addParameter("alias[0]", emailAddress);
066    
067                            executeMethod(method);
068                    }
069                    catch (Exception e) {
070                            _log.error(e, e);
071                    }
072            }
073    
074            public void addVacationMessage(
075                    long companyId, long userId, String emailAddress,
076                    String vacationMessage) {
077            }
078    
079            public void deleteEmailAddress(long companyId, long userId) {
080                    try {
081                            User user = UserLocalServiceUtil.getUserById(userId);
082    
083                            String mailUserId = getMailUserId(companyId, userId);
084    
085                            PostMethod method = getPostMethod();
086    
087                            method.addParameter("request", "removealias");
088                            method.addParameter("user", mailUserId);
089                            method.addParameter("alias", user.getEmailAddress());
090    
091                            executeMethod(method);
092                    }
093                    catch (Exception e) {
094                            _log.error(e, e);
095                    }
096            }
097    
098            public void deleteUser(long companyId, long userId) {
099                    try {
100                            String mailUserId = getMailUserId(companyId, userId);
101    
102                            PostMethod method = getPostMethod();
103    
104                            method.addParameter("request", "terminate");
105                            method.addParameter("user", mailUserId);
106    
107                            executeMethod(method);
108                    }
109                    catch (Exception e) {
110                            _log.error(e, e);
111                    }
112            }
113    
114            public void updateBlocked(
115                    long companyId, long userId, List<String> blocked) {
116            }
117    
118            public void updateEmailAddress(
119                    long companyId, long userId, String emailAddress) {
120    
121                    try {
122                            deleteEmailAddress(companyId, userId);
123    
124                            String mailUserId = getMailUserId(companyId, userId);
125    
126                            PostMethod method = getPostMethod();
127    
128                            method.addParameter("request", "modify");
129                            method.addParameter("user", mailUserId);
130                            method.addParameter("alias[0]", emailAddress);
131    
132                            executeMethod(method);
133                    }
134                    catch (Exception e) {
135                            _log.error(e, e);
136                    }
137            }
138    
139            public void updatePassword(long companyId, long userId, String password) {
140                    try {
141                            String mailUserId = getMailUserId(companyId, userId);
142    
143                            PostMethod method = getPostMethod();
144    
145                            method.addParameter("request", "modify");
146                            method.addParameter("user", mailUserId);
147                            method.addParameter("password", password);
148    
149                            executeMethod(method);
150                    }
151                    catch (Exception e) {
152                            _log.error(e, e);
153                    }
154            }
155    
156            protected int executeMethod(PostMethod method) throws Exception {
157                    HttpClient client = getHttpClient();
158    
159                    int status = client.executeMethod(method);
160    
161                    if (_log.isDebugEnabled()) {
162                            _log.debug("Posting to URI: " + method.getURI());
163    
164                            NameValuePair[] pairs = method.getParameters();
165    
166                            if (pairs.length > 0) {
167                                    StringBundler sb = new StringBundler(pairs.length * 3 + 1);
168    
169                                    sb.append("With parameters:\n");
170    
171                                    for (int i = 0; i < pairs.length; i++) {
172                                            sb.append("\t");
173                                            sb.append(pairs[i]);
174                                            sb.append("\n");
175                                    }
176    
177                                    _log.debug(sb.toString());
178                            }
179    
180                            _log.debug("Status: " + status);
181                            _log.debug("Response body: " + method.getResponseBodyAsString());
182                    }
183    
184                    return status;
185            }
186    
187            protected String getMailUserId(long companyId, long userId)
188                    throws Exception {
189    
190                    Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
191    
192                    String mailUserId = company.getMx().concat(StringPool.PERIOD).concat(
193                            String.valueOf(userId));
194    
195                    if (_log.isDebugEnabled()) {
196                            _log.debug("Mail user id " + mailUserId + " for user id " + userId);
197                    }
198    
199                    return mailUserId;
200            }
201    
202            protected HttpClient getHttpClient() {
203                    return _client;
204            }
205    
206            protected PostMethod getPostMethod() {
207                    PostMethod post = new PostMethod(_URL);
208    
209                    post.addParameter("PlatformUser", _USERNAME);
210                    post.addParameter("PlatformPassword", _PASSWORD);
211    
212                    return post;
213            }
214    
215            private static final String _URL = PropsUtil.get(
216                    PropsKeys.MAIL_HOOK_FUSEMAIL_URL);
217    
218            private static final String _USERNAME = PropsUtil.get(
219                    PropsKeys.MAIL_HOOK_FUSEMAIL_USERNAME);
220    
221            private static final String _PASSWORD = PropsUtil.get(
222                    PropsKeys.MAIL_HOOK_FUSEMAIL_PASSWORD);
223    
224            private static final String _ACCOUNT_TYPE = PropsUtil.get(
225                    PropsKeys.MAIL_HOOK_FUSEMAIL_ACCOUNT_TYPE);
226    
227            private static final String _GROUP_PARENT = PropsUtil.get(
228                    PropsKeys.MAIL_HOOK_FUSEMAIL_GROUP_PARENT);
229    
230            private static Log _log = LogFactoryUtil.getLog(FuseMailHook.class);
231    
232            private HttpClient _client;
233    
234    }