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 |
} |