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 PROPPATCH method.
030: * @author Jim Amsden <jamsden@us.ibm.com>
031: */
032: public class PropPatchMethod extends WebDAVMethod {
033: private static Logger m_logger = Logger
034: .getLogger(PropPatchMethod.class.getName());
035:
036: /** Construct a PropPatchMethod.
037: * @param request the servlet request
038: * @param response the servlet response
039: * @exception com.ibm.webdav.WebDAVException
040: */
041: public PropPatchMethod(HttpServletRequest request,
042: HttpServletResponse response) throws WebDAVException {
043: super (request, response);
044: methodName = "PROPPATCH";
045: context.setMethodName(methodName);
046: }
047:
048: /** Execute the method.
049: * @return the result status code
050: */
051: public WebDAVStatus execute() {
052: setStatusCode(WebDAVStatus.SC_OK); // the default status code
053: MultiStatus multiStatus = null;
054: try {
055: // get any arguments out of the headers
056: String depth = context.getRequestContext().depth();
057:
058: // get the request entity body and parse it
059: WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(
060: resource.getURL().toString());
061: DocumentBuilderFactory factory = DocumentBuilderFactory
062: .newInstance();
063: factory.setNamespaceAware(true);
064: DocumentBuilder docbuilder = factory.newDocumentBuilder();
065: docbuilder.setErrorHandler(errorHandler);
066: /*Parser xmlParser = new Parser(resource.getURL().toString(), errorListener, null);
067: xmlParser.setWarningNoDoctypeDecl(false);
068: xmlParser.setProcessNamespace(true);
069: Document contents = xmlParser.readStream(request.getReader());*/
070: Document contents = docbuilder
071: .parse(new org.xml.sax.InputSource(request
072: .getReader()));
073: if (errorHandler.getErrorCount() > 0) {
074: if (true)
075: throw new RuntimeException();
076: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
077: "Syntax error in PROPPATCH request entity body");
078: }
079: if (ResourceImpl.debug) {
080: System.err.println("property update request entity:");
081: PrintWriter pout = new PrintWriter(System.err);
082: pout.print(XMLUtility.printNode(contents
083: .getDocumentElement()));
084: //((Document) contents).printWithFormat(pout);
085: }
086:
087: context.setMethodName(methodName);
088: multiStatus = resource.setProperties(context, contents);
089: Enumeration responses = multiStatus.getResponses();
090:
091: if (responses.hasMoreElements()) {
092: // there's more than one response, so return a multistatus
093: context.getResponseContext().contentType("text/xml");
094: setResponseHeaders();
095: setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
096:
097: // output the results as an XML document
098: Document results = multiStatus.asXML();
099: //((Document) results).setEncoding(getResponseCharset());
100: if (ResourceImpl.debug) {
101: System.err.println("property update results:");
102: PrintWriter pout = new PrintWriter(System.err);
103: pout.print(XMLUtility.printNode(results
104: .getDocumentElement()));
105: //((Document) results).printWithFormat(pout);
106: }
107: PrintWriter pout = new PrintWriter(
108: response.getWriter(), false);
109: //((Document) results).print(pout);
110: pout.print(multiStatus.toString());
111: pout.close();
112: } else {
113: setStatusCode(WebDAVStatus.SC_OK); // the default status code
114: setResponseHeaders();
115: }
116: } catch (WebDAVException exc) {
117: m_logger.log(Level.INFO, exc.getLocalizedMessage() + " - "
118: + request.getQueryString());
119: setStatusCode(exc.getStatusCode());
120: } catch (Exception exc) {
121: m_logger.log(Level.WARNING, exc.getMessage(), exc);
122: setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
123: }
124: return context.getStatusCode();
125: }
126: }
|