001: /*
002: * (C) Copyright Simulacra Media Ltd, 2004. All rights reserved.
003: *
004: * The program is provided "AS IS" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * Simulacra Media Ltd will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will Simulacra Media Ltd be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * Simulacra Media Ltd has been advised of the possibility of their occurrence.
011: * Simulacra Media Ltd will not be liable for any third party claims against you.
012: *
013: */
014: package com.ibm.webdav.protocol.http;
015:
016: import java.io.*;
017: import java.util.*;
018: import java.util.logging.*;
019:
020: import javax.servlet.http.*;
021: import javax.xml.parsers.*;
022:
023: import org.w3c.dom.*;
024:
025: import com.ibm.webdav.*;
026: import com.ibm.webdav.impl.*;
027:
028: /**
029: * Executes the WebDAV ordered collections ORDERPATCH method.
030: *
031: * @author Michael Bell
032: * @version $Revision: 1.1 $
033: * @since November 21, 2003
034: */
035: public class OrderPatchMethod extends WebDAVMethod {
036:
037: private static Logger m_logger = Logger
038: .getLogger(OrderPatchMethod.class.getName());
039:
040: public static final String METHOD_NAME = "ORDERPATCH";
041:
042: /**
043: * @param request
044: * @param response
045: * @throws WebDAVException
046: */
047: public OrderPatchMethod(HttpServletRequest request,
048: HttpServletResponse response) throws WebDAVException {
049: super (request, response);
050: methodName = METHOD_NAME;
051: }
052:
053: /* (non-Javadoc)
054: * @see com.ibm.webdav.protocol.http.WebDAVMethod#execute()
055: */
056: public WebDAVStatus execute() throws WebDAVException {
057:
058: try {
059: // get any arguments out of the headers
060: String depth = context.getRequestContext().depth();
061:
062: Document contents = null;
063:
064: if (context.getRequestContext().contentLength() > 0) {
065: // get the request entity body and parse it
066: WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(
067: resource.getURL().toString());
068:
069: DocumentBuilderFactory factory = DocumentBuilderFactory
070: .newInstance();
071: factory.setNamespaceAware(true);
072:
073: DocumentBuilder docbuilder = factory
074: .newDocumentBuilder();
075: docbuilder.setErrorHandler(errorHandler);
076: contents = docbuilder
077: .parse(new org.xml.sax.InputSource(request
078: .getReader()));
079:
080: if (errorHandler.getErrorCount() > 0) {
081: throw new WebDAVException(
082: WebDAVStatus.SC_BAD_REQUEST,
083: "Syntax error in PROPFIND request entity body");
084: }
085: }
086:
087: // get the arguments for the getProperties() method, and figure
088: // out which method variant to call.
089: if (ResourceImpl.debug) {
090: System.err.println("property request entity:");
091:
092: PrintWriter pout = new PrintWriter(System.err);
093: pout.print(XMLUtility.printNode(contents
094: .getDocumentElement()));
095:
096: //((Document) contents).printWithFormat(pout);
097: }
098:
099: if (resource.isCollection() == true) {
100: MultiStatus multiStatus = ((CollectionImpl) resource)
101: .setOrdering(context, contents);
102: Enumeration responses = multiStatus.getResponses();
103:
104: if (responses.hasMoreElements()) {
105: // there's more than one response, so return a multistatus
106: context.getResponseContext()
107: .contentType("text/xml");
108: setResponseHeaders();
109: setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
110:
111: // output the results as an XML document
112: Document results = multiStatus.asXML();
113: //((Document) results).setEncoding(getResponseCharset());
114: if (ResourceImpl.debug) {
115: System.err.println("property update results:");
116: PrintWriter pout = new PrintWriter(System.err);
117: pout.print(XMLUtility.printNode(results
118: .getDocumentElement()));
119: //((Document) results).printWithFormat(pout);
120: }
121: PrintWriter pout = new PrintWriter(response
122: .getWriter(), false);
123: //((Document) results).print(pout);
124: pout.print(multiStatus.toString());
125: pout.close();
126: } else {
127: setStatusCode(WebDAVStatus.SC_OK); // the default status code
128: setResponseHeaders();
129: }
130: } else {
131: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
132: "Invalid request on a non-collection resource");
133: }
134:
135: } catch (WebDAVException exc) {
136: m_logger.log(Level.INFO, exc.getLocalizedMessage());
137: setStatusCode(exc.getStatusCode());
138: } catch (Exception exc) {
139: m_logger.log(Level.WARNING, exc.getMessage(), exc);
140: setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
141: }
142:
143: return context.getStatusCode();
144: }
145:
146: }
|