Skip to content

Commit 2b73c70

Browse files
committed
Fix SMTP env-first config and InboxService missing-row crash (v2.7.3)
EmailUtil: port and from-address now resolve env vars first (DOCS_SMTP_PORT, DOCS_SMTP_FROM) with safe DB fallback — missing t_config rows no longer throw IllegalStateException. Hostname fallback also made null-safe. InboxService: all getConfigBooleanValue calls now use the default overload (false), preventing the recurring TransactionRequiredException when INBOX_ENABLED/DELETE_IMPORTED/AUTOMATIC_TAGS rows are absent. ConfigUtil: added getConfigStringValue and getConfigIntegerValue overloads with default values, following the existing boolean pattern. 5 new tests in TestConfigUtil verify missing-row fallback behavior.
1 parent 9016150 commit 2b73c70

9 files changed

Lines changed: 112 additions & 20 deletions

File tree

docs-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.sismics.docs</groupId>
77
<artifactId>docs-parent</artifactId>
8-
<version>2.7.2</version>
8+
<version>2.7.3</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

docs-core/src/main/java/com/sismics/docs/core/constant/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class Constants {
6262
public static final String SMTP_PORT_ENV = "DOCS_SMTP_PORT";
6363
public static final String SMTP_USERNAME_ENV = "DOCS_SMTP_USERNAME";
6464
public static final String SMTP_PASSWORD_ENV = "DOCS_SMTP_PASSWORD";
65+
public static final String SMTP_FROM_ENV = "DOCS_SMTP_FROM";
6566

6667
/**
6768
* Global quota environment variable.

docs-core/src/main/java/com/sismics/docs/core/service/InboxService.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected void runOneIteration() {
8585
*/
8686
public void syncInbox() {
8787
TransactionUtil.handle(() -> {
88-
Boolean enabled = ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_ENABLED);
88+
boolean enabled = ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_ENABLED, false);
8989
if (!enabled) {
9090
return;
9191
}
@@ -114,8 +114,7 @@ public void syncInbox() {
114114
} finally {
115115
try {
116116
if (inbox != null) {
117-
// The parameter controls if the messages flagged to be deleted, should actually get deleted.
118-
inbox.close(ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_DELETE_IMPORTED));
117+
inbox.close(ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_DELETE_IMPORTED, false));
119118
inbox.getStore().close();
120119
}
121120
} catch (Exception e) {
@@ -131,7 +130,7 @@ public void syncInbox() {
131130
* @return Number of messages currently in the remote inbox
132131
*/
133132
public int testInbox() {
134-
Boolean enabled = ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_ENABLED);
133+
boolean enabled = ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_ENABLED, false);
135134
if (!enabled) {
136135
return -1;
137136
}
@@ -293,7 +292,7 @@ private void importMessage(Message message, Map<String, String> tags,InternetAdd
293292
document.getLanguage(), "admin", document.getId());
294293
}
295294

296-
if (ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_DELETE_IMPORTED)) {
295+
if (ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_DELETE_IMPORTED, false)) {
297296
message.setFlag(Flags.Flag.DELETED, true);
298297
}
299298
}
@@ -304,7 +303,7 @@ private void importMessage(Message message, Map<String, String> tags,InternetAdd
304303
* @return Map with all tags or null if not enabled
305304
*/
306305
private Map<String, String> getAllTags() {
307-
if (!ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_AUTOMATIC_TAGS)) {
306+
if (!ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_AUTOMATIC_TAGS, false)) {
308307
return null;
309308
}
310309
TagDao tagDao = new TagDao();

docs-core/src/main/java/com/sismics/docs/core/util/ConfigUtil.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,36 @@ public static long getConfigLongValue(ConfigType configType) {
6262
return Long.parseLong(value);
6363
}
6464

65+
/**
66+
* Returns the textual value of a configuration parameter with a default value.
67+
*
68+
* @param configType Type of the configuration parameter
69+
* @param defaultValue Default value to return if the configuration parameter is undefined
70+
* @return Textual value of the configuration parameter
71+
*/
72+
public static String getConfigStringValue(ConfigType configType, String defaultValue) {
73+
try {
74+
return getConfigStringValue(configType);
75+
} catch (IllegalStateException e) {
76+
return defaultValue;
77+
}
78+
}
79+
80+
/**
81+
* Returns the integer value of a configuration parameter with a default value.
82+
*
83+
* @param configType Type of the configuration parameter
84+
* @param defaultValue Default value to return if the configuration parameter is undefined
85+
* @return Integer value of the configuration parameter
86+
*/
87+
public static int getConfigIntegerValue(ConfigType configType, int defaultValue) {
88+
try {
89+
return getConfigIntegerValue(configType);
90+
} catch (IllegalStateException e) {
91+
return defaultValue;
92+
}
93+
}
94+
6595
/**
6696
* Returns the boolean value of a configuration parameter.
6797
*

docs-core/src/main/java/com/sismics/util/EmailUtil.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.sismics.docs.core.dao.dto.UserDto;
99
import com.sismics.docs.core.model.context.AppContext;
1010
import com.sismics.docs.core.model.jpa.Config;
11-
import com.sismics.docs.core.util.ConfigUtil;
1211
import freemarker.template.Configuration;
1312
import freemarker.template.DefaultObjectWrapperBuilder;
1413
import freemarker.template.Template;
@@ -91,19 +90,27 @@ private static void sendEmail(String templateName, UserDto recipientUser, String
9190
email.setCharset(StandardCharsets.UTF_8.name());
9291
ConfigDao configDao = new ConfigDao();
9392

94-
// Hostname
93+
// Hostname: env first, then DB
9594
String envHostname = System.getenv(Constants.SMTP_HOSTNAME_ENV);
96-
if (Strings.isNullOrEmpty(envHostname)) {
97-
email.setHostName(ConfigUtil.getConfigStringValue(ConfigType.SMTP_HOSTNAME));
98-
} else {
95+
if (!Strings.isNullOrEmpty(envHostname)) {
9996
email.setHostName(envHostname);
97+
} else {
98+
Config hostnameConfig = configDao.getById(ConfigType.SMTP_HOSTNAME);
99+
if (hostnameConfig != null) {
100+
email.setHostName(hostnameConfig.getValue());
101+
}
100102
}
101103

102-
// Port
103-
int port = ConfigUtil.getConfigIntegerValue(ConfigType.SMTP_PORT);
104+
// Port: env first, then DB, default 587
105+
int port = 587;
104106
String envPort = System.getenv(Constants.SMTP_PORT_ENV);
105107
if (!Strings.isNullOrEmpty(envPort)) {
106-
port = Integer.valueOf(envPort);
108+
port = Integer.parseInt(envPort);
109+
} else {
110+
Config portConfig = configDao.getById(ConfigType.SMTP_PORT);
111+
if (portConfig != null) {
112+
port = Integer.parseInt(portConfig.getValue());
113+
}
107114
}
108115
email.setSmtpPort(port);
109116
if (port == 465) {
@@ -138,8 +145,16 @@ private static void sendEmail(String templateName, UserDto recipientUser, String
138145
}
139146
}
140147

141-
// From email address (defined only by configuration value in the database)
142-
email.setFrom(ConfigUtil.getConfigStringValue(ConfigType.SMTP_FROM), appName);
148+
// From email address: env first, then DB
149+
String envFrom = System.getenv(Constants.SMTP_FROM_ENV);
150+
if (!Strings.isNullOrEmpty(envFrom)) {
151+
email.setFrom(envFrom, appName);
152+
} else {
153+
Config fromConfig = configDao.getById(ConfigType.SMTP_FROM);
154+
if (fromConfig != null) {
155+
email.setFrom(fromConfig.getValue(), appName);
156+
}
157+
}
143158

144159
// Locale (defined only by environment variable)
145160
java.util.Locale userLocale = LocaleUtil.getLocale(System.getenv(Constants.DEFAULT_LANGUAGE_ENV));
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.sismics.docs.core.util;
2+
3+
import com.sismics.docs.BaseTransactionalTest;
4+
import com.sismics.docs.core.constant.ConfigType;
5+
import com.sismics.docs.core.dao.ConfigDao;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* Tests for ConfigUtil safe fallback overloads and ConfigDao null behavior.
11+
* Verifies that missing t_config rows do not throw, which is the fix for
12+
* EmailUtil SMTP env-based config and InboxService startup errors.
13+
*/
14+
public class TestConfigUtil extends BaseTransactionalTest {
15+
16+
@Test
17+
public void testGetConfigStringValueThrowsWhenMissing() {
18+
Assertions.assertThrows(IllegalStateException.class, () ->
19+
ConfigUtil.getConfigStringValue(ConfigType.SMTP_PORT));
20+
}
21+
22+
@Test
23+
public void testGetConfigStringValueWithDefault() {
24+
String value = ConfigUtil.getConfigStringValue(ConfigType.SMTP_PORT, "587");
25+
Assertions.assertEquals("587", value);
26+
}
27+
28+
@Test
29+
public void testGetConfigIntegerValueWithDefault() {
30+
int value = ConfigUtil.getConfigIntegerValue(ConfigType.SMTP_PORT, 587);
31+
Assertions.assertEquals(587, value);
32+
}
33+
34+
@Test
35+
public void testGetConfigBooleanValueWithDefault() {
36+
boolean value = ConfigUtil.getConfigBooleanValue(ConfigType.INBOX_ENABLED, false);
37+
Assertions.assertFalse(value);
38+
}
39+
40+
@Test
41+
public void testConfigDaoGetByIdReturnsNullWhenMissing() {
42+
ConfigDao configDao = new ConfigDao();
43+
Assertions.assertNull(configDao.getById(ConfigType.SMTP_PORT));
44+
Assertions.assertNull(configDao.getById(ConfigType.SMTP_FROM));
45+
Assertions.assertNull(configDao.getById(ConfigType.SMTP_HOSTNAME));
46+
}
47+
}

docs-web-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.sismics.docs</groupId>
77
<artifactId>docs-parent</artifactId>
8-
<version>2.7.2</version>
8+
<version>2.7.3</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

docs-web/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.sismics.docs</groupId>
77
<artifactId>docs-parent</artifactId>
8-
<version>2.7.2</version>
8+
<version>2.7.3</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>com.sismics.docs</groupId>
77
<artifactId>docs-parent</artifactId>
88
<packaging>pom</packaging>
9-
<version>2.7.2</version>
9+
<version>2.7.3</version>
1010

1111
<name>Docs Parent</name>
1212

0 commit comments

Comments
 (0)