KickJava   Java API By Example, From Geeks To Geeks.

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


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.impl.*;
28
29 /** Executes the WebDAV PROPPATCH method.
30  * @author Jim Amsden <jamsden@us.ibm.com>
31  */

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

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

49 public WebDAVStatus execute() {
50     setStatusCode(WebDAVStatus.SC_OK); // 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
56         // get the request entity body and parse it
57
WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(resource.getURL().toString());
58                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
59                 factory.setNamespaceAware(true);
60                 DocumentBuilder docbuilder = factory.newDocumentBuilder();
61                 docbuilder.setErrorHandler(errorHandler);
62                 /*Parser xmlParser = new Parser(resource.getURL().toString(), errorListener, null);
63         xmlParser.setWarningNoDoctypeDecl(false);
64         xmlParser.setProcessNamespace(true);
65         Document contents = xmlParser.readStream(request.getReader());*/

66                 Document contents = docbuilder.parse(new org.xml.sax.InputSource JavaDoc(request.getReader()));
67         if (errorHandler.getErrorCount() > 0) {
68                   if(true) throw new RuntimeException JavaDoc();
69             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Syntax error in PROPPATCH request entity body");
70         }
71         if (ResourceImpl.debug) {
72             System.err.println("property update request entity:");
73             PrintWriter pout = new PrintWriter(System.err);
74             pout.print(XMLUtility.printNode(contents.getDocumentElement()));
75                         //((Document) contents).printWithFormat(pout);
76
}
77         
78         context.setMethodName(methodName);
79         multiStatus = resource.setProperties(context, contents);
80         Enumeration responses = multiStatus.getResponses();
81         
82         if (responses.hasMoreElements()) {
83             // there's more than one response, so return a multistatus
84
context.getResponseContext().contentType("text/xml");
85             setResponseHeaders();
86             setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
87
88             // output the results as an XML document
89
Document results = multiStatus.asXML();
90             //((Document) results).setEncoding(getResponseCharset());
91
if (ResourceImpl.debug) {
92                 System.err.println("property update results:");
93                 PrintWriter pout = new PrintWriter(System.err);
94                 pout.print(XMLUtility.printNode(results.getDocumentElement()));
95                                 //((Document) results).printWithFormat(pout);
96
}
97             PrintWriter pout = new PrintWriter(response.getWriter(), false);
98             //((Document) results).print(pout);
99
pout.print(multiStatus.toString());
100                         pout.close();
101         } else {
102             setStatusCode(WebDAVStatus.SC_OK); // the default status code
103
setResponseHeaders();
104         }
105     } catch (WebDAVException exc) {
106         m_logger.log(Level.INFO, exc.getLocalizedMessage() + " - " + request.getQueryString());
107         setStatusCode(exc.getStatusCode());
108     } catch (Exception JavaDoc exc) {
109         m_logger.log(Level.WARNING, exc.getMessage(), exc);
110         setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
111     }
112     return context.getStatusCode();
113 }
114 }
115
Popular Tags