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.crawler.transformer;
17  
18  import java.net.URLDecoder;
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.codelibs.core.collection.LruHashMap;
26  import org.codelibs.core.lang.StringUtil;
27  import org.codelibs.fess.Constants;
28  import org.codelibs.fess.crawler.entity.AccessResult;
29  import org.codelibs.fess.crawler.entity.AccessResultData;
30  import org.codelibs.fess.crawler.entity.UrlQueue;
31  import org.codelibs.fess.crawler.util.CrawlingParameterUtil;
32  import org.codelibs.fess.mylasta.direction.FessConfig;
33  import org.codelibs.fess.util.ComponentUtil;
34  import org.codelibs.fess.util.GroovyUtil;
35  import org.slf4j.Logger;
36  
37  public interface FessTransformer {
38  
39      Map<String, String> parentEncodingMap = Collections.synchronizedMap(new LruHashMap<String, String>(1000));
40  
41      FessConfig getFessConfig();
42  
43      Logger getLogger();
44  
45      default String getHost(final String u) {
46          if (StringUtil.isBlank(u)) {
47              return StringUtil.EMPTY; // empty
48          }
49  
50          String url = u;
51          final String originalUrl = url;
52  
53          int idx = url.indexOf("://");
54          if (idx >= 0) {
55              url = url.substring(idx + 3);
56          }
57  
58          idx = url.indexOf('/');
59          if (idx >= 0) {
60              url = url.substring(0, idx);
61          }
62  
63          if (url.equals(originalUrl)) {
64              return getFessConfig().getCrawlerDocumentUnknownHostname();
65          }
66  
67          return url;
68      }
69  
70      default String getSite(final String u, final String encoding) {
71          if (StringUtil.isBlank(u)) {
72              return StringUtil.EMPTY; // empty
73          }
74  
75          String url = u;
76          int idx = url.indexOf("://");
77          if (idx >= 0) {
78              url = url.substring(idx + 3);
79          }
80  
81          idx = url.indexOf('?');
82          if (idx >= 0) {
83              url = url.substring(0, idx);
84          }
85  
86          if (encoding != null) {
87              String enc;
88              if (StringUtil.isNotBlank(getFessConfig().getCrawlerDocumentSiteEncoding())) {
89                  if (getFessConfig().isCrawlerDocumentUseSiteEncodingOnEnglish()) {
90                      if ("ISO-8859-1".equalsIgnoreCase(encoding) || "US-ASCII".equalsIgnoreCase(encoding)) {
91                          enc = getFessConfig().getCrawlerDocumentSiteEncoding();
92                      } else {
93                          enc = encoding;
94                      }
95                  } else {
96                      enc = getFessConfig().getCrawlerDocumentSiteEncoding();
97                  }
98              } else {
99                  enc = encoding;
100             }
101 
102             try {
103                 url = URLDecoder.decode(url, enc);
104             } catch (final Exception e) {}
105         }
106 
107         return abbreviateSite(url);
108     }
109 
110     default void putResultDataBody(final Map<String, Object> dataMap, final String key, final Object value) {
111         final FessConfig fessConfig = ComponentUtil.getFessConfig();
112         if (fessConfig.getIndexFieldUrl().equals(key)) {
113             dataMap.put(key, value);
114         } else if (dataMap.containsKey(key)) {
115             if (getFessConfig().isCrawlerDocumentAppendData()) {
116                 final Object oldValue = dataMap.get(key);
117                 final Object[] oldValues = oldValue instanceof Object[] ? (Object[]) oldValue : new Object[] { oldValue };
118                 if (value.getClass().isArray()) {
119                     final Object[] newValues = (Object[]) value;
120                     final Object[] values = Arrays.copyOf(oldValues, oldValues.length + newValues.length);
121                     for (int i = 0; i < newValues.length; i++) {
122                         values[values.length - 1 + i] = newValues[i];
123                     }
124                     dataMap.put(key, values);
125                 } else {
126                     final Object[] values = Arrays.copyOf(oldValues, oldValues.length + 1);
127                     values[values.length - 1] = value;
128                     dataMap.put(key, values);
129                 }
130             } else {
131                 dataMap.put(key, value);
132             }
133         } else {
134             dataMap.put(key, value);
135         }
136     }
137 
138     default void putResultDataWithTemplate(final Map<String, Object> dataMap, final String key, final Object value, final String template) {
139         Object target = value;
140         if (template != null) {
141             final Map<String, Object> contextMap = new HashMap<>();
142             contextMap.put("doc", dataMap);
143             final Map<String, Object> paramMap = new HashMap<>(dataMap.size() + 2);
144             paramMap.putAll(dataMap);
145             paramMap.put("value", target);
146             paramMap.put("context", contextMap);
147             target = evaluateValue(template, paramMap);
148         }
149         if (key != null && target != null) {
150             putResultDataBody(dataMap, key, target);
151         }
152     }
153 
154     default Object evaluateValue(final String template, final Map<String, Object> paramMap) {
155         if (StringUtil.isEmpty(template)) {
156             return StringUtil.EMPTY;
157         }
158 
159         return GroovyUtil.evaluate(template, paramMap);
160     }
161 
162     default int getMaxSiteLength() {
163         return getFessConfig().getCrawlerDocumentMaxSiteLengthAsInteger();
164     }
165 
166     default String abbreviateSite(final String value) {
167         final int maxSiteLength = getMaxSiteLength();
168         if (maxSiteLength > -1) {
169             return StringUtils.abbreviate(value, maxSiteLength);
170         } else {
171             return value;
172         }
173     }
174 
175     default String getFileName(final String url, final String encoding) {
176         if (StringUtil.isBlank(url)) {
177             return StringUtil.EMPTY;
178         }
179 
180         String u = url;
181         int idx = u.lastIndexOf('?');
182         if (idx >= 0) {
183             u = u.substring(0, idx);
184         }
185 
186         idx = u.lastIndexOf('#');
187         if (idx >= 0) {
188             u = u.substring(0, idx);
189         }
190         u = decodeUrlAsName(u, u.startsWith("file:"));
191         idx = u.lastIndexOf('/');
192         if (idx >= 0) {
193             if (u.length() > idx + 1) {
194                 u = u.substring(idx + 1);
195             } else {
196                 u = StringUtil.EMPTY;
197             }
198         }
199         return u;
200     }
201 
202     default String decodeUrlAsName(final String url, final boolean escapePlus) {
203         if (url == null) {
204             return null;
205         }
206 
207         final FessConfig fessConfig = getFessConfig();
208         String enc = Constants.UTF_8;
209         if (StringUtil.isBlank(fessConfig.getCrawlerDocumentFileNameEncoding())) {
210             final UrlQueue<?> urlQueue = CrawlingParameterUtil.getUrlQueue();
211             if (urlQueue != null) {
212                 final String parentUrl = urlQueue.getParentUrl();
213                 if (StringUtil.isNotEmpty(parentUrl)) {
214                     final String sessionId = urlQueue.getSessionId();
215                     final String pageEnc = getParentEncoding(parentUrl, sessionId);
216                     if (pageEnc != null) {
217                         enc = pageEnc;
218                     } else if (urlQueue.getEncoding() != null) {
219                         enc = urlQueue.getEncoding();
220                     }
221                 }
222             }
223         } else {
224             enc = fessConfig.getCrawlerDocumentFileNameEncoding();
225         }
226 
227         final String escapedUrl = escapePlus ? url.replace("+", "%2B") : url;
228         try {
229             return URLDecoder.decode(escapedUrl, enc);
230         } catch (final Exception e) {
231             return url;
232         }
233     }
234 
235     default String getParentEncoding(final String parentUrl, final String sessionId) {
236         final String key = sessionId + ":" + parentUrl;
237         String enc = parentEncodingMap.get(key);
238         if (enc != null) {
239             return enc;
240         }
241 
242         final AccessResult<?> accessResult = ComponentUtil.getDataService().getAccessResult(sessionId, parentUrl);
243         if (accessResult != null) {
244             final AccessResultData<?> accessResultData = accessResult.getAccessResultData();
245             if (accessResultData != null && accessResultData.getEncoding() != null) {
246                 enc = accessResultData.getEncoding();
247                 parentEncodingMap.put(key, enc);
248                 return enc;
249             }
250         }
251         return null;
252     }
253 }