1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.webdav.methods;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.webdav.WebDAVException;
22  import com.liferay.portal.webdav.WebDAVRequest;
23  import com.liferay.portal.webdav.WebDAVStorage;
24  import com.liferay.portal.webdav.WebDAVUtil;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  /**
30   * <a href="UnlockMethodImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Alexander Chow
33   */
34  public class UnlockMethodImpl implements Method {
35  
36      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
37          WebDAVStorage storage = webDavRequest.getWebDAVStorage();
38  
39          String token = getToken(webDavRequest.getHttpServletRequest());
40  
41          if (!storage.isSupportsClassTwo()) {
42              return HttpServletResponse.SC_METHOD_NOT_ALLOWED;
43          }
44  
45          if (storage.unlockResource(webDavRequest, token)) {
46              return HttpServletResponse.SC_NO_CONTENT;
47          }
48          else {
49              return HttpServletResponse.SC_PRECONDITION_FAILED;
50          }
51      }
52  
53      protected String getToken(HttpServletRequest request) {
54          String token = StringPool.BLANK;
55  
56          String value = GetterUtil.getString(request.getHeader("Lock-Token"));
57  
58          if (_log.isDebugEnabled()) {
59              _log.debug("\"Lock-Token\" header is " + value);
60          }
61  
62          if (value.startsWith("<") && value.endsWith(">")) {
63              value = value.substring(1, value.length() - 1);
64          }
65  
66          int index = value.indexOf(WebDAVUtil.TOKEN_PREFIX);
67  
68          if (index >= 0) {
69              index += WebDAVUtil.TOKEN_PREFIX.length();
70  
71              if (index < value.length()) {
72                  token = GetterUtil.getString(value.substring(index));
73              }
74          }
75  
76          return token;
77      }
78  
79      private static Log _log = LogFactoryUtil.getLog(UnlockMethodImpl.class);
80  
81  }