1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.webdav.methods;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.webdav.Resource;
25  import com.liferay.portal.webdav.WebDAVException;
26  import com.liferay.portal.webdav.WebDAVRequest;
27  import com.liferay.portal.webdav.WebDAVStorage;
28  import com.liferay.portal.webdav.WebDAVUtil;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  /**
34   * <a href="CopyMethodImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Alexander Chow
38   *
39   */
40  public class CopyMethodImpl implements Method {
41  
42      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
43          WebDAVStorage storage = webDavRequest.getWebDAVStorage();
44          HttpServletRequest request = webDavRequest.getHttpServletRequest();
45  
46          String destination = WebDAVUtil.getDestination(
47              request, storage.getRootPath());
48  
49          StringBuilder sb = new StringBuilder();
50  
51          if (_log.isInfoEnabled()) {
52              sb.append("Destination is " + destination);
53          }
54  
55          int status = HttpServletResponse.SC_FORBIDDEN;
56  
57          if ((!destination.equals(webDavRequest.getPath())) &&
58              (WebDAVUtil.getGroupId(destination) ==
59                  webDavRequest.getGroupId())) {
60  
61              Resource resource = storage.getResource(webDavRequest);
62  
63              if (resource == null) {
64                  status = HttpServletResponse.SC_NOT_FOUND;
65              }
66              else if (resource.isCollection()) {
67                  boolean overwrite = WebDAVUtil.isOverwrite(request);
68                  long depth = WebDAVUtil.getDepth(request);
69  
70                  if (_log.isInfoEnabled()) {
71                      sb.append(", overwrite is " + overwrite);
72                      sb.append(", depth is " + depth);
73  
74                      _log.info(sb.toString());
75                  }
76  
77                  status = storage.copyCollectionResource(
78                      webDavRequest, resource, destination, overwrite, depth);
79              }
80              else {
81                  boolean overwrite = WebDAVUtil.isOverwrite(request);
82  
83                  if (_log.isInfoEnabled()) {
84                      sb.append(", overwrite is " + 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  }