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 CopyMethodImpl 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                            if (resource.isCollection()) {
063                                    boolean overwrite = WebDAVUtil.isOverwrite(request);
064                                    long depth = WebDAVUtil.getDepth(request);
065    
066                                    if (_log.isInfoEnabled()) {
067                                            sb.append(", overwrite is ");
068                                            sb.append(overwrite);
069                                            sb.append(", depth is ");
070                                            sb.append(depth);
071    
072                                            _log.info(sb.toString());
073                                    }
074    
075                                    return storage.copyCollectionResource(
076                                            webDAVRequest, resource, destination, overwrite, depth);
077                            }
078    
079                            boolean overwrite = WebDAVUtil.isOverwrite(request);
080    
081                            if (_log.isInfoEnabled()) {
082                                    sb.append(", overwrite is ");
083                                    sb.append(overwrite);
084    
085                                    _log.info(sb.toString());
086                            }
087    
088                            return storage.copySimpleResource(
089                                    webDAVRequest, resource, destination, overwrite);
090                    }
091    
092                    return HttpServletResponse.SC_FORBIDDEN;
093            }
094    
095            private static Log _log = LogFactoryUtil.getLog(CopyMethodImpl.class);
096    
097    }