View | Details | Raw Unified | Return to bug 45772
Collapse All | Expand All

(-)xdocs/usermanual/component_reference.xml (+35 lines)
Lines 3460-3465 Link Here
3460
<p>For details of all the methods available on each of the above variables, please check the Javadoc</p>
3460
<p>For details of all the methods available on each of the above variables, please check the Javadoc</p>
3461
</component>
3461
</component>
3462
3462
3463
<component name="RegEx User Parameters" index="&sect-num;.7.9"  width="638" height="274" screenshot="regex_user_params.png">
3464
	<description><p>Allows the user to specify dynamic values for HTTP parameters extracted from another HTTP Request using regular expressions.
3465
			RegEx User Parameters are specific to individual threads.</p>
3466
		<p>User Variables can also be specified in the Test Plan but not specific to individual threads. This panel allows
3467
			you to specify reference name of a regular expression that extracts names and values of HTTP request parameters. 
3468
			Regular expression group numbers must be specified for parameter's name and also for parameter's value.</p>
3469
	</description>
3470
	
3471
	<properties>
3472
		<property name="Name" required="">Descriptive name for this element that is shown in the tree.</property>
3473
		<property name="Regular Expression Reference Name" required="Yes">Name of a reference to a regular expression</property>
3474
		<property name="Parameter names group nr" required="Yes">Group nr of regular expression used to extract parameter names</property>
3475
		<property name="Parameter values group nr" required="Yes">Group nr of regular expression used to extract parameter values</property>
3476
	</properties>
3477
	
3478
	<p>Example:</p>
3479
	<p>1. - Create Post Processor Regular Expression for first HTTP Request</p>
3480
	<ul>
3481
		<li>refName - set name of a regular expression Ex. (listParams)</li>
3482
		<li>regular expression - expression that will etract input names and input values attributes
3483
			<br/>
3484
			Ex: input((\s+((name(\s*=\s*(?:["']*(.*?)["']*|[^'">\s]+)))|(value(\s*=\s*(?:"(.*?)"|'(.*?)'|[^'">\s]+)))|(\w+))(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)</li>
3485
		<li>match nr - -1 (in order to iterate through all the possible matches)</li>
3486
	</ul>
3487
	
3488
	<p>2. - Create Pre Processor Reg Exp User Parameters for second HTTP Request</p>
3489
	<ul>
3490
		<li>refName - set the same reference name of a regular expression Ex. (listParams - in our example)</li>
3491
		<li>parameter names group nr - groups nr of regular expression for parameter names Ex.(6 - in our example)</li>
3492
		<li>parameter values group nr - groups nr of regular expression for parameter values Ex.(9 - in our example)</li>
3493
	</ul>
3494
	
3495
	<p>See also the <complink name="Regular Expression Extractor"/> element, which is used to extract parametes names and values</p>
3496
</component>
3497
3463
<a href="#">^</a>
3498
<a href="#">^</a>
3464
3499
3465
</section>
3500
</section>
(-)src/protocol/http/org/apache/jmeter/protocol/http/modifier/gui/RegExUserParametersGui.java (+159 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 * 
17
 */
18
19
package org.apache.jmeter.protocol.http.modifier.gui;
20
21
import java.awt.BorderLayout;
22
import java.awt.Component;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25
import java.util.List;
26
27
import javax.swing.Box;
28
import javax.swing.JPanel;
29
30
import org.apache.jmeter.processor.gui.AbstractPreProcessorGui;
31
import org.apache.jmeter.protocol.http.modifier.RegExUserParameters;
32
import org.apache.jmeter.testelement.TestElement;
33
import org.apache.jmeter.util.JMeterUtils;
34
import org.apache.jorphan.gui.JLabeledTextField;
35
import org.apache.jorphan.logging.LoggingManager;
36
import org.apache.log.Logger;
37
38
public class RegExUserParametersGui extends AbstractPreProcessorGui {
39
	
40
	private static final Logger log = LoggingManager.getLoggerForClass();
41
42
	private JLabeledTextField refRegExRefNameField;
43
	
44
	private JLabeledTextField paramNamesGrNrField;
45
46
	private JLabeledTextField paramValuesGrNrField;
47
48
	public RegExUserParametersGui() {
49
		super();
50
		init();
51
	}
52
53
	public String getLabelResource() {
54
		return "regex_user_parameters_title"; //$NON-NLS-1$
55
	}
56
57
	public void configure(TestElement el) {
58
		super.configure(el);
59
		if (el instanceof RegExUserParameters){
60
			RegExUserParameters re = (RegExUserParameters) el;
61
			paramNamesGrNrField.setText(re.getRegParamNamesGrNr());
62
			paramValuesGrNrField.setText(re.getRegExParamValuesGrNr());
63
			refRegExRefNameField.setText(re.getRegExRefName());
64
		}
65
	}
66
67
	/**
68
	 * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
69
	 */
70
	public TestElement createTestElement() {
71
		RegExUserParameters regExUserParams = new RegExUserParameters();
72
		modifyTestElement(regExUserParams);
73
		return regExUserParams;
74
	}
75
76
	/**
77
	 * Modifies a given TestElement to mirror the data in the gui components.
78
	 * 
79
	 * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
80
	 */
81
	public void modifyTestElement(TestElement extractor) {
82
		super.configureTestElement(extractor);
83
		if (extractor instanceof RegExUserParameters) {
84
			RegExUserParameters regExUserParams = (RegExUserParameters) extractor;		
85
			regExUserParams.setRegExRefName(refRegExRefNameField.getText());
86
			regExUserParams.setRegExParamNamesGrNr(paramNamesGrNrField.getText());
87
			regExUserParams.setRegExParamValuesGrNr(paramValuesGrNrField.getText());
88
		}
89
	}
90
    
91
    /**
92
     * Implements JMeterGUIComponent.clearGui
93
     */
94
    public void clearGui() {
95
        super.clearGui();
96
        
97
        paramNamesGrNrField.setText(""); //$NON-NLS-1$
98
        paramValuesGrNrField.setText(""); //$NON-NLS-1$
99
        refRegExRefNameField.setText(""); //$NON-NLS-1$
100
    }    
101
102
	private void init() {
103
		setLayout(new BorderLayout());
104
		setBorder(makeBorder());
105
106
		Box box = Box.createVerticalBox();
107
		box.add(makeTitlePanel());
108
		add(box, BorderLayout.NORTH);
109
		add(makeParameterPanel(), BorderLayout.CENTER);
110
	}
111
112
	private JPanel makeParameterPanel() {
113
		refRegExRefNameField = new JLabeledTextField(JMeterUtils.getResString("regex_ref_name_field")); //$NON-NLS-1$
114
		paramNamesGrNrField = new JLabeledTextField(JMeterUtils.getResString("regex_params_names_field")); //$NON-NLS-1$
115
		paramValuesGrNrField = new JLabeledTextField(JMeterUtils.getResString("regex_params_values_field")); //$NON-NLS-1$
116
117
		JPanel panel = new JPanel(new GridBagLayout());
118
		GridBagConstraints gbc = new GridBagConstraints();
119
		initConstraints(gbc);
120
		addField(panel, refRegExRefNameField, gbc);
121
		resetContraints(gbc);
122
		addField(panel, paramNamesGrNrField, gbc);
123
		resetContraints(gbc);
124
		gbc.weighty = 1;
125
		addField(panel, paramValuesGrNrField, gbc);
126
//		resetContraints(gbc);
127
//		gbc.weighty = 1;
128
//		addField(panel, defaultField, gbc);
129
		return panel;
130
	}
131
132
	private void addField(JPanel panel, JLabeledTextField field, GridBagConstraints gbc) {
133
		List item = field.getComponentList();
134
		panel.add((Component) item.get(0), gbc.clone());
135
		gbc.gridx++;
136
		gbc.weightx = 1;
137
		gbc.fill=GridBagConstraints.HORIZONTAL;
138
		panel.add((Component) item.get(1), gbc.clone());
139
	}
140
141
	// Next line
142
	private void resetContraints(GridBagConstraints gbc) {
143
		gbc.gridx = 0;
144
		gbc.gridy++;
145
		gbc.weightx = 0;
146
        gbc.fill=GridBagConstraints.NONE;
147
	}
148
149
	private void initConstraints(GridBagConstraints gbc) {
150
		gbc.anchor = GridBagConstraints.NORTHWEST;
151
		gbc.fill = GridBagConstraints.NONE;
152
		gbc.gridheight = 1;
153
		gbc.gridwidth = 1;
154
		gbc.gridx = 0;
155
		gbc.gridy = 0;
156
		gbc.weightx = 0;
157
		gbc.weighty = 0;
158
	}
159
}
(-)src/protocol/http/org/apache/jmeter/protocol/http/modifier/RegExUserParameters.java (+168 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 * 
17
 */
18
19
package org.apache.jmeter.protocol.http.modifier;
20
21
import java.io.Serializable;
22
import java.util.HashMap;
23
import java.util.Map;
24
25
import org.apache.jmeter.config.Argument;
26
import org.apache.jmeter.engine.event.LoopIterationEvent;
27
import org.apache.jmeter.engine.event.LoopIterationListener;
28
import org.apache.jmeter.processor.PreProcessor;
29
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
30
import org.apache.jmeter.samplers.Sampler;
31
import org.apache.jmeter.testelement.AbstractTestElement;
32
import org.apache.jmeter.testelement.TestElement;
33
import org.apache.jmeter.testelement.property.PropertyIterator;
34
import org.apache.jmeter.threads.JMeterVariables;
35
import org.apache.jorphan.logging.LoggingManager;
36
import org.apache.log.Logger;
37
38
public class RegExUserParameters extends AbstractTestElement implements Serializable, PreProcessor, LoopIterationListener {
39
	private static final Logger log = LoggingManager.getLoggerForClass();
40
41
	public static final String REG_EX_REF_NAME = "RegExUserParameters.regex_ref_name";// $NON-NLS-1$
42
43
	public static final String REG_EX_PARAM_NAMES_GR_NR = "RegExUserParameters.param_names_gr_nr";// $NON-NLS-1$
44
45
	public static final String REG_EX_PARAM_VALUES_GR_NR = "RegExUserParameters.param_values_gr_nr";// $NON-NLS-1$
46
47
	/*
48
	 * Although the lock appears to be an instance lock, in fact the lock is
49
	 * shared between all threads in a thread group, but different thread groups
50
	 * have different locks - see the clone() method below
51
	 * 
52
	 * The lock ensures that all the variables are processed together.
53
	 */
54
	private Integer lock = new Integer(0);
55
56
	public void setRegExRefName(String str) {
57
		setProperty(REG_EX_REF_NAME, str);
58
	}
59
60
	public String getRegExRefName() {
61
		return getPropertyAsString(REG_EX_REF_NAME);
62
	}
63
64
	public void setRegExParamNamesGrNr(String str) {
65
		setProperty(REG_EX_PARAM_NAMES_GR_NR, str);
66
	}
67
68
	public String getRegParamNamesGrNr() {
69
		return getPropertyAsString(REG_EX_PARAM_NAMES_GR_NR);
70
	}
71
72
	public void setRegExParamValuesGrNr(String str) {
73
		setProperty(REG_EX_PARAM_VALUES_GR_NR, str);
74
	}
75
76
	public String getRegExParamValuesGrNr() {
77
		return getPropertyAsString(REG_EX_PARAM_VALUES_GR_NR);
78
	}
79
80
	public void process() {
81
		if (log.isDebugEnabled()) {
82
			log.debug(Thread.currentThread().getName());//$NON-NLS-1$
83
		}
84
		setValues();
85
	}
86
87
	private void setValues() {
88
		synchronized (lock) {
89
			if (log.isDebugEnabled()) {
90
				log.debug(Thread.currentThread().getName() + " Running up named: " + getName());//$NON-NLS-1$
91
			}
92
			Sampler entry = getThreadContext().getCurrentSampler();
93
			if (!(entry instanceof HTTPSamplerBase)) {
94
				return;
95
			}
96
97
			Map paramMap = buildParamsMap();
98
			if(paramMap == null){
99
				return;
100
			}
101
102
			HTTPSamplerBase sampler = (HTTPSamplerBase) entry;
103
			PropertyIterator iter = sampler.getArguments().iterator();
104
			while (iter.hasNext()) {
105
				Argument arg = (Argument) iter.next().getObjectValue();
106
				if (log.isDebugEnabled()) {log.debug("param before: "+arg.getName() +" = "+ arg.getValue());}
107
				// if parameter name exists in http request
108
				// then change its value with value obtained with regular expression
109
				if (paramMap.containsKey(arg.getName())) {
110
					String val = (String) paramMap.get(arg.getName());
111
					arg.setValue(val);
112
				}	
113
				if (log.isDebugEnabled()){log.debug("param after: "+arg.getName() +" = "+ arg.getValue());}
114
			}
115
		}
116
	}
117
	
118
	private Map buildParamsMap(){		
119
		String regExRefName = getRegExRefName()+"_";
120
		String grNames = getRegParamNamesGrNr();
121
		String grValues = getRegExParamValuesGrNr();
122
		JMeterVariables jmvars = getThreadContext().getVariables();	
123
		// verify if regex groups exists
124
		if(jmvars.get(regExRefName + "matchNr") == null
125
				|| jmvars.get(regExRefName + 1 + "_g" + grNames) == null 
126
				|| jmvars.get(regExRefName + 1 + "_g" + grValues) == null){
127
			return null;
128
		}
129
		int n = Integer.parseInt(jmvars.get(regExRefName + "matchNr"));	
130
		Map map = new HashMap(n);
131
		for(int i=1; i<n; i++){
132
			map.put(jmvars.get(regExRefName + i + "_g" + grNames), 
133
					jmvars.get(regExRefName + i + "_g" + grValues));
134
		}
135
		return map;
136
	}
137
138
	/**
139
	 * @see LoopIterationListener#iterationStart(LoopIterationEvent)
140
	 */
141
	public void iterationStart(LoopIterationEvent event) {
142
//		setValues();
143
	}
144
145
	/*
146
	 * (non-Javadoc) A new instance is created for each thread group, and the
147
	 * clone() method is then called to create copies for each thread in a
148
	 * thread group. This means that the lock object is common to a thread
149
	 * group; separate thread groups have separate locks. If this is not
150
	 * intended, the lock object could be made static.
151
	 * 
152
	 * @see java.lang.Object#clone()
153
	 */
154
	public Object clone() {
155
		RegExUserParameters up = (RegExUserParameters) super.clone();
156
		up.lock = lock; // ensure that clones share the same lock object
157
		return up;
158
	}
159
160
	/*
161
	 * (non-Javadoc)
162
	 * 
163
	 * @see AbstractTestElement#mergeIn(TestElement)
164
	 */
165
	protected void mergeIn(TestElement element) {
166
		// super.mergeIn(element);
167
	}
168
}

Return to bug 45772