001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    import com.liferay.portal.kernel.webdav.methods.Method;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Alexander Chow
032     */
033    public class UnlockMethodImpl implements Method {
034    
035            @Override
036            public int process(WebDAVRequest webDAVRequest) throws WebDAVException {
037                    WebDAVStorage storage = webDAVRequest.getWebDAVStorage();
038    
039                    String token = getToken(webDAVRequest.getHttpServletRequest());
040    
041                    if (!storage.isSupportsClassTwo()) {
042                            return HttpServletResponse.SC_METHOD_NOT_ALLOWED;
043                    }
044    
045                    if (storage.unlockResource(webDAVRequest, token)) {
046                            return HttpServletResponse.SC_NO_CONTENT;
047                    }
048                    else {
049                            return HttpServletResponse.SC_PRECONDITION_FAILED;
050                    }
051            }
052    
053            protected String getToken(HttpServletRequest request) {
054                    String token = StringPool.BLANK;
055    
056                    String value = GetterUtil.getString(request.getHeader("Lock-Token"));
057    
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("\"Lock-Token\" header is " + value);
060                    }
061    
062                    if (value.startsWith("<") && value.endsWith(">")) {
063                            value = value.substring(1, value.length() - 1);
064                    }
065    
066                    int index = value.indexOf(WebDAVUtil.TOKEN_PREFIX);
067    
068                    if (index >= 0) {
069                            index += WebDAVUtil.TOKEN_PREFIX.length();
070    
071                            if (index < value.length()) {
072                                    token = GetterUtil.getString(value.substring(index));
073                            }
074                    }
075    
076                    return token;
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(UnlockMethodImpl.class);
080    
081    }