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