001: /*
002: * $Id: CommonEvents.java,v 1.2 2003/09/26 17:06:17 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.common;
026:
027: import java.io.IOException;
028: import java.io.PrintWriter;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033:
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036:
037: import org.ofbiz.base.util.Debug;
038: import org.ofbiz.base.util.StringUtil;
039: import org.ofbiz.base.util.UtilCache;
040: import org.ofbiz.base.util.UtilMisc;
041: import org.ofbiz.entity.GenericDelegator;
042: import org.ofbiz.entity.GenericEntityException;
043: import org.ofbiz.entity.GenericValue;
044: import org.ofbiz.security.Security;
045:
046: /**
047: * Common Services
048: *
049: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
050: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
051: * @version $Revision: 1.2 $
052: * @since 2.1
053: */
054: public class CommonEvents {
055:
056: public static final String module = CommonEvents.class.getName();
057:
058: public static UtilCache appletSessions = new UtilCache(
059: "AppletSessions", 0, 600000, true);
060:
061: public static String checkAppletRequest(HttpServletRequest request,
062: HttpServletResponse response) {
063: GenericDelegator delegator = (GenericDelegator) request
064: .getAttribute("delegator");
065: String sessionId = request.getParameter("sessionId");
066: String visitId = request.getParameter("visitId");
067: sessionId = sessionId.trim();
068: visitId = visitId.trim();
069:
070: String responseString = "";
071:
072: GenericValue visit = null;
073: try {
074: visit = delegator.findByPrimaryKey("Visit", UtilMisc.toMap(
075: "visitId", visitId));
076: } catch (GenericEntityException e) {
077: Debug.logError(e, "Cannot Visit Object", module);
078: }
079:
080: if (visit != null
081: && visit.getString("sessionId").equals(sessionId)
082: && appletSessions.containsKey(sessionId)) {
083: Map sessionMap = (Map) appletSessions.get(sessionId);
084: if (sessionMap != null
085: && sessionMap.containsKey("followPage"))
086: responseString = (String) sessionMap
087: .remove("followPage");
088: }
089:
090: try {
091: PrintWriter out = response.getWriter();
092: response.setContentType("text/plain");
093: out.println(responseString);
094: out.close();
095: } catch (IOException e) {
096: Debug.logError(e, "Problems writing servlet output!",
097: module);
098: }
099:
100: return "success";
101: }
102:
103: public static String receiveAppletRequest(
104: HttpServletRequest request, HttpServletResponse response) {
105: GenericDelegator delegator = (GenericDelegator) request
106: .getAttribute("delegator");
107: String sessionId = request.getParameter("sessionId");
108: String visitId = request.getParameter("visitId");
109: sessionId = sessionId.trim();
110: visitId = visitId.trim();
111:
112: String responseString = "ERROR";
113:
114: GenericValue visit = null;
115: try {
116: visit = delegator.findByPrimaryKey("Visit", UtilMisc.toMap(
117: "visitId", visitId));
118: } catch (GenericEntityException e) {
119: Debug.logError(e, "Cannot Visit Object", module);
120: }
121:
122: if (visit.getString("sessionId").equals(sessionId)) {
123: String currentPage = (String) request
124: .getParameter("currentPage");
125: if (appletSessions.containsKey(sessionId)) {
126: Map sessionMap = (Map) appletSessions.get(sessionId);
127: String followers = (String) sessionMap.get("followers");
128: List folList = StringUtil.split(followers, ",");
129: Iterator i = folList.iterator();
130: while (i.hasNext()) {
131: String follower = (String) i.next();
132: Map folSesMap = UtilMisc.toMap("followPage",
133: currentPage);
134: appletSessions.put(follower, folSesMap);
135: }
136: }
137: responseString = "OK";
138: }
139:
140: try {
141: PrintWriter out = response.getWriter();
142: response.setContentType("text/plain");
143: out.println(responseString);
144: out.close();
145: } catch (IOException e) {
146: Debug.logError(e, "Problems writing servlet output!",
147: module);
148: }
149:
150: return "success";
151: }
152:
153: public static String setAppletFollower(HttpServletRequest request,
154: HttpServletResponse response) {
155: Security security = (Security) request.getAttribute("security");
156: GenericValue userLogin = (GenericValue) request.getSession()
157: .getAttribute("userLogin");
158: String visitId = request.getParameter("visitId");
159: if (visitId != null)
160: request.setAttribute("visitId", visitId);
161: if (security.hasPermission("SEND_CONTROL_APPLET", userLogin)) {
162: String followerSessionId = request
163: .getParameter("followerSid");
164: String followSessionId = request.getParameter("followSid");
165: Map follow = (Map) appletSessions.get(followSessionId);
166: if (follow == null)
167: follow = new HashMap();
168: String followerListStr = (String) follow.get("followers");
169: if (followerListStr == null) {
170: followerListStr = followerSessionId;
171: } else {
172: followerListStr = followerListStr + ","
173: + followerSessionId;
174: }
175: appletSessions.put(followSessionId, follow);
176: appletSessions.put(followerSessionId, null);
177: }
178: return "success";
179: }
180:
181: public static String setFollowerPage(HttpServletRequest request,
182: HttpServletResponse response) {
183: Security security = (Security) request.getAttribute("security");
184: GenericValue userLogin = (GenericValue) request.getSession()
185: .getAttribute("userLogin");
186: String visitId = request.getParameter("visitId");
187: if (visitId != null)
188: request.setAttribute("visitId", visitId);
189: if (security.hasPermission("SEND_CONTROL_APPLET", userLogin)) {
190: String followerSessionId = request
191: .getParameter("followerSid");
192: String pageUrl = request.getParameter("pageUrl");
193: Map follow = (Map) appletSessions.get(followerSessionId);
194: if (follow == null)
195: follow = new HashMap();
196: follow.put("followPage", pageUrl);
197: appletSessions.put(followerSessionId, follow);
198: }
199: return "success";
200: }
201: }
|