Clover coverage report - Cactus 1.5 for J2EE API 1.2
Coverage timestamp: Wed Feb 18 2004 09:04:33 EST
file stats: LOC: 350   Methods: 11
NCLOC: 137   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
WebRequest.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * ====================================================================
 3   
  *
 4   
  * The Apache Software License, Version 1.1
 5   
  *
 6   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 7   
  * reserved.
 8   
  *
 9   
  * Redistribution and use in source and binary forms, with or without
 10   
  * modification, are permitted provided that the following conditions
 11   
  * are met:
 12   
  *
 13   
  * 1. Redistributions of source code must retain the above copyright
 14   
  *    notice, this list of conditions and the following disclaimer.
 15   
  *
 16   
  * 2. Redistributions in binary form must reproduce the above copyright
 17   
  *    notice, this list of conditions and the following disclaimer in
 18   
  *    the documentation and/or other materials provided with the
 19   
  *    distribution.
 20   
  *
 21   
  * 3. The end-user documentation included with the redistribution, if
 22   
  *    any, must include the following acknowlegement:
 23   
  *       "This product includes software developed by the
 24   
  *        Apache Software Foundation (http://www.apache.org/)."
 25   
  *    Alternately, this acknowlegement may appear in the software itself,
 26   
  *    if and wherever such third-party acknowlegements normally appear.
 27   
  *
 28   
  * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
 29   
  *    Foundation" must not be used to endorse or promote products
 30   
  *    derived from this software without prior written permission. For
 31   
  *    written permission, please contact apache@apache.org.
 32   
  *
 33   
  * 5. Products derived from this software may not be called "Apache"
 34   
  *    nor may "Apache" appear in their names without prior written
 35   
  *    permission of the Apache Group.
 36   
  *
 37   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 38   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 39   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 40   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 41   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 42   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 43   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 44   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 45   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 46   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 47   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 48   
  * SUCH DAMAGE.
 49   
  * ====================================================================
 50   
  *
 51   
  * This software consists of voluntary contributions made by many
 52   
  * individuals on behalf of the Apache Software Foundation.  For more
 53   
  * information on the Apache Software Foundation, please see
 54   
  * <http://www.apache.org/>.
 55   
  *
 56   
  */
 57   
 package org.apache.cactus;
 58   
 
 59   
 import java.net.HttpURLConnection;
 60   
 import java.util.StringTokenizer;
 61   
 
 62   
 import org.apache.cactus.client.ClientException;
 63   
 import org.apache.cactus.client.WebResponseObjectFactory;
 64   
 import org.apache.cactus.client.connector.http.ConnectionHelper;
 65   
 import org.apache.cactus.client.connector.http.ConnectionHelperFactory;
 66   
 import org.apache.cactus.configuration.WebConfiguration;
 67   
 import org.apache.cactus.util.ChainedRuntimeException;
 68   
 
 69   
 /**
 70   
  * Extends {@link BaseWebRequest} to add properties specific to the
 71   
  * Cactus Web Redirectors.
 72   
  *
 73   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
 74   
  * @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
 75   
  *
 76   
  * @version $Id: WebRequest.java,v 1.25.2.1 2003/10/23 15:10:08 vmassol Exp $
 77   
  */
 78   
 public class WebRequest extends BaseWebRequest
 79   
 {
 80   
     
 81   
     /**
 82   
      * The URL to simulate
 83   
      */
 84   
     private ServletURL url;
 85   
 
 86   
     /**
 87   
      * Automatic session creation flag (default is true).
 88   
      */
 89   
     private boolean isAutomaticSession = true;
 90   
 
 91   
     /**
 92   
      * Redirector Name. This is to let the user the possibility to override
 93   
      * the default Redirector Name specified in <code>cactus.properties</code>.
 94   
      */
 95   
     private String redirectorName;
 96   
 
 97   
     /**
 98   
      * Default constructor that requires that 
 99   
      * {@link #setConfiguration(Configuration)} be called before the methods
 100   
      * requiring a configuration object.
 101   
      * 
 102   
      */
 103  0
     public WebRequest()
 104   
     {
 105   
     }
 106   
 
 107   
     /**
 108   
      * @param theConfiguration the Cactus configuration
 109   
      */
 110  0
     public WebRequest(WebConfiguration theConfiguration)
 111   
     {
 112  0
         super(theConfiguration);
 113   
     }
 114   
 
 115   
     /**
 116   
      * Override the redirector Name defined in <code>cactus.properties</code>.
 117   
      * This is useful to define a per test case Name (for example, if some
 118   
      * test case need to have authentication turned on and not other tests,
 119   
      * etc).
 120   
      *
 121   
      * @param theRedirectorName the new redirector Name to use
 122   
      */
 123  0
     public void setRedirectorName(String theRedirectorName)
 124   
     {
 125  0
         this.redirectorName = theRedirectorName;
 126   
     }
 127   
 
 128   
     /**
 129   
      * @return the overriden redirector Name or null if none has been defined
 130   
      */
 131  0
     public String getRedirectorName()
 132   
     {
 133  0
         return this.redirectorName;
 134   
     }
 135   
 
 136   
     /**
 137   
      * @param isAutomaticSession whether the redirector servlet will
 138   
      *        automatically create the HTTP session or not. Default is true.
 139   
      */
 140  0
     public void setAutomaticSession(boolean isAutomaticSession)
 141   
     {
 142  0
         this.isAutomaticSession = isAutomaticSession;
 143   
     }
 144   
 
 145   
     /**
 146   
      * @return true if session will be automatically created for the user or
 147   
      *         false otherwise.
 148   
      */
 149  0
     public boolean getAutomaticSession()
 150   
     {
 151  0
         return this.isAutomaticSession;
 152   
     }
 153   
 
 154   
     /**
 155   
      * Sets the simulated URL. A URL is of the form :<br>
 156   
      * <code><pre><b>
 157   
      * URL = "http://" + serverName (including port) + requestURI ? queryString
 158   
      * <br>requestURI = contextPath + servletPath + pathInfo
 159   
      * </b></pre></code>
 160   
      * From the Servlet 2.2 specification :<br>
 161   
      * <code><pre><ul>
 162   
      * <li><b>Context Path</b>: The path prefix associated with the
 163   
      *   ServletContext that this servlet is a part of. If this context is the
 164   
      *   default context rooted at the base of the web server's URL namespace,
 165   
      *   this path will be an empty string. Otherwise, this path starts with a
 166   
      *   character but does not end with a character.</li>
 167   
      * <li><b>Servlet Path</b>: The path section that directly corresponds to
 168   
      *   the mapping which activated this request. This path starts with a
 169   
      *   character.</li>
 170   
      * <li><b>PathInfo</b>: The part of the request path that is not part of the
 171   
      *   Context Path or the Servlet Path.</li>
 172   
      * </ul></pre></code>
 173   
      *
 174   
      * @param theServerName the server name (and port) in the URL to simulate,
 175   
      *                      i.e. this is the name that will be returned by the
 176   
      *                      <code>HttpServletRequest.getServerName()</code> and
 177   
      *                      <code>HttpServletRequest.getServerPort()</code>.
 178   
      * @param theContextPath the webapp context path in the URL to simulate,
 179   
      *                      i.e. this is the name that will be returned by the
 180   
      *                      <code>HttpServletRequest.getContextPath()</code>.
 181   
      *                      Can be null. Format: "/" + name or an empty string
 182   
      *                      for the default context.
 183   
      * @param theServletPath the servlet path in the URL to simulate,
 184   
      *                      i.e. this is the name that will be returned by the
 185   
      *                      <code>HttpServletRequest.getServletPath()</code>.
 186   
      *                      Can be null. Format : "/" + name.
 187   
      * @param thePathInfo   the path info in the URL to simulate, i.e. this is
 188   
      *                      the name that will be returned by the
 189   
      *                      <code>HttpServletRequest.getPathInfo()</code>. Can
 190   
      *                      be null. Format : "/" + name.
 191   
      * @param theQueryString the Query string in the URL to simulate, i.e. this
 192   
      *                       is the string that will be returned by the
 193   
      *                       <code>HttpServletResquest.getQueryString()</code>.
 194   
      *                       Can be null.
 195   
      */
 196  0
     public void setURL(String theServerName, String theContextPath, 
 197   
         String theServletPath, String thePathInfo, String theQueryString)
 198   
     {
 199  0
         this.url = new ServletURL(theServerName, theContextPath, 
 200   
             theServletPath, thePathInfo, theQueryString);
 201   
 
 202   
         // Now automatically add all HTTP parameters to the list of passed
 203   
         // parameters
 204  0
         addQueryStringParameters(theQueryString);
 205   
     }
 206   
 
 207   
     /**
 208   
      * @return the simulated URL
 209   
      */
 210  0
     public ServletURL getURL()
 211   
     {
 212  0
         return this.url;
 213   
     }
 214   
 
 215   
     /**
 216   
      * @return a string representation of the request
 217   
      */
 218  0
     public String toString()
 219   
     {
 220  0
         StringBuffer buffer = new StringBuffer();
 221   
 
 222  0
         buffer.append("simulation URL = [" + getURL() + "], ");
 223  0
         buffer.append("automatic session = [" + getAutomaticSession() + "], ");
 224   
 
 225  0
         buffer.append(super.toString());
 226   
         
 227  0
         return buffer.toString();
 228   
     }
 229   
 
 230   
     /**
 231   
      * Extract the HTTP parameters that might have been specified on the
 232   
      * query string and add them to the list of parameters to pass to the
 233   
      * servlet redirector.
 234   
      *
 235   
      * @param theQueryString the Query string in the URL to simulate, i.e. this
 236   
      *                       is the string that will be returned by the
 237   
      *                       <code>HttpServletResquest.getQueryString()</code>.
 238   
      *                       Can be null.
 239   
      */
 240  0
     private void addQueryStringParameters(String theQueryString)
 241   
     {
 242  0
         if (theQueryString == null)
 243   
         {
 244  0
             return;
 245   
         }
 246   
 
 247  0
         String nameValue = null;
 248  0
         StringTokenizer tokenizer = new StringTokenizer(theQueryString, "&");
 249  0
         int breakParam = -1;
 250   
 
 251  0
         while (tokenizer.hasMoreTokens())
 252   
         {
 253  0
             nameValue = tokenizer.nextToken();
 254  0
             breakParam = nameValue.indexOf("=");
 255   
 
 256  0
             if (breakParam != -1)
 257   
             {
 258  0
                 addParameter(nameValue.substring(0, breakParam), 
 259   
                     nameValue.substring(breakParam + 1));
 260   
             }
 261   
             else
 262   
             {
 263  0
                 throw new RuntimeException("Bad QueryString [" + theQueryString
 264   
                     + "] NameValue pair: [" + nameValue + "]");
 265   
             }
 266   
         }
 267   
     }
 268   
     
 269   
     /**
 270   
      * Gets an HTTP session id by calling the server side and retrieving
 271   
      * the jsessionid cookie in the HTTP response. This is achieved by
 272   
      * calling the Cactus redirector used by the current test case.
 273   
      * 
 274   
      * @return the HTTP session id as a <code>HttpSessionCookie</code> object
 275   
      */
 276  0
     public HttpSessionCookie getSessionCookie()
 277   
     {
 278  0
         if (getConfiguration() == null)
 279   
         {
 280  0
             throw new ChainedRuntimeException("setConfiguration() should have "
 281   
                 + "been called prior to calling getSessionCookie()");
 282   
         }
 283   
         
 284  0
         ConnectionHelper helper = ConnectionHelperFactory.getConnectionHelper(
 285   
             ((WebConfiguration) getConfiguration()).getRedirectorURL(this), 
 286   
             getConfiguration());
 287   
 
 288  0
         WebRequest obtainSessionIdRequest = new WebRequest(
 289   
             (WebConfiguration) getConfiguration());
 290   
             
 291   
         
 292   
         //Not sure whether I should be adding the service parameter to
 293   
         //this request (this) or to the obtainSessionIdRequest
 294   
         //seems obvious that it should be the obtainSessionIdRequest
 295  0
         RequestDirectives directives = 
 296   
             new RequestDirectives(obtainSessionIdRequest);
 297  0
         directives.setService(ServiceEnumeration.CREATE_SESSION_SERVICE);
 298   
 
 299  0
         HttpURLConnection resultConnection;
 300  0
         try
 301   
         {
 302  0
             resultConnection =
 303   
                 helper.connect(obtainSessionIdRequest, getConfiguration());
 304   
         }
 305   
         catch (Throwable e)
 306   
         {
 307  0
             throw new ChainedRuntimeException("Failed to connect to ["
 308   
                 + ((WebConfiguration) getConfiguration()).getRedirectorURL(this)
 309   
                 + "]", e);
 310   
         }
 311   
 
 312  0
         WebResponse response;
 313  0
         try
 314   
         {
 315  0
             response =
 316   
                 (WebResponse) new WebResponseObjectFactory().getResponseObject(
 317   
                     WebResponse.class.getName(),
 318   
                     obtainSessionIdRequest,
 319   
                     resultConnection);
 320   
         }
 321   
         catch (ClientException e)
 322   
         {
 323  0
             throw new ChainedRuntimeException("Failed to connect to ["
 324   
                 + ((WebConfiguration) getConfiguration()).getRedirectorURL(this)
 325   
                 + "]", e);
 326   
         }
 327   
 
 328  0
         Cookie cookie = response.getCookieIgnoreCase("jsessionid");
 329   
 
 330   
         // TODO: Add a constructor to the Cookie class that takes a Cookie
 331   
         // as parameter.
 332   
 
 333  0
         HttpSessionCookie sessionCookie = null;
 334   
 
 335  0
         if (cookie != null)                
 336   
         {
 337  0
             sessionCookie = new HttpSessionCookie(cookie.getDomain(), 
 338   
                 cookie.getName(), cookie.getValue());
 339  0
             sessionCookie.setComment(cookie.getComment());
 340  0
             sessionCookie.setExpiryDate(cookie.getExpiryDate());
 341  0
             sessionCookie.setPath(cookie.getPath());
 342  0
             sessionCookie.setSecure(cookie.isSecure());
 343   
         }
 344   
                 
 345  0
         return sessionCookie;
 346   
     }
 347   
 
 348   
 
 349   
 }
 350