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.Collection;
028: import com.ibm.webdav.impl.*;
029:
030: /** Executes the WebDAV MOVE method.
031: * @author Jim Amsden <jamsden@us.ibm.com>
032: */
033: public class MoveMethod extends WebDAVMethod {
034: private static Logger m_logger = Logger.getLogger(MoveMethod.class
035: .getName());
036:
037: /** Construct a MoveMethod.
038: * @param request the servlet request
039: * @param response the servlet response
040: * @exception com.ibm.webdav.WebDAVException
041: */
042: public MoveMethod(HttpServletRequest request,
043: HttpServletResponse response) throws WebDAVException {
044: super (request, response);
045: methodName = "MOVE";
046: context.setMethodName("MOVE");
047: }
048:
049: /** Execute the method.
050: * @return the result status code
051: */
052: public WebDAVStatus execute() {
053: setStatusCode(WebDAVStatus.SC_CREATED); // the default status code
054: MultiStatus multiStatus = null;
055: try {
056: // get any arguments out of the headers
057: String destination = context.getRequestContext()
058: .destination();
059: String overwriteString = context.getRequestContext()
060: .overwrite();
061: boolean overwrite = overwriteString != null
062: && overwriteString.equals("T");
063: String depth = context.getRequestContext().depth();
064: if (depth == null) {
065: depth = Collection.deep;
066: }
067: if (!depth.equals(Collection.deep)) {
068: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
069: "Depth header must be infinity if present");
070: }
071:
072: // get the request entity body and parse it. This will contain the rules
073: // for moving properties
074: Vector propertiesToMove = null;
075: if (request.getContentLength() > 0) {
076: WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(
077: resource.getURL().toString());
078: /*Parser xmlParser = new Parser(resource.getURL().toString(), errorListener, null);
079: xmlParser.setWarningNoDoctypeDecl(false);
080: xmlParser.setProcessNamespace(true);
081: Document document = xmlParser.readStream(request.getReader());*/
082: DocumentBuilderFactory factory = DocumentBuilderFactory
083: .newInstance();
084: factory.setNamespaceAware(true);
085: DocumentBuilder docbuilder = factory
086: .newDocumentBuilder();
087: docbuilder.setErrorHandler(errorHandler);
088: Document document = docbuilder
089: .parse(new org.xml.sax.InputSource(request
090: .getReader()));
091: if (errorHandler.getErrorCount() > 0) {
092: throw new WebDAVException(
093: WebDAVStatus.SC_BAD_REQUEST,
094: "Syntax error in MOVE request entity body");
095: }
096: Element propertybehavior = (Element) document
097: .getDocumentElement();
098: Element keepalive = (Element) propertybehavior
099: .getElementsByTagNameNS("DAV:", "keepalive")
100: .item(0);
101: if (keepalive != null) {
102: propertiesToMove = new Vector();
103: NodeList hrefs = keepalive.getElementsByTagNameNS(
104: "DAV:", "href");
105: Element href = null;
106: for (int i = 0; i < hrefs.getLength(); i++) {
107: href = (Element) hrefs.item(i);
108: String propertyURI = ((Text) href
109: .getFirstChild()).getData();
110: propertiesToMove.addElement(propertyURI);
111: }
112: }
113: } // endif (request has body)
114: context.setMethodName("MOVE");
115: multiStatus = resource.move(context, destination,
116: overwrite, propertiesToMove);
117: Enumeration responses = multiStatus.getResponses();
118: if (responses.hasMoreElements()) {
119: MethodResponse methodResponse = (MethodResponse) responses
120: .nextElement();
121: if (responses.hasMoreElements()) {
122: // there's more than one response, so return a multistatus
123: context.getResponseContext()
124: .contentType("text/xml");
125: setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
126: setResponseHeaders();
127:
128: // output the results as an XML document
129: Document results = multiStatus.asXML();
130: //((Document) results).setEncoding(getResponseCharset());
131: if (ResourceImpl.debug) {
132: System.err.println("move results:");
133: PrintWriter pout = new PrintWriter(System.err);
134: pout.print(XMLUtility.printNode(results
135: .getDocumentElement()));
136: //((Document) results).printWithFormat(pout);
137: }
138: PrintWriter pout = new PrintWriter(response
139: .getWriter(), false);
140: pout.print(XMLUtility.printNode(results
141: .getDocumentElement()));
142: //((Document) results).print(pout);
143: pout.close();
144: } else {
145: // there was just one MethodResponse, so return it directly instead
146: // of wrapped in a multistatus
147: setStatusCode(methodResponse.getStatus());
148: setResponseHeaders();
149: }
150: } else {
151: setResponseHeaders();
152: }
153: } catch (WebDAVException exc) {
154: m_logger.log(Level.INFO, exc.getLocalizedMessage() + " - "
155: + request.getQueryString());
156: setStatusCode(exc.getStatusCode());
157:
158: } catch (Exception exc) {
159: m_logger.log(Level.WARNING, exc.getMessage(), exc);
160: setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
161: }
162: return context.getStatusCode();
163: }
164: }
|