use of org.apache.oro.text.regex.MatchResult 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.MatchResult 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;
}
use of org.apache.oro.text.regex.MatchResult in project jmeter by apache.
the class HtmlParsingUtils method extractStyleURLs.
public static void extractStyleURLs(final URL baseUrl, final URLCollection urls, String styleTagStr) {
Perl5Matcher matcher = JMeterUtils.getMatcher();
Pattern pattern = JMeterUtils.getPatternCache().getPattern(// $NON-NLS-1$
"URL\\(\\s*('|\")(.*)('|\")\\s*\\)", Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK | Perl5Compiler.READ_ONLY_MASK);
PatternMatcherInput input = null;
input = new PatternMatcherInput(styleTagStr);
while (matcher.contains(input, pattern)) {
MatchResult match = matcher.getMatch();
// The value is in the second group
String styleUrl = match.group(2);
urls.addURL(styleUrl, baseUrl);
}
}
use of org.apache.oro.text.regex.MatchResult in project jmeter by apache.
the class URLRewritingModifier method process.
@Override
public void process() {
JMeterContext ctx = getThreadContext();
Sampler sampler = ctx.getCurrentSampler();
if (!(sampler instanceof HTTPSamplerBase)) {
// Ignore non-HTTP samplers
return;
}
SampleResult responseText = ctx.getPreviousResult();
if (responseText == null) {
return;
}
initRegex(getArgumentName());
String text = responseText.getResponseDataAsString();
Perl5Matcher matcher = JMeterUtils.getMatcher();
String value = "";
if (isPathExtension() && isPathExtensionNoEquals() && isPathExtensionNoQuestionmark()) {
if (matcher.contains(text, pathExtensionNoEqualsNoQuestionmarkRegexp)) {
MatchResult result = matcher.getMatch();
value = result.group(1);
}
} else if (// && !isPathExtensionNoQuestionmark()
isPathExtension() && isPathExtensionNoEquals()) {
if (matcher.contains(text, pathExtensionNoEqualsQuestionmarkRegexp)) {
MatchResult result = matcher.getMatch();
value = result.group(1);
}
} else if (// && !isPathExtensionNoEquals()
isPathExtension() && isPathExtensionNoQuestionmark()) {
if (matcher.contains(text, pathExtensionEqualsNoQuestionmarkRegexp)) {
MatchResult result = matcher.getMatch();
value = result.group(1);
}
} else if (// && !isPathExtensionNoEquals() && !isPathExtensionNoQuestionmark()
isPathExtension()) {
if (matcher.contains(text, pathExtensionEqualsQuestionmarkRegexp)) {
MatchResult result = matcher.getMatch();
value = result.group(1);
}
} else // if ! isPathExtension()
{
if (matcher.contains(text, parameterRegexp)) {
MatchResult result = matcher.getMatch();
for (int i = 1; i < result.groups(); i++) {
value = result.group(i);
if (value != null) {
break;
}
}
}
}
// Bug 15025 - save session value across samplers
if (shouldCache()) {
if (value == null || value.length() == 0) {
value = savedValue;
} else {
savedValue = value;
}
}
modify((HTTPSamplerBase) sampler, value);
}
use of org.apache.oro.text.regex.MatchResult in project jmeter by apache.
the class RegexpHTMLParser method getEmbeddedResourceURLs.
/**
* {@inheritDoc}
*/
@Override
public Iterator<URL> getEmbeddedResourceURLs(String userAgent, byte[] html, URL baseUrl, URLCollection urls, String encoding) throws HTMLParseException {
Pattern pattern = null;
Perl5Matcher matcher = null;
try {
matcher = JMeterUtils.getMatcher();
PatternMatcherInput input = localInput.get();
// TODO: find a way to avoid the cost of creating a String here --
// probably a new PatternMatcherInput working on a byte[] would do
// better.
input.setInput(new String(html, encoding));
pattern = JMeterUtils.getPatternCache().getPattern(REGEXP, Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK | Perl5Compiler.READ_ONLY_MASK);
while (matcher.contains(input, pattern)) {
MatchResult match = matcher.getMatch();
String s;
if (log.isDebugEnabled()) {
log.debug("match groups " + match.groups() + " " + match.toString());
}
// Check for a BASE HREF:
for (int g = 1; g <= NUM_BASE_GROUPS && g <= match.groups(); g++) {
s = match.group(g);
if (s != null) {
if (log.isDebugEnabled()) {
log.debug("new baseUrl: " + s + " - " + baseUrl.toString());
}
try {
baseUrl = ConversionUtils.makeRelativeURL(baseUrl, s);
} catch (MalformedURLException e) {
// Maybe it isn't: Ignore the exception.
if (log.isDebugEnabled()) {
log.debug("Can't build base URL from RL " + s + " in page " + baseUrl, e);
}
}
}
}
for (int g = NUM_BASE_GROUPS + 1; g <= match.groups(); g++) {
s = match.group(g);
if (s != null) {
if (log.isDebugEnabled()) {
log.debug("group " + g + " - " + match.group(g));
}
urls.addURL(s, baseUrl);
}
}
}
return urls.iterator();
} catch (UnsupportedEncodingException | MalformedCachePatternException e) {
throw new HTMLParseException(e.getMessage(), e);
} finally {
JMeterUtils.clearMatcherMemory(matcher, pattern);
}
}
Aggregations