1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.helper;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.function.BiFunction;
23 import java.util.regex.Matcher;
24
25 import javax.annotation.PostConstruct;
26
27 import org.codelibs.core.lang.StringUtil;
28 import org.codelibs.fess.Constants;
29 import org.codelibs.fess.es.config.exbhv.PathMappingBhv;
30 import org.codelibs.fess.es.config.exentity.PathMapping;
31 import org.codelibs.fess.util.ComponentUtil;
32 import org.codelibs.fess.util.DocumentUtil;
33 import org.codelibs.fess.util.GroovyUtil;
34 import org.lastaflute.di.core.exception.ComponentNotFoundException;
35 import org.lastaflute.di.core.factory.SingletonLaContainerFactory;
36 import org.lastaflute.web.util.LaRequestUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class PathMappingHelper {
41
42 private static final Logger logger = LoggerFactory.getLogger(PathMappingHelper.class);
43
44 protected static final String FUNCTION_ENCODEURL_MATCHER = "function:encodeUrl";
45
46 protected static final String GROOVY_MATCHER = "groovy:";
47
48 protected final Map<String, List<PathMapping>> pathMappingMap = new HashMap<>();
49
50 protected volatile List<PathMapping> cachedPathMappingList = null;
51
52 @PostConstruct
53 public void init() {
54 update();
55 }
56
57 public int update() {
58 final List<String> ptList = new ArrayList<>();
59 ptList.add(Constants.PROCESS_TYPE_DISPLAYING);
60 ptList.add(Constants.PROCESS_TYPE_BOTH);
61
62 try {
63 final PathMappingBhv pathMappingBhv = ComponentUtil.getComponent(PathMappingBhv.class);
64 cachedPathMappingList = pathMappingBhv.selectList(cb -> {
65 cb.query().addOrderBy_SortOrder_Asc();
66 cb.query().setProcessType_InScope(ptList);
67 cb.fetchFirst(ComponentUtil.getFessConfig().getPagePathMappingMaxFetchSizeAsInteger());
68 });
69 return cachedPathMappingList.size();
70 } catch (final ComponentNotFoundException e) {
71 if (logger.isDebugEnabled()) {
72 logger.debug("Failed to load path mappings.", e);
73 }
74 cachedPathMappingList = new ArrayList<>();
75 } catch (final Exception e) {
76 logger.warn("Failed to load path mappings.", e);
77 }
78 return 0;
79 }
80
81 public void setPathMappingList(final String sessionId, final List<PathMapping> pathMappingList) {
82 if (sessionId != null) {
83 if (pathMappingList != null) {
84 pathMappingMap.put(sessionId, pathMappingList);
85 } else {
86 removePathMappingList(sessionId);
87 }
88 }
89 }
90
91 public void removePathMappingList(final String sessionId) {
92 pathMappingMap.remove(sessionId);
93 }
94
95 public List<PathMapping> getPathMappingList(final String sessionId) {
96 if (sessionId == null) {
97 return null;
98 }
99 return pathMappingMap.get(sessionId);
100 }
101
102 public String replaceUrl(final String sessionId, final String url) {
103 final List<PathMapping> pathMappingList = getPathMappingList(sessionId);
104 if (pathMappingList == null) {
105 return url;
106 }
107 return replaceUrl(pathMappingList, url);
108 }
109
110 public String replaceUrls(final String text) {
111 if (cachedPathMappingList == null) {
112 synchronized (this) {
113 if (cachedPathMappingList == null) {
114 init();
115 }
116 }
117 }
118 String result = text;
119 for (final PathMapping pathMapping : cachedPathMappingList) {
120 if (matchUserAgent(pathMapping)) {
121 String replacement = pathMapping.getReplacement();
122 if (replacement == null) {
123 replacement = StringUtil.EMPTY;
124 }
125 result = result.replaceAll("(\"[^\"]*)" + pathMapping.getRegex() + "([^\"]*\")", "$1" + replacement + "$2");
126 }
127 }
128 return result;
129 }
130
131 public String replaceUrl(final String url) {
132 if (cachedPathMappingList == null) {
133 synchronized (this) {
134 if (cachedPathMappingList == null) {
135 init();
136 }
137 }
138 }
139 return replaceUrl(cachedPathMappingList, url);
140 }
141
142 public BiFunction<String, Matcher, String> createPathMatcher(final Matcher matcher, final String replacement) {
143 if (replacement.equals(FUNCTION_ENCODEURL_MATCHER)) {
144 return (u, m) -> DocumentUtil.encodeUrl(u);
145 } else if (replacement.startsWith(GROOVY_MATCHER)) {
146 final String template = replacement.substring(GROOVY_MATCHER.length());
147 return (u, m) -> {
148 final Map<String, Object> paramMap = new HashMap<>();
149 paramMap.put("url", u);
150 paramMap.put("matcher", m);
151 final Object value = GroovyUtil.evaluate(template, paramMap);
152 if (value == null) {
153 return u;
154 }
155 return value.toString();
156 };
157 } else {
158 return (u, m) -> m.replaceAll(replacement);
159 }
160 }
161
162 private String replaceUrl(final List<PathMapping> pathMappingList, final String url) {
163 String newUrl = url;
164 for (final PathMapping pathMapping : pathMappingList) {
165 if (matchUserAgent(pathMapping)) {
166 newUrl = pathMapping.process(this, newUrl);
167 }
168 }
169 return newUrl;
170 }
171
172 private boolean matchUserAgent(final PathMapping pathMapping) {
173 if (!pathMapping.hasUAMathcer()) {
174 return true;
175 }
176
177 if (SingletonLaContainerFactory.getExternalContext().getRequest() != null) {
178 return LaRequestUtil.getOptionalRequest().map(request -> {
179 final String userAgent = request.getHeader("user-agent");
180 if (StringUtil.isBlank(userAgent)) {
181 return false;
182 }
183
184 return pathMapping.getUAMatcher(userAgent).find();
185 }).orElse(false);
186 }
187 return false;
188 }
189 }