1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29 public final class StorageClientFactory {
30
31 private static final Logger logger = LogManager.getLogger(StorageClientFactory.class);
32
33 private StorageClientFactory() {
34
35 }
36
37
38
39
40
41
42
43 public static StorageType detectStorageType(final String endpoint) {
44 if (StringUtil.isBlank(endpoint)) {
45
46 return StorageType.S3;
47 }
48
49 final String lowerEndpoint = endpoint.toLowerCase(Locale.ROOT);
50
51
52 if (lowerEndpoint.contains("storage.googleapis.com") || lowerEndpoint.contains(".storage.cloud.google.com")) {
53 return StorageType.GCS;
54 }
55
56
57 if (lowerEndpoint.contains(".amazonaws.com") || lowerEndpoint.contains("s3.") || lowerEndpoint.contains("s3-")) {
58 return StorageType.S3;
59 }
60
61
62 return StorageType.S3_COMPAT;
63 }
64
65
66
67
68
69
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
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
101
102
103
104 public static StorageClient createClient() {
105 return createClient(ComponentUtil.getFessConfig());
106 }
107
108
109
110
111
112
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 }