View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not 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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.es.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.es.config.bsentity.BsPathMapping;
26  import org.codelibs.fess.helper.PathMappingHelper;
27  
28  /**
29   * @author FreeGen
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> pathMapper;
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 (pathMapper == null) {
66                  final String replacement = StringUtil.isNotBlank(getReplacement()) ? getReplacement() : StringUtil.EMPTY;
67                  pathMapper = pathMappingHelper.createPathMatcher(matcher, replacement);
68              }
69              try {
70                  return pathMapper.apply(input, matcher);
71              } catch (final Exception e) {
72                  logger.warn("Failed to apply {}", pathMapper, 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  }