001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.action;
022:
023: import com.liferay.portal.kernel.util.ParamUtil;
024: import com.liferay.portal.model.RequestWait;
025: import com.liferay.portal.model.ReverseAjax;
026: import com.liferay.portal.struts.JSONAction;
027: import com.liferay.portal.util.WebKeys;
028: import com.liferay.portlet.messaging.util.MessagingUtil;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.servlet.http.HttpSession;
033:
034: import org.apache.struts.action.ActionForm;
035: import org.apache.struts.action.ActionMapping;
036:
037: import org.json.JSONObject;
038:
039: /**
040: * <a href="ReverseAjaxAction.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Ming-Gih Lam
043: *
044: */
045: public class ReverseAjaxAction extends JSONAction {
046:
047: public String getJSON(ActionMapping mapping, ActionForm form,
048: HttpServletRequest req, HttpServletResponse res)
049: throws Exception {
050:
051: HttpSession ses = req.getSession();
052:
053: ReverseAjax reverseAjax = (ReverseAjax) ses
054: .getAttribute(WebKeys.REVERSE_AJAX);
055:
056: JSONObject jo = new JSONObject();
057:
058: boolean release = ParamUtil.getBoolean(req, "release");
059:
060: if (release) {
061: release(reverseAjax);
062: } else {
063: try {
064: RequestWait reqWait = reverseAjax.getRequestWait();
065: boolean missedEvents = true;
066:
067: if (reqWait != null) {
068: reverseAjax.setRequestWait(null);
069: reqWait.expire();
070: }
071:
072: if (!reverseAjax.pendingEvents()) {
073: missedEvents = false;
074: reqWait = new RequestWait();
075: reqWait.setSessionId(ses.getId());
076:
077: reverseAjax.setRequestWait(reqWait);
078:
079: reqWait.waitForRequest();
080: }
081:
082: if (missedEvents
083: || (reverseAjax.pendingEvents() && !reqWait
084: .isExpired())) {
085:
086: if (reverseAjax.isPendingChatMessage()) {
087: jo.put("chatMessages", MessagingUtil
088: .getChatMessages(req.getSession()));
089:
090: reverseAjax.setPendingChatMessage(false);
091: }
092: if (reverseAjax.isPendingChatRoster()) {
093: jo.put("chatRoster", MessagingUtil
094: .getRosterEntries(req.getSession()));
095:
096: reverseAjax.setPendingChatRoster(false);
097: }
098:
099: jo.put("status", "success");
100: } else if (reqWait.isTimedOut()) {
101: jo.put("status", "timedOut");
102: } else {
103: jo.put("status", "failure");
104: }
105: } catch (Exception e) {
106: jo.put("status", "failure");
107: } finally {
108: reverseAjax.setRequestWait(null);
109: }
110: }
111:
112: return jo.toString();
113: }
114:
115: protected void release(ReverseAjax reverseAjax) {
116: RequestWait reqWait = reverseAjax.getRequestWait();
117:
118: if (reqWait != null) {
119: reverseAjax.setRequestWait(null);
120:
121: reqWait.expire();
122: }
123: }
124:
125: }
|