📄 applicationcontext.java
字号:
/**
* Return the MIME type of the specified file, or <code>null</code> if
* the MIME type cannot be determined.
*
* @param file Filename for which to identify a MIME type
*/
public String getMimeType(String file) {
if (file == null)
return (null);
int period = file.lastIndexOf(".");
if (period < 0)
return (null);
String extension = file.substring(period + 1);
if (extension.length() < 1)
return (null);
return (context.findMimeMapping(extension));
}
/**
* Return a <code>RequestDispatcher</code> object that acts as a
* wrapper for the named servlet.
*
* @param name Name of the servlet for which a dispatcher is requested
*/
public RequestDispatcher getNamedDispatcher(String name) {
// Validate the name argument
if (name == null)
return (null);
// Create and return a corresponding request dispatcher
Wrapper wrapper = (Wrapper) context.findChild(name);
if (wrapper == null)
return (null);
ApplicationDispatcher dispatcher;
dispatcher =
new ApplicationDispatcher(wrapper, null, null, null, null, name);
return ((RequestDispatcher) dispatcher);
}
/**
* Return the real path for a given virtual path, if possible; otherwise
* return <code>null</code>.
*
* @param path The path to the desired resource
*/
public String getRealPath(String path) {
if (!context.isFilesystemBased())
return null;
File file = new File(basePath, path);
return (file.getAbsolutePath());
}
/**
* Return a <code>RequestDispatcher</code> instance that acts as a
* wrapper for the resource at the given path. The path must begin
* with a "/" and is interpreted as relative to the current context root.
*
* @param path The path to the desired resource.
*/
public RequestDispatcher getRequestDispatcher(String path) {
// Validate the path argument
if (path == null)
return (null);
if (!path.startsWith("/"))
throw new IllegalArgumentException
(sm.getString
("applicationContext.requestDispatcher.iae", path));
path = normalize(path);
if (path == null)
return (null);
// Retrieve the thread local URI
MessageBytes uriMB = (MessageBytes) localUriMB.get();
if (uriMB == null) {
uriMB = new MessageBytes();
CharChunk uriCC = uriMB.getCharChunk();
uriCC.setLimit(-1);
localUriMB.set(uriMB);
} else {
uriMB.recycle();
}
// Get query string
String queryString = null;
int pos = path.indexOf('?');
if (pos >= 0) {
queryString = path.substring(pos + 1);
} else {
pos = path.length();
}
// Retrieve the thread local mapping data
MappingData mappingData = (MappingData) localMappingData.get();
if (mappingData == null) {
mappingData = new MappingData();
localMappingData.set(mappingData);
}
// Map the URI
CharChunk uriCC = uriMB.getCharChunk();
try {
uriCC.append(context.getPath(), 0, context.getPath().length());
/*
* Ignore any trailing path params (separated by ';') for mapping
* purposes
*/
int semicolon = path.indexOf(';');
uriCC.append(path, 0, semicolon > 0 ? semicolon : pos);
context.getMapper().map(uriMB, mappingData);
if (mappingData.wrapper == null) {
return (null);
}
/*
* Append any trailing path params (separated by ';') that were
* ignored for mapping purposes, so that they're reflected in the
* RequestDispatcher's requestURI
*/
if (semicolon > 0) {
uriCC.append(path, semicolon, pos - semicolon);
}
} catch (Exception e) {
// Should never happen
log(sm.getString("applicationContext.mapping.error"), e);
return (null);
}
Wrapper wrapper = (Wrapper) mappingData.wrapper;
String wrapperPath = mappingData.wrapperPath.toString();
String pathInfo = mappingData.pathInfo.toString();
mappingData.recycle();
// Construct a RequestDispatcher to process this request
return (RequestDispatcher) new ApplicationDispatcher
(wrapper, uriCC.toString(), wrapperPath, pathInfo,
queryString, null);
}
/**
* Return the URL to the resource that is mapped to a specified path.
* The path must begin with a "/" and is interpreted as relative to the
* current context root.
*
* @param path The path to the desired resource
*
* @exception MalformedURLException if the path is not given
* in the correct form
*/
public URL getResource(String path)
throws MalformedURLException {
if (path == null || !path.startsWith("/")) {
throw new MalformedURLException(sm.getString("applicationContext.requestDispatcher.iae", path));
}
path = normalize(path);
if (path == null)
return (null);
String libPath = "/WEB-INF/lib/";
if ((path.startsWith(libPath)) && (path.endsWith(".jar"))) {
File jarFile = null;
if (context.isFilesystemBased()) {
jarFile = new File(basePath, path);
} else {
jarFile = new File(context.getWorkPath(), path);
}
if (jarFile.exists()) {
return jarFile.toURL();
} else {
return null;
}
} else {
DirContext resources = context.getResources();
if (resources != null) {
String fullPath = context.getName() + path;
String hostName = context.getParent().getName();
try {
resources.lookup(path);
return new URL
("jndi", null, 0, getJNDIUri(hostName, fullPath),
new DirContextURLStreamHandler(resources));
} catch (Exception e) {
// Ignore
}
}
}
return (null);
}
/**
* Return the requested resource as an <code>InputStream</code>. The
* path must be specified according to the rules described under
* <code>getResource</code>. If no such resource can be identified,
* return <code>null</code>.
*
* @param path The path to the desired resource.
*/
public InputStream getResourceAsStream(String path) {
path = normalize(path);
if (path == null)
return (null);
DirContext resources = context.getResources();
if (resources != null) {
try {
Object resource = resources.lookup(path);
if (resource instanceof Resource)
return (((Resource) resource).streamContent());
} catch (Exception e) {
}
}
return (null);
}
/**
* Return a Set containing the resource paths of resources member of the
* specified collection. Each path will be a String starting with
* a "/" character. The returned set is immutable.
*
* @param path Collection path
*/
public Set getResourcePaths(String path) {
path = normalize(path);
if (path == null)
return (null);
DirContext resources = context.getResources();
if (resources != null) {
return (getResourcePathsInternal(resources, path));
}
return (null);
}
/**
* Internal implementation of getResourcesPath() logic.
*
* @param resources Directory context to search
* @param path Collection path
*/
private Set getResourcePathsInternal(DirContext resources, String path) {
ResourceSet set = new ResourceSet();
try {
listCollectionPaths(set, resources, path);
} catch (NamingException e) {
return (null);
}
set.setLocked(true);
return (set);
}
/**
* Return the name and version of the servlet container.
*/
public String getServerInfo() {
return (ServerInfo.getServerInfo());
}
/**
* @deprecated As of Java Servlet API 2.1, with no direct replacement.
*/
public Servlet getServlet(String name) {
return (null);
}
/**
* Return the display name of this web application.
*/
public String getServletContextName() {
return (context.getDisplayName());
}
/**
* @deprecated As of Java Servlet API 2.1, with no direct replacement.
*/
public Enumeration getServletNames() {
return (new Enumerator(empty));
}
/**
* @deprecated As of Java Servlet API 2.1, with no direct replacement.
*/
public Enumeration getServlets() {
return (new Enumerator(empty));
}
/**
* Writes the specified message to a servlet log file.
*
* @param message Message to be written
*/
public void log(String message) {
Logger logger = context.getLogger();
if (logger != null)
logger.log(context.logName() + message);
}
/**
* Writes the specified exception and message to a servlet log file.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -