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.helper;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.apache.logging.log4j.LogManager;
22 import org.apache.logging.log4j.Logger;
23 import org.codelibs.fess.app.service.DuplicateHostService;
24 import org.codelibs.fess.opensearch.config.exentity.DuplicateHost;
25 import org.codelibs.fess.util.ComponentUtil;
26 import org.lastaflute.di.core.exception.AutoBindingFailureException;
27
28 import jakarta.annotation.PostConstruct;
29
30 /**
31 * Helper class for managing duplicate host configurations in the Fess search system.
32 * This class handles URL conversion based on duplicate host rules, allowing multiple
33 * hostnames or URLs to be treated as equivalent for crawling and indexing purposes.
34 * It maintains a list of DuplicateHost rules and applies them to URLs.
35 *
36 */
37 public class DuplicateHostHelper {
38 private static final Logger logger = LogManager.getLogger(DuplicateHostHelper.class);
39
40 /** List of duplicate host rules for URL conversion */
41 protected List<DuplicateHost> duplicateHostList;
42
43 /**
44 * Default constructor for DuplicateHostHelper.
45 * Creates a new duplicate host helper instance.
46 */
47 public DuplicateHostHelper() {
48 // Default constructor
49 }
50
51 /**
52 * Initializes the duplicate host helper after construction.
53 * Loads duplicate host configurations from the DuplicateHostService.
54 */
55 @PostConstruct
56 public void init() {
57 if (logger.isDebugEnabled()) {
58 logger.debug("Initializing {}", this.getClass().getSimpleName());
59 }
60 if (duplicateHostList == null) {
61 duplicateHostList = new ArrayList<>();
62 }
63 try {
64 final DuplicateHostService duplicateHostService = ComponentUtil.getComponent(DuplicateHostService.class);
65 duplicateHostList.addAll(duplicateHostService.getDuplicateHostList());
66 } catch (final AutoBindingFailureException e) {
67 logger.warn("DuplicateHostService is not found.", e);
68 }
69 }
70
71 /**
72 * Sets the list of duplicate host rules.
73 *
74 * @param duplicateHostList the list of duplicate host rules to use
75 */
76 public void setDuplicateHostList(final List<DuplicateHost> duplicateHostList) {
77 this.duplicateHostList = duplicateHostList;
78 }
79
80 /**
81 * Adds a new duplicate host rule to the list.
82 * Initializes the list if it doesn't exist.
83 *
84 * @param duplicateHost the duplicate host rule to add
85 */
86 public void add(final DuplicateHost duplicateHost) {
87 if (duplicateHostList == null) {
88 duplicateHostList = new ArrayList<>();
89 }
90 duplicateHostList.add(duplicateHost);
91 }
92
93 /**
94 * Converts a URL using all configured duplicate host rules.
95 * Applies each duplicate host rule in sequence to transform the URL
96 * according to the configured patterns.
97 *
98 * @param url the URL to convert
99 * @return the converted URL after applying all duplicate host rules,
100 * or null if the input URL is null
101 */
102 public String convert(final String url) {
103 if (url == null) {
104 return null;
105 }
106
107 if (duplicateHostList == null) {
108 init();
109 }
110
111 String newUrl = url;
112 for (final DuplicateHost duplicateHost : duplicateHostList) {
113 newUrl = duplicateHost.convert(newUrl);
114 }
115 return newUrl;
116 }
117 }