1
22
23 package com.liferay.mail.util;
24
25 import com.liferay.portal.kernel.util.StringMaker;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.model.User;
28 import com.liferay.portal.service.UserLocalServiceUtil;
29 import com.liferay.portal.util.PropsUtil;
30
31 import java.util.List;
32
33 import org.apache.commons.httpclient.HttpClient;
34 import org.apache.commons.httpclient.NameValuePair;
35 import org.apache.commons.httpclient.methods.PostMethod;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39
45 public class FuseMailHook implements Hook {
46
47 public FuseMailHook() {
48 _client = new HttpClient();
49 }
50
51 public void addForward(
52 long userId, List filters, List emailAddresses, boolean leaveCopy) {
53 }
54
55 public void addUser(
56 long userId, String password, String firstName, String middleName,
57 String lastName, String emailAddress) {
58
59 try {
60 String mailUserId = getMailUserId(userId);
61
62 PostMethod method = getPostMethod();
63
64 method.addParameter("request", "order");
65 method.addParameter("user", mailUserId);
66 method.addParameter("password", password);
67 method.addParameter("first_name", firstName);
68 method.addParameter("last_name", lastName);
69 method.addParameter("account_type", _ACCOUNT_TYPE);
70 method.addParameter("group_parent", _GROUP_PARENT);
71 method.addParameter("alias[0]", emailAddress);
72
73 executeMethod(method);
74 }
75 catch (Exception e) {
76 _log.error(e, e);
77 }
78 }
79
80 public void addVacationMessage(
81 long userId, String emailAddress, String vacationMessage) {
82 }
83
84 public void deleteEmailAddress(long userId) {
85 try {
86 User user = UserLocalServiceUtil.getUserById(userId);
87
88 String mailUserId = getMailUserId(userId);
89
90 PostMethod method = getPostMethod();
91
92 method.addParameter("request", "removealias");
93 method.addParameter("user", mailUserId);
94 method.addParameter("alias", user.getEmailAddress());
95
96 executeMethod(method);
97 }
98 catch (Exception e) {
99 _log.error(e, e);
100 }
101 }
102
103 public void deleteUser(long userId) {
104 try {
105 String mailUserId = getMailUserId(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 public void updateBlocked(long userId, List blocked) {
120 }
121
122 public void updateEmailAddress(long userId, String emailAddress) {
123 try {
124 deleteEmailAddress(userId);
125
126 String mailUserId = getMailUserId(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 userId, String password) {
142 try {
143 String mailUserId = getMailUserId(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 StringMaker sm = new StringMaker();
170
171 sm.append("With parameters:\n");
172
173 for (int i = 0; i < pairs.length; i++) {
174 sm.append("\t");
175 sm.append(pairs[i]);
176 sm.append("\n");
177 }
178
179 _log.debug(sm.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 userId) throws Exception {
190 User user = UserLocalServiceUtil.getUserById(userId);
191
192 StringMaker sm = new StringMaker();
193
194 sm.append(user.getCompanyMx());
195 sm.append(StringPool.PERIOD);
196 sm.append(user.getUserId());
197
198 String mailUserId = sm.toString();
199
200 if (_log.isDebugEnabled()) {
201 _log.debug("Mail user id " + mailUserId + " for user id " + userId);
202 }
203
204 return mailUserId;
205 }
206
207 protected HttpClient getHttpClient() {
208 return _client;
209 }
210
211 protected PostMethod getPostMethod() {
212 PostMethod post = new PostMethod(_URL);
213
214 post.addParameter("PlatformUser", _USERNAME);
215 post.addParameter("PlatformPassword", _PASSWORD);
216
217 return post;
218 }
219
220 private static final String _URL = PropsUtil.get(
221 PropsUtil.MAIL_HOOK_FUSEMAIL_URL);
222
223 private static final String _USERNAME = PropsUtil.get(
224 PropsUtil.MAIL_HOOK_FUSEMAIL_USERNAME);
225
226 private static final String _PASSWORD = PropsUtil.get(
227 PropsUtil.MAIL_HOOK_FUSEMAIL_PASSWORD);
228
229 private static final String _ACCOUNT_TYPE = PropsUtil.get(
230 PropsUtil.MAIL_HOOK_FUSEMAIL_ACCOUNT_TYPE);
231
232 private static final String _GROUP_PARENT = PropsUtil.get(
233 PropsUtil.MAIL_HOOK_FUSEMAIL_GROUP_PARENT);
234
235 private static Log _log = LogFactory.getLog(FuseMailHook.class);
236
237 private HttpClient _client;
238
239 }