Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 281   Methods: 13
NCLOC: 153   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DAVRequest.java 60% 75.9% 84.6% 72%
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   
 18    package org.apache.xindice.webadmin.webdav;
 19   
 20    import org.apache.commons.logging.Log;
 21    import org.apache.commons.logging.LogFactory;
 22   
 23    import org.w3c.dom.Document;
 24    import org.xml.sax.InputSource;
 25   
 26    import javax.servlet.ServletInputStream;
 27    import javax.servlet.http.HttpServletRequest;
 28    import javax.xml.parsers.DocumentBuilder;
 29    import javax.xml.parsers.DocumentBuilderFactory;
 30   
 31    import java.io.IOException;
 32    import java.io.UnsupportedEncodingException;
 33    import java.net.URLDecoder;
 34   
 35    /**
 36    * This is a wrapper class that provides additional DAV-specific functionality.
 37    *
 38    * @version $Revision: 551440 $, $Date: 2007-06-27 21:26:07 -0700 (Wed, 27 Jun 2007) $
 39    */
 40    public class DAVRequest {
 41    public static final int DEPTH_0 = 0;
 42    public static final int DEPTH_1 = 1;
 43    public static final int DEPTH_INFINITY = -1;
 44   
 45    private static final Log log = LogFactory.getLog(DAVRequest.class);
 46   
 47    private HttpServletRequest httpRequest;
 48    private String contextPath;
 49    private Document requestDoc;
 50    private boolean documentInit;
 51   
 52    /**
 53    * Creates new instance of DAVRequest based on incoming HTTP request.
 54    *
 55    * @param request HttpServletRequest
 56    */
 57  36 public DAVRequest(HttpServletRequest request) {
 58  36 httpRequest = request;
 59  36 contextPath = makeContextPath();
 60    }
 61   
 62    /**
 63    * @return the path to calling servlet (not null and no ending '/')
 64    */
 65  36 private String makeContextPath() {
 66  36 StringBuffer path = new StringBuffer();
 67  36 path.append(httpRequest.getContextPath());
 68    // Is it default servlet? If not, servletPath is added to contextPath path.
 69  36 if (httpRequest.getPathInfo() != null) {
 70  0 path.append(httpRequest.getServletPath());
 71    }
 72  36 if (path.length() > 0 && path.charAt(path.length() - 1) == '/') {
 73  0 path.deleteCharAt(path.length() - 1);
 74    }
 75   
 76  36 return path.toString();
 77    }
 78   
 79    /**
 80    * Retreives request document if incoming request has any.
 81    *
 82    * @return Parsed request document if request body has one or null if
 83    * request body was empty or does not have a valid XML.
 84    * @throws IOException ServletInputStream IOException
 85    */
 86  7 public Document getRequestDoc() throws IOException {
 87  7 if (!documentInit) {
 88  7 documentInit = true;
 89  7 requestDoc = parseRequestContent();
 90    }
 91   
 92  7 return requestDoc;
 93    }
 94   
 95    /**
 96    * Wrapper method, gets resource path.
 97    *
 98    * If servlet is mapped as default servlet (with url pattern '/'),
 99    * resource path is obtained from {@link HttpServletRequest#getServletPath},
 100    * and {@link HttpServletRequest#getPathInfo} is always null.
 101    *
 102    * If servlet is mapped to a path pattern (url pattern like '/foo/*'),
 103    * resource path is retrieved from {@link HttpServletRequest#getPathInfo}.
 104    *
 105    * @return Called servlet path.
 106    */
 107  19 public String getPath() {
 108  19 String path = httpRequest.getPathInfo();
 109  19 if (path == null) {
 110  9 path = httpRequest.getServletPath();
 111    }
 112  19 return path;
 113    }
 114   
 115    /**
 116    * Wrapper method, gets HTTP request header.
 117    * @param name Header name
 118    * @return Header value
 119    */
 120  0 public String getHeader(String name) {
 121  0 return httpRequest.getHeader(name);
 122    }
 123   
 124    /**
 125    * Retrieves Overwrite header value from HTTP request. Possible values
 126    * are "T" or "F" (true of false). By default value is "T".
 127    *
 128    * @return False if HTTP request has a header "Overwrite: F", true othewise.
 129    */
 130  11 public boolean getOverwrite() {
 131    // Parsing overwrite header
 132  11 boolean overwrite = true;
 133  11 String overwriteHeader = httpRequest.getHeader("Overwrite");
 134  11 if (overwriteHeader != null) {
 135  2 overwrite = !overwriteHeader.equalsIgnoreCase("F");
 136    }
 137  11 return overwrite;
 138    }
 139   
 140    /**
 141    * Retrieves Depth header value from HTTP request. Possible values
 142    * are "0", "1", and "infinity". By default, value is "infinity".
 143    *
 144    * @return 0, 1, -1 for "0", "1", "infinity" header values respectively.
 145    */
 146  16 public int getDepth() {
 147  16 String value = httpRequest.getHeader("Depth");
 148  16 int depth = -1;
 149  16 if ("0".equals(value)) {
 150  3 depth = 0;
 151  13 } else if ("1".equals(value)) {
 152  1 depth = 1;
 153  12 } else if (value == null || "infinity".equals(value)) {
 154  12 depth = -1;
 155    }
 156   
 157  16 return depth;
 158    }
 159   
 160    /**
 161    * Retrieves Destination header value from HTTP request. Its value is URL,
 162    * it can be absolute or relative. This method returns only local path,
 163    * deleting protocol, server name and context path.
 164    *
 165    * @return Local part of the destination path
 166    */
 167  24 public String getDestinationPath() {
 168  24 String destinationPath = httpRequest.getHeader("Destination");
 169  24 if (destinationPath == null) {
 170  4 return null;
 171    }
 172   
 173  20 int protocolIndex = destinationPath.indexOf("://");
 174  20 if (protocolIndex >= 0) {
 175    // if the Destination URL contains the protocol, we can safely
 176    // trim everything upto the first "/" character after "://"
 177  0 int firstSeparator = destinationPath.indexOf("/", protocolIndex + 4);
 178  0 if (firstSeparator < 0) {
 179  0 destinationPath = "/";
 180    } else {
 181  0 destinationPath = destinationPath.substring(firstSeparator);
 182    }
 183    } else {
 184  20 String hostName = httpRequest.getServerName();
 185  20 if ((hostName != null) && (destinationPath.startsWith(hostName))) {
 186  0 destinationPath = destinationPath.substring(hostName.length());
 187    }
 188   
 189  20 int portIndex = destinationPath.indexOf(":");
 190  20 if (portIndex >= 0) {
 191  0 destinationPath = destinationPath.substring(portIndex);
 192    }
 193   
 194  20 if (destinationPath.startsWith(":")) {
 195  0 int firstSeparator = destinationPath.indexOf("/");
 196  0 if (firstSeparator < 0) {
 197  0 destinationPath = "/";
 198    } else {
 199  0 destinationPath = destinationPath.substring(firstSeparator);
 200    }
 201    }
 202    }
 203   
 204  20 String contextPath = httpRequest.getContextPath();
 205  20 if ((contextPath != null) && (destinationPath.startsWith(contextPath))) {
 206  20 destinationPath = destinationPath.substring(contextPath.length());
 207    }
 208   
 209  20 String pathInfo = httpRequest.getPathInfo();
 210  20 if (pathInfo != null) {
 211  7 String servletPath = httpRequest.getServletPath();
 212  7 if ((servletPath != null) && (destinationPath.startsWith(servletPath))) {
 213  7 destinationPath = destinationPath.substring(servletPath.length());
 214    }
 215    }
 216   
 217  20 try {
 218  20 destinationPath = URLDecoder.decode(destinationPath, "utf-8");
 219    } catch (UnsupportedEncodingException e) {
 220    // won't happen
 221  0 log.error(e);
 222  0 destinationPath = null;
 223    }
 224   
 225  20 return destinationPath;
 226    }
 227   
 228    /**
 229    * Wrapper method, gets servlet context path.
 230    * @return Called servlet context path.
 231    */
 232  5 public String getContextPath() {
 233  5 return contextPath;
 234    }
 235   
 236  7 private Document parseRequestContent() throws IOException {
 237  7 if (httpRequest.getContentLength() != 0) {
 238  5 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 239  5 dbf.setNamespaceAware(true);
 240  5 try {
 241  5 DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
 242  5 return documentBuilder.parse(new InputSource(httpRequest.getInputStream()));
 243    } catch (IOException e) {
 244  0 throw e;
 245    } catch (Exception e) {
 246  0 if (log.isWarnEnabled()) {
 247  0 log.warn("Cannot parse request document", e);
 248    }
 249    }
 250    }
 251   
 252  2 return null;
 253    }
 254   
 255    /**
 256    * Wrapper method, gets servlet input stream.
 257    * @return Servlet input stream.
 258    */
 259  4 public ServletInputStream getInputStream() throws IOException {
 260  4 return httpRequest.getInputStream();
 261    }
 262   
 263    /**
 264    * Wrapper method, gets HTTP request parameter.
 265    *
 266    * @param name Parameter name
 267    * @return Parameter value
 268    */
 269  0 public String getParameter(String name) {
 270  0 return httpRequest.getParameter(name);
 271    }
 272   
 273    /**
 274    * Wrapper method, gets request protocol.
 275    *
 276    * @return Request protocol and version.
 277    */
 278  5 public String getProtocol() {
 279  5 return httpRequest.getProtocol();
 280    }
 281    }