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