KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

40 public LockMethod(HttpServletRequest request, HttpServletResponse response) throws WebDAVException {
41     super(request, response);
42     methodName = "LOCK";
43 }
44 /** Execute the method.
45 * @return the result status code
46 */

47 public WebDAVStatus execute() {
48
49     try {
50         setStatusCode(WebDAVStatus.SC_OK);
51
52         // get information from the headers
53
String JavaDoc depth = context.getRequestContext().depth();
54         if (depth == null) {
55             depth = Collection.deep;
56         }
57         if (!(depth.equals(Collection.shallow) || depth.equals(Collection.deep))) {
58             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Depth header must be 0 or infinity");
59         }
60
61         int timeout = context.getRequestContext().getTimeout();
62         context.setMethodName(methodName);
63         String JavaDoc lockToken = null;
64         Precondition precondition = context.getRequestContext().precondition();
65         if (precondition != null) {
66             lockToken = context.getRequestContext().precondition().toString();
67         }
68         if (lockToken != null && lockToken.length() > 2) {
69             // strip off the (<...>) delimiters
70
lockToken = lockToken.substring(2, lockToken.length() - 2);
71         }
72
73         // get the request entity body and lock or refresh the lock
74
// on the resource
75
Element lockinfo = null;
76         // don't attempt to read an empty request body if there's a
77
// lock token. It must be a refresh which doesn't have a request
78
// entity body.
79
if (lockToken == null) {
80             WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(resource.getURL().toString());
81                 /*Parser xmlParser = new Parser(resource.getURL().toString(), errorListener, null);
82             xmlParser.setWarningNoDoctypeDecl(false);
83             xmlParser.setProcessNamespace(true);
84             Document contents = xmlParser.readStream(request.getReader());*/

85                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
86                         factory.setNamespaceAware(true);
87                         DocumentBuilder docbuilder = factory.newDocumentBuilder();
88                         docbuilder.setErrorHandler(errorHandler);
89                         
90                         Document contents = docbuilder.parse(new org.xml.sax.InputSource JavaDoc(request.getReader()));
91             if (errorHandler.getErrorCount() > 0) {
92                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Syntax error in LOCK request entity body");
93             }
94             lockinfo = (Element) contents.getDocumentElement();
95         }
96
97                 
98
99         MultiStatus multiStatus = null;
100         if (lockinfo != null) { // doing a lock
101

102
103             Element lockscope = (Element)lockinfo.getElementsByTagNameNS("DAV:","lockscope").item(0);
104
105                         
106
107                         String JavaDoc type = null;
108             Element locktype = (Element)lockinfo.getElementsByTagNameNS("DAV:", "locktype").item(0);
109             String JavaDoc scope = null;
110             if (lockscope != null) {
111                 if (lockscope.getElementsByTagNameNS("DAV:", "exclusive").item(0) != null) {
112                     scope = ActiveLock.exclusive;
113                 } else
114                     if (lockscope.getElementsByTagNameNS("DAV:", "shared").item(0) != null) {
115                         scope = ActiveLock.shared;
116                     }
117             }
118
119             if (locktype != null) {
120                 if (locktype.getElementsByTagNameNS("DAV:", "write").item(0) != null) {
121                     type = ActiveLock.writeLock;
122                 }
123             }
124             Element owner = (Element)lockinfo.getElementsByTagNameNS("DAV:", "owner").item(0);
125             if (resource.exists() && resource.isCollection()) {
126                 multiStatus = ((CollectionImpl) resource).lock(context, scope, type, timeout, owner, depth);
127             } else {
128                 multiStatus = resource.lock(context, scope, type, timeout, owner);
129             }
130
131         } else {
132
133             if (lockToken != null) { // doing a refresh
134
multiStatus = resource.refreshLock(context, lockToken, timeout);
135             } else { // bad lock request
136
throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "LOCK missing Lock-Token header or bad LOCK request entity body");
137             }
138
139                 }
140         if (multiStatus.getResponses().hasMoreElements()) {
141             context.getResponseContext().contentType("text/xml");
142             setResponseHeaders();
143
144             // output the results as an XML document
145
// if the lock request is on a single resource (depth=shallow),
146
// output a document with a prop containing a lockdiscovery on the
147
// resource as the document element. Otherwise, use a multistatus.
148
//
149
Document results = null;
150             if (depth.equals(Collection.shallow)) {
151                 results = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
152                 //results.setVersion(Resource.XMLVersion);
153
Element prop = results.createElementNS("DAV:","D:prop");
154                 prop.setAttribute("xmlns:D", "DAV:");
155                 results.appendChild(prop);
156                 // the property must be a lockdiscovery property
157
PropertyResponse response = (PropertyResponse) multiStatus.getResponses().nextElement();
158                 PropertyValue lockDiscovery = response.getProperty( PropertyName.pnLockdiscovery );
159                 if (lockDiscovery != null) {
160                     prop.appendChild(results.importNode(lockDiscovery.value,true));
161                 }
162             } else {
163                 setStatusCode(WebDAVStatus.SC_MULTI_STATUS);
164                 results = (Document) multiStatus.asXML();
165             }
166             //((Document) results).setEncoding(getResponseCharset());
167
PrintWriter pout = new PrintWriter(response.getWriter(), false);
168             pout.print(XMLUtility.printNode(results.getDocumentElement()));
169                         //((Document) results).print(pout);
170
pout.close();
171         } else {
172             setResponseHeaders();
173         }
174     } catch (WebDAVException exc) {
175         m_logger.log(Level.INFO, exc.getLocalizedMessage() + " - " + request.getQueryString());
176         setStatusCode(exc.getStatusCode());
177     } catch (Exception JavaDoc exc) {
178         m_logger.log(Level.WARNING, exc.getMessage(), exc);
179         setStatusCode(WebDAVStatus.SC_INTERNAL_SERVER_ERROR);
180     }
181     return context.getStatusCode();
182 }
183 }
184
Popular Tags