1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.sms;
16  
17  import com.liferay.mail.service.MailServiceUtil;
18  import com.liferay.portal.kernel.mail.MailMessage;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.servlet.SessionMessages;
21  import com.liferay.portal.kernel.util.Constants;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.util.bridges.mvc.MVCPortlet;
28  
29  import javax.mail.internet.InternetAddress;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletException;
34  
35  /**
36   * <a href="SMSPortlet.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class SMSPortlet extends MVCPortlet {
41  
42      public void processAction(
43              ActionRequest actionRequest, ActionResponse actionResponse)
44          throws PortletException {
45  
46          try {
47              String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
48  
49              if (cmd.equals(Constants.SEND)) {
50                  String to = actionRequest.getParameter("to");
51                  String subject = ParamUtil.getString(actionRequest, "subject");
52                  String message = ParamUtil.getString(actionRequest, "message");
53  
54                  if (!Validator.isEmailAddress(to)) {
55                      SessionErrors.add(actionRequest, "to");
56                  }
57                  else if (message.length() > 500) {
58                      SessionErrors.add(actionRequest, "message");
59                  }
60  
61                  if (SessionErrors.isEmpty(actionRequest)) {
62                      User user = PortalUtil.getUser(actionRequest);
63  
64                      MailServiceUtil.sendEmail(new MailMessage(
65                          new InternetAddress(
66                              user.getEmailAddress(), user.getFullName()),
67                          new InternetAddress(to), subject, message, false));
68  
69                      actionResponse.setRenderParameter("to", StringPool.BLANK);
70                      actionResponse.setRenderParameter(
71                          "subject", StringPool.BLANK);
72                      actionResponse.setRenderParameter(
73                          "message", StringPool.BLANK);
74  
75                      SessionMessages.add(
76                          actionRequest,
77                          getPortletConfig().getPortletName() + ".send", to);
78                  }
79                  else {
80                      actionResponse.setRenderParameter("to", to);
81                      actionResponse.setRenderParameter("subject", subject);
82                      actionResponse.setRenderParameter("message", message);
83                  }
84              }
85          }
86          catch (Exception e) {
87              throw new PortletException(e);
88          }
89      }
90  
91  }