use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class RegexFunction method execute.
/**
* {@inheritDoc}
*/
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
// $NON-NLS-1$
String valueIndex = "";
// $NON-NLS-1$
String defaultValue = "";
// $NON-NLS-1$
String between = "";
// $NON-NLS-1$
String name = "";
// $NON-NLS-1$
String inputVariable = "";
Pattern searchPattern;
Object[] tmplt;
try {
searchPattern = JMeterUtils.getPatternCache().getPattern(((CompoundVariable) values[0]).execute(), Perl5Compiler.READ_ONLY_MASK);
tmplt = generateTemplate(((CompoundVariable) values[1]).execute());
if (values.length > 2) {
valueIndex = ((CompoundVariable) values[2]).execute();
}
if (valueIndex.length() == 0) {
// $NON-NLS-1$
valueIndex = "1";
}
if (values.length > 3) {
between = ((CompoundVariable) values[3]).execute();
}
if (values.length > 4) {
String dv = ((CompoundVariable) values[4]).execute();
if (dv.length() != 0) {
defaultValue = dv;
}
}
if (values.length > 5) {
name = ((CompoundVariable) values[5]).execute();
}
if (values.length > 6) {
inputVariable = ((CompoundVariable) values[6]).execute();
}
} catch (MalformedCachePatternException e) {
log.error("Malformed cache pattern:{}", values[0], e);
throw new InvalidVariableException("Malformed cache pattern:" + values[0], e);
}
// Relatively expensive operation, so do it once
JMeterVariables vars = getVariables();
if (vars == null) {
// Can happen if called during test closedown
return defaultValue;
}
if (name.length() > 0) {
vars.put(name, defaultValue);
}
String textToMatch = null;
if (inputVariable.length() > 0) {
textToMatch = vars.get(inputVariable);
} else if (previousResult != null) {
textToMatch = previousResult.getResponseDataAsString();
}
if (textToMatch == null || textToMatch.length() == 0) {
return defaultValue;
}
List<MatchResult> collectAllMatches = new ArrayList<>();
try {
PatternMatcher matcher = JMeterUtils.getMatcher();
PatternMatcherInput input = new PatternMatcherInput(textToMatch);
while (matcher.contains(input, searchPattern)) {
MatchResult match = matcher.getMatch();
if (match != null) {
collectAllMatches.add(match);
}
}
} finally {
if (name.length() > 0) {
// $NON-NLS-1$
vars.put(name + "_matchNr", Integer.toString(collectAllMatches.size()));
}
}
if (collectAllMatches.isEmpty()) {
return defaultValue;
}
if (valueIndex.equals(ALL)) {
StringBuilder value = new StringBuilder();
Iterator<MatchResult> it = collectAllMatches.iterator();
boolean first = true;
while (it.hasNext()) {
if (!first) {
value.append(between);
} else {
first = false;
}
value.append(generateResult(it.next(), name, tmplt, vars));
}
return value.toString();
} else if (valueIndex.equals(RAND)) {
MatchResult result = collectAllMatches.get(ThreadLocalRandom.current().nextInt(collectAllMatches.size()));
return generateResult(result, name, tmplt, vars);
} else {
try {
int index = Integer.parseInt(valueIndex) - 1;
if (index >= collectAllMatches.size()) {
return defaultValue;
}
MatchResult result = collectAllMatches.get(index);
return generateResult(result, name, tmplt, vars);
} catch (NumberFormatException e) {
float ratio = Float.parseFloat(valueIndex);
MatchResult result = collectAllMatches.get((int) (collectAllMatches.size() * ratio + .5) - 1);
return generateResult(result, name, tmplt, vars);
}
}
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class TestHTTPSamplersAgainstHttpMirrorServer method getBoundaryStringFromContentType.
private String getBoundaryStringFromContentType(String requestHeaders) {
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
String regularExpression = "^" + HTTPConstants.HEADER_CONTENT_TYPE + ": multipart/form-data; boundary=(.+)$";
Pattern pattern = JMeterUtils.getPattern(regularExpression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.MULTILINE_MASK);
if (localMatcher.contains(requestHeaders, pattern)) {
MatchResult match = localMatcher.getMatch();
String matchString = match.group(1);
// Header may contain ;charset= , regexp extracts it so computed boundary is wrong
int indexOf = matchString.indexOf(';');
if (indexOf >= 0) {
return matchString.substring(0, indexOf);
} else {
return matchString;
}
} else {
return null;
}
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class TestHTTPSamplersAgainstHttpMirrorServer method checkRegularExpression.
private boolean checkRegularExpression(String stringToCheck, String regularExpression) {
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
Pattern pattern = JMeterUtils.getPattern(regularExpression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK);
return localMatcher.contains(stringToCheck, pattern);
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class TestHTTPSamplersAgainstHttpMirrorServer method getSentRequestHeaderValue.
private String getSentRequestHeaderValue(String requestHeaders, String headerName) {
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
String expression = ".*" + headerName + ": (\\d*).*";
Pattern pattern = JMeterUtils.getPattern(expression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK);
if (localMatcher.matches(requestHeaders, pattern)) {
// The value is in the first group, group 0 is the whole match
return localMatcher.getMatch().group(1);
}
return null;
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class HttpMirrorThread method getPositionOfBody.
private static int getPositionOfBody(String stringToCheck) {
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
// The headers and body are divided by a blank line (the \r is to allow for the CR before LF)
// $NON-NLS-1$
String regularExpression = "^\\r$";
Pattern pattern = JMeterUtils.getPattern(regularExpression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.MULTILINE_MASK);
PatternMatcherInput input = new PatternMatcherInput(stringToCheck);
if (localMatcher.contains(input, pattern)) {
MatchResult match = localMatcher.getMatch();
return match.beginOffset(0);
}
// No divider was found
return -1;
}
Aggregations