1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.mail.util;
21  
22  import com.liferay.mail.model.Filter;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.model.Company;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.service.CompanyLocalServiceUtil;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.portal.util.PropsKeys;
31  import com.liferay.portal.util.PropsUtil;
32  
33  import java.util.List;
34  
35  import org.apache.commons.httpclient.HttpClient;
36  import org.apache.commons.httpclient.NameValuePair;
37  import org.apache.commons.httpclient.methods.PostMethod;
38  
39  /**
40   * <a href="FuseMailHook.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class FuseMailHook implements Hook {
46  
47      public FuseMailHook() {
48          _client = new HttpClient();
49      }
50  
51      public void addForward(
52          long companyId, long userId, List<Filter> filters,
53          List<String> emailAddresses, boolean leaveCopy) {
54      }
55  
56      public void addUser(
57          long companyId, long userId, String password, String firstName,
58          String middleName, String lastName, String emailAddress) {
59  
60          try {
61              String mailUserId = getMailUserId(companyId, userId);
62  
63              PostMethod method = getPostMethod();
64  
65              method.addParameter("request", "order");
66              method.addParameter("user", mailUserId);
67              method.addParameter("password", password);
68              method.addParameter("first_name", firstName);
69              method.addParameter("last_name", lastName);
70              method.addParameter("account_type", _ACCOUNT_TYPE);
71              method.addParameter("group_parent", _GROUP_PARENT);
72              method.addParameter("alias[0]", emailAddress);
73  
74              executeMethod(method);
75          }
76          catch (Exception e) {
77              _log.error(e, e);
78          }
79      }
80  
81      public void addVacationMessage(
82          long companyId, long userId, String emailAddress,
83          String vacationMessage) {
84      }
85  
86      public void deleteEmailAddress(long companyId, long userId) {
87          try {
88              User user = UserLocalServiceUtil.getUserById(userId);
89  
90              String mailUserId = getMailUserId(companyId, 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 companyId, long userId) {
106         try {
107             String mailUserId = getMailUserId(companyId, userId);
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(
122         long companyId, long userId, List<String> blocked) {
123     }
124 
125     public void updateEmailAddress(
126         long companyId, long userId, String emailAddress) {
127 
128         try {
129             deleteEmailAddress(companyId, userId);
130 
131             String mailUserId = getMailUserId(companyId, userId);
132 
133             PostMethod method = getPostMethod();
134 
135             method.addParameter("request", "modify");
136             method.addParameter("user", mailUserId);
137             method.addParameter("alias[0]", emailAddress);
138 
139             executeMethod(method);
140         }
141         catch (Exception e) {
142             _log.error(e, e);
143         }
144     }
145 
146     public void updatePassword(long companyId, long userId, String password) {
147         try {
148             String mailUserId = getMailUserId(companyId, userId);
149 
150             PostMethod method = getPostMethod();
151 
152             method.addParameter("request", "modify");
153             method.addParameter("user", mailUserId);
154             method.addParameter("password", password);
155 
156             executeMethod(method);
157         }
158         catch (Exception e) {
159             _log.error(e, e);
160         }
161     }
162 
163     protected int executeMethod(PostMethod method) throws Exception {
164         HttpClient client = getHttpClient();
165 
166         int status = client.executeMethod(method);
167 
168         if (_log.isDebugEnabled()) {
169             _log.debug("Posting to URI: " + method.getURI());
170 
171             NameValuePair[] pairs = method.getParameters();
172 
173             if (pairs.length > 0) {
174                 StringBuilder sb = new StringBuilder();
175 
176                 sb.append("With parameters:\n");
177 
178                 for (int i = 0; i < pairs.length; i++) {
179                     sb.append("\t");
180                     sb.append(pairs[i]);
181                     sb.append("\n");
182                 }
183 
184                 _log.debug(sb.toString());
185             }
186 
187             _log.debug("Status: " + status);
188             _log.debug("Response body: " + method.getResponseBodyAsString());
189         }
190 
191         return status;
192     }
193 
194     protected String getMailUserId(long companyId, long userId)
195         throws Exception {
196 
197         Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
198 
199         StringBuilder sb = new StringBuilder();
200 
201         sb.append(company.getMx());
202         sb.append(StringPool.PERIOD);
203         sb.append(userId);
204 
205         String mailUserId = sb.toString();
206 
207         if (_log.isDebugEnabled()) {
208             _log.debug("Mail user id " + mailUserId + " for user id " + userId);
209         }
210 
211         return mailUserId;
212     }
213 
214     protected HttpClient getHttpClient() {
215         return _client;
216     }
217 
218     protected PostMethod getPostMethod() {
219         PostMethod post = new PostMethod(_URL);
220 
221         post.addParameter("PlatformUser", _USERNAME);
222         post.addParameter("PlatformPassword", _PASSWORD);
223 
224         return post;
225     }
226 
227     private static final String _URL = PropsUtil.get(
228         PropsKeys.MAIL_HOOK_FUSEMAIL_URL);
229 
230     private static final String _USERNAME = PropsUtil.get(
231         PropsKeys.MAIL_HOOK_FUSEMAIL_USERNAME);
232 
233     private static final String _PASSWORD = PropsUtil.get(
234         PropsKeys.MAIL_HOOK_FUSEMAIL_PASSWORD);
235 
236     private static final String _ACCOUNT_TYPE = PropsUtil.get(
237         PropsKeys.MAIL_HOOK_FUSEMAIL_ACCOUNT_TYPE);
238 
239     private static final String _GROUP_PARENT = PropsUtil.get(
240         PropsKeys.MAIL_HOOK_FUSEMAIL_GROUP_PARENT);
241 
242     private static Log _log = LogFactoryUtil.getLog(FuseMailHook.class);
243 
244     private HttpClient _client;
245 
246 }