1 17 package com.sslexplorer.vfs.webdav.methods; 18 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.util.Iterator ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import com.maverick.util.URLUTF8Encoder; 27 import com.sslexplorer.vfs.VFSLockManager; 28 import com.sslexplorer.vfs.VFSResource; 29 import com.sslexplorer.vfs.webdav.DAVException; 30 import com.sslexplorer.vfs.webdav.DAVMethod; 31 import com.sslexplorer.vfs.webdav.DAVRedirection; 32 import com.sslexplorer.vfs.webdav.DAVTransaction; 33 import com.sslexplorer.vfs.webdav.DAVUtilities; 34 import com.sslexplorer.vfs.webdav.LockedException; 35 36 44 public class PROPFIND implements DAVMethod { 45 46 private static Log log = LogFactory.getLog(PROPFIND.class); 47 48 53 public PROPFIND() { 54 super(); 55 } 56 57 62 public void process(DAVTransaction transaction, VFSResource resource) throws LockedException, IOException { 63 if (transaction.isRequiredRootRedirect() || !transaction.isResourcePath(resource.getFullPath())) { 64 throw new DAVRedirection(false, resource); 65 } 66 67 String handle = VFSLockManager.getNewHandle(); 68 VFSLockManager.getInstance().lock(resource, transaction.getSessionInfo(), false, false, handle); 69 try { 70 71 int depth = transaction.getDepth(); 72 if (depth > 1) 73 new DAVException(403, "Invalid depth"); 74 75 76 transaction.setStatus(207); 77 transaction.setContentType("text/xml; charset=\"utf-8\""); 78 PrintWriter out = transaction.write("utf-8"); 79 80 81 out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 82 out.println("<D:multistatus xmlns:D=\"DAV:\">"); 83 84 85 this.process(transaction, out, resource); 86 87 88 if (resource.isCollection() && (depth > 0)) { 89 Iterator children = resource.getChildren(); 90 while (children.hasNext()) { 91 VFSResource child = (VFSResource) children.next(); 92 this.process(transaction, out, child); 93 } 94 } 95 96 97 out.println("</D:multistatus>"); 98 out.flush(); 99 resource.getMount().resourceAccessList(resource, transaction, null); 100 } catch (Exception e) { 101 resource.getMount().resourceAccessList(resource, transaction, e); 102 IOException ioe = new IOException (e.getMessage()); 103 ioe.initCause(e); 104 throw ioe; 105 } finally { 106 VFSLockManager.getInstance().unlock(transaction.getSessionInfo(), handle); 107 } 108 } 109 110 private void process(DAVTransaction txn, PrintWriter out, VFSResource res) throws IOException { 111 out.println(" <D:response>"); 112 113 if (log.isDebugEnabled()) 114 log.debug("Returning " + res.getFullURI().toString()); 115 116 120 121 out.println(" <D:href>" + (txn.getRequest().isSecure() ? "https" : "http") + "://" + txn.getRequest().getHeader("Host") 122 + "/fs" + URLUTF8Encoder.encode(res.getFullPath(), false) + "</D:href>"); 123 out.println(" <D:propstat>"); 124 125 if (res.isNull()) { 126 out.println(" <D:status>HTTP/1.1 404 Not Found</D:status>"); 127 128 } else { 129 out.println(" <D:prop>"); 130 131 132 if (res.isCollection()) { 133 this.process(out, "resourcetype", "<D:collection/>"); 134 this.process(out, "getcontenttype", GET.COLLECTION_MIME_TYPE); 135 } else { 136 this.process(out, "getcontenttype", res.getContentType()); 137 } 138 139 this.process(out, "getetag", res.getEntityTag()); 140 String lmod = DAVUtilities.format(res.getLastModified()); 141 this.process(out, "getlastmodified", lmod); 142 String clen = DAVUtilities.format(res.getContentLength()); 143 this.process(out, "getcontentlength", clen); 144 145 out.println(" </D:prop>"); 146 147 out.println(" <D:status>HTTP/1.1 200 OK</D:status>"); 148 149 } 150 151 out.println(" </D:propstat>"); 152 out.println(" </D:response>"); 153 } 154 155 private void process(PrintWriter out, String name, String value) { 156 if (value == null) 157 return; 158 out.print(" <D:"); 159 out.print(name); 160 out.print(">"); 161 out.print(value); 162 out.print("</D:"); 163 out.print(name); 164 out.println(">"); 165 } 166 167 168 } 169 | Popular Tags |