1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.webdav.methods;
24  
25  import com.liferay.lock.model.Lock;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.Time;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.webdav.Status;
32  import com.liferay.portal.webdav.WebDAVException;
33  import com.liferay.portal.webdav.WebDAVRequest;
34  import com.liferay.portal.webdav.WebDAVStorage;
35  import com.liferay.portal.webdav.WebDAVUtil;
36  import com.liferay.util.servlet.ServletResponseUtil;
37  import com.liferay.util.xml.XMLFormatter;
38  
39  import java.io.StringReader;
40  
41  import java.util.List;
42  
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import org.dom4j.Document;
50  import org.dom4j.Element;
51  import org.dom4j.io.SAXReader;
52  
53  /**
54   * <a href="LockMethodImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Alexander Chow
57   *
58   */
59  public class LockMethodImpl implements Method {
60  
61      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
62          try {
63              return doProcess(webDavRequest);
64          }
65          catch (Exception e) {
66              throw new WebDAVException(e);
67          }
68      }
69  
70      protected int doProcess(WebDAVRequest webDavRequest) throws Exception {
71          WebDAVStorage storage = webDavRequest.getWebDAVStorage();
72  
73          if (!storage.isSupportsClassTwo()) {
74              return HttpServletResponse.SC_METHOD_NOT_ALLOWED;
75          }
76  
77          HttpServletRequest request = webDavRequest.getHttpServletRequest();
78          HttpServletResponse response = webDavRequest.getHttpServletResponse();
79  
80          Lock lock = null;
81          Status status = null;
82  
83          String lockUuid = webDavRequest.getLockUuid();
84          long timeout = WebDAVUtil.getTimeout(request);
85  
86          if (Validator.isNull(lockUuid)) {
87  
88              // Create new lock
89  
90              String owner = null;
91              String xml = new String(
92                  FileUtil.getBytes(request.getInputStream()));
93  
94              if (Validator.isNotNull(xml)) {
95                  if (_log.isDebugEnabled()) {
96                      _log.debug(
97                          "Request XML\n" + XMLFormatter.toString(xml));
98                  }
99  
100                 SAXReader reader = new SAXReader();
101 
102                 Document doc = reader.read(new StringReader(xml));
103 
104                 Element root = doc.getRootElement();
105 
106                 boolean exclusive = false;
107 
108                 List<Element> lockscopeEls = root.element(
109                     "lockscope").elements();
110 
111                 for (Element scopeEl : lockscopeEls) {
112                     String name = GetterUtil.getString(scopeEl.getName());
113 
114                     if (name.equals("exclusive")) {
115                         exclusive = true;
116                     }
117                 }
118 
119                 if (!exclusive) {
120                     return HttpServletResponse.SC_BAD_REQUEST;
121                 }
122 
123                 Element ownerEl = root.element("owner");
124 
125                 owner = ownerEl.getTextTrim();
126 
127                 if (Validator.isNull(owner)) {
128                     List<Element> childEls = ownerEl.elements("href");
129 
130                     for (Element childEl : childEls) {
131                         owner =
132                             "<D:href>" + childEl.getTextTrim() + "</D:href>";
133                     }
134                 }
135             }
136             else {
137                 _log.error("Empty request XML");
138 
139                 return HttpServletResponse.SC_PRECONDITION_FAILED;
140             }
141 
142             status = storage.lockResource(webDavRequest, owner, timeout);
143 
144             lock = (Lock)status.getObject();
145         }
146         else {
147 
148             // Refresh existing lock
149 
150             lock = storage.refreshResourceLock(
151                 webDavRequest, lockUuid, timeout);
152 
153             status = new Status(HttpServletResponse.SC_OK);
154         }
155 
156         // Return lock details
157 
158         if (lock == null) {
159             return status.getCode();
160         }
161 
162         long depth = WebDAVUtil.getDepth(request);
163 
164         String xml = getResponseXML(lock, depth);
165 
166         if (_log.isDebugEnabled()) {
167             _log.debug("Response XML\n" + xml);
168         }
169 
170         String lockToken = "<" + WebDAVUtil.TOKEN_PREFIX + lock.getUuid() + ">";
171 
172         response.setContentType(ContentTypes.TEXT_XML_UTF8);
173         response.setHeader("Lock-Token", lockToken);
174         response.setStatus(status.getCode());
175 
176         if (_log.isInfoEnabled()) {
177             _log.info("Returning lock token " + lockToken);
178             _log.info("Status code " + status.getCode());
179         }
180 
181         try {
182             ServletResponseUtil.write(response, xml);
183         }
184         catch (Exception e) {
185             if (_log.isWarnEnabled()) {
186                 _log.warn(e);
187             }
188         }
189 
190         return -1;
191     }
192 
193     protected String getResponseXML(Lock lock, long depth) throws Exception {
194         StringBuilder sb = new StringBuilder();
195 
196         long timeoutSecs = lock.getExpirationTime() / Time.SECOND;
197 
198         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
199         sb.append("<D:prop xmlns:D=\"DAV:\">");
200         sb.append("<D:lockdiscovery>");
201         sb.append("<D:activelock>");
202         sb.append("<D:locktype><D:write/></D:locktype>");
203         sb.append("<D:lockscope><D:exclusive/></D:lockscope>");
204 
205         if (depth < 0) {
206             sb.append("<D:depth>Infinity</D:depth>");
207         }
208 
209         sb.append("<D:owner>" + lock.getOwner() + "</D:owner>");
210         sb.append("<D:timeout>Second-" + timeoutSecs + "</D:timeout>");
211         sb.append(
212             "<D:locktoken><D:href>" + WebDAVUtil.TOKEN_PREFIX + lock.getUuid() +
213             "</D:href></D:locktoken>");
214         sb.append("</D:activelock>");
215         sb.append("</D:lockdiscovery>");
216         sb.append("</D:prop>");
217 
218         return XMLFormatter.toString(sb.toString());
219     }
220 
221     private static Log _log = LogFactory.getLog(LockMethodImpl.class);
222 
223 }