View Javadoc
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.opensearch.client;
17  
18  import static org.codelibs.core.stream.StreamUtil.split;
19  
20  import org.codelibs.core.lang.StringUtil;
21  import org.codelibs.fesen.client.HttpClient;
22  import org.codelibs.fess.Constants;
23  import org.codelibs.fess.crawler.client.FesenClient;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  import org.codelibs.fess.util.ComponentUtil;
26  import org.opensearch.common.settings.Settings;
27  import org.opensearch.common.settings.Settings.Builder;
28  import org.opensearch.transport.client.Client;
29  
30  /**
31   * OpenSearch client implementation specifically for crawler operations.
32   * Extends FesenClient to provide search engine connectivity for the crawler components.
33   */
34  public class CrawlerEngineClient extends FesenClient {
35  
36      /**
37       * Creates a new instance of CrawlerEngineClient.
38       */
39      public CrawlerEngineClient() {
40          super();
41      }
42  
43      @Override
44      protected Client createClient() {
45          final FessConfig fessConfig = ComponentUtil.getFessConfig();
46          final String[] hosts =
47                  split(address, ",").get(stream -> stream.map(String::trim).filter(StringUtil::isNotEmpty).toArray(n -> new String[n]));
48          final Builder builder = Settings.builder()
49                  .putList("http.hosts", hosts)
50                  .put("processors", fessConfig.getCrawlerHttpProcessors())
51                  .put("http.heartbeat_interval", fessConfig.getFesenHeartbeatInterval());
52          final String username = fessConfig.getFesenUsername();
53          final String password = fessConfig.getFesenPassword();
54          if (StringUtil.isNotBlank(username) && StringUtil.isNotBlank(password)) {
55              builder.put(Constants.FESEN_USERNAME, username);
56              builder.put(Constants.FESEN_PASSWORD, password);
57          }
58          final String authorities = fessConfig.getFesenHttpSslCertificateAuthorities();
59          if (StringUtil.isNotBlank(authorities)) {
60              builder.put("http.ssl.certificate_authorities", authorities);
61          }
62          return new HttpClient(builder.build(), null);
63      }
64  }