This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
The following commit(s) were added to refs/heads/master by this push: new b17498323d JAMES-3775 ClamAVScan should be able to configure socket timeout when scanning a message (#2778) b17498323d is described below commit b17498323d947c3ea3028666bf7c05bb7e46f986 Author: Trần Hồng Quân <55171818+quantranhong1...@users.noreply.github.com> AuthorDate: Sat Jul 26 02:54:17 2025 +0700 JAMES-3775 ClamAVScan should be able to configure socket timeout when scanning a message (#2778) --- docs/modules/servers/partials/ClamAVScan.adoc | 5 ++++- .../java/org/apache/james/clamav/ClamAVScan.java | 24 +++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/docs/modules/servers/partials/ClamAVScan.adoc b/docs/modules/servers/partials/ClamAVScan.adoc index 5a6f813ab5..a247c41bb3 100644 --- a/docs/modules/servers/partials/ClamAVScan.adoc +++ b/docs/modules/servers/partials/ClamAVScan.adoc @@ -27,9 +27,12 @@ The default is *localhost*. * *<maxPings>*: the maximum number of connection retries during startup. If the value is *0* no startup test will be done. The default is *6*. -* *<pingIntervalMilli>*: the interval (in milliseconds) +* *<pingIntervalMilli>*: the duration e.g. `30s`, `30000ms` (default in the millisecond unit if the unit is not specified) between each connection retry during startup. The default is *30000* (30 seconds). +* *<socketTimeout>*: the duration e.g. `5s`, `5000ms` (default in the millisecond unit if the unit is not specified) +for the socket timeout when scanning a message. +The default is *5000* (5 seconds). * *<streamBufferSize>*: the BufferedOutputStream buffer size to use writing to the *stream connection*. The default is *8192*. diff --git a/third-party/clamav/src/main/java/org/apache/james/clamav/ClamAVScan.java b/third-party/clamav/src/main/java/org/apache/james/clamav/ClamAVScan.java index 0faa287e48..35d86ac8a6 100644 --- a/third-party/clamav/src/main/java/org/apache/james/clamav/ClamAVScan.java +++ b/third-party/clamav/src/main/java/org/apache/james/clamav/ClamAVScan.java @@ -35,6 +35,7 @@ import java.net.Socket; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import java.time.temporal.ChronoUnit; import java.util.HashSet; import java.util.Optional; import java.util.Set; @@ -44,6 +45,7 @@ import jakarta.mail.internet.MimeMessage; import org.apache.james.server.core.MimeMessageInputStream; import org.apache.james.util.AuditTrail; +import org.apache.james.util.DurationParser; import org.apache.mailet.Attribute; import org.apache.mailet.AttributeName; import org.apache.mailet.AttributeValue; @@ -193,6 +195,8 @@ public class ClamAVScan extends GenericMailet { private static final int DEFAULT_PING_INTERVAL_MILLI = 10000; + private static final int DEFAULT_SOCKET_TIMEOUT_MILLI = 5000; + private static final int DEFAULT_STREAM_BUFFER_SIZE = 8192; private static final String FOUND_STRING = "FOUND"; @@ -226,6 +230,11 @@ public class ClamAVScan extends GenericMailet { */ private int pingIntervalMilli; + /** + * Holds value of property socketTimeout. + */ + private int socketTimeoutMilli; + /** * Holds value of property streamBufferSize. */ @@ -382,13 +391,21 @@ public class ClamAVScan extends GenericMailet { * Initializer for property pingIntervalMilli. */ protected void initPingIntervalMilli() { - String pingIntervalMilliParam = getInitParameter("pingIntervalMilli"); - setPingIntervalMilli((pingIntervalMilliParam == null) ? DEFAULT_PING_INTERVAL_MILLI : Integer.parseInt(pingIntervalMilliParam)); + setPingIntervalMilli(Optional.ofNullable(getInitParameter("pingIntervalMilli")) + .map(string -> (int) DurationParser.parse(string, ChronoUnit.MILLIS).toMillis()) + .orElse(DEFAULT_PING_INTERVAL_MILLI)); + if (isDebug()) { LOGGER.debug("pingIntervalMilli: {}", getPingIntervalMilli()); } } + protected void initSocketTimeout() { + this.socketTimeoutMilli = Optional.ofNullable(getInitParameter("socketTimeout")) + .map(string -> (int) DurationParser.parse(string, ChronoUnit.MILLIS).toMillis()) + .orElse(DEFAULT_SOCKET_TIMEOUT_MILLI); + } + /** * Getter for property pingIntervalMilli. * @@ -553,6 +570,7 @@ public class ClamAVScan extends GenericMailet { initPort(); initMaxPings(); initPingIntervalMilli(); + initSocketTimeout(); initStreamBufferSize(); // If "maxPings is > ping the CLAMD server to check if it is up @@ -733,7 +751,7 @@ public class ClamAVScan extends GenericMailet { try (Socket socket = getClamdSocket(); OutputStream clamAVOutputStream = new BufferedOutputStream(socket.getOutputStream(), getStreamBufferSize()); InputStream clamAvInputStream = socket.getInputStream()) { - socket.setSoTimeout(2000); + socket.setSoTimeout(socketTimeoutMilli); clamAVOutputStream.write("zINSTREAM\0".getBytes()); clamAVOutputStream.flush(); --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org