001    /**
002     * Copyright (c) 2000-2013 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    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    }