1 /*
2 * Copyright 2012-2025 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 org.apache.logging.log4j.LogManager;
19 import org.apache.logging.log4j.Logger;
20 import org.codelibs.fess.crawler.entity.AccessResult;
21 import org.codelibs.fess.crawler.entity.ResponseData;
22 import org.codelibs.fess.crawler.entity.ResultData;
23 import org.codelibs.fess.crawler.processor.impl.DefaultResponseProcessor;
24 import org.codelibs.fess.ingest.IngestFactory;
25 import org.codelibs.fess.ingest.Ingester;
26 import org.codelibs.fess.util.ComponentUtil;
27
28 import jakarta.annotation.PostConstruct;
29
30 /**
31 * Response processor implementation for the Fess search engine.
32 * This processor extends DefaultResponseProcessor to provide additional
33 * processing capabilities through the ingest framework, allowing for
34 * custom data transformation and enrichment during the crawling process.
35 *
36 * <p>It supports pluggable ingesters that can modify the result data
37 * before it is stored in the search index.</p>
38 */
39 public class FessResponseProcessor extends DefaultResponseProcessor {
40 /**
41 * Default constructor.
42 */
43 public FessResponseProcessor() {
44 super();
45 }
46
47 /** Logger instance for this class */
48 private static final Logger logger = LogManager.getLogger(FessResponseProcessor.class);
49
50 /** Factory for creating and managing ingesters */
51 private IngestFactory ingestFactory = null;
52
53 /**
54 * Initializes the processor after dependency injection.
55 * Sets up the ingest factory if available in the component system.
56 */
57 @PostConstruct
58 public void init() {
59 if (ComponentUtil.hasIngestFactory()) {
60 ingestFactory = ComponentUtil.getIngestFactory();
61 }
62 }
63
64 /**
65 * Creates an access result after processing the response data through ingesters.
66 *
67 * @param responseData the response data from the crawled resource
68 * @param resultData the result data to be processed
69 * @return the access result containing the processed data
70 */
71 @Override
72 protected AccessResult<?> createAccessResult(final ResponseData responseData, final ResultData resultData) {
73 return super.createAccessResult(responseData, ingest(responseData, resultData));
74 }
75
76 /**
77 * Processes the result data through all available ingesters.
78 * Each ingester can transform and enrich the data before it is indexed.
79 *
80 * @param responseData the response data from the crawled resource
81 * @param resultData the result data to be processed
82 * @return the processed result data after all ingesters have been applied
83 */
84 private ResultData ingest(final ResponseData responseData, final ResultData resultData) {
85 if (ingestFactory == null) {
86 return resultData;
87 }
88 ResultData target = resultData;
89 for (final Ingester ingester : ingestFactory.getIngesters()) {
90 try {
91 target = ingester.process(target, responseData);
92 } catch (final Exception e) {
93 logger.warn("Failed to process Ingest[{}]", ingester.getClass().getSimpleName(), e);
94 }
95 }
96 return target;
97 }
98 }