📄 webapplication.java
字号:
} } static final FilterMetaData[] NO_FILTERS = new FilterMetaData[0]; static class ServletRequestImpl implements ServletMetaData { private URL _url; private String _fullServletPath; private WebResourceMapping _mapping; private Hashtable _filtersPerName; private FilterUrlMap _filtersPerUrl; ServletRequestImpl( URL url, String servletPath, WebResourceMapping mapping, Hashtable filtersPerName, FilterUrlMap filtersPerUrl ) { _url = url; _fullServletPath = servletPath; _mapping = mapping; _filtersPerName = filtersPerName; _filtersPerUrl = filtersPerUrl; } public Servlet getServlet() throws ServletException { if (getConfiguration() == null) throw new HttpNotFoundException( "No servlet mapping defined", _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( _fullServletPath ); } public String getPathInfo() { return _mapping == null ? null : _mapping.getPathInfo( _fullServletPath ); } public FilterMetaData[] getFilters() { if (getConfiguration() == null) return NO_FILTERS; List filters = new ArrayList(); addFiltersForPath( filters, _fullServletPath ); addFiltersForServletWithName( filters, getConfiguration().getServletName() ); return (FilterMetaData[]) filters.toArray( new FilterMetaData[ filters.size() ]); } private void addFiltersForPath( List filters, String fullServletPath ) { FilterMetaData[] matches = _filtersPerUrl.getMatchingFilters( fullServletPath ); for (int i = 0; i < matches.length; i++) { filters.add( matches[i] ); } } private void addFiltersForServletWithName( List filters, String servletName ) { if (servletName == null) return; List matches = (List) _filtersPerName.get( servletName ); if (matches != null) filters.addAll( matches ); } private ServletConfiguration getConfiguration() { return _mapping == null ? null : (ServletConfiguration) _mapping.getConfiguration(); } } static class WebResourceMapping { private WebResourceConfiguration _configuration; WebResourceConfiguration getConfiguration() { return _configuration; } WebResourceMapping( WebResourceConfiguration configuration ) { _configuration = configuration; } /** * Returns the portion of the request path which was actually used to select the servlet. This default * implementation returns the full specified path. * @param requestPath the full path of the request, relative to the application root. */ String getServletPath( String requestPath ) { return requestPath; } /** * Returns the portion of the request path which was not used to select the servlet, and can be * used as data by the servlet. This default implementation returns null. * @param requestPath the full path of the request, relative to the application root. */ String getPathInfo( String requestPath ) { return null; } public void destroyResource() { getConfiguration().destroyResource(); } } static class PartialMatchWebResourceMapping extends WebResourceMapping { private String _prefix; public PartialMatchWebResourceMapping( WebResourceConfiguration 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 requestPath ) { return _prefix; } String getPathInfo( String requestPath ) { return requestPath.length() > _prefix.length() ? requestPath.substring( _prefix.length() ) : null; } } /** * A utility class for mapping web resources to url patterns. This implements the * matching algorithm documented in section 10 of the JSDK-2.2 reference. */ class WebResourceMap { private final Map _exactMatches = new HashMap(); private final Map _extensions = new HashMap(); private final Map _urlTree = new HashMap(); private WebResourceMapping _defaultMapping; void put( String mapping, WebResourceConfiguration configuration ) { if (mapping.equals( "/" )) { _defaultMapping = new WebResourceMapping( configuration ); } else if (mapping.startsWith( "*." )) { _extensions.put( mapping.substring( 2 ), new WebResourceMapping( configuration ) ); } else if (!mapping.startsWith( "/" ) || !mapping.endsWith( "/*" )) { _exactMatches.put( mapping, new WebResourceMapping( configuration ) ); } else { ParsedPath path = new ParsedPath( mapping ); Map context = _urlTree; while (path.hasNext()) { String part = path.next(); if (part.equals( "*" )) { context.put( "*", new PartialMatchWebResourceMapping( configuration, mapping ) ); return; } if (!context.containsKey( part )) { context.put( part, new HashMap() ); } context = (Map) context.get( part ); } } } ServletMetaData get( URL url ) { String file = url.getFile(); if (!file.startsWith( _contextPath )) throw new HttpNotFoundException( "File path does not begin with '" + _contextPath + "'", url ); String servletPath = getServletPath( file.substring( _contextPath.length() ) ); if (servletPath.endsWith( "j_security_check" )) { return new ServletRequestImpl( url, servletPath, SECURITY_CHECK_MAPPING, _filterMapping, _filterUrlMapping ); } else { return new ServletRequestImpl( url, servletPath, getMapping( servletPath ), _filterMapping, _filterUrlMapping ); } } private String getServletPath( String urlFile ) { if (urlFile.indexOf( '?' ) < 0) { return urlFile; } else { return urlFile.substring( 0, urlFile.indexOf( '?' ) ); } } public void destroyWebResources() { if (_defaultMapping != null) _defaultMapping.destroyResource(); destroyWebResources( _exactMatches ); destroyWebResources( _extensions ); destroyWebResources( _urlTree ); } private void destroyWebResources( Map map ) { for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { Object o = iterator.next(); if (o instanceof WebResourceMapping) { WebResourceMapping webResourceMapping = (WebResourceMapping) o; webResourceMapping.destroyResource(); } else { destroyWebResources( (Map) o ); } } } void autoLoadServlets() { ArrayList autoLoadable = new ArrayList(); if (_defaultMapping != null && _defaultMapping.getConfiguration().isLoadOnStartup()) autoLoadable.add( _defaultMapping.getConfiguration() ); collectAutoLoadableServlets( autoLoadable, _exactMatches ); collectAutoLoadableServlets( autoLoadable, _extensions ); collectAutoLoadableServlets( autoLoadable, _urlTree ); if (autoLoadable.isEmpty()) return; Collections.sort( autoLoadable, new Comparator() { public int compare( Object o1, Object o2 ) { ServletConfiguration sc1 = (ServletConfiguration) o1; ServletConfiguration sc2 = (ServletConfiguration) o2; return (sc1.getLoadOrder() <= sc2.getLoadOrder()) ? -1 : +1; } }); for (Iterator iterator = autoLoadable.iterator(); iterator.hasNext();) { ServletConfiguration servletConfiguration = (ServletConfiguration) iterator.next(); try { servletConfiguration.getServlet(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( "Unable to autoload servlet: " + servletConfiguration.getClassName() + ": " + e ); } } } private void collectAutoLoadableServlets( Collection collection, Map map ) { for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { Object o = iterator.next(); if (o instanceof WebResourceMapping) { WebResourceMapping servletMapping = (WebResourceMapping) o; if (servletMapping.getConfiguration().isLoadOnStartup()) collection.add( servletMapping.getConfiguration() ); } else { collectAutoLoadableServlets( collection, (Map) o ); } } } private WebResourceMapping getMapping( String url ) { if (_exactMatches.containsKey( url )) return (WebResourceMapping) _exactMatches.get( url ); Map context = getContextForLongestPathPrefix( url ); if (context.containsKey( "*" )) return (WebResourceMapping) context.get( "*" ); if (_extensions.containsKey( getExtension( url ))) return (WebResourceMapping) _extensions.get( getExtension( url ) ); if (_urlTree.containsKey( "/" )) return (WebResourceMapping) _urlTree.get( "/" ); if (_defaultMapping != null) return _defaultMapping; final String prefix = "/servlet/"; if (!url.startsWith( prefix )) return null; String className = url.substring( prefix.length() ); try { Class.forName( className ); return new WebResourceMapping( new ServletConfiguration( className ) ); } catch (ClassNotFoundException e) { return null; } } private Map getContextForLongestPathPrefix( String url ) { Map context = _urlTree; ParsedPath path = new ParsedPath( url ); while (path.hasNext()) { String part = path.next(); if (!context.containsKey( part )) break; context = (Map) context.get( part ); } return context; } private String getExtension( String url ) { int index = url.lastIndexOf( '.' ); if (index == -1 || index >= url.length() - 1) { return ""; } else { return url.substring( index + 1 ); } } }}/** * A utility class for parsing URLs into paths * * @author <a href="balld@webslingerZ.com">Donald Ball</a> */class ParsedPath { private final String path; 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 ); } this.path = path; } /** * Returns true if there are more parts left, otherwise false */ public final boolean hasNext() { 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) { offset++; } String result = path.substring( position + 1, offset ); position = offset; return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -