1 20 package org.enhydra.barracuda.core.event; 21 22 import java.io.*; 23 import java.util.*; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 27 import org.apache.log4j.*; 28 29 import org.enhydra.barracuda.core.view.*; 30 import org.enhydra.barracuda.plankton.data.*; 31 32 48 public class DefaultEventContext implements ViewEventContext { 49 50 protected static final Logger logger = Logger.getLogger(DefaultEventContext.class.getName()); 52 53 private static final String CONTEXT_ID = "$ctxid"; 55 private static final String STATE = "_STATE"; 56 private static final String LOCAL_OR = "_LOCAL_OR"; 57 private StateMap statemap = null; 58 private static long uid = System.currentTimeMillis(); 59 60 70 public DefaultEventContext(DispatchQueue queue, ViewCapabilities vc, ServletConfig config, HttpServletRequest req, HttpServletResponse resp, HttpResponseEvent defaultResponseEvent) { 71 restoreContext(req); 72 this.putState(EventContext.DISPATCH_QUEUE, queue); 73 this.putState(EventContext.VIEW_CAPABILITIES, vc); 74 this.putState(ControlEventContext.SERVLET_CONFIG, config); 75 this.putState(ControlEventContext.HTTP_SERVLET_REQUEST, req); 76 this.putState(ViewEventContext.HTTP_SERVLET_RESPONSE, resp); 77 this.putState(DefaultEventDispatcher.DEFAULT_RESPONSE_EVENT, defaultResponseEvent); 78 } 79 80 85 public BaseEvent getEvent() { 86 return (BaseEvent) this.getState(EventContext.BASE_EVENT); 87 } 88 89 94 public DispatchQueue getQueue() { 95 return (DispatchQueue) this.getState(EventContext.DISPATCH_QUEUE); 96 } 97 98 101 public ViewCapabilities getViewCapabilities() { 102 return (ViewCapabilities) this.getState(EventContext.VIEW_CAPABILITIES); 103 } 104 105 110 public ServletConfig getConfig() { 111 return (ServletConfig) this.getState(ControlEventContext.SERVLET_CONFIG); 112 } 113 114 119 public HttpServletRequest getRequest() { 120 return (HttpServletRequest) this.getState(ControlEventContext.HTTP_SERVLET_REQUEST); 121 } 122 123 128 public HttpServletResponse getResponse() { 129 return (HttpServletResponse) this.getState(ViewEventContext.HTTP_SERVLET_RESPONSE); 130 } 131 132 134 152 public void persistContext(ClientSideRedirectException re) { 153 if (logger.isInfoEnabled()) logger.info("persisting context to prepare for client side redirect..."); 154 155 172 ObjectRepository wgor = ObjectRepository.getSoftGlobalRepository(); String id = getUID(); 179 if (logger.isDebugEnabled()) logger.debug("...getting uid:"+id); 180 181 wgor.putState(CONTEXT_ID+id+STATE, statemap); 185 if (logger.isDebugEnabled()) logger.debug("...saved statemap, key = "+CONTEXT_ID+id+STATE); 186 ObjectRepository lr = ObjectRepository.getLocalRepository(); 188 List list = lr.getStateKeys(); 189 if (list!=null && list.size()>0) { 190 StateMap sm = new DefaultStateMap(); 191 wgor.putState(CONTEXT_ID+id+LOCAL_OR, sm); 192 if (logger.isDebugEnabled()) logger.debug("...saved lr, key = "+CONTEXT_ID+id+LOCAL_OR); 193 Iterator it = list.iterator(); 194 while (it.hasNext()) { 195 Object key = it.next(); 196 Object val = lr.getState(key); 197 sm.putState(key, val); 198 if (logger.isDebugEnabled()) logger.debug("......saved lr items: key="+key+" val="+val); 199 } 200 } 201 202 String url = re.getRedirectURL(); 205 int spos = url.indexOf("?"); 206 String sep = (spos>-1 ? "&" : "?"); 207 re.setRedirectURL(url+sep+CONTEXT_ID+"="+id); 208 if (logger.isDebugEnabled()) logger.debug("...adjusting redirect URL from:"+url+" to:"+re.getRedirectURL()); 209 } 210 211 217 public void restoreContext(HttpServletRequest req) { 218 if (logger.isInfoEnabled()) logger.info("see if we need to restore context..."); 219 220 String id = req.getParameter(CONTEXT_ID); 222 if (logger.isDebugEnabled()) logger.debug("...looking for context id in req:"+id); 223 if (id!=null) { 224 if (logger.isDebugEnabled()) logger.debug("...found persisted context! attempting to restore..."); 225 226 ObjectRepository wgor = ObjectRepository.getSoftGlobalRepository(); String stkey = CONTEXT_ID+id+STATE; 231 Object o = wgor.getState(stkey); 232 if (logger.isDebugEnabled()) logger.debug("......looking for statemap in wgor, key:"+stkey+" found:"+o); 233 if (o!=null && o instanceof StateMap) statemap = (StateMap) o; 234 else logger.warn("......expected statemap context missing!"); 235 wgor.removeState(stkey); 236 stkey = CONTEXT_ID+id+LOCAL_OR; 238 o = wgor.getState(stkey); 239 if (logger.isDebugEnabled()) logger.debug("......looking for lr in wgor, key:"+stkey+" found:"+o); 240 if (o!=null && o instanceof StateMap) { 241 StateMap sm = (StateMap) o; 242 if (sm!=null) { 243 List list = sm.getStateKeys(); 244 if (list!=null) { 245 ObjectRepository lr = ObjectRepository.getLocalRepository(); 246 Iterator it = list.iterator(); 247 while (it.hasNext()) { 248 Object key = it.next(); 249 Object val = sm.getState(key); 250 lr.putState(key, val); 251 if (logger.isDebugEnabled()) logger.debug(".........retrieved lr items: key="+key+" val="+val); 252 } 253 } 254 } 255 } 256 wgor.removeState(stkey); 257 } else { 258 if (logger.isDebugEnabled()) logger.debug("...no persisted context"); 259 } 260 261 if (statemap==null) statemap = new DefaultStateMap(); 262 288 } 289 290 private synchronized static String getUID() { 291 return "_"+(++uid); 292 } 293 294 295 296 303 public void putState(Object key, Object val) { 304 statemap.putState(key,val); 305 } 306 307 313 public Object getState(Object key) { 314 return statemap.getState(key); 315 } 316 317 323 public Object removeState(Object key) { 324 return statemap.removeState(key); 325 } 326 327 332 public List getStateKeys() { 333 return statemap.getStateKeys(); 334 } 335 336 341 public Map getStateValues() { 342 return statemap.getStateValues(); 343 } 344 345 349 public void clearState() { 350 statemap.clearState(); 351 } 352 } 353 | Popular Tags |