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.function.Supplier;
25 import java.util.regex.Pattern;
26
27 import org.codelibs.core.lang.StringUtil;
28 import org.codelibs.fess.Constants;
29 import org.codelibs.fess.app.service.FileAuthenticationService;
30 import org.codelibs.fess.crawler.client.CrawlerClientFactory;
31 import org.codelibs.fess.crawler.client.ftp.FtpAuthentication;
32 import org.codelibs.fess.crawler.client.smb.SmbAuthentication;
33 import org.codelibs.fess.es.config.bsentity.BsFileConfig;
34 import org.codelibs.fess.helper.SystemHelper;
35 import org.codelibs.fess.util.ComponentUtil;
36 import org.codelibs.fess.util.ParameterUtil;
37
38
39
40
41 public class FileConfig extends BsFileConfig implements CrawlingConfig {
42
43 private static final long serialVersionUID = 1L;
44
45 protected volatile Pattern[] includedDocPathPatterns;
46
47 protected volatile Pattern[] excludedDocPathPatterns;
48
49 protected transient volatile Map<ConfigName, Map<String, String>> configParameterMap;
50
51 protected CrawlerClientFactory crawlerClientFactory = null;
52
53 public FileConfig() {
54 setBoost(1.0f);
55 }
56
57 @Override
58 public String getDocumentBoost() {
59 return getBoost().toString();
60 }
61
62 @Override
63 public String getIndexingTarget(final String input) {
64 if (includedDocPathPatterns == null || excludedDocPathPatterns == null) {
65 initDocPathPattern();
66 }
67
68 if (includedDocPathPatterns.length == 0 && excludedDocPathPatterns.length == 0) {
69 return Constants.TRUE;
70 }
71
72 for (final Pattern pattern : includedDocPathPatterns) {
73 if (pattern.matcher(input).matches()) {
74 return Constants.TRUE;
75 }
76 }
77
78 for (final Pattern pattern : excludedDocPathPatterns) {
79 if (pattern.matcher(input).matches()) {
80 return Constants.FALSE;
81 }
82 }
83
84 return Constants.TRUE;
85
86 }
87
88 protected synchronized void initDocPathPattern() {
89 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
90
91 if (includedDocPathPatterns == null) {
92 if (StringUtil.isNotBlank(getIncludedDocPaths())) {
93 final List<Pattern> pathPatterList = new ArrayList<>();
94 final String[] paths = getIncludedDocPaths().split("[\r\n]");
95 for (final String u : paths) {
96 final String v = systemHelper.normalizeConfigPath(u);
97 if (StringUtil.isNotBlank(v)) {
98 pathPatterList.add(Pattern.compile(systemHelper.encodeUrlFilter(v)));
99 }
100 }
101 includedDocPathPatterns = pathPatterList.toArray(new Pattern[pathPatterList.size()]);
102 } else {
103 includedDocPathPatterns = new Pattern[0];
104 }
105 }
106
107 if (excludedDocPathPatterns == null) {
108 if (StringUtil.isNotBlank(getExcludedDocPaths())) {
109 final List<Pattern> pathPatterList = new ArrayList<>();
110 final String[] paths = getExcludedDocPaths().split("[\r\n]");
111 for (final String u : paths) {
112 final String v = systemHelper.normalizeConfigPath(u);
113 if (StringUtil.isNotBlank(v)) {
114 pathPatterList.add(Pattern.compile(systemHelper.encodeUrlFilter(v)));
115 }
116 }
117 excludedDocPathPatterns = pathPatterList.toArray(new Pattern[pathPatterList.size()]);
118 } else if (includedDocPathPatterns.length > 0) {
119 excludedDocPathPatterns = new Pattern[] { Pattern.compile(".*") };
120 } else {
121 excludedDocPathPatterns = 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.FILE.getConfigId(getId());
144 }
145
146 @Override
147 public CrawlerClientFactory initializeClientFactory(final Supplier<CrawlerClientFactory> creator) {
148 if (crawlerClientFactory != null) {
149 return crawlerClientFactory;
150 }
151 final CrawlerClientFactory factory = creator.get();
152
153 final FileAuthenticationService fileAuthenticationService = ComponentUtil.getComponent(FileAuthenticationService.class);
154
155
156 final Map<String, Object> paramMap = new HashMap<>();
157 factory.setInitParameterMap(paramMap);
158
159 final Map<String, String> clientConfigMap = getConfigParameterMap(ConfigName.CLIENT);
160 if (clientConfigMap != null) {
161 paramMap.putAll(clientConfigMap);
162 }
163
164
165 final List<FileAuthentication> fileAuthList = fileAuthenticationService.getFileAuthenticationList(getId());
166 final List<SmbAuthentication> smbAuthList = new ArrayList<>();
167 final List<org.codelibs.fess.crawler.client.smb1.SmbAuthentication> smb1AuthList = new ArrayList<>();
168 final List<FtpAuthentication> ftpAuthList = new ArrayList<>();
169 for (final FileAuthentication fileAuth : fileAuthList) {
170 if (Constants.SAMBA.equals(fileAuth.getProtocolScheme())) {
171 final SmbAuthentication smbAuth = new SmbAuthentication();
172 final Map<String, String> map = ParameterUtil.parse(fileAuth.getParameters());
173 final String domain = map.get("domain");
174 smbAuth.setDomain(domain == null ? StringUtil.EMPTY : domain);
175 smbAuth.setServer(fileAuth.getHostname());
176 smbAuth.setPort(fileAuth.getPort() == null ? -1 : fileAuth.getPort());
177 smbAuth.setUsername(fileAuth.getUsername());
178 smbAuth.setPassword(fileAuth.getPassword());
179 smbAuthList.add(smbAuth);
180
181 final org.codelibs.fess.crawler.client.smb1.SmbAuthentication smb1Auth =
182 new org.codelibs.fess.crawler.client.smb1.SmbAuthentication();
183 smb1Auth.setDomain(domain == null ? StringUtil.EMPTY : domain);
184 smb1Auth.setServer(fileAuth.getHostname());
185 smb1Auth.setPort(fileAuth.getPort() == null ? -1 : fileAuth.getPort());
186 smb1Auth.setUsername(fileAuth.getUsername());
187 smb1Auth.setPassword(fileAuth.getPassword());
188 smb1AuthList.add(smb1Auth);
189 } else if (Constants.FTP.equals(fileAuth.getProtocolScheme())) {
190 final FtpAuthentication ftpAuth = new FtpAuthentication();
191 ftpAuth.setServer(fileAuth.getHostname());
192 ftpAuth.setPort(fileAuth.getPort() == null ? -1 : fileAuth.getPort());
193 ftpAuth.setUsername(fileAuth.getUsername());
194 ftpAuth.setPassword(fileAuth.getPassword());
195 ftpAuthList.add(ftpAuth);
196 }
197 }
198 paramMap.put(Param.Client.SMB_AUTHENTICATIONS, smbAuthList.toArray(new SmbAuthentication[smbAuthList.size()]));
199 paramMap.put(Param.Client.SMB1_AUTHENTICATIONS,
200 smb1AuthList.toArray(new org.codelibs.fess.crawler.client.smb1.SmbAuthentication[smb1AuthList.size()]));
201 paramMap.put(Param.Client.FTP_AUTHENTICATIONS, ftpAuthList.toArray(new FtpAuthentication[ftpAuthList.size()]));
202
203 crawlerClientFactory = factory;
204 return factory;
205 }
206
207 @Override
208 public Map<String, String> getConfigParameterMap(final ConfigName name) {
209 if (configParameterMap == null) {
210 configParameterMap = ParameterUtil.createConfigParameterMap(getConfigParameter());
211 }
212
213 final Map<String, String> configMap = configParameterMap.get(name);
214 if (configMap == null) {
215 return Collections.emptyMap();
216 }
217 return configMap;
218 }
219
220 @Override
221 public String getId() {
222 return asDocMeta().id();
223 }
224
225 public void setId(final String id) {
226 asDocMeta().id(id);
227 }
228
229 public Long getVersionNo() {
230 return asDocMeta().version();
231 }
232
233 public void setVersionNo(final Long version) {
234 asDocMeta().version(version);
235 }
236
237 @Override
238 public String toString() {
239 return "FileConfig [available=" + available + ", boost=" + boost + ", configParameter=" + configParameter + ", createdBy="
240 + createdBy + ", createdTime=" + createdTime + ", depth=" + depth + ", excludedDocPaths=" + excludedDocPaths
241 + ", excludedPaths=" + excludedPaths + ", includedDocPaths=" + includedDocPaths + ", includedPaths=" + includedPaths
242 + ", intervalTime=" + intervalTime + ", timeToLive=" + timeToLive + ", maxAccessCount=" + maxAccessCount + ", name=" + name
243 + ", numOfThread=" + numOfThread + ", paths=" + paths + ", permissions=" + Arrays.toString(permissions) + ", sortOrder="
244 + sortOrder + ", updatedBy=" + updatedBy + ", updatedTime=" + updatedTime + "]";
245 }
246 }