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

📄 proxycontrolgui.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			} catch (NumberFormatException nfe) {
				int length = portField.getText().length();
				if (length > 0) {
					JOptionPane.showMessageDialog(this, "Only digits allowed", "Invalid data",
							JOptionPane.WARNING_MESSAGE);
					// Drop the last character:
					portField.setText(portField.getText().substring(0, length-1));
				}
			}
			enableRestart();
		} else if (fieldName.equals(ENABLE_RESTART)){
			enableRestart();			
		}
	}

	private void init() {
		setLayout(new BorderLayout(0, 5));
		setBorder(makeBorder());

		add(makeTitlePanel(), BorderLayout.NORTH);

		JPanel mainPanel = new JPanel(new BorderLayout());

		Box myBox = Box.createVerticalBox();
		myBox.add(createPortPanel());
		myBox.add(Box.createVerticalStrut(5));
		myBox.add(createTestPlanContentPanel());
		myBox.add(Box.createVerticalStrut(5));
		myBox.add(createHTTPSamplerPanel());
		myBox.add(Box.createVerticalStrut(5));
		myBox.add(createContentTypePanel());
		myBox.add(Box.createVerticalStrut(5));				
		mainPanel.add(myBox, BorderLayout.NORTH);

		Box includeExcludePanel = Box.createVerticalBox();
		includeExcludePanel.add(createIncludePanel());
		includeExcludePanel.add(createExcludePanel());
		mainPanel.add(includeExcludePanel, BorderLayout.CENTER);

		mainPanel.add(createControls(), BorderLayout.SOUTH);

		add(mainPanel, BorderLayout.CENTER);
	}

	private JPanel createControls() {
		start = new JButton(JMeterUtils.getResString("start")); // $NON-NLS-1$
		start.addActionListener(this);
		start.setActionCommand(START);
		start.setEnabled(true);

		stop = new JButton(JMeterUtils.getResString("stop")); // $NON-NLS-1$
		stop.addActionListener(this);
		stop.setActionCommand(STOP);
		stop.setEnabled(false);

		restart = new JButton(JMeterUtils.getResString("restart")); // $NON-NLS-1$
		restart.addActionListener(this);
		restart.setActionCommand(RESTART);
		restart.setEnabled(false);

		JPanel panel = new JPanel();
		panel.add(start);
		panel.add(stop);
		panel.add(restart);
		return panel;
	}

	private JPanel createPortPanel() {
		portField = new JTextField(ProxyControl.DEFAULT_PORT_S, 5);
		portField.setName(PORTFIELD);
		portField.addKeyListener(this);

		JLabel label = new JLabel(JMeterUtils.getResString("port")); // $NON-NLS-1$
		label.setLabelFor(portField);

		httpsSpoof = new JCheckBox(JMeterUtils.getResString("proxy_httpsspoofing")); // $NON-NLS-1$
		httpsSpoof.setSelected(false);
		httpsSpoof.addActionListener(this);
		httpsSpoof.setActionCommand(ENABLE_RESTART);		
		
		httpsMatch = new JTextField(40);
		httpsMatch.addKeyListener(this);
		httpsMatch.setName(ENABLE_RESTART);

		JLabel matchlabel = new JLabel(JMeterUtils.getResString("proxy_httpsspoofing_match")); // $NON-NLS-1$
		matchlabel.setLabelFor(httpsMatch);
		
		HorizontalPanel panel = new HorizontalPanel();
		panel.add(label);
		panel.add(portField);

		panel.add(Box.createHorizontalStrut(10));
		panel.add(httpsSpoof);

		panel.add(matchlabel);
		panel.add(httpsMatch);
		
		return panel;
	}

	private JPanel createTestPlanContentPanel() {
		httpHeaders = new JCheckBox(JMeterUtils.getResString("proxy_headers")); // $NON-NLS-1$
		httpHeaders.setSelected(true); // maintain original default
		httpHeaders.addActionListener(this);
		httpHeaders.setActionCommand(ENABLE_RESTART);

		addAssertions = new JCheckBox(JMeterUtils.getResString("proxy_assertions")); // $NON-NLS-1$
		addAssertions.setSelected(false);
		addAssertions.addActionListener(this);
		addAssertions.setActionCommand(ENABLE_RESTART);

		regexMatch = new JCheckBox(JMeterUtils.getResString("proxy_regex")); // $NON-NLS-1$
		regexMatch.setSelected(false);
		regexMatch.addActionListener(this);
		regexMatch.setActionCommand(ENABLE_RESTART);

		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
				JMeterUtils.getResString("proxy_test_plan_content"))); // $NON-NLS-1$

		HorizontalPanel nodeCreationPanel = new HorizontalPanel();
		nodeCreationPanel.add(httpHeaders);
		nodeCreationPanel.add(addAssertions);
		nodeCreationPanel.add(regexMatch);
		
		mainPanel.add(createTargetPanel());
		mainPanel.add(createGroupingPanel());
		mainPanel.add(nodeCreationPanel);

		return mainPanel;
	}

	private JPanel createHTTPSamplerPanel() {
		DefaultComboBoxModel m = new DefaultComboBoxModel();
		// Note: position of these elements in the menu *must* match the
		// corresponding ProxyControl.SAMPLER_TYPE_* values.
		m.addElement(JMeterUtils.getResString("web_testing_title")); // $NON-NLS-1$
		m.addElement(JMeterUtils.getResString("web_testing2_title")); // $NON-NLS-1$
		samplerTypeName = new JComboBox(m);
		samplerTypeName.setSelectedIndex(0);
		samplerTypeName.addItemListener(this);
		JLabel label2 = new JLabel(JMeterUtils.getResString("proxy_sampler_type")); // $NON-NLS-1$
		label2.setLabelFor(samplerTypeName);

		samplerRedirectAutomatically = new JCheckBox(JMeterUtils.getResString("follow_redirects_auto")); // $NON-NLS-1$
		samplerRedirectAutomatically.setSelected(false);
		samplerRedirectAutomatically.addActionListener(this);
		samplerRedirectAutomatically.setActionCommand(ENABLE_RESTART);
		
		samplerFollowRedirects = new JCheckBox(JMeterUtils.getResString("follow_redirects")); // $NON-NLS-1$
		samplerFollowRedirects.setSelected(true);
		samplerFollowRedirects.addActionListener(this);
		samplerFollowRedirects.setActionCommand(ENABLE_RESTART);
		
		useKeepAlive = new JCheckBox(JMeterUtils.getResString("use_keepalive")); // $NON-NLS-1$
		useKeepAlive.setSelected(true);
		useKeepAlive.addActionListener(this);
		useKeepAlive.setActionCommand(ENABLE_RESTART);

		samplerDownloadImages = new JCheckBox(JMeterUtils.getResString("web_testing_retrieve_images")); // $NON-NLS-1$
		samplerDownloadImages.setSelected(false);
		samplerDownloadImages.addActionListener(this);
		samplerDownloadImages.setActionCommand(ENABLE_RESTART);
		
		HorizontalPanel panel = new HorizontalPanel();
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
				JMeterUtils.getResString("proxy_sampler_settings"))); // $NON-NLS-1$
		panel.add(label2);
		panel.add(samplerTypeName);
		panel.add(samplerRedirectAutomatically);
		panel.add(samplerFollowRedirects);
		panel.add(useKeepAlive);
		panel.add(samplerDownloadImages);

		return panel;
	}
	
	private JPanel createTargetPanel() {
		targetNodesModel = new DefaultComboBoxModel();
		targetNodes = new JComboBox(targetNodesModel);
		targetNodes.setActionCommand(CHANGE_TARGET);
		// Action listener will be added later

		JLabel label = new JLabel(JMeterUtils.getResString("proxy_target")); // $NON-NLS-1$
		label.setLabelFor(targetNodes);

		HorizontalPanel panel = new HorizontalPanel();
		panel.add(label);
		panel.add(targetNodes);

		/*
		 * This listener subscription prevents freeing up the GUI when it's no
		 * longer in use (e.g. on locale change)... plus causes some anoying
		 * NPEs in the GUI instance created by the menu manager just to find out
		 * our name and which menus we want to be in... ... plus I don't think
		 * it's really necessary: configure(TestElement) already takes care of
		 * reinitializing the target combo when we come back to it. And I can't
		 * see how the tree can change in a relevant way without we leaving this
		 * GUI (since it is very unlikely that we will want to record into one
		 * of the controllers created by the proxy). I'll comment it out for the
		 * time being: TODO: remove once we're convinced it's really
		 * unnecessary.
		 */
		/*
		 * try { Class addToTree =
		 * Class.forName("org.apache.jmeter.gui.action.AddToTree"); Class remove =
		 * Class.forName("org.apache.jmeter.gui.action.Remove"); ActionListener
		 * listener = new ActionListener() { public void
		 * actionPerformed(ActionEvent e) { reinitializeTargetCombo(); } };
		 * ActionRouter ar = ActionRouter.getInstance();
		 * ar.addPostActionListener(addToTree, listener);
		 * ar.addPostActionListener(remove, listener); } catch
		 * (ClassNotFoundException e) { // This should never happen -- throw an
		 * Error: throw new Error(e.toString());//JDK1.4: remove .toString() }
		 */

		return panel;
	}

	private JPanel createGroupingPanel() {
		DefaultComboBoxModel m = new DefaultComboBoxModel();
		// Note: position of these elements in the menu *must* match the
		// corresponding ProxyControl.GROUPING_* values.
		m.addElement(JMeterUtils.getResString("grouping_no_groups")); // $NON-NLS-1$
		m.addElement(JMeterUtils.getResString("grouping_add_separators")); // $NON-NLS-1$
		m.addElement(JMeterUtils.getResString("grouping_in_controllers")); // $NON-NLS-1$
		m.addElement(JMeterUtils.getResString("grouping_store_first_only")); // $NON-NLS-1$
		groupingMode = new JComboBox(m);
		groupingMode.setSelectedIndex(0);
		groupingMode.addItemListener(this);

		JLabel label2 = new JLabel(JMeterUtils.getResString("grouping_mode")); // $NON-NLS-1$
		label2.setLabelFor(groupingMode);

		HorizontalPanel panel = new HorizontalPanel();
		panel.add(label2);
		panel.add(groupingMode);

		return panel;
	}
	
	private JPanel createContentTypePanel() {
		contentTypeInclude = new JTextField(30);
		contentTypeInclude.addKeyListener(this);
		contentTypeInclude.setName(ENABLE_RESTART);
		JLabel labelInclude = new JLabel(JMeterUtils.getResString("proxy_content_type_include")); // $NON-NLS-1$
		labelInclude.setLabelFor(contentTypeInclude);
		// Default value
		contentTypeInclude.setText(JMeterUtils.getProperty("proxy.content_type_include")); // $NON-NLS-1$

		contentTypeExclude = new JTextField(30);
		contentTypeExclude.addKeyListener(this);
		contentTypeExclude.setName(ENABLE_RESTART);
		JLabel labelExclude = new JLabel(JMeterUtils.getResString("proxy_content_type_exclude")); // $NON-NLS-1$
		labelExclude.setLabelFor(contentTypeExclude);
		// Default value
		contentTypeExclude.setText(JMeterUtils.getProperty("proxy.content_type_exclude")); // $NON-NLS-1$
		
		HorizontalPanel panel = new HorizontalPanel();
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
				JMeterUtils.getResString("proxy_content_type_filter"))); // $NON-NLS-1$
		panel.add(labelInclude);
		panel.add(contentTypeInclude);
		panel.add(labelExclude);
		panel.add(contentTypeExclude);
		
		return panel;
	}

	private JPanel createIncludePanel() {
		includeModel = new PowerTableModel(new String[] { INCLUDE_COL }, new Class[] { String.class });
		includeTable = new JTable(includeModel);
		includeTable.setPreferredScrollableViewportSize(new Dimension(100, 30));

		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), JMeterUtils
				.getResString("patterns_to_include"))); // $NON-NLS-1$

		panel.add(new JScrollPane(includeTable), BorderLayout.CENTER);
		panel.add(createTableButtonPanel(ADD_INCLUDE, DELETE_INCLUDE), BorderLayout.SOUTH);

		return panel;
	}

	private JPanel createExcludePanel() {
		excludeModel = new PowerTableModel(new String[] { EXCLUDE_COL }, new Class[] { String.class });
		excludeTable = new JTable(excludeModel);
		excludeTable.setPreferredScrollableViewportSize(new Dimension(100, 30));

		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), JMeterUtils
				.getResString("patterns_to_exclude"))); // $NON-NLS-1$

		panel.add(new JScrollPane(excludeTable), BorderLayout.CENTER);
		panel.add(createTableButtonPanel(ADD_EXCLUDE, DELETE_EXCLUDE), BorderLayout.SOUTH);

		return panel;
	}

	private JPanel createTableButtonPanel(String addCommand, String deleteCommand) {
		JPanel buttonPanel = new JPanel();

		JButton addButton = new JButton(JMeterUtils.getResString("add")); // $NON-NLS-1$
		addButton.setActionCommand(addCommand);
		addButton.addActionListener(this);
		buttonPanel.add(addButton);

		JButton deleteButton = new JButton(JMeterUtils.getResString("delete")); // $NON-NLS-1$
		deleteButton.setActionCommand(deleteCommand);
		deleteButton.addActionListener(this);
		buttonPanel.add(deleteButton);

		return buttonPanel;
	}

	private void reinitializeTargetCombo() {
		log.debug("Reinitializing target combo");

		// Stop action notifications while we shuffle this around:
		targetNodes.removeActionListener(this);

		targetNodesModel.removeAllElements();
		GuiPackage gp = GuiPackage.getInstance();
		JMeterTreeNode root;
		if (gp != null) {
			root = (JMeterTreeNode) GuiPackage.getInstance().getTreeModel().getRoot();
			targetNodesModel
					.addElement(new TreeNodeWrapper(null, JMeterUtils.getResString("use_recording_controller"))); // $NON-NLS-1$
			buildNodesModel(root, "", 0);
		}
		TreeNodeWrapper choice = null;
		for (int i = 0; i < targetNodesModel.getSize(); i++) {
			choice = (TreeNodeWrapper) targetNodesModel.getElementAt(i);
			log.debug("Selecting item " + choice + " for model " + model + " in " + this);
			if (choice.getTreeNode() == model.getTarget()) // .equals caused
															// NPE
			{
				break;
			}
		}
		// Reinstate action notifications:
		targetNodes.addActionListener(this);
		// Set the current value:
		targetNodesModel.setSelectedItem(choice);

		log.debug("Reinitialization complete");
	}

	private void buildNodesModel(JMeterTreeNode node, String parent_name, int level) {
		String seperator = " > ";
		if (node != null) {
			for (int i = 0; i < node.getChildCount(); i++) {
				StringBuffer name = new StringBuffer();
				JMeterTreeNode cur = (JMeterTreeNode) node.getChildAt(i);
				TestElement te = cur.getTestElement();
				/*
				 * Will never be true. Probably intended to use
				 * org.apache.jmeter.threads.ThreadGroup rather than
				 * java.lang.ThreadGroup However, that does not work correctly;
				 * whereas treating it as a Controller does. if (te instanceof
				 * ThreadGroup) { name.append(parent_name);
				 * name.append(cur.getName()); name.append(seperator);
				 * buildNodesModel(cur, name.toString(), level); } else
				 */
				if (te instanceof Controller) {
					name.append(spaces(level));
					name.append(parent_name);
					name.append(cur.getName());
					TreeNodeWrapper tnw = new TreeNodeWrapper(cur, name.toString());
					targetNodesModel.addElement(tnw);
					name = new StringBuffer();
					name.append(cur.getName());
					name.append(seperator);
					buildNodesModel(cur, name.toString(), level + 1);
				} else if (te instanceof TestPlan || te instanceof WorkBench) {
					name.append(cur.getName());
					name.append(seperator);
					buildNodesModel(cur, name.toString(), 0);
				}
				// Ignore everything else
			}
		}
	}

	private String spaces(int level) {
		int multi = 4;
		StringBuffer spaces = new StringBuffer(level * multi);
		for (int i = 0; i < level * multi; i++) {
			spaces.append(" "); // $NON-NLS-1$
		}
		return spaces.toString();
	}

	public void setNode(JMeterTreeNode node) {
		getNamePanel().setNode(node);
	}
}

class TreeNodeWrapper {
	private JMeterTreeNode tn;

	private String label;

	private TreeNodeWrapper() {
	};

	public TreeNodeWrapper(JMeterTreeNode tn, String label) {
		this.tn = tn;
		this.label = label;
	}

	public JMeterTreeNode getTreeNode() {
		return tn;
	}

	public String toString() {
		return label;
	}
}

⌨️ 快捷键说明

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