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