📄 webapplication.java
字号:
package com.meterware.servletunit;/******************************************************************************************************************** * $Id: WebApplication.java,v 1.27 2006/03/24 19:59:12 russgold Exp $ * * Copyright (c) 2001-2004, 2006 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 * 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 * of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * *******************************************************************************************************************/import com.meterware.httpunit.HttpInternalErrorException;import com.meterware.httpunit.HttpNotFoundException;import java.io.File;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;/** * This class represents the information recorded about a single web * application. It is usually extracted from web.xml. * * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a> * @author <a href="balld@webslingerZ.com">Donald Ball</a> * @author <a href="jaydunning@users.sourceforge.net">Jay Dunning</a> **/class WebApplication implements SessionListenerDispatcher { private final static SecurityConstraint NULL_SECURITY_CONSTRAINT = new NullSecurityConstraint(); private final ServletConfiguration SECURITY_CHECK_CONFIGURATION = new ServletConfiguration( SecurityCheckServlet.class.getName() ); private final WebResourceMapping SECURITY_CHECK_MAPPING = new WebResourceMapping( SECURITY_CHECK_CONFIGURATION ); /** A mapping of resource names to servlet configurations. **/ private WebResourceMap _servletMapping = new WebResourceMap(); /** A mapping of resource names to filter configurations. **/ private FilterUrlMap _filterUrlMapping = new FilterUrlMap(); /** A mapping of servlet names to filter configurations. **/ private Hashtable _filterMapping = new Hashtable(); private ArrayList _securityConstraints = new ArrayList(); private ArrayList _contextListeners = new ArrayList(); private ArrayList _contextAttributeListeners = new ArrayList(); private ArrayList _sessionListeners = new ArrayList(); private ArrayList _sessionAttributeListeners = new ArrayList(); private boolean _useBasicAuthentication; private boolean _useFormAuthentication; private String _authenticationRealm = ""; private URL _loginURL; private URL _errorURL; private Hashtable _contextParameters = new Hashtable(); private File _contextDir = null; private String _contextPath = null; private ServletUnitServletContext _servletContext; private String _displayName; /** * Constructs a default application spec with no information. */ WebApplication() { _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 ); } /** * Constructs an application spec from an XML document. */ WebApplication( Document document, File file, String contextPath ) throws MalformedURLException, SAXException { if (contextPath != null && contextPath.length() > 0 && !contextPath.startsWith( "/" )) throw new IllegalArgumentException( "Context path " + contextPath + " must start with '/'" ); _contextDir = file; _contextPath = contextPath == null ? "" : contextPath; NodeList nl = document.getElementsByTagName( "display-name" ); if (nl.getLength() > 0) _displayName = XMLUtils.getTextValue( nl.item(0) ).trim(); registerServlets( document ); registerFilters( document ); extractSecurityConstraints( document ); extractContextParameters( document ); extractLoginConfiguration( document ); extractListeners( document ); notifyContextInitialized(); _servletMapping.autoLoadServlets(); } private void extractListeners( Document document ) throws SAXException { NodeList nl = document.getElementsByTagName( "listener" ); for (int i = 0; i < nl.getLength(); i++) { String listenerName = XMLUtils.getChildNodeValue((Element) nl.item(i), "listener-class").trim(); try { Object listener = Class.forName( listenerName ).newInstance(); if (listener instanceof ServletContextListener) _contextListeners.add( listener ); if (listener instanceof ServletContextAttributeListener) _contextAttributeListeners.add( listener ); if (listener instanceof HttpSessionListener) _sessionListeners.add( listener ); if (listener instanceof HttpSessionAttributeListener) _sessionAttributeListeners.add( listener ); } catch (Throwable e) { throw new RuntimeException("Unable to load context listener " + listenerName + ": " + e.toString() ); } } } private void notifyContextInitialized() { ServletContextEvent event = new ServletContextEvent( getServletContext() ); for (Iterator i = _contextListeners.iterator(); i.hasNext();) { ServletContextListener listener = (ServletContextListener) i.next(); listener.contextInitialized( event ); } } void shutDown() { destroyServlets(); notifyContextDestroyed(); } private void notifyContextDestroyed() { ServletContextEvent event = new ServletContextEvent( getServletContext() ); for (ListIterator i = _contextListeners.listIterator( _contextListeners.size() ); i.hasPrevious();) { ServletContextListener listener = (ServletContextListener) i.previous(); listener.contextDestroyed( event ); } } void sendAttributeAdded( String name, Object value ) { ServletContextAttributeEvent event = new ServletContextAttributeEvent( getServletContext(), name, value ); for (Iterator i = _contextAttributeListeners.iterator(); i.hasNext();) { ServletContextAttributeListener listener = (ServletContextAttributeListener) i.next(); listener.attributeAdded( event ); } } void sendAttributeReplaced( String name, Object value ) { ServletContextAttributeEvent event = new ServletContextAttributeEvent( getServletContext(), name, value ); for (Iterator i = _contextAttributeListeners.iterator(); i.hasNext();) { ServletContextAttributeListener listener = (ServletContextAttributeListener) i.next(); listener.attributeReplaced( event ); } } void sendAttributeRemoved( String name, Object value ) { ServletContextAttributeEvent event = new ServletContextAttributeEvent( getServletContext(), name, value ); for (Iterator i = _contextAttributeListeners.iterator(); i.hasNext();) { ServletContextAttributeListener listener = (ServletContextAttributeListener) i.next(); listener.attributeRemoved( event ); } } 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 ) ) ); } } String getContextPath() { return _contextPath; } ServletContext getServletContext() { if (_servletContext == null) { _servletContext = new ServletUnitServletContext( this ); } return _servletContext; } /** * Registers a servlet class to be run. **/ void registerServlet( String resourceName, String servletClassName, Hashtable initParams ) { registerServlet( resourceName, new ServletConfiguration( servletClassName, initParams ) ); } /** * Registers a servlet to be run. **/ void registerServlet( String resourceName, ServletConfiguration servletConfiguration ) { // FIXME - shouldn't everything start with one or the other? if (!resourceName.startsWith( "/" ) && !resourceName.startsWith( "*" )) { resourceName = "/" + resourceName; } _servletMapping.put( resourceName, servletConfiguration ); } /** * Calls the destroy method for every active servlet. */ void destroyServlets() { _servletMapping.destroyWebResources(); } ServletMetaData getServletRequest( URL url ) { return _servletMapping.get( url ); } /** * Returns true if this application uses Basic Authentication. */ boolean usesBasicAuthentication() { return _useBasicAuthentication; } /** * Returns true if this application uses form-based authentication. */ boolean usesFormAuthentication() { return _useFormAuthentication; } String getAuthenticationRealm() { return _authenticationRealm; } URL getLoginURL() { return _loginURL; } URL getErrorURL() { return _errorURL; } /** * Returns true if the specified path may only be accesses by an authorized user. * @param url the application-relative path of the URL */ boolean requiresAuthorization( URL url ) { 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; } /** * Returns an array containing the roles permitted to access the specified URL. */ String[] getPermittedRoles( URL url ) { 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 ).getPermittedRoles(); } private SecurityConstraint getControllingConstraint( String urlPath ) { for (Iterator i = _securityConstraints.iterator(); i.hasNext();) { SecurityConstraint sc = (SecurityConstraint) i.next(); if (sc.controlsPath( urlPath )) return sc; } return NULL_SECURITY_CONSTRAINT; } File getResourceFile( String path ) { String relativePath = path.startsWith( "/" ) ? path.substring(1) : path; if (_contextDir == null) { return new File( relativePath ); } else { return new File( _contextDir, relativePath ); } } Hashtable getContextParameters() { return _contextParameters; }//---------------------------------------- SessionListenerDispatcher methods ------------------------------------------- public void sendSessionCreated( HttpSession session ) { HttpSessionEvent event = new HttpSessionEvent( session ); for (Iterator i = _sessionListeners.iterator(); i.hasNext();) { HttpSessionListener listener = (HttpSessionListener) i.next(); listener.sessionCreated( event ); } } public void sendSessionDestroyed( HttpSession session ) { HttpSessionEvent event = new HttpSessionEvent( session );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -