[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit ServletRequest.java,NONE,1.1 Invocatio
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-06-19 13:47:25
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit In directory usw-pr-cvs1:/tmp/cvs-serv14187/src/com/meterware/servletunit Modified Files: InvocationContextImpl.java ServletUnitHttpRequest.java ServletUnitServletConfig.java ServletUnitServletContext.java WebApplication.java Added Files: ServletRequest.java Log Message: implemented getServerPath and getPathInfo ***** Error reading new file[Errno 2] No such file or directory: 'ServletRequest.java' Index: InvocationContextImpl.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/InvocationContextImpl.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- InvocationContextImpl.java 6 Mar 2002 04:59:52 -0000 1.4 +++ InvocationContextImpl.java 19 Jun 2002 13:47:19 -0000 1.5 @@ -67,13 +67,13 @@ public Servlet getServlet() throws ServletException { if (_servlet == null) { if (!_application.requiresAuthorization( _requestURL ) || userIsAuthorized() ) { - _servlet = _application.getServlet( _requestURL ); + _servlet = _application.getServletRequest( _requestURL ).getServlet(); } else if (_request.getRemoteUser() != null) { throw new AccessDeniedException( _requestURL ); } else if (_application.usesBasicAuthentication()) { throw new BasicAuthenticationRequiredException( _application.getAuthenticationRealm() ); } else if (_application.usesFormAuthentication()) { - _servlet = _application.getServlet( _application.getLoginURL() ); + _servlet = _application.getServletRequest( _application.getLoginURL() ).getServlet(); ((ServletUnitHttpRequest) getRequest()).setOriginalURL( _requestURL ); } else { throw new IllegalStateException( "Authorization required but no authentication method defined" ); @@ -135,7 +135,8 @@ _requestURL = request.getURL(); _target = request.getTarget(); - _request = new ServletUnitHttpRequest( request, runner.getContext(), clientHeaders, messageBody ); + _request = new ServletUnitHttpRequest( _application.getServletRequest( _requestURL ), request, runner.getContext(), + clientHeaders, messageBody ); for (int i = 0; i < cookies.length; i++) _request.addCookie( cookies[i] ); if (_application.usesBasicAuthentication()) _request.readBasicAuthentication(); Index: ServletUnitHttpRequest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitHttpRequest.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- ServletUnitHttpRequest.java 21 May 2002 13:57:56 -0000 1.12 +++ ServletUnitHttpRequest.java 19 Jun 2002 13:47:19 -0000 1.13 @@ -57,7 +57,8 @@ /** * Constructs a ServletUnitHttpRequest from a WebRequest object. **/ - ServletUnitHttpRequest( WebRequest request, ServletUnitContext context, Dictionary clientHeaders, byte[] messageBody ) throws MalformedURLException { + ServletUnitHttpRequest( ServletRequest servletRequest, WebRequest request, ServletUnitContext context, Dictionary clientHeaders, byte[] messageBody ) throws MalformedURLException { + _servletRequest = servletRequest; _request = request; _context = context; _headers = new WebClient.HeaderDictionary(); @@ -165,8 +166,7 @@ * or a path to the servlet, but does not include any extra path information or a query string. **/ public String getServletPath() { - throwNotImplementedYet(); - return ""; + return _servletRequest.getServletPath(); } @@ -184,7 +184,7 @@ * This method returns null if there was no extra path information. **/ public String getPathInfo() { - return null; + return _servletRequest.getPathInfo(); } @@ -720,6 +720,7 @@ private WebRequest _request; + private ServletRequest _servletRequest; private WebClient.HeaderDictionary _headers; private ServletUnitContext _context; private ServletUnitHttpSession _session; Index: ServletUnitServletConfig.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitServletConfig.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ServletUnitServletConfig.java 1 Mar 2002 06:08:24 -0000 1.5 +++ ServletUnitServletConfig.java 19 Jun 2002 13:47:19 -0000 1.6 @@ -34,10 +34,10 @@ class ServletUnitServletConfig implements ServletConfig { - ServletUnitServletConfig( Servlet servlet, WebApplication application, Hashtable initParams, Hashtable contextParams, File contextDir ) { + ServletUnitServletConfig( Servlet servlet, WebApplication application, Hashtable initParams ) { _name = servlet.getClass().getName(); _initParameters = initParams; - _context = new ServletUnitServletContext( application, contextParams, contextDir ); + _context = new ServletUnitServletContext( application ); } Index: ServletUnitServletContext.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitServletContext.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- ServletUnitServletContext.java 1 Mar 2002 06:08:24 -0000 1.8 +++ ServletUnitServletContext.java 19 Jun 2002 13:47:19 -0000 1.9 @@ -23,6 +23,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.FileNotFoundException; import java.net.URL; import java.net.MalformedURLException; @@ -31,6 +32,7 @@ import java.util.Hashtable; import java.util.Vector; import java.util.Set; +import java.util.Map; import javax.servlet.*; @@ -41,10 +43,8 @@ **/ class ServletUnitServletContext implements ServletContext { - ServletUnitServletContext( WebApplication application, Hashtable contextParams, File contextDir ) { + ServletUnitServletContext( WebApplication application ) { _application = application; - _contextParams = contextParams; - _contextDir = contextDir; } @@ -111,15 +111,11 @@ * method does not use class loaders. **/ public java.net.URL getResource( String path ) { - if (_contextDir == null) { - return null; // no context, but maybe try against working dir? - } else { - try { - File resourceFile = new File(_contextDir, path.substring(1)); - return resourceFile.toURL(); - } catch (IOException e) { - return null; - } + try { + File resourceFile = _application.getResourceFile( path ); + return resourceFile == null ? null : resourceFile.toURL(); + } catch (MalformedURLException e) { + return null; } } @@ -138,15 +134,11 @@ * containers to make a resource available to a servlet from any location, without using a class loader. **/ public java.io.InputStream getResourceAsStream( String path ) { - if (_contextDir == null) { - return null; // no context, but maybe try against working dir? - } else { - try { - File resourceFile = new File(_contextDir, path.substring(1)); - return new FileInputStream(resourceFile); - } catch (IOException e) { - return null; - } + try { + File resourceFile = _application.getResourceFile( path ); + return resourceFile == null ? null : new FileInputStream( resourceFile ); + } catch (FileNotFoundException e) { + return null; } } @@ -161,7 +153,8 @@ **/ public javax.servlet.RequestDispatcher getRequestDispatcher( String path ) { try { - return new RequestDispatcherImpl( _application.getServlet( new URL( "http", "localhost", path ) ) ); + URL url = new URL( "http", "localhost", path ); + return new RequestDispatcherImpl( _application.getServletRequest( url ).getServlet() ); } catch (ServletException e) { return null; } catch (MalformedURLException e) { @@ -267,7 +260,7 @@ * webmaster's email address or the name of a system that holds critical data. **/ public java.lang.String getInitParameter( String name ) { - return (String) _contextParams.get( name ); + return (String) getContextParams().get( name ); } @@ -276,7 +269,7 @@ * or an empty Enumeration if the context has no initialization parameters. **/ public java.util.Enumeration getInitParameterNames() { - return _contextParams.keys(); + return getContextParams().keys(); } @@ -351,12 +344,12 @@ void setInitParameter( String name, Object initParameter ) { - _contextParams.put( name, initParameter ); + getContextParams().put( name, initParameter ); } void removeInitParameter( String name ) { - _contextParams.remove( name ); + getContextParams().remove( name ); } @@ -366,6 +359,9 @@ private Hashtable _attributes = new Hashtable(); private WebApplication _application; - private Hashtable _contextParams = new Hashtable(); - private File _contextDir; + + + private Hashtable getContextParams() { + return _application.getContextParameters(); + } } Index: WebApplication.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/WebApplication.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- WebApplication.java 21 May 2002 20:43:31 -0000 1.9 +++ WebApplication.java 19 Jun 2002 13:47:19 -0000 1.10 @@ -55,6 +55,7 @@ **/ class WebApplication { + /** * Constructs a default application spec with no information. */ @@ -120,53 +121,8 @@ } - synchronized Servlet getServlet( URL url ) throws ServletException { - final ServletConfiguration configuration = getServletConfiguration( url ); - if (configuration == null) throw new HttpNotFoundException( url ); - - try { - Class servletClass = Class.forName( configuration.getClassName() ); - - Servlet cachedServlet = (Servlet)_cachedServlets.get(servletClass); - if (cachedServlet != null) return cachedServlet; - - if (!Servlet.class.isAssignableFrom( servletClass )) throw new HttpInternalErrorException( url ); - Servlet servlet = (Servlet) servletClass.newInstance(); - servlet.init( new ServletUnitServletConfig( servlet, this, configuration.getInitParams(), _contextParameters, _contextDir ) ); - _cachedServlets.put( servletClass, servlet ); - return servlet; - } catch (ClassNotFoundException e) { - throw new HttpNotFoundException( url, e ); - } catch (IllegalAccessException e) { - throw new HttpInternalErrorException( url, e ); - } catch (InstantiationException e) { - throw new HttpInternalErrorException( url, e ); - } - } - - - private ServletConfiguration getServletConfiguration( URL url ) { - if (!url.getFile().startsWith( _contextPath )) { - return null; - } - String servletName = getServletName( getURLPath( url ) ); - if (servletName.endsWith( "j_security_check" )) { - return SECURITY_CHECK_CONFIGURATION; - } else { - return _servletMapping.get( servletName ); - } - } - - - private String getURLPath( URL url ) { - String file = url.getFile(); - if (_contextPath.equals( "" )) { - return file; - } else if (file.startsWith( _contextPath )) { - return file.substring( _contextPath.length() ); - } else { - return null; - } + ServletRequest getServletRequest( URL url ) { + return _servletMapping.get( url ); } @@ -206,7 +162,16 @@ * @param url the application-relative path of the URL */ boolean requiresAuthorization( URL url ) { - return getControllingConstraint( getURLPath( url ) ) != NULL_SECURITY_CONSTRAINT; + String result; + String file = url.getFile(); + if (_contextPath.equals( "" )) { + result = file; + } else if (file.startsWith( _contextPath )) { + result = file.substring( _contextPath.length() ); + } else { + result = null; + } + return getControllingConstraint( result ) != NULL_SECURITY_CONSTRAINT; } @@ -214,7 +179,16 @@ * Returns true of the specified role may access the desired URL path. */ boolean roleMayAccess( String roleName, URL url ) { - return getControllingConstraint( getURLPath( url ) ).hasRole( roleName ); + String result; + String file = url.getFile(); + if (_contextPath.equals( "" )) { + result = file; + } else if (file.startsWith( _contextPath )) { + result = file.substring( _contextPath.length() ); + } else { + result = null; + } + return getControllingConstraint( result ).hasRole( roleName ); } @@ -227,12 +201,31 @@ } + File getResourceFile( String path ) { + if (_contextDir == null) { + return null; + } else { + return new File( _contextDir, path.substring(1) ); + } + } + + + Hashtable getContextParameters() { + return _contextParameters; + } + + //------------------------------------------------ private members --------------------------------------------- - private final static ServletConfiguration SECURITY_CHECK_CONFIGURATION = new ServletConfiguration( SecurityCheckServlet.class.getName() ); + + private final static SecurityConstraint NULL_SECURITY_CONSTRAINT = new NullSecurityConstraint(); + + private final ServletConfiguration SECURITY_CHECK_CONFIGURATION = new ServletConfiguration( SecurityCheckServlet.class.getName() ); + + private final ServletMapping SECURITY_CHECK_MAPPING = new ServletMapping( SECURITY_CHECK_CONFIGURATION ); /** A mapping of resource names to servlet class names. **/ - private ServletMapping _servletMapping = new ServletMapping(); + private ServletMap _servletMapping = new ServletMap(); private ArrayList _securityConstraints = new ArrayList(); @@ -252,10 +245,6 @@ private String _contextPath = null; - private HashMap _cachedServlets = new HashMap(); - - final static private SecurityConstraint NULL_SECURITY_CONSTRAINT = new NullSecurityConstraint(); - private void extractLoginConfiguration( Document document ) throws MalformedURLException, SAXException { NodeList nl = document.getElementsByTagName( "login-config" ); @@ -338,17 +327,9 @@ } - private String getServletName( String urlFile ) { - if (urlFile.indexOf( '?' ) < 0) { - return urlFile; - } else { - return urlFile.substring( 0, urlFile.indexOf( '?' ) ); - } - } - - //============================================= SecurityCheckServlet class ============================================= + static class SecurityCheckServlet extends HttpServlet { protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { @@ -372,7 +353,7 @@ //============================================= ServletConfiguration class ============================================= - static class ServletConfiguration { + class ServletConfiguration { public ServletConfiguration( String className ) { _className = className; @@ -389,6 +370,17 @@ } + synchronized Servlet getServlet() throws ClassNotFoundException, InstantiationException, IllegalAccessException, ServletException { + if (_servlet == null) { + Class servletClass = Class.forName( getClassName() ); + _servlet = (Servlet) servletClass.newInstance(); + _servlet.init( new ServletUnitServletConfig( _servlet, WebApplication.this, getInitParams() ) ); + } + + return _servlet; + } + + String getClassName() { return _className; } @@ -399,6 +391,7 @@ } + private Servlet _servlet; private String _className; private Hashtable _initParams = new Hashtable(); } @@ -485,28 +478,126 @@ } + static class ServletRequestImpl implements ServletRequest { + + private URL _url; + private String _servletName; + private ServletMapping _mapping; + + + ServletRequestImpl( URL url, String servletName, ServletMapping mapping ) { + _url = url; + _servletName = servletName; + _mapping = mapping; + } + + + public Servlet getServlet() throws ServletException { + if (getConfiguration() == null) throw new HttpNotFoundException( _url ); + + try { + return getConfiguration().getServlet(); + } catch (ClassNotFoundException e) { + throw new HttpNotFoundException( _url, e ); + } catch (IllegalAccessException e) { + throw new HttpInternalErrorException( _url, e ); + } catch (InstantiationException e) { + throw new HttpInternalErrorException( _url, e ); + } catch (ClassCastException e) { + throw new HttpInternalErrorException( _url, e ); + } + } + + + public String getServletPath() { + return _mapping == null ? null : _mapping.getServletPath( _servletName ); + } + + + public String getPathInfo() { + return _mapping == null ? null : _mapping.getPathInfo( _servletName ); + } + + + private ServletConfiguration getConfiguration() { + return _mapping == null ? null : _mapping.getConfiguration(); + } + } + + + static class ServletMapping { + + private ServletConfiguration _configuration; + + + ServletConfiguration getConfiguration() { + return _configuration; + } + + + ServletMapping( ServletConfiguration configuration ) { + _configuration = configuration; + } + + + String getServletPath( String servletName ) { + return servletName; + } + + + String getPathInfo( String servletName ) { + return null; + } + } + + + static class PartialMatchServletMapping extends ServletMapping { + + private String _prefix; + + + public PartialMatchServletMapping( ServletConfiguration configuration, String prefix ) { + super( configuration ); + if (!prefix.endsWith( "/*" )) throw new IllegalArgumentException( prefix + " does not end with '/*'" ); + _prefix = prefix.substring( 0, prefix.length()-2 ); + } + + + String getServletPath( String servletName ) { + return _prefix; + } + + + String getPathInfo( String servletName ) { + return servletName.length() > _prefix.length() + ? servletName.substring( _prefix.length() ) + : null; + } + } + + /** * A utility class for mapping servlets to url patterns. This implements the * matching algorithm documented in section 10 of the JSDK-2.2 reference. */ - class ServletMapping { + class ServletMap { 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 ); + if (mapping.startsWith( "*." )) { + _extensions.put( mapping.substring( 2 ), new ServletMapping( servletConfiguration ) ); + } else if (!mapping.startsWith( "/" ) || !mapping.endsWith( "/*" )) { + _exactMatches.put( mapping, new ServletMapping( servletConfiguration ) ); } else { ParsedPath path = new ParsedPath( mapping ); Map context = _urlTree; while (path.hasNext()) { String part = path.next(); if (part.equals( "*" )) { - context.put( "*", servletConfiguration ); + context.put( "*", new PartialMatchServletMapping( servletConfiguration, mapping ) ); return; } if (!context.containsKey( part )) { @@ -514,20 +605,42 @@ } context = (Map) context.get( part ); } - context.put( "/", servletConfiguration ); } } - ServletConfiguration get( String url ) { - if (_exactMatches.containsKey( url )) return (ServletConfiguration) _exactMatches.get( url ); + ServletRequest get( URL url ) { + String file = url.getFile(); + if (!file.startsWith( _contextPath )) throw new HttpNotFoundException( url ); + + String servletName = getServletName( file.substring( _contextPath.length() ) ); + + if (servletName.endsWith( "j_security_check" )) { + return new ServletRequestImpl( url, servletName, SECURITY_CHECK_MAPPING ); + } else { + return new ServletRequestImpl( url, servletName, getMapping( servletName ) ); + } + } + + + private String getServletName( String urlFile ) { + if (urlFile.indexOf( '?' ) < 0) { + return urlFile; + } else { + return urlFile.substring( 0, urlFile.indexOf( '?' ) ); + } + } + + + private ServletMapping getMapping( String url ) { + if (_exactMatches.containsKey( url )) return (ServletMapping) _exactMatches.get( url ); Map context = getContextForLongestPathPrefix( url ); - if (context.containsKey( "*" )) return (ServletConfiguration) context.get( "*" ); + if (context.containsKey( "*" )) return (ServletMapping) context.get( "*" ); - if (_extensions.containsKey( getExtension( url ))) return (ServletConfiguration) _extensions.get( getExtension( url ) ); + if (_extensions.containsKey( getExtension( url ))) return (ServletMapping) _extensions.get( getExtension( url ) ); - if (_urlTree.containsKey( "/" )) return (ServletConfiguration) _urlTree.get( "/" ); + if (_urlTree.containsKey( "/" )) return (ServletMapping) _urlTree.get( "/" ); final String prefix = "/servlet/"; if (!url.startsWith( prefix )) return null; @@ -535,7 +648,7 @@ String className = url.substring( prefix.length() ); try { Class.forName( className ); - return new ServletConfiguration( className ); + return new ServletMapping( new ServletConfiguration( className ) ); } catch (ClassNotFoundException e) { return null; } |