Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 131   Methods: 2
NCLOC: 63   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Mkcol.java 50% 58.1% 100% 56.9%
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: Mkcol.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   
 24    import javax.servlet.ServletException;
 25   
 26    import org.apache.xindice.core.Collection;
 27    import org.apache.xindice.core.DBException;
 28    import org.apache.xindice.core.FaultCodes;
 29    import org.apache.xindice.webadmin.util.CollectionConfigurationHelper;
 30    import org.apache.xindice.webadmin.webdav.WebdavStatus;
 31    import org.apache.xindice.webadmin.webdav.DAVResponse;
 32    import org.apache.xindice.webadmin.webdav.DAVRequest;
 33    import org.apache.xindice.webadmin.Location;
 34    import org.apache.commons.logging.Log;
 35    import org.apache.commons.logging.LogFactory;
 36   
 37    /**
 38    * This class implements the Mkcol command for WebDAV operations on
 39    * Xindice.<br>
 40    * <br>
 41    * MKCOL command creates new collection at a specified location. There must be
 42    * no existing collection at the specified location. Newly created collection
 43    * will be empty. WebDAV specification allows request to have a body that may
 44    * contain instructions for creating child resources, but this behavior is
 45    * not suppoted. <br>
 46    * <br>
 47    * Operation possible status codes include: <ul>
 48    * <li>
 49    * 201 (Created) - The collection or structured resource was created in
 50    * its entirety.
 51    * <li>
 52    * 403 (Forbidden) - This indicates at least one of two conditions: 1)
 53    * the server does not allow the creation of collections at the given
 54    * location in its namespace, or 2) the parent collection of the
 55    * Request-URI exists but cannot accept members.
 56    * <li>
 57    * 405 (Method Not Allowed) - MKCOL can only be executed on a
 58    * deleted/non-existent resource.
 59    * <li>
 60    * 409 (Conflict) - A collection cannot be made at the Request-URI until
 61    * one or more intermediate collections have been created.
 62    * <li>
 63    * 415 (Unsupported Media Type)- The server does not support the request
 64    * type of the body.
 65    * </ul>
 66    *
 67    * @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
 68    * @author <a href="mailto:gianugo@apache.org">Gianugo Rabellino</a>
 69    * @version $Revision: 541515 $, $Date: 2007-05-24 19:45:06 -0700 (Thu, 24 May 2007) $
 70    */
 71    public class Mkcol implements DAVComponent {
 72    private final Log log = LogFactory.getLog(Mkcol.class);
 73   
 74  4 public void execute(DAVRequest req, DAVResponse res, Location target) throws ServletException, IOException {
 75  4 if (target.isRoot()) {
 76  0 res.setStatus(WebdavStatus.SC_METHOD_NOT_ALLOWED);
 77  0 return;
 78    }
 79   
 80  4 Collection col = target.getCollection();
 81  4 String name = target.getName();
 82   
 83  4 if (name != null && req.getRequestDoc() != null) {
 84    // request body is not supported
 85  1 res.setStatus(WebdavStatus.SC_UNSUPPORTED_MEDIA_TYPE);
 86  1 return;
 87    }
 88   
 89  3 if (col == null) { // parent collection does not exist
 90  1 res.setStatus(WebdavStatus.SC_CONFLICT);
 91  1 return;
 92    }
 93   
 94  2 if (name == null) {
 95  1 if (log.isDebugEnabled()) {
 96  0 log.debug("Collection already exists");
 97    }
 98  1 res.setStatus(WebdavStatus.SC_METHOD_NOT_ALLOWED);
 99    } else {
 100  1 try {
 101  1 res.setStatus(createCollection(col, name));
 102    } catch (DBException e) {
 103  0 log.error("Failed to create collection " + col.getCanonicalName() + "/" + name, e);
 104  0 throw new ServletException(e);
 105    }
 106    }
 107    }
 108   
 109  1 private int createCollection(Collection parent, String name) throws DBException {
 110  1 try {
 111  1 parent.createCollection(name, CollectionConfigurationHelper.createDefaultConfiguration(name));
 112  1 if (log.isDebugEnabled()) {
 113  0 log.debug("Collection " + name + " created");
 114    }
 115   
 116    // TODO: set SC_FORBIDDEN for system collections
 117    } catch (DBException e) {
 118  0 if (e.faultCode == FaultCodes.COL_DUPLICATE_COLLECTION) {
 119  0 return WebdavStatus.SC_METHOD_NOT_ALLOWED;
 120  0 } else if (e.faultCode == FaultCodes.COL_COLLECTION_NOT_FOUND) {
 121  0 return WebdavStatus.SC_METHOD_NOT_ALLOWED;
 122  0 } else if (e.faultCode == FaultCodes.COL_CANNOT_CREATE) {
 123  0 return WebdavStatus.SC_FORBIDDEN;
 124    }
 125   
 126  0 throw e;
 127    }
 128   
 129  1 return WebdavStatus.SC_CREATED;
 130    }
 131    }