⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proxycontrolgui.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.protocol.http.proxy.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.apache.jmeter.control.Controller;
import org.apache.jmeter.control.gui.LogicControllerGui;
import org.apache.jmeter.engine.util.ValueReplacer;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.JMeterGUIComponent;
import org.apache.jmeter.gui.UnsharedComponent;
import org.apache.jmeter.gui.tree.JMeterTreeNode;
import org.apache.jmeter.gui.util.HorizontalPanel;
import org.apache.jmeter.gui.util.MenuFactory;
import org.apache.jmeter.gui.util.PowerTableModel;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.protocol.http.proxy.ProxyControl;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.WorkBench;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

public class ProxyControlGui extends LogicControllerGui implements JMeterGUIComponent, ActionListener, ItemListener,
		KeyListener, UnsharedComponent {
	private static transient Logger log = LoggingManager.getLoggerForClass();

	private static final long serialVersionUID = 1L;

	private JTextField portField;

	/**
	 * Used to indicate that HTTP request headers should be captured. The
	 * default is to capture the HTTP request headers, which are specific to
	 * particular browser settings.
	 */
	private JCheckBox httpHeaders;

	/**
	 * Whether to group requests together based on inactivity separation periods --
	 * and how to handle such grouping afterwards.
	 */
	private JComboBox groupingMode;

	/**
	 * Add an Assertion to the first sample of each set
	 */
	private JCheckBox addAssertions;

	/**
	 * Set/clear the Use Keep-Alive box on the samplers (default is true)
	 */
	private JCheckBox useKeepAlive;

	/*
	 * Use regexes to match the source data
	 */
	private JCheckBox regexMatch;

	/**
	 * The list of sampler type names to choose from
	 */
	private JComboBox samplerTypeName;

	/**
	 * Set/clear the Redirect automatically box on the samplers (default is false)
	 */
	private JCheckBox samplerRedirectAutomatically;

	/**
	 * Set/clear the Follow-redirects box on the samplers (default is true)
	 */
	private JCheckBox samplerFollowRedirects;

	/**
	 * Set/clear the Download images box on the samplers (default is false)
	 */
	private JCheckBox samplerDownloadImages;

	/*
	 * Spoof the client into thinking that it is communicating with http
	 * even if it is really https.
	 */
	private JCheckBox httpsSpoof;

	private JTextField httpsMatch;
	
	/**
	 * Regular expression to include results based on content type
	 */
	private JTextField contentTypeInclude;

	/**
	 * Regular expression to exclude results based on content type
	 */
	private JTextField contentTypeExclude;
	
	/**
	 * List of available target controllers
	 */
	private JComboBox targetNodes;

	private DefaultComboBoxModel targetNodesModel;

	private ProxyControl model;

	private JTable excludeTable;

	private PowerTableModel excludeModel;

	private JTable includeTable;

	private PowerTableModel includeModel;

	private static final String CHANGE_TARGET = "change_target"; // $NON-NLS-1$

	private JButton stop, start, restart;

	//+ action names
	private static final String STOP = "stop"; // $NON-NLS-1$

	private static final String START = "start"; // $NON-NLS-1$

	private static final String RESTART = "restart"; // $NON-NLS-1$

	private static final String ENABLE_RESTART = "enable_restart"; // $NON-NLS-1$

	private static final String ADD_INCLUDE = "add_include"; // $NON-NLS-1$

	private static final String ADD_EXCLUDE = "add_exclude"; // $NON-NLS-1$

	private static final String DELETE_INCLUDE = "delete_include"; // $NON-NLS-1$

	private static final String DELETE_EXCLUDE = "delete_exclude"; // $NON-NLS-1$
	//- action names

	private static final String INCLUDE_COL = JMeterUtils.getResString("patterns_to_include"); // $NON-NLS-1$

	private static final String EXCLUDE_COL = JMeterUtils.getResString("patterns_to_exclude"); // $NON-NLS-1$

	// Used by itemListener
	private static final String PORTFIELD = "portField"; // $NON-NLS-1$

	public ProxyControlGui() {
		super();
		log.debug("Creating ProxyControlGui");
		init();
	}

	public TestElement createTestElement() {
		model = makeProxyControl();
		log.debug("creating/configuring model = " + model);
		modifyTestElement(model);
		return model;
	}

	protected ProxyControl makeProxyControl() {
		ProxyControl local = new ProxyControl();
		return local;
	}

	/**
	 * Modifies a given TestElement to mirror the data in the gui components.
	 * 
	 * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
	 */
	public void modifyTestElement(TestElement el) {
		if (excludeTable.isEditing()) {// Bug 42948
			excludeTable.getCellEditor().stopCellEditing();
		}
		if (includeTable.isEditing()) {// Bug 42948
			includeTable.getCellEditor().stopCellEditing();
		}
		configureTestElement(el);
		if (el instanceof ProxyControl) {
			model = (ProxyControl) el;
			model.setPort(portField.getText());
			setIncludeListInProxyControl(model);
			setExcludeListInProxyControl(model);
			model.setCaptureHttpHeaders(httpHeaders.isSelected());
			model.setGroupingMode(groupingMode.getSelectedIndex());
			model.setAssertions(addAssertions.isSelected());
			model.setSamplerTypeName(samplerTypeName.getSelectedIndex());
			model.setSamplerRedirectAutomatically(samplerRedirectAutomatically.isSelected());
			model.setSamplerFollowRedirects(samplerFollowRedirects.isSelected());
			model.setUseKeepAlive(useKeepAlive.isSelected());
			model.setSamplerDownloadImages(samplerDownloadImages.isSelected());
			model.setRegexMatch(regexMatch.isSelected());
			model.setHttpsSpoof(httpsSpoof.isSelected());
			model.setHttpsSpoofMatch(httpsMatch.getText());
			model.setContentTypeInclude(contentTypeInclude.getText());
			model.setContentTypeExclude(contentTypeExclude.getText());
			TreeNodeWrapper nw = (TreeNodeWrapper) targetNodes.getSelectedItem();
			if (nw == null) {
				model.setTarget(null);
			} else {
				model.setTarget(nw.getTreeNode());
			}
		}
	}

	protected void setIncludeListInProxyControl(ProxyControl element) {
		List includeList = getDataList(includeModel, INCLUDE_COL);
		element.setIncludeList(includeList);
	}

	protected void setExcludeListInProxyControl(ProxyControl element) {
		List excludeList = getDataList(excludeModel, EXCLUDE_COL);
		element.setExcludeList(excludeList);
	}

	private List getDataList(PowerTableModel p_model, String colName) {
		String[] dataArray = p_model.getData().getColumn(colName);
		List list = new LinkedList();
		for (int i = 0; i < dataArray.length; i++) {
			list.add(dataArray[i]);
		}
		return list;
	}

	public String getLabelResource() {
		return "proxy_title"; // $NON-NLS-1$
	}

	public Collection getMenuCategories() {
		return Arrays.asList(new String[] { MenuFactory.NON_TEST_ELEMENTS });
	}

	public void configure(TestElement element) {
		log.debug("Configuring gui with " + element);
		super.configure(element);
		model = (ProxyControl) element;
		portField.setText(model.getPortString());
		httpHeaders.setSelected(model.getCaptureHttpHeaders());
		groupingMode.setSelectedIndex(model.getGroupingMode());
		addAssertions.setSelected(model.getAssertions());
		samplerTypeName.setSelectedIndex(model.getSamplerTypeName());
		samplerRedirectAutomatically.setSelected(model.getSamplerRedirectAutomatically());
		samplerFollowRedirects.setSelected(model.getSamplerFollowRedirects());
		useKeepAlive.setSelected(model.getUseKeepalive());
		samplerDownloadImages.setSelected(model.getSamplerDownloadImages());
		regexMatch.setSelected(model.getRegexMatch());
		httpsSpoof.setSelected(model.getHttpsSpoof());
		httpsMatch.setText(model.getHttpsSpoofMatch());
		contentTypeInclude.setText(model.getContentTypeInclude());
		contentTypeExclude.setText(model.getContentTypeExclude());

		reinitializeTargetCombo();// Set up list of potential targets and
									// enable listener

		populateTable(includeModel, model.getIncludePatterns().iterator());
		populateTable(excludeModel, model.getExcludePatterns().iterator());
		repaint();
	}

	private void populateTable(PowerTableModel p_model, PropertyIterator iter) {
		p_model.clearData();
		while (iter.hasNext()) {
			p_model.addRow(new Object[] { iter.next().getStringValue() });
		}
		p_model.fireTableDataChanged();
	}

	/*
	 * Handles groupingMode. actionPerfomed is not suitable, as that seems to be
	 * activated whenever the Proxy is selected in the Test Plan
	 * Also handles samplerTypeName
	 */
	public void itemStateChanged(ItemEvent e) {
		// System.err.println(e.paramString());
		enableRestart();
	}

	/***************************************************************************
	 * !ToDo (Method description)
	 * 
	 * @param action
	 *            !ToDo (Parameter description)
	 **************************************************************************/
	public void actionPerformed(ActionEvent action) {
		String command = action.getActionCommand();

		// System.err.println(action.paramString()+" "+command+ "
		// "+action.getModifiers());

		if (command.equals(STOP)) {
			model.stopProxy();
			stop.setEnabled(false);
			start.setEnabled(true);
			restart.setEnabled(false);
		} else if (command.equals(START)) {
			startProxy();
		} else if (command.equals(RESTART)) {
			model.stopProxy();
			startProxy();
		} else if (command.equals(ENABLE_RESTART)){
			enableRestart();
		} else if (command.equals(ADD_EXCLUDE)) {
			excludeModel.addNewRow();
			excludeModel.fireTableDataChanged();
			enableRestart();
		} else if (command.equals(ADD_INCLUDE)) {
			includeModel.addNewRow();
			includeModel.fireTableDataChanged();
			enableRestart();
		} else if (command.equals(DELETE_EXCLUDE)) {
			excludeModel.removeRow(excludeTable.getSelectedRow());
			excludeModel.fireTableDataChanged();
			enableRestart();
		} else if (command.equals(DELETE_INCLUDE)) {
			includeModel.removeRow(includeTable.getSelectedRow());
			includeModel.fireTableDataChanged();
			enableRestart();
		} else if (command.equals(CHANGE_TARGET)) {
			log.debug("Change target " + targetNodes.getSelectedItem());
			log.debug("In model " + model);
			TreeNodeWrapper nw = (TreeNodeWrapper) targetNodes.getSelectedItem();
			model.setTarget(nw.getTreeNode());
			enableRestart();
		}
	}

	private void startProxy() {
		ValueReplacer replacer = GuiPackage.getInstance().getReplacer();
		modifyTestElement(model);
		try {
			replacer.replaceValues(model);
			model.startProxy();
			start.setEnabled(false);
			stop.setEnabled(true);
			restart.setEnabled(false);
		} catch (InvalidVariableException e) {
			JOptionPane.showMessageDialog(this, 
					JMeterUtils.getResString("invalid_variables"), // $NON-NLS-1$
					"Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void enableRestart() {
		if (stop.isEnabled()) {
			// System.err.println("Enable Restart");
			restart.setEnabled(true);
		}
	}

	/***************************************************************************
	 * !ToDo (Method description)
	 * 
	 * @param e
	 *            !ToDo (Parameter description)
	 **************************************************************************/
	public void keyPressed(KeyEvent e) {
	}

	/***************************************************************************
	 * !ToDo (Method description)
	 * 
	 * @param e
	 *            !ToDo (Parameter description)
	 **************************************************************************/
	public void keyTyped(KeyEvent e) {
	}

	/***************************************************************************
	 * !ToDo (Method description)
	 * 
	 * @param e
	 *            !ToDo (Parameter description)
	 **************************************************************************/
	public void keyReleased(KeyEvent e) {
		String fieldName = e.getComponent().getName();

		if (fieldName.equals(PORTFIELD)) {
			try {
				Integer.parseInt(portField.getText());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -