Skip to content

Commit 4f9d292

Browse files
authored
[ISSUE #9439] Add escape for win in the method returning broker configuration (#9440)
1 parent 9d9431d commit 4f9d292

6 files changed

Lines changed: 52 additions & 0 deletions

File tree

broker/src/main/java/org/apache/rocketmq/broker/processor/AdminBrokerProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ private RemotingCommand getBrokerConfig(ChannelHandlerContext ctx, RemotingComma
10651065
String content = this.brokerController.getConfiguration().getAllConfigsFormatString();
10661066
if (content != null && content.length() > 0) {
10671067
try {
1068+
content = MixAll.adjustConfigForPlatform(content);
10681069
response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET));
10691070
} catch (UnsupportedEncodingException e) {
10701071
LOGGER.error("AdminBrokerProcessor#getBrokerConfig: unexpected error, caller={}",

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,4 +557,13 @@ public static boolean topicAllowsLMQ(String topic) {
557557
&& !topic.startsWith(TopicValidator.SYSTEM_TOPIC_PREFIX)
558558
&& !topic.equals(TopicValidator.RMQ_SYS_SCHEDULE_TOPIC);
559559
}
560+
561+
public static String adjustConfigForPlatform(String config) {
562+
if (StringUtils.isNotBlank(config)) {
563+
if (isWindows()) {
564+
config = StringUtils.replace(config, "\\", "\\\\");
565+
}
566+
}
567+
return config;
568+
}
560569
}

common/src/test/java/org/apache/rocketmq/common/MixAllTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,43 @@ public void testIsLmq() {
8585
testLmq = "%LMQ%GID_TEST";
8686
assertThat(MixAll.isLmq(testLmq)).isTrue();
8787
}
88+
89+
@Test
90+
public void testAdjustConfigForPlatform_OnWindows() {
91+
if (MixAll.isWindows()) {
92+
String configWithSingleBackslash = "data\\path\\config\\file.properties";
93+
String adjusted = MixAll.adjustConfigForPlatform(configWithSingleBackslash);
94+
assertThat(adjusted).isEqualTo("data\\\\path\\\\config\\\\file.properties");
95+
96+
String configWithMultipleBackslashes = "C:\\\\RocketMQ\\\\logs\\\\broker.log";
97+
adjusted = MixAll.adjustConfigForPlatform(configWithMultipleBackslashes);
98+
assertThat(adjusted).isEqualTo("C:\\\\\\\\RocketMQ\\\\\\\\logs\\\\\\\\broker.log");
99+
100+
String configWithoutBackslash = "listenPort=10911";
101+
adjusted = MixAll.adjustConfigForPlatform(configWithoutBackslash);
102+
assertThat(adjusted).isEqualTo("listenPort=10911");
103+
104+
String emptyConfig = "";
105+
adjusted = MixAll.adjustConfigForPlatform(emptyConfig);
106+
assertThat(adjusted).isEqualTo("");
107+
108+
adjusted = MixAll.adjustConfigForPlatform(null);
109+
assertThat(adjusted).isNull();
110+
} else {
111+
String configWithSingleBackslash = "/home/rocketmq/conf/broker.conf";
112+
String adjusted = MixAll.adjustConfigForPlatform(configWithSingleBackslash);
113+
assertThat(adjusted).isEqualTo("/home/rocketmq/conf/broker.conf");
114+
115+
String linuxPathWithBackslash = "some\\directory\\file.txt";
116+
adjusted = MixAll.adjustConfigForPlatform(linuxPathWithBackslash);
117+
assertThat(adjusted).isEqualTo("some\\directory\\file.txt");
118+
119+
String emptyConfig = "";
120+
adjusted = MixAll.adjustConfigForPlatform(emptyConfig);
121+
assertThat(adjusted).isEqualTo("");
122+
123+
adjusted = MixAll.adjustConfigForPlatform(null);
124+
assertThat(adjusted).isNull();
125+
}
126+
}
88127
}

container/src/main/java/org/apache/rocketmq/container/BrokerContainerProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ private RemotingCommand getBrokerConfig(ChannelHandlerContext ctx, RemotingComma
291291
String content = this.brokerContainer.getConfiguration().getAllConfigsFormatString();
292292
if (content != null && content.length() > 0) {
293293
try {
294+
content = MixAll.adjustConfigForPlatform(content);
294295
response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET));
295296
} catch (UnsupportedEncodingException e) {
296297
LOGGER.error("", e);

controller/src/main/java/org/apache/rocketmq/controller/processor/ControllerRequestProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ private RemotingCommand handleGetControllerConfig(ChannelHandlerContext ctx, Rem
311311
String content = this.controllerManager.getConfiguration().getAllConfigsFormatString();
312312
if (content != null && content.length() > 0) {
313313
try {
314+
content = MixAll.adjustConfigForPlatform(content);
314315
response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET));
315316
} catch (UnsupportedEncodingException e) {
316317
log.error("getConfig error, ", e);

namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ private RemotingCommand getConfig(ChannelHandlerContext ctx, RemotingCommand req
658658
String content = this.namesrvController.getConfiguration().getAllConfigsFormatString();
659659
if (StringUtils.isNotBlank(content)) {
660660
try {
661+
content = MixAll.adjustConfigForPlatform(content);
661662
response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET));
662663
} catch (UnsupportedEncodingException e) {
663664
log.error("getConfig error, ", e);

0 commit comments

Comments
 (0)