Skip to content

Commit 65316a7

Browse files
committed
Quote log message for Sling Job
Otherwise you may experience a IllegalArgumentException This closes #845
1 parent d2ae8ce commit 65316a7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/impl/AcInstallationServiceImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public JobExecutionResult process(Job job, JobExecutionContext context) {
230230
boolean isLogVerbose = Boolean.parseBoolean(jobProperties.getOrDefault(JOB_PROPERTY_IS_LOG_VERBOSE, "false").toString());
231231
asyncInstallLog.attachMessageListener((logLevel, message) -> {
232232
if (isLogVerbose || logLevel != InstallationLogLevel.TRACE) {
233-
context.log(logLevel.toString() + ": " + message);
233+
context.log(quoteForMessageFormat(logLevel.toString() + ": " + message));
234234
}
235235
});
236236
asyncInstallLog.attachFinishListener(isSuccess ->
@@ -255,6 +255,14 @@ public JobExecutionResult process(Job job, JobExecutionContext context) {
255255
return result;
256256
}
257257

258+
/**
259+
* Quotes the given string so that it can be safely used as literal argument to {@link java.text.MessageFormat} without being interpreted
260+
* @return the quoted string
261+
*/
262+
static String quoteForMessageFormat(String text) {
263+
return "'" + text.replace("'", "''") + "'";
264+
}
265+
258266
@Override
259267
public boolean attachLogListener(String jobId, BiConsumer<InstallationLogLevel, String> messageListener, Consumer<Boolean> finishListener) {
260268
return false;

accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/impl/AcInstallationServiceImplTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import static org.junit.jupiter.api.Assertions.assertTrue;
2020
import static org.mockito.Mockito.when;
2121

22+
import java.text.MessageFormat;
2223
import java.util.Arrays;
2324
import java.util.HashMap;
2425
import java.util.Iterator;
2526
import java.util.List;
2627
import java.util.Map;
28+
import java.util.function.UnaryOperator;
2729

2830
import javax.jcr.RepositoryException;
2931

@@ -180,4 +182,16 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
180182
AcInstallationServiceImpl acInstallationServiceImpl2 = new AcInstallationServiceImpl();
181183
acInstallationServiceImpl2.process(job, jobContext);
182184
}
185+
186+
@Test
187+
void testQuoteForMessageFormat() {
188+
assertQuotedTextForMessageFormat("dev=d 19:41:29.927: Global DEF Statement: ENV_KEY_MAPPING={dev=d, stage=s, int=i, prod=p} 19:41:29.928: ", AcInstallationServiceImpl::quoteForMessageFormat);
189+
assertQuotedTextForMessageFormat("This is a test' with apostrophe and {0} braces", AcInstallationServiceImpl::quoteForMessageFormat);
190+
}
191+
192+
static void assertQuotedTextForMessageFormat(String text, UnaryOperator<String> quoteFunction) {
193+
String quotedText = quoteFunction.apply(text);
194+
String output = MessageFormat.format(quotedText, new Object[0]);
195+
assertEquals(text, output);
196+
}
183197
}

0 commit comments

Comments
 (0)