dmitri 2004/05/08 08:10:49
Modified: jxpath/src/java/org/apache/commons/jxpath/servlet
ServletRequestHandler.java HttpSessionHandler.java
JXPathServletContexts.java PageContextHandler.java
ServletContextHandler.java
PageScopeContextHandler.java
Added: jxpath/src/java/org/apache/commons/jxpath/servlet
ServletRequestAndContext.java
HttpSessionAndServletContext.java
jxpath/src/test-webapp JXPathServletContextsTestServlet.java
JXPathTest.jsp
jxpath/src/test-webapp/WEB-INF web.xml
Removed: jxpath/src/java/org/apache/commons/jxpath/servlet Util.java
Log:
Fixed issues with JXPathServletContexts
Revision Changes Path
1.6 +38 -11
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/ServletRequestHandler.java
Index: ServletRequestHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/ServletRequestHandler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ServletRequestHandler.java 29 Feb 2004 14:17:40 -0000 1.5
+++ ServletRequestHandler.java 8 May 2004 15:10:49 -0000 1.6
@@ -16,26 +16,53 @@
package org.apache.commons.jxpath.servlet;
import java.util.Enumeration;
+import java.util.HashSet;
import javax.servlet.ServletRequest;
-import org.apache.commons.jxpath.DynamicPropertyHandler;
/**
* Implementation of the DynamicPropertyHandler interface that provides
- * access to attributes of a ServletRequest.
+ * access to attributes and parameters of a ServletRequest.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
-public class ServletRequestHandler implements DynamicPropertyHandler {
-
- public String[] getPropertyNames(Object request) {
- Enumeration e = ((ServletRequest) request).getAttributeNames();
- return Util.toStrings(e);
+public class ServletRequestHandler extends HttpSessionHandler {
+
+ protected void collectPropertyNames(HashSet set, Object bean) {
+ super.collectPropertyNames(set, bean);
+ ServletRequestAndContext handle = (ServletRequestAndContext) bean;
+ ServletRequest servletRequest = handle.getServletRequest();
+ Enumeration e = servletRequest.getAttributeNames();
+ while (e.hasMoreElements()) {
+ set.add(e.nextElement());
+ }
+ e = servletRequest.getParameterNames();
+ while (e.hasMoreElements()) {
+ set.add(e.nextElement());
+ }
}
-
- public Object getProperty(Object request, String property) {
- return ((ServletRequest) request).getAttribute(property);
+
+ public Object getProperty(Object bean, String property) {
+ ServletRequestAndContext handle = (ServletRequestAndContext) bean;
+ ServletRequest servletRequest = handle.getServletRequest();
+ String[] strings = servletRequest.getParameterValues(property);
+ if (strings != null) {
+ if (strings.length == 0) {
+ return null;
+ }
+ if (strings.length == 1) {
+ return strings[0];
+ }
+ return strings;
+ }
+
+ Object object = servletRequest.getAttribute(property);
+ if (object != null) {
+ return object;
+ }
+
+ return super.getProperty(bean, property);
}
public void setProperty(Object request, String property, Object value) {
1.6 +39 -11
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/HttpSessionHandler.java
Index: HttpSessionHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/HttpSessionHandler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- HttpSessionHandler.java 29 Feb 2004 14:17:40 -0000 1.5
+++ HttpSessionHandler.java 8 May 2004 15:10:49 -0000 1.6
@@ -16,9 +16,11 @@
package org.apache.commons.jxpath.servlet;
import java.util.Enumeration;
+import java.util.HashSet;
import javax.servlet.http.HttpSession;
-import org.apache.commons.jxpath.DynamicPropertyHandler;
+
+import org.apache.commons.jxpath.JXPathException;
/**
* Implementation of the DynamicPropertyHandler interface that provides
@@ -27,18 +29,44 @@
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
-public class HttpSessionHandler implements DynamicPropertyHandler {
+public class HttpSessionHandler extends ServletContextHandler {
- public String[] getPropertyNames(Object session) {
- Enumeration e = ((HttpSession) session).getAttributeNames();
- return Util.toStrings(e);
+ protected void collectPropertyNames(HashSet set, Object bean) {
+ HttpSessionAndServletContext handle =
+ (HttpSessionAndServletContext) bean;
+ super.collectPropertyNames(set, handle.getServletContext());
+ HttpSession session = handle.getSession();
+ if (session != null) {
+ Enumeration e = session.getAttributeNames();
+ while (e.hasMoreElements()) {
+ set.add(e.nextElement());
+ }
+ }
}
-
- public Object getProperty(Object session, String property) {
- return ((HttpSession) session).getAttribute(property);
+
+ public Object getProperty(Object bean, String property) {
+ HttpSessionAndServletContext handle =
+ (HttpSessionAndServletContext) bean;
+ HttpSession session = handle.getSession();
+ if (session != null) {
+ Object object = session.getAttribute(property);
+ if (object != null) {
+ return object;
+ }
+ }
+ return super.getProperty(handle.getServletContext(), property);
}
- public void setProperty(Object session, String property, Object value) {
- ((HttpSession) session).setAttribute(property, value);
+ public void setProperty(Object bean, String property, Object value) {
+ HttpSessionAndServletContext handle =
+ (HttpSessionAndServletContext) bean;
+ HttpSession session = handle.getSession();
+ if (session != null) {
+ session.setAttribute(property, value);
+ }
+ else {
+ throw new JXPathException("Cannot set session attribute: "
+ + "there is no session");
+ }
}
}
1.6 +45 -32
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/JXPathServletContexts.java
Index: JXPathServletContexts.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/JXPathServletContexts.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- JXPathServletContexts.java 29 Feb 2004 14:17:40 -0000 1.5
+++ JXPathServletContexts.java 8 May 2004 15:10:49 -0000 1.6
@@ -69,8 +69,20 @@
static {
JXPathIntrospector.registerDynamicClass(
- PageScopeContext.class,
- PageScopeContextHandler.class);
+ PageScopeContext.class,
+ PageScopeContextHandler.class);
+ JXPathIntrospector.registerDynamicClass(
+ PageContext.class,
+ PageContextHandler.class);
+ JXPathIntrospector.registerDynamicClass(
+ ServletContext.class,
+ ServletContextHandler.class);
+ JXPathIntrospector.registerDynamicClass(
+ ServletRequestAndContext.class,
+ ServletRequestHandler.class);
+ JXPathIntrospector.registerDynamicClass(
+ HttpSessionAndServletContext.class,
+ HttpSessionHandler.class);
factory = JXPathContextFactory.newInstance();
}
@@ -82,9 +94,6 @@
JXPathContext context =
(JXPathContext) pageContext.getAttribute(Constants.JXPATH_CONTEXT);
if (context == null) {
- JXPathIntrospector.registerDynamicClass(
- pageContext.getClass(),
- PageContextHandler.class);
JXPathContext parentContext =
getRequestContext(
pageContext.getRequest(),
@@ -109,26 +118,34 @@
{
JXPathContext context =
(JXPathContext) request.getAttribute(Constants.JXPATH_CONTEXT);
- if (context == null) {
- JXPathContext parentContext = null;
- if (request instanceof HttpServletRequest) {
- HttpSession session =
- ((HttpServletRequest) request).getSession(false);
- if (session != null) {
- parentContext = getSessionContext(session, servletContext);
- }
- else {
- parentContext = getApplicationContext(servletContext);
- }
+ // If we are in an included JSP or Servlet, the request parameter
+ // will represent the included URL, but the JXPathContext we have
+ // just acquired will represent the outer request.
+ if (context != null) {
+ ServletRequestAndContext handle =
+ (ServletRequestAndContext) context.getContextBean();
+ if (handle.getServletRequest() == request) {
+ return context;
+ }
+ }
+
+ JXPathContext parentContext = null;
+ if (request instanceof HttpServletRequest) {
+ HttpSession session =
+ ((HttpServletRequest) request).getSession(false);
+ if (session != null) {
+ parentContext = getSessionContext(session, servletContext);
+ }
+ else {
+ parentContext = getApplicationContext(servletContext);
}
- JXPathIntrospector.registerDynamicClass(
- request.getClass(),
- ServletRequestHandler.class);
- context = factory.newContext(parentContext, request);
- context.setVariables(
- new KeywordVariables(Constants.REQUEST_SCOPE, request));
- request.setAttribute(Constants.JXPATH_CONTEXT, context);
}
+ ServletRequestAndContext handle =
+ new ServletRequestAndContext(request, servletContext);
+ context = factory.newContext(parentContext, handle);
+ context.setVariables(
+ new KeywordVariables(Constants.REQUEST_SCOPE, handle));
+ request.setAttribute(Constants.JXPATH_CONTEXT, context);
return context;
}
@@ -143,13 +160,12 @@
JXPathContext context =
(JXPathContext) session.getAttribute(Constants.JXPATH_CONTEXT);
if (context == null) {
- JXPathIntrospector.registerDynamicClass(
- session.getClass(),
- HttpSessionHandler.class);
JXPathContext parentContext = getApplicationContext(servletContext);
- context = factory.newContext(parentContext, session);
+ HttpSessionAndServletContext handle =
+ new HttpSessionAndServletContext(session, servletContext);
+ context = factory.newContext(parentContext, handle);
context.setVariables(
- new KeywordVariables(Constants.SESSION_SCOPE, session));
+ new KeywordVariables(Constants.SESSION_SCOPE, handle));
session.setAttribute(Constants.JXPATH_CONTEXT, context);
}
return context;
@@ -166,9 +182,6 @@
(JXPathContext) servletContext.getAttribute(
Constants.JXPATH_CONTEXT);
if (context == null) {
- JXPathIntrospector.registerDynamicClass(
- servletContext.getClass(),
- ServletContextHandler.class);
context = factory.newContext(null, servletContext);
context.setVariables(
new KeywordVariables(
1.6 +4 -3
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/PageContextHandler.java
Index: PageContextHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/PageContextHandler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- PageContextHandler.java 29 Feb 2004 14:17:40 -0000 1.5
+++ PageContextHandler.java 8 May 2004 15:10:49 -0000 1.6
@@ -15,10 +15,11 @@
*/
package org.apache.commons.jxpath.servlet;
-import java.util.ArrayList;
import java.util.Enumeration;
+import java.util.HashSet;
import javax.servlet.jsp.PageContext;
+
import org.apache.commons.jxpath.DynamicPropertyHandler;
/**
@@ -31,7 +32,7 @@
public class PageContextHandler implements DynamicPropertyHandler {
public String[] getPropertyNames(Object pageContext) {
- ArrayList list = new ArrayList();
+ HashSet list = new HashSet();
Enumeration e =
((PageContext) pageContext).getAttributeNamesInScope(
PageContext.PAGE_SCOPE);
1.6 +15 -3
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/ServletContextHandler.java
Index: ServletContextHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/ServletContextHandler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ServletContextHandler.java 29 Feb 2004 14:17:40 -0000 1.5
+++ ServletContextHandler.java 8 May 2004 15:10:49 -0000 1.6
@@ -16,8 +16,10 @@
package org.apache.commons.jxpath.servlet;
import java.util.Enumeration;
+import java.util.HashSet;
import javax.servlet.ServletContext;
+
import org.apache.commons.jxpath.DynamicPropertyHandler;
/**
@@ -28,10 +30,20 @@
* @version $Revision$ $Date$
*/
public class ServletContextHandler implements DynamicPropertyHandler {
+
+ private static final String[] STRING_ARRAY = new String[0];
public String[] getPropertyNames(Object context) {
- Enumeration e = ((ServletContext) context).getAttributeNames();
- return Util.toStrings(e);
+ HashSet list = new HashSet(16);
+ collectPropertyNames(list, context);
+ return (String[]) list.toArray(STRING_ARRAY);
+ }
+
+ protected void collectPropertyNames(HashSet set, Object bean) {
+ Enumeration e = ((ServletContext) bean).getAttributeNames();
+ while (e.hasMoreElements()) {
+ set.add(e.nextElement());
+ }
}
public Object getProperty(Object context, String property) {
1.6 +9 -2
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/PageScopeContextHandler.java
Index: PageScopeContextHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/PageScopeContextHandler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- PageScopeContextHandler.java 29 Feb 2004 14:17:40 -0000 1.5
+++ PageScopeContextHandler.java 8 May 2004 15:10:49 -0000 1.6
@@ -15,6 +15,7 @@
*/
package org.apache.commons.jxpath.servlet;
+import java.util.ArrayList;
import java.util.Enumeration;
import org.apache.commons.jxpath.DynamicPropertyHandler;
@@ -27,10 +28,16 @@
* @version $Revision$ $Date$
*/
public class PageScopeContextHandler implements DynamicPropertyHandler {
+
+ private static final String[] STRING_ARRAY = new String[0];
public String[] getPropertyNames(Object pageScope) {
Enumeration e = ((PageScopeContext) pageScope).getAttributeNames();
- return Util.toStrings(e);
+ ArrayList list = new ArrayList(16);
+ while (e.hasMoreElements()) {
+ list.add(e.nextElement());
+ }
+ return (String[]) list.toArray(STRING_ARRAY);
}
public Object getProperty(Object pageScope, String property) {
1.1
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/ServletRequestAndContext.java
Index: ServletRequestAndContext.java
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* Just a structure to hold a ServletRequest and ServletContext together.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2004/05/08 15:10:49 $
*/
public class ServletRequestAndContext extends
HttpSessionAndServletContext
{
private ServletRequest request;
public ServletRequestAndContext(ServletRequest request,
ServletContext context)
{
super(null, context);
this.request = request;
}
public HttpSession getSession() {
if (request instanceof HttpServletRequest) {
return ((HttpServletRequest)request).getSession(false);
}
return null;
}
public ServletRequest getServletRequest() {
return request;
}
}
1.1
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/servlet/HttpSessionAndServletContext.java
Index: HttpSessionAndServletContext.java
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.servlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
/**
* Just a structure to hold a ServletRequest and ServletContext together.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2004/05/08 15:10:49 $
*/
public class HttpSessionAndServletContext {
private HttpSession session;
private ServletContext context;
public HttpSessionAndServletContext(HttpSession session,
ServletContext context) {
this.session = session;
this.context = context;
}
public HttpSession getSession() {
return session;
}
public ServletContext getServletContext() {
return context;
}
}
1.1
jakarta-commons/jxpath/src/test-webapp/JXPathServletContextsTestServlet.java
Index: JXPathServletContextsTestServlet.java
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.servlet.JXPathServletContexts;
/**
* Invoke like this: http://localhost:8080/jxpath?parm=OK
*
* @version $Revision: 1.1 $ $Date: 2004/05/08 15:10:49 $
*/
public class JXPathServletContextsTestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
ServletContext servletContext = getServletContext();
servletContext.setAttribute("app", "OK");
JXPathContext appContext = JXPathServletContexts
.getApplicationContext(servletContext);
request.setAttribute("attr", "OK");
JXPathContext reqContext = JXPathServletContexts.getRequestContext(
request,
servletContext);
HttpSession session = request.getSession();
Integer count = (Integer) session.getAttribute("count");
if (count == null) {
count = new Integer(0);
}
else {
count = new Integer(count.intValue() + 1);
}
session.setAttribute("count", count);
JXPathContext sessionContext = JXPathServletContexts.getSessionContext(
session,
servletContext);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JXPathServletContext</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>JXPathServletContexts Servlet Context Test</h1>");
assertEqual(
out,
"Application Context",
appContext.getValue("app"),
"OK");
assertEqual(
out,
"Request Context Attribute",
reqContext.getValue("attr"),
"OK");
assertEqual(
out,
"Request Context Attribute",
reqContext.getValue("attr"),
"OK");
if (request.getParameter("parm") == null) {
out.println("<p><b>Invoke this test servlet like this: "
+ "http://localhost:8080/jxpath-war/jxpath?parm=OK<b>");
}
else {
assertEqual(
out,
"Request Context Parameter",
reqContext.getValue("parm"),
"OK");
}
assertEqual(
out,
"Session Context Parameter (reload for actual test)",
sessionContext.getValue("count"),
count);
assertEqual(
out,
"Application Context via Request Context",
reqContext.getValue("app"),
"OK");
assertEqual(
out,
"Session Context via Request Context",
reqContext.getValue("count"),
count);
assertEqual(
out,
"Application Context via Session Context",
sessionContext.getValue("app"),
"OK");
out.println("</body>");
out.println("</html>");
}
private void assertEqual(
PrintWriter out,
String title,
Object actual,
Object expected)
{
if ((actual == null && expected == null)
|| (actual != null && actual.equals(expected))) {
out.println("<p>" + title + ": Ok");
}
else {
out.println("<p><font color=red>" + title + ": Failure</font>");
}
}
}
1.1 jakarta-commons/jxpath/src/test-webapp/JXPathTest.jsp
Index: JXPathTest.jsp
===================================================================
<%@ page language="java" %>
<%@ page import="org.apache.commons.jxpath.JXPathContext" %>
<%@ page import="org.apache.commons.jxpath.servlet.JXPathServletContexts" %>
<%!
private String assertEqual(
String title,
Object actual,
Object expected)
{
if ((actual == null && expected == null)
|| (actual != null && actual.equals(expected))) {
return "<p>" + title + ": Ok";
}
else {
return "<p><font color=red>" + title + ": Failure</font>";
}
}
%>
<html>
<head>
<title>JXPathServletContext</title>
</head>
<body>
<%
pageContext.setAttribute("page", "page");
pageContext.getServletContext().setAttribute("app", "app");
request.setAttribute("request", "request");
session.setAttribute("session", "session");
JXPathContext context = JXPathServletContexts.getPageContext(pageContext);
context.setLenient(true);
%>
<h1>JXPathServletContexts JSP PageContext Context Test</h1>
<%= assertEqual("Page Scope", context.getValue("page"), "page") %>
<%= assertEqual("Request Scope", context.getValue("request"), "request") %>
<%= assertEqual("Session Scope", context.getValue("session"), "session") %>
<%= assertEqual("Application Scope", context.getValue("app"), "app") %>
<%= assertEqual("Explicit Page Scope", context.getValue("$page/page"), "page") %>
<%= assertEqual("Explicit Request Scope", context.getValue("$request/request"),
"request") %>
<%= assertEqual("Explicit Session Scope", context.getValue("$session/session"),
"session") %>
<%= assertEqual("Explicit Application Scope",
context.getValue("$application/app"), "app") %>
<jsp:include page="jxpath?parm=OK"/>
</body>
</html>
1.1 jakarta-commons/jxpath/src/test-webapp/WEB-INF/web.xml
Index: web.xml
===================================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>JXPathServletContextsTest</display-name>
<servlet>
<servlet-name>jxpath</servlet-name>
<servlet-class>JXPathServletContextsTestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jxpath</servlet-name>
<url-pattern>/jxpath</url-pattern>
</servlet-mapping>
</web-app>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]