Skip to content

Commit 47e9954

Browse files
Improve managing <messageML> tag (#858)
1 parent d9a4e4a commit 47e9954

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

  • symphony-bdk-core/src

symphony-bdk-core/src/main/java/com/symphony/bdk/core/service/message/model/Message.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,14 @@ public Message build() {
206206
}
207207

208208
// check if content is encapsulated in <messageML/> node
209-
boolean isAlreadyWrapped = this.content.startsWith("<messageML") && this.content.endsWith("</messageML>");
210-
if (!isAlreadyWrapped) {
209+
String trimmedContent = this.content.trim();
210+
boolean hasStartTag = trimmedContent.startsWith("<messageML");
211+
boolean hasEndTag = trimmedContent.endsWith("</messageML>");
212+
if (hasStartTag != hasEndTag) {
213+
throw new MessageCreationException(
214+
"Malformed <messageML> tag. Missing " + (hasStartTag ? "closing" : "opening") + " tag");
215+
}
216+
if (!hasStartTag) {
211217
log.trace("Processing content to prefix with <messageML> and suffix with </messageML>");
212218
if (Boolean.TRUE.equals(this.beta)) {
213219
this.content = "<messageML beta=\"true\">" + this.content + "</messageML>";
@@ -219,6 +225,9 @@ public Message build() {
219225
throw new MessageCreationException(
220226
"Cannot set beta=true when content is already wrapped with <messageML> without the beta attribute. "
221227
+ "Either remove the wrapper or include beta=\"true\" in your messageML tag.");
228+
} else {
229+
// content is already wrapped, but we can trim if needed
230+
this.content = trimmedContent;
222231
}
223232

224233
// check done below because it will rejected by the agent otherwise
@@ -229,4 +238,15 @@ public Message build() {
229238
return new Message(this);
230239
}
231240
}
241+
242+
private boolean isAlreadyWrapped(String content) {
243+
boolean hasStart = content.startsWith("<messageML>");
244+
boolean hasEnd = content.endsWith("</messageML>");
245+
246+
if (hasStart != hasEnd) {
247+
throw new IllegalStateException("Malformed MessageML: missing " + (hasStart ? "closing" : "opening") + " tag.");
248+
}
249+
250+
return hasStart; // Since they are equal, if hasStart is true, both are true.
251+
}
232252
}

symphony-bdk-core/src/test/java/com/symphony/bdk/core/service/message/model/MessageTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ void checkMessageMLNotAppendedToContentIfSet() {
3131
assertEquals("<messageML>hello</messageML>", Message.builder().content("<messageML>hello</messageML>").build().getContent());
3232
}
3333

34+
@Test
35+
void checkMessageMLNotAppendedToContentIfSetWithSpaces() {
36+
assertEquals("<messageML>hello</messageML>", Message.builder().content(" <messageML>hello</messageML> ").build().getContent());
37+
}
38+
39+
@Test
40+
void checkMessageMLWithoutClosingTagThrowsException() {
41+
MessageCreationException exception = assertThrows(MessageCreationException.class, Message.builder().content("<messageML>hello")::build);
42+
assertEquals("Malformed <messageML> tag. Missing closing tag", exception.getMessage());
43+
}
44+
45+
@Test
46+
void checkMessageMLWithoutOpeningTagThrowsException() {
47+
MessageCreationException exception = assertThrows(MessageCreationException.class, Message.builder().content("hello</messageML>")::build);
48+
assertEquals("Malformed <messageML> tag. Missing opening tag", exception.getMessage());
49+
}
50+
3451
@Test
3552
void checkMessageSilentValueIfSet() {
3653
assertEquals(Boolean.FALSE, Message.builder().content("<messageML>hello</messageML>").silent(Boolean.FALSE).build().getSilent());

0 commit comments

Comments
 (0)