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.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    }