KickJava   Java API By Example, From Geeks To Geeks.

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


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

14 package com.ibm.webdav.protocol.http;
15
16 import java.io.*;
17 import java.util.*;
18 import java.util.logging.*;
19
20 import javax.servlet.http.*;
21 import javax.xml.parsers.*;
22
23 import org.w3c.dom.*;
24
25 import com.ibm.webdav.*;
26 import com.ibm.webdav.impl.*;
27
28 /**
29  * Executes the WebDAV ordered collections ORDERPATCH method.
30  *
31  * @author Michael Bell
32  * @version $Revision: 1.1 $
33  * @since November 21, 2003
34  */

35 public class OrderPatchMethod extends WebDAVMethod {
36     
37     private static Logger m_logger = Logger.getLogger(OrderPatchMethod.class.getName());
38
39     
40     public static final String JavaDoc METHOD_NAME = "ORDERPATCH";
41
42     /**
43      * @param request
44      * @param response
45      * @throws WebDAVException
46      */

47     public OrderPatchMethod(
48         HttpServletRequest request,
49         HttpServletResponse response)
50         throws WebDAVException {
51         super(request, response);
52         methodName = METHOD_NAME;
53     }
54
55     /* (non-Javadoc)
56      * @see com.ibm.webdav.protocol.http.WebDAVMethod#execute()
57      */

58     public WebDAVStatus execute() throws WebDAVException {
59
60         try {
61             // get any arguments out of the headers
62
String JavaDoc depth = context.getRequestContext().depth();
63             
64             Document contents = null;
65             
66             if (context.getRequestContext().contentLength() > 0) {
67                 // get the request entity body and parse it
68
WebDAVErrorHandler errorHandler =
69                     new WebDAVErrorHandler(resource.getURL().toString());
70             
71                 DocumentBuilderFactory factory =
72                     DocumentBuilderFactory.newInstance();
73                 factory.setNamespaceAware(true);
74             
75                 DocumentBuilder docbuilder = factory.newDocumentBuilder();
76                 docbuilder.setErrorHandler(errorHandler);
77                 contents =
78                     docbuilder.parse(
79                         new org.xml.sax.InputSource JavaDoc(request.getReader()));
80             
81                 if (errorHandler.getErrorCount() > 0) {
82                     throw new WebDAVException(
83                         WebDAVStatus.SC_BAD_REQUEST,
84                         "Syntax error in PROPFIND request entity body");
85                 }
86             }
87             
88             // get the arguments for the getProperties() method, and figure
89
// out which method variant to call.
90
if (ResourceImpl.debug) {
91                 System.err.println("property request entity:");
92             
93                 PrintWriter pout = new PrintWriter(System.err);
94                 pout.print(XMLUtility.printNode(contents.getDocumentElement()));
95             
96                 //((Document) contents).printWithFormat(pout);
97
}
98             
99             if(resource.isCollection() == true) {
100                 MultiStatus multiStatus = ((CollectionImpl)resource).setOrdering(context,contents);
101                 Enumeration responses = multiStatus.getResponses();
102         
103                 if (responses.hasMoreElements()) {
104                     // there's more than one response, so return a multistatus
105
context.getResponseContext().contentType("text/xml");
106                     setResponseHeaders();
107                     setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
108
109                     // output the results as an XML document
110
Document results = multiStatus.asXML();
111                     //((Document) results).setEncoding(getResponseCharset());
112
if (ResourceImpl.debug) {
113                         System.err.println("property update results:");
114                         PrintWriter pout = new PrintWriter(System.err);
115                         pout.print(XMLUtility.printNode(results.getDocumentElement()));
116                                         //((Document) results).printWithFormat(pout);
117
}
118                     PrintWriter pout = new PrintWriter(response.getWriter(), false);
119                     //((Document) results).print(pout);
120
pout.print(multiStatus.toString());
121                                 pout.close();
122                 } else {
123                     setStatusCode(WebDAVStatus.SC_OK); // the default status code
124
setResponseHeaders();
125                 }
126             } else {
127                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,"Invalid request on a non-collection resource");
128             }
129             
130         
131         } catch (WebDAVException exc) {
132             m_logger.log(Level.INFO, exc.getLocalizedMessage());
133             setStatusCode(exc.getStatusCode());
134         } catch (Exception JavaDoc exc) {
135             m_logger.log(Level.WARNING, exc.getMessage(), exc);
136             setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
137         }
138                 
139         return context.getStatusCode();
140     }
141
142 }
143
Popular Tags