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.helper;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.annotation.PostConstruct;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  import org.codelibs.fess.app.service.DuplicateHostService;
26  import org.codelibs.fess.es.config.exentity.DuplicateHost;
27  import org.codelibs.fess.util.ComponentUtil;
28  
29  public class DuplicateHostHelper {
30      private static final Logger logger = LogManager.getLogger(DuplicateHostHelper.class);
31  
32      protected List<DuplicateHost> duplicateHostList;
33  
34      @PostConstruct
35      public void init() {
36          if (logger.isDebugEnabled()) {
37              logger.debug("Initialize {}", this.getClass().getSimpleName());
38          }
39          if (duplicateHostList == null) {
40              duplicateHostList = new ArrayList<>();
41          }
42          final DuplicateHostService duplicateHostService = ComponentUtil.getComponent(DuplicateHostService.class);
43          duplicateHostList.addAll(duplicateHostService.getDuplicateHostList());
44      }
45  
46      public void setDuplicateHostList(final List<DuplicateHost> duplicateHostList) {
47          this.duplicateHostList = duplicateHostList;
48      }
49  
50      public void add(final DuplicateHost duplicateHost) {
51          if (duplicateHostList == null) {
52              duplicateHostList = new ArrayList<>();
53          }
54          duplicateHostList.add(duplicateHost);
55      }
56  
57      public String convert(final String url) {
58          if (duplicateHostList == null) {
59              init();
60          }
61  
62          String newUrl = url;
63          for (final DuplicateHost duplicateHost : duplicateHostList) {
64              newUrl = duplicateHost.convert(newUrl);
65          }
66          return newUrl;
67      }
68  }