001: package com.ibm.webdav.protocol.http;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017: import java.io.*;
018: import java.util.*;
019: import java.util.logging.*;
020:
021: import javax.servlet.http.*;
022: import javax.xml.parsers.*;
023:
024: import org.w3c.dom.*;
025:
026: import com.ibm.webdav.*;
027: import com.ibm.webdav.impl.*;
028:
029: /** Executes the WebDAV PROPFIND method.
030: * @author Jim Amsden <jamsden@us.ibm.com>
031: */
032: public class PropFindMethod extends WebDAVMethod {
033: private static Logger m_logger = Logger
034: .getLogger(PropFindMethod.class.getName());
035:
036: /** Construct a PropFindMethod.
037: * @param request the servlet request
038: * @param response the servlet response
039: * @exception com.ibm.webdav.WebDAVException
040: */
041: public PropFindMethod(HttpServletRequest request,
042: HttpServletResponse response) throws WebDAVException {
043: super (request, response);
044: methodName = "PROPFIND";
045: }
046:
047: /** Execute the method.
048: * @return the result status code
049: */
050: public WebDAVStatus execute() {
051: MultiStatus multiStatus = null;
052:
053: try {
054: // get any arguments out of the headers
055: String depth = context.getRequestContext().depth();
056:
057: Document contents = null;
058:
059: if (context.getRequestContext().contentLength() > 0) {
060: // get the request entity body and parse it
061: WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(
062: resource.getURL().toString());
063:
064: DocumentBuilderFactory factory = DocumentBuilderFactory
065: .newInstance();
066: factory.setNamespaceAware(true);
067:
068: DocumentBuilder docbuilder = factory
069: .newDocumentBuilder();
070: docbuilder.setErrorHandler(errorHandler);
071: contents = docbuilder
072: .parse(new org.xml.sax.InputSource(request
073: .getReader()));
074:
075: if (errorHandler.getErrorCount() > 0) {
076: throw new WebDAVException(
077: WebDAVStatus.SC_BAD_REQUEST,
078: "Syntax error in PROPFIND request entity body");
079: }
080: }
081:
082: // get the arguments for the getProperties() method, and figure
083: // out which method variant to call.
084: if (ResourceImpl.debug) {
085: System.err.println("property request entity:");
086:
087: PrintWriter pout = new PrintWriter(System.err);
088: pout.print(XMLUtility.printNode(contents
089: .getDocumentElement()));
090:
091: //((Document) contents).printWithFormat(pout);
092: }
093:
094: Element propfind = (Element) ((contents == null) ? null
095: : contents.getDocumentElement());
096:
097: if ((contents == null)
098: || (propfind == null)
099: || (propfind.getElementsByTagNameNS("DAV:",
100: "allprop").getLength() > 0)) {
101:
102: if (propfind != null) {
103: Element allpropEl = (Element) propfind
104: .getElementsByTagNameNS("DAV:", "allprop")
105: .item(0);
106: }
107:
108: if (resource.exists() && resource.isCollection()) {
109: multiStatus = ((CollectionImpl) resource)
110: .getProperties(context, depth);
111: } else {
112: multiStatus = resource.getProperties(context);
113: }
114: } else if (propfind.getElementsByTagNameNS("DAV:",
115: "propname").getLength() > 0) {
116: if (resource.exists() && resource.isCollection()) {
117: multiStatus = ((CollectionImpl) resource)
118: .getPropertyNames(context, depth);
119: } else {
120: multiStatus = resource.getPropertyNames(context);
121: }
122: } else {
123: Vector tempNames = new Vector();
124: Node prop = propfind.getElementsByTagNameNS("DAV:",
125: "prop").item(0);
126: NodeList propnames = prop.getChildNodes();
127: String sss = "bleh";
128: Element propname = null;
129:
130: for (int i = 0; i < propnames.getLength(); i++) {
131: //propname = (Element) propnames.item(i);
132: Object objj = (Object) 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: //tempNames.addElement(propname.getTagName());
144: tempNames.addElement(new PropertyName(tx));
145: }
146: }
147:
148: PropertyName[] names = new PropertyName[tempNames
149: .size()];
150: Enumeration nameEnumerator = tempNames.elements();
151: int i = 0;
152:
153: while (nameEnumerator.hasMoreElements()) {
154: names[i] = (PropertyName) nameEnumerator
155: .nextElement();
156: i++;
157: }
158:
159: if (resource.exists() && resource.isCollection()) {
160: multiStatus = ((CollectionImpl) resource)
161: .getProperties(context, names, depth);
162: } else {
163: multiStatus = resource
164: .getProperties(context, names);
165: }
166: }
167:
168: context.getResponseContext().contentType("text/xml");
169: setResponseHeaders();
170: setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
171:
172: // output the results as an XML document
173: Document results = multiStatus.asXML();
174:
175: // set the character encoding for the document to that specified by
176: // the client in the Accept header
177: //((Document) results).setEncoding(getResponseCharset());
178: if (ResourceImpl.debug) {
179: System.err.println("property results:");
180:
181: PrintWriter pout = new PrintWriter(System.err);
182: pout.print(XMLUtility.printNode(results
183: .getDocumentElement()));
184:
185: //((Document) results).printWithFormat(pout);
186: }
187:
188: //PrintWriter pout = new PrintWriter(response.getWriter(), false);
189: OutputStream os = response.getOutputStream();
190: OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
191: osw.write(XMLUtility
192: .printNode(results.getDocumentElement()));
193: //pout.print(XMLUtility.printNode(results.getDocumentElement()));
194:
195: //((Document) results).print(pout);
196: //pout.print(multiStatus.toString());
197:
198: //pout.close();
199: osw.close();
200: //os.close();
201: } catch (WebDAVException exc) {
202: if (exc.getStatusCode() == 500) {
203: m_logger.log(Level.WARNING, exc.getLocalizedMessage(),
204: exc);
205: } else {
206: m_logger.log(Level.INFO, exc.getLocalizedMessage()
207: + " - " + request.getRequestURI());
208: }
209:
210: setStatusCode(exc.getStatusCode());
211: } catch (Exception exc) {
212: m_logger.log(Level.WARNING, exc.getMessage(), exc);
213: setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
214: }
215:
216: return context.getStatusCode();
217: }
218: }
|