Search in sources :

Example 11 with MatchResult

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);
        }
    }
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) Pattern(org.apache.oro.text.regex.Pattern) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) ArrayList(java.util.ArrayList) MatchResult(org.apache.oro.text.regex.MatchResult) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 12 with MatchResult

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;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 13 with MatchResult

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);
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 14 with MatchResult

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);
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) HTTPSamplerBase(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)

Example 15 with MatchResult

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);
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) MalformedURLException(java.net.MalformedURLException) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Aggregations

MatchResult (org.apache.oro.text.regex.MatchResult)25 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)16 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)15 Pattern (org.apache.oro.text.regex.Pattern)14 ArrayList (java.util.ArrayList)10 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)4 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)4 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3 SampleResult (org.apache.jmeter.samplers.SampleResult)3 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)3 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Charset (java.nio.charset.Charset)2