Skip to content

Commit b174983

Browse files
JAMES-3775 ClamAVScan should be able to configure socket timeout when scanning a message (#2778)
1 parent be7bd6d commit b174983

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

docs/modules/servers/partials/ClamAVScan.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ The default is *localhost*.
2727
* *<maxPings>*: the maximum number of connection retries during startup.
2828
If the value is *0* no startup test will be done.
2929
The default is *6*.
30-
* *<pingIntervalMilli>*: the interval (in milliseconds)
30+
* *<pingIntervalMilli>*: the duration e.g. `30s`, `30000ms` (default in the millisecond unit if the unit is not specified)
3131
between each connection retry during startup.
3232
The default is *30000* (30 seconds).
33+
* *<socketTimeout>*: the duration e.g. `5s`, `5000ms` (default in the millisecond unit if the unit is not specified)
34+
for the socket timeout when scanning a message.
35+
The default is *5000* (5 seconds).
3336
* *<streamBufferSize>*: the BufferedOutputStream buffer size to use
3437
writing to the *stream connection*. The default is *8192*.
3538

third-party/clamav/src/main/java/org/apache/james/clamav/ClamAVScan.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.net.UnknownHostException;
3636
import java.nio.ByteBuffer;
3737
import java.nio.charset.StandardCharsets;
38+
import java.time.temporal.ChronoUnit;
3839
import java.util.HashSet;
3940
import java.util.Optional;
4041
import java.util.Set;
@@ -44,6 +45,7 @@
4445

4546
import org.apache.james.server.core.MimeMessageInputStream;
4647
import org.apache.james.util.AuditTrail;
48+
import org.apache.james.util.DurationParser;
4749
import org.apache.mailet.Attribute;
4850
import org.apache.mailet.AttributeName;
4951
import org.apache.mailet.AttributeValue;
@@ -193,6 +195,8 @@ public class ClamAVScan extends GenericMailet {
193195

194196
private static final int DEFAULT_PING_INTERVAL_MILLI = 10000;
195197

198+
private static final int DEFAULT_SOCKET_TIMEOUT_MILLI = 5000;
199+
196200
private static final int DEFAULT_STREAM_BUFFER_SIZE = 8192;
197201

198202
private static final String FOUND_STRING = "FOUND";
@@ -226,6 +230,11 @@ public class ClamAVScan extends GenericMailet {
226230
*/
227231
private int pingIntervalMilli;
228232

233+
/**
234+
* Holds value of property socketTimeout.
235+
*/
236+
private int socketTimeoutMilli;
237+
229238
/**
230239
* Holds value of property streamBufferSize.
231240
*/
@@ -382,13 +391,21 @@ public void setMaxPings(int maxPings) {
382391
* Initializer for property pingIntervalMilli.
383392
*/
384393
protected void initPingIntervalMilli() {
385-
String pingIntervalMilliParam = getInitParameter("pingIntervalMilli");
386-
setPingIntervalMilli((pingIntervalMilliParam == null) ? DEFAULT_PING_INTERVAL_MILLI : Integer.parseInt(pingIntervalMilliParam));
394+
setPingIntervalMilli(Optional.ofNullable(getInitParameter("pingIntervalMilli"))
395+
.map(string -> (int) DurationParser.parse(string, ChronoUnit.MILLIS).toMillis())
396+
.orElse(DEFAULT_PING_INTERVAL_MILLI));
397+
387398
if (isDebug()) {
388399
LOGGER.debug("pingIntervalMilli: {}", getPingIntervalMilli());
389400
}
390401
}
391402

403+
protected void initSocketTimeout() {
404+
this.socketTimeoutMilli = Optional.ofNullable(getInitParameter("socketTimeout"))
405+
.map(string -> (int) DurationParser.parse(string, ChronoUnit.MILLIS).toMillis())
406+
.orElse(DEFAULT_SOCKET_TIMEOUT_MILLI);
407+
}
408+
392409
/**
393410
* Getter for property pingIntervalMilli.
394411
*
@@ -553,6 +570,7 @@ public void init() throws MessagingException {
553570
initPort();
554571
initMaxPings();
555572
initPingIntervalMilli();
573+
initSocketTimeout();
556574
initStreamBufferSize();
557575

558576
// If "maxPings is > ping the CLAMD server to check if it is up
@@ -733,7 +751,7 @@ public boolean hasVirus(InputStream mimeMessage) throws IOException {
733751
try (Socket socket = getClamdSocket();
734752
OutputStream clamAVOutputStream = new BufferedOutputStream(socket.getOutputStream(), getStreamBufferSize());
735753
InputStream clamAvInputStream = socket.getInputStream()) {
736-
socket.setSoTimeout(2000);
754+
socket.setSoTimeout(socketTimeoutMilli);
737755
clamAVOutputStream.write("zINSTREAM\0".getBytes());
738756
clamAVOutputStream.flush();
739757

0 commit comments

Comments
 (0)