📄 contexthandler.java
字号:
//========================================================================//$Id: ContextHandler.java,v 1.16 2005/11/17 11:19:45 gregwilkins Exp $//Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.//------------------------------------------------------------------------//Licensed under the Apache License, Version 2.0 (the "License");//you may not use this file except in compliance with the License.//You may obtain a copy of the License at //http://www.apache.org/licenses/LICENSE-2.0//Unless required by applicable law or agreed to in writing, software//distributed under the License is distributed on an "AS IS" BASIS,//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.//See the License for the specific language governing permissions and//limitations under the License.//========================================================================package org.mortbay.jetty.handler;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;import java.util.Arrays;import java.util.Collections;import java.util.Enumeration;import java.util.EventListener;import java.util.HashMap;import java.util.HashSet;import java.util.Locale;import java.util.Map;import java.util.Set;import javax.servlet.RequestDispatcher;import javax.servlet.Servlet;import javax.servlet.ServletContext;import javax.servlet.ServletContextAttributeEvent;import javax.servlet.ServletContextAttributeListener;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.ServletException;import javax.servlet.ServletRequestAttributeListener;import javax.servlet.ServletRequestEvent;import javax.servlet.ServletRequestListener;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.mortbay.io.Buffer;import org.mortbay.jetty.Handler;import org.mortbay.jetty.HandlerContainer;import org.mortbay.jetty.HttpConnection;import org.mortbay.jetty.HttpException;import org.mortbay.jetty.MimeTypes;import org.mortbay.jetty.Request;import org.mortbay.jetty.Server;import org.mortbay.jetty.webapp.WebAppClassLoader;import org.mortbay.log.Log;import org.mortbay.log.Logger;import org.mortbay.resource.Resource;import org.mortbay.util.Attributes;import org.mortbay.util.AttributesMap;import org.mortbay.util.LazyList;import org.mortbay.util.Loader;import org.mortbay.util.URIUtil;/* ------------------------------------------------------------ *//** ContextHandler. * * This handler wraps a call to handle by setting the context and * servlet path, plus setting the context classloader. * * <p> * If the context init parameter "org.mortbay.jetty.servlet.ManagedAttributes" * is set to a coma separated list of names, then they are treated as context * attribute names, which if set as attributes are passed to the servers Container * so that they may be managed with JMX. * * @org.apache.xbean.XBean description="Creates a basic HTTP context" * * @author gregw * */public class ContextHandler extends HandlerWrapper implements Attributes, Server.Graceful{ private static ThreadLocal __context=new ThreadLocal(); public static final String MANAGED_ATTRIBUTES = "org.mortbay.jetty.servlet.ManagedAttributes"; /* ------------------------------------------------------------ */ /** Get the current ServletContext implementation. * This call is only valid during a call to doStart and is available to * nested handlers to access the context. * * @return ServletContext implementation */ public static SContext getCurrentContext() { SContext context = (SContext)__context.get(); return context; } protected SContext _scontext; private AttributesMap _attributes; private AttributesMap _contextAttributes; private ClassLoader _classLoader; private String _contextPath="/"; private Map _initParams; private String _displayName; private Resource _baseResource; private MimeTypes _mimeTypes; private Map _localeEncodingMap; private String[] _welcomeFiles; private ErrorHandler _errorHandler; private String[] _vhosts; private Set _connectors; private EventListener[] _eventListeners; private Logger _logger; private boolean _shutdown; private boolean _allowNullPathInfo; private int _maxFormContentSize=Integer.getInteger("org.mortbay.jetty.Request.maxFormContentSize",200000).intValue(); private boolean _compactPath=false; private Object _contextListeners; private Object _contextAttributeListeners; private Object _requestListeners; private Object _requestAttributeListeners; private Set _managedAttributes; /* ------------------------------------------------------------ */ /** * */ public ContextHandler() { super(); _scontext=new SContext(); _attributes=new AttributesMap(); _initParams=new HashMap(); } /* ------------------------------------------------------------ */ /** * */ protected ContextHandler(SContext context) { super(); _scontext=context; _attributes=new AttributesMap(); _initParams=new HashMap(); } /* ------------------------------------------------------------ */ /** * */ public ContextHandler(String contextPath) { this(); setContextPath(contextPath); } /* ------------------------------------------------------------ */ /** * */ public ContextHandler(HandlerContainer parent, String contextPath) { this(); setContextPath(contextPath); parent.addHandler(this); } /* ------------------------------------------------------------ */ public SContext getServletContext() { return _scontext; } /* ------------------------------------------------------------ */ /** * @return the allowNullPathInfo true if /context is not redirected to /context/ */ public boolean getAllowNullPathInfo() { return _allowNullPathInfo; } /* ------------------------------------------------------------ */ /** * @param allowNullPathInfo true if /context is not redirected to /context/ */ public void setAllowNullPathInfo(boolean allowNullPathInfo) { _allowNullPathInfo=allowNullPathInfo; } /* ------------------------------------------------------------ */ public void setServer(Server server) { if (_errorHandler!=null) { Server old_server=getServer(); if (old_server!=null && old_server!=server) old_server.getContainer().update(this, _errorHandler, null, "error",true); super.setServer(server); if (server!=null && server!=old_server) server.getContainer().update(this, null, _errorHandler, "error",true); _errorHandler.setServer(server); } else super.setServer(server); } /* ------------------------------------------------------------ */ /** Set the virtual hosts for the context. * Only requests that have a matching host header or fully qualified * URL will be passed to that context with a virtual host name. * A context with no virtual host names or a null virtual host name is * available to all requests that are not served by a context with a * matching virtual host name. * @param vhosts Array of virtual hosts that this context responds to. A * null host name or null/empty array means any hostname is acceptable. * Host names may be String representation of IP addresses. Host names may * start with '*.' to wildcard one level of names. */ public void setVirtualHosts( String[] vhosts ) { if ( vhosts == null ) { _vhosts = vhosts; } else { _vhosts = new String[vhosts.length]; for ( int i = 0; i < vhosts.length; i++ ) _vhosts[i] = normalizeHostname( vhosts[i]); } } /* ------------------------------------------------------------ */ /** Get the virtual hosts for the context. * Only requests that have a matching host header or fully qualified * URL will be passed to that context with a virtual host name. * A context with no virtual host names or a null virtual host name is * available to all requests that are not served by a context with a * matching virtual host name. * @return Array of virtual hosts that this context responds to. A * null host name or empty array means any hostname is acceptable. * Host names may be String representation of IP addresses. * Host names may start with '*.' to wildcard one level of names. */ public String[] getVirtualHosts() { return _vhosts; } /* ------------------------------------------------------------ */ /** * @deprecated use {@link #setConnectorNames(String[])} */ public void setHosts(String[] hosts) { setConnectorNames(hosts); } /* ------------------------------------------------------------ */ /** Get the hosts for the context. * @deprecated */ public String[] getHosts() { return getConnectorNames(); } /* ------------------------------------------------------------ */ /** * @return an array of connector names that this context * will accept a request from. */ public String[] getConnectorNames() { if (_connectors==null || _connectors.size()==0) return null; return (String[])_connectors.toArray(new String[_connectors.size()]); } /* ------------------------------------------------------------ */ /** Set the names of accepted connectors. * * Names are either "host:port" or a specific configured name for a connector. * * @param connectors If non null, an array of connector names that this context * will accept a request from. */ public void setConnectorNames(String[] connectors) { if (connectors==null || connectors.length==0) _connectors=null; else _connectors= new HashSet(Arrays.asList(connectors)); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletContext#getAttribute(java.lang.String) */ public Object getAttribute(String name) { return _attributes.getAttribute(name); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletContext#getAttributeNames() */ public Enumeration getAttributeNames() { return AttributesMap.getAttributeNamesCopy(_attributes); } /* ------------------------------------------------------------ */ /** * @return Returns the attributes. */ public Attributes getAttributes() { return _attributes; } /* ------------------------------------------------------------ */ /** * @return Returns the classLoader. */ public ClassLoader getClassLoader() { return _classLoader; } /* ------------------------------------------------------------ */ /** * Make best effort to extract a file classpath from the context classloader * @return Returns the classLoader. */ public String getClassPath() { if ( _classLoader==null || !(_classLoader instanceof URLClassLoader)) return null; URLClassLoader loader = (URLClassLoader)_classLoader; URL[] urls =loader.getURLs(); StringBuffer classpath=new StringBuffer(); for (int i=0;i<urls.length;i++) { try { Resource resource = Resource.newResource(urls[i]); File file=resource.getFile(); if (file.exists()) { if (classpath.length()>0) classpath.append(File.pathSeparatorChar); classpath.append(file.getAbsolutePath()); } } catch (IOException e) { Log.debug(e); } } if (classpath.length()==0) return null; return classpath.toString(); } /* ------------------------------------------------------------ */ /** * @return Returns the _contextPath. */ public String getContextPath() { return _contextPath; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletContext#getInitParameter(java.lang.String) */ public String getInitParameter(String name) { return (String)_initParams.get(name); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletContext#getInitParameterNames() */ public Enumeration getInitParameterNames()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -