1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.mvc.MVCPortlet;
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  public class SMSPortlet extends MVCPortlet {
49  
50      public void processAction(
51              ActionRequest actionRequest, ActionResponse actionResponse)
52          throws PortletException {
53  
54          try {
55              String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
56  
57              if (cmd.equals(Constants.SEND)) {
58                  String to = actionRequest.getParameter("to");
59                  String subject = ParamUtil.getString(actionRequest, "subject");
60                  String message = ParamUtil.getString(actionRequest, "message");
61  
62                  if (!Validator.isEmailAddress(to)) {
63                      SessionErrors.add(actionRequest, "to");
64                  }
65                  else if (message.length() > 500) {
66                      SessionErrors.add(actionRequest, "message");
67                  }
68  
69                  if (SessionErrors.isEmpty(actionRequest)) {
70                      User user = PortalUtil.getUser(actionRequest);
71  
72                      MailServiceUtil.sendEmail(new MailMessage(
73                          new InternetAddress(
74                              user.getEmailAddress(), user.getFullName()),
75                          new InternetAddress(to), subject, message, false));
76  
77                      actionResponse.setRenderParameter("to", StringPool.BLANK);
78                      actionResponse.setRenderParameter(
79                          "subject", StringPool.BLANK);
80                      actionResponse.setRenderParameter(
81                          "message", StringPool.BLANK);
82  
83                      SessionMessages.add(
84                          actionRequest,
85                          getPortletConfig().getPortletName() + ".send", to);
86                  }
87                  else {
88                      actionResponse.setRenderParameter("to", to);
89                      actionResponse.setRenderParameter("subject", subject);
90                      actionResponse.setRenderParameter("message", message);
91                  }
92              }
93          }
94          catch (Exception e) {
95              throw new PortletException(e);
96          }
97      }
98  
99  }