001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.webdav.methods;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.webdav.WebDAVException;
022    import com.liferay.portal.kernel.webdav.WebDAVRequest;
023    import com.liferay.portal.kernel.webdav.WebDAVStorage;
024    import com.liferay.portal.kernel.webdav.WebDAVUtil;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * @author Alexander Chow
031     */
032    public class UnlockMethodImpl implements Method {
033    
034            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
035                    WebDAVStorage storage = webDavRequest.getWebDAVStorage();
036    
037                    String token = getToken(webDavRequest.getHttpServletRequest());
038    
039                    if (!storage.isSupportsClassTwo()) {
040                            return HttpServletResponse.SC_METHOD_NOT_ALLOWED;
041                    }
042    
043                    if (storage.unlockResource(webDavRequest, token)) {
044                            return HttpServletResponse.SC_NO_CONTENT;
045                    }
046                    else {
047                            return HttpServletResponse.SC_PRECONDITION_FAILED;
048                    }
049            }
050    
051            protected String getToken(HttpServletRequest request) {
052                    String token = StringPool.BLANK;
053    
054                    String value = GetterUtil.getString(request.getHeader("Lock-Token"));
055    
056                    if (_log.isDebugEnabled()) {
057                            _log.debug("\"Lock-Token\" header is " + value);
058                    }
059    
060                    if (value.startsWith("<") && value.endsWith(">")) {
061                            value = value.substring(1, value.length() - 1);
062                    }
063    
064                    int index = value.indexOf(WebDAVUtil.TOKEN_PREFIX);
065    
066                    if (index >= 0) {
067                            index += WebDAVUtil.TOKEN_PREFIX.length();
068    
069                            if (index < value.length()) {
070                                    token = GetterUtil.getString(value.substring(index));
071                            }
072                    }
073    
074                    return token;
075            }
076    
077            private static Log _log = LogFactoryUtil.getLog(UnlockMethodImpl.class);
078    
079    }