This is an automated email from the ASF dual-hosted git repository.
aloyszhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git
The following commit(s) were added to refs/heads/master by this push:
new ab543f86c0 [INLONG-10723][Manager] Update base64 encoder (#10724)
ab543f86c0 is described below
commit ab543f86c0bc8d51656347b0fda98896b2e70979
Author: AloysZhang <[email protected]>
AuthorDate: Fri Jul 26 16:59:00 2024 +0800
[INLONG-10723][Manager] Update base64 encoder (#10724)
---
.../service/resource/sink/es/ElasticsearchApi.java | 10 ++++--
.../resource/sink/Base64CompatibleTest.java | 42 ++++++++++++++++++++++
2 files changed, 49 insertions(+), 3 deletions(-)
diff --git
a/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/es/ElasticsearchApi.java
b/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/es/ElasticsearchApi.java
index e88bb102b3..8dc0c0b8c0 100644
---
a/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/es/ElasticsearchApi.java
+++
b/inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/es/ElasticsearchApi.java
@@ -39,11 +39,12 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException.NotFound;
-import sun.misc.BASE64Encoder;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Base64.Encoder;
import java.util.List;
import java.util.Map;
@@ -54,6 +55,8 @@ import java.util.Map;
@Component
public class ElasticsearchApi {
+ private static final Logger LOG =
LoggerFactory.getLogger(ElasticsearchApi.class);
+
private static final Gson GSON = new GsonBuilder().create();
private static final String MAPPINGS_KEY = "mappings";
@@ -68,7 +71,7 @@ public class ElasticsearchApi {
private static final String CONTENT_TYPE_VALUE =
"application/json;charset=UTF-8";
- private static final Logger LOG =
LoggerFactory.getLogger(ElasticsearchApi.class);
+ private final Encoder base64Encoder = Base64.getEncoder();
@Autowired
private ElasticsearchConfig esConfig;
@@ -84,7 +87,7 @@ public class ElasticsearchApi {
if (esConfig.getAuthEnable()) {
if (StringUtils.isNotEmpty(esConfig.getUsername()) &&
StringUtils.isNotEmpty(esConfig.getPassword())) {
String tokenStr = esConfig.getUsername() + ":" +
esConfig.getPassword();
- String token = String.valueOf(new
BASE64Encoder().encode(tokenStr.getBytes(StandardCharsets.UTF_8)));
+ String token =
base64Encoder.encodeToString(tokenStr.getBytes(StandardCharsets.UTF_8));
headers.add("Authorization", "Basic " + token);
}
}
@@ -214,6 +217,7 @@ public class ElasticsearchApi {
JsonObject fields = null;
Map<String, ElasticsearchFieldInfo> fieldInfos = Maps.newHashMap();
if (ObjectUtils.isNotEmpty(mappings)) {
+ String MAPPINGS_KEY = "mappings";
properties = mappings.getAsJsonObject(MAPPINGS_KEY);
}
if (ObjectUtils.isNotEmpty(properties)) {
diff --git
a/inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/service/resource/sink/Base64CompatibleTest.java
b/inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/service/resource/sink/Base64CompatibleTest.java
new file mode 100644
index 0000000000..4659468bff
--- /dev/null
+++
b/inlong-manager/manager-service/src/test/java/org/apache/inlong/manager/service/resource/sink/Base64CompatibleTest.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.service.resource.sink;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import sun.misc.BASE64Encoder;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+import java.util.Base64.Encoder;
+
+public class Base64CompatibleTest {
+
+ @Test
+ public void base64CompatibleForDifferentVersionTest() {
+ String tokenStr = "root" + ":" + "admin";
+ byte[] tokeBytes = tokenStr.getBytes(StandardCharsets.UTF_8);
+ // token encode by sun.misc.BASE64Encoder, this test will fail if the
jdk version is 9 or later
+ // so, this test should be removed when Apache InLong project support
jdk 9 or later
+ String tokenOld = String.valueOf(new
BASE64Encoder().encode(tokeBytes));
+ // token encode by java.util.Base64.Encoder
+ Encoder encoder = Base64.getEncoder();
+ String tokenNew = encoder.encodeToString(tokeBytes);
+ Assertions.assertEquals(tokenOld, tokenNew);
+ }
+}