KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > webdav > methods > PROPFIND


1 /* ========================================================================== *
2  * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
3  * All rights reserved. *
4  * ========================================================================== *
5  * *
6  * Licensed under the Apache License, Version 2.0 (the "License"). You may *
7  * not use this file except in compliance with the License. You may obtain a *
8  * copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>. *
9  * *
10  * Unless required by applicable law or agreed to in writing, software *
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT *
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
13  * License for the specific language governing permissions and limitations *
14  * under the License. *
15  * *
16  * ========================================================================== */

17 package com.sslexplorer.vfs.webdav.methods;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.util.Iterator JavaDoc;
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 /**
37  * <p>
38  * <a HREF="http://www.rfc-editor.org/rfc/rfc2518.txt">WebDAV</a>
39  * <code>PROPFIND</code> metohd implementation.
40  * </p>
41  *
42  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
43  */

44 public class PROPFIND implements DAVMethod {
45
46     private static Log log = LogFactory.getLog(PROPFIND.class);
47
48     /**
49      * <p>
50      * Create a new {@link PROPFIND} instance.
51      * </p>
52      */

53     public PROPFIND() {
54         super();
55     }
56
57     /**
58      * <p>
59      * Process the <code>PROPFIND</code> method.
60      * </p>
61      */

62     public void process(DAVTransaction transaction, VFSResource resource) throws LockedException, IOException JavaDoc {
63         if (transaction.isRequiredRootRedirect() || !transaction.isResourcePath(resource.getFullPath())) {
64             throw new DAVRedirection(false, resource);
65         }
66
67         String JavaDoc handle = VFSLockManager.getNewHandle();
68         VFSLockManager.getInstance().lock(resource, transaction.getSessionInfo(), false, false, handle);
69         try {
70             /* Check depth */
71             int depth = transaction.getDepth();
72             if (depth > 1)
73                 new DAVException(403, "Invalid depth");
74
75             /* What to do on a collection resource */
76             transaction.setStatus(207);
77             transaction.setContentType("text/xml; charset=\"utf-8\"");
78             PrintWriter JavaDoc out = transaction.write("utf-8");
79
80             /* Output the XML declaration and the root document tag */
81             out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
82             out.println("<D:multistatus xmlns:D=\"DAV:\">");
83
84             /* Process this resource's property (always) */
85             this.process(transaction, out, resource);
86
87             /* Process this resource's children (if required) */
88             if (resource.isCollection() && (depth > 0)) {
89                 Iterator JavaDoc children = resource.getChildren();
90                 while (children.hasNext()) {
91                     VFSResource child = (VFSResource) children.next();
92                     this.process(transaction, out, child);
93                 }
94             }
95
96             /* Close up the XML Multi-Status response */
97             out.println("</D:multistatus>");
98             out.flush();
99             resource.getMount().resourceAccessList(resource, transaction, null);
100         } catch (Exception JavaDoc e) {
101             resource.getMount().resourceAccessList(resource, transaction, e);
102             IOException JavaDoc ioe = new IOException JavaDoc(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 JavaDoc out, VFSResource res) throws IOException JavaDoc {
111         out.println(" <D:response>");
112
113         if (log.isDebugEnabled())
114             log.debug("Returning " + res.getFullURI().toString());
115
116         /**
117          * LDP - This was using toASCIIString which was causing problems with drive mapping. Changed to use getFullPath seems to
118          * fix this error and non ascii characters are now being shown correctly in drive mapping, network places and web folders.
119          */

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             /* Figure out what we're dealing with here */
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 JavaDoc lmod = DAVUtilities.format(res.getLastModified());
141             this.process(out, "getlastmodified", lmod);
142             String JavaDoc 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 JavaDoc out, String JavaDoc name, String JavaDoc 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