📄 webapplication.java
字号:
for (Iterator i = _sessionListeners.iterator(); i.hasNext();) { HttpSessionListener listener = (HttpSessionListener) i.next(); listener.sessionDestroyed( event ); } } public void sendAttributeAdded( HttpSession session, String name, Object value ) { HttpSessionBindingEvent event = new HttpSessionBindingEvent( session, name, value ); for (Iterator i = _sessionAttributeListeners.iterator(); i.hasNext();) { HttpSessionAttributeListener listener = (HttpSessionAttributeListener) i.next(); listener.attributeAdded( event ); } } public void sendAttributeReplaced( HttpSession session, String name, Object oldValue ) { HttpSessionBindingEvent event = new HttpSessionBindingEvent( session, name, oldValue ); for (Iterator i = _sessionAttributeListeners.iterator(); i.hasNext();) { HttpSessionAttributeListener listener = (HttpSessionAttributeListener) i.next(); listener.attributeReplaced( event ); } } public void sendAttributeRemoved( HttpSession session, String name, Object oldValue ) { HttpSessionBindingEvent event = new HttpSessionBindingEvent( session, name, oldValue ); for (Iterator i = _sessionAttributeListeners.iterator(); i.hasNext();) { HttpSessionAttributeListener listener = (HttpSessionAttributeListener) i.next(); listener.attributeRemoved( event ); } }//--------------------------------------------------- private members -------------------------------------------------- private void registerFilters( Document document ) throws SAXException { Hashtable nameToClass = new Hashtable(); NodeList nl = document.getElementsByTagName( "filter" ); for (int i = 0; i < nl.getLength(); i++) registerFilterClass( nameToClass, (Element) nl.item( i ) ); nl = document.getElementsByTagName( "filter-mapping" ); for (int i = 0; i < nl.getLength(); i++) registerFilter( nameToClass, (Element) nl.item( i ) ); } private void registerFilterClass( Dictionary mapping, Element filterElement ) throws SAXException { String filterName = XMLUtils.getChildNodeValue( filterElement, "filter-name" ); mapping.put( filterName, new FilterConfiguration( filterName, filterElement ) ); } private void registerFilter( Dictionary mapping, Element filterElement ) throws SAXException { if (XMLUtils.hasChildNode( filterElement, "servlet-name" )) { registerFilterForServlet( XMLUtils.getChildNodeValue( filterElement, "servlet-name" ), (FilterConfiguration) mapping.get( XMLUtils.getChildNodeValue( filterElement, "filter-name" ) ) ); } if (XMLUtils.hasChildNode( filterElement, "url-pattern" )) { registerFilterForUrl( XMLUtils.getChildNodeValue( filterElement, "url-pattern" ), (FilterConfiguration) mapping.get( XMLUtils.getChildNodeValue( filterElement, "filter-name" ) ) ); } } private void registerFilterForUrl( String resourceName, FilterConfiguration filterConfiguration ) { _filterUrlMapping.put( resourceName, filterConfiguration ); } private void registerFilterForServlet( String servletName, FilterConfiguration filterConfiguration ) { List list = (List) _filterMapping.get( servletName ); if (list == null) { list = new ArrayList(); _filterMapping.put( servletName, list ); } list.add( filterConfiguration ); } 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 ); String authenticationMethod = XMLUtils.getChildNodeValue( loginConfigElement, "auth-method", "BASIC" ); _authenticationRealm = XMLUtils.getChildNodeValue( loginConfigElement, "realm-name", "" ); if (authenticationMethod.equalsIgnoreCase( "BASIC" )) { _useBasicAuthentication = true; if (_authenticationRealm.length() == 0) throw new SAXException( "No realm specified for BASIC Authorization" ); } else if (authenticationMethod.equalsIgnoreCase( "FORM" )) { _useFormAuthentication = true; if (_authenticationRealm.length() == 0) throw new SAXException( "No realm specified for FORM Authorization" ); _loginURL = new URL( "http", "localhost", _contextPath + XMLUtils.getChildNodeValue( loginConfigElement, "form-login-page" ) ); _errorURL = new URL( "http", "localhost", _contextPath + XMLUtils.getChildNodeValue( loginConfigElement, "form-error-page" ) ); } } } 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 ) ); nl = document.getElementsByTagName( "servlet-mapping" ); for (int i = 0; i < nl.getLength(); i++) registerServlet( nameToClass, (Element) nl.item( i ) ); } private void registerServletClass( Dictionary mapping, Element servletElement ) throws SAXException { mapping.put( XMLUtils.getChildNodeValue( servletElement, "servlet-name" ), new ServletConfiguration( servletElement ) ); } private void registerServlet( Dictionary mapping, Element servletElement ) throws SAXException { registerServlet( XMLUtils.getChildNodeValue( servletElement, "url-pattern" ), (ServletConfiguration) mapping.get( XMLUtils.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 = XMLUtils.getChildNodeValue( param, "param-name" ); String value = XMLUtils.getChildNodeValue( param, "param-value" ); _contextParameters.put( name, value ); } } private static boolean patternMatches( String urlPattern, String urlPath ) { return urlPattern.equals( urlPath ); } String getDisplayName() { return _displayName; }//============================================= SecurityCheckServlet class ============================================= static class SecurityCheckServlet extends HttpServlet { protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { handleLogin( req, resp ); } protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { handleLogin( req, resp ); } private void handleLogin( HttpServletRequest req, HttpServletResponse resp ) throws IOException { final String username = req.getParameter( "j_username" ); final String roleList = req.getParameter( "j_password" ); getServletSession( req ).setUserInformation( username, ServletUnitHttpRequest.toArray( roleList ) ); resp.sendRedirect( getServletSession( req ).getOriginalURL().toExternalForm() ); } private ServletUnitHttpSession getServletSession( HttpServletRequest req ) { return (ServletUnitHttpSession) req.getSession(); } }//============================================= ServletConfiguration class ============================================= final static int DONT_AUTOLOAD = Integer.MIN_VALUE; final static int ANY_LOAD_ORDER = Integer.MAX_VALUE; class ServletConfiguration extends WebResourceConfiguration { private Servlet _servlet; private String _servletName; private int _loadOrder = DONT_AUTOLOAD; ServletConfiguration( String className ) { super( className ); } ServletConfiguration( String className, Hashtable initParams ) { super( className, initParams ); } ServletConfiguration( Element servletElement ) throws SAXException { super( servletElement, "servlet-class" ); _servletName = XMLUtils.getChildNodeValue( servletElement, "servlet-name" ); final NodeList loadOrder = servletElement.getElementsByTagName( "load-on-startup" ); for (int i = 0; i < loadOrder.getLength(); i++) { String order = XMLUtils.getTextValue( loadOrder.item(i) ); try { _loadOrder = Integer.parseInt( order ); } catch (NumberFormatException e) { _loadOrder = ANY_LOAD_ORDER; } } } synchronized Servlet getServlet() throws ClassNotFoundException, InstantiationException, IllegalAccessException, ServletException { if (_servlet == null) { Class servletClass = Class.forName( getClassName() ); _servlet = (Servlet) servletClass.newInstance(); String servletName = _servletName != null ? _servletName : _servlet.getClass().getName(); _servlet.init( new ServletUnitServletConfig( servletName, WebApplication.this, getInitParams() ) ); } return _servlet; } synchronized void destroyResource() { if (_servlet != null) _servlet.destroy(); } String getServletName() { return _servletName; } boolean isLoadOnStartup() { return _loadOrder != DONT_AUTOLOAD; } public int getLoadOrder() { return _loadOrder; } }//============================================= FilterConfiguration class ============================================= class FilterConfiguration extends WebResourceConfiguration implements FilterMetaData { private Filter _filter; private String _name; FilterConfiguration( String name, Element filterElement ) throws SAXException { super( filterElement, "filter-class" ); _name = name; } public synchronized Filter getFilter() throws ServletException { try { if (_filter == null) { Class filterClass = Class.forName( getClassName() ); _filter = (Filter) filterClass.newInstance(); _filter.init( new FilterConfigImpl( _name, getServletContext(), getInitParams() ) ); } return _filter; } catch (ClassNotFoundException e) { throw new ServletException( "Did not find filter class: " + getClassName() ); } catch (IllegalAccessException e) { throw new ServletException( "Filter class " + getClassName() + " lacks a public no-arg constructor" ); } catch (InstantiationException e) { throw new ServletException( "Filter class " + getClassName() + " could not be instantiated." ); } catch (ClassCastException e) { throw new ServletException( "Filter class " + getClassName() + " does not implement" + Filter.class.getName() ); } } boolean isLoadOnStartup() { return false; } synchronized void destroyResource() { if (_filter != null) _filter.destroy(); } }//=================================== SecurityConstract interface and implementations ================================== interface SecurityConstraint { boolean controlsPath( String urlPath ); String[] getPermittedRoles(); } static class NullSecurityConstraint implements SecurityConstraint { private static final String[] NO_ROLES = new String[0]; public boolean controlsPath( String urlPath ) { return false; } public String[] getPermittedRoles() { return NO_ROLES; } } 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++) _roleList.add( XMLUtils.getTextValue( 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 ) ) ); } public boolean controlsPath( String urlPath ) { return getMatchingCollection( urlPath ) != null; } public String[] getPermittedRoles() { if (_roles == null) { _roles = (String[]) _roleList.toArray( new String[ _roleList.size() ] ); } return _roles; } private String[] _roles; private ArrayList _roleList = new ArrayList(); private ArrayList _resources = new ArrayList(); public WebResourceCollection getMatchingCollection( String urlPath ) { for (Iterator i = _resources.iterator(); i.hasNext();) { WebResourceCollection wrc = (WebResourceCollection) i.next(); if (wrc.controlsPath( urlPath )) return wrc; } 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( XMLUtils.getTextValue( urlPatterns.item( i ) ) ); } boolean controlsPath( String urlPath ) { for (Iterator i = _urlPatterns.iterator(); i.hasNext();) { String pattern = (String) i.next(); if (patternMatches( pattern, urlPath )) return true; } return false; } private ArrayList _urlPatterns = new ArrayList();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -