001    /**
002     * Copyright (c) 2000-2010 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    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Alexander Chow
032     */
033    public class MoveMethodImpl implements Method {
034    
035            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
036                    WebDAVStorage storage = webDavRequest.getWebDAVStorage();
037                    HttpServletRequest request = webDavRequest.getHttpServletRequest();
038    
039                    long companyId = webDavRequest.getCompanyId();
040                    String destination = WebDAVUtil.getDestination(
041                            request, storage.getRootPath());
042    
043                    StringBundler sb = new StringBundler();
044    
045                    if (_log.isInfoEnabled()) {
046                            sb.append("Destination is ");
047                            sb.append(destination);
048                    }
049    
050                    int status = HttpServletResponse.SC_FORBIDDEN;
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                                    status = HttpServletResponse.SC_NOT_FOUND;
060                            }
061                            else {
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                                            status = storage.moveCollectionResource(
073                                                    webDavRequest, resource, destination, overwrite);
074                                    }
075                                    else {
076                                            status = storage.moveSimpleResource(
077                                                    webDavRequest, resource, destination, overwrite);
078                                    }
079                            }
080                    }
081    
082                    return status;
083            }
084    
085            private static Log _log = LogFactoryUtil.getLog(MoveMethodImpl.class);
086    
087    }