📄 filedircontext.java
字号:
public NamingEnumeration search(String name, String filter,
SearchControls cons)
throws NamingException {
return null;
}
/**
* Searches in the named context or object for entries that satisfy the
* given search filter. Performs the search as specified by the search
* controls.
*
* @param name the name of the context or object to search
* @param filterExpr the filter expression to use for the search.
* The expression may contain variables of the form "{i}" where i is a
* nonnegative integer. May not be null.
* @param filterArgs the array of arguments to substitute for the
* variables in filterExpr. The value of filterArgs[i] will replace each
* occurrence of "{i}". If null, equivalent to an empty array.
* @param cons the search controls that control the search. If null, the
* default search controls are used (equivalent to (new SearchControls())).
* @return an enumeration of SearchResults of the objects that satisy the
* filter; never null
* @exception ArrayIndexOutOfBoundsException if filterExpr contains {i}
* expressions where i is outside the bounds of the array filterArgs
* @exception InvalidSearchControlsException if cons contains invalid
* settings
* @exception InvalidSearchFilterException if filterExpr with filterArgs
* represents an invalid search filter
* @exception NamingException if a naming exception is encountered
*/
public NamingEnumeration search(String name, String filterExpr,
Object[] filterArgs, SearchControls cons)
throws NamingException {
return null;
}
// ------------------------------------------------------ Protected Methods
/**
* Return a context-relative path, beginning with a "/", that represents
* the canonical version of the specified path after ".." and "." elements
* are resolved out. If the specified path attempts to go outside the
* boundaries of the current context (i.e. too many ".." path elements
* are present), return <code>null</code> instead.
*
* @param path Path to be normalized
*/
protected String normalize(String path) {
String normalized = path;
// Normalize the slashes and add leading slash if necessary
if (normalized.indexOf('\\') >= 0)
normalized = normalized.replace('\\', '/');
if (!normalized.startsWith("/"))
normalized = "/" + normalized;
// Resolve occurrences of "//" in the normalized path
while (true) {
int index = normalized.indexOf("//");
if (index < 0)
break;
normalized = normalized.substring(0, index) +
normalized.substring(index + 1);
}
// Resolve occurrences of "/./" in the normalized path
while (true) {
int index = normalized.indexOf("/./");
if (index < 0)
break;
normalized = normalized.substring(0, index) +
normalized.substring(index + 2);
}
// Resolve occurrences of "/../" in the normalized path
while (true) {
int index = normalized.indexOf("/../");
if (index < 0)
break;
if (index == 0)
return (null); // Trying to go outside our context
int index2 = normalized.lastIndexOf('/', index - 1);
normalized = normalized.substring(0, index2) +
normalized.substring(index + 3);
}
// Return the normalized path that we have completed
return (normalized);
}
/**
* Return a File object representing the specified normalized
* context-relative path if it exists and is readable. Otherwise,
* return <code>null</code>.
*
* @param name Normalized context-relative path (with leading '/')
*/
protected File file(String name) {
File file = new File(base, name);
if (file.exists() && file.canRead()) {
// Check that this file belongs to our root path
String canPath = null;
try {
canPath = file.getCanonicalPath();
} catch (IOException e) {
}
if (canPath == null)
return null;
// Check to see if going outside of the web application root
if ((!allowLinking) && (!canPath.startsWith(absoluteBase))) {
return null;
}
// Case sensitivity check
if (!allowLinking && caseSensitive) {
String fileAbsPath = file.getAbsolutePath();
if (fileAbsPath.endsWith("."))
fileAbsPath = fileAbsPath + "/";
String absPath = normalize(fileAbsPath);
if (canPath != null)
canPath = normalize(canPath);
if ((absoluteBase.length() < absPath.length())
&& (absoluteBase.length() < canPath.length())) {
absPath = absPath.substring(absoluteBase.length() + 1);
if ((canPath == null) || (absPath == null))
return null;
if (absPath.equals(""))
absPath = "/";
canPath = canPath.substring(absoluteBase.length() + 1);
if (canPath.equals(""))
canPath = "/";
if (!canPath.equals(absPath))
return null;
}
}
} else {
return null;
}
return file;
}
/**
* List the resources which are members of a collection.
*
* @param file Collection
* @return Vector containg NamingEntry objects
*/
protected Vector list(File file) {
Vector entries = new Vector();
if (!file.isDirectory())
return entries;
String[] names = file.list();
Arrays.sort(names); // Sort alphabetically
if (names == null)
return entries;
NamingEntry entry = null;
for (int i = 0; i < names.length; i++) {
File currentFile = new File(file, names[i]);
Object object = null;
if (currentFile.isDirectory()) {
FileDirContext tempContext = new FileDirContext(env);
tempContext.setDocBase(file.getPath());
object = tempContext;
} else {
object = new FileResource(currentFile);
}
entry = new NamingEntry(names[i], object, NamingEntry.ENTRY);
entries.addElement(entry);
}
return entries;
}
// ----------------------------------------------- FileResource Inner Class
/**
* This specialized resource implementation avoids opening the IputStream
* to the file right away (which would put a lock on the file).
*/
protected class FileResource extends Resource {
// -------------------------------------------------------- Constructor
public FileResource(File file) {
this.file = file;
}
// --------------------------------------------------- Member Variables
/**
* Associated file object.
*/
protected File file;
/**
* File length.
*/
protected long length = -1L;
// --------------------------------------------------- Resource Methods
/**
* Content accessor.
*
* @return InputStream
*/
public InputStream streamContent()
throws IOException {
if (binaryContent == null) {
inputStream = new FileInputStream(file);
}
return super.streamContent();
}
}
// ------------------------------------- FileResourceAttributes Inner Class
/**
* This specialized resource attribute implementation does some lazy
* reading (to speed up simple checks, like checking the last modified
* date).
*/
protected class FileResourceAttributes extends ResourceAttributes {
// -------------------------------------------------------- Constructor
public FileResourceAttributes(File file) {
this.file = file;
}
// --------------------------------------------------- Member Variables
protected File file;
protected boolean accessed = false;
// ----------------------------------------- ResourceAttributes Methods
/**
* Is collection.
*/
public boolean isCollection() {
if (!accessed) {
collection = file.isDirectory();
accessed = true;
}
return super.isCollection();
}
/**
* Get content length.
*
* @return content length value
*/
public long getContentLength() {
if (contentLength != -1L)
return contentLength;
contentLength = file.length();
return contentLength;
}
/**
* Get creation time.
*
* @return creation time value
*/
public long getCreation() {
if (creation != -1L)
return creation;
creation = file.lastModified();
return creation;
}
/**
* Get creation date.
*
* @return Creation date value
*/
public Date getCreationDate() {
if (creation == -1L) {
creation = file.lastModified();
}
return super.getCreationDate();
}
/**
* Get last modified time.
*
* @return lastModified time value
*/
public long getLastModified() {
if (lastModified != -1L)
return lastModified;
lastModified = file.lastModified();
return lastModified;
}
/**
* Get lastModified date.
*
* @return LastModified date value
*/
public Date getLastModifiedDate() {
if (lastModified == -1L) {
lastModified = file.lastModified();
}
return super.getLastModifiedDate();
}
/**
* Get name.
*
* @return Name value
*/
public String getName() {
if (name == null)
name = file.getName();
return name;
}
/**
* Get resource type.
*
* @return String resource type
*/
public String getResourceType() {
if (!accessed) {
collection = file.isDirectory();
accessed = true;
}
return super.getResourceType();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -