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

📄 proxycontrol.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	public String getContentTypeInclude() {
		return getPropertyAsString(CONTENT_TYPE_INCLUDE);
	}


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

	public void addConfigElement(ConfigElement config) {
	}

	public void startProxy() {
		notifyTestListenersOfStart();
		server = new Daemon(getPort(), this);
		server.start();
	}

	public void addExcludedPattern(String pattern) {
		getExcludePatterns().addItem(pattern);
	}

	public CollectionProperty getExcludePatterns() {
		return (CollectionProperty) getProperty(EXCLUDE_LIST);
	}

	public void addIncludedPattern(String pattern) {
		getIncludePatterns().addItem(pattern);
	}

	public CollectionProperty getIncludePatterns() {
		return (CollectionProperty) getProperty(INCLUDE_LIST);
	}

	public void clearExcludedPatterns() {
		getExcludePatterns().clear();
	}

	public void clearIncludedPatterns() {
		getIncludePatterns().clear();
	}

	/**
	 * @return the target controller node
	 */
	public JMeterTreeNode getTarget() {
		return target;
	}

	/**
	 * Sets the target node where the samples generated by the proxy have to be
	 * stored.
	 */
	public void setTarget(JMeterTreeNode target) {
		this.target = target;
	}

	/**
	 * Receives the recorded sampler from the proxy server for placing in the
	 * test tree. param serverResponse to be added to allow saving of the
	 * server's response while recording. A future consideration.
	 */
	public synchronized void deliverSampler(HTTPSamplerBase sampler, TestElement[] subConfigs, SampleResult result) {
		if (filterContentType(result) && filterUrl(sampler)) {
			JMeterTreeNode myTarget = findTargetControllerNode();
			Collection defaultConfigurations = findApplicableElements(myTarget, ConfigTestElement.class, false);
			Collection userDefinedVariables = findApplicableElements(myTarget, Arguments.class, true);

			removeValuesFromSampler(sampler, defaultConfigurations);
			replaceValues(sampler, subConfigs, userDefinedVariables);
			sampler.setAutoRedirects(samplerRedirectAutomatically);
			sampler.setFollowRedirects(samplerFollowRedirects);
			sampler.setUseKeepAlive(useKeepAlive);
			sampler.setImageParser(samplerDownloadImages);

			placeSampler(sampler, subConfigs, myTarget);

			notifySampleListeners(new SampleEvent(result, "WorkBench")); // TODO - is this the correct threadgroup name?
		}
		else {
			if(log.isDebugEnabled()) {
				log.debug("Sample excluded based on url or content-type: " + result.getUrlAsString() + " - " + result.getContentType());
			}
			result.setSampleLabel("["+result.getSampleLabel()+"]");
			notifySampleListeners(new SampleEvent(result, "WorkBench")); // TODO - is this the correct threadgroup name?
		}
	}

	public void stopProxy() {
		if (server != null) {
			server.stopServer();
			try {
				server.join(1000); // wait for server to stop
			} catch (InterruptedException e) {
			}
			notifyTestListenersOfEnd();
			server = null;
		}
	}

    // Package protected to allow test case access
    boolean filterUrl(HTTPSamplerBase sampler) {
		String domain = sampler.getDomain();
		if (domain == null || domain.length() == 0) {
			return false;
		}

		String url = generateMatchUrl(sampler);
		CollectionProperty includePatterns = getIncludePatterns();
		if (includePatterns.size() > 0) {
			if (!matchesPatterns(url, includePatterns)) {
				return false;
			}
		}

		CollectionProperty excludePatterns = getExcludePatterns();
		if (excludePatterns.size() > 0) {
			if (matchesPatterns(url, excludePatterns)) {
				return false;
			}
		}

		return true;
	}

    // Package protected to allow test case access
    /**
     * Filter the response based on the content type.
     * If no include nor exclude filter is specified, the result will be included
     * 
     * @param result the sample result to check
     */
    boolean filterContentType(SampleResult result) {
    	String includeExp = getContentTypeInclude(); 
    	String excludeExp = getContentTypeExclude();
    	// If no expressions are specified, we let the sample pass
    	if((includeExp == null || includeExp.length() == 0) &&
    			(excludeExp == null || excludeExp.length() == 0)
    			)
    	{
    		return true;
    	}
    	
    	// Check that we have a content type
    	String sampleContentType = result.getContentType();    	
    	if(sampleContentType == null || sampleContentType.length() == 0) {
        	if(log.isDebugEnabled()) {
        		log.debug("No Content-type found for : " + result.getUrlAsString());
        	}
    		
    		return true;
    	}

    	if(log.isDebugEnabled()) {
    		log.debug("Content-type to filter : " + sampleContentType);
    	}
    	// Check if the include pattern is mathed
    	if(includeExp != null && includeExp.length() > 0) {
        	if(log.isDebugEnabled()) {
        		log.debug("Include expression : " + includeExp);
        	}    		
    		
    		Pattern pattern = null;
    		try {
    			pattern = JMeterUtils.getPatternCache().getPattern(includeExp, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
    			if(!JMeterUtils.getMatcher().contains(sampleContentType, pattern)) {
    				return false;
    			}
    		} catch (MalformedCachePatternException e) {
    			log.warn("Skipped invalid content include pattern: " + includeExp, e);
    		}
    	}

    	// Check if the exclude pattern is mathed
    	if(excludeExp != null && excludeExp.length() > 0) {
        	if(log.isDebugEnabled()) {
        		log.debug("Exclude expression : " + excludeExp);
        	}

    		Pattern pattern = null;
    		try {
    			pattern = JMeterUtils.getPatternCache().getPattern(excludeExp, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
    			if(JMeterUtils.getMatcher().contains(sampleContentType, pattern)) {
    				return false;
    			}
    		} catch (MalformedCachePatternException e) {
    			log.warn("Skipped invalid content exclude pattern: " + includeExp, e);
    		}
    	}

    	return true;
	}

	/*
	 * Helper method to add a Response Assertion
	 */
	private void addAssertion(JMeterTreeModel model, JMeterTreeNode node) throws IllegalUserActionException {
		ResponseAssertion ra = new ResponseAssertion();
		ra.setProperty(TestElement.GUI_CLASS, ASSERTION_GUI);
		ra.setName("Check response");
		ra.setTestFieldResponseData();
		model.addComponent(ra, node);
	}

	/*
	 * Helper method to add a Divider
	 */
	private void addDivider(JMeterTreeModel model, JMeterTreeNode node) throws IllegalUserActionException {
		GenericController sc = new GenericController();
		sc.setProperty(TestElement.GUI_CLASS, LOGIC_CONTROLLER_GUI);
		sc.setName("-------------------"); // $NON-NLS-1$
		model.addComponent(sc, node);
	}

	/**
	 * Helper method to add a Simple Controller to contain the samplers.
	 * 
	 * @param model
	 *            Test component tree model
	 * @param node
	 *            Node in the tree where we will add the Controller
	 * @param name
	 *            A name for the Controller
	 */
	private void addSimpleController(JMeterTreeModel model, JMeterTreeNode node, String name)
			throws IllegalUserActionException {
		GenericController sc = new GenericController();
		sc.setProperty(TestElement.GUI_CLASS, LOGIC_CONTROLLER_GUI);
		sc.setName(name);
		model.addComponent(sc, node);
	}

	/**
	 * Helpler method to replicate any timers found within the Proxy Controller
	 * into the provided sampler, while replacing any occurences of string _T_
	 * in the timer's configuration with the provided deltaT.
	 * 
	 * @param model
	 *            Test component tree model
	 * @param node
	 *            Sampler node in where we will add the timers
	 * @param deltaT
	 *            Time interval from the previous request
	 */
	private void addTimers(JMeterTreeModel model, JMeterTreeNode node, long deltaT) {
		TestPlan variables = new TestPlan();
		variables.addParameter("T", Long.toString(deltaT)); // $NON-NLS-1$
		ValueReplacer replacer = new ValueReplacer(variables);
		JMeterTreeNode mySelf = model.getNodeOf(this);
		Enumeration children = mySelf.children();
		while (children.hasMoreElements()) {
			JMeterTreeNode templateNode = (JMeterTreeNode) children.nextElement();
			if (templateNode.isEnabled()) {
				TestElement template = templateNode.getTestElement();
				if (template instanceof Timer) {
					TestElement timer = (TestElement) template.clone();
					try {
						replacer.undoReverseReplace(timer);
						model.addComponent(timer, node);
					} catch (InvalidVariableException e) {
						// Not 100% sure, but I believe this can't happen, so
						// I'll log and throw an error:
						log.error("Program error", e);
						throw new Error(e);
					} catch (IllegalUserActionException e) {
						// Not 100% sure, but I believe this can't happen, so
						// I'll log and throw an error:
						log.error("Program error", e);
						throw new Error(e);
					}
				}
			}
		}
	}

	/**
	 * Finds the first enabled node of a given type in the tree.
	 * 
	 * @param type
	 *            class of the node to be found
	 * 
	 * @return the first node of the given type in the test component tree, or
	 *         <code>null</code> if none was found.
	 */
	private JMeterTreeNode findFirstNodeOfType(Class type) {
		JMeterTreeModel treeModel = GuiPackage.getInstance().getTreeModel();
		List nodes = treeModel.getNodesOfType(type);
		Iterator iter = nodes.iterator();
		while (iter.hasNext()) {
			JMeterTreeNode node = (JMeterTreeNode) iter.next();
			if (node.isEnabled()) {
				return node;
			}
		}
		return null;
	}

	/**
	 * Finds the controller where samplers have to be stored, that is:
	 * <ul>
	 * <li>The controller specified by the <code>target</code> property.
	 * <li>If none was specified, the first RecordingController in the tree.
	 * <li>If none is found, the first ThreadGroup in the tree.
	 * <li>If none is found, the Workspace.
	 * </ul>
	 * 
	 * @return the tree node for the controller where the proxy must store the
	 *         generated samplers.
	 */
	private JMeterTreeNode findTargetControllerNode() {
		JMeterTreeNode myTarget = getTarget();
		if (myTarget != null)
			return myTarget;
		myTarget = findFirstNodeOfType(RecordingController.class);
		if (myTarget != null)

⌨️ 快捷键说明

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