View Javadoc
1   /*
2    * Copyright 2012-2019 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.helper;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.concurrent.ConcurrentHashMap;
23  import java.util.concurrent.ExecutionException;
24  import java.util.concurrent.TimeUnit;
25  import java.util.regex.Pattern;
26  
27  import javax.annotation.PostConstruct;
28  
29  import org.codelibs.core.lang.StringUtil;
30  import org.codelibs.fess.Constants;
31  import org.codelibs.fess.app.service.DataConfigService;
32  import org.codelibs.fess.app.service.FileConfigService;
33  import org.codelibs.fess.app.service.WebConfigService;
34  import org.codelibs.fess.es.config.exbhv.DataConfigBhv;
35  import org.codelibs.fess.es.config.exbhv.FailureUrlBhv;
36  import org.codelibs.fess.es.config.exbhv.FileConfigBhv;
37  import org.codelibs.fess.es.config.exbhv.WebConfigBhv;
38  import org.codelibs.fess.es.config.exentity.CrawlingConfig;
39  import org.codelibs.fess.es.config.exentity.CrawlingConfig.ConfigName;
40  import org.codelibs.fess.es.config.exentity.CrawlingConfig.ConfigType;
41  import org.codelibs.fess.es.config.exentity.DataConfig;
42  import org.codelibs.fess.es.config.exentity.FailureUrl;
43  import org.codelibs.fess.es.config.exentity.FileConfig;
44  import org.codelibs.fess.es.config.exentity.WebConfig;
45  import org.codelibs.fess.mylasta.direction.FessConfig;
46  import org.codelibs.fess.util.ComponentUtil;
47  import org.dbflute.cbean.result.ListResultBean;
48  import org.dbflute.optional.OptionalThing;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.google.common.cache.Cache;
53  import com.google.common.cache.CacheBuilder;
54  
55  public class CrawlingConfigHelper {
56  
57      private static final Logger logger = LoggerFactory.getLogger(CrawlingConfigHelper.class);
58  
59      protected final Map<String, CrawlingConfig> crawlingConfigMap = new ConcurrentHashMap<>();
60  
61      protected int count = 1;
62  
63      protected Cache<String, CrawlingConfig> crawlingConfigCache;
64  
65      @PostConstruct
66      public void init() {
67          crawlingConfigCache = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(10, TimeUnit.MINUTES).build();
68      }
69  
70      public ConfigType getConfigType(final String configId) {
71          if (configId == null || configId.length() < 2) {
72              return null;
73          }
74          final String configType = configId.substring(0, 1);
75          if (ConfigType.WEB.getTypePrefix().equals(configType)) {
76              return ConfigType.WEB;
77          } else if (ConfigType.FILE.getTypePrefix().equals(configType)) {
78              return ConfigType.FILE;
79          } else if (ConfigType.DATA.getTypePrefix().equals(configType)) {
80              return ConfigType.DATA;
81          }
82          return null;
83      }
84  
85      protected String getId(final String configId) {
86          if (configId == null || configId.length() < 2) {
87              return null;
88          }
89          return configId.substring(1);
90      }
91  
92      public CrawlingConfig getCrawlingConfig(final String configId) {
93          try {
94              return crawlingConfigCache.get(configId, () -> {
95                  final ConfigType configType = getConfigType(configId);
96                  if (configType == null) {
97                      return null;
98                  }
99                  final String id = getId(configId);
100                 if (id == null) {
101                     return null;
102                 }
103                 switch (configType) {
104                 case WEB:
105                     final WebConfigService webConfigService = ComponentUtil.getComponent(WebConfigService.class);
106                     return webConfigService.getWebConfig(id).get();
107                 case FILE:
108                     final FileConfigService fileConfigService = ComponentUtil.getComponent(FileConfigService.class);
109                     return fileConfigService.getFileConfig(id).get();
110                 case DATA:
111                     final DataConfigService dataConfigService = ComponentUtil.getComponent(DataConfigService.class);
112                     return dataConfigService.getDataConfig(id).get();
113                 default:
114                     return null;
115                 }
116             });
117         } catch (final ExecutionException e) {
118             logger.warn("Failed to access a crawling config cache: " + configId, e);
119             return null;
120         }
121     }
122 
123     public OptionalThing<String> getPipeline(final String configId) {
124         final CrawlingConfig config = getCrawlingConfig(configId);
125         if (config == null) {
126             return OptionalThing.empty();
127         }
128         final String pipeline = config.getConfigParameterMap(ConfigName.CONFIG).get("pipeline");
129         if (StringUtil.isBlank(pipeline)) {
130             return OptionalThing.empty();
131         }
132         return OptionalThing.of(pipeline);
133     }
134 
135     public void refresh() {
136         crawlingConfigCache.invalidateAll();
137     }
138 
139     public synchronized String store(final String sessionId, final CrawlingConfig crawlingConfig) {
140         final String sessionCountId = sessionId + "-" + count;
141         crawlingConfigMap.put(sessionCountId, crawlingConfig);
142         count++;
143         return sessionCountId;
144     }
145 
146     public void remove(final String sessionId) {
147         crawlingConfigMap.remove(sessionId);
148     }
149 
150     public CrawlingConfig get(final String sessionId) {
151         return crawlingConfigMap.get(sessionId);
152     }
153 
154     public List<WebConfig> getAllWebConfigList() {
155         return getAllWebConfigList(true, true, true, null);
156     }
157 
158     public List<WebConfig> getWebConfigListByIds(final List<String> idList) {
159         if (idList == null) {
160             return getAllWebConfigList();
161         } else {
162             return getAllWebConfigList(true, true, false, idList);
163         }
164     }
165 
166     public List<WebConfig> getAllWebConfigList(final boolean withLabelType, final boolean withRoleType, final boolean available,
167             final List<String> idList) {
168         return ComponentUtil.getComponent(WebConfigBhv.class).selectList(cb -> {
169             if (available) {
170                 cb.query().setAvailable_Equal(Constants.T);
171             }
172             if (idList != null) {
173                 cb.query().setId_InScope(idList);
174             }
175             cb.query().addOrderBy_SortOrder_Asc();
176             cb.query().addOrderBy_Name_Asc();
177             cb.fetchFirst(ComponentUtil.getFessConfig().getPageWebConfigMaxFetchSizeAsInteger());
178         });
179     }
180 
181     public List<FileConfig> getAllFileConfigList() {
182         return getAllFileConfigList(true, true, true, null);
183     }
184 
185     public List<FileConfig> getFileConfigListByIds(final List<String> idList) {
186         if (idList == null) {
187             return getAllFileConfigList();
188         } else {
189             return getAllFileConfigList(true, true, false, idList);
190         }
191     }
192 
193     public List<FileConfig> getAllFileConfigList(final boolean withLabelType, final boolean withRoleType, final boolean available,
194             final List<String> idList) {
195         return ComponentUtil.getComponent(FileConfigBhv.class).selectList(cb -> {
196             if (available) {
197                 cb.query().setAvailable_Equal(Constants.T);
198             }
199             if (idList != null) {
200                 cb.query().setId_InScope(idList);
201             }
202             cb.query().addOrderBy_SortOrder_Asc();
203             cb.query().addOrderBy_Name_Asc();
204             cb.fetchFirst(ComponentUtil.getFessConfig().getPageFileConfigMaxFetchSizeAsInteger());
205         });
206     }
207 
208     public List<DataConfig> getAllDataConfigList() {
209         return getAllDataConfigList(true, true, true, null);
210     }
211 
212     public List<DataConfig> getDataConfigListByIds(final List<String> idList) {
213         if (idList == null) {
214             return getAllDataConfigList();
215         } else {
216             return getAllDataConfigList(true, true, false, idList);
217         }
218     }
219 
220     public List<DataConfig> getAllDataConfigList(final boolean withLabelType, final boolean withRoleType, final boolean available,
221             final List<String> idList) {
222         return ComponentUtil.getComponent(DataConfigBhv.class).selectList(cb -> {
223             if (available) {
224                 cb.query().setAvailable_Equal(Constants.T);
225             }
226             if (idList != null) {
227                 cb.query().setId_InScope(idList);
228             }
229             cb.query().addOrderBy_SortOrder_Asc();
230             cb.query().addOrderBy_Name_Asc();
231             cb.fetchFirst(ComponentUtil.getFessConfig().getPageDataConfigMaxFetchSizeAsInteger());
232         });
233     }
234 
235     public List<String> getExcludedUrlList(final String configId) {
236         final FessConfig fessConfig = ComponentUtil.getFessConfig();
237         final int failureCount = fessConfig.getFailureCountThreshold();
238         final String ignoreFailureType = fessConfig.getIgnoreFailureType();
239 
240         if (failureCount < 0) {
241             return Collections.emptyList();
242         }
243 
244         final int count = failureCount;
245         final ListResultBean<FailureUrl> list = ComponentUtil.getComponent(FailureUrlBhv.class).selectList(cb -> {
246             cb.query().setConfigId_Equal(configId);
247             cb.query().setErrorCount_GreaterEqual(count);
248             cb.fetchFirst(fessConfig.getPageFailureUrlMaxFetchSizeAsInteger());
249         });
250         if (list.isEmpty()) {
251             return Collections.emptyList();
252         }
253 
254         Pattern pattern = null;
255         if (StringUtil.isNotBlank(ignoreFailureType)) {
256             pattern = Pattern.compile(ignoreFailureType);
257         }
258         final List<String> urlList = new ArrayList<>();
259         for (final FailureUrl failureUrl : list) {
260             if (pattern != null) {
261                 if (!pattern.matcher(failureUrl.getErrorName()).matches()) {
262                     urlList.add(failureUrl.getUrl());
263                 }
264             } else {
265                 urlList.add(failureUrl.getUrl());
266             }
267         }
268         return urlList;
269     }
270 }