1
22
23 package com.liferay.mail.util;
24
25 import com.liferay.mail.model.Filter;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.model.Company;
30 import com.liferay.portal.model.User;
31 import com.liferay.portal.service.CompanyLocalServiceUtil;
32 import com.liferay.portal.service.UserLocalServiceUtil;
33 import com.liferay.portal.util.PropsKeys;
34 import com.liferay.portal.util.PropsUtil;
35
36 import java.util.List;
37
38 import org.apache.commons.httpclient.HttpClient;
39 import org.apache.commons.httpclient.NameValuePair;
40 import org.apache.commons.httpclient.methods.PostMethod;
41
42
47 public class FuseMailHook implements Hook {
48
49 public FuseMailHook() {
50 _client = new HttpClient();
51 }
52
53 public void addForward(
54 long companyId, long userId, List<Filter> filters,
55 List<String> emailAddresses, boolean leaveCopy) {
56 }
57
58 public void addUser(
59 long companyId, long userId, String password, String firstName,
60 String middleName, String lastName, String emailAddress) {
61
62 try {
63 String mailUserId = getMailUserId(companyId, userId);
64
65 PostMethod method = getPostMethod();
66
67 method.addParameter("request", "order");
68 method.addParameter("user", mailUserId);
69 method.addParameter("password", password);
70 method.addParameter("first_name", firstName);
71 method.addParameter("last_name", lastName);
72 method.addParameter("account_type", _ACCOUNT_TYPE);
73 method.addParameter("group_parent", _GROUP_PARENT);
74 method.addParameter("alias[0]", emailAddress);
75
76 executeMethod(method);
77 }
78 catch (Exception e) {
79 _log.error(e, e);
80 }
81 }
82
83 public void addVacationMessage(
84 long companyId, long userId, String emailAddress,
85 String vacationMessage) {
86 }
87
88 public void deleteEmailAddress(long companyId, long userId) {
89 try {
90 User user = UserLocalServiceUtil.getUserById(userId);
91
92 String mailUserId = getMailUserId(companyId, userId);
93
94 PostMethod method = getPostMethod();
95
96 method.addParameter("request", "removealias");
97 method.addParameter("user", mailUserId);
98 method.addParameter("alias", user.getEmailAddress());
99
100 executeMethod(method);
101 }
102 catch (Exception e) {
103 _log.error(e, e);
104 }
105 }
106
107 public void deleteUser(long companyId, long userId) {
108 try {
109 String mailUserId = getMailUserId(companyId, userId);
110
111 PostMethod method = getPostMethod();
112
113 method.addParameter("request", "terminate");
114 method.addParameter("user", mailUserId);
115
116 executeMethod(method);
117 }
118 catch (Exception e) {
119 _log.error(e, e);
120 }
121 }
122
123 public void updateBlocked(
124 long companyId, long userId, List<String> blocked) {
125 }
126
127 public void updateEmailAddress(
128 long companyId, long userId, String emailAddress) {
129
130 try {
131 deleteEmailAddress(companyId, userId);
132
133 String mailUserId = getMailUserId(companyId, userId);
134
135 PostMethod method = getPostMethod();
136
137 method.addParameter("request", "modify");
138 method.addParameter("user", mailUserId);
139 method.addParameter("alias[0]", emailAddress);
140
141 executeMethod(method);
142 }
143 catch (Exception e) {
144 _log.error(e, e);
145 }
146 }
147
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 _log.debug("Posting to URI: " + method.getURI());
172
173 NameValuePair[] pairs = method.getParameters();
174
175 if (pairs.length > 0) {
176 StringBuilder sb = new StringBuilder();
177
178 sb.append("With parameters:\n");
179
180 for (int i = 0; i < pairs.length; i++) {
181 sb.append("\t");
182 sb.append(pairs[i]);
183 sb.append("\n");
184 }
185
186 _log.debug(sb.toString());
187 }
188
189 _log.debug("Status: " + status);
190 _log.debug("Response body: " + method.getResponseBodyAsString());
191 }
192
193 return status;
194 }
195
196 protected String getMailUserId(long companyId, long userId)
197 throws Exception {
198
199 Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
200
201 StringBuilder sb = new StringBuilder();
202
203 sb.append(company.getMx());
204 sb.append(StringPool.PERIOD);
205 sb.append(userId);
206
207 String mailUserId = sb.toString();
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 HttpClient getHttpClient() {
217 return _client;
218 }
219
220 protected PostMethod getPostMethod() {
221 PostMethod post = new PostMethod(_URL);
222
223 post.addParameter("PlatformUser", _USERNAME);
224 post.addParameter("PlatformPassword", _PASSWORD);
225
226 return post;
227 }
228
229 private static final String _URL = PropsUtil.get(
230 PropsKeys.MAIL_HOOK_FUSEMAIL_URL);
231
232 private static final String _USERNAME = PropsUtil.get(
233 PropsKeys.MAIL_HOOK_FUSEMAIL_USERNAME);
234
235 private static final String _PASSWORD = PropsUtil.get(
236 PropsKeys.MAIL_HOOK_FUSEMAIL_PASSWORD);
237
238 private static final String _ACCOUNT_TYPE = PropsUtil.get(
239 PropsKeys.MAIL_HOOK_FUSEMAIL_ACCOUNT_TYPE);
240
241 private static final String _GROUP_PARENT = PropsUtil.get(
242 PropsKeys.MAIL_HOOK_FUSEMAIL_GROUP_PARENT);
243
244 private static Log _log = LogFactoryUtil.getLog(FuseMailHook.class);
245
246 private HttpClient _client;
247
248 }