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