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