1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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  }