Clover coverage report - Cactus 1.4b1 for J2EE API 13
Coverage timestamp: Mon Jul 29 2002 00:34:41 BST
file stats: LOC: 476   Methods: 21
NCLOC: 182   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ServletURL.java 23.5% 62.2% 90.5% 57.2%
 1   
 /*   Generated by AspectJ version 1.0.5 */
 2   
 package org.apache.cactus;
 3   
 import javax.servlet.http.HttpServletRequest;
 4   
 import org.apache.cactus.server.ServletUtil;
 5   
 import org.apache.commons.logging.Log;
 6   
 import org.apache.commons.logging.LogFactory;
 7   
 
 8   
 /** 
 9   
  * Simulate an HTTP URL by breaking it into its different parts :<br> 
 10   
  * <code><pre><b> 
 11   
  * URL = "http://" + serverName (including port) + requestURI ? queryString<br> 
 12   
  * requestURI = contextPath + servletPath + pathInfo 
 13   
  * </b></pre></code> 
 14   
  * From the Servlet 2.2 specification :<br> 
 15   
  * <code><pre><ul><li><b>Context Path</b>: The path prefix associated with the 
 16   
  *   ServletContext that this servlet is a part of. If this context is the 
 17   
  *   default context rooted at the base of the web server's URL namespace, this 
 18   
  *   path will be an empty string. Otherwise, this path starts with a character 
 19   
  *   but does not end with a character.</li> 
 20   
  *   <li><b>Servlet Path</b>: The path section that directly corresponds to the 
 21   
  *   mapping which activated this request. This path starts with a 
 22   
  *   character.</li> 
 23   
  *   <li><b>PathInfo</b>: The part of the request path that is not part of the 
 24   
  *   Context Path or the Servlet Path.</li></ul></pre></code> 
 25   
  * 
 26   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 27   
  * 
 28   
  * @version $Id: ServletURL.java,v 1.5 2002/07/22 12:26:04 vmassol Exp $ 
 29   
  */
 30   
 public class ServletURL {
 31   
   /** 
 32   
        * Name of the parameter in the HTTP request that represents the protocol 
 33   
        * (HTTP, HTTPS, etc) in the URL to simulate. The name is voluntarily long 
 34   
        * so that it will not clash with a user-defined parameter. 
 35   
        */
 36   
   public static final String URL_PROTOCOL_PARAM = "Cactus_URL_Protocol";
 37   
   /** 
 38   
        * Name of the parameter in the HTTP request that represents the Server 
 39   
        * name (+ port) in the URL to simulate. The name is voluntarily long so 
 40   
        * that it will not clash with a user-defined parameter. 
 41   
        */
 42   
   public static final String URL_SERVER_NAME_PARAM = "Cactus_URL_Server";
 43   
   /** 
 44   
        * Name of the parameter in the HTTP request that represents the context 
 45   
        * path in the URL to simulate. The name is voluntarily long so that it 
 46   
        * will not clash with a user-defined parameter. 
 47   
        */
 48   
   public static final String URL_CONTEXT_PATH_PARAM = "Cactus_URL_ContextPath";
 49   
   /** 
 50   
        * Name of the parameter in the HTTP request that represents the Servlet 
 51   
        * Path in the URL to simulate. The name is voluntarily long so that it 
 52   
        * will not clash with a user-defined parameter. 
 53   
        */
 54   
   public static final String URL_SERVLET_PATH_PARAM = "Cactus_URL_ServletPath";
 55   
   /** 
 56   
        * Name of the parameter in the HTTP request that represents the Path Info 
 57   
        * in the URL to simulate. The name is voluntarily long so that it will not 
 58   
        * clash with a user-defined parameter. 
 59   
        */
 60   
   public static final String URL_PATH_INFO_PARAM = "Cactus_URL_PathInfo";
 61   
   /** 
 62   
        * Name of the parameter in the HTTP request that represents the Query 
 63   
        * String in the URL to simulate. The name is voluntarily long so that it 
 64   
        * will not clash with a user-defined parameter. 
 65   
        */
 66   
   public static final String URL_QUERY_STRING_PARAM = "Cactus_URL_QueryString";
 67   
   /** 
 68   
        * Http protocol. 
 69   
        */
 70   
   public static final String PROTOCOL_HTTP = "http";
 71   
   /** 
 72   
        * Https protocol. 
 73   
        */
 74   
   public static final String PROTOCOL_HTTPS = "https";
 75   
   /** 
 76   
        * The server name to simulate (including port number) 
 77   
        */
 78   
   private String serverName;
 79   
   /** 
 80   
        * The context path to simulate 
 81   
        */
 82   
   private String contextPath;
 83   
   /** 
 84   
        * The servlet path to simulate 
 85   
        */
 86   
   private String servletPath;
 87   
   /** 
 88   
        * The Path Info to simulate 
 89   
        */
 90   
   private String pathInfo;
 91   
   /** 
 92   
        * The Query string 
 93   
        */
 94   
   private String queryString;
 95   
   /** 
 96   
        * The protocol to use. Default to HTTP. 
 97   
        */
 98   
   private String protocol;
 99   
   /** 
 100   
        * The logger 
 101   
        */
 102   
   private static Log LOGGER;
 103   
   /** 
 104   
        * Default constructor. Need to call the different setters to make this 
 105   
        * a valid object. 
 106   
        */
 107  5
   public ServletURL() {
 108  5
     super();
 109   
     {
 110   
       /** 
 111   
            * The protocol to use. Default to HTTP. 
 112   
            */
 113  5
       this.protocol = "http";
 114   
     } 
 115   
   } 
 116   
   /** 
 117   
        * Creates the URL to simulate. 
 118   
        * 
 119   
        * @param theProtocol   the protocol to simulate (either 
 120   
        *                      <code>ServletURL.PROTOCOL_HTTP</code> or 
 121   
        *                      <code>ServletURL.PROTOCOL_HTTPS</code>. 
 122   
        * @param theServerName the server name (and port) in the URL to simulate, 
 123   
        *                      i.e. this is the name that will be returned by the 
 124   
        *                      <code>HttpServletRequest.getServerName()</code> and 
 125   
        *                      <code>HttpServletRequest.getServerPort()</code>. Can 
 126   
        *                      be null. If null, then the server name and port from 
 127   
        *                      the Servlet Redirector will be returned. 
 128   
        * @param theContextPath the webapp context path in the URL to simulate, 
 129   
        *                      i.e. this is the name that will be returned by the 
 130   
        *                      <code>HttpServletRequest.getContextPath()</code>. 
 131   
        *                      Can be null. If null, then the context from the 
 132   
        *                      Servlet Redirector will be returned. 
 133   
        *                      Format: "/" + name or an empty string 
 134   
        *                      for the default context. 
 135   
        * @param theServletPath the servlet path in the URL to simulate, 
 136   
        *                      i.e. this is the name that will be returned by the 
 137   
        *                      <code>HttpServletRequest.getServletPath()</code>. 
 138   
        *                      Can be null. Format : "/" + name. 
 139   
        * @param thePathInfo   the path info in the URL to simulate, i.e. this is 
 140   
        *                      the name that will be returned by the 
 141   
        *                      <code>HttpServletRequest.getPathInfo()</code>. Can 
 142   
        *                      be null. Format : "/" + name. 
 143   
        * @param theQueryString the Query string in the URL to simulate, i.e. this 
 144   
        *                       is the string that will be returned by the 
 145   
        *                       <code>HttpServletResquest.getQueryString()</code>. 
 146   
        *                       Can be null. 
 147   
        */
 148  3
   public ServletURL(String theProtocol, String theServerName, String theContextPath, 
 149   
       String theServletPath, String thePathInfo, String theQueryString) {
 150  3
     super();
 151   
     {
 152   
       /** 
 153   
            * The protocol to use. Default to HTTP. 
 154   
            */
 155  3
       this.protocol = "http";
 156   
     } 
 157  3
     this.setProtocol(theProtocol);
 158  3
     this.setServerName(theServerName);
 159  3
     this.setContextPath(theContextPath);
 160  3
     this.setServletPath(theServletPath);
 161  3
     this.setPathInfo(thePathInfo);
 162  3
     this.setQueryString(theQueryString);
 163   
   } 
 164   
   /** 
 165   
        * Creates the URL to simulate, using the default HTTP protocol. 
 166   
        * 
 167   
        * @param theServerName the server name (and port) in the URL to simulate, 
 168   
        *                      i.e. this is the name that will be returned by the 
 169   
        *                      <code>HttpServletRequest.getServerName()</code> and 
 170   
        *                      <code>HttpServletRequest.getServerPort()</code>. Can 
 171   
        *                      be null. If null, then the server name and port from 
 172   
        *                      the Servlet Redirector will be returned. 
 173   
        * @param theContextPath the webapp context path in the URL to simulate, 
 174   
        *                      i.e. this is the name that will be returned by the 
 175   
        *                      <code>HttpServletRequest.getContextPath()</code>. 
 176   
        *                      Can be null. If null, then the context from the 
 177   
        *                      Servlet Redirector will be returned. 
 178   
        *                      Format: "/" + name or an empty string 
 179   
        *                      for the default context. 
 180   
        * @param theServletPath the servlet path in the URL to simulate, 
 181   
        *                      i.e. this is the name that will be returned by the 
 182   
        *                      <code>HttpServletRequest.getServletPath()</code>. 
 183   
        *                      Can be null. Format : "/" + name. 
 184   
        * @param thePathInfo   the path info in the URL to simulate, i.e. this is 
 185   
        *                      the name that will be returned by the 
 186   
        *                      <code>HttpServletRequest.getPathInfo()</code>. Can 
 187   
        *                      be null. Format : "/" + name. 
 188   
        * @param theQueryString the Query string in the URL to simulate, i.e. this 
 189   
        *                       is the string that will be returned by the 
 190   
        *                       <code>HttpServletResquest.getQueryString()</code>. 
 191   
        *                       Can be null. 
 192   
        */
 193  3
   public ServletURL(String theServerName, String theContextPath, String theServletPath, 
 194   
       String thePathInfo, String theQueryString) {
 195  3
     this("http", theServerName, theContextPath, theServletPath, thePathInfo, theQueryString);
 196   
     ;
 197   
   } 
 198   
   /** 
 199   
        * @return the protocol used to connect to the URL (HTTP, HTTPS, etc). 
 200   
        */
 201  3
   public String getProtocol() {
 202  3
     return this.protocol;
 203   
   } 
 204   
 
 205   
   /** 
 206   
        * Sets the protocol to simulate (either 
 207   
        * <code>ServletURL.PROTOCOL_HTTP</code> or 
 208   
        * <code>ServletURL.PROTOCOL_HTTPS</code>. If parameter is null then 
 209   
        * PROTOCOL_HTTP is assumed. 
 210   
        * 
 211   
        * @param theProtocol the protocol to simulate 
 212   
        */
 213  6
   public void setProtocol(String theProtocol) {
 214  6
     if ((!theProtocol.equals("http")) && (!theProtocol.equals("https"))) {
 215  1
       throw new RuntimeException("Invalid protocol [" + theProtocol + 
 216   
           "]. Currently supported protocols are [" + "http" + "] and [" + "https" + "].");
 217   
     } 
 218  5
     this.protocol = theProtocol;
 219   
   } 
 220   
 
 221   
   /** 
 222   
        * @return the simulated URL server name (including the port number) 
 223   
        */
 224  11
   public String getServerName() {
 225  11
     return this.serverName;
 226   
   } 
 227   
 
 228   
   /** 
 229   
        * Sets the server name (and port) in the URL to simulate, ie this is the 
 230   
        * name that will be returned by the 
 231   
        * <code>HttpServletRequest.getServerName()</code> and 
 232   
        * <code>HttpServletRequest.getServerPort()</code>. Does not need to be 
 233   
        * set. If not set or null, then the server name and port from the Servlet 
 234   
        * Redirector will be returned. 
 235   
        * 
 236   
        * @param theServerName the server name and port (ex: 
 237   
        *        "jakarta.apache.org:80") 
 238   
        */
 239  4
   public void setServerName(String theServerName) {
 240  4
     this.serverName = theServerName;
 241   
   } 
 242   
 
 243   
   /** 
 244   
        * @return the simulated URL server name (excluding the port number) 
 245   
        */
 246  2
   public String getHost() {
 247  2
     String host = this.getServerName();
 248  2
     if (host != null) {
 249  2
       int pos = host.indexOf(":");
 250  2
       if (pos > 0) {
 251  2
         host = host.substring(0, pos);
 252   
       } 
 253   
     } 
 254  2
     return host;
 255   
   } 
 256   
 
 257   
   /** 
 258   
        * @return the port number or -1 if none has been defined or it is a bad 
 259   
        *         port 
 260   
        */
 261  3
   public int getPort() {
 262  3
     int port = -1;
 263  3
     if (this.getServerName() != null) {
 264  3
       int pos = this.getServerName().indexOf(":");
 265  3
       if (pos < 0) {
 266  0
         return -1;
 267   
       } 
 268  3
       try {
 269  3
         port = Integer.parseInt(this.getServerName().substring(pos + 1));
 270   
       } catch (NumberFormatException e) {
 271  1
         port = -1;
 272   
       } 
 273   
     } 
 274  3
     return port;
 275   
   } 
 276   
 
 277   
   /** 
 278   
        * @return the simulated URL context path 
 279   
        */
 280  4
   public String getContextPath() {
 281  4
     return this.contextPath;
 282   
   } 
 283   
 
 284   
   /** 
 285   
        * Sets the webapp context path in the URL to simulate, ie this is the 
 286   
        * name that will be returned by the 
 287   
        * <code>HttpServletRequest.getContextPath()</code>. If not set, the 
 288   
        * context from the Servlet Redirector will be returned. Format: "/" + 
 289   
        * name or an empty string for the default context. 
 290   
        * 
 291   
        * @param theContextPath the context path to simulate 
 292   
        */
 293  4
   public void setContextPath(String theContextPath) {
 294  4
     this.contextPath = theContextPath;
 295   
   } 
 296   
 
 297   
   /** 
 298   
        * @return the simulated URL servlet path 
 299   
        */
 300  4
   public String getServletPath() {
 301  4
     return this.servletPath;
 302   
   } 
 303   
 
 304   
   /** 
 305   
        * Sets the servlet path in the URL to simulate, ie this is the name that 
 306   
        * will be returned by the <code>HttpServletRequest.getServletPath()</code>. 
 307   
        * If not set, the servlet path from the Servlet Redirector will be 
 308   
        * returned. Format : "/" + name. 
 309   
        * 
 310   
        * @param theServletPath the servlet path to simulate 
 311   
        */
 312  4
   public void setServletPath(String theServletPath) {
 313  4
     this.servletPath = theServletPath;
 314   
   } 
 315   
 
 316   
   /** 
 317   
        * @return the simulated URL path info 
 318   
        */
 319  4
   public String getPathInfo() {
 320  4
     return this.pathInfo;
 321   
   } 
 322   
 
 323   
   /** 
 324   
        * Sets the path info in the URL to simulate, ie this is the name that will 
 325   
        * be returned by the <code>HttpServletRequest.getPathInfo()</code>. If not 
 326   
        * set, the path info from the Servlet Redirector will be returned. 
 327   
        * Format : "/" + name. 
 328   
        * 
 329   
        * @param thePathInfo the path info to simulate 
 330   
        */
 331  4
   public void setPathInfo(String thePathInfo) {
 332  4
     this.pathInfo = thePathInfo;
 333   
   } 
 334   
 
 335   
   /** 
 336   
        * @return the simulated Query String 
 337   
        */
 338  1
   public String getQueryString() {
 339  1
     return this.queryString;
 340   
   } 
 341   
 
 342   
   /** 
 343   
        * Sets the Query string in the URL to simulate, ie this is the string that 
 344   
        * will be returned by the 
 345   
        * <code>HttpServletResquest.getQueryString()</code>. If not set, the 
 346   
        * query string from the Servlet Redirector will be returned. 
 347   
        * 
 348   
        * @param theQueryString the query string to simulate 
 349   
        */
 350  4
   public void setQueryString(String theQueryString) {
 351  4
     this.queryString = theQueryString;
 352   
   } 
 353   
 
 354   
   /** 
 355   
        * @return the path (contextPath + servletPath + pathInfo) or null if 
 356   
        *         not set 
 357   
        */
 358  2
   public String getPath() {
 359  2
     String path;
 360  2
     path = this.getContextPath() == null ? "" : this.getContextPath();
 361  2
     path += this.getServletPath() == null ? "" : this.getServletPath();
 362  2
     path += this.getPathInfo() == null ? "" : this.getPathInfo();
 363  2
     if (path.length() == 0) {
 364  1
       path = null;
 365   
     } 
 366  2
     return path;
 367   
   } 
 368   
 
 369   
   /** 
 370   
        * Saves the current URL to a <code>WebRequest</code> object. 
 371   
        * 
 372   
        * @param theRequest the object to which the current URL should be saved to 
 373   
        */
 374  0
   public void saveToRequest(WebRequest theRequest) {
 375  0
     theRequest.addParameter("Cactus_URL_Protocol", this.getProtocol(), "GET");
 376  0
     if (this.getServerName() != null) {
 377  0
       theRequest.addParameter("Cactus_URL_Server", this.getServerName(), "GET");
 378   
     } 
 379  0
     if (this.getContextPath() != null) {
 380  0
       theRequest.addParameter("Cactus_URL_ContextPath", this.getContextPath(), "GET");
 381   
     } 
 382  0
     if (this.getServletPath() != null) {
 383  0
       theRequest.addParameter("Cactus_URL_ServletPath", this.getServletPath(), "GET");
 384   
     } 
 385  0
     if (this.getPathInfo() != null) {
 386  0
       theRequest.addParameter("Cactus_URL_PathInfo", this.getPathInfo(), "GET");
 387   
     } 
 388  0
     if (this.getQueryString() != null) {
 389  0
       theRequest.addParameter("Cactus_URL_QueryString", this.getQueryString(), "GET");
 390   
     } 
 391   
   } 
 392   
 
 393   
   /** 
 394   
        * Creates a <code>ServletURL</code> object by loading it's values from the 
 395   
        * HTTP request. 
 396   
        * 
 397   
        * @param theRequest the incoming HTTP request. 
 398   
        * @return the <code>ServletURL</code> object unserialized from the HTTP 
 399   
        *         request 
 400   
        */
 401  0
   public static ServletURL loadFromRequest(HttpServletRequest theRequest) {
 402  0
     String qString = theRequest.getQueryString();
 403  0
     ServletURL url = new ServletURL();
 404  0
     String protocol = ServletUtil.getQueryStringParameter(qString, "Cactus_URL_Protocol");
 405  0
     if (protocol != null) {
 406  0
       url.setProtocol(protocol);
 407   
     } 
 408  0
     String serverName = ServletUtil.getQueryStringParameter(qString, "Cactus_URL_Server");
 409  0
     if (serverName != null) {
 410  0
       url.setServerName(serverName);
 411   
     } 
 412  0
     String contextPath = ServletUtil.getQueryStringParameter(qString, "Cactus_URL_ContextPath");
 413  0
     if (contextPath != null) {
 414  0
       url.setContextPath(contextPath);
 415   
     } 
 416  0
     String servletPath = ServletUtil.getQueryStringParameter(qString, "Cactus_URL_ServletPath");
 417  0
     if (servletPath != null) {
 418  0
       url.setServletPath(servletPath);
 419   
     } 
 420  0
     String pathInfo = ServletUtil.getQueryStringParameter(qString, "Cactus_URL_PathInfo");
 421  0
     if (pathInfo != null) {
 422  0
       url.setPathInfo(pathInfo);
 423   
     } 
 424  0
     String queryString = ServletUtil.getQueryStringParameter(qString, "Cactus_URL_QueryString");
 425  0
     if (queryString != null) {
 426  0
       url.setQueryString(queryString);
 427   
     } 
 428  0
     ServletURL.LOGGER.debug("URL = [" + url + "]");
 429  0
     return url;
 430   
   } 
 431   
 
 432   
   /** 
 433   
        * @return a string representation 
 434   
        */
 435  1
   public String toString() {
 436  1
     StringBuffer buffer = new StringBuffer();
 437  1
     buffer.append("protocol = [" + this.getProtocol() + "], ");
 438  1
     buffer.append("host name = [" + this.getHost() + "], ");
 439  1
     buffer.append("port = [" + this.getPort() + "], ");
 440  1
     buffer.append("context path = [" + this.getContextPath() + "], ");
 441  1
     buffer.append("servlet path = [" + this.getServletPath() + "], ");
 442  1
     buffer.append("path info = [" + this.getPathInfo() + "], ");
 443  1
     buffer.append("query string = [" + this.getQueryString() + "]");
 444  1
     return buffer.toString();
 445   
   } 
 446   
 
 447   
   /** 
 448   
    * Simulate an HTTP URL by breaking it into its different parts :<br> 
 449   
    * <code><pre><b> 
 450   
    * URL = "http://" + serverName (including port) + requestURI ? queryString<br> 
 451   
    * requestURI = contextPath + servletPath + pathInfo 
 452   
    * </b></pre></code> 
 453   
    * From the Servlet 2.2 specification :<br> 
 454   
    * <code><pre><ul><li><b>Context Path</b>: The path prefix associated with the 
 455   
    *   ServletContext that this servlet is a part of. If this context is the 
 456   
    *   default context rooted at the base of the web server's URL namespace, this 
 457   
    *   path will be an empty string. Otherwise, this path starts with a character 
 458   
    *   but does not end with a character.</li> 
 459   
    *   <li><b>Servlet Path</b>: The path section that directly corresponds to the 
 460   
    *   mapping which activated this request. This path starts with a 
 461   
    *   character.</li> 
 462   
    *   <li><b>PathInfo</b>: The part of the request path that is not part of the 
 463   
    *   Context Path or the Servlet Path.</li></ul></pre></code> 
 464   
    * 
 465   
    * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 466   
    * 
 467   
    * @version $Id: ServletURL.java,v 1.5 2002/07/22 12:26:04 vmassol Exp $ 
 468   
    */
 469   
   static {
 470   
     /** 
 471   
          * The logger 
 472   
          */
 473  1
     ServletURL.LOGGER = LogFactory.getLog(ServletURL.class);
 474   
   } 
 475   
 
 476   
 }