[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit ServletUnitHttpResponse.java,1.9,1.10
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-01-30 21:29:54
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit
In directory usw-pr-cvs1:/tmp/cvs-serv15077/src/com/meterware/servletunit
Modified Files:
ServletUnitHttpResponse.java ServletUnitServletConfig.java
ServletUnitServletContext.java WebApplication.java
Log Message:
from Alex Chaffee: implemented context parameters in ServletUnit
Index: ServletUnitHttpResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitHttpResponse.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- ServletUnitHttpResponse.java 2002/01/22 15:25:16 1.9
+++ ServletUnitHttpResponse.java 2002/01/30 21:21:11 1.10
@@ -5,12 +5,12 @@
*
* Copyright (c) 2000-2001, 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
+* 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
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
-* The above copyright notice and this permission notice shall be included in all copies or substantial portions
+* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
@@ -110,6 +110,7 @@
* Relative URLs are not permitted here.
*/
public void sendRedirect( String location ) throws IOException {
+ setStatus( HttpServletResponse.SC_MOVED_PERMANENTLY );
setHeader( "Location", location );
}
@@ -162,8 +163,8 @@
/**
- * @deprecated As of version 2.1, due to ambiguous meaning of the message parameter.
- * To set a status code use setStatus(int), to send an error with a description
+ * @deprecated As of version 2.1, due to ambiguous meaning of the message parameter.
+ * To set a status code use setStatus(int), to send an error with a description
* use sendError(int, String). Sets the status code and message for this response.
**/
public void setStatus( int sc, String msg ) {
@@ -325,10 +326,10 @@
throw new RuntimeException( "addDateHeader not implemented" );
}
-
+
/**
- * Sets the preferred buffer size for the body of the response. The servlet container
- * will use a buffer at least as large as the size requested. The actual buffer size
+ * Sets the preferred buffer size for the body of the response. The servlet container
+ * will use a buffer at least as large as the size requested. The actual buffer size
* used can be found using getBufferSize.
**/
public void setBufferSize( int size ) { // XXX throw IllegalStateException if anything has been written
@@ -338,32 +339,32 @@
/**
* Returns the actual buffer size used for the response. If no buffering is used, this method returns 0.
- **/
+ **/
public int getBufferSize() {
return 0;
}
/**
- * Returns a boolean indicating if the response has been committed. A commited response has
+ * Returns a boolean indicating if the response has been committed. A commited response has
* already had its status code and headers written.
**/
public boolean isCommitted() {
return false; // XXX set true if flushBuffer has been called
}
-
-
+
+
/**
- * Forces any content in the buffer to be written to the client. A call to this method automatically
+ * Forces any content in the buffer to be written to the client. A call to this method automatically
* commits the response, meaning the status code and headers will be written.
**/
public void flushBuffer() throws IOException {
throw new RuntimeException( "flushBuffer not implemented" );
}
-
+
/**
- * Clears any data that exists in the buffer as well as the status code and headers.
+ * Clears any data that exists in the buffer as well as the status code and headers.
* If the response has been committed, this method throws an IllegalStateException.
**/
public void reset() {
@@ -372,23 +373,23 @@
/**
- * Sets the locale of the response, setting the headers (including the Content-Type's charset)
- * as appropriate. This method should be called before a call to getWriter().
+ * Sets the locale of the response, setting the headers (including the Content-Type's charset)
+ * as appropriate. This method should be called before a call to getWriter().
* By default, the response locale is the default locale for the server.
**/
public void setLocale( Locale locale ) {
throw new RuntimeException( "setLocale not implemented" );
}
+
-
/**
* Returns the locale assigned to the response.
**/
public Locale getLocale() {
throw new RuntimeException( "getLocale not implemented" );
}
+
-
//----------------------------- methods added to ServletResponse in JSDK 2.3 --------------------------------------
Index: ServletUnitServletConfig.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitServletConfig.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ServletUnitServletConfig.java 2002/01/22 15:25:16 1.3
+++ ServletUnitServletConfig.java 2002/01/30 21:21:11 1.4
@@ -4,12 +4,12 @@
*
* Copyright (c) 2000-2002, 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
+* 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
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
-* The above copyright notice and this permission notice shall be included in all copies or substantial portions
+* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
@@ -33,10 +33,10 @@
class ServletUnitServletConfig implements ServletConfig {
- ServletUnitServletConfig( Servlet servlet, WebApplication application, Hashtable initParams ) {
+ ServletUnitServletConfig( Servlet servlet, WebApplication application, Hashtable initParams, Hashtable contextParams ) {
_name = servlet.getClass().getName();
_initParameters = initParams;
- _context = new ServletUnitServletContext( application );
+ _context = new ServletUnitServletContext( application, contextParams );
}
Index: ServletUnitServletContext.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitServletContext.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ServletUnitServletContext.java 2002/01/22 15:25:16 1.6
+++ ServletUnitServletContext.java 2002/01/30 21:21:11 1.7
@@ -4,12 +4,12 @@
*
* Copyright (c) 2000-2002, 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
+* 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
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
-* The above copyright notice and this permission notice shall be included in all copies or substantial portions
+* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
@@ -39,13 +39,14 @@
**/
class ServletUnitServletContext implements ServletContext {
- public ServletUnitServletContext( WebApplication application ) {
+ ServletUnitServletContext( WebApplication application, Hashtable contextParams ) {
_application = application;
+ _contextParams = contextParams;
}
/**
- * Returns a ServletContext object that corresponds to a specified URL on the server.
+ * Returns a ServletContext object that corresponds to a specified URL on the server.
* <p>
* This method allows servlets to gain access to the context for various parts of the server,
* and as needed obtain RequestDispatcher objects from the context. The given path must be
@@ -59,7 +60,7 @@
/**
- * Returns the major version of the Java Servlet API that this servlet container supports.
+ * Returns the major version of the Java Servlet API that this servlet container supports.
* All implementations that comply with Version 2.3 must have this method return the integer 2.
**/
public int getMajorVersion() {
@@ -68,7 +69,7 @@
/**
- * Returns the minor version of the Servlet API that this servlet container supports.
+ * Returns the minor version of the Servlet API that this servlet container supports.
* All implementations that comply with Version 2.3 must have this method return the integer 3.
**/
public int getMinorVersion() {
@@ -77,8 +78,8 @@
/**
- * Returns the MIME type of the specified file, or null if the MIME type is not known.
- * The MIME type is determined by the configuration of the servlet container, and
+ * Returns the MIME type of the specified file, or null if the MIME type is not known.
+ * The MIME type is determined by the configuration of the servlet container, and
* may be specified in a web application deployment descriptor. Common MIME types are
* "text/html" and "image/gif".
**/
@@ -88,20 +89,20 @@
/**
- * Returns a URL to the resource that is mapped to a specified path. The path must begin
- * with a "/" and is interpreted as relative to the current context root.
+ * Returns a URL to the resource that is mapped to a specified path. The path must begin
+ * with a "/" and is interpreted as relative to the current context root.
* <p>
- * This method allows the servlet container to make a resource available to servlets from any source.
- * Resources can be located on a local or remote file system, in a database, or in a .war file.
+ * This method allows the servlet container to make a resource available to servlets from any source.
+ * Resources can be located on a local or remote file system, in a database, or in a .war file.
* <p>
- * The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.
+ * The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.
* <p>
- * This method returns null if no resource is mapped to the pathname.
+ * This method returns null if no resource is mapped to the pathname.
*
- * Some containers may allow writing to the URL returned by this method using the methods of the URL class.
+ * Some containers may allow writing to the URL returned by this method using the methods of the URL class.
*
* The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a
- * RequestDispatcher instead to include results of an execution.
+ * RequestDispatcher instead to include results of an execution.
*
* This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This
* method does not use class loaders.
@@ -112,14 +113,14 @@
/**
- * Returns the resource located at the named path as an InputStream object.
+ * Returns the resource located at the named path as an InputStream object.
*
* The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource.
- * This method returns null if no resource exists at the specified path.
+ * This method returns null if no resource exists at the specified path.
- * Meta-information such as content length and content type that is available via getResource method is lost when using this method.
+ * Meta-information such as content length and content type that is available via getResource method is lost when using this method.
- * The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.
+ * The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.
* This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet
* containers to make a resource available to a servlet from any location, without using a class loader.
@@ -149,10 +150,10 @@
/**
- * Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
- *
+ * Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
+ *
* Servlets (and JSP pages also) may be given names via server administration or via a web application deployment descriptor. A servlet
- * instance can determine its name using ServletConfig.getServletName().
+ * instance can determine its name using ServletConfig.getServletName().
*
* This method returns null if the ServletContext cannot return a RequestDispatcher for any reason.
**/
@@ -186,7 +187,7 @@
/**
- * Writes the specified message to a servlet log file, usually an event log.
+ * Writes the specified message to a servlet log file, usually an event log.
* The name and type of the servlet log file is specific to the servlet container.
**/
public void log( String message ) { // XXX change this to use something testable
@@ -202,7 +203,7 @@
/**
- * Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file.
+ * Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file.
* The name and type of the servlet log file is specific to the servlet container, usually an event log.
**/
public void log( String message, Throwable t ) {
@@ -213,7 +214,7 @@
/**
* Returns a String containing the real path for a given virtual path. For example, the virtual path "/index.html" has a real path of
- * whatever file on the server's filesystem would be served by a request for "/index.html".
+ * whatever file on the server's filesystem would be served by a request for "/index.html".
*
* The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running,
* including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for
@@ -225,10 +226,10 @@
/**
- * Returns the name and version of the servlet container on which the servlet is running.
+ * Returns the name and version of the servlet container on which the servlet is running.
* The form of the returned string is servername/versionnumber. For example, the JavaServer Web Development Kit may return the
- * string JavaServer Web Dev Kit/1.0.
+ * string JavaServer Web Dev Kit/1.0.
* The servlet container may return other optional information after the primary string in parentheses, for example, JavaServer Web
* Dev Kit/1.0 (JDK 1.1.6; Windows NT 4.0 x86).
@@ -239,31 +240,31 @@
/**
- * Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
+ * Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
*
* This method can make available configuration information useful to an entire "web application". For example, it can provide a
* webmaster's email address or the name of a system that holds critical data.
**/
public java.lang.String getInitParameter( String name ) {
- return null; // XXX not implemented
- }
+ return (String) _contextParams.get( name );
+ }
/**
- * Returns the names of the context's initialization parameters as an Enumeration of String objects,
+ * Returns the names of the context's initialization parameters as an Enumeration of String objects,
* or an empty Enumeration if the context has no initialization parameters.
**/
public java.util.Enumeration getInitParameterNames() {
- return EMPTY_VECTOR.elements(); // XXX not implemented
- }
+ return _contextParams.keys();
+ }
/**
- * Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
- * An attribute allows a servlet container to give the servlet additional information not already
+ * Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
+ * An attribute allows a servlet container to give the servlet additional information not already
* provided by this interface. See your server documentation for information
* about its attributes. A list of supported attributes can be retrieved using getAttributeNames.
- **/
+ **/
public Object getAttribute( String name ) {
return _attributes.get( name );
}
@@ -325,6 +326,18 @@
return null;
}
+//------------------------------------------- package members ----------------------------------------------------
+
+
+ void setInitParameter( String name, Object initParameter ) {
+ _contextParams.put( name, initParameter );
+ }
+
+
+ void removeInitParameter( String name ) {
+ _contextParams.remove( name );
+ }
+
//------------------------------------------- private members ----------------------------------------------------
@@ -332,4 +345,5 @@
private Hashtable _attributes = new Hashtable();
private WebApplication _application;
+ private Hashtable _contextParams = new Hashtable();
}
Index: WebApplication.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/WebApplication.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WebApplication.java 2002/01/22 15:25:16 1.3
+++ WebApplication.java 2002/01/30 21:21:11 1.4
@@ -70,6 +70,7 @@
_securityConstraints.add( new SecurityConstraintImpl( (Element) nl.item(i) ) );
}
+ extractContextParameters( document );
extractLoginConfiguration( document );
}
@@ -98,7 +99,7 @@
if (!Servlet.class.isAssignableFrom( servletClass )) throw new HttpInternalErrorException( url );
Servlet servlet = (Servlet) servletClass.newInstance(); // XXX cache instances - by class?
- servlet.init( new ServletUnitServletConfig( servlet, this, configuration.getInitParams() ) );
+ servlet.init( new ServletUnitServletConfig( servlet, this, configuration.getInitParams(), _contextParameters ) );
return servlet;
} catch (ClassNotFoundException e) {
throw new HttpNotFoundException( url, e );
@@ -201,6 +202,7 @@
private URL _errorURL;
+ private Hashtable _contextParameters = new Hashtable();
final static private SecurityConstraint NULL_SECURITY_CONSTRAINT = new NullSecurityConstraint();
@@ -243,6 +245,17 @@
private void registerServlet( Dictionary mapping, Element servletElement ) throws SAXException {
registerServlet( getChildNodeValue( servletElement, "url-pattern" ),
(ServletConfiguration) mapping.get( getChildNodeValue( servletElement, "servlet-name" ) ) );
+ }
+
+
+ private void extractContextParameters( Document document ) throws SAXException {
+ NodeList nl = document.getElementsByTagName( "context-param" );
+ for (int i = 0; i < nl.getLength(); i++) {
+ Element param = (Element)nl.item(i);
+ String name = getChildNodeValue(param, "param-name");
+ String value = getChildNodeValue(param, "param-value");
+ _contextParameters.put(name, value);
+ }
}
|