1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.opensearch.config.exentity;
17
18 import java.util.function.BiFunction;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.codelibs.core.lang.StringUtil;
25 import org.codelibs.fess.helper.PathMappingHelper;
26 import org.codelibs.fess.opensearch.config.bsentity.BsPathMapping;
27
28
29
30
31 public class PathMapping extends BsPathMapping {
32
33 private static final Logger logger = LogManager.getLogger(PathMapping.class);
34
35 private static final long serialVersionUID = 1L;
36
37 protected Pattern userAgentPattern;
38
39 protected Pattern regexPattern;
40
41 protected BiFunction<String, Matcher, String> pathMapperFunc;
42
43 public String getId() {
44 return asDocMeta().id();
45 }
46
47 public void setId(final String id) {
48 asDocMeta().id(id);
49 }
50
51 public Long getVersionNo() {
52 return asDocMeta().version();
53 }
54
55 public void setVersionNo(final Long version) {
56 asDocMeta().version(version);
57 }
58
59 public String process(final PathMappingHelper pathMappingHelper, final String input) {
60 if (regexPattern == null) {
61 regexPattern = Pattern.compile(getRegex());
62 }
63 final Matcher matcher = regexPattern.matcher(input);
64 if (matcher.find()) {
65 if (pathMapperFunc == null) {
66 final String replacement = StringUtil.isNotBlank(getReplacement()) ? getReplacement() : StringUtil.EMPTY;
67 pathMapperFunc = pathMappingHelper.createPathMatcher(matcher, replacement);
68 }
69 try {
70 return pathMapperFunc.apply(input, matcher);
71 } catch (final Exception e) {
72 logger.warn("Failed to apply {} to {}.", regexPattern.pattern(), input, e);
73 }
74 }
75 return input;
76 }
77
78 public boolean hasUAMathcer() {
79 return StringUtil.isNotBlank(getUserAgent());
80 }
81
82 public Matcher getUAMatcher(final CharSequence input) {
83 if (!hasUAMathcer()) {
84 return null;
85 }
86
87 if (userAgentPattern == null) {
88 userAgentPattern = Pattern.compile(getUserAgent());
89 }
90 return userAgentPattern.matcher(input);
91 }
92
93 @Override
94 public String toString() {
95 return "PathMapping [regexPattern=" + regexPattern + ", createdBy=" + createdBy + ", createdTime=" + createdTime + ", processType="
96 + processType + ", regex=" + regex + ", replacement=" + replacement + ", sortOrder=" + sortOrder + ", userAgent="
97 + userAgent + ", updatedBy=" + updatedBy + ", updatedTime=" + updatedTime + ", docMeta=" + docMeta + "]";
98 }
99 }