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            @Override
036            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
037                    WebDAVStorage storage = webDavRequest.getWebDAVStorage();
038                    HttpServletRequest request = webDavRequest.getHttpServletRequest();
039    
040                    long companyId = webDavRequest.getCompanyId();
041                    String destination = WebDAVUtil.getDestination(
042                            request, storage.getRootPath());
043    
044                    StringBundler sb = new StringBundler();
045    
046                    if (_log.isInfoEnabled()) {
047                            sb.append("Destination is ");
048                            sb.append(destination);
049                    }
050    
051                    if (!destination.equals(webDavRequest.getPath()) &&
052                            (WebDAVUtil.getGroupId(companyId, destination) ==
053                                    webDavRequest.getGroupId())) {
054    
055                            Resource resource = storage.getResource(webDavRequest);
056    
057                            if (resource == null) {
058                                    return HttpServletResponse.SC_NOT_FOUND;
059                            }
060    
061                            if (resource.isCollection()) {
062                                    boolean overwrite = WebDAVUtil.isOverwrite(request);
063                                    long depth = WebDAVUtil.getDepth(request);
064    
065                                    if (_log.isInfoEnabled()) {
066                                            sb.append(", overwrite is ");
067                                            sb.append(overwrite);
068                                            sb.append(", depth is ");
069                                            sb.append(depth);
070    
071                                            _log.info(sb.toString());
072                                    }
073    
074                                    return storage.copyCollectionResource(
075                                            webDavRequest, resource, destination, overwrite, depth);
076                            }
077    
078                            boolean overwrite = WebDAVUtil.isOverwrite(request);
079    
080                            if (_log.isInfoEnabled()) {
081                                    sb.append(", overwrite is ");
082                                    sb.append(overwrite);
083    
084                                    _log.info(sb.toString());
085                            }
086    
087                            return storage.copySimpleResource(
088                                    webDavRequest, resource, destination, overwrite);
089                    }
090    
091                    return HttpServletResponse.SC_FORBIDDEN;
092            }
093    
094            private static Log _log = LogFactoryUtil.getLog(CopyMethodImpl.class);
095    
096    }