Migrate PulsarPropertiesMapper to use JsonWriter
Closes gh-41490
This commit is contained in:
@@ -24,7 +24,6 @@ import java.util.TreeMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.pulsar.client.admin.PulsarAdminBuilder;
|
||||
import org.apache.pulsar.client.api.Authentication;
|
||||
@@ -39,6 +38,7 @@ import org.apache.pulsar.client.api.ServiceUrlProvider;
|
||||
import org.apache.pulsar.client.impl.AutoClusterFailover.AutoClusterFailoverBuilderImpl;
|
||||
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.pulsar.core.PulsarTemplate;
|
||||
import org.springframework.pulsar.listener.PulsarContainerProperties;
|
||||
import org.springframework.pulsar.reader.PulsarReaderContainerProperties;
|
||||
@@ -53,6 +53,9 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
final class PulsarPropertiesMapper {
|
||||
|
||||
private static final JsonWriter<Map<String, String>> jsonWriter = JsonWriter
|
||||
.of((members) -> members.addSelf().as(TreeMap::new).usingPairs(Map::forEach));
|
||||
|
||||
private final PulsarProperties properties;
|
||||
|
||||
PulsarPropertiesMapper(PulsarProperties properties) {
|
||||
@@ -124,7 +127,7 @@ final class PulsarPropertiesMapper {
|
||||
String pluginClassName = properties.getPluginClassName();
|
||||
if (StringUtils.hasText(pluginClassName)) {
|
||||
try {
|
||||
action.accept(pluginClassName, getAuthenticationParamsJson(properties.getParam()));
|
||||
action.accept(pluginClassName, jsonWriter.writeToString(properties.getParam()));
|
||||
}
|
||||
catch (UnsupportedAuthenticationException ex) {
|
||||
throw new IllegalStateException("Unable to configure Pulsar authentication", ex);
|
||||
@@ -132,30 +135,6 @@ final class PulsarPropertiesMapper {
|
||||
}
|
||||
}
|
||||
|
||||
private String getAuthenticationParamsJson(Map<String, String> params) {
|
||||
Map<String, String> sortedParams = new TreeMap<>(params);
|
||||
try {
|
||||
return sortedParams.entrySet()
|
||||
.stream()
|
||||
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), escapeJson(entry.getValue())))
|
||||
.collect(Collectors.joining(",", "{", "}"));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private String escapeJson(String raw) {
|
||||
return raw.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"")
|
||||
.replace("/", "\\/")
|
||||
.replace("\b", "\\b")
|
||||
.replace("\t", "\\t")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\f", "\\f")
|
||||
.replace("\r", "\\r");
|
||||
}
|
||||
|
||||
<T> void customizeProducerBuilder(ProducerBuilder<T> producerBuilder) {
|
||||
PulsarProperties.Producer properties = this.properties.getProducer();
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
|
||||
Reference in New Issue
Block a user