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.StringBundler;
020    import com.liferay.portal.kernel.webdav.Resource;
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 Brian Wing Shun Chan
032     * @author Alexander Chow
033     */
034    public class MoveMethodImpl implements Method {
035    
036            @Override
037            public int process(WebDAVRequest webDAVRequest) throws WebDAVException {
038                    WebDAVStorage storage = webDAVRequest.getWebDAVStorage();
039                    HttpServletRequest request = webDAVRequest.getHttpServletRequest();
040    
041                    long companyId = webDAVRequest.getCompanyId();
042                    String destination = WebDAVUtil.getDestination(
043                            request, storage.getRootPath());
044    
045                    StringBundler sb = new StringBundler();
046    
047                    if (_log.isInfoEnabled()) {
048                            sb.append("Destination is ");
049                            sb.append(destination);
050                    }
051    
052                    if (!destination.equals(webDAVRequest.getPath()) &&
053                            (WebDAVUtil.getGroupId(companyId, destination) ==
054                                    webDAVRequest.getGroupId())) {
055    
056                            Resource resource = storage.getResource(webDAVRequest);
057    
058                            if (resource == null) {
059                                    return HttpServletResponse.SC_NOT_FOUND;
060                            }
061    
062                            boolean overwrite = WebDAVUtil.isOverwrite(request);
063    
064                            if (_log.isInfoEnabled()) {
065                                    sb.append(", overwrite is ");
066                                    sb.append(overwrite);
067    
068                                    _log.info(sb.toString());
069                            }
070    
071                            if (resource.isCollection()) {
072                                    return storage.moveCollectionResource(
073                                            webDAVRequest, resource, destination, overwrite);
074                            }
075                            else {
076                                    return storage.moveSimpleResource(
077                                            webDAVRequest, resource, destination, overwrite);
078                            }
079                    }
080    
081                    return HttpServletResponse.SC_FORBIDDEN;
082            }
083    
084            private static Log _log = LogFactoryUtil.getLog(MoveMethodImpl.class);
085    
086    }