KickJava   Java API By Example, From Geeks To Geeks.

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


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 COPY method.
31  * @author Jim Amsden <jamsden@us.ibm.com>
32  */

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

42 public CopyMethod(HttpServletRequest request, HttpServletResponse response) throws WebDAVException {
43     super(request, response);
44     methodName = "COPY";
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 depth = context.getRequestContext().depth();
55         if (depth == null) {
56             depth = Collection.deep;
57         }
58         if (!(depth.equals(Collection.shallow) || depth.equals(Collection.deep))) {
59             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Depth header must be 0 or infinity if present");
60         }
61         String JavaDoc destination = context.getRequestContext().destination();
62         String JavaDoc overwriteString = context.getRequestContext().overwrite();
63         boolean overwrite = overwriteString != null && overwriteString.equals("T");
64         Vector propertiesToCopy = null;
65         if (request.getContentLength() > 0) {
66             // get the request entity body and parse it. This will contain the rules
67
// for copying properties
68
WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(resource.getURL().toString());
69                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
70                         factory.setNamespaceAware(true);
71                         DocumentBuilder docbuilder = factory.newDocumentBuilder();
72                         docbuilder.setErrorHandler(errorHandler);
73                         /*Parser xmlParser = new Parser(resource.getURL().toString(), errorListener, null);
74
75                         xmlParser.setProcessNamespace(true);
76             xmlParser.setWarningNoDoctypeDecl(false);
77
78                         Document document = xmlParser.readStream(request.getReader());*/

79                         Document document = docbuilder.parse(new org.xml.sax.InputSource JavaDoc(request.getReader()));
80             if (errorHandler.getErrorCount() > 0) {
81                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Syntax error in COPY request entity body");
82             }
83             Element propertybehavior = (Element) document.getDocumentElement();
84             Element keepalive = (Element) propertybehavior.getElementsByTagNameNS("DAV:","keepalive").item(0);
85             if (keepalive != null) {
86                 propertiesToCopy = new Vector();
87                 NodeList hrefs = keepalive.getElementsByTagNameNS("DAV:","href");
88                 Element href = null;
89                 for (int i = 0; i < hrefs.getLength(); i++) {
90                     href = (Element) hrefs.item(i);
91                     String JavaDoc propertyURI = ((Text) href.getFirstChild()).getData();
92                     propertiesToCopy.addElement(propertyURI);
93                 }
94             }
95         }
96
97         if (ResourceImpl.debug) {
98             System.err.println("Copying " + resource.getURL() + " to " + destination);
99             System.err.print("Copying properties: ");
100             if (propertiesToCopy == null) {
101                 System.err.println("all");
102             } else {
103                 System.err.println();
104                 Enumeration propertyNames = propertiesToCopy.elements();
105                 while (propertyNames.hasMoreElements()) {
106                     System.err.println(" " + propertyNames.nextElement());
107                 }
108             }
109         }
110         if (resource.isCollection()) {
111             multiStatus = ((CollectionImpl) resource).copy(context, destination, overwrite, propertiesToCopy, depth);
112         } else {
113             multiStatus = resource.copy(context, destination, overwrite, propertiesToCopy);
114         }
115
116         // don't send back an empty multistatus response entity,
117
// just return the status code
118
Enumeration responses = multiStatus.getResponses();
119         if (responses.hasMoreElements()) {
120             MethodResponse methodResponse = (MethodResponse) responses.nextElement();
121             if (responses.hasMoreElements()) {
122                 // there's more than one response, so return a multistatus
123
context.getResponseContext().contentType("text/xml");
124                 setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
125                 setResponseHeaders();
126
127                 // output the results as an XML document
128
Document results = multiStatus.asXML();
129                 //((Document) results).setEncoding(getResponseCharset());
130
if (ResourceImpl.debug) {
131                     System.err.println("copy results:");
132                     PrintWriter pout = new PrintWriter(System.err);
133                     pout.print(XMLUtility.printNode(results.getDocumentElement()));
134                                         //((Document) results).printWithFormat(pout);
135
}
136                 PrintWriter pout = new PrintWriter(response.getWriter(), false);
137                 pout.print(XMLUtility.printNode(results.getDocumentElement()));
138                                 //((Document) results).print(pout);
139
pout.close();
140             } else {
141                 // there was just one MethodResponse, so return it directly instead
142
// of wrapped in a multistatus
143
setStatusCode(methodResponse.getStatus());
144                 setResponseHeaders();
145             }
146         } else {
147             setStatusCode(WebDAVStatus.SC_CREATED); // the default status code
148
setResponseHeaders();
149         }
150     } catch (WebDAVException exc) {
151         m_logger.log(Level.INFO, exc.getLocalizedMessage() + " - " + request.getQueryString());
152         setStatusCode(exc.getStatusCode());
153     } catch (Exception JavaDoc exc) {
154         m_logger.log(Level.WARNING, exc.getMessage(), exc);
155         
156         setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
157     }
158     return context.getStatusCode();
159 }
160 }
161
Popular Tags