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 e88bb102b39..8dc0c0b8c04 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.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 @@ @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 @@ private HttpHeaders getHttpHeaders() { 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 Map getMappingMap(String indexName) throw JsonObject fields = null; Map 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 00000000000..4659468bff9 --- /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); + } +}