Skip to content

Commit 53e12ca

Browse files
committed
[ISSUE #9439] Add backslash escaping to MixAll
.properties2String
1 parent 577371e commit 53e12ca

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

common/src/main/java/org/apache/rocketmq/common/MixAll.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,21 +317,34 @@ public static void printObjectProperties(final Logger logger, final Object objec
317317
}
318318
}
319319

320-
public static String properties2String(final Properties properties) {
321-
return properties2String(properties, false);
322-
}
323320

324-
public static String properties2String(final Properties properties, final boolean isSort) {
321+
public static String properties2String(final Properties properties, final boolean isSort, final boolean escapeBackslashes) {
325322
StringBuilder sb = new StringBuilder();
326323
Set<Map.Entry<Object, Object>> entrySet = isSort ? new TreeMap<>(properties).entrySet() : properties.entrySet();
324+
327325
for (Map.Entry<Object, Object> entry : entrySet) {
328326
if (entry.getValue() != null) {
329-
sb.append(entry.getKey().toString() + "=" + entry.getValue().toString() + "\n");
327+
String key = entry.getKey().toString();
328+
String value = entry.getValue().toString();
329+
330+
if (escapeBackslashes && value.contains("\\")) {
331+
value = value.replace("\\", "\\\\");
332+
}
333+
334+
sb.append(key).append("=").append(value).append("\n");
330335
}
331336
}
332337
return sb.toString();
333338
}
334339

340+
public static String properties2String(final Properties properties, final boolean isSort) {
341+
return properties2String(properties, isSort, false);
342+
}
343+
344+
public static String properties2String(final Properties properties) {
345+
return properties2String(properties, false, false);
346+
}
347+
335348
public static Properties string2Properties(final String str) {
336349
Properties properties = new Properties();
337350
try {

remoting/src/main/java/org/apache/rocketmq/remoting/Configuration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public void persist() {
207207
readWriteLock.readLock().lockInterruptibly();
208208

209209
try {
210-
String allConfigs = getAllConfigsInternal();
210+
String allConfigs = getAllConfigsInternal(false);
211211

212212
MixAll.string2File(allConfigs, getStorePath());
213213
} catch (IOException e) {
@@ -226,7 +226,7 @@ public String getAllConfigsFormatString() {
226226

227227
try {
228228

229-
return getAllConfigsInternal();
229+
return getAllConfigsInternal(true);
230230

231231
} finally {
232232
readWriteLock.readLock().unlock();
@@ -278,7 +278,7 @@ public Properties getAllConfigs() {
278278
return null;
279279
}
280280

281-
private String getAllConfigsInternal() {
281+
private String getAllConfigsInternal(boolean escapeBackslashes) {
282282
StringBuilder stringBuilder = new StringBuilder();
283283

284284
// reload from config object ?
@@ -292,7 +292,7 @@ private String getAllConfigsInternal() {
292292
}
293293

294294
{
295-
stringBuilder.append(MixAll.properties2String(this.allConfigs, true));
295+
stringBuilder.append(MixAll.properties2String(this.allConfigs, true, escapeBackslashes));
296296
}
297297

298298
return stringBuilder.toString();

0 commit comments

Comments
 (0)