webdavservlet.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,536 行 · 第 1/3 页
JAVA
1,536 行
private void handleMkcol(HttpServletRequest req, HttpServletResponse res, WriteStream out) throws ServletException, IOException { res.setContentType("text/xml; charset=\"utf-8\""); ServletContext app = getServletContext(); String pathInfo = req.getPathInfo(); if (pathInfo == null) pathInfo = "/"; if (_path.exists(pathInfo, req, app)) { res.sendError(res.SC_METHOD_NOT_ALLOWED, "Collection already exists"); return; } if (! _path.isDirectory(getParent(pathInfo), req, app)) { res.sendError(res.SC_CONFLICT, "MKCOL needs parent collection"); return; } InputStream is = req.getInputStream(); int ch = is.read(); if (ch >= 0) { res.sendError(res.SC_UNSUPPORTED_MEDIA_TYPE, "MKCOL doesn't understand content-type"); return; } if (! _path.mkdir(pathInfo, req, app)) { res.sendError(res.SC_FORBIDDEN, "MKCOL forbidden"); return; } res.setHeader("Location", req.getRequestURI()); sendError(res, out, res.SC_CREATED, null, "Created collection " + HTTPUtil.encodeString(req.getRequestURI())); } private void addAllProperties(ArrayList<AttributeName> properties, String pathInfo, HttpServletRequest req, Application app) throws IOException, ServletException { properties.add(new AttributeName("DAV:", "resourcetype", "D:resourcetype")); properties.add(new AttributeName("DAV:", "getcontenttype", "D:getcontenttype")); properties.add(new AttributeName("DAV:", "getcontentlength", "D:getcontentlength")); properties.add(new AttributeName("DAV:", "creationdate", "D:creationdate")); properties.add(new AttributeName("DAV:", "getlastmodified", "D:getlastmodified")); Iterator<AttributeName> iter = _path.getAttributeNames(pathInfo, req, app); while (iter.hasNext()) { AttributeName name = iter.next(); if (! properties.contains(name)) properties.add(name); } } private void printPathProperties(WriteStream out, HttpServletRequest req, ServletContext app, String uri, String pathInfo, ArrayList<AttributeName> properties, boolean isPropname, int depth) throws IOException, ServletException { out.println("<D:response>"); out.print("<D:href>"); out.print(escapeXml(uri)); out.println("</D:href>"); if (! _path.exists(pathInfo, req, app)) { out.println("<D:propstat>"); out.println("<D:status>HTTP/1.1 404 Not Found</D:status>"); out.println("</D:propstat>"); out.println("</D:response>"); return; } ArrayList<AttributeName> unknownProperties = new ArrayList<AttributeName>(); out.println("<D:propstat>"); out.println("<D:prop>"); boolean isDirectory = _path.isDirectory(pathInfo, req, app); for (int j = 0; j < properties.size(); j++) { AttributeName prop = properties.get(j); String localName = prop.getLocal(); String propUri = prop.getNamespace(); String qName = prop.getName(); String prefix = prop.getPrefix(); if (isPropname) { if (propUri.equals("DAV:")) out.println("<D:" + localName + "/>"); else { String nsPrefix; if (prefix.equals("D")) { prefix = "caucho-D"; qName = "caucho-D:" + localName; } if (prefix.equals("")) nsPrefix = "xmlns"; else nsPrefix = "xmlns:" + prefix; out.println("<" + qName + " " + nsPrefix + "=\"" + propUri + "\"/>"); } continue; } String value = _path.getAttribute(prop, pathInfo, req, app); if (value != null) { String nsPrefix; if (prefix.equals("D")) { prefix = "caucho-D"; qName = "caucho-D:" + localName; } if (prefix.equals("")) nsPrefix = "xmlns"; else nsPrefix = "xmlns:" + prefix; out.print("<" + qName + " " + nsPrefix + "=\"" + propUri + "\">"); out.print(value); out.println("</" + prop.getName() + ">"); continue; } if (! propUri.equals("DAV:")) { unknownProperties.add(prop); } else if (localName.equals("resourcetype")) { if (isDirectory) { out.print("<D:resourcetype>"); out.print("<D:collection/>"); out.println("</D:resourcetype>"); } else { out.println("<D:resourcetype/>"); } } else if (localName.equals("getcontentlength")) { out.print("<D:getcontentlength>"); out.print(_path.getLength(pathInfo, req, app)); out.println("</D:getcontentlength>"); } else if (localName.equals("getlastmodified")) { out.print("<D:getlastmodified>"); out.print(_calendar.formatGMT(_path.getLastModified(pathInfo, req, app))); out.println("</D:getlastmodified>"); } else if (localName.equals("creationdate")) { out.print("<D:creationdate>"); long time = _path.getLastModified(pathInfo, req, app); out.print(_calendar.formatGMT(time, "%Y-%m-%dT%H:%M:%SZ")); out.println("</D:creationdate>"); } else if (localName.equals("displayname")) { out.print("<D:displayname>"); String name = pathInfo; if (name.endsWith("/")) name = name.substring(0, name.length() - 1); int p = pathInfo.lastIndexOf('/'); if (p > 0 && p < pathInfo.length()) name = pathInfo.substring(p + 1); out.print(escapeXml(name)); out.println("</D:displayname>"); } else if (localName.equals("getcontenttype")) { String mimeType = app.getMimeType(uri); if (mimeType != null) { out.print("<D:getcontenttype>"); out.print(mimeType); out.println("</D:getcontenttype>"); } else { out.println("<D:getcontenttype/>"); } } else unknownProperties.add(prop); } out.println("</D:prop>"); out.println("<D:status>HTTP/1.1 200 OK</D:status>"); out.println("</D:propstat>"); if (unknownProperties.size() != 0) { out.println("<D:propstat>"); out.println("<D:prop>"); for (int j = 0; j < unknownProperties.size(); j++) { AttributeName prop = (AttributeName) unknownProperties.get(j); if (prop.getNamespace().equals("DAV:")) out.println("<D:" + prop.getLocal() + "/>"); else { String nsPrefix; String prefix = prop.getPrefix(); String qName = prop.getName(); if (prefix.equals("D")) { prefix = "caucho-D"; qName = "caucho-D:" + prop.getLocal(); } if (prefix.equals("")) nsPrefix = "xmlns"; else nsPrefix = "xmlns:" + prefix; out.println("<" + qName + " " + nsPrefix + "=\"" + prop.getNamespace() + "\"/>"); } } out.println("</D:prop>"); out.println("<D:status>HTTP/1.1 404 Not Found</D:status>"); out.println("</D:propstat>"); } out.println("</D:response>"); if (depth > 0 && _path.isDirectory(pathInfo, req, app)) { String []list = _path.list(pathInfo, req, app); ArrayList<String> sortedList = new ArrayList<String>(); for (int i = 0; i < list.length; i++) sortedList.add(list[i]); Collections.sort(sortedList); for (int i = 0; i < sortedList.size(); i++) { String filename = sortedList.get(i); String suburi; if (uri.endsWith("/")) suburi = uri + filename; else suburi = uri + "/" + filename; String subpath; if (pathInfo.endsWith("/")) subpath = pathInfo + filename; else subpath = pathInfo + "/" + filename; if (_path.isDirectory(subpath, req, app)) suburi = suburi + '/'; if (! _path.canRead(subpath, req, app) || filename.startsWith(".") || filename.equals("CVS") || filename.endsWith("~")) continue; printPathProperties(out, req, app, suburi, subpath, properties, isPropname, depth - 1); } } } private void handleDelete(HttpServletRequest req, HttpServletResponse res, WriteStream out) throws ServletException, IOException { ServletContext app = getServletContext(); String pathInfo = req.getPathInfo(); if (pathInfo == null) pathInfo = "/"; String uri = req.getContextPath() + pathInfo; if (_path.isFile(pathInfo, req, app)) { if (! _path.remove(pathInfo, req, app)) res.sendError(403, "Forbidden"); else res.setStatus(204, "No Content"); } else if (_path.isDirectory(pathInfo, req, app)) { if (deleteRecursive(req, res, out, uri, pathInfo, false)) { out.println("<D:status>HTTP/1.0 403 Forbidden</D:status>"); out.println("</D:response>"); out.println("</D:multistatus>"); } else res.setStatus(204, "No Content"); } else { res.sendError(res.SC_NOT_FOUND); } } private boolean deleteRecursive(HttpServletRequest req, HttpServletResponse res, WriteStream out, String uri, String pathInfo, boolean hasError) throws IOException { ServletContext app = getServletContext(); boolean newError = false; if (_path.isDirectory(pathInfo, req, app)) { String []list = _path.list(pathInfo, req, app); for (int i = 0; i < list.length; i++) { try { String suburi = lookup(uri, list[i]); String subpath = lookup(pathInfo, list[i]); hasError = deleteRecursive(req, res, out, suburi, subpath, hasError); } catch (IOException e) { log.log(Level.WARNING, e.toString(), e); } } if (! _path.rmdir(pathInfo, req, app)) newError = true; } else if (! _path.remove(pathInfo, req, app)) newError = true; if (newError) { if (! hasError) { startMultistatus(res, out); out.println("<D:response>"); } out.println("<D:href>" + escapeXml(uri) + "</D:href>"); hasError = true; } return hasError; } private void handleCopy(HttpServletRequest req, HttpServletResponse res, WriteStream out, int depth) throws ServletException, IOException { ServletContext app = getServletContext(); String pathInfo = req.getPathInfo(); if (pathInfo == null) pathInfo = "/"; if (depth == 1) depth = Integer.MAX_VALUE; if (! _path.exists(pathInfo, req, app)) { res.sendError(res.SC_NOT_FOUND); return; } String destURI = getDestination(req); if (destURI == null) { res.sendError(403, "Forbidden"); return; } String prefix = req.getContextPath(); if (req.getServletPath() != null) prefix += req.getServletPath(); if (! destURI.startsWith(prefix)) { res.sendError(403, "Forbidden"); return; } String destPath = destURI.substring(prefix.length()); if (destPath.equals(pathInfo)) { res.sendError(403, "Forbidden"); return; } else if (destPath.startsWith(pathInfo) && (pathInfo.endsWith("/") || destPath.startsWith(pathInfo + '/'))) { res.sendError(403, "Forbidden"); return; } else if (pathInfo.startsWith(destPath) && (destPath.endsWith("/") || pathInfo.startsWith(destPath + '/'))) { res.sendError(403, "Forbidden"); return; } String overwrite = req.getHeader("Overwrite"); if (overwrite == null) overwrite = "T"; if (! _path.exists(destPath, req, app)) { res.setStatus(res.SC_CREATED); } else if (! overwrite.equals("F")) { removeRecursive(destPath, req); res.setStatus(204, "No Content"); } else { res.sendError(412, "Overwrite not allowed for COPY"); return; } if (! _path.exists(getParent(destPath), req, app)) { res.sendError(409, "COPY needs parent of destination"); return; } if (_path.isFile(pathInfo, req, app)) { OutputStream os = _path.openWrite(destPath, req, app); WriteStream ws = Vfs.openWrite(os); try { InputStream is = _path.openRead(pathInfo, req, app); try { ws.writeStream(is); } finally { is.close(); } } finally { ws.close(); } return; } else { copyRecursive(pathInfo, destPath, depth, req); return; } } private void removeRecursive(String pathInfo, HttpServletRequest req) throws IOException { ServletContext app = getServletContext(); if (_path.isDirectory(pathInfo, req, app)) { String []list = _path.list(pathInfo, req, app); for (int i = 0; i < list.length; i++) { try { removeRecursive(lookup(pathInfo, list[i]), req); } catch (IOException e) { log.log(Level.WARNING, e.toString(), e); } } } _path.remove(pathInfo, req, app); } private void copyRecursive(String srcPath, String destPath, int depth, HttpServletRequest req) throws IOException { ServletContext app = getServletContext(); if (_path.isDirectory(srcPath, req, app)) { _path.mkdir(destPath, req, app); if (depth == 0) return; String []list = _path.list(srcPath, req, app); for (int i = 0; i < list.length; i++) { try { copyRecursive(lookup(srcPath, list[i]), lookup(destPath, list[i]), depth - 1, req); } catch (IOException e) { log.log(Level.WARNING, e.toString(), e); } } } else { OutputStream os = _path.openWrite(destPath, req, app); WriteStream ws = Vfs.openWrite(os); try { InputStream is = _path.openRead(srcPath, req, app); try { ws.writeStream(is); } finally { is.close(); } } finally { ws.close(); } } } private void handleMove(HttpServletRequest req, HttpServletResponse res, WriteStream out) throws ServletException, IOException { ServletContext app = getServletContext();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?