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.portal.action;
24  
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.model.RequestWait;
27  import com.liferay.portal.model.ReverseAjax;
28  import com.liferay.portal.struts.JSONAction;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.messaging.util.MessagingUtil;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.http.HttpSession;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionMapping;
38  
39  import org.json.JSONObject;
40  
41  /**
42   * <a href="ReverseAjaxAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Ming-Gih Lam
45   *
46   */
47  public class ReverseAjaxAction extends JSONAction {
48  
49      public String getJSON(
50              ActionMapping mapping, ActionForm form, HttpServletRequest req,
51              HttpServletResponse res)
52          throws Exception {
53  
54          HttpSession ses = req.getSession();
55  
56          ReverseAjax reverseAjax =
57              (ReverseAjax)ses.getAttribute(WebKeys.REVERSE_AJAX);
58  
59          JSONObject jo = new JSONObject();
60  
61          boolean release = ParamUtil.getBoolean(req, "release");
62  
63          if (release) {
64              release(reverseAjax);
65          }
66          else {
67              try {
68                  RequestWait reqWait = reverseAjax.getRequestWait();
69                  boolean missedEvents = true;
70  
71                  if (reqWait != null) {
72                      reverseAjax.setRequestWait(null);
73                      reqWait.expire();
74                  }
75  
76                  if (!reverseAjax.pendingEvents()) {
77                      missedEvents = false;
78                      reqWait = new RequestWait();
79                      reqWait.setSessionId(ses.getId());
80  
81                      reverseAjax.setRequestWait(reqWait);
82  
83                      reqWait.waitForRequest();
84                  }
85  
86                  if (missedEvents || (reverseAjax.pendingEvents() &&
87                      !reqWait.isExpired())) {
88  
89                      if (reverseAjax.isPendingChatMessage()) {
90                          jo.put("chatMessages",
91                              MessagingUtil.getChatMessages(req.getSession()));
92  
93                          reverseAjax.setPendingChatMessage(false);
94                      }
95                      if (reverseAjax.isPendingChatRoster()) {
96                          jo.put("chatRoster",
97                              MessagingUtil.getRosterEntries(req.getSession()));
98  
99                          reverseAjax.setPendingChatRoster(false);
100                     }
101 
102                     jo.put("status", "success");
103                 }
104                 else if (reqWait.isTimedOut()) {
105                     jo.put("status", "timedOut");
106                 }
107                 else {
108                     jo.put("status", "failure");
109                 }
110             }
111             catch (Exception e) {
112                 jo.put("status", "failure");
113             }
114             finally {
115                 reverseAjax.setRequestWait(null);
116             }
117         }
118 
119         return jo.toString();
120     }
121 
122     protected void release(ReverseAjax reverseAjax) {
123         RequestWait reqWait = reverseAjax.getRequestWait();
124 
125         if (reqWait != null) {
126             reverseAjax.setRequestWait(null);
127 
128             reqWait.expire();
129         }
130     }
131 
132 }