use of org.apache.oro.text.regex.Pattern 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);
}
// Check for a BASE HREF:
for (int g = 1; g <= NUM_BASE_GROUPS && g <= match.groups(); g++) {
s = match.group(g);
if (s != null) {
log.debug("new baseUrl: {} - {}", s, baseUrl);
try {
baseUrl = ConversionUtils.makeRelativeURL(baseUrl, s);
} catch (MalformedURLException e) {
// Doesn't even look like a URL?
// Maybe it isn't: Ignore the exception.
log.debug("Can't build base URL from URL {} in page {}", s, 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);
}
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class RegexExtractor method initTemplate.
private void initTemplate() {
if (template != null) {
return;
}
// Contains Strings and Integers
List<Object> combined = new ArrayList<>();
String rawTemplate = getTemplate();
PatternMatcher matcher = JMeterUtils.getMatcher();
Pattern templatePattern = // $NON-NLS-1$
JMeterUtils.getPatternCache().getPattern(// $NON-NLS-1$
"\\$(\\d+)\\$", Perl5Compiler.READ_ONLY_MASK & Perl5Compiler.SINGLELINE_MASK);
if (log.isDebugEnabled()) {
log.debug("Pattern = '{}', template = '{}'", templatePattern.getPattern(), rawTemplate);
}
int beginOffset = 0;
MatchResult currentResult;
PatternMatcherInput pinput = new PatternMatcherInput(rawTemplate);
while (matcher.contains(pinput, templatePattern)) {
currentResult = matcher.getMatch();
final int beginMatch = currentResult.beginOffset(0);
if (beginMatch > beginOffset) {
// string is not empty
combined.add(rawTemplate.substring(beginOffset, beginMatch));
}
// add match as Integer
combined.add(Integer.valueOf(currentResult.group(1)));
beginOffset = currentResult.endOffset(0);
}
if (beginOffset < rawTemplate.length()) {
// trailing string is not empty
combined.add(rawTemplate.substring(beginOffset, rawTemplate.length()));
}
if (log.isDebugEnabled()) {
log.debug("Template item count: {}", combined.size());
int i = 0;
for (Object o : combined) {
log.debug("Template item-{}: {} '{}'", i++, o.getClass(), o);
}
}
template = combined;
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class HTTPSamplerBase method downloadPageResources.
/**
* Download the resources of an HTML page.
*
* @param pRes
* result of the initial request - must contain an HTML response
* @param container
* for storing the results, if any
* @param frameDepth
* Depth of this target in the frame structure. Used only to
* prevent infinite recursion.
* @return res if no resources exist, otherwise the "Container" result with one subsample per request issued
*/
protected HTTPSampleResult downloadPageResources(final HTTPSampleResult pRes, final HTTPSampleResult container, final int frameDepth) {
HTTPSampleResult res = pRes;
Iterator<URL> urls = null;
try {
final byte[] responseData = res.getResponseData();
if (responseData.length > 0) {
// Bug 39205
final LinkExtractorParser parser = getParser(res);
if (parser != null) {
String userAgent = getUserAgent(res);
urls = parser.getEmbeddedResourceURLs(userAgent, responseData, res.getURL(), res.getDataEncodingWithDefault());
}
}
} catch (LinkExtractorParseException e) {
// Don't break the world just because this failed:
res.addSubResult(errorResult(e, new HTTPSampleResult(res)));
setParentSampleSuccess(res, false);
}
HTTPSampleResult lContainer = container;
// Iterate through the URLs and download each image:
if (urls != null && urls.hasNext()) {
if (lContainer == null) {
lContainer = new HTTPSampleResult(res);
lContainer.addRawSubResult(res);
}
res = lContainer;
// Get the URL matcher
String allowRegex = getEmbeddedUrlRE();
Perl5Matcher localMatcher = null;
Pattern allowPattern = null;
if (allowRegex.length() > 0) {
try {
allowPattern = JMeterUtils.getPattern(allowRegex);
// don't fetch unless pattern compiles
localMatcher = JMeterUtils.getMatcher();
} catch (MalformedCachePatternException e) {
// NOSONAR
log.warn("Ignoring embedded URL match string: {}", e.getMessage());
}
}
Pattern excludePattern = null;
String excludeRegex = getEmbededUrlExcludeRE();
if (excludeRegex.length() > 0) {
try {
excludePattern = JMeterUtils.getPattern(excludeRegex);
if (localMatcher == null) {
// don't fetch unless pattern compiles
localMatcher = JMeterUtils.getMatcher();
}
} catch (MalformedCachePatternException e) {
// NOSONAR
log.warn("Ignoring embedded URL exclude string: {}", e.getMessage());
}
}
// For concurrent get resources
final List<Callable<AsynSamplerResultHolder>> list = new ArrayList<>();
// init with default value
int maxConcurrentDownloads = CONCURRENT_POOL_SIZE;
boolean isConcurrentDwn = isConcurrentDwn();
if (isConcurrentDwn) {
try {
maxConcurrentDownloads = Integer.parseInt(getConcurrentPool());
} catch (NumberFormatException nfe) {
log.warn(// $NON-NLS-1$
"Concurrent download resources selected, " + // $NON-NLS-1$
"but pool size value is bad. Use default value");
}
// no need to use another thread, do the sample on the current thread
if (maxConcurrentDownloads == 1) {
log.warn("Number of parallel downloads set to 1, (sampler name={})", getName());
isConcurrentDwn = false;
}
}
while (urls.hasNext()) {
// See catch clause below
Object binURL = urls.next();
try {
URL url = (URL) binURL;
if (url == null) {
log.warn("Null URL detected (should not happen)");
} else {
try {
url = escapeIllegalURLCharacters(url);
} catch (Exception e) {
// NOSONAR
res.addSubResult(errorResult(new Exception(url.toString() + " is not a correct URI", e), new HTTPSampleResult(res)));
setParentSampleSuccess(res, false);
continue;
}
log.debug("allowPattern: {}, excludePattern: {}, localMatcher: {}, url: {}", allowPattern, excludePattern, localMatcher, url);
// I don't think localMatcher can be null here, but check just in case
if (allowPattern != null && localMatcher != null && !localMatcher.matches(url.toString(), allowPattern)) {
// we have a pattern and the URL does not match, so skip it
continue;
}
if (excludePattern != null && localMatcher != null && localMatcher.matches(url.toString(), excludePattern)) {
// we have a pattern and the URL does not match, so skip it
continue;
}
try {
url = url.toURI().normalize().toURL();
} catch (MalformedURLException | URISyntaxException e) {
res.addSubResult(errorResult(new Exception(url.toString() + " URI can not be normalized", e), new HTTPSampleResult(res)));
setParentSampleSuccess(res, false);
continue;
}
if (isConcurrentDwn) {
// if concurrent download emb. resources, add to a list for async gets later
list.add(new ASyncSample(url, HTTPConstants.GET, false, frameDepth + 1, getCookieManager(), this));
} else {
// default: serial download embedded resources
HTTPSampleResult binRes = sample(url, HTTPConstants.GET, false, frameDepth + 1);
res.addSubResult(binRes);
setParentSampleSuccess(res, res.isSuccessful() && (binRes == null || binRes.isSuccessful()));
}
}
} catch (ClassCastException e) {
// NOSONAR
res.addSubResult(errorResult(new Exception(binURL + " is not a correct URI", e), new HTTPSampleResult(res)));
setParentSampleSuccess(res, false);
}
}
// IF for download concurrent embedded resources
if (isConcurrentDwn && !list.isEmpty()) {
ResourcesDownloader resourcesDownloader = ResourcesDownloader.getInstance();
try {
// sample all resources
final List<Future<AsynSamplerResultHolder>> retExec = resourcesDownloader.invokeAllAndAwaitTermination(maxConcurrentDownloads, list);
CookieManager cookieManager = getCookieManager();
// add result to main sampleResult
for (Future<AsynSamplerResultHolder> future : retExec) {
// this call will not block as the futures return by invokeAllAndAwaitTermination
// are either done or cancelled
AsynSamplerResultHolder binRes = future.get();
if (cookieManager != null) {
CollectionProperty cookies = binRes.getCookies();
for (JMeterProperty jMeterProperty : cookies) {
Cookie cookie = (Cookie) jMeterProperty.getObjectValue();
cookieManager.add(cookie);
}
}
res.addSubResult(binRes.getResult());
setParentSampleSuccess(res, res.isSuccessful() && (binRes.getResult() != null ? binRes.getResult().isSuccessful() : true));
}
} catch (InterruptedException ie) {
// $NON-NLS-1$
log.warn("Interrupted fetching embedded resources", ie);
Thread.currentThread().interrupt();
} catch (ExecutionException ee) {
// $NON-NLS-1$
log.warn("Execution issue when fetching embedded resources", ee);
}
}
}
return res;
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class LogFilter method excPattern.
/**
* The method assumes by default the text is not excluded. If the text
* matches the pattern, it will then return true.
*
* @param text text to be checked
* @return <code>true</code> if text is excluded
*/
protected boolean excPattern(String text) {
this.USEFILE = true;
boolean exc = false;
for (Pattern excludePattern : this.EXCPATTERNS) {
if (JMeterUtils.getMatcher().contains(text, excludePattern)) {
exc = true;
this.USEFILE = false;
break;
}
}
return exc;
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class ReplaceFunctionsWithStrings method transformValue.
@Override
public JMeterProperty transformValue(JMeterProperty prop) throws InvalidVariableException {
PatternMatcher pm = JMeterUtils.getMatcher();
PatternCompiler compiler = new Perl5Compiler();
String input = prop.getStringValue();
if (input == null) {
return prop;
}
for (Map.Entry<String, String> entry : getVariables().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (regexMatch) {
try {
Pattern pattern = compiler.compile(constructPattern(value));
input = Util.substitute(pm, pattern, new StringSubstitution(FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX), input, Util.SUBSTITUTE_ALL);
} catch (MalformedPatternException e) {
log.warn("Malformed pattern: {}", value);
}
} else {
input = StringUtilities.substitute(input, value, FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX);
}
}
return new StringProperty(prop.getName(), input);
}
Aggregations