1 16 package org.apache.cocoon.jxpath; 17 18 import org.apache.avalon.framework.component.Component; 19 import org.apache.avalon.framework.context.Contextualizable; 20 import org.apache.avalon.framework.thread.ThreadSafe; 21 22 import org.apache.cocoon.components.ContextHelper; 23 import org.apache.cocoon.environment.Context; 24 import org.apache.cocoon.environment.Cookie; 25 import org.apache.cocoon.environment.ObjectModelHelper; 26 import org.apache.cocoon.environment.Request; 27 import org.apache.cocoon.environment.Session; 28 29 import org.apache.commons.jxpath.JXPathContext; 30 import org.apache.commons.jxpath.JXPathContextFactory; 31 import org.apache.commons.jxpath.JXPathIntrospector; 32 import org.apache.commons.jxpath.servlet.Constants; 33 import org.apache.commons.jxpath.servlet.KeywordVariables; 34 35 import java.io.InputStream ; 36 import java.net.MalformedURLException ; 37 import java.net.URL ; 38 import java.security.Principal ; 39 import java.util.Enumeration ; 40 import java.util.Locale ; 41 import java.util.Map ; 42 43 73 public final class JXPathCocoonContexts implements Component, Contextualizable, ThreadSafe { 74 75 public static final String ROLE = JXPathCocoonContexts.class.getName(); 76 77 private static final String VARCONTEXT = Constants.JXPATH_CONTEXT + "/VAR"; 78 79 private static JXPathContextFactory factory; 80 private org.apache.avalon.framework.context.Context context; 81 82 static { 83 factory = JXPathContextFactory.newInstance(); 84 JXPathIntrospector.registerDynamicClass(RequestProxy.class, CocoonRequestHandler.class); 85 JXPathIntrospector.registerDynamicClass(SessionProxy.class, CocoonSessionHandler.class); 86 JXPathIntrospector.registerDynamicClass(ContextProxy.class, CocoonContextHandler.class); 87 } 88 89 public void contextualize(org.apache.avalon.framework.context.Context context) { 90 this.context = context; 91 } 92 93 public final JXPathContext getVariableContext() { 94 final Map objectModel = ContextHelper.getObjectModel(this.context); 95 96 Request request = ObjectModelHelper.getRequest(objectModel); 97 JXPathContext context = (JXPathContext) request.getAttribute(VARCONTEXT); 98 if (context == null) { 99 context = factory.newContext(getRequestContext(), null); 100 request.setAttribute(VARCONTEXT, context); 101 } 102 return context; 103 } 104 105 109 public final JXPathContext getRequestContext() { 110 final Map objectModel = ContextHelper.getObjectModel(this.context); 111 112 Request request = ObjectModelHelper.getRequest(objectModel); 113 JXPathContext context = (JXPathContext) request.getAttribute(Constants.JXPATH_CONTEXT); 114 if (context == null) { 115 Context envContext = ObjectModelHelper.getContext(objectModel); 116 JXPathContext parentContext = null; 117 118 Session session = request.getSession(false); 119 if (session != null) { 120 parentContext = getSessionContext(session, envContext); 121 } else { 122 parentContext = getApplicationContext(envContext); 123 } 124 125 request = new RequestProxy(request); 126 context = factory.newContext(parentContext, request); 127 context.setVariables(new KeywordVariables(Constants.REQUEST_SCOPE, request)); 128 request.setAttribute(Constants.JXPATH_CONTEXT, context); 129 } 130 return context; 131 } 132 133 137 public final JXPathContext getSessionContext(Session session, Context envContext) { 138 JXPathContext context = (JXPathContext) session.getAttribute(Constants.JXPATH_CONTEXT); 139 if (context == null) { 140 JXPathContext parentContext = getApplicationContext(envContext); 141 session = new SessionProxy(session); 142 context = factory.newContext(parentContext, session); 143 context.setVariables(new KeywordVariables(Constants.SESSION_SCOPE, session)); 144 session.setAttribute(Constants.JXPATH_CONTEXT, context); 145 } 146 return context; 147 } 148 149 153 public final JXPathContext getApplicationContext(Context envContext) { 154 JXPathContext context = (JXPathContext) envContext.getAttribute(Constants.JXPATH_CONTEXT); 155 if (context == null) { 156 envContext = new ContextProxy(envContext); 157 context = factory.newContext(null, envContext); 158 context.setVariables(new KeywordVariables(Constants.APPLICATION_SCOPE, envContext)); 159 envContext.setAttribute(Constants.JXPATH_CONTEXT, context); 160 } 161 return context; 162 } 163 164 public static final class RequestProxy implements Request { 165 private Request delegate; 166 167 public RequestProxy (Request delegate) { 168 this.delegate = delegate; 169 } 170 171 public Object get(String name) { 172 return this.delegate.get(name); 173 } 174 175 public Object getAttribute(String name) { 176 return this.delegate.getAttribute(name); 177 } 178 179 public Enumeration getAttributeNames() { 180 return this.delegate.getAttributeNames(); 181 } 182 183 public String getCharacterEncoding() { 184 return this.delegate.getCharacterEncoding(); 185 } 186 187 public void setCharacterEncoding(String enc) 188 throws java.io.UnsupportedEncodingException { 189 this.delegate.setCharacterEncoding(enc); 190 } 191 192 public int getContentLength() { 193 return this.delegate.getContentLength(); 194 } 195 196 public String getContentType() { 197 return this.delegate.getContentType(); 198 } 199 200 public String getParameter(String name) { 201 return this.delegate.getParameter(name); 202 } 203 204 public Enumeration getParameterNames() { 205 return this.delegate.getParameterNames(); 206 } 207 208 public String [] getParameterValues(String name) { 209 return this.delegate.getParameterValues(name); 210 } 211 212 public String getProtocol() { 213 return this.delegate.getProtocol(); 214 } 215 216 public String getScheme() { 217 return this.delegate.getScheme(); 218 } 219 220 public String getServerName() { 221 return this.delegate.getServerName(); 222 } 223 224 public int getServerPort() { 225 return this.delegate.getServerPort(); 226 } 227 228 public String getRemoteAddr() { 229 return this.delegate.getRemoteAddr(); 230 } 231 232 public String getRemoteHost() { 233 return this.delegate.getRemoteHost(); 234 } 235 236 public void setAttribute(String name, Object o) { 237 this.delegate.setAttribute(name, o); 238 } 239 240 public void removeAttribute(String name) { 241 this.delegate.removeAttribute(name); 242 } 243 244 public Locale getLocale() { 245 return this.delegate.getLocale(); 246 } 247 248 public Enumeration getLocales() { 249 return this.delegate.getLocales(); 250 } 251 252 public boolean isSecure() { 253 return this.delegate.isSecure(); 254 } 255 256 public Cookie[] getCookies() { 257 return this.delegate.getCookies(); 258 } 259 260 public Map getCookieMap() { 261 return this.delegate.getCookieMap(); 262 } 263 264 public long getDateHeader(String name) { 265 return this.delegate.getDateHeader(name); 266 } 267 268 public String getHeader(String name) { 269 return this.delegate.getHeader(name); 270 } 271 272 public Enumeration getHeaders(String name) { 273 return this.delegate.getHeaders(name); 274 } 275 276 public Enumeration getHeaderNames() { 277 return this.delegate.getHeaderNames(); 278 } 279 280 public String getMethod() { 281 return this.delegate.getMethod(); 282 } 283 284 public String getPathInfo() { 285 return this.delegate.getPathInfo(); 286 } 287 288 public String getPathTranslated() { 289 return this.delegate.getPathTranslated(); 290 } 291 292 public String getContextPath() { 293 return this.delegate.getContextPath(); 294 } 295 296 public String getQueryString() { 297 return this.delegate.getQueryString(); 298 } 299 300 public String getRemoteUser() { 301 return this.delegate.getRemoteUser(); 302 } 303 304 public String getRequestedSessionId() { 305 return this.delegate.getRequestedSessionId(); 306 } 307 308 public String getRequestURI() { 309 return this.delegate.getRequestURI(); 310 } 311 312 public String getSitemapURI() { 313 return this.delegate.getSitemapURI(); 314 } 315 316 public String getSitemapURIPrefix() { 317 return this.delegate.getSitemapURIPrefix(); 318 } 319 320 public String getServletPath() { 321 return this.delegate.getServletPath(); 322 } 323 324 public Session getSession(boolean create) { 325 return this.delegate.getSession(create); 326 } 327 328 public Session getSession() { 329 return this.delegate.getSession(); 330 } 331 332 public boolean isRequestedSessionIdValid() { 333 return this.delegate.isRequestedSessionIdValid(); 334 } 335 336 public boolean isRequestedSessionIdFromCookie() { 337 return this.delegate.isRequestedSessionIdFromCookie(); 338 } 339 340 public boolean isRequestedSessionIdFromURL() { 341 return this.delegate.isRequestedSessionIdFromURL(); 342 } 343 344 public Principal getUserPrincipal() { 345 return this.delegate.getUserPrincipal(); 346 } 347 348 public boolean isUserInRole(String role) { 349 return this.delegate.isUserInRole(role); 350 } 351 352 public String getAuthType() { 353 return this.delegate.getAuthType(); 354 } 355 } 356 357 public class SessionProxy implements Session { 358 private Session delegate; 359 360 public SessionProxy(Session delegate) { 361 this.delegate = delegate; 362 } 363 364 public long getCreationTime() { 365 return this.delegate.getCreationTime(); 366 } 367 368 public String getId() { 369 return this.delegate.getId(); 370 } 371 372 public long getLastAccessedTime() { 373 return this.delegate.getLastAccessedTime(); 374 } 375 376 public void setMaxInactiveInterval(int interval) { 377 this.delegate.setMaxInactiveInterval(interval); 378 } 379 380 public int getMaxInactiveInterval() { 381 return this.delegate.getMaxInactiveInterval(); 382 } 383 384 public Object getAttribute(String name) { 385 return this.delegate.getAttribute(name); 386 } 387 388 public Enumeration getAttributeNames() { 389 return this.delegate.getAttributeNames(); 390 } 391 392 public void setAttribute(String name, Object value) { 393 this.delegate.setAttribute(name, value); 394 } 395 396 public void removeAttribute(String name) { 397 this.delegate.removeAttribute(name); 398 } 399 400 public void invalidate() { 401 this.delegate.invalidate(); 402 } 403 404 public boolean isNew() { 405 return this.delegate.isNew(); 406 } 407 } 408 409 public class ContextProxy implements Context { 410 private Context delegate; 411 412 public ContextProxy(org.apache.cocoon.environment.Context delegate) { 413 this.delegate = delegate; 414 } 415 416 public Object getAttribute(String name) { 417 return this.delegate.getAttribute(name); 418 } 419 420 public void setAttribute(String name, Object value) { 421 this.delegate.setAttribute(name, value); 422 } 423 424 public void removeAttribute(String name) { 425 this.delegate.removeAttribute(name); 426 } 427 428 public Enumeration getAttributeNames() { 429 return this.delegate.getAttributeNames(); 430 } 431 432 public URL getResource(String path) throws MalformedURLException { 433 return this.delegate.getResource(path); 434 } 435 436 public String getRealPath(String path) { 437 return this.delegate.getRealPath(path); 438 } 439 440 public String getMimeType(String file) { 441 return this.delegate.getMimeType(file); 442 } 443 444 public String getInitParameter(String name) { 445 return this.delegate.getInitParameter(name); 446 } 447 448 public InputStream getResourceAsStream(String path) { 449 return this.delegate.getResourceAsStream(path); 450 } 451 } 452 } 453 | Popular Tags |