neth        01/08/15 03:45:33

  Modified:    src/org/apache/jmeter/protocol/http/sampler HTTPSampler.java
  Log:
  Slight modification to cater for View Result Tree visualizer
  
  Revision  Changes    Path
  1.20      +150 -36   
jakarta-jmeter/src/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
  
  Index: HTTPSampler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- HTTPSampler.java  2001/08/09 18:48:51     1.19
  +++ HTTPSampler.java  2001/08/15 10:45:33     1.20
  @@ -6,7 +6,7 @@
    * companies.
    *
    * All rights reserved
  - * $Header: 
/home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java,v
 1.19 2001/08/09 18:48:51 mstover1 Exp $
  + * $Header: 
/home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java,v
 1.20 2001/08/15 10:45:33 neth Exp $
    */
   package org.apache.jmeter.protocol.http.sampler;
   
  @@ -23,13 +23,15 @@
   import org.apache.jmeter.samplers.SampleResult;
   import org.apache.jmeter.samplers.Entry;
   
  +import org.apache.log4j.*;
  +
   /**
    *  A sampler which understands all the parts necessary to read statistics about
    *  HTTP requests, including cookies and authentication.
    *
    *@author     Michael Stover
  - *@created    $Date: 2001/08/09 18:48:51 $
  - *@version    $Revision: 1.19 $
  + *@created    $Date: 2001/08/15 10:45:33 $
  + *@version    $Revision: 1.20 $
    */
   public class HTTPSampler implements Sampler
   {
  @@ -81,6 +83,14 @@
   
        protected static String encoding = "iso-8859-1";
   
  +        protected static final String NON_HTTP_RESPONSE_CODE = 
  +          "Non HTTP response code";
  +        protected static final String NON_HTTP_RESPONSE_MESSAGE = 
  +          "Non HTTP response message";
  +
  +        private static Category catClass = Category.getInstance(
  +          HTTPSampler.class.getName());
  +
        /**
         *  Constructor for the HTTPSampler object
         */
  @@ -89,10 +99,11 @@
        }
   
        /**
  -      *  Description of the Method
  +      *  A convenience method to call <code>sample</code> with no redirection
         *
  -      *@param  e  Description of Parameter
  -      *@return    Description of the Returned Value
  +      * @param  e  <code>Entry</code> to be sampled
  +      * @return    results of the sampling
  +         * @see       
org.apache.jmeter.protocol.http.sampler.HTTPSampler.sample(org.apache.jmeter.samplers.Entry,
 boolean)
         */
        public SampleResult sample(Entry e)
        {
  @@ -100,11 +111,12 @@
        }
   
        /**
  -      *  Send POST data from Entry to the open connection.
  +      *  Send POST data from <code>Entry</code> to the open connection.
         *
  -      *@param  connection       Description of Parameter
  -      *@param  url              !ToDo (Parameter description)
  -      *@exception  IOException  Description of Exception
  +      * @param  connection       <code>URLConnection</code> of where POST data 
should be
  +      *                          sent
  +      * @param  url              contains the query string for POST
  +      * @exception  IOException  if an I/O exception occurs
         */
        public void sendPostData(URLConnection connection, UrlConfig url)
                         throws IOException
  @@ -121,13 +133,15 @@
        }
   
        /**
  -      *  Description of the Method
  +      * Returns a <code>HttpURLConnection</code> with request method(GET or POST),
  +         * headers, cookies, authorization properly set for the URL request
         *
  -      *@param  u                Description of Parameter
  -      *@param  url              Description of Parameter
  -      *@param  e                Description of Parameter
  -      *@return                  Description of the Returned Value
  -      *@exception  IOException  Description of Exception
  +      * @param  u                <code>URL</code> of the URL request
  +      * @param  url              <code>UrlConfig</code> of the URL request
  +      * @param  e                <code>Entry</code> from which all other info can
  +         *                       be derived from e.g. cookies, header, authorization
  +      * @return                  <code>HttpURLConnection</code> of the URL request
  +      * @exception  IOException  if an I/O Exception occurs
         */
        protected HttpURLConnection setupConnection(URL u, UrlConfig url, Entry e) 
throws IOException
        {
  @@ -144,21 +158,25 @@
        /**
         *  Gets the UrlConfig attribute of the HTTPSampler object
         *
  -      *@param  e  Description of Parameter
  -      *@return    The UrlConfig value
  +      * @param  e  Description of Parameter
  +      * @return    The UrlConfig value
         */
  -     protected UrlConfig getUrlConfig(Entry e)
  +     public UrlConfig getUrlConfig(Entry e)
        {
  -             return (UrlConfig) e.getConfigElement(UrlConfig.class);
  +                UrlConfig urlConfig = 
(UrlConfig)e.getConfigElement(UrlConfig.class);
  +                catClass.debug("getUrlConfig1 : Returning urlConfig - " + 
urlConfig);
  +             return urlConfig;
        }
   
        /**
  -      *  !ToDo (Method description)
  +      * This method will setup <code>HttpURLConnection</code> to handle post using 
  +         * <code>sendPostData</code> method if the URL request is actually a html 
form
  +         * that needs to be posted
         *
  -      *@param  redirected       !ToDo (Parameter description)
  -      *@param  url              !ToDo (Parameter description)
  -      *@param  conn             !ToDo (Parameter description)
  -      *@exception  IOException  Description of Exception
  +      * @param  redirected       does <code>HttpURLConnection</code> allow 
redirection
  +      * @param  url              contains query string for POST
  +      * @param  conn             <HttpURLConnection> of the URL request
  +      * @exception  IOException  if 
         */
        protected void writeToStream(boolean redirected, UrlConfig url, 
HttpURLConnection conn) throws IOException
        {
  @@ -176,6 +194,17 @@
         * }
         * }
         */
  +
  +        /**
  +         * Extracts all the required cookies for that particular URL request and 
set them
  +         * in the <code>HttpURLConnection</code> passed in
  +         *
  +         * @param conn               <code>HttpUrlConnection</code> which 
represents the 
  +      *                      URL request
  +         * @param u          <code>URL</code> of the URL request
  +         * @param cookieManager      the <code>CookieManager</code> containing all 
the 
  +         *                   cookies for this <code>UrlConfig</code>
  +         */
        private void setConnectionCookie(HttpURLConnection conn, URL u, CookieManager 
cookieManager)
        {
                if (cookieManager != null)
  @@ -188,6 +217,16 @@
                }
        }
   
  +        /**
  +         * Extracts all the required headers for that particular URL request and 
set them
  +         * in the <code>HttpURLConnection</code> passed in
  +         *
  +         * @param conn               <code>HttpUrlConnection</code> which 
represents the 
  +      *                      URL request
  +         * @param u          <code>URL</code> of the URL request
  +         * @param headerManager      the <code>HeaderManager</code> containing all 
the 
  +         *                   cookies for this <code>UrlConfig</code>
  +         */
        private void setConnectionHeaders(HttpURLConnection conn, URL u, HeaderManager 
headerManager)
        {
                if (headerManager != null)
  @@ -205,6 +244,16 @@
                }
        }
   
  +        /**
  +         * Extracts all the required authorization for that particular URL request 
and set 
  +      * them in the <code>HttpURLConnection</code> passed in
  +         *
  +         * @param conn               <code>HttpUrlConnection</code> which 
represents the 
  +      *                      URL request
  +         * @param u          <code>URL</code> of the URL request
  +         * @param authManager        the <code>AuthManager</code> containing all 
the 
  +         *                   cookies for this <code>UrlConfig</code>
  +         */
        private void setConnectionAuthorization(HttpURLConnection conn, URL u, 
AuthManager authManager)
        {
                if (authManager != null)
  @@ -216,24 +265,55 @@
                        }
                }
        }
  -
   
  +        /**
  +         * Get the response code of the URL connection and divide it by 100 thus
  +         * returning 2(for 2xx response codes), 3(for 3xx reponse codes), etc
  +         *
  +         * @param conn               <code>HttpURLConnection</code> of URL request
  +         * @param res                where all results of sampling will be stored
  +         * @param time               time when the URL request was first started
  +         * @return           HTTP response code divided by 100
  +         */
        private int getErrorLevel(HttpURLConnection conn, SampleResult res, long time)
        {
                int errorLevel = 2;
  +                int responseCode = 0;
  +                String message = null;
                try
                {
  -                     errorLevel = ((HttpURLConnection) conn).getResponseCode() / 
100;
  +                     responseCode = ((HttpURLConnection) conn).getResponseCode();
  +                     errorLevel = responseCode / 100;
  +                        message = ((HttpURLConnection) conn).getResponseMessage();
  +                        res.putValue(this.RESPONSE_CODE, 
  +                          String.valueOf(responseCode));
  +                        res.putValue(this.RESPONSE_MESSAGE,
  +                          message);
                }
                catch (Exception e2)
                {
  -                     res.putValue(SampleResult.TEXT_RESPONSE, e2.toString());
  +                     res.putValue(this.TEXT_RESPONSE, e2.toString());
  +                        res.putValue(this.RESPONSE_CODE, 
  +                          NON_HTTP_RESPONSE_CODE);
  +                        res.putValue(this.RESPONSE_MESSAGE, 
  +                          NON_HTTP_RESPONSE_MESSAGE);
                        res.setTime(System.currentTimeMillis() - time);
                        res.putValue(SampleResult.SUCCESS, new Boolean(false));
                }
                return errorLevel;
        }
   
  +        /**
  +         * Follow redirection manually.  Normally if the web server does a 
redirection the
  +         * intermediate page is not returned.  Only the resultant page and the 
response code
  +         * for the page will be returned.  With redirection turned off, the 
response code of
  +         * 3xx will be returned together with a "Location" header-value pair to 
indicate
  +         * that the "Location" value needs to be followed to get the resultant page.
  +         *
  +         * @param conn               connection
  +         * @param u          
  +         * @exception MalformedURLException  if URL is not understood
  +         */
        private void redirectUrl(HttpURLConnection conn, URL u, UrlConfig urlConfig) 
throws MalformedURLException
        {
                String loc = conn.getHeaderField("Location");
  @@ -257,21 +337,33 @@
                URL newUrl = new URL(loc);
                urlConfig.putProperty(UrlConfig.DOMAIN, newUrl.getHost());
                urlConfig.putProperty(UrlConfig.PATH, newUrl.getFile());
  -             urlConfig.removeArguments();
        }
   
  +        /**
  +         * Samples <code>Entry</code> passed in and stores the result in 
  +         * <code>SampleResult</code>
  +         *
  +         * @param e          <code>Entry</code> to be sampled
  +         * @param redirected whether redirection is turned on
  +         * @return           results of the sampling
  +         */
        private SampleResult sample(Entry e, boolean redirected)
        {
  +                catClass.debug("Start : sample2");
                long time;
                SampleResult res = new SampleResult();
                UrlConfig url = getUrlConfig(e);
  +                
                URL u = null;
                try
                {
                        u = url.getUrl();
  -                     res.putValue(URL,u);
  -                     res.putValue(SampleResult.SAMPLE_LABEL, u.toString());
  +                     res.putValue(Sampler.SAMPLE_LABEL, u.toString());
                        System.out.println("Sampling url: " + u);
  +                        if(catClass.isDebugEnabled())
  +                        {
  +                          catClass.debug("sample2 : sampling url - " + u);
  +                        }
                        HttpURLConnection conn = setupConnection(u, url, e);
                        writeToStream(redirected, url, conn);
                        time = System.currentTimeMillis();
  @@ -283,7 +375,7 @@
                        {
                                String ret = readResponse(conn);
                                time = System.currentTimeMillis() - time;
  -                             res.putValue(SampleResult.TEXT_RESPONSE, ret);
  +                             res.putValue(this.TEXT_RESPONSE, ret);
                                res.putValue(SampleResult.SUCCESS, new Boolean(true));
                                getResponseHeaders(conn, res);
                        }
  @@ -306,14 +398,29 @@
                }
                catch (IOException ex)
                {
  -                     //ex.printStackTrace();
  +                     ex.printStackTrace();
                        res.setTime((long) 0);
  -                     res.putValue(SampleResult.TEXT_RESPONSE, ex.toString());
  +                        res.putValue(this.RESPONSE_CODE, 
  +                          NON_HTTP_RESPONSE_CODE);
  +                        res.putValue(this.RESPONSE_MESSAGE, 
  +                          NON_HTTP_RESPONSE_MESSAGE);
  +                     res.putValue(this.TEXT_RESPONSE, ex.toString());
                        res.putValue(SampleResult.SUCCESS, new Boolean(false));
                }
  +                catClass.debug("End : sample2");
                return res;
        }
   
  +        /**
  +         * From the <code>HttpURLConnection</code>, store all the "set-cookie" 
key-pair
  +         * values in the cookieManager of the <code>UrlConfig</code>
  +         *
  +         * @param conn               <code>HttpUrlConnection</code> which 
represents the 
  +      *                      URL request
  +         * @param u          <code>URL</code> of the URL request
  +         * @param cookieManager      the <code>CookieManager</code> containing all 
the 
  +         *                   cookies for this <code>UrlConfig</code>
  +         */
        private void saveConnectionCookies(HttpURLConnection conn, URL u, 
CookieManager cookieManager)
        {
                if (cookieManager != null)
  @@ -328,6 +435,13 @@
                }
        }
   
  +        /**
  +         * Reads the response from the URL connection
  +         *
  +         * @param conn                       URL from which to read response
  +         * @return                   response in <code>String</code>
  +         * @exception IOException    if an I/O exception occurs
  +         */
        protected String readResponse(HttpURLConnection conn) throws IOException
        {
                byte[] buffer = new byte[4096];
  @@ -363,8 +477,8 @@
         *  Gets the ResponseHeaders from the URLConnection, save them to the 
SampleResults
         *  object.
         *
  -      *@param  conn  Description of Parameter
  -      *@param  res   Description of Parameter
  +      *@param  conn          connection from which the headers are read
  +      *@param  res           where the headers read are stored
         */
        protected void getResponseHeaders(HttpURLConnection conn, SampleResult res)
        {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to