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    
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 CopyMethodImpl 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                    if (!destination.equals(webDAVRequest.getPath()) &&
051                            (WebDAVUtil.getGroupId(companyId, destination) ==
052                                    webDAVRequest.getGroupId())) {
053    
054                            Resource resource = storage.getResource(webDAVRequest);
055    
056                            if (resource == null) {
057                                    return HttpServletResponse.SC_NOT_FOUND;
058                            }
059    
060                            if (resource.isCollection()) {
061                                    boolean overwrite = WebDAVUtil.isOverwrite(request);
062                                    long depth = WebDAVUtil.getDepth(request);
063    
064                                    if (_log.isInfoEnabled()) {
065                                            sb.append(", overwrite is ");
066                                            sb.append(overwrite);
067                                            sb.append(", depth is ");
068                                            sb.append(depth);
069    
070                                            _log.info(sb.toString());
071                                    }
072    
073                                    return storage.copyCollectionResource(
074                                            webDAVRequest, resource, destination, overwrite, depth);
075                            }
076    
077                            boolean overwrite = WebDAVUtil.isOverwrite(request);
078    
079                            if (_log.isInfoEnabled()) {
080                                    sb.append(", overwrite is ");
081                                    sb.append(overwrite);
082    
083                                    _log.info(sb.toString());
084                            }
085    
086                            return storage.copySimpleResource(
087                                    webDAVRequest, resource, destination, overwrite);
088                    }
089    
090                    return HttpServletResponse.SC_FORBIDDEN;
091            }
092    
093            private static Log _log = LogFactoryUtil.getLog(CopyMethodImpl.class);
094    
095    }