Skip to content

Commit 89f3052

Browse files
committed
Migrate PulsarPropertiesMapper to use JsonWriter
Closes gh-41490
1 parent 20c2af1 commit 89f3052

File tree

1 file changed

+5
-26
lines changed

1 file changed

+5
-26
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/pulsar/PulsarPropertiesMapper.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.concurrent.TimeUnit;
2525
import java.util.function.BiConsumer;
2626
import java.util.function.Consumer;
27-
import java.util.stream.Collectors;
2827

2928
import org.apache.pulsar.client.admin.PulsarAdminBuilder;
3029
import org.apache.pulsar.client.api.Authentication;
@@ -39,6 +38,7 @@
3938
import org.apache.pulsar.client.impl.AutoClusterFailover.AutoClusterFailoverBuilderImpl;
4039

4140
import org.springframework.boot.context.properties.PropertyMapper;
41+
import org.springframework.boot.json.JsonWriter;
4242
import org.springframework.pulsar.core.PulsarTemplate;
4343
import org.springframework.pulsar.listener.PulsarContainerProperties;
4444
import org.springframework.pulsar.reader.PulsarReaderContainerProperties;
@@ -53,6 +53,9 @@
5353
*/
5454
final class PulsarPropertiesMapper {
5555

56+
private static final JsonWriter<Map<String, String>> jsonWriter = JsonWriter
57+
.of((members) -> members.addSelf().as(TreeMap::new).usingPairs(Map::forEach));
58+
5659
private final PulsarProperties properties;
5760

5861
PulsarPropertiesMapper(PulsarProperties properties) {
@@ -124,38 +127,14 @@ private void customizeAuthentication(PulsarProperties.Authentication properties,
124127
String pluginClassName = properties.getPluginClassName();
125128
if (StringUtils.hasText(pluginClassName)) {
126129
try {
127-
action.accept(pluginClassName, getAuthenticationParamsJson(properties.getParam()));
130+
action.accept(pluginClassName, jsonWriter.writeToString(properties.getParam()));
128131
}
129132
catch (UnsupportedAuthenticationException ex) {
130133
throw new IllegalStateException("Unable to configure Pulsar authentication", ex);
131134
}
132135
}
133136
}
134137

135-
private String getAuthenticationParamsJson(Map<String, String> params) {
136-
Map<String, String> sortedParams = new TreeMap<>(params);
137-
try {
138-
return sortedParams.entrySet()
139-
.stream()
140-
.map((entry) -> "\"%s\":\"%s\"".formatted(entry.getKey(), escapeJson(entry.getValue())))
141-
.collect(Collectors.joining(",", "{", "}"));
142-
}
143-
catch (Exception ex) {
144-
throw new IllegalStateException("Could not convert auth parameters to encoded string", ex);
145-
}
146-
}
147-
148-
private String escapeJson(String raw) {
149-
return raw.replace("\\", "\\\\")
150-
.replace("\"", "\\\"")
151-
.replace("/", "\\/")
152-
.replace("\b", "\\b")
153-
.replace("\t", "\\t")
154-
.replace("\n", "\\n")
155-
.replace("\f", "\\f")
156-
.replace("\r", "\\r");
157-
}
158-
159138
<T> void customizeProducerBuilder(ProducerBuilder<T> producerBuilder) {
160139
PulsarProperties.Producer properties = this.properties.getProducer();
161140
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();

0 commit comments

Comments
 (0)