Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 167   Methods: 2
NCLOC: 91   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Move.java 66.7% 66% 100% 67.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: Move.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.Map;
 24    import java.util.Iterator;
 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.core.FaultCodes;
 31    import org.apache.xindice.webadmin.util.DAVOperations;
 32    import org.apache.xindice.webadmin.webdav.WebdavStatus;
 33    import org.apache.xindice.webadmin.webdav.DAVRequest;
 34    import org.apache.xindice.webadmin.webdav.DAVResponse;
 35    import org.apache.xindice.webadmin.PartialResponse;
 36    import org.apache.xindice.webadmin.Location;
 37    import org.apache.commons.logging.Log;
 38    import org.apache.commons.logging.LogFactory;
 39   
 40    /**
 41    * This class implements the Move command for WebDAV operations on
 42    * Xindice.<br>
 43    * <br>
 44    * MOVE commans instructs that the collection or resource be moved to the
 45    * URI specified in the Destination header. In case of collection, all
 46    * resources and child collections are to be moved to locations relative
 47    * to it, recursively through all levels of the collection hierarchy.<br>
 48    * <br>
 49    * MOVE can overwrite existing resource/collection, and this behavior can
 50    * be changed by using Overwrite flag in request header.<br>
 51    * <br>
 52    * For collections, Depth header cannot have any value other than infinity.<br>
 53    * <br>
 54    * Operation possible status codes include: <ul>
 55    * <li>
 56    * 201 (Created) - The source resource was successfully moved, and a new
 57    * resource was created at the destination.
 58    *
 59    * 204 (No Content) - The source resource was successfully moved to a
 60    * pre-existing destination resource.
 61    *
 62    * 403 (Forbidden) _ The source and destination URIs are the same.
 63    *
 64    * 409 (Conflict) _ A resource cannot be created at the destination
 65    * until one or more intermediate collections have been created.
 66    *
 67    * 412 (Precondition Failed) - The server was unable to maintain the
 68    * liveness of the properties listed in the propertybehavior XML element
 69    * or the Overwrite header is "F" and the state of the destination
 70    * resource is non-null.
 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 Move implements DAVComponent {
 77    private static final Log log = LogFactory.getLog(Move.class);
 78   
 79  10 public void execute(DAVRequest req, DAVResponse res, Location target) throws ServletException, IOException {
 80  10 if (target.isRoot()) {
 81  0 res.setStatus(WebdavStatus.SC_FORBIDDEN);
 82  0 return;
 83    }
 84   
 85  10 Collection col = target.getCollection();
 86  10 String name = target.getName();
 87   
 88  10 if (col == null) {
 89  1 res.setStatus(WebdavStatus.SC_NOT_FOUND);
 90  1 return;
 91    }
 92   
 93  9 String dest = req.getDestinationPath();
 94  9 if (dest == null) {
 95  2 res.setStatus(WebdavStatus.SC_BAD_REQUEST);
 96  2 return;
 97    }
 98   
 99  7 if (dest.equals(req.getPath())) {
 100  2 res.setStatus(WebdavStatus.SC_FORBIDDEN);
 101  2 return;
 102    }
 103   
 104  5 int depth = req.getDepth();
 105  5 if (depth != DAVRequest.DEPTH_INFINITY) {
 106  0 res.setStatus(WebdavStatus.SC_BAD_REQUEST);
 107  0 return;
 108    }
 109   
 110  5 if (name == null) { // moving collection
 111  2 try {
 112  2 Map results = DAVOperations.copy(col, req.getDestinationPath(), req.getOverwrite(), true);
 113  2 interpretResults(results, res, dest, col);
 114    } catch (DBException e) {
 115  0 log.error("Failed to move collection " + col.getCanonicalName(), e);
 116  0 throw new ServletException(e);
 117    }
 118    } else { // moving resource
 119  3 try {
 120  3 int status = DAVOperations.copy(col, name, req.getDestinationPath(), req.getOverwrite());
 121  3 if (status == WebdavStatus.SC_NO_CONTENT || status == WebdavStatus.SC_CREATED) {
 122  1 col.remove(name);
 123    }
 124  3 res.setStatus(status);
 125    } catch (DBException e) {
 126  0 log.error("Failed to move resource " + name + " from collection " + col.getCanonicalName(), e);
 127  0 throw new ServletException(e);
 128    }
 129    }
 130    }
 131   
 132  2 private void interpretResults(Map results, DAVResponse res, String dest, Collection col) throws IOException {
 133  2 for (Iterator i = results.keySet().iterator(); i.hasNext(); ) {
 134  2 String url = (String) i.next();
 135  2 int status = ((Integer) results.get(url)).intValue();
 136  2 if (url.equals(dest)) {
 137  2 if (status == WebdavStatus.SC_CREATED || status == WebdavStatus.SC_NO_CONTENT) {
 138  1 try {
 139  1 col.getDatabase().dropCollection(col);
 140    } catch (DBException e) {
 141  0 if (e.faultCode == FaultCodes.COL_CANNOT_DROP) {
 142    // cannot drop database
 143  0 status = WebdavStatus.SC_FORBIDDEN;
 144  0 } else if (e.faultCode != FaultCodes.COL_COLLECTION_NOT_FOUND) {
 145  0 status = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
 146    }
 147    }
 148    }
 149   
 150  2 res.setStatus(status);
 151  2 return;
 152    }
 153   
 154    /*
 155    * If an error in executing the MOVE method occurs with a resource other
 156    * than the resource identified in the Request-URI then the response
 157    * MUST be a 207 (Multi-Status).
 158    */
 159  0 PartialResponse part = new PartialResponse(dest);
 160  0 String st = res.getProtocol() + " " + status + " " + WebdavStatus.getStatusText(status);
 161  0 part.addContent("status", st);
 162  0 res.sendMultiStatusResponse(part);
 163    }
 164   
 165  0 res.closeMultistatusResponse();
 166    }
 167    }