View Javadoc
1   /*
2    * Copyright 2012-2021 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.processor;
17  
18  import javax.annotation.PostConstruct;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.codelibs.fess.crawler.entity.AccessResult;
23  import org.codelibs.fess.crawler.entity.ResponseData;
24  import org.codelibs.fess.crawler.entity.ResultData;
25  import org.codelibs.fess.crawler.processor.impl.DefaultResponseProcessor;
26  import org.codelibs.fess.ingest.IngestFactory;
27  import org.codelibs.fess.ingest.Ingester;
28  import org.codelibs.fess.util.ComponentUtil;
29  
30  public class FessResponseProcessor extends DefaultResponseProcessor {
31      private static final Logger logger = LogManager.getLogger(FessResponseProcessor.class);
32  
33      private IngestFactory ingestFactory = null;
34  
35      @PostConstruct
36      public void init() {
37          if (ComponentUtil.hasIngestFactory()) {
38              ingestFactory = ComponentUtil.getIngestFactory();
39          }
40      }
41  
42      @Override
43      protected AccessResult<?> createAccessResult(final ResponseData responseData, final ResultData resultData) {
44          return super.createAccessResult(responseData, ingest(responseData, resultData));
45      }
46  
47      private ResultData ingest(final ResponseData responseData, final ResultData resultData) {
48          if (ingestFactory == null) {
49              return resultData;
50          }
51          ResultData target = resultData;
52          for (final Ingester ingester : ingestFactory.getIngesters()) {
53              try {
54                  target = ingester.process(target, responseData);
55              } catch (final Exception e) {
56                  logger.warn("Failed to process Ingest[{}]", ingester.getClass().getSimpleName(), e);
57              }
58          }
59          return target;
60      }
61  }