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.storage;
17  
18  import java.util.Locale;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.fess.mylasta.direction.FessConfig;
24  import org.codelibs.fess.util.ComponentUtil;
25  
26  /**
27   * Factory for creating appropriate StorageClient based on configuration.
28   */
29  public final class StorageClientFactory {
30  
31      private static final Logger logger = LogManager.getLogger(StorageClientFactory.class);
32  
33      private StorageClientFactory() {
34          // Utility class
35      }
36  
37      /**
38       * Auto-detect storage type from endpoint URL.
39       *
40       * @param endpoint the storage endpoint URL
41       * @return detected storage type
42       */
43      public static StorageType detectStorageType(final String endpoint) {
44          if (StringUtil.isBlank(endpoint)) {
45              // Default to S3 if no endpoint (uses AWS default)
46              return StorageType.S3;
47          }
48  
49          final String lowerEndpoint = endpoint.toLowerCase(Locale.ROOT);
50  
51          // GCS patterns
52          if (lowerEndpoint.contains("storage.googleapis.com") || lowerEndpoint.contains(".storage.cloud.google.com")) {
53              return StorageType.GCS;
54          }
55  
56          // S3 patterns
57          if (lowerEndpoint.contains(".amazonaws.com") || lowerEndpoint.contains("s3.") || lowerEndpoint.contains("s3-")) {
58              return StorageType.S3;
59          }
60  
61          // Default to S3-compatible (MinIO, etc.)
62          return StorageType.S3_COMPAT;
63      }
64  
65      /**
66       * Creates a StorageClient based on FessConfig.
67       *
68       * @param fessConfig the Fess configuration
69       * @return configured StorageClient
70       */
71      public static StorageClient createClient(final FessConfig fessConfig) {
72          final String endpoint = fessConfig.getStorageEndpoint();
73          final String accessKey = fessConfig.getStorageAccessKey();
74          final String secretKey = fessConfig.getStorageSecretKey();
75          final String bucket = fessConfig.getStorageBucket();
76  
77          // Get explicit type or auto-detect
78          final String typeStr = fessConfig.getStorageType();
79          final StorageType type;
80          if (StringUtil.isBlank(typeStr) || "auto".equalsIgnoreCase(typeStr)) {
81              type = detectStorageType(endpoint);
82              if (logger.isDebugEnabled()) {
83                  logger.debug("Auto-detected storage type: {} for endpoint: {}", type, endpoint);
84              }
85          } else {
86              type = parseStorageType(typeStr);
87          }
88  
89          switch (type) {
90          case GCS:
91              return new GcsStorageClient(fessConfig.getStorageProjectId(), bucket, endpoint, fessConfig.getStorageCredentialsPath());
92          case S3:
93          case S3_COMPAT:
94          default:
95              return new S3StorageClient(endpoint, accessKey, secretKey, bucket, fessConfig.getStorageRegion());
96          }
97      }
98  
99      /**
100      * Creates a StorageClient using the default FessConfig.
101      *
102      * @return configured StorageClient
103      */
104     public static StorageClient createClient() {
105         return createClient(ComponentUtil.getFessConfig());
106     }
107 
108     /**
109      * Parses a storage type string to StorageType enum.
110      *
111      * @param typeStr the type string (s3, gcs, s3_compat, auto)
112      * @return parsed StorageType
113      */
114     private static StorageType parseStorageType(final String typeStr) {
115         final String upper = typeStr.toUpperCase(Locale.ROOT);
116         try {
117             return StorageType.valueOf(upper);
118         } catch (final IllegalArgumentException e) {
119             logger.warn("Unknown storage type: {}, defaulting to S3_COMPAT", typeStr);
120             return StorageType.S3_COMPAT;
121         }
122     }
123 }