📄 webdavservlet.java
字号:
String servletPath = req.getServletPath();
if ((servletPath != null) &&
(destinationPath.startsWith(servletPath))) {
destinationPath = destinationPath
.substring(servletPath.length());
}
}
if (debug > 0)
System.out.println("Dest path :" + destinationPath);
if ((destinationPath.toUpperCase().startsWith("/WEB-INF")) ||
(destinationPath.toUpperCase().startsWith("/META-INF"))) {
resp.sendError(WebdavStatus.SC_FORBIDDEN);
return false;
}
String path = getRelativePath(req);
if ((path.toUpperCase().startsWith("/WEB-INF")) ||
(path.toUpperCase().startsWith("/META-INF"))) {
resp.sendError(WebdavStatus.SC_FORBIDDEN);
return false;
}
if (destinationPath.equals(path)) {
resp.sendError(WebdavStatus.SC_FORBIDDEN);
return false;
}
// Parsing overwrite header
boolean overwrite = true;
String overwriteHeader = req.getHeader("Overwrite");
if (overwriteHeader != null) {
if (overwriteHeader.equalsIgnoreCase("T")) {
overwrite = true;
} else {
overwrite = false;
}
}
// Overwriting the destination
// Retrieve the resources
DirContext resources = getResources();
if (resources == null) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return false;
}
boolean exists = true;
try {
resources.lookup(destinationPath);
} catch (NamingException e) {
exists = false;
}
if (overwrite) {
// Delete destination resource, if it exists
if (exists) {
if (!deleteResource(destinationPath, req, resp)) {
return false;
} else {
resp.setStatus(WebdavStatus.SC_NO_CONTENT);
}
} else {
resp.setStatus(WebdavStatus.SC_CREATED);
}
} else {
// If the destination exists, then it's a conflict
if (exists) {
resp.sendError(WebdavStatus.SC_PRECONDITION_FAILED);
return false;
}
}
// Copying source to destination
Hashtable errorList = new Hashtable();
boolean result = copyResource(resources, errorList,
path, destinationPath);
if ((!result) || (!errorList.isEmpty())) {
sendReport(req, resp, errorList);
return false;
}
// Removing any lock-null resource which would be present at
// the destination path
lockNullResources.remove(destinationPath);
return true;
}
/**
* Copy a collection.
*
* @param resources Resources implementation to be used
* @param errorList Hashtable containing the list of errors which occurred
* during the copy operation
* @param source Path of the resource to be copied
* @param dest Destination path
*/
private boolean copyResource(DirContext resources, Hashtable errorList,
String source, String dest) {
if (debug > 1)
System.out.println("Copy: " + source + " To: " + dest);
Object object = null;
try {
object = resources.lookup(source);
} catch (NamingException e) {
}
if (object instanceof DirContext) {
try {
resources.createSubcontext(dest);
} catch (NamingException e) {
errorList.put
(dest, new Integer(WebdavStatus.SC_CONFLICT));
return false;
}
try {
NamingEnumeration enum = resources.list(source);
while (enum.hasMoreElements()) {
NameClassPair ncPair = (NameClassPair) enum.nextElement();
String childDest = dest;
if (!childDest.equals("/"))
childDest += "/";
childDest += ncPair.getName();
String childSrc = source;
if (!childSrc.equals("/"))
childSrc += "/";
childSrc += ncPair.getName();
copyResource(resources, errorList, childSrc, childDest);
}
} catch (NamingException e) {
errorList.put
(dest, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return false;
}
} else {
if (object instanceof Resource) {
try {
resources.bind(dest, object);
} catch (NamingException e) {
errorList.put
(source,
new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return false;
}
} else {
errorList.put
(source,
new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return false;
}
}
return true;
}
/**
* Delete a resource.
*
* @param req Servlet request
* @param resp Servlet response
* @return boolean true if the copy is successful
*/
private boolean deleteResource(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
String path = getRelativePath(req);
return deleteResource(path, req, resp);
}
/**
* Delete a resource.
*
* @param path Path of the resource which is to be deleted
* @param req Servlet request
* @param resp Servlet response
*/
private boolean deleteResource(String path, HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
if ((path.toUpperCase().startsWith("/WEB-INF")) ||
(path.toUpperCase().startsWith("/META-INF"))) {
resp.sendError(WebdavStatus.SC_FORBIDDEN);
return false;
}
String ifHeader = req.getHeader("If");
if (ifHeader == null)
ifHeader = "";
String lockTokenHeader = req.getHeader("Lock-Token");
if (lockTokenHeader == null)
lockTokenHeader = "";
if (isLocked(path, ifHeader + lockTokenHeader)) {
resp.sendError(WebdavStatus.SC_LOCKED);
return false;
}
// Retrieve the resources
DirContext resources = getResources();
if (resources == null) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return false;
}
boolean exists = true;
Object object = null;
try {
object = resources.lookup(path);
} catch (NamingException e) {
exists = false;
}
if (!exists) {
resp.sendError(WebdavStatus.SC_NOT_FOUND);
return false;
}
boolean collection = (object instanceof DirContext);
if (!collection) {
try {
resources.unbind(path);
} catch (NamingException e) {
resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
return false;
}
} else {
Hashtable errorList = new Hashtable();
deleteCollection(req, resources, path, errorList);
try {
resources.unbind(path);
} catch (NamingException e) {
errorList.put(path, new Integer
(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
}
if (!errorList.isEmpty()) {
sendReport(req, resp, errorList);
return false;
}
}
resp.setStatus(WebdavStatus.SC_NO_CONTENT);
return true;
}
/**
* Deletes a collection.
*
* @param resources Resources implementation associated with the context
* @param path Path to the collection to be deleted
* @param errorList Contains the list of the errors which occurred
*/
private void deleteCollection(HttpServletRequest req,
DirContext resources,
String path, Hashtable errorList) {
if (debug > 1)
System.out.println("Delete:" + path);
if ((path.toUpperCase().startsWith("/WEB-INF")) ||
(path.toUpperCase().startsWith("/META-INF"))) {
errorList.put(path, new Integer(WebdavStatus.SC_FORBIDDEN));
return;
}
String ifHeader = req.getHeader("If");
if (ifHeader == null)
ifHeader = "";
String lockTokenHeader = req.getHeader("Lock-Token");
if (lockTokenHeader == null)
lockTokenHeader = "";
Enumeration enum = null;
try {
enum = resources.list(path);
} catch (NamingException e) {
errorList.put(path, new Integer
(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return;
}
while (enum.hasMoreElements()) {
NameClassPair ncPair = (NameClassPair) enum.nextElement();
String childName = path;
if (!childName.equals("/"))
childName += "/";
childName += ncPair.getName();
if (isLocked(childName, ifHeader + lockTokenHeader)) {
errorList.put(childName, new Integer(WebdavStatus.SC_LOCKED));
} else {
try {
Object object = resources.lookup(childName);
if (object instanceof DirContext) {
deleteCollection(req, resources, childName, errorList);
}
try {
resources.unbind(childName);
} catch (NamingException e) {
if (!(object instanceof DirContext)) {
// If it's not a collection, then it's an unknown
// error
errorList.put
(childName, new Integer
(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
}
}
} catch (NamingException e) {
errorList.put
(childName, new Integer
(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
}
}
}
}
/**
* Send a multistatus element containing a complete error report to the
* client.
*
* @param req Servlet request
* @param resp Servlet response
* @param errorList List of error to be displayed
*/
private void sendReport(HttpServletRequest req, HttpServletResponse resp,
Hashtable errorList)
throws ServletException, IOException {
resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
String absoluteUri = req.getRequestURI();
String relativePath = getRelativePath(req);
XMLWriter generatedXML = new XMLWriter();
generatedXML.writeXMLHeader();
generatedXML.writeElement(null, "multistatus"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -