KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > protocol > http > PropFindMethod


1 package com.ibm.webdav.protocol.http;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import java.io.*;
18 import java.util.*;
19 import java.util.logging.*;
20
21 import javax.servlet.http.*;
22 import javax.xml.parsers.*;
23
24 import org.w3c.dom.*;
25
26 import com.ibm.webdav.*;
27 import com.ibm.webdav.impl.*;
28
29
30 /** Executes the WebDAV PROPFIND method.
31  * @author Jim Amsden <jamsden@us.ibm.com>
32  */

33 public class PropFindMethod extends WebDAVMethod {
34     private static Logger m_logger = Logger.getLogger(PropFindMethod.class.getName());
35
36     /** Construct a PropFindMethod.
37     * @param request the servlet request
38     * @param response the servlet response
39     * @exception com.ibm.webdav.WebDAVException
40     */

41     public PropFindMethod(HttpServletRequest request,
42                           HttpServletResponse response)
43                    throws WebDAVException {
44         super(request, response);
45         methodName = "PROPFIND";
46     }
47
48     /** Execute the method.
49     * @return the result status code
50     */

51     public WebDAVStatus execute() {
52         MultiStatus multiStatus = null;
53
54         try {
55             // get any arguments out of the headers
56
String JavaDoc depth = context.getRequestContext().depth();
57
58             Document contents = null;
59
60             if (context.getRequestContext().contentLength() > 0) {
61                 // get the request entity body and parse it
62
WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(
63                                                           resource.getURL()
64                                                                   .toString());
65
66
67                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
68                 factory.setNamespaceAware(true);
69
70                 DocumentBuilder docbuilder = factory.newDocumentBuilder();
71                 docbuilder.setErrorHandler(errorHandler);
72                 contents = docbuilder.parse(
73                                    new org.xml.sax.InputSource JavaDoc(
74                                            request.getReader()));
75
76                 if (errorHandler.getErrorCount() > 0) {
77                     throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
78                                               "Syntax error in PROPFIND request entity body");
79                 }
80             }
81
82             // get the arguments for the getProperties() method, and figure
83
// out which method variant to call.
84
if (ResourceImpl.debug) {
85                 System.err.println("property request entity:");
86
87                 PrintWriter pout = new PrintWriter(System.err);
88                 pout.print(XMLUtility.printNode(contents.getDocumentElement()));
89
90                 //((Document) contents).printWithFormat(pout);
91
}
92
93             Element propfind = (Element) ((contents == null)
94                                           ? null : contents.getDocumentElement());
95
96
97             if ((contents == null) || (propfind == null) ||
98                     (propfind.getElementsByTagNameNS("DAV:", "allprop")
99                              .getLength() > 0)) {
100                 
101
102                 if (propfind != null) {
103                     Element allpropEl = (Element) propfind.getElementsByTagNameNS(
104                                                           "DAV:", "allprop")
105                                                           .item(0);
106                 }
107
108                 if (resource.exists() && resource.isCollection()) {
109                     multiStatus = ((CollectionImpl) resource).getProperties(
110                                           context, depth);
111                 } else {
112                     multiStatus = resource.getProperties(context);
113                 }
114             } else if (propfind.getElementsByTagNameNS("DAV:", "propname")
115                              .getLength() > 0) {
116                 if (resource.exists() && resource.isCollection()) {
117                     multiStatus = ((CollectionImpl) resource).getPropertyNames(
118                                           context, depth);
119                 } else {
120                     multiStatus = resource.getPropertyNames(context);
121                 }
122             } else {
123                 Vector tempNames = new Vector();
124                 Node prop = propfind.getElementsByTagNameNS("DAV:", "prop")
125                                     .item(0);
126                 NodeList propnames = prop.getChildNodes();
127                 String JavaDoc sss = "bleh";
128                 Element propname = null;
129
130                 for (int i = 0; i < propnames.getLength(); i++) {
131                     //propname = (Element) propnames.item(i);
132
Object JavaDoc objj = (Object JavaDoc) propnames.item(i);
133
134                     if (objj instanceof Text) {
135                         // skip. Probably just a CRLF or something
136
Text tt = (Text) objj;
137                         sss = "x" + tt.getNodeValue() + "y";
138                     } else {
139                         propname = (Element) objj;
140
141                         Element tx = (Element) propname;
142
143
144                         //tempNames.addElement(propname.getTagName());
145
tempNames.addElement(new PropertyName(tx));
146                     }
147                 }
148
149                 PropertyName[] names = new PropertyName[tempNames.size()];
150                 Enumeration nameEnumerator = tempNames.elements();
151                 int i = 0;
152
153                 while (nameEnumerator.hasMoreElements()) {
154                     names[i] = (PropertyName) nameEnumerator.nextElement();
155                     i++;
156                 }
157
158                 if (resource.exists() && resource.isCollection()) {
159                     multiStatus = ((CollectionImpl) resource).getProperties(
160                                           context, names, depth);
161                 } else {
162                     multiStatus = resource.getProperties(context, names);
163                 }
164             }
165
166             context.getResponseContext().contentType("text/xml");
167             setResponseHeaders();
168             setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
169
170             // output the results as an XML document
171
Document results = multiStatus.asXML();
172
173             // set the character encoding for the document to that specified by
174
// the client in the Accept header
175
//((Document) results).setEncoding(getResponseCharset());
176
if (ResourceImpl.debug) {
177                 System.err.println("property results:");
178
179                 PrintWriter pout = new PrintWriter(System.err);
180                 pout.print(XMLUtility.printNode(results.getDocumentElement()));
181
182                 //((Document) results).printWithFormat(pout);
183
}
184
185             //PrintWriter pout = new PrintWriter(response.getWriter(), false);
186
OutputStream os = response.getOutputStream();
187             OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
188             osw.write(XMLUtility.printNode(results.getDocumentElement()));
189             //pout.print(XMLUtility.printNode(results.getDocumentElement()));
190

191
192             //((Document) results).print(pout);
193
//pout.print(multiStatus.toString());
194

195             //pout.close();
196
osw.close();
197             //os.close();
198
} catch (WebDAVException exc) {
199             if(exc.getStatusCode() == 500) {
200                 m_logger.log(Level.WARNING, exc.getLocalizedMessage(), exc);
201             } else {
202                 m_logger.log(Level.INFO, exc.getLocalizedMessage() + " - " + request.getRequestURI());
203             }
204             
205             setStatusCode(exc.getStatusCode());
206         } catch (Exception JavaDoc exc) {
207             m_logger.log(Level.WARNING, exc.getMessage(), exc);
208             setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
209         }
210
211         return context.getStatusCode();
212     }
213 }
Popular Tags