1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.webdav.methods;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.webdav.Resource;
21  import com.liferay.portal.kernel.webdav.WebDAVException;
22  import com.liferay.portal.kernel.webdav.WebDAVRequest;
23  import com.liferay.portal.kernel.webdav.WebDAVStorage;
24  import com.liferay.portal.kernel.webdav.WebDAVUtil;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  /**
30   * <a href="CopyMethodImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   * @author Alexander Chow
34   */
35  public class CopyMethodImpl implements Method {
36  
37      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
38          WebDAVStorage storage = webDavRequest.getWebDAVStorage();
39          HttpServletRequest request = webDavRequest.getHttpServletRequest();
40  
41          long companyId = webDavRequest.getCompanyId();
42          String destination = WebDAVUtil.getDestination(
43              request, storage.getRootPath());
44  
45          StringBundler sb = new StringBundler();
46  
47          if (_log.isInfoEnabled()) {
48              sb.append("Destination is ");
49              sb.append(destination);
50          }
51  
52          int status = HttpServletResponse.SC_FORBIDDEN;
53  
54          if ((!destination.equals(webDavRequest.getPath())) &&
55              (WebDAVUtil.getGroupId(companyId, destination) ==
56                  webDavRequest.getGroupId())) {
57  
58              Resource resource = storage.getResource(webDavRequest);
59  
60              if (resource == null) {
61                  status = HttpServletResponse.SC_NOT_FOUND;
62              }
63              else if (resource.isCollection()) {
64                  boolean overwrite = WebDAVUtil.isOverwrite(request);
65                  long depth = WebDAVUtil.getDepth(request);
66  
67                  if (_log.isInfoEnabled()) {
68                      sb.append(", overwrite is ");
69                      sb.append(overwrite);
70                      sb.append(", depth is ");
71                      sb.append(depth);
72  
73                      _log.info(sb.toString());
74                  }
75  
76                  status = storage.copyCollectionResource(
77                      webDavRequest, resource, destination, overwrite, depth);
78              }
79              else {
80                  boolean overwrite = WebDAVUtil.isOverwrite(request);
81  
82                  if (_log.isInfoEnabled()) {
83                      sb.append(", overwrite is ");
84                      sb.append(overwrite);
85  
86                      _log.info(sb.toString());
87                  }
88  
89                  status = storage.copySimpleResource(
90                      webDavRequest, resource, destination, overwrite);
91              }
92          }
93  
94          return status;
95      }
96  
97      private static Log _log = LogFactoryUtil.getLog(CopyMethodImpl.class);
98  
99  }