View Javadoc
1   /*
2    * Copyright 2012-2017 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.codelibs.fess.app.service.DuplicateHostService;
24  import org.codelibs.fess.es.config.exentity.DuplicateHost;
25  import org.codelibs.fess.util.ComponentUtil;
26  
27  public class DuplicateHostHelper {
28  
29      protected List<DuplicateHost> duplicateHostList;
30  
31      @PostConstruct
32      public void init() {
33          if (duplicateHostList == null) {
34              duplicateHostList = new ArrayList<>();
35          }
36          final DuplicateHostService duplicateHostService = ComponentUtil.getComponent(DuplicateHostService.class);
37          duplicateHostList.addAll(duplicateHostService.getDuplicateHostList());
38      }
39  
40      public void setDuplicateHostList(final List<DuplicateHost> duplicateHostList) {
41          this.duplicateHostList = duplicateHostList;
42      }
43  
44      public void add(final DuplicateHost duplicateHost) {
45          if (duplicateHostList == null) {
46              duplicateHostList = new ArrayList<>();
47          }
48          duplicateHostList.add(duplicateHost);
49      }
50  
51      public String convert(final String url) {
52          if (duplicateHostList == null) {
53              init();
54          }
55  
56          String newUrl = url;
57          for (final DuplicateHost duplicateHost : duplicateHostList) {
58              newUrl = duplicateHost.convert(newUrl);
59          }
60          return newUrl;
61      }
62  }