Search in sources :

Example 56 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 57 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);
        }
        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 58 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 59 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project jmeter by apache.

the class SessionFilter method getIpAddress.

protected String getIpAddress(String logLine) {
    Pattern incIp = JMeterUtils.getPatternCache().getPattern("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    matcher.contains(logLine, incIp);
    return matcher.getMatch().group(0);
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher)

Example 60 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project jmeter by apache.

the class HttpMirrorThread method getRequestHeaderValue.

private static String getRequestHeaderValue(String requestHeaders, String headerName) {
    Perl5Matcher localMatcher = JMeterUtils.getMatcher();
    // We use multi-line mask so can prefix the line with ^
    // $NON-NLS-1$ $NON-NLS-2$
    String expression = "^" + headerName + ":\\s+([^\\r\\n]+)";
    Pattern pattern = JMeterUtils.getPattern(expression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.MULTILINE_MASK);
    if (localMatcher.contains(requestHeaders, pattern)) {
        // The value is in the first group, group 0 is the whole match
        return localMatcher.getMatch().group(1);
    } else {
        return null;
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher)

Aggregations

Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)102 Pattern (org.apache.oro.text.regex.Pattern)78 MatchResult (org.apache.oro.text.regex.MatchResult)39 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)38 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)37 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)32 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)22 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)20 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)8 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)7 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)7 MalformedURLException (java.net.MalformedURLException)5 PatternCacheLRU (org.apache.oro.text.PatternCacheLRU)5 PluginException (org.apache.wiki.api.exceptions.PluginException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 NoSuchElementException (java.util.NoSuchElementException)3 SampleResult (org.apache.jmeter.samplers.SampleResult)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2