1 25 package org.ofbiz.common; 26 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import org.ofbiz.base.util.Debug; 38 import org.ofbiz.base.util.StringUtil; 39 import org.ofbiz.base.util.UtilHttp; 40 import org.ofbiz.base.util.UtilMisc; 41 import org.ofbiz.base.util.UtilValidate; 42 import org.ofbiz.base.util.cache.UtilCache; 43 import org.ofbiz.entity.GenericDelegator; 44 import org.ofbiz.entity.GenericEntityException; 45 import org.ofbiz.entity.GenericValue; 46 import org.ofbiz.security.Security; 47 48 56 public class CommonEvents { 57 58 public static final String module = CommonEvents.class.getName(); 59 60 public static UtilCache appletSessions = new UtilCache("AppletSessions", 0, 600000, true); 61 62 public static String checkAppletRequest(HttpServletRequest request, HttpServletResponse response) { 63 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 64 String sessionId = request.getParameter("sessionId"); 65 String visitId = request.getParameter("visitId"); 66 sessionId = sessionId.trim(); 67 visitId = visitId.trim(); 68 69 String responseString = ""; 70 71 GenericValue visit = null; 72 try { 73 visit = delegator.findByPrimaryKey("Visit", UtilMisc.toMap("visitId", visitId)); 74 } catch (GenericEntityException e) { 75 Debug.logError(e, "Cannot Visit Object", module); 76 } 77 78 if (visit != null && visit.getString("sessionId").equals(sessionId) && appletSessions.containsKey(sessionId)) { 79 Map sessionMap = (Map ) appletSessions.get(sessionId); 80 if (sessionMap != null && sessionMap.containsKey("followPage")) 81 responseString = (String ) sessionMap.remove("followPage"); 82 } 83 84 try { 85 PrintWriter out = response.getWriter(); 86 response.setContentType("text/plain"); 87 out.println(responseString); 88 out.close(); 89 } catch (IOException e) { 90 Debug.logError(e, "Problems writing servlet output!", module); 91 } 92 93 return "success"; 94 } 95 96 public static String receiveAppletRequest(HttpServletRequest request, HttpServletResponse response) { 97 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 98 String sessionId = request.getParameter("sessionId"); 99 String visitId = request.getParameter("visitId"); 100 sessionId = sessionId.trim(); 101 visitId = visitId.trim(); 102 103 String responseString = "ERROR"; 104 105 GenericValue visit = null; 106 try { 107 visit = delegator.findByPrimaryKey("Visit", UtilMisc.toMap("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 = (String ) 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", currentPage); 122 appletSessions.put(follower, folSesMap); 123 } 124 } 125 responseString = "OK"; 126 } 127 128 try { 129 PrintWriter out = response.getWriter(); 130 response.setContentType("text/plain"); 131 out.println(responseString); 132 out.close(); 133 } catch (IOException e) { 134 Debug.logError(e, "Problems writing servlet output!", module); 135 } 136 137 return "success"; 138 } 139 140 public static String setAppletFollower(HttpServletRequest request, HttpServletResponse response) { 141 Security security = (Security) request.getAttribute("security"); 142 GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); 143 String visitId = request.getParameter("visitId"); 144 if (visitId != null) request.setAttribute("visitId", visitId); 145 if (security.hasPermission("SEND_CONTROL_APPLET", userLogin)) { 146 String followerSessionId = request.getParameter("followerSid"); 147 String followSessionId = request.getParameter("followSid"); 148 Map follow = (Map ) appletSessions.get(followSessionId); 149 if (follow == null) follow = new HashMap (); 150 String followerListStr = (String ) follow.get("followers"); 151 if (followerListStr == null) { 152 followerListStr = followerSessionId; 153 } else { 154 followerListStr = followerListStr + "," + followerSessionId; 155 } 156 appletSessions.put(followSessionId, follow); 157 appletSessions.put(followerSessionId, null); 158 } 159 return "success"; 160 } 161 162 public static String setFollowerPage(HttpServletRequest request, HttpServletResponse response) { 163 Security security = (Security) request.getAttribute("security"); 164 GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); 165 String visitId = request.getParameter("visitId"); 166 if (visitId != null) request.setAttribute("visitId", visitId); 167 if (security.hasPermission("SEND_CONTROL_APPLET", userLogin)) { 168 String followerSessionId = request.getParameter("followerSid"); 169 String pageUrl = request.getParameter("pageUrl"); 170 Map follow = (Map ) appletSessions.get(followerSessionId); 171 if (follow == null) follow = new HashMap (); 172 follow.put("followPage", pageUrl); 173 appletSessions.put(followerSessionId, follow); 174 } 175 return "success"; 176 } 177 178 179 public static String setSessionLocale(HttpServletRequest request, HttpServletResponse response) { 180 String localeString = request.getParameter("locale"); 181 if (UtilValidate.isNotEmpty(localeString)) { 182 UtilHttp.setLocale(request, localeString); 183 184 GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); 186 if (userLogin == null) { 187 userLogin = (GenericValue) request.getSession().getAttribute("autoUserLogin"); 188 } 189 190 if (userLogin != null) { 191 GenericValue ulUpdate = GenericValue.create(userLogin); 192 ulUpdate.set("lastLocale", localeString); 193 try { 194 ulUpdate.store(); 195 userLogin.refreshFromCache(); 196 } catch (GenericEntityException e) { 197 Debug.logWarning(e, module); 198 } 199 } 200 } 201 return "success"; 202 } 203 204 205 public static String setSessionCurrencyUom(HttpServletRequest request, HttpServletResponse response) { 206 String currencyUom = request.getParameter("currencyUom"); 207 if (UtilValidate.isNotEmpty(currencyUom)) { 208 UtilHttp.setCurrencyUom(request.getSession(), currencyUom); 210 211 GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); 213 if (userLogin == null) { 214 userLogin = (GenericValue) request.getSession().getAttribute("autoUserLogin"); 215 } 216 217 if (userLogin != null) { 218 GenericValue ulUpdate = GenericValue.create(userLogin); 219 ulUpdate.set("lastCurrencyUom", currencyUom); 220 try { 221 ulUpdate.store(); 222 userLogin.refreshFromCache(); 223 } catch (GenericEntityException e) { 224 Debug.logWarning(e, module); 225 } 226 } 227 } 228 return "success"; 229 } 230 } 231 232 | Popular Tags |