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.webdav.Resource;
21  import com.liferay.portal.webdav.WebDAVException;
22  import com.liferay.portal.webdav.WebDAVRequest;
23  import com.liferay.portal.webdav.WebDAVStorage;
24  import com.liferay.portal.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          String destination = WebDAVUtil.getDestination(
42              request, storage.getRootPath());
43  
44          StringBundler sb = new StringBundler();
45  
46          if (_log.isInfoEnabled()) {
47              sb.append("Destination is ");
48              sb.append(destination);
49          }
50  
51          int status = HttpServletResponse.SC_FORBIDDEN;
52  
53          if ((!destination.equals(webDavRequest.getPath())) &&
54              (WebDAVUtil.getGroupId(destination) ==
55                  webDavRequest.getGroupId())) {
56  
57              Resource resource = storage.getResource(webDavRequest);
58  
59              if (resource == null) {
60                  status = HttpServletResponse.SC_NOT_FOUND;
61              }
62              else if (resource.isCollection()) {
63                  boolean overwrite = WebDAVUtil.isOverwrite(request);
64                  long depth = WebDAVUtil.getDepth(request);
65  
66                  if (_log.isInfoEnabled()) {
67                      sb.append(", overwrite is ");
68                      sb.append(overwrite);
69                      sb.append(", depth is ");
70                      sb.append(depth);
71  
72                      _log.info(sb.toString());
73                  }
74  
75                  status = storage.copyCollectionResource(
76                      webDavRequest, resource, destination, overwrite, depth);
77              }
78              else {
79                  boolean overwrite = WebDAVUtil.isOverwrite(request);
80  
81                  if (_log.isInfoEnabled()) {
82                      sb.append(", overwrite is ");
83                      sb.append(overwrite);
84  
85                      _log.info(sb.toString());
86                  }
87  
88                  status = storage.copySimpleResource(
89                      webDavRequest, resource, destination, overwrite);
90              }
91          }
92  
93          return status;
94      }
95  
96      private static Log _log = LogFactoryUtil.getLog(CopyMethodImpl.class);
97  
98  }