1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.sms;
24  
25  import com.liferay.mail.service.MailServiceUtil;
26  import com.liferay.portal.kernel.mail.MailMessage;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.servlet.SessionMessages;
29  import com.liferay.portal.kernel.util.Constants;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.util.bridges.jsp.JSPPortlet;
36  
37  import javax.mail.internet.InternetAddress;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletException;
42  
43  /**
44   * <a href="SMSPortlet.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class SMSPortlet extends JSPPortlet {
50  
51      public void processAction(
52              ActionRequest actionRequest, ActionResponse actionResponse)
53          throws PortletException {
54  
55          try {
56              String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
57  
58              if (cmd.equals(Constants.SEND)) {
59                  String to = actionRequest.getParameter("to");
60                  String subject = ParamUtil.getString(actionRequest, "subject");
61                  String message = ParamUtil.getString(actionRequest, "message");
62  
63                  if (!Validator.isEmailAddress(to)) {
64  
65                      SessionErrors.add(actionRequest, "to");
66                  }
67  
68                  if (SessionErrors.isEmpty(actionRequest)) {
69                      User user = PortalUtil.getUser(actionRequest);
70  
71                      MailServiceUtil.sendEmail(new MailMessage(
72                          new InternetAddress(
73                              user.getEmailAddress(), user.getFullName()),
74                          new InternetAddress(to), subject, message));
75  
76                      actionResponse.setRenderParameter("to", StringPool.BLANK);
77                      actionResponse.setRenderParameter(
78                          "subject", StringPool.BLANK);
79                      actionResponse.setRenderParameter(
80                          "message", StringPool.BLANK);
81  
82                      SessionMessages.add(
83                          actionRequest,
84                          getPortletConfig().getPortletName() + ".send", to);
85                  }
86                  else {
87                      actionResponse.setRenderParameter("to", to);
88                      actionResponse.setRenderParameter("subject", subject);
89                      actionResponse.setRenderParameter("message", message);
90                  }
91              }
92          }
93          catch (Exception e) {
94              throw new PortletException(e);
95          }
96      }
97  
98  }