1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.webdav.methods;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.webdav.WebDAVException;
27  import com.liferay.portal.webdav.WebDAVRequest;
28  import com.liferay.portal.webdav.WebDAVStorage;
29  import com.liferay.portal.webdav.WebDAVUtil;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  /**
35   * <a href="UnlockMethodImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Alexander Chow
38   *
39   */
40  public class UnlockMethodImpl implements Method {
41  
42      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
43          WebDAVStorage storage = webDavRequest.getWebDAVStorage();
44  
45          String token = getToken(webDavRequest.getHttpServletRequest());
46  
47          if (!storage.isSupportsClassTwo()) {
48              return HttpServletResponse.SC_METHOD_NOT_ALLOWED;
49          }
50  
51          if (storage.unlockResource(webDavRequest, token)) {
52              return HttpServletResponse.SC_NO_CONTENT;
53          }
54          else {
55              return HttpServletResponse.SC_PRECONDITION_FAILED;
56          }
57      }
58  
59      protected String getToken(HttpServletRequest request) {
60          String token = StringPool.BLANK;
61  
62          String value = GetterUtil.getString(request.getHeader("Lock-Token"));
63  
64          if (_log.isInfoEnabled()) {
65              _log.info("\"Lock-Token\" header is " + value);
66          }
67  
68          if (value.startsWith("<") && value.endsWith(">")) {
69              value = value.substring(1, value.length() - 1);
70          }
71  
72          int index = value.indexOf(WebDAVUtil.TOKEN_PREFIX);
73  
74          if (index >= 0) {
75              index += WebDAVUtil.TOKEN_PREFIX.length();
76  
77              if (index < value.length()) {
78                  token = GetterUtil.getString(value.substring(index));
79              }
80          }
81  
82          return token;
83      }
84  
85      private static Log _log = LogFactoryUtil.getLog(UnlockMethodImpl.class);
86  
87  }