1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ContentTypes;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.Time;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.webdav.Status;
34  import com.liferay.portal.webdav.WebDAVException;
35  import com.liferay.portal.webdav.WebDAVRequest;
36  import com.liferay.portal.webdav.WebDAVStorage;
37  import com.liferay.portal.webdav.WebDAVUtil;
38  import com.liferay.util.servlet.ServletResponseUtil;
39  import com.liferay.util.xml.XMLFormatter;
40  
41  import java.io.StringReader;
42  
43  import java.util.List;
44  
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpServletResponse;
47  
48  import org.dom4j.Document;
49  import org.dom4j.Element;
50  import org.dom4j.io.SAXReader;
51  
52  /**
53   * <a href="LockMethodImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Alexander Chow
56   *
57   */
58  public class LockMethodImpl implements Method {
59  
60      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
61          try {
62              return doProcess(webDavRequest);
63          }
64          catch (Exception e) {
65              throw new WebDAVException(e);
66          }
67      }
68  
69      protected int doProcess(WebDAVRequest webDavRequest) throws Exception {
70          WebDAVStorage storage = webDavRequest.getWebDAVStorage();
71  
72          if (!storage.isSupportsClassTwo()) {
73              return HttpServletResponse.SC_METHOD_NOT_ALLOWED;
74          }
75  
76          HttpServletRequest request = webDavRequest.getHttpServletRequest();
77          HttpServletResponse response = webDavRequest.getHttpServletResponse();
78  
79          Lock lock = null;
80          Status status = null;
81  
82          String lockUuid = webDavRequest.getLockUuid();
83          long timeout = WebDAVUtil.getTimeout(request);
84  
85          if (Validator.isNull(lockUuid)) {
86  
87              // Create new lock
88  
89              String owner = null;
90              String xml = new String(
91                  FileUtil.getBytes(request.getInputStream()));
92  
93              if (Validator.isNotNull(xml)) {
94                  if (_log.isDebugEnabled()) {
95                      _log.debug(
96                          "Request XML\n" + XMLFormatter.toString(xml));
97                  }
98  
99                  SAXReader reader = new SAXReader();
100 
101                 Document doc = reader.read(new StringReader(xml));
102 
103                 Element root = doc.getRootElement();
104 
105                 boolean exclusive = false;
106 
107                 List<Element> lockscopeEls = root.element(
108                     "lockscope").elements();
109 
110                 for (Element scopeEl : lockscopeEls) {
111                     String name = GetterUtil.getString(scopeEl.getName());
112 
113                     if (name.equals("exclusive")) {
114                         exclusive = true;
115                     }
116                 }
117 
118                 if (!exclusive) {
119                     return HttpServletResponse.SC_BAD_REQUEST;
120                 }
121 
122                 Element ownerEl = root.element("owner");
123 
124                 owner = ownerEl.getTextTrim();
125 
126                 if (Validator.isNull(owner)) {
127                     List<Element> childEls = ownerEl.elements("href");
128 
129                     for (Element childEl : childEls) {
130                         owner =
131                             "<D:href>" + childEl.getTextTrim() + "</D:href>";
132                     }
133                 }
134             }
135             else {
136                 _log.error("Empty request XML");
137 
138                 return HttpServletResponse.SC_PRECONDITION_FAILED;
139             }
140 
141             status = storage.lockResource(webDavRequest, owner, timeout);
142 
143             lock = (Lock)status.getObject();
144         }
145         else {
146 
147             // Refresh existing lock
148 
149             lock = storage.refreshResourceLock(
150                 webDavRequest, lockUuid, timeout);
151 
152             status = new Status(HttpServletResponse.SC_OK);
153         }
154 
155         // Return lock details
156 
157         if (lock == null) {
158             return status.getCode();
159         }
160 
161         long depth = WebDAVUtil.getDepth(request);
162 
163         String xml = getResponseXML(lock, depth);
164 
165         if (_log.isDebugEnabled()) {
166             _log.debug("Response XML\n" + xml);
167         }
168 
169         String lockToken = "<" + WebDAVUtil.TOKEN_PREFIX + lock.getUuid() + ">";
170 
171         response.setContentType(ContentTypes.TEXT_XML_UTF8);
172         response.setHeader("Lock-Token", lockToken);
173         response.setStatus(status.getCode());
174 
175         if (_log.isInfoEnabled()) {
176             _log.info("Returning lock token " + lockToken);
177             _log.info("Status code " + status.getCode());
178         }
179 
180         try {
181             ServletResponseUtil.write(response, xml);
182         }
183         catch (Exception e) {
184             if (_log.isWarnEnabled()) {
185                 _log.warn(e);
186             }
187         }
188 
189         return -1;
190     }
191 
192     protected String getResponseXML(Lock lock, long depth) throws Exception {
193         StringBuilder sb = new StringBuilder();
194 
195         long timeoutSecs = lock.getExpirationTime() / Time.SECOND;
196 
197         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
198         sb.append("<D:prop xmlns:D=\"DAV:\">");
199         sb.append("<D:lockdiscovery>");
200         sb.append("<D:activelock>");
201         sb.append("<D:locktype><D:write/></D:locktype>");
202         sb.append("<D:lockscope><D:exclusive/></D:lockscope>");
203 
204         if (depth < 0) {
205             sb.append("<D:depth>Infinity</D:depth>");
206         }
207 
208         sb.append("<D:owner>" + lock.getOwner() + "</D:owner>");
209         sb.append("<D:timeout>Second-" + timeoutSecs + "</D:timeout>");
210         sb.append(
211             "<D:locktoken><D:href>" + WebDAVUtil.TOKEN_PREFIX + lock.getUuid() +
212             "</D:href></D:locktoken>");
213         sb.append("</D:activelock>");
214         sb.append("</D:lockdiscovery>");
215         sb.append("</D:prop>");
216 
217         return XMLFormatter.toString(sb.toString());
218     }
219 
220     private static Log _log = LogFactoryUtil.getLog(LockMethodImpl.class);
221 
222 }