001: /*
002: * (C) Copyright SimulacraMedia 2003. All rights reserved.
003: *
004: * Created on Nov 13, 2003
005: *
006: */
007: package com.ibm.webdav.protocol.http;
008:
009: import java.io.*;
010: import java.util.*;
011: import java.util.logging.*;
012:
013: import javax.servlet.http.*;
014: import javax.xml.parsers.*;
015:
016: import org.w3c.dom.*;
017:
018: import com.ibm.webdav.*;
019: import com.ibm.webdav.impl.*;
020:
021: /**
022: * Executes the WebDAV Delta-V Report method.
023: *
024: * @author Michael Bell
025: * @version $Revision: 1.1 $
026: *
027: */
028: public class ReportMethod extends WebDAVMethod {
029: private static Logger m_logger = Logger
030: .getLogger(ReportMethod.class.getName());
031:
032: public static final String METHOD_NAME = "REPORT";
033:
034: /**
035: * @param request
036: * @param response
037: * @throws WebDAVException
038: */
039: public ReportMethod(HttpServletRequest request,
040: HttpServletResponse response) throws WebDAVException {
041: super (request, response);
042: methodName = METHOD_NAME;
043: }
044:
045: /* (non-Javadoc)
046: * @see com.ibm.webdav.protocol.http.WebDAVMethod#execute()
047: */
048: public WebDAVStatus execute() throws WebDAVException {
049: MultiStatus multiStatus = null;
050:
051: try {
052: // get any arguments out of the headers
053: String depth = context.getRequestContext().depth();
054:
055: Document contents = null;
056:
057: if (context.getRequestContext().contentLength() > 0) {
058: // get the request entity body and parse it
059: WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(
060: resource.getURL().toString());
061:
062: DocumentBuilderFactory factory = DocumentBuilderFactory
063: .newInstance();
064: factory.setNamespaceAware(true);
065:
066: DocumentBuilder docbuilder = factory
067: .newDocumentBuilder();
068: docbuilder.setErrorHandler(errorHandler);
069: contents = docbuilder
070: .parse(new org.xml.sax.InputSource(request
071: .getReader()));
072:
073: if (errorHandler.getErrorCount() > 0) {
074: throw new WebDAVException(
075: WebDAVStatus.SC_BAD_REQUEST,
076: "Syntax error in PROPFIND request entity body");
077: }
078: }
079:
080: // get the arguments for the getProperties() method, and figure
081: // out which method variant to call.
082: if (ResourceImpl.debug) {
083: System.err.println("property request entity:");
084:
085: PrintWriter pout = new PrintWriter(System.err);
086: pout.print(XMLUtility.printNode(contents
087: .getDocumentElement()));
088:
089: //((Document) contents).printWithFormat(pout);
090: }
091:
092: Element versionTree = (Element) ((contents == null) ? null
093: : contents.getDocumentElement());
094:
095: if (versionTree == null
096: || versionTree.getElementsByTagNameNS("DAV:",
097: "version-tree").getLength() == 0) {
098: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
099: "Invalid request body");
100: } else if (versionTree.getElementsByTagNameNS("DAV:",
101: "allprop").getLength() > 0) {
102:
103: multiStatus = resource.getVersionTreeReport(context);
104:
105: } else {
106: Vector tempNames = new Vector();
107: Node prop = versionTree.getElementsByTagNameNS("DAV:",
108: "prop").item(0);
109: NodeList propnames = prop.getChildNodes();
110: String sss = "bleh";
111: Element propname = null;
112:
113: for (int i = 0; i < propnames.getLength(); i++) {
114: //propname = (Element) propnames.item(i);
115: Object objj = (Object) propnames.item(i);
116:
117: if (objj instanceof Text) {
118: // skip. Probably just a CRLF or something
119: Text tt = (Text) objj;
120: sss = "x" + tt.getNodeValue() + "y";
121: } else {
122: propname = (Element) objj;
123:
124: Element tx = (Element) propname;
125:
126: //tempNames.addElement(propname.getTagName());
127: tempNames.addElement(new PropertyName(tx));
128: }
129: }
130:
131: PropertyName[] names = new PropertyName[tempNames
132: .size()];
133: Enumeration nameEnumerator = tempNames.elements();
134: int i = 0;
135:
136: while (nameEnumerator.hasMoreElements()) {
137: names[i] = (PropertyName) nameEnumerator
138: .nextElement();
139: i++;
140: }
141:
142: if (resource.exists() == true) {
143: multiStatus = resource.getVersionTreeReport(
144: context, names);
145: }
146: }
147:
148: context.getResponseContext().contentType("text/xml");
149: setResponseHeaders();
150: setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
151:
152: // output the results as an XML document
153: Document results = multiStatus.asXML();
154:
155: // set the character encoding for the document to that specified by
156: // the client in the Accept header
157: //((Document) results).setEncoding(getResponseCharset());
158: if (ResourceImpl.debug) {
159: System.err.println("property results:");
160:
161: PrintWriter pout = new PrintWriter(System.err);
162: pout.print(XMLUtility.printNode(results
163: .getDocumentElement()));
164:
165: //((Document) results).printWithFormat(pout);
166: }
167:
168: PrintWriter pout = new PrintWriter(response.getWriter(),
169: false);
170: pout.print(XMLUtility.printNode(results
171: .getDocumentElement()));
172:
173: //((Document) results).print(pout);
174: //pout.print(multiStatus.toString());
175: pout.close();
176: } catch (WebDAVException exc) {
177: setStatusCode(exc.getStatusCode());
178: } catch (Exception exc) {
179: m_logger.log(Level.WARNING, exc.getMessage(), exc);
180: setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
181: }
182:
183: return context.getStatusCode();
184: }
185:
186: }
|