001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.common;
019:
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.base.util.StringUtil;
032: import org.ofbiz.base.util.UtilHttp;
033: import org.ofbiz.base.util.UtilMisc;
034: import org.ofbiz.base.util.UtilValidate;
035: import org.ofbiz.base.util.cache.UtilCache;
036: import org.ofbiz.entity.GenericDelegator;
037: import org.ofbiz.entity.GenericEntityException;
038: import org.ofbiz.entity.GenericValue;
039: import org.ofbiz.security.Security;
040:
041: /**
042: * Common Services
043: */
044: public class CommonEvents {
045:
046: public static final String module = CommonEvents.class.getName();
047:
048: public static UtilCache appletSessions = new UtilCache(
049: "AppletSessions", 0, 600000, true);
050:
051: public static String checkAppletRequest(HttpServletRequest request,
052: HttpServletResponse response) {
053: GenericDelegator delegator = (GenericDelegator) request
054: .getAttribute("delegator");
055: String sessionId = request.getParameter("sessionId");
056: String visitId = request.getParameter("visitId");
057: sessionId = sessionId.trim();
058: visitId = visitId.trim();
059:
060: String responseString = "";
061:
062: GenericValue visit = null;
063: try {
064: visit = delegator.findByPrimaryKey("Visit", UtilMisc.toMap(
065: "visitId", visitId));
066: } catch (GenericEntityException e) {
067: Debug.logError(e, "Cannot Visit Object", module);
068: }
069:
070: if (visit != null
071: && visit.getString("sessionId").equals(sessionId)
072: && appletSessions.containsKey(sessionId)) {
073: Map sessionMap = (Map) appletSessions.get(sessionId);
074: if (sessionMap != null
075: && sessionMap.containsKey("followPage"))
076: responseString = (String) sessionMap
077: .remove("followPage");
078: }
079:
080: try {
081: PrintWriter out = response.getWriter();
082: response.setContentType("text/plain");
083: out.println(responseString);
084: out.close();
085: } catch (IOException e) {
086: Debug.logError(e, "Problems writing servlet output!",
087: module);
088: }
089:
090: return "success";
091: }
092:
093: public static String receiveAppletRequest(
094: HttpServletRequest request, HttpServletResponse response) {
095: GenericDelegator delegator = (GenericDelegator) request
096: .getAttribute("delegator");
097: String sessionId = request.getParameter("sessionId");
098: String visitId = request.getParameter("visitId");
099: sessionId = sessionId.trim();
100: visitId = visitId.trim();
101:
102: String responseString = "ERROR";
103:
104: GenericValue visit = null;
105: try {
106: visit = delegator.findByPrimaryKey("Visit", UtilMisc.toMap(
107: "visitId", visitId));
108: } catch (GenericEntityException e) {
109: Debug.logError(e, "Cannot Visit Object", module);
110: }
111:
112: if (visit.getString("sessionId").equals(sessionId)) {
113: String currentPage = request.getParameter("currentPage");
114: if (appletSessions.containsKey(sessionId)) {
115: Map sessionMap = (Map) appletSessions.get(sessionId);
116: String followers = (String) sessionMap.get("followers");
117: List folList = StringUtil.split(followers, ",");
118: Iterator i = folList.iterator();
119: while (i.hasNext()) {
120: String follower = (String) i.next();
121: Map folSesMap = UtilMisc.toMap("followPage",
122: currentPage);
123: appletSessions.put(follower, folSesMap);
124: }
125: }
126: responseString = "OK";
127: }
128:
129: try {
130: PrintWriter out = response.getWriter();
131: response.setContentType("text/plain");
132: out.println(responseString);
133: out.close();
134: } catch (IOException e) {
135: Debug.logError(e, "Problems writing servlet output!",
136: module);
137: }
138:
139: return "success";
140: }
141:
142: public static String setAppletFollower(HttpServletRequest request,
143: HttpServletResponse response) {
144: Security security = (Security) request.getAttribute("security");
145: GenericValue userLogin = (GenericValue) request.getSession()
146: .getAttribute("userLogin");
147: String visitId = request.getParameter("visitId");
148: if (visitId != null)
149: request.setAttribute("visitId", visitId);
150: if (security.hasPermission("SEND_CONTROL_APPLET", userLogin)) {
151: String followerSessionId = request
152: .getParameter("followerSid");
153: String followSessionId = request.getParameter("followSid");
154: Map follow = (Map) appletSessions.get(followSessionId);
155: if (follow == null)
156: follow = new HashMap();
157: String followerListStr = (String) follow.get("followers");
158: if (followerListStr == null) {
159: followerListStr = followerSessionId;
160: } else {
161: followerListStr = followerListStr + ","
162: + followerSessionId;
163: }
164: appletSessions.put(followSessionId, follow);
165: appletSessions.put(followerSessionId, null);
166: }
167: return "success";
168: }
169:
170: public static String setFollowerPage(HttpServletRequest request,
171: HttpServletResponse response) {
172: Security security = (Security) request.getAttribute("security");
173: GenericValue userLogin = (GenericValue) request.getSession()
174: .getAttribute("userLogin");
175: String visitId = request.getParameter("visitId");
176: if (visitId != null)
177: request.setAttribute("visitId", visitId);
178: if (security.hasPermission("SEND_CONTROL_APPLET", userLogin)) {
179: String followerSessionId = request
180: .getParameter("followerSid");
181: String pageUrl = request.getParameter("pageUrl");
182: Map follow = (Map) appletSessions.get(followerSessionId);
183: if (follow == null)
184: follow = new HashMap();
185: follow.put("followPage", pageUrl);
186: appletSessions.put(followerSessionId, follow);
187: }
188: return "success";
189: }
190:
191: /** Simple event to set the users per-session locale setting. The user's locale
192: * setting should be passed as a "newLocale" request parameter. */
193: public static String setSessionLocale(HttpServletRequest request,
194: HttpServletResponse response) {
195: String localeString = request.getParameter("newLocale");
196: if (UtilValidate.isNotEmpty(localeString)) {
197: UtilHttp.setLocale(request, localeString);
198:
199: // update the UserLogin object
200: GenericValue userLogin = (GenericValue) request
201: .getSession().getAttribute("userLogin");
202: if (userLogin == null) {
203: userLogin = (GenericValue) request.getSession()
204: .getAttribute("autoUserLogin");
205: }
206:
207: if (userLogin != null) {
208: GenericValue ulUpdate = GenericValue.create(userLogin);
209: ulUpdate.set("lastLocale", localeString);
210: try {
211: ulUpdate.store();
212: userLogin.refreshFromCache();
213: } catch (GenericEntityException e) {
214: Debug.logWarning(e, module);
215: }
216: }
217: }
218: return "success";
219: }
220:
221: /** Simple event to set the users per-session currency uom value */
222: public static String setSessionCurrencyUom(
223: HttpServletRequest request, HttpServletResponse response) {
224: String currencyUom = request.getParameter("currencyUom");
225: if (UtilValidate.isNotEmpty(currencyUom)) {
226: // update the session
227: UtilHttp.setCurrencyUom(request.getSession(), currencyUom);
228:
229: // update the UserLogin object
230: GenericValue userLogin = (GenericValue) request
231: .getSession().getAttribute("userLogin");
232: if (userLogin == null) {
233: userLogin = (GenericValue) request.getSession()
234: .getAttribute("autoUserLogin");
235: }
236:
237: if (userLogin != null) {
238: GenericValue ulUpdate = GenericValue.create(userLogin);
239: ulUpdate.set("lastCurrencyUom", currencyUom);
240: try {
241: ulUpdate.store();
242: userLogin.refreshFromCache();
243: } catch (GenericEntityException e) {
244: Debug.logWarning(e, module);
245: }
246: }
247: }
248: return "success";
249: }
250: }
|