Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 149   Methods: 2
NCLOC: 72   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Copy.java 78.6% 71.1% 100% 74.1%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: Copy.java 541515 2007-05-25 02:45:06Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.webadmin.webdav.components;
 21   
 22    import java.io.IOException;
 23    import java.util.Iterator;
 24    import java.util.Map;
 25   
 26    import javax.servlet.ServletException;
 27   
 28    import org.apache.xindice.core.Collection;
 29    import org.apache.xindice.core.DBException;
 30    import org.apache.xindice.webadmin.util.DAVOperations;
 31    import org.apache.xindice.webadmin.webdav.DAVRequest;
 32    import org.apache.xindice.webadmin.webdav.DAVResponse;
 33    import org.apache.xindice.webadmin.webdav.WebdavStatus;
 34    import org.apache.xindice.webadmin.PartialResponse;
 35    import org.apache.xindice.webadmin.Location;
 36    import org.apache.commons.logging.Log;
 37    import org.apache.commons.logging.LogFactory;
 38   
 39    /**
 40    * This class implements the Copy command for WebDAV operations on
 41    * Xindice. <br>
 42    * <br>
 43    * COPY commans instructs that the collection or resource be moved to
 44    * the URI specified in the Destination header. It can overwrite
 45    * existing collection/resource, and this behavior can be changed by
 46    * using Overwrite flag in request header.<br>
 47    * <br>
 48    * COPY request for collection can have header Depth set to 0 or
 49    * infinity, where infinity means that the entire subtree of child
 50    * collections and resources will be copied to the new location,
 51    * and 0 means only collection itself will be copied.<br>
 52    * <br>
 53    * Operation possible status codes include: <ul>
 54    * <li>
 55    * 201 (Created) - The source resource was successfully copied. The
 56    * copy operation resulted in the creation of a new resource.
 57    * <li>
 58    * 204 (No Content) - The source resource was successfully copied to a
 59    * pre-existing destination resource.
 60    * <li>
 61    * 403 (Forbidden) - The source and destination URIs are the same.
 62    * <li>
 63    * 409 (Conflict) - A resource cannot be created at the destination
 64    * until one or more intermediate collections have been created.
 65    * <li>
 66    * 412 (Precondition Failed) - The server was unable to maintain the
 67    * liveness of the properties listed in the propertybehavior XML element
 68    * or the Overwrite header is "F" and the state of the destination
 69    * resource is non-null.
 70    * </ul>
 71    *
 72    * @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
 73    * @author <a href="mailto:gianugo@apache.org">Gianugo Rabellino</a>
 74    * @version $Revision: 541515 $, $Date: 2007-05-24 19:45:06 -0700 (Thu, 24 May 2007) $
 75    */
 76    public class Copy implements DAVComponent {
 77    private static final Log log = LogFactory.getLog(Copy.class);
 78   
 79  11 public void execute(DAVRequest req, DAVResponse res, Location target) throws ServletException, IOException {
 80  11 if (target.isRoot()) {
 81  0 res.setStatus(WebdavStatus.SC_FORBIDDEN);
 82  0 return;
 83    }
 84   
 85  11 Collection col = target.getCollection();
 86  11 String name = target.getName();
 87   
 88  11 if (col == null) {
 89  1 res.setStatus(WebdavStatus.SC_NOT_FOUND);
 90  1 return;
 91    }
 92   
 93  10 String dest = req.getDestinationPath();
 94  10 if (dest == null) {
 95  2 res.setStatus(WebdavStatus.SC_BAD_REQUEST);
 96  2 return;
 97    }
 98   
 99  8 if (dest.equals(req.getPath())) {
 100  2 res.setStatus(WebdavStatus.SC_FORBIDDEN);
 101  2 return;
 102    }
 103   
 104  6 if (name == null) { // copying collection
 105  3 try {
 106  3 boolean deep = req.getDepth() != 0;
 107  3 Map results = DAVOperations.copy(col, dest, req.getOverwrite(), deep);
 108  3 interpretResults(results, res, dest);
 109   
 110    } catch (DBException e) {
 111  0 log.error("Failed to copy collection " + col.getCanonicalName(), e);
 112  0 throw new ServletException(e);
 113    }
 114   
 115    } else { // copying resource
 116  3 try {
 117  3 int status = DAVOperations.copy(col, name, dest, req.getOverwrite());
 118  3 res.setStatus(status);
 119    } catch (DBException e) {
 120  0 log.error("Failed to copy resource " + name + " from collection " + col.getCanonicalName(), e);
 121  0 throw new ServletException(e);
 122    }
 123    }
 124   
 125    }
 126   
 127  3 private void interpretResults(Map results, DAVResponse res, String dest) throws IOException {
 128  3 for (Iterator i = results.keySet().iterator(); i.hasNext(); ) {
 129  3 String url = (String) i.next();
 130  3 int status = ((Integer) results.get(url)).intValue();
 131  3 if (url.equals(dest)) {
 132  3 res.setStatus(status);
 133  3 return;
 134    }
 135   
 136    /*
 137    * If an error in executing the COPY method occurs with a resource other
 138    * than the resource identified in the Request-URI then the response
 139    * MUST be a 207 (Multi-Status).
 140    */
 141  0 PartialResponse part = new PartialResponse(dest);
 142  0 String st = res.getProtocol() + " " + status + " " + WebdavStatus.getStatusText(status);
 143  0 part.addContent("status", st);
 144  0 res.sendMultiStatusResponse(part);
 145    }
 146   
 147  0 res.closeMultistatusResponse();
 148    }
 149    }