Package org.apache.jmeter.protocol.http.proxy

Source Code of org.apache.jmeter.protocol.http.proxy.ProxyControl$Test

/*
* ====================================================================
* 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.proxy;

import java.net.UnknownHostException;
import java.util.*;
import java.io.*;

import junit.framework.TestCase;

import org.apache.jmeter.config.*;
import org.apache.jmeter.control.LogicController;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.protocol.http.config.UrlConfig;
import org.apache.jmeter.protocol.http.control.HttpTestSample;
import org.apache.oro.text.regex.*;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.gui.*;
import org.apache.jmeter.gui.tree.*;
import org.apache.jmeter.protocol.http.control.HttpTestSample;
import org.apache.jmeter.exceptions.IllegalUserActionException;

/************************************************************
*  Title: Apache JMeter Description: Copyright: Copyright (c) 2000 Company:
*  Apache Foundation
*
*@author     Michael Stover
*@created    $Date: 2002/03/06 03:51:36 $
*@version    1.0
***********************************************************/

public class ProxyControl extends AbstractConfigElement implements Serializable
{
  Daemon server;
  List excludes, includes;
  private final int DEFAULT_PORT = 8080;
  transient Perl5Compiler compiler;
  transient Perl5Matcher matcher;
  public final static String PORT = "port";

  /************************************************************
   *  !ToDo (Constructor description)
   ***********************************************************/
  public ProxyControl()
  {
    matcher = new Perl5Matcher();
    compiler = new Perl5Compiler();
    setPort(DEFAULT_PORT);
    excludes = new LinkedList();
    includes = new LinkedList();
  }

  /************************************************************
   *  !ToDo (Method description)
   *
   *@param  port  !ToDo (Parameter description)
   ***********************************************************/
  public void setPort(int port)
  {
    this.putProperty(PORT, new Integer(port));
  }

  public Class getTagHandlerClass()
  {
    return org.apache.jmeter.protocol.http.save.ProxyControlHandler.class;
  }

  /************************************************************
   *  !ToDoo (Method description)
   *
   *@return    !ToDo (Return description)
   ***********************************************************/
  public String getClassLabel()
  {
    return JMeterUtils.getResString("proxy_title");
  }

  /************************************************************
   *  !ToDoo (Method description)
   *
   *@return    !ToDo (Return description)
   ***********************************************************/
  public int getPort()
  {
    if (this.getProperty(PORT) instanceof String)
    {
      setPort(Integer.parseInt((String)getProperty(PORT)));
      return ((Integer)this.getProperty(PORT)).intValue();
    }
    else
    {
      return ((Integer)this.getProperty(PORT)).intValue();
    }
  }

  /************************************************************
   *  !ToDoo (Method description)
   *
   *@return    !ToDo (Return description)
   ***********************************************************/
  public int getDefaultPort()
  {
    return DEFAULT_PORT;
  }

  public Class getGuiClass()
  {
    return org.apache.jmeter.protocol.http.proxy.gui.ProxyControlGui.class;
  }

  /************************************************************
   *  !ToDo (Method description)
   *
   *@return    !ToDo (Return description)
   ***********************************************************/
  public Object clone()
  {
    ProxyControl clone = new ProxyControl();
    configureClone(clone);
    return clone;
  }

  /************************************************************
   *  !ToDo
   *
   *@param  config  !ToDo
   ***********************************************************/
  public void addConfigElement(ConfigElement config)
  {
  }

  /************************************************************
   *  !ToDo (Method description)
   ***********************************************************/
  public void startProxy()
  {
    try
    {
      server = new Daemon(getPort(), this);
      server.start();
    }
    catch (UnknownHostException e)
    {
      e.printStackTrace();
    }
  }

  /************************************************************
   *  !ToDo
   *
   *@param  pattern  !ToDo
   ***********************************************************/
  public void addExcludedPattern(String pattern)
  {
    excludes.add(pattern);
  }

  public List getExcludePatterns()
  {
    return excludes;
  }

  /************************************************************
   *  !ToDo
   *
   *@param  pattern  !ToDo
   ***********************************************************/
  public void addIncludedPattern(String pattern)
  {
    includes.add(pattern);
  }

  public List getIncludePatterns()
  {
    return includes;
  }

  /************************************************************
   *  !ToDo (Method description)
   ***********************************************************/
  public void clearExcludedPatterns()
  {
    excludes.clear();
  }

  /************************************************************
   *  !ToDo (Method description)
   ***********************************************************/
  public void clearIncludedPatterns()
  {
    includes.clear();
  }

  /************************************************************
   *  !ToDo (Method description)
   *
   *@param  config  !ToDo (Parameter description)
   ***********************************************************/
  public void deliverUrlConfig(UrlConfig config)
  {
    if (filterUrl(config))
    {
      placeConfigElement(config);
    }
  }

  /************************************************************
   *  !ToDo (Method description)
   ***********************************************************/
  public void stopProxy()
  {
    if (server != null)
    {
      server.stopServer();
    }
  }

  private boolean filterUrl(UrlConfig config)
  {
    boolean ok = false;
    if (includes.size() == 0)
    {
      ok = true;
    }
    else
    {
      ok = checkIncludes(config);
    }
    if (!ok)
    {
      return ok;
    }
    else
    {
      if (excludes.size() == 0)
      {
        return ok;
      }
      else
      {
        ok = checkExcludes(config);
      }
    }
    return ok;
  }

  private void placeConfigElement(UrlConfig config)
  {
    UrlConfig urlConfig = null;
    JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
    List nodes = treeModel.getNodesOfType(LogicController.class);
    if(nodes.size() == 0)
    {
      nodes = treeModel.getNodesOfType(ThreadGroup.class);
    }
    Iterator iter = nodes.iterator();
    while (iter.hasNext())
    {
      JMeterTreeNode node = (JMeterTreeNode)iter.next();
      JMeterComponentModel sample = (JMeterComponentModel)node.getUserObject();
      if(sample instanceof LogicController)
      {
        treeModel.compileComponent(node);
        Iterator iter2 = ((LogicController)sample).getConfigElements().iterator();
        while(iter2.hasNext())
        {
          try
          {
            urlConfig = (UrlConfig)iter2.next();
            break;
          }
          catch(ClassCastException e)
          {
            urlConfig = null;
          }
        }
      }
      if(urlConfig == null || (urlConfig.getDomain() == null ||
          urlConfig.getDomain().equals("") ||
          urlConfig.getDomain().equals(config.getDomain())) &&
          (urlConfig.getPath() == null ||
          urlConfig.getPath().equals("/") ||
          urlConfig.getPath().equals(config.getPath())))
      {
        if(urlConfig != null && urlConfig.getDomain() != null &&
            !urlConfig.getDomain().equals(""))
        {
          config.setDomain("");
        }
        if(urlConfig != null && urlConfig.getPath() != null &&
            !urlConfig.getPath().equals("/"))
        {
          config.setPath("");
        }
        HttpTestSample test = new HttpTestSample();
        test.setDefaultUrl(config);
        try
        {
          treeModel.addComponent(test,node);
        }
        catch(IllegalUserActionException e)
        {
          JMeterUtils.reportErrorToUser(e.getMessage());
        }
      }
    }
  }

  private boolean checkIncludes(UrlConfig config)
  {
    boolean ok = false;
    Iterator iter = includes.iterator();
    while (iter.hasNext())
    {
      String item = (String)iter.next();
      try
      {
        Pattern pattern = compiler.compile(item);
        ok = matcher.matches(config.getDomain() + config.getPath(), pattern);
      }
      catch (MalformedPatternException e)
      {
        JMeterUtils.reportErrorToUser("Bad Regular expression: " + item);
      }
      if (ok)
      {
        break;
      }
    }
    return ok;
  }

  private boolean checkExcludes(UrlConfig config)
  {
    boolean ok = true;
    Iterator iter = excludes.iterator();
    while (iter.hasNext())
    {
      String item = (String)iter.next();
      try
      {
        Pattern pattern = compiler.compile(item);
        ok = ok && !matcher.matches(config.getDomain() + config.getPath(), pattern);
      }
      catch (MalformedPatternException e)
      {
        JMeterUtils.reportErrorToUser("Bad Regular expression: " + item);
      }
      if (!ok)
      {
        return ok;
      }
    }
    return ok;
  }

  public static class Test extends TestCase
  {
    public Test(String name)
    {
      super(name);
    }

    public void testFiltering() throws Exception
    {
      ProxyControl control = new ProxyControl();
      control.addIncludedPattern(".*\\.jsp");
      control.addExcludedPattern(".*apache.org.*");
      UrlConfig config = new UrlConfig();
      config.setDomain("jakarta.org");
      config.setPath("index.jsp");
      assertTrue(control.filterUrl(config));
      config.setDomain("www.apache.org");
      assertTrue(!control.filterUrl(config));
      config.setPath("header.gif");
      config.setDomain("jakarta.org");
      assertTrue(!control.filterUrl(config));
    }
  }

}
TOP

Related Classes of org.apache.jmeter.protocol.http.proxy.ProxyControl$Test

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.