Skip to content

Commit a3f09e4

Browse files
committed
Add a content version for configuration map entries
ConfigurationProperty.versionOf returns the lowercase hex SHA-256 of an entry's value, a unit separator (U+001F), and its comment, with a null value and a null or blank comment treated as empty, and the empty string for a missing entry. It depends only on the entry's contents, so a client can calculate it from what the server returned and send it back as a precondition without the API storing or returning a revision. The unit separator is used instead of NUL because some clients can't easily hold a NUL in a string, such as C strings and shell variables. It is a static method rather than a getter so that it doesn't show up as a property of ConfigurationProperty in the OpenAPI schema. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Tony Germano <tony@germano.name>
1 parent d966f8c commit a3f09e4

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

‎server/src/main/java/com/mirth/connect/util/ConfigurationProperty.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
package com.mirth.connect.util;
1111

1212
import java.io.Serializable;
13+
import java.nio.charset.StandardCharsets;
14+
import java.security.MessageDigest;
15+
import java.security.NoSuchAlgorithmException;
16+
import java.util.HexFormat;
1317

1418
public class ConfigurationProperty implements Serializable {
1519

@@ -37,6 +41,39 @@ public void setComment(String comment) {
3741
this.comment = comment;
3842
}
3943

44+
/**
45+
* Returns the version of a configuration map entry, for use as the expected version when
46+
* updating that entry. The version depends only on the entry's contents: it is the lowercase
47+
* hex SHA-256 of the UTF-8 bytes of the value, a unit separator (U+001F), and the comment. A null value
48+
* and a null or blank comment are treated as empty strings. A missing entry has the empty
49+
* string as its version.
50+
*
51+
* The version is calculated from what the server currently holds, so a client can calculate it
52+
* from the entries returned by the server. Saving the map to a file can change some values and
53+
* comments when the server next starts (for example by trimming whitespace), so a version
54+
* calculated before a restart may no longer match.
55+
*
56+
* @param property
57+
* The entry, or null if there is no entry for the key.
58+
* @return The entry's version.
59+
*/
60+
public static String versionOf(ConfigurationProperty property) {
61+
if (property == null) {
62+
return "";
63+
}
64+
65+
String value = property.getValue() == null ? "" : property.getValue();
66+
String comment = property.getComment() == null || property.getComment().isBlank() ? "" : property.getComment();
67+
68+
try {
69+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
70+
return HexFormat.of().formatHex(digest.digest((value + '\u001F' + comment).getBytes(StandardCharsets.UTF_8)));
71+
} catch (NoSuchAlgorithmException e) {
72+
// Every Java platform is required to support SHA-256
73+
throw new IllegalStateException(e);
74+
}
75+
}
76+
4077
@Override
4178
public String toString() {
4279
StringBuilder builder = new StringBuilder();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: MPL-2.0
2+
// SPDX-FileCopyrightText: 2026 Tony Germano <tony@germano.name>
3+
4+
package com.mirth.connect.util;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNotEquals;
8+
9+
import org.junit.Test;
10+
11+
public class ConfigurationPropertyTest {
12+
13+
// Expected values calculated with: printf '%s\037%s' '<value>' '<comment>' | sha256sum
14+
15+
@Test
16+
public void versionOf_ValueAndComment() {
17+
assertEquals("c1dcc868b7a3de969e0d671b3fce131d00d46d3da2a79805d91774ccfabaa8de", ConfigurationProperty.versionOf(new ConfigurationProperty("1", "comment")));
18+
}
19+
20+
@Test
21+
public void versionOf_NonAscii() {
22+
assertEquals("b736f6189175d479edd12794d68abfdc2832ca4c2e3d5c87cc9e9ebe5c08a131", ConfigurationProperty.versionOf(new ConfigurationProperty("é", "✓")));
23+
}
24+
25+
@Test
26+
public void versionOf_NullAndBlankCommentsAreEmpty() {
27+
String expected = "2b8d9cc6944cb101828f20db1b3c405182192eaf2800cf6687ae528349ec1d73";
28+
assertEquals(expected, ConfigurationProperty.versionOf(new ConfigurationProperty("1", null)));
29+
assertEquals(expected, ConfigurationProperty.versionOf(new ConfigurationProperty("1", "")));
30+
assertEquals(expected, ConfigurationProperty.versionOf(new ConfigurationProperty("1", " \n")));
31+
}
32+
33+
@Test
34+
public void versionOf_NullValueIsEmpty() {
35+
String expected = "ffe679bb831c95b67dc17819c63c5090d221aac6f4c7bf530f594ab43d21fa1e";
36+
assertEquals(expected, ConfigurationProperty.versionOf(new ConfigurationProperty(null, null)));
37+
assertEquals(expected, ConfigurationProperty.versionOf(new ConfigurationProperty("", null)));
38+
}
39+
40+
@Test
41+
public void versionOf_MissingEntryIsEmptyString() {
42+
assertEquals("", ConfigurationProperty.versionOf(null));
43+
}
44+
45+
@Test
46+
public void versionOf_ValueAndCommentAreSeparated() {
47+
assertNotEquals(ConfigurationProperty.versionOf(new ConfigurationProperty("ab", null)), ConfigurationProperty.versionOf(new ConfigurationProperty("a", "b")));
48+
}
49+
}

0 commit comments

Comments
 (0)