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.codelibs.core.lang.StringUtil;
27 import org.codelibs.fess.Constants;
28 import org.codelibs.fess.app.service.FileAuthenticationService;
29 import org.codelibs.fess.crawler.client.CrawlerClientFactory;
30 import org.codelibs.fess.crawler.client.ftp.FtpAuthentication;
31 import org.codelibs.fess.crawler.client.ftp.FtpClient;
32 import org.codelibs.fess.crawler.client.smb.SmbAuthentication;
33 import org.codelibs.fess.crawler.client.smb.SmbClient;
34 import org.codelibs.fess.es.config.bsentity.BsFileConfig;
35 import org.codelibs.fess.helper.SystemHelper;
36 import org.codelibs.fess.util.ComponentUtil;
37 import org.codelibs.fess.util.ParameterUtil;
38
39
40
41
42 public class FileConfig extends BsFileConfig implements CrawlingConfig {
43
44 private static final long serialVersionUID = 1L;
45
46 protected volatile Pattern[] includedDocPathPatterns;
47
48 protected volatile Pattern[] excludedDocPathPatterns;
49
50 protected transient volatile Map<ConfigName, Map<String, String>> configParameterMap;
51
52 public FileConfig() {
53 super();
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 Map<String, Object> initializeClientFactory(final CrawlerClientFactory clientFactory) {
148 final FileAuthenticationService fileAuthenticationService = ComponentUtil.getComponent(FileAuthenticationService.class);
149
150
151 final Map<String, Object> paramMap = new HashMap<>();
152 clientFactory.setInitParameterMap(paramMap);
153
154 final Map<String, String> clientConfigMap = getConfigParameterMap(ConfigName.CLIENT);
155 if (clientConfigMap != null) {
156 paramMap.putAll(clientConfigMap);
157 }
158
159
160 final List<FileAuthentication> fileAuthList = fileAuthenticationService.getFileAuthenticationList(getId());
161 final List<SmbAuthentication> smbAuthList = new ArrayList<>();
162 final List<org.codelibs.fess.crawler.client.smb1.SmbAuthentication> smb1AuthList = new ArrayList<>();
163 final List<FtpAuthentication> ftpAuthList = new ArrayList<>();
164 for (final FileAuthentication fileAuth : fileAuthList) {
165 if (Constants.SAMBA.equals(fileAuth.getProtocolScheme())) {
166 final SmbAuthentication smbAuth = new SmbAuthentication();
167 final Map<String, String> map = ParameterUtil.parse(fileAuth.getParameters());
168 final String domain = map.get("domain");
169 smbAuth.setDomain(domain == null ? StringUtil.EMPTY : domain);
170 smbAuth.setServer(fileAuth.getHostname());
171 smbAuth.setPort(fileAuth.getPort() == null ? -1 : fileAuth.getPort().intValue());
172 smbAuth.setUsername(fileAuth.getUsername());
173 smbAuth.setPassword(fileAuth.getPassword());
174 smbAuthList.add(smbAuth);
175
176 final org.codelibs.fess.crawler.client.smb1.SmbAuthentication smb1Auth =
177 new org.codelibs.fess.crawler.client.smb1.SmbAuthentication();
178 smb1Auth.setDomain(domain == null ? StringUtil.EMPTY : domain);
179 smb1Auth.setServer(fileAuth.getHostname());
180 smb1Auth.setPort(fileAuth.getPort() == null ? -1 : fileAuth.getPort().intValue());
181 smb1Auth.setUsername(fileAuth.getUsername());
182 smb1Auth.setPassword(fileAuth.getPassword());
183 smb1AuthList.add(smb1Auth);
184 } else if (Constants.FTP.equals(fileAuth.getProtocolScheme())) {
185 final FtpAuthentication ftpAuth = new FtpAuthentication();
186 ftpAuth.setServer(fileAuth.getHostname());
187 ftpAuth.setPort(fileAuth.getPort() == null ? -1 : fileAuth.getPort());
188 ftpAuth.setUsername(fileAuth.getUsername());
189 ftpAuth.setPassword(fileAuth.getPassword());
190 ftpAuthList.add(ftpAuth);
191 }
192 }
193 paramMap.put(SmbClient.SMB_AUTHENTICATIONS_PROPERTY, smbAuthList.toArray(new SmbAuthentication[smbAuthList.size()]));
194 paramMap.put(org.codelibs.fess.crawler.client.smb1.SmbClient.SMB_AUTHENTICATIONS_PROPERTY,
195 smb1AuthList.toArray(new org.codelibs.fess.crawler.client.smb1.SmbAuthentication[smb1AuthList.size()]));
196 paramMap.put(FtpClient.FTP_AUTHENTICATIONS_PROPERTY, ftpAuthList.toArray(new FtpAuthentication[ftpAuthList.size()]));
197
198 return paramMap;
199 }
200
201 @Override
202 public Map<String, String> getConfigParameterMap(final ConfigName name) {
203 if (configParameterMap == null) {
204 configParameterMap = ParameterUtil.createConfigParameterMap(getConfigParameter());
205 }
206
207 final Map<String, String> configMap = configParameterMap.get(name);
208 if (configMap == null) {
209 return Collections.emptyMap();
210 }
211 return configMap;
212 }
213
214 @Override
215 public String getId() {
216 return asDocMeta().id();
217 }
218
219 public void setId(final String id) {
220 asDocMeta().id(id);
221 }
222
223 public Long getVersionNo() {
224 return asDocMeta().version();
225 }
226
227 public void setVersionNo(final Long version) {
228 asDocMeta().version(version);
229 }
230
231 @Override
232 public String toString() {
233 return "FileConfig [available=" + available + ", boost=" + boost + ", configParameter=" + configParameter + ", createdBy="
234 + createdBy + ", createdTime=" + createdTime + ", depth=" + depth + ", excludedDocPaths=" + excludedDocPaths
235 + ", excludedPaths=" + excludedPaths + ", includedDocPaths=" + includedDocPaths + ", includedPaths=" + includedPaths
236 + ", intervalTime=" + intervalTime + ", timeToLive=" + timeToLive + ", maxAccessCount=" + maxAccessCount + ", name=" + name
237 + ", numOfThread=" + numOfThread + ", paths=" + paths + ", permissions=" + Arrays.toString(permissions) + ", sortOrder="
238 + sortOrder + ", updatedBy=" + updatedBy + ", updatedTime=" + updatedTime + "]";
239 }
240
241 }