001    /**
002     * Copyright (c) 2000-present 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.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 = null;
046    
047                    if (_log.isInfoEnabled()) {
048                            sb = new StringBundler(4);
049    
050                            sb.append("Destination is ");
051                            sb.append(destination);
052                    }
053    
054                    if (!destination.equals(webDAVRequest.getPath()) &&
055                            (WebDAVUtil.getGroupId(companyId, destination) ==
056                                    webDAVRequest.getGroupId())) {
057    
058                            Resource resource = storage.getResource(webDAVRequest);
059    
060                            if (resource == null) {
061                                    return HttpServletResponse.SC_NOT_FOUND;
062                            }
063    
064                            boolean overwrite = WebDAVUtil.isOverwrite(request);
065    
066                            if (_log.isInfoEnabled()) {
067                                    sb.append(", overwrite is ");
068                                    sb.append(overwrite);
069    
070                                    _log.info(sb.toString());
071                            }
072    
073                            if (resource.isCollection()) {
074                                    return storage.moveCollectionResource(
075                                            webDAVRequest, resource, destination, overwrite);
076                            }
077                            else {
078                                    return storage.moveSimpleResource(
079                                            webDAVRequest, resource, destination, overwrite);
080                            }
081                    }
082    
083                    return HttpServletResponse.SC_FORBIDDEN;
084            }
085    
086            private static final Log _log = LogFactoryUtil.getLog(MoveMethodImpl.class);
087    
088    }