Search in sources :

Example 21 with Perl5Matcher

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;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) StringWrapper(org.apache.ofbiz.base.util.StringUtil.StringWrapper) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) CategoryContentWrapper(org.apache.ofbiz.product.category.CategoryContentWrapper)

Example 22 with Perl5Matcher

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

Example 23 with Perl5Matcher

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

Example 24 with Perl5Matcher

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

Example 25 with Perl5Matcher

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);
}
Also used : ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) SampleResult(org.apache.jmeter.samplers.SampleResult) MatchResult(org.apache.oro.text.regex.MatchResult)

Aggregations

Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)72 Pattern (org.apache.oro.text.regex.Pattern)50 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)23 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)22 MatchResult (org.apache.oro.text.regex.MatchResult)21 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)17 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)14 ArrayList (java.util.ArrayList)12 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)8 IOException (java.io.IOException)6 MalformedURLException (java.net.MalformedURLException)6 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)5 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SampleResult (org.apache.jmeter.samplers.SampleResult)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 PatternCacheLRU (org.apache.oro.text.PatternCacheLRU)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2 File (java.io.File)2