1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.es.config.exentity;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.regex.Pattern;
25
26 import org.apache.http.auth.UsernamePasswordCredentials;
27 import org.codelibs.core.lang.StringUtil;
28 import org.codelibs.fess.Constants;
29 import org.codelibs.fess.app.service.RequestHeaderService;
30 import org.codelibs.fess.app.service.WebAuthenticationService;
31 import org.codelibs.fess.crawler.client.CrawlerClientFactory;
32 import org.codelibs.fess.crawler.client.http.Authentication;
33 import org.codelibs.fess.crawler.client.http.HcHttpClient;
34 import org.codelibs.fess.es.config.bsentity.BsWebConfig;
35 import org.codelibs.fess.helper.SystemHelper;
36 import org.codelibs.fess.mylasta.direction.FessConfig;
37 import org.codelibs.fess.util.ComponentUtil;
38 import org.codelibs.fess.util.ParameterUtil;
39
40
41
42
43 public class WebConfig extends BsWebConfig implements CrawlingConfig {
44
45 private static final long serialVersionUID = 1L;
46
47 protected volatile Pattern[] includedDocUrlPatterns;
48
49 protected volatile Pattern[] excludedDocUrlPatterns;
50
51 protected transient volatile Map<ConfigName, Map<String, String>> configParameterMap;
52
53 public WebConfig() {
54 super();
55 setBoost(1.0f);
56 }
57
58 @Override
59 public String getDocumentBoost() {
60 return getBoost().toString();
61 }
62
63 @Override
64 public String getIndexingTarget(final String input) {
65 if (includedDocUrlPatterns == null || excludedDocUrlPatterns == null) {
66 initDocUrlPattern();
67 }
68
69 if (includedDocUrlPatterns.length == 0 && excludedDocUrlPatterns.length == 0) {
70 return Constants.TRUE;
71 }
72
73 for (final Pattern pattern : includedDocUrlPatterns) {
74 if (pattern.matcher(input).matches()) {
75 return Constants.TRUE;
76 }
77 }
78
79 for (final Pattern pattern : excludedDocUrlPatterns) {
80 if (pattern.matcher(input).matches()) {
81 return Constants.FALSE;
82 }
83 }
84
85 return Constants.TRUE;
86 }
87
88 protected synchronized void initDocUrlPattern() {
89
90 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
91 if (includedDocUrlPatterns == null) {
92 if (StringUtil.isNotBlank(getIncludedDocUrls())) {
93 final List<Pattern> urlPatterList = new ArrayList<>();
94 final String[] urls = getIncludedDocUrls().split("[\r\n]");
95 for (final String u : urls) {
96 final String v = systemHelper.normalizeConfigPath(u);
97 if (StringUtil.isNotBlank(v)) {
98 urlPatterList.add(Pattern.compile(v));
99 }
100 }
101 includedDocUrlPatterns = urlPatterList.toArray(new Pattern[urlPatterList.size()]);
102 } else {
103 includedDocUrlPatterns = new Pattern[0];
104 }
105 }
106
107 if (excludedDocUrlPatterns == null) {
108 if (StringUtil.isNotBlank(getExcludedDocUrls())) {
109 final List<Pattern> urlPatterList = new ArrayList<>();
110 final String[] urls = getExcludedDocUrls().split("[\r\n]");
111 for (final String u : urls) {
112 final String v = systemHelper.normalizeConfigPath(u);
113 if (StringUtil.isNotBlank(v)) {
114 urlPatterList.add(Pattern.compile(v));
115 }
116 }
117 excludedDocUrlPatterns = urlPatterList.toArray(new Pattern[urlPatterList.size()]);
118 } else if (includedDocUrlPatterns.length > 0) {
119 excludedDocUrlPatterns = new Pattern[] { Pattern.compile(".*") };
120 } else {
121 excludedDocUrlPatterns = new Pattern[0];
122 }
123 }
124 }
125
126 public String getBoostValue() {
127 if (boost != null) {
128 return boost.toString();
129 }
130 return null;
131 }
132
133 public void setBoostValue(final String value) {
134 if (value != null) {
135 try {
136 boost = Float.parseFloat(value);
137 } catch (final Exception e) {}
138 }
139 }
140
141 @Override
142 public String getConfigId() {
143 return ConfigType.WEB.getConfigId(getId());
144 }
145
146 @Override
147 public Map<String, Object> initializeClientFactory(final CrawlerClientFactory clientFactory) {
148 final WebAuthenticationService webAuthenticationService = ComponentUtil.getComponent(WebAuthenticationService.class);
149 final RequestHeaderService requestHeaderService = ComponentUtil.getComponent(RequestHeaderService.class);
150 final FessConfig fessConfig = ComponentUtil.getFessConfig();
151
152
153 final Map<String, Object> paramMap = new HashMap<>();
154 clientFactory.setInitParameterMap(paramMap);
155
156 final Map<String, String> clientConfigMap = getConfigParameterMap(ConfigName.CLIENT);
157 if (clientConfigMap != null) {
158 paramMap.putAll(clientConfigMap);
159 }
160
161
162 if (paramMap.get(HcHttpClient.ROBOTS_TXT_ENABLED_PROPERTY) == null) {
163 paramMap.put(HcHttpClient.ROBOTS_TXT_ENABLED_PROPERTY, !fessConfig.isCrawlerIgnoreRobotsTxt());
164 }
165
166 final String userAgent = getUserAgent();
167 if (StringUtil.isNotBlank(userAgent)) {
168 paramMap.put(HcHttpClient.USER_AGENT_PROPERTY, userAgent);
169 }
170
171 final List<WebAuthentication> webAuthList = webAuthenticationService.getWebAuthenticationList(getId());
172 final List<Authentication> basicAuthList = new ArrayList<>();
173 for (final WebAuthentication webAuth : webAuthList) {
174 basicAuthList.add(webAuth.getAuthentication());
175 }
176 paramMap.put(HcHttpClient.BASIC_AUTHENTICATIONS_PROPERTY, basicAuthList.toArray(new Authentication[basicAuthList.size()]));
177
178
179 final List<RequestHeader> requestHeaderList = requestHeaderService.getRequestHeaderList(getId());
180 final List<org.codelibs.fess.crawler.client.http.RequestHeader> rhList = new ArrayList<>();
181 for (final RequestHeader requestHeader : requestHeaderList) {
182 rhList.add(requestHeader.getCrawlerRequestHeader());
183 }
184 paramMap.put(HcHttpClient.REQUERT_HEADERS_PROPERTY,
185 rhList.toArray(new org.codelibs.fess.crawler.client.http.RequestHeader[rhList.size()]));
186
187 final String proxyHost = (String) paramMap.get("proxyHost");
188 final String proxyPort = (String) paramMap.get("proxyPort");
189 if (StringUtil.isNotBlank(proxyHost) && StringUtil.isNotBlank(proxyPort)) {
190
191 if (paramMap.get("proxyUsername") != null && paramMap.get("proxyPassword") != null) {
192 paramMap.put(HcHttpClient.PROXY_CREDENTIALS_PROPERTY, new UsernamePasswordCredentials(paramMap.remove("proxyUsername")
193 .toString(), paramMap.remove("proxyPassword").toString()));
194 }
195 } else {
196 initializeDefaultHttpProxy(paramMap);
197 }
198
199 return paramMap;
200 }
201
202 @Override
203 public Map<String, String> getConfigParameterMap(final ConfigName name) {
204 if (configParameterMap == null) {
205 configParameterMap = ParameterUtil.createConfigParameterMap(getConfigParameter());
206 }
207
208 final Map<String, String> configMap = configParameterMap.get(name);
209 if (configMap == null) {
210 return Collections.emptyMap();
211 }
212 return configMap;
213 }
214
215 @Override
216 public String getId() {
217 return asDocMeta().id();
218 }
219
220 public void setId(final String id) {
221 asDocMeta().id(id);
222 }
223
224 public Long getVersionNo() {
225 return asDocMeta().version();
226 }
227
228 public void setVersionNo(final Long version) {
229 asDocMeta().version(version);
230 }
231
232 @Override
233 public String toString() {
234 return "WebConfig [available=" + available + ", boost=" + boost + ", configParameter=" + configParameter + ", createdBy="
235 + createdBy + ", createdTime=" + createdTime + ", depth=" + depth + ", excludedDocUrls=" + excludedDocUrls
236 + ", excludedUrls=" + excludedUrls + ", includedDocUrls=" + includedDocUrls + ", includedUrls=" + includedUrls
237 + ", intervalTime=" + intervalTime + ", timeToLive=" + timeToLive + ", maxAccessCount=" + maxAccessCount + ", name=" + name
238 + ", numOfThread=" + numOfThread + ", permissions=" + Arrays.toString(permissions) + ", sortOrder=" + sortOrder
239 + ", updatedBy=" + updatedBy + ", updatedTime=" + updatedTime + ", urls=" + urls + ", userAgent=" + userAgent + "]";
240 }
241
242 }