[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit ServletRunner.java,1.13,1.14 WebApplic
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-05-08 20:08:11
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit In directory usw-pr-cvs1:/tmp/cvs-serv15716/src/com/meterware/servletunit Modified Files: ServletRunner.java WebApplication.java Log Message: Remove superfluous setting of context directory Index: ServletRunner.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletRunner.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- ServletRunner.java 1 Mar 2002 06:08:24 -0000 1.13 +++ ServletRunner.java 8 May 2002 19:20:59 -0000 1.14 @@ -60,10 +60,12 @@ * * @param webXMLFileSpec the full path to the web.xml file */ - public ServletRunner( String webXMLFileSpec) throws IOException, SAXException { - this(webXMLFileSpec, null); + public ServletRunner( String webXMLFileSpec ) throws IOException, SAXException { + _context = new ServletUnitContext(); + _application = new WebApplication( HttpUnitUtils.newParser().parse( webXMLFileSpec ) ); } + /** * Constructor which expects the full path to the web.xml for the * application and a context path under which to mount it. @@ -72,10 +74,11 @@ * @param contextPath the context path */ public ServletRunner( String webXMLFileSpec, String contextPath ) throws IOException, SAXException { - _context = new ServletUnitContext(contextPath); - File webXMLFile = new File(webXMLFileSpec); - _application = new WebApplication(HttpUnitUtils.newParser().parse( webXMLFileSpec ), webXMLFile.getParentFile().getParentFile(), _context.getContextPath()); + _context = new ServletUnitContext( contextPath ); + File webXMLFile = new File( webXMLFileSpec ); + _application = new WebApplication( HttpUnitUtils.newParser().parse( webXMLFileSpec ), webXMLFile.getParentFile().getParentFile(), _context.getContextPath() ); } + /** * Constructor which expects an input stream containing the web.xml for the application. Index: WebApplication.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/WebApplication.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- WebApplication.java 1 Mar 2002 06:08:24 -0000 1.5 +++ WebApplication.java 8 May 2002 19:20:59 -0000 1.6 @@ -24,11 +24,8 @@ import java.io.File; import java.io.IOException; -import java.io.PrintWriter; - -import java.net.URL; import java.net.MalformedURLException; - +import java.net.URL; import java.util.ArrayList; import java.util.Dictionary; import java.util.HashMap; @@ -50,14 +47,13 @@ /** - * This class represents the information recorded about a single web + * This class represents the information recorded about a single web * application. It is usually extracted from web.xml. * * @author <a href="mailto:rus...@ac...">Russell Gold</a> **/ class WebApplication { - /** * Constructs a default application spec with no information. */ @@ -65,42 +61,44 @@ _contextPath = ""; } - /** - * Constructs an application spec from an XML document. - */ - WebApplication(Document document) throws MalformedURLException,SAXException { - this(document, null, ""); - } /** * Constructs an application spec from an XML document. */ - WebApplication(Document document, String contextPath) throws MalformedURLException,SAXException { - this(document, null, contextPath); + WebApplication( Document document ) throws MalformedURLException, SAXException { + this( document, null, "" ); } + /** * Constructs an application spec from an XML document. */ - WebApplication(Document document, File file) throws MalformedURLException,SAXException { - this(document, null, ""); + WebApplication( Document document, String contextPath ) throws MalformedURLException, SAXException { + this( document, null, contextPath ); } + /** * Constructs an application spec from an XML document. */ - WebApplication(Document document, File file, String contextPath) throws MalformedURLException,SAXException { + WebApplication( Document document, File file, String contextPath ) throws MalformedURLException, SAXException { _contextDir = file; _contextPath = contextPath; registerServlets( document ); + extractSecurityConstraints( document ); + extractContextParameters( document ); + extractLoginConfiguration( document ); + } + + + private void extractSecurityConstraints( Document document ) throws SAXException { NodeList nl = document.getElementsByTagName( "security-constraint" ); for (int i = 0; i < nl.getLength(); i++) { - _securityConstraints.add( new SecurityConstraintImpl( (Element) nl.item(i) ) ); + _securityConstraints.add( new SecurityConstraintImpl( (Element) nl.item( i ) ) ); } - extractContextParameters( document ); - extractLoginConfiguration( document ); } + /** * Registers a servlet class to be run. **/ @@ -114,10 +112,10 @@ **/ void registerServlet( String resourceName, ServletConfiguration servletConfiguration ) { // FIXME - shouldn't everything start with one or the other? - if (!resourceName.startsWith("/") && !resourceName.startsWith("*")) { - resourceName = "/"+resourceName; + if (!resourceName.startsWith( "/" ) && !resourceName.startsWith( "*" )) { + resourceName = "/" + resourceName; } - _servletMapping.put( resourceName, servletConfiguration); + _servletMapping.put( resourceName, servletConfiguration ); } @@ -142,8 +140,8 @@ } - private ServletConfiguration getServletConfiguration(URL url) { - if (!url.getFile().startsWith(_contextPath)) { + private ServletConfiguration getServletConfiguration( URL url ) { + if (!url.getFile().startsWith( _contextPath )) { return null; } String servletName = getServletName( getURLPath( url ) ); @@ -157,10 +155,10 @@ private String getURLPath( URL url ) { String file = url.getFile(); - if (_contextPath.equals("")) { + if (_contextPath.equals( "" )) { return file; - } else if (file.startsWith(_contextPath)) { - return file.substring(_contextPath.length()); + } else if (file.startsWith( _contextPath )) { + return file.substring( _contextPath.length() ); } else { return null; } @@ -252,11 +250,10 @@ final static private SecurityConstraint NULL_SECURITY_CONSTRAINT = new NullSecurityConstraint(); - - private void extractLoginConfiguration( Document document ) throws MalformedURLException,SAXException { + private void extractLoginConfiguration( Document document ) throws MalformedURLException, SAXException { NodeList nl = document.getElementsByTagName( "login-config" ); if (nl.getLength() == 1) { - final Element loginConfigElement = (Element) nl.item(0); + final Element loginConfigElement = (Element) nl.item( 0 ); String authenticationMethod = getChildNodeValue( loginConfigElement, "auth-method", "BASIC" ); _authenticationRealm = getChildNodeValue( loginConfigElement, "realm-name", "" ); if (authenticationMethod.equalsIgnoreCase( "BASIC" )) { @@ -275,9 +272,9 @@ private void registerServlets( Document document ) throws SAXException { Hashtable nameToClass = new Hashtable(); NodeList nl = document.getElementsByTagName( "servlet" ); - for (int i = 0; i < nl.getLength(); i++) registerServletClass( nameToClass, (Element) nl.item(i) ); + for (int i = 0; i < nl.getLength(); i++) registerServletClass( nameToClass, (Element) nl.item( i ) ); nl = document.getElementsByTagName( "servlet-mapping" ); - for (int i = 0; i < nl.getLength(); i++) registerServlet( nameToClass, (Element) nl.item(i) ); + for (int i = 0; i < nl.getLength(); i++) registerServlet( nameToClass, (Element) nl.item( i ) ); } @@ -294,13 +291,13 @@ 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); - } + 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 ); + } } @@ -312,7 +309,7 @@ private static String getChildNodeValue( Element root, String childNodeName, String defaultValue ) throws SAXException { NodeList nl = root.getElementsByTagName( childNodeName ); if (nl.getLength() == 1) { - return getTextValue( nl.item(0) ); + return getTextValue( nl.item( 0 ) ); } else if (defaultValue == null) { throw new SAXException( "Node <" + root.getNodeName() + "> has no child named <" + childNodeName + ">" ); } else { @@ -355,6 +352,7 @@ //============================================= SecurityCheckServlet class ============================================= static class SecurityCheckServlet extends HttpServlet { + protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { handleLogin( (ServletUnitHttpRequest) req, resp ); } @@ -386,9 +384,9 @@ ServletConfiguration( Element servletElement ) throws SAXException { this( getChildNodeValue( servletElement, "servlet-class" ) ); final NodeList initParams = servletElement.getElementsByTagName( "init-param" ); - for (int i = initParams.getLength()-1; i >= 0; i--) { - _initParams.put( getChildNodeValue( (Element) initParams.item(i), "param-name" ), - getChildNodeValue( (Element) initParams.item(i), "param-value" ) ); + for (int i = initParams.getLength() - 1; i >= 0; i--) { + _initParams.put( getChildNodeValue( (Element) initParams.item( i ), "param-name" ), + getChildNodeValue( (Element) initParams.item( i ), "param-value" ) ); } } @@ -403,7 +401,7 @@ } - private String _className; + private String _className; private Hashtable _initParams = new Hashtable(); } @@ -412,37 +410,52 @@ interface SecurityConstraint { + boolean controlsPath( String urlPath ); + + boolean hasRole( String roleName ); } static class NullSecurityConstraint implements SecurityConstraint { - public boolean controlsPath( String urlPath ) { return false; } - public boolean hasRole( String roleName ) { return true; } + + public boolean controlsPath( String urlPath ) { + return false; + } + + + public boolean hasRole( String roleName ) { + return true; + } } static class SecurityConstraintImpl implements SecurityConstraint { + SecurityConstraintImpl( Element root ) throws SAXException { final NodeList roleNames = root.getElementsByTagName( "role-name" ); - for (int i = 0; i < roleNames.getLength(); i++) _roles.add( getTextValue( (Element) roleNames.item(i) ) ); + for (int i = 0; i < roleNames.getLength(); i++) _roles.add( getTextValue( (Element) roleNames.item( i ) ) ); final NodeList resources = root.getElementsByTagName( "web-resource-collection" ); - for (int i = 0; i < resources.getLength(); i++) _resources.add( new WebResourceCollection( (Element) resources.item(i) ) ); + for (int i = 0; i < resources.getLength(); i++) _resources.add( new WebResourceCollection( (Element) resources.item( i ) ) ); } + public boolean controlsPath( String urlPath ) { return getMatchingCollection( urlPath ) != null; } + public boolean hasRole( String roleName ) { return _roles.contains( roleName ); } - private ArrayList _roles = new ArrayList(); + + private ArrayList _roles = new ArrayList(); private ArrayList _resources = new ArrayList(); + public WebResourceCollection getMatchingCollection( String urlPath ) { for (Iterator i = _resources.iterator(); i.hasNext();) { WebResourceCollection wrc = (WebResourceCollection) i.next(); @@ -451,12 +464,15 @@ return null; } + class WebResourceCollection { + WebResourceCollection( Element root ) throws SAXException { final NodeList urlPatterns = root.getElementsByTagName( "url-pattern" ); - for (int i = 0; i < urlPatterns.getLength(); i++) _urlPatterns.add( getTextValue( (Element) urlPatterns.item(i) ) ); + for (int i = 0; i < urlPatterns.getLength(); i++) _urlPatterns.add( getTextValue( (Element) urlPatterns.item( i ) ) ); } + boolean controlsPath( String urlPath ) { for (Iterator i = _urlPatterns.iterator(); i.hasNext();) { String pattern = (String) i.next(); @@ -465,10 +481,12 @@ return false; } - private ArrayList _urlPatterns = new ArrayList(); + + private ArrayList _urlPatterns = new ArrayList(); } } + /** * A utility class for mapping servlets to url patterns. This implements the * matching algorithm documented in section 10 of the JSDK-2.2 reference. @@ -477,66 +495,69 @@ * @version $Revision$ */ class ServletMapping { - + private final Map exactMatches = new HashMap(); private final Map extensions = new HashMap(); private final Map urlTree = new HashMap(); - - void put(String mapping, ServletConfiguration servletConfiguration) { - if (mapping.indexOf('*') == -1) { - exactMatches.put(mapping, servletConfiguration); - } else if (mapping.startsWith("*.")) { - extensions.put(mapping.substring(2), servletConfiguration); + + + void put( String mapping, ServletConfiguration servletConfiguration ) { + if (mapping.indexOf( '*' ) == -1) { + exactMatches.put( mapping, servletConfiguration ); + } else if (mapping.startsWith( "*." )) { + extensions.put( mapping.substring( 2 ), servletConfiguration ); } else { - ParsedPath path = new ParsedPath(mapping); + ParsedPath path = new ParsedPath( mapping ); Map context = urlTree; while (path.hasNext()) { String part = path.next(); - if (part.equals("*")) { - context.put("*", servletConfiguration); + if (part.equals( "*" )) { + context.put( "*", servletConfiguration ); return; } - if (!context.containsKey(part)) { - context.put(part, new HashMap()); + if (!context.containsKey( part )) { + context.put( part, new HashMap() ); } - context = (Map)context.get(part); + context = (Map) context.get( part ); } - context.put("/", servletConfiguration); + context.put( "/", servletConfiguration ); } } - - ServletConfiguration get(String url) { - if (exactMatches.containsKey(url)) { - return (ServletConfiguration)exactMatches.get(url); + + + ServletConfiguration get( String url ) { + if (exactMatches.containsKey( url )) { + return (ServletConfiguration) exactMatches.get( url ); } - ParsedPath path = new ParsedPath(url); + ParsedPath path = new ParsedPath( url ); Map context = urlTree; while (path.hasNext()) { String part = path.next(); - if (!context.containsKey(part)) { - if (context.containsKey("*")) { - return (ServletConfiguration)context.get("*"); + if (!context.containsKey( part )) { + if (context.containsKey( "*" )) { + return (ServletConfiguration) context.get( "*" ); } else { - int index = url.lastIndexOf('.'); - if (index == -1 || index == url.length()-1) { + int index = url.lastIndexOf( '.' ); + if (index == -1 || index == url.length() - 1) { return null; } else { - return (ServletConfiguration)extensions.get(url.substring(index+1)); + return (ServletConfiguration) extensions.get( url.substring( index + 1 ) ); } } } - context = (Map)context.get(part); + context = (Map) context.get( part ); } - if (context.containsKey("*")) { - return (ServletConfiguration)context.get("*"); + if (context.containsKey( "*" )) { + return (ServletConfiguration) context.get( "*" ); } - return (ServletConfiguration)context.get("/"); + return (ServletConfiguration) context.get( "/" ); } - + } } + /** * A utility class for parsing URLs into paths * @@ -548,18 +569,20 @@ private int position = 0; static final char seperator_char = '/'; + /** * Creates a new parsed path for the given path value * * @param path the path */ - ParsedPath(String path) { - if (path.charAt(0) != seperator_char) { - throw new IllegalArgumentException("Illegal path '"+path+"', does not begin with "+seperator_char); + ParsedPath( String path ) { + if (path.charAt( 0 ) != seperator_char) { + throw new IllegalArgumentException( "Illegal path '" + path + "', does not begin with " + seperator_char ); } this.path = path; } + /** * Returns true if there are more parts left, otherwise false */ @@ -567,15 +590,16 @@ return (position < path.length()); } + /** * Returns the next part in the path */ public final String next() { - int offset = position+1; - while (offset < path.length() && path.charAt(offset) != seperator_char) { + int offset = position + 1; + while (offset < path.length() && path.charAt( offset ) != seperator_char) { offset++; } - String result = path.substring(position+1, offset); + String result = path.substring( position + 1, offset ); position = offset; return result; } |