use of org.apache.oro.text.regex.Perl5Matcher in project ofbiz-framework by apache.
the class CatalogUrlSeoTransform method initCategoryMap.
public static synchronized void initCategoryMap(HttpServletRequest request, Delegator delegator) {
if (SeoConfigUtil.checkCategoryUrl()) {
categoryNameIdMap = new Hashtable<>();
categoryIdNameMap = new Hashtable<>();
Perl5Matcher matcher = new Perl5Matcher();
try {
Collection<GenericValue> allCategories = delegator.findList("ProductCategory", null, UtilMisc.toSet("productCategoryId", "categoryName"), null, null, false);
for (GenericValue category : allCategories) {
String categoryName = category.getString("categoryName");
String categoryNameId = null;
String categoryIdName = null;
String categoryId = category.getString("productCategoryId");
if (UtilValidate.isNotEmpty(categoryName)) {
categoryName = SeoUrlUtil.replaceSpecialCharsUrl(categoryName.trim());
if (matcher.matches(categoryName, asciiPattern)) {
categoryIdName = categoryName.replaceAll(" ", URL_HYPHEN);
categoryNameId = categoryIdName + URL_HYPHEN + categoryId.trim().replaceAll(" ", URL_HYPHEN);
} else {
categoryIdName = categoryId.trim().replaceAll(" ", URL_HYPHEN);
categoryNameId = categoryIdName;
}
} else {
GenericValue productCategory = EntityQuery.use(delegator).from("ProductCategory").where("productCategoryId", categoryId).cache().queryOne();
CategoryContentWrapper wrapper = new CategoryContentWrapper(productCategory, request);
StringWrapper alternativeUrl = wrapper.get("ALTERNATIVE_URL", "url");
if (UtilValidate.isNotEmpty(alternativeUrl) && UtilValidate.isNotEmpty(alternativeUrl.toString())) {
categoryIdName = SeoUrlUtil.replaceSpecialCharsUrl(alternativeUrl.toString());
categoryNameId = categoryIdName + URL_HYPHEN + categoryId.trim().replaceAll(" ", URL_HYPHEN);
} else {
categoryNameId = categoryId.trim().replaceAll(" ", URL_HYPHEN);
categoryIdName = categoryNameId;
}
}
if (categoryNameIdMap.containsKey(categoryNameId)) {
categoryNameId = categoryId.trim().replaceAll(" ", URL_HYPHEN);
categoryIdName = categoryNameId;
}
if (!matcher.matches(categoryNameId, asciiPattern) || categoryNameIdMap.containsKey(categoryNameId)) {
continue;
}
categoryNameIdMap.put(categoryNameId, categoryId);
categoryIdNameMap.put(categoryId, categoryIdName);
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
}
categoryMapInitialed = true;
}
use of org.apache.oro.text.regex.Perl5Matcher in project ofbiz-framework by apache.
the class UrlRegexpTransform method seoUrl.
/**
* Transform a url according to seo pattern regular expressions.
*
* @param url
* , String to do the seo transform
* @param isAnon
* , boolean to indicate whether it's an anonymous visit.
*
* @return String, the transformed url.
*/
public static String seoUrl(String url, boolean isAnon) {
Perl5Matcher matcher = new Perl5Matcher();
if (SeoConfigUtil.checkUseUrlRegexp() && matcher.matches(url, SeoConfigUtil.getGeneralRegexpPattern())) {
Iterator<String> keys = SeoConfigUtil.getSeoPatterns().keySet().iterator();
boolean foundMatch = false;
while (keys.hasNext()) {
String key = keys.next();
Pattern pattern = SeoConfigUtil.getSeoPatterns().get(key);
if (pattern.getPattern().contains(";jsessionid=")) {
if (isAnon) {
if (SeoConfigUtil.isJSessionIdAnonEnabled()) {
continue;
}
} else {
if (SeoConfigUtil.isJSessionIdUserEnabled()) {
continue;
}
boolean foundException = false;
for (int i = 0; i < SeoConfigUtil.getUserExceptionPatterns().size(); i++) {
if (matcher.matches(url, SeoConfigUtil.getUserExceptionPatterns().get(i))) {
foundException = true;
break;
}
}
if (foundException) {
continue;
}
}
}
String replacement = SeoConfigUtil.getSeoReplacements().get(key);
if (matcher.matches(url, pattern)) {
for (int i = 1; i < matcher.getMatch().groups(); i++) {
replacement = replacement.replaceAll("\\$" + i, matcher.getMatch().group(i));
}
// break if found any matcher
url = replacement;
foundMatch = true;
break;
}
}
if (!foundMatch) {
if (Debug.verboseOn()) {
Debug.logVerbose("Can NOT find a seo transform pattern for this url: " + url, module);
}
}
}
return url;
}
use of org.apache.oro.text.regex.Perl5Matcher in project ofbiz-framework by apache.
the class UrlRegexpTransform method forwardUri.
/**
* Forward a uri according to forward pattern regular expressions. Note: this is developed for Filter usage.
*
* @param uri
* String to reverse transform
* @return String
*/
public static boolean forwardUri(HttpServletResponse response, String uri) {
Perl5Matcher matcher = new Perl5Matcher();
boolean foundMatch = false;
Integer responseCodeInt = null;
if (SeoConfigUtil.checkUseUrlRegexp() && SeoConfigUtil.getSeoPatterns() != null && SeoConfigUtil.getForwardReplacements() != null) {
Iterator<String> keys = SeoConfigUtil.getSeoPatterns().keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
Pattern pattern = SeoConfigUtil.getSeoPatterns().get(key);
String replacement = SeoConfigUtil.getForwardReplacements().get(key);
if (matcher.matches(uri, pattern)) {
for (int i = 1; i < matcher.getMatch().groups(); i++) {
replacement = replacement.replaceAll("\\$" + i, matcher.getMatch().group(i));
}
// break if found any matcher
uri = replacement;
responseCodeInt = SeoConfigUtil.getForwardResponseCodes().get(key);
foundMatch = true;
break;
}
}
}
if (foundMatch) {
if (responseCodeInt == null) {
response.setStatus(SeoConfigUtil.getDefaultResponseCode());
} else {
response.setStatus(responseCodeInt.intValue());
}
response.setHeader("Location", uri);
} else {
Debug.logInfo("Can NOT forward this url: " + uri, module);
}
return foundMatch;
}
use of org.apache.oro.text.regex.Perl5Matcher in project ofbiz-framework by apache.
the class SeoContextFilter method forwardUri.
/**
* Forward a uri according to forward pattern regular expressions. Note: this is developed for Filter usage.
*
* @param uri String to reverse transform
* @return String
*/
protected static boolean forwardUri(HttpServletResponse response, String uri) {
Perl5Matcher matcher = new Perl5Matcher();
boolean foundMatch = false;
Integer responseCodeInt = null;
if (SeoConfigUtil.checkUseUrlRegexp() && SeoConfigUtil.getSeoPatterns() != null && SeoConfigUtil.getForwardReplacements() != null) {
Iterator<String> keys = SeoConfigUtil.getSeoPatterns().keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
Pattern pattern = SeoConfigUtil.getSeoPatterns().get(key);
String replacement = SeoConfigUtil.getForwardReplacements().get(key);
if (matcher.matches(uri, pattern)) {
for (int i = matcher.getMatch().groups(); i > 0; i--) {
replacement = replacement.replaceAll("\\$" + i, matcher.getMatch().group(i));
}
uri = replacement;
responseCodeInt = SeoConfigUtil.getForwardResponseCodes().get(key);
foundMatch = true;
// be careful, we don't break after finding a match
}
}
}
if (foundMatch) {
if (responseCodeInt == null) {
response.setStatus(SeoConfigUtil.getDefaultResponseCode());
} else {
response.setStatus(responseCodeInt.intValue());
}
response.setHeader("Location", uri);
} else {
Debug.logInfo("Can NOT forward this url: " + uri, module);
}
return foundMatch;
}
use of org.apache.oro.text.regex.Perl5Matcher in project jmeter by apache.
the class RegexExtractor method processMatches.
private List<MatchResult> processMatches(Pattern pattern, String regex, SampleResult result, int matchNumber, JMeterVariables vars) {
log.debug("Regex = '{}'", regex);
Perl5Matcher matcher = JMeterUtils.getMatcher();
List<MatchResult> matches = new ArrayList<>();
int found = 0;
if (isScopeVariable()) {
String inputString = vars.get(getVariableName());
if (inputString == null) {
if (log.isWarnEnabled()) {
log.warn("No variable '{}' found to process by RegexExtractor '{}', skipping processing", getVariableName(), getName());
}
return Collections.emptyList();
}
matchStrings(matchNumber, matcher, pattern, matches, found, inputString);
} else {
List<SampleResult> sampleList = getSampleList(result);
for (SampleResult sr : sampleList) {
String inputString = getInputString(sr);
found = matchStrings(matchNumber, matcher, pattern, matches, found, inputString);
if (matchNumber > 0 && found == matchNumber) {
// no need to process further
break;
}
}
}
return Collections.unmodifiableList(matches);
}
Aggregations