Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 131   Methods: 2
NCLOC: 85   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Put.java 63.6% 78% 100% 73.8%
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: Put.java 541515 2007-05-25 02:45:06Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.webadmin.webdav.components;
 21   
 22    import java.io.BufferedInputStream;
 23    import java.io.ByteArrayOutputStream;
 24    import java.io.IOException;
 25    import java.io.InputStream;
 26   
 27    import javax.servlet.ServletException;
 28    import javax.servlet.http.HttpServletResponse;
 29   
 30    import org.apache.xindice.core.Collection;
 31    import org.apache.xindice.core.DBException;
 32    import org.apache.xindice.core.data.Key;
 33    import org.apache.xindice.util.XindiceException;
 34    import org.apache.xindice.webadmin.util.CollectionConfigurationHelper;
 35    import org.apache.xindice.webadmin.webdav.WebdavStatus;
 36    import org.apache.xindice.webadmin.webdav.DAVRequest;
 37    import org.apache.xindice.webadmin.webdav.DAVResponse;
 38    import org.apache.xindice.webadmin.Location;
 39    import org.apache.xindice.xml.dom.DOMParser;
 40    import org.apache.commons.logging.Log;
 41    import org.apache.commons.logging.LogFactory;
 42    import org.w3c.dom.Document;
 43   
 44    /**
 45    * This class implements the Put HTTP command for WebDAV operations on
 46    * Xindice.
 47    *
 48    * @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
 49    * @author <a href="mailto:gianugo@apache.org">Gianugo Rabellino</a>
 50    * @version $Revision: 541515 $, $Date: 2007-05-24 19:45:06 -0700 (Thu, 24 May 2007) $
 51    */
 52    public class Put implements DAVComponent {
 53    private static final Log log = LogFactory.getLog(Put.class);
 54   
 55    /** Size of file transfer buffer in bytes. */
 56    protected static final int BUFFER_SIZE = 4096;
 57   
 58  6 public void execute(DAVRequest req, DAVResponse res, Location target) throws ServletException, IOException {
 59  6 if (target.isRoot()) {
 60  0 res.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
 61  0 return;
 62    }
 63   
 64  6 if (target.getCollection() == null) {
 65    // one or more intermediate collections do not exist
 66  1 res.setStatus(WebdavStatus.SC_CONFLICT);
 67  5 } else if (target.getName() == null) {
 68  1 if (log.isDebugEnabled()) {
 69  0 log.debug("Method Put is not implemented for collections: use Mkcol");
 70    }
 71  1 res.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
 72    } else {
 73  4 Collection col = target.getCollection();
 74  4 String name = target.getName();
 75  4 try {
 76  4 res.setStatus(storeResource(col, name, req.getInputStream()));
 77    } catch (DBException e) {
 78  0 log.error("Failed to create resource " + name + " in the collection " + col.getCanonicalName(), e);
 79  0 throw new ServletException(e);
 80    }
 81    }
 82    }
 83   
 84  4 private int storeResource(Collection col, String name, InputStream input) throws IOException, DBException {
 85    // read input
 86  4 ByteArrayOutputStream rawBinCache = new ByteArrayOutputStream(10 * BUFFER_SIZE);
 87  4 BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE);
 88   
 89  4 int bytesRead;
 90  4 byte[] transferBuffer = new byte[BUFFER_SIZE];
 91  ? while ((bytesRead = in.read(transferBuffer)) != -1) {
 92  4 rawBinCache.write(transferBuffer, 0, bytesRead);
 93    }
 94  4 in.close();
 95  4 byte[] binCache = rawBinCache.toByteArray();
 96   
 97  4 Document document = null;
 98  4 byte[] binary = null;
 99  4 try {
 100  4 document = DOMParser.toDocument(binCache);
 101    } catch (XindiceException e) { // binary resource
 102  2 binary = binCache;
 103    }
 104   
 105  4 if (document != null) { // update resource
 106   
 107  2 if (col.setDocument(new Key(name), document)) {
 108  1 return HttpServletResponse.SC_CREATED;
 109    } else {
 110  1 return HttpServletResponse.SC_NO_CONTENT;
 111    }
 112  2 } else if (binary != null) { // create binary
 113    // check if the collection inline meta is enabled
 114  2 if (!CollectionConfigurationHelper.isInlineMetaEnabled(col)) {
 115  0 if (log.isWarnEnabled()) {
 116  0 log.warn("Collection Inline Metadata is not enabled!");
 117    }
 118  0 return WebdavStatus.SC_FORBIDDEN;
 119    }
 120   
 121  2 if (col.setBinary(new Key(name), binary)) {
 122  1 return HttpServletResponse.SC_CREATED;
 123    } else {
 124  1 return WebdavStatus.SC_NO_CONTENT;
 125    }
 126    }
 127   
 128  0 return WebdavStatus.SC_INTERNAL_SERVER_ERROR;
 129    }
 130   
 131    }