KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > protocol > http > MoveMethod


1 package com.ibm.webdav.protocol.http;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import java.io.*;
18 import java.util.*;
19 import java.util.logging.*;
20
21 import javax.servlet.http.*;
22 import javax.xml.parsers.*;
23
24 import org.w3c.dom.*;
25
26 import com.ibm.webdav.*;
27 import com.ibm.webdav.Collection;
28 import com.ibm.webdav.impl.*;
29
30 /** Executes the WebDAV MOVE method.
31  * @author Jim Amsden <jamsden@us.ibm.com>
32  */

33 public class MoveMethod extends WebDAVMethod {
34     private static Logger m_logger = Logger.getLogger(MoveMethod.class.getName());
35
36 /** Construct a MoveMethod.
37 * @param request the servlet request
38 * @param response the servlet response
39 * @exception com.ibm.webdav.WebDAVException
40 */

41 public MoveMethod(HttpServletRequest request, HttpServletResponse response) throws WebDAVException {
42     super(request, response);
43     methodName = "MOVE";
44         context.setMethodName("MOVE");
45 }
46 /** Execute the method.
47 * @return the result status code
48 */

49 public WebDAVStatus execute() {
50     setStatusCode(WebDAVStatus.SC_CREATED); // the default status code
51
MultiStatus multiStatus = null;
52     try {
53         // get any arguments out of the headers
54
String JavaDoc destination = context.getRequestContext().destination();
55         String JavaDoc overwriteString = context.getRequestContext().overwrite();
56         boolean overwrite = overwriteString != null && overwriteString.equals("T");
57         String JavaDoc depth = context.getRequestContext().depth();
58         if (depth == null) {
59             depth = Collection.deep;
60         }
61         if (!depth.equals(Collection.deep)) {
62             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Depth header must be infinity if present");
63         }
64
65         // get the request entity body and parse it. This will contain the rules
66
// for moving properties
67
Vector propertiesToMove = null;
68         if (request.getContentLength() > 0) {
69             WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(resource.getURL().toString());
70                         /*Parser xmlParser = new Parser(resource.getURL().toString(), errorListener, null);
71             xmlParser.setWarningNoDoctypeDecl(false);
72             xmlParser.setProcessNamespace(true);
73             Document document = xmlParser.readStream(request.getReader());*/

74                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
75                         factory.setNamespaceAware(true);
76                         DocumentBuilder docbuilder = factory.newDocumentBuilder();
77                         docbuilder.setErrorHandler(errorHandler);
78                         Document document = docbuilder.parse(new org.xml.sax.InputSource JavaDoc(request.getReader()));
79             if (errorHandler.getErrorCount() > 0) {
80                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Syntax error in MOVE request entity body");
81             }
82             Element propertybehavior = (Element) document.getDocumentElement();
83             Element keepalive = (Element) propertybehavior.getElementsByTagNameNS("DAV:", "keepalive").item(0);
84             if (keepalive != null) {
85                 propertiesToMove = new Vector();
86                 NodeList hrefs = keepalive.getElementsByTagNameNS("DAV:", "href");
87                 Element href = null;
88                 for (int i = 0; i < hrefs.getLength(); i++) {
89                     href = (Element) hrefs.item(i);
90                     String JavaDoc propertyURI = ((Text) href.getFirstChild()).getData();
91                     propertiesToMove.addElement(propertyURI);
92                 }
93             }
94         } // endif (request has body)
95
context.setMethodName("MOVE");
96         multiStatus = resource.move(context, destination, overwrite, propertiesToMove);
97         Enumeration responses = multiStatus.getResponses();
98         if (responses.hasMoreElements()) {
99             MethodResponse methodResponse = (MethodResponse) responses.nextElement();
100             if (responses.hasMoreElements()) {
101                 // there's more than one response, so return a multistatus
102
context.getResponseContext().contentType("text/xml");
103                 setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
104                 setResponseHeaders();
105
106                 // output the results as an XML document
107
Document results = multiStatus.asXML();
108                 //((Document) results).setEncoding(getResponseCharset());
109
if (ResourceImpl.debug) {
110                     System.err.println("move results:");
111                     PrintWriter pout = new PrintWriter(System.err);
112                     pout.print(XMLUtility.printNode(results.getDocumentElement()));
113                                         //((Document) results).printWithFormat(pout);
114
}
115                 PrintWriter pout = new PrintWriter(response.getWriter(), false);
116                 pout.print(XMLUtility.printNode(results.getDocumentElement()));
117                                         //((Document) results).print(pout);
118
pout.close();
119             } else {
120                 // there was just one MethodResponse, so return it directly instead
121
// of wrapped in a multistatus
122
setStatusCode(methodResponse.getStatus());
123                 setResponseHeaders();
124             }
125         } else {
126             setResponseHeaders();
127         }
128     } catch (WebDAVException exc) {
129         m_logger.log(Level.INFO, exc.getLocalizedMessage() + " - " + request.getQueryString());
130         setStatusCode(exc.getStatusCode());
131                 
132     } catch (Exception JavaDoc exc) {
133         m_logger.log(Level.WARNING, exc.getMessage(), exc);
134         setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
135     }
136     return context.getStatusCode();
137 }
138 }
139
Popular Tags