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.portlet.sms;
21  
22  import com.liferay.mail.service.MailServiceUtil;
23  import com.liferay.portal.kernel.mail.MailMessage;
24  import com.liferay.portal.kernel.servlet.SessionErrors;
25  import com.liferay.portal.kernel.servlet.SessionMessages;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.util.bridges.mvc.MVCPortlet;
33  
34  import javax.mail.internet.InternetAddress;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletException;
39  
40  /**
41   * <a href="SMSPortlet.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class SMSPortlet extends MVCPortlet {
47  
48      public void processAction(
49              ActionRequest actionRequest, ActionResponse actionResponse)
50          throws PortletException {
51  
52          try {
53              String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
54  
55              if (cmd.equals(Constants.SEND)) {
56                  String to = actionRequest.getParameter("to");
57                  String subject = ParamUtil.getString(actionRequest, "subject");
58                  String message = ParamUtil.getString(actionRequest, "message");
59  
60                  if (!Validator.isEmailAddress(to)) {
61                      SessionErrors.add(actionRequest, "to");
62                  }
63                  else if (message.length() > 500) {
64                      SessionErrors.add(actionRequest, "message");
65                  }
66  
67                  if (SessionErrors.isEmpty(actionRequest)) {
68                      User user = PortalUtil.getUser(actionRequest);
69  
70                      MailServiceUtil.sendEmail(new MailMessage(
71                          new InternetAddress(
72                              user.getEmailAddress(), user.getFullName()),
73                          new InternetAddress(to), subject, message, false));
74  
75                      actionResponse.setRenderParameter("to", StringPool.BLANK);
76                      actionResponse.setRenderParameter(
77                          "subject", StringPool.BLANK);
78                      actionResponse.setRenderParameter(
79                          "message", StringPool.BLANK);
80  
81                      SessionMessages.add(
82                          actionRequest,
83                          getPortletConfig().getPortletName() + ".send", to);
84                  }
85                  else {
86                      actionResponse.setRenderParameter("to", to);
87                      actionResponse.setRenderParameter("subject", subject);
88                      actionResponse.setRenderParameter("message", message);
89                  }
90              }
91          }
92          catch (Exception e) {
93              throw new PortletException(e);
94          }
95      }
96  
97  }