1 48 49 package org.jpublish; 50 51 import java.io.IOException ; 52 import java.util.HashMap ; 53 import java.util.Map ; 54 import java.util.Set ; 55 56 import javax.servlet.RequestDispatcher ; 57 import javax.servlet.ServletContext ; 58 import javax.servlet.ServletException ; 59 import javax.servlet.http.HttpServletRequest ; 60 import javax.servlet.http.HttpServletResponse ; 61 import javax.servlet.http.HttpSession ; 62 63 import org.apache.commons.logging.Log; 64 import org.apache.commons.logging.LogFactory; 65 import org.jpublish.page.Page; 66 import org.jpublish.util.DateUtilities; 67 import org.jpublish.util.NumberUtilities; 68 import org.jpublish.util.URLUtilities; 69 import org.jpublish.util.encoding.CharacterEncodingMap; 70 71 77 78 public class RequestContext { 79 80 81 public static final String JPUBLISH_PATH = "path"; 82 public static final String JPUBLISH_REDIRECT = "redirect"; 83 public static final String JPUBLISH_STOP_PROCESSING = "stop-processing"; 84 public static final String JPUBLISH_REQUEST = "request"; 85 public static final String JPUBLISH_RESPONSE = "response"; 86 public static final String JPUBLISH_SESSION = "session"; 87 public static final String JPUBLISH_APPLICATION = "application"; 88 public static final String JPUBLISH_CHARACTER_ENCODING_MAP = 89 "characterEncodingMap"; 90 public static final String JPUBLISH_URL_UTILITIES = "urlUtilities"; 91 public static final String JPUBLISH_DATE_UTILITIES = "dateUtilities"; 92 public static final String JPUBLISH_NUMBER_UTILITIES = "numberUtilities"; 93 public static final String JPUBLISH_SYSLOG = "syslog"; 94 public static final String JPUBLISH_SITE = "site"; 95 public static final String JPUBLISH_PAGE = "page"; 96 97 100 private static final String [] reservedNames = { 101 "path", "request", "response", "session", "application", 102 "page", "site", "components" 103 }; 104 105 private static Log log = LogFactory.getLog(RequestContext.class); 106 107 private static RequestContextThreadLocal requestContextThreadLocal = 108 new RequestContextThreadLocal(); 109 110 private Map data = new HashMap (); 111 private boolean checkReservedNames = false; 112 private boolean forwarded = false; 113 114 119 120 public static RequestContext getRequestContext() { 121 return (RequestContext) requestContextThreadLocal.get(); 122 } 123 124 130 131 public Object get(String key) { 132 return data.get(key); 133 } 134 135 143 144 public Object put(String key, Object value) { 145 if (checkReservedNames && (isReservedName(key))) { 146 throw new ReservedNameException(key); 147 } 148 149 return data.put(key, value); 150 } 151 152 159 160 public Object remove(String key) { 161 if (checkReservedNames && isReservedName(key)) { 162 throw new ReservedNameException(key); 163 } 164 165 return data.remove(key); 166 } 167 168 173 174 public String [] getKeys() { 175 Set keys = data.keySet(); 176 return (String []) keys.toArray(new String [keys.size()]); 177 } 178 179 185 186 public boolean containsKey(String key) { 187 return data.containsKey(key); 188 } 189 190 193 194 public void clear() { 195 data.clear(); 196 } 197 198 201 202 public final void enableCheckReservedNames() { 203 checkReservedNames = true; 204 } 205 206 209 210 public final void disableCheckReservedNames() { 211 checkReservedNames = false; 212 } 213 214 218 219 224 225 public String getPath() { 226 return (String ) get(JPUBLISH_PATH); 227 } 228 229 235 236 public String getRedirect() { 237 return (String ) get(JPUBLISH_REDIRECT); 238 } 239 240 245 246 public void setRedirect(String redirect) { 247 put(JPUBLISH_REDIRECT, redirect); 248 } 249 250 255 256 public String getStopProcessing() { 257 return (String ) get(JPUBLISH_STOP_PROCESSING); 258 } 259 260 266 267 public void setStopProcessing(String stopProcessing) { 268 put(JPUBLISH_STOP_PROCESSING, stopProcessing); 269 } 270 271 276 277 public void forward(String path) throws ServletException , IOException { 278 HttpServletRequest request = getRequest(); 279 HttpServletResponse response = getResponse(); 280 RequestDispatcher dispatcher = request.getRequestDispatcher(path); 281 setStopProcessing("true"); 282 setForwarded(true); 283 log.debug("Forwarding now"); 284 dispatcher.forward(request, response); 285 setStopProcessing("true"); 286 setForwarded(true); 287 log.debug("Forward complete"); 288 } 289 290 296 297 public boolean isForwarded() { 298 return forwarded; 299 } 300 301 void setForwarded(boolean forwarded) { 302 this.forwarded = forwarded; 303 } 304 305 310 311 public void include(String path) throws ServletException , IOException { 312 HttpServletRequest request = getRequest(); 313 HttpServletResponse response = getResponse(); 314 RequestDispatcher dispatcher = request.getRequestDispatcher(path); 315 dispatcher.include(request, response); 316 } 317 318 323 324 public HttpServletRequest getRequest() { 325 return (HttpServletRequest ) get(JPUBLISH_REQUEST); 326 } 327 328 333 334 public HttpServletResponse getResponse() { 335 return (HttpServletResponse ) get(JPUBLISH_RESPONSE); 336 } 337 338 343 344 public HttpSession getSession() { 345 return (HttpSession ) get(JPUBLISH_SESSION); 346 } 347 348 353 354 public ServletContext getApplication() { 355 return (ServletContext ) get(JPUBLISH_APPLICATION); 356 } 357 358 363 364 public CharacterEncodingMap getCharacterEncodingMap() { 365 return (CharacterEncodingMap) get(JPUBLISH_CHARACTER_ENCODING_MAP); 366 } 367 368 373 374 public URLUtilities getUrlUtilities() { 375 return (URLUtilities) get(JPUBLISH_URL_UTILITIES); 376 } 377 378 383 384 public DateUtilities getDateUtilities() { 385 return (DateUtilities) get(JPUBLISH_DATE_UTILITIES); 386 } 387 388 393 394 public NumberUtilities getNumberUtilities() { 395 return (NumberUtilities) get(JPUBLISH_NUMBER_UTILITIES); 396 } 397 398 403 404 public Log getSyslog() { 405 return (Log) get(JPUBLISH_SYSLOG); 406 } 407 408 413 414 public SiteContext getSiteContext() { 415 return (SiteContext) get(JPUBLISH_SITE); 416 } 417 418 423 424 public Page getPage() { 425 return (Page) get(JPUBLISH_PAGE); 426 } 427 428 435 436 private static final boolean isReservedName(String name) { 437 for (int i = 0; i < reservedNames.length; i++) { 438 if (name.equals(reservedNames[i])) { 439 return true; 440 } 441 } 442 return false; 443 } 444 445 450 451 private static class RequestContextThreadLocal extends ThreadLocal { 452 453 protected Object initialValue() { 454 return new RequestContext(); 455 } 456 } 457 458 } 459
| Popular Tags
|