1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.servlet;
18
19 import javax.servlet.ServletContext;
20 import javax.servlet.ServletRequest;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpSession;
23 import javax.servlet.jsp.PageContext;
24 import org.apache.commons.jxpath.JXPathContext;
25 import org.apache.commons.jxpath.JXPathContextFactory;
26 import org.apache.commons.jxpath.JXPathIntrospector;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public final class JXPathServletContexts {
69
70 private static JXPathContextFactory factory;
71
72 static {
73 JXPathIntrospector.registerDynamicClass(
74 PageScopeContext.class,
75 PageScopeContextHandler.class);
76 JXPathIntrospector.registerDynamicClass(
77 PageContext.class,
78 PageContextHandler.class);
79 JXPathIntrospector.registerDynamicClass(
80 ServletContext.class,
81 ServletContextHandler.class);
82 JXPathIntrospector.registerDynamicClass(
83 ServletRequestAndContext.class,
84 ServletRequestHandler.class);
85 JXPathIntrospector.registerDynamicClass(
86 HttpSessionAndServletContext.class,
87 HttpSessionHandler.class);
88 factory = JXPathContextFactory.newInstance();
89 }
90
91
92
93
94
95
96
97 public static JXPathContext getPageContext(PageContext pageContext) {
98 JXPathContext context =
99 (JXPathContext) pageContext.getAttribute(Constants.JXPATH_CONTEXT);
100 if (context == null) {
101 JXPathContext parentContext =
102 getRequestContext(
103 pageContext.getRequest(),
104 pageContext.getServletContext());
105 context = factory.newContext(parentContext, pageContext);
106 context.setVariables(
107 new KeywordVariables(
108 Constants.PAGE_SCOPE,
109 new PageScopeContext(pageContext)));
110 pageContext.setAttribute(Constants.JXPATH_CONTEXT, context);
111 }
112 return context;
113 }
114
115
116
117
118
119
120
121
122 public static JXPathContext getRequestContext(ServletRequest request,
123 ServletContext servletContext) {
124 JXPathContext context =
125 (JXPathContext) request.getAttribute(Constants.JXPATH_CONTEXT);
126
127
128
129 if (context != null) {
130 ServletRequestAndContext handle =
131 (ServletRequestAndContext) context.getContextBean();
132 if (handle.getServletRequest() == request) {
133 return context;
134 }
135 }
136
137 JXPathContext parentContext = null;
138 if (request instanceof HttpServletRequest) {
139 HttpSession session =
140 ((HttpServletRequest) request).getSession(false);
141 if (session != null) {
142 parentContext = getSessionContext(session, servletContext);
143 }
144 else {
145 parentContext = getApplicationContext(servletContext);
146 }
147 }
148 ServletRequestAndContext handle =
149 new ServletRequestAndContext(request, servletContext);
150 context = factory.newContext(parentContext, handle);
151 context.setVariables(
152 new KeywordVariables(Constants.REQUEST_SCOPE, handle));
153 request.setAttribute(Constants.JXPATH_CONTEXT, context);
154 return context;
155 }
156
157
158
159
160
161
162
163
164 public static JXPathContext getSessionContext(HttpSession session,
165 ServletContext servletContext) {
166 JXPathContext context =
167 (JXPathContext) session.getAttribute(Constants.JXPATH_CONTEXT);
168 if (context == null) {
169 JXPathContext parentContext = getApplicationContext(servletContext);
170 HttpSessionAndServletContext handle =
171 new HttpSessionAndServletContext(session, servletContext);
172 context = factory.newContext(parentContext, handle);
173 context.setVariables(
174 new KeywordVariables(Constants.SESSION_SCOPE, handle));
175 session.setAttribute(Constants.JXPATH_CONTEXT, context);
176 }
177 return context;
178 }
179
180
181
182
183
184
185
186 public static JXPathContext getApplicationContext(
187 ServletContext servletContext) {
188 JXPathContext context =
189 (JXPathContext) servletContext.getAttribute(
190 Constants.JXPATH_CONTEXT);
191 if (context == null) {
192 context = factory.newContext(null, servletContext);
193 context.setVariables(
194 new KeywordVariables(
195 Constants.APPLICATION_SCOPE,
196 servletContext));
197 servletContext.setAttribute(Constants.JXPATH_CONTEXT, context);
198 }
199 return context;
200 }
201 }