|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. The ASF licenses this file to You |
| 4 | + * under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. For additional information regarding |
| 15 | + * copyright in this work, please see the NOTICE file in the top level |
| 16 | + * directory of this distribution. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.roller.util; |
| 20 | + |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.Callable; |
| 25 | +import java.util.concurrent.Executors; |
| 26 | +import java.util.regex.Matcher; |
| 27 | +import java.util.regex.Pattern; |
| 28 | +import org.apache.commons.codec.binary.Hex; |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * Regular expressions utility class. |
| 33 | + */ |
| 34 | +public final class RegexUtil { |
| 35 | + |
| 36 | + public static final Pattern MAILTO_PATTERN = |
| 37 | + Pattern.compile("mailto:([a-zA-Z0-9\\.\\-]+@[a-zA-Z0-9\\.\\-]+\\.[a-zA-Z0-9]+)"); |
| 38 | + |
| 39 | + public static final Pattern EMAIL_PATTERN = |
| 40 | + Pattern.compile("\\b[a-zA-Z0-9\\.\\-]+(@)([a-zA-Z0-9\\.\\-]+)(\\.)([a-zA-Z0-9]+)\\b"); |
| 41 | + |
| 42 | + |
| 43 | + public static String encodeEmail(String str) { |
| 44 | + // obfuscate mailto's: turns them into hex encoded, |
| 45 | + // so that browsers can still understand the mailto link |
| 46 | + Matcher mailtoMatch = MAILTO_PATTERN.matcher(str); |
| 47 | + while (mailtoMatch.find()) { |
| 48 | + String email = mailtoMatch.group(1); |
| 49 | + //System.out.println("email=" + email); |
| 50 | + String hexed = encode(email); |
| 51 | + str = str.replaceFirst("mailto:"+email, "mailto:"+hexed); |
| 52 | + } |
| 53 | + |
| 54 | + return obfuscateEmail(str); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * obfuscate plaintext emails: makes them |
| 60 | + * "human-readable" - still too easy for |
| 61 | + * machines to parse however. |
| 62 | + */ |
| 63 | + public static String obfuscateEmail(String str) { |
| 64 | + Matcher emailMatch = EMAIL_PATTERN.matcher(str); |
| 65 | + while (executeWithTimeout(() -> emailMatch.find(), 5000)) { |
| 66 | + String at = emailMatch.group(1); |
| 67 | + //System.out.println("at=" + at); |
| 68 | + str = str.replaceFirst(at, "-AT-"); |
| 69 | + |
| 70 | + String dot = emailMatch.group(2) + emailMatch.group(3) + emailMatch.group(4); |
| 71 | + String newDot = emailMatch.group(2) + "-DOT-" + emailMatch.group(4); |
| 72 | + //System.out.println("dot=" + dot); |
| 73 | + str = str.replaceFirst(dot, newDot); |
| 74 | + } |
| 75 | + return str; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + /** |
| 80 | + * Return the specified match "groups" from the pattern. |
| 81 | + * For each group matched a String will be entered in the ArrayList. |
| 82 | + * |
| 83 | + * @param pattern The Pattern to use. |
| 84 | + * @param match The String to match against. |
| 85 | + * @param group The group number to return in case of a match. |
| 86 | + * @return List of matched groups from the pattern. |
| 87 | + */ |
| 88 | + public static List<String> getMatches(Pattern pattern, String match, int group) { |
| 89 | + List<String> matches = new ArrayList<>(); |
| 90 | + Matcher matcher = pattern.matcher(match); |
| 91 | + while (matcher.find()) { |
| 92 | + matches.add( matcher.group(group) ); |
| 93 | + } |
| 94 | + return matches; |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + /** |
| 99 | + * Thanks to the folks at Blojsom (http://sf.net/projects/blojsom) |
| 100 | + * for showing me what I was doing wrong with the Hex class. |
| 101 | + * |
| 102 | + * @param email |
| 103 | + * @return |
| 104 | + */ |
| 105 | + public static String encode(String email) { |
| 106 | + StringBuilder result = new StringBuilder(16); |
| 107 | + char[] hexString = Hex.encodeHex(email.getBytes(StandardCharsets.UTF_8)); |
| 108 | + for (int i = 0; i < hexString.length; i++) { |
| 109 | + if (i % 2 == 0) { |
| 110 | + result.append('%'); |
| 111 | + } |
| 112 | + result.append(hexString[i]); |
| 113 | + } |
| 114 | + |
| 115 | + return result.toString(); |
| 116 | + } |
| 117 | + |
| 118 | + public <E> E executeWithTimeout(final Callable<E> action, final int timeout) { |
| 119 | + Future<E> maybeResult = Executors.newSingleThreadExecutor().submit(action); |
| 120 | + try { |
| 121 | + return maybeResult.get(timeout, TimeUnit.MILLISECONDS); |
| 122 | + } catch (Exception e) { |
| 123 | + throw new RuntimeException("Failed to execute within time limit."); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments