[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit RequestContext.java,NONE,1.1 Invocatio
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-02-11 04:32:04
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit
In directory sc8-pr-cvs1:/tmp/cvs-serv29478/src/com/meterware/servletunit
Modified Files:
InvocationContext.java InvocationContextImpl.java
RequestDispatcherImpl.java ServletUnitHttpRequest.java
ServletUnitServletContext.java
Added Files:
RequestContext.java
Log Message:
Support parameter overrides in request dispatcher
***** Error reading new file[Errno 2] No such file or directory: 'RequestContext.java'
Index: InvocationContext.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/InvocationContext.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- InvocationContext.java 6 Mar 2002 04:59:52 -0000 1.5
+++ InvocationContext.java 11 Feb 2003 04:32:00 -0000 1.6
@@ -26,6 +26,8 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
+import javax.servlet.RequestDispatcher;
+
import java.io.IOException;
@@ -67,5 +69,17 @@
* Returns the target for the original request.
*/
String getTarget();
+
+
+ /**
+ * Adds a request dispatcher to this context.
+ */
+ void pushIncludedContext( RequestDispatcher rd );
+
+
+ /**
+ * Removes the top request dispatcher from this context.
+ */
+ void popContext();
}
Index: InvocationContextImpl.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/InvocationContextImpl.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- InvocationContextImpl.java 21 Oct 2002 13:34:19 -0000 1.9
+++ InvocationContextImpl.java 11 Feb 2003 04:32:00 -0000 1.10
@@ -32,6 +32,7 @@
import javax.servlet.Servlet;
import javax.servlet.ServletException;
+import javax.servlet.RequestDispatcher;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -106,6 +107,16 @@
*/
public String getTarget() {
return _target;
+ }
+
+
+ public void pushIncludedContext( RequestDispatcher rd ) {
+ _request.pushRequestContext( (RequestContext) rd );
+ }
+
+
+ public void popContext() {
+ _request.popRequestContext();
}
Index: RequestDispatcherImpl.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/RequestDispatcherImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RequestDispatcherImpl.java 22 Jan 2002 15:25:16 -0000 1.1
+++ RequestDispatcherImpl.java 11 Feb 2003 04:32:00 -0000 1.2
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
- * Copyright (c) 2002, Russell Gold
+ * Copyright (c) 2002-2003, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -20,6 +20,7 @@
*
*******************************************************************************************************************/
import java.io.IOException;
+import java.net.URL;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletRequest;
@@ -30,15 +31,16 @@
/**
*
- * @author <a href="mailto:rus...@ac...">Russell Gold</a>
+ * @author <a href="mailto:rus...@ht...">Russell Gold</a>
**/
-class RequestDispatcherImpl implements RequestDispatcher {
+class RequestDispatcherImpl extends RequestContext implements RequestDispatcher {
private Servlet _servlet;
- public RequestDispatcherImpl( Servlet servlet ) {
- _servlet = servlet;
+ public RequestDispatcherImpl( WebApplication application, URL url ) throws ServletException {
+ super( url );
+ _servlet = application.getServletRequest( url ).getServlet();
}
Index: ServletUnitHttpRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitHttpRequest.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- ServletUnitHttpRequest.java 6 Feb 2003 01:33:11 -0000 1.21
+++ ServletUnitHttpRequest.java 11 Feb 2003 04:32:00 -0000 1.22
@@ -19,7 +19,6 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import com.meterware.httpunit.HttpUnitUtils;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebClient;
import com.meterware.httpunit.Base64;
@@ -47,12 +46,15 @@
private String _contentType;
private Vector _locales;
private boolean _secure;
+ private RequestContext _requestContext;
/**
* Constructs a ServletUnitHttpRequest from a WebRequest object.
**/
ServletUnitHttpRequest( ServletRequest servletRequest, WebRequest request, ServletUnitContext context, Dictionary clientHeaders, byte[] messageBody ) throws MalformedURLException {
+ if (context == null) throw new IllegalArgumentException( "Context must not be null" );
+
_servletRequest = servletRequest;
_request = request;
_context = context;
@@ -62,13 +64,10 @@
_messageBody = messageBody;
_secure = request.getURL().getProtocol().equalsIgnoreCase( "https" );
_contentType = (String) _headers.get( "Content-Type" );
- if (context == null) throw new IllegalArgumentException( "Context must not be null" );
- String file = request.getURL().getFile();
- if (file.indexOf( '?' ) >= 0) loadParameters( file.substring( file.indexOf( '?' )+1 ) );
- if (_messageBody == null) return;
- if (_contentType == null || _contentType.indexOf( "x-www-form-urlencoded" ) >= 0 ) {
- loadParameters( new String( _messageBody ) );
+ _requestContext = new RequestContext( request.getURL() );
+ if (_messageBody != null && (_contentType == null || _contentType.indexOf( "x-www-form-urlencoded" ) >= 0 )) {
+ _requestContext.loadParameters( new String( _messageBody ) );
}
}
@@ -222,13 +221,10 @@
* Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
**/
public String getRequestURI() {
- try {
- return _request.getURL().getPath();
- } catch (MalformedURLException e) {
- return null;
- }
+ return _requestContext.getRequestURI();
}
+
/**
* Returns the current HttpSession associated with this request or, if there is no current session
* and create is true, returns a new session.
@@ -357,7 +353,7 @@
* been read.
**/
public Enumeration getParameterNames() {
- return _parameters.keys();
+ return _requestContext.getParameterNames();
}
@@ -391,7 +387,7 @@
* containing the values of a query string or posted form.
**/
public String[] getParameterValues( String name ) {
- return (String[]) _parameters.get( name );
+ return _requestContext.getParameterValues( name );
}
@@ -612,7 +608,7 @@
* but does not end with a "/" character. For servlets in the default (root) context,
* this method returns "".
**/
- public java.lang.String getContextPath() {
+ public String getContextPath() {
return _context.getContextPath();
}
@@ -627,7 +623,7 @@
* @since 1.3
**/
public Map getParameterMap() {
- return _parameters;
+ return _requestContext.getParameterMap();
}
@@ -732,6 +728,17 @@
}
+ void pushRequestContext( RequestContext requestContext ) {
+ requestContext.setParentContext( _requestContext );
+ _requestContext = requestContext;
+ }
+
+
+ void popRequestContext() {
+ _requestContext = _requestContext.getParentContext();
+ }
+
+
//--------------------------------------------- private members ----------------------------------------------
@@ -745,7 +752,6 @@
private ServletUnitContext _context;
private ServletUnitHttpSession _session;
private Hashtable _attributes = new Hashtable();
- private Hashtable _parameters = new Hashtable();
private Vector _cookies = new Vector();
private String _sessionID;
private byte[] _messageBody;
@@ -754,75 +760,6 @@
private String[] _roles;
- final static private int STATE_INITIAL = 0;
- final static private int STATE_HAVE_NAME = 1;
- final static private int STATE_HAVE_EQUALS = 2;
- final static private int STATE_HAVE_VALUE = 3;
-
-
- /**
- * This method employs a state machine to parse a parameter query string.
- * The transition rules are as follows:
- * State \ text '=' '&'
- * initial: have_name - initial
- * have_name: - have_equals initial
- * have_equals: have_value - initial
- * have_value: - initial initial
- * actions occur on the following transitions:
- * initial -> have_name: save token as name
- * have_equals -> initial: record parameter with null value
- * have_value -> initial: record parameter with value
- **/
- private void loadParameters( String queryString ) {
- if (queryString.length() == 0) return;
- StringTokenizer st = new StringTokenizer( queryString, "&=", /* return tokens */ true );
- int state = STATE_INITIAL;
- String name = null;
- String value = null;
-
- while (st.hasMoreTokens()) {
- String token = st.nextToken();
- if (token.equals( "&" )) {
- state = STATE_INITIAL;
- if (name != null && value != null) addParameter( name, value );
- name = value = null;
- } else if (token.equals( "=" )) {
- if (state == STATE_HAVE_NAME) {
- state = STATE_HAVE_EQUALS;
- } else if (state == STATE_HAVE_VALUE) {
- state = STATE_INITIAL;
- }
- } else if (state == STATE_INITIAL) {
- name = HttpUnitUtils.decode( token );
- value = "";
- state = STATE_HAVE_NAME;
- } else {
- value = HttpUnitUtils.decode( token );
- state = STATE_HAVE_VALUE;
- }
- }
- if (name != null && value != null) addParameter( name, value );
- }
-
-
- private void addParameter( String name, String encodedValue ) {
- String[] values = (String[]) _parameters.get( name );
- if (values == null) {
- _parameters.put( name, new String[] { encodedValue } );
- } else {
- _parameters.put( name, extendedArray( values, encodedValue ) );
- }
- }
-
-
- private String[] extendedArray( String[] baseArray, String newValue ) {
- String[] result = new String[ baseArray.length+1 ];
- System.arraycopy( baseArray, 0, result, 0, baseArray.length );
- result[ baseArray.length ] = newValue;
- return result;
- }
-
-
private void throwNotImplementedYet() {
throw new RuntimeException( "Not implemented yet" );
}
@@ -871,3 +808,5 @@
}
}
+
+
Index: ServletUnitServletContext.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitServletContext.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- ServletUnitServletContext.java 6 Feb 2003 01:33:11 -0000 1.11
+++ ServletUnitServletContext.java 11 Feb 2003 04:32:01 -0000 1.12
@@ -152,7 +152,7 @@
public javax.servlet.RequestDispatcher getRequestDispatcher( String path ) {
try {
URL url = new URL( "http", "localhost", _application.getContextPath() + path );
- return new RequestDispatcherImpl( _application.getServletRequest( url ).getServlet() );
+ return new RequestDispatcherImpl( _application, url );
} catch (ServletException e) {
return null;
} catch (MalformedURLException e) {
|