Package org.apache.jmeter.protocol.http.sampler

Source Code of org.apache.jmeter.protocol.http.sampler.HTTPSampler

/*
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation.  All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache JMeter" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache JMeter", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation.  For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jmeter.protocol.http.sampler;

import java.io.*;
import java.net.*;
import java.security.Security;
import java.util.*;

import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.protocol.http.control.*;
import org.apache.jmeter.protocol.http.config.UrlConfig;
import org.apache.jmeter.config.*;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Entry;

/************************************************************
*  A sampler which understands all the parts necessary to read statistics about
*  HTTP requests, including cookies and authentication.
*
*@author     Michael Stover
*@created    $Date: 2001/06/07 23:38:47 $
*@version    $Revision: 1.11 $
***********************************************************/
public class HTTPSampler implements Sampler
{

  public final static String ARGUMENTS = "httpsampler.Arguments";
  public final static String URL = "httpsampler.URL";
  public final static String POST = "httpsampler.POST";
  public final static String GET = "httpsampler.GET";
  public final static String FILE_NAME = "httpsampler.FILE_NAME";
  public final static String FILE_FIELD = "httpsampler.FILE_FIELD";
  public final static String FILE_DATA = "httpsampler.FILE_DATA";
  public final static String FILE_MIMETYPE = "httpsampler.FILE_MIMETYPE";
  public final static String CONTENT_TYPE = "httpsampler.CONTENT_TYPE";
  public final static String NORMAL_FORM = "normal_form";
  public final static String MULTIPART_FORM = "multipart_form";

  private final static String BOUNDARY = "----------------BbAaJj{5002}Zz";
  private final static String CRLF = "\r\n";

  /************************************************************
   *  Constructor for the HTTPSampler object
   ***********************************************************/
  public HTTPSampler()
  {
  }

  /************************************************************
   *  Description of the Method
   *
   *@param  e  Description of Parameter
   *@return    Description of the Returned Value
   ***********************************************************/
  public SampleResult sample(Entry e)
  {
    return sample(e, false);
  }

  /************************************************************
   *  Send POST data from Entry to the open connection.
   *
   *@param  connection       Description of Parameter
   *@param  url              !ToDo (Parameter description)
   *@exception  IOException  Description of Exception
   ***********************************************************/
  public void sendPostData(URLConnection connection, UrlConfig url)
       throws IOException
  {
    ((HttpURLConnection)connection).setRequestMethod("POST");
    String postData = url.getQueryString();
    connection.setRequestProperty("Content-length", "" + postData.length());
    connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
    connection.setDoOutput(true);

    PrintWriter out = new PrintWriter(connection.getOutputStream());
    out.print(postData);
    out.close();
  }

  /*
   * Uploading a file - put in separate sampler
   * else if (contentType.equals(MULTIPART_FORM))
   * {
   * connection.setRequestProperty("Content-type", "multipart/form-data, " + BOUNDARY);
   * connection.setDoOutput(true);
   * OutputStream o = connection.getOutputStream();
   * PrintWriter out = new PrintWriter(o);
   * out.print(CRLF);
   * out.print(CRLF);
   * out.print("--" + BOUNDARY);
   * String[][] args = (String[][]) e.getValue(this.ARGUMENTS);
   * for (int i = 0; i < args.length; i++)
   * {
   * writeFormMultipartStyle(out, args[i][0], args[i][1]);
   * }
   * writeFileToURL(o, (String) e.getValue(this.FILE_NAME),
   * (String) e.getValue(FILE_FIELD), (InputStream) e.getValue(FILE_DATA),
   * (String) e.getValue(FILE_MIMETYPE));
   * out.print("--");
   * o.close();
   * }
   * }
   */

  private void setConnectionCookie(HttpURLConnection conn, URL u, CookieManager cookieManager)
  {
    if (cookieManager != null)
    {
      String cookieHeader = cookieManager.getCookieHeaderForURL(u);
      if (cookieHeader != null)
      {
        conn.setRequestProperty("Cookie", cookieHeader);
      }
    }
  }

  private void setConnectionAuthorization(HttpURLConnection conn, URL u, AuthManager authManager)
  {
    if (authManager != null)
    {
      String authHeader = authManager.getAuthHeaderForURL(u);
      if (authHeader != null)
      {
        conn.setRequestProperty("Authorization", authHeader);
      }
    }
  }



  private int getErrorLevel(HttpURLConnection conn, SampleResult res, long time)
  {
    int errorLevel = 2;
    try
    {
      errorLevel = ((HttpURLConnection)conn).getResponseCode() / 100;
    }
    catch (Exception e2)
    {
      res.putValue(this.TEXT_RESPONSE, e2.toString());
      res.setTime(System.currentTimeMillis() - time);
      res.putValue(SUCCESS, new Boolean(false));
    }
    return errorLevel;
  }

  private void redirectUrl(HttpURLConnection conn, URL u, UrlConfig urlConfig) throws MalformedURLException
  {
    String loc = conn.getHeaderField("Location");
    System.out.println("Original location=" + loc);
    if (loc != null)
    {
      if (loc.indexOf("http") == -1)
      {
        String tempURL = u.toString();
        if (loc.startsWith("/"))
        {
          int ind = tempURL.indexOf("//") + 2;
          loc = tempURL.substring(0, tempURL.indexOf("/", ind) + 1) + loc.substring(1);
        }
        else
        {
          loc = u.toString().substring(0,
              u.toString().lastIndexOf('/') + 1) + loc;
        }
      }
    }
    System.out.println("Modified location=" + loc);
    URL newUrl = new URL(loc);
    urlConfig.putProperty(UrlConfig.DOMAIN, newUrl.getHost());
    urlConfig.putProperty(UrlConfig.PATH, newUrl.getFile());
  }

  private SampleResult sample(Entry e, boolean redirected)
  {
    long time;
    HttpURLConnection conn;
    SampleResult res = new SampleResult();
    UrlConfig url = (UrlConfig)e.getConfigElement(UrlConfig.class);
    URL u = null;
    try
    {
      u = url.getUrl();
      System.out.println("Sampling url: "+u);
      res.putValue(Sampler.SAMPLE_LABEL, u.toString());
      conn = (HttpURLConnection)u.openConnection();
      conn.setFollowRedirects(false);
      conn.setRequestMethod((String)url.getProperty(UrlConfig.METHOD));
      setConnectionCookie(conn, u, (CookieManager)e.getConfigElement(CookieManager.class));
      setConnectionAuthorization(conn, u, (AuthManager)e.getConfigElement(AuthManager.class));
      // if POSTing data, write data to output stream
      if (!redirected && url.getProperty(UrlConfig.METHOD).equals(UrlConfig.POST))
      {
        sendPostData(conn, url);
      }
      time = System.currentTimeMillis();
      conn.connect();
      saveConnectionCookies(conn, u, (CookieManager)e.getConfigElement(CookieManager.class));

      int errorLevel = getErrorLevel(conn, res, time);
      if (errorLevel == 2)
      {
        String ret = readResponse(conn);
        time = System.currentTimeMillis() - time;
        res.putValue(this.TEXT_RESPONSE, ret);
        res.putValue(SUCCESS, new Boolean(true));
      }
      else if (errorLevel == 3)
      {
        redirectUrl(conn, u, url);

        time = System.currentTimeMillis() - time;
        res = sample(e, true);
        time += res.getTime();
      }
      else
      {
        // Could not sample the URL
        System.out.println("URL = "+u);
        throw new IOException(((HttpURLConnection)conn).getResponseMessage());
      }
      res.setTime(time);
      return res;
    }
    catch (IOException ex)
    {
      ex.printStackTrace();
      res.setTime((long)0);
      res.putValue(this.TEXT_RESPONSE, ex.toString());
      res.putValue(SUCCESS, new Boolean(false));
    }
    return res;
  }

  /************************************************************
   *  Writes form data in multipart format.
   *
   *@param  out    Description of Parameter
   *@param  name   Description of Parameter
   *@param  value  Description of Parameter
   ***********************************************************/
  private void writeFormMultipartStyle(PrintWriter out, String name, String value)
  {
    out.print(CRLF);
    out.print("Content-disposition: form-data; name=\"" + name + "\"");
    out.print(CRLF);
    out.print(CRLF);
    out.print(value);
    out.print(CRLF);
    out.println("--" + BOUNDARY);
  }

  /************************************************************
   *  Writes out the contents of a file in correct multipart format.
   *
   *@param  o                Description of Parameter
   *@param  filename         Description of Parameter
   *@param  fieldname        Description of Parameter
   *@param  in               Description of Parameter
   *@param  mimetype         Description of Parameter
   *@exception  IOException  Description of Exception
   ***********************************************************/
  private void writeFileToURL(OutputStream o, String filename, String fieldname,
      InputStream in, String mimetype) throws IOException
  {
    PrintWriter w = new PrintWriter(o);
    BufferedOutputStream out = new BufferedOutputStream(o);
    w.print(CRLF);
    w.print("Content-disposition: form-data; name=\"" + fieldname + "\"; filename=\"" +
        filename + "\"");
    w.print(CRLF);
    w.print("Content-Type: " + mimetype);
    w.print(CRLF);
    w.print(CRLF);

    byte[] buf = new byte[1024 * 100];
    //100k
    int read;
    while ((read = in.read(buf)) != 0)
    {
      out.write(buf, 0, read);
    }
    w.print(CRLF);
    w.print("--" + BOUNDARY);
  }

  private void saveConnectionCookies(HttpURLConnection conn, URL u, CookieManager cookieManager)
  {
    if (cookieManager != null)
    {
      for (int i = 1; conn.getHeaderFieldKey(i) != null; i++)
      {
        if (conn.getHeaderFieldKey(i).equalsIgnoreCase("set-cookie"))
        {
          cookieManager.addCookieFromHeader(conn.getHeaderField(i), u);
        }
      }
    }
  }

  private String readResponse(HttpURLConnection conn) throws IOException
  {
    byte[] buffer = new byte[4096];
    BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
    java.io.ByteArrayOutputStream w = new ByteArrayOutputStream();
    int x = 0;
    while ((x = in.read(buffer)) != -1)
    {
      w.write(buffer, 0, x);
    }
    in.close();
    return w.toString();
  }

  static
  {
    if (!JMeterUtils.getPropDefault("ssl.provider", "none").equals("none"))
    {
      try
      {
        Class c = Class.forName("org.apache.jmeter.protocol.http.util.SSLStaticProvider");
        c.newInstance();
      }
      catch (Exception ex)
      {
        System.err.println("Could not find SSL Provider");
        ex.printStackTrace();
      }
    }
  }

}
TOP

Related Classes of org.apache.jmeter.protocol.http.sampler.HTTPSampler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.