|
35 | 35 | import java.net.UnknownHostException; |
36 | 36 | import java.nio.ByteBuffer; |
37 | 37 | import java.nio.charset.StandardCharsets; |
| 38 | +import java.time.temporal.ChronoUnit; |
38 | 39 | import java.util.HashSet; |
39 | 40 | import java.util.Optional; |
40 | 41 | import java.util.Set; |
|
44 | 45 |
|
45 | 46 | import org.apache.james.server.core.MimeMessageInputStream; |
46 | 47 | import org.apache.james.util.AuditTrail; |
| 48 | +import org.apache.james.util.DurationParser; |
47 | 49 | import org.apache.mailet.Attribute; |
48 | 50 | import org.apache.mailet.AttributeName; |
49 | 51 | import org.apache.mailet.AttributeValue; |
@@ -193,6 +195,8 @@ public class ClamAVScan extends GenericMailet { |
193 | 195 |
|
194 | 196 | private static final int DEFAULT_PING_INTERVAL_MILLI = 10000; |
195 | 197 |
|
| 198 | + private static final int DEFAULT_SOCKET_TIMEOUT_MILLI = 5000; |
| 199 | + |
196 | 200 | private static final int DEFAULT_STREAM_BUFFER_SIZE = 8192; |
197 | 201 |
|
198 | 202 | private static final String FOUND_STRING = "FOUND"; |
@@ -226,6 +230,11 @@ public class ClamAVScan extends GenericMailet { |
226 | 230 | */ |
227 | 231 | private int pingIntervalMilli; |
228 | 232 |
|
| 233 | + /** |
| 234 | + * Holds value of property socketTimeout. |
| 235 | + */ |
| 236 | + private int socketTimeoutMilli; |
| 237 | + |
229 | 238 | /** |
230 | 239 | * Holds value of property streamBufferSize. |
231 | 240 | */ |
@@ -382,13 +391,21 @@ public void setMaxPings(int maxPings) { |
382 | 391 | * Initializer for property pingIntervalMilli. |
383 | 392 | */ |
384 | 393 | 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 | + |
387 | 398 | if (isDebug()) { |
388 | 399 | LOGGER.debug("pingIntervalMilli: {}", getPingIntervalMilli()); |
389 | 400 | } |
390 | 401 | } |
391 | 402 |
|
| 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 | + |
392 | 409 | /** |
393 | 410 | * Getter for property pingIntervalMilli. |
394 | 411 | * |
@@ -553,6 +570,7 @@ public void init() throws MessagingException { |
553 | 570 | initPort(); |
554 | 571 | initMaxPings(); |
555 | 572 | initPingIntervalMilli(); |
| 573 | + initSocketTimeout(); |
556 | 574 | initStreamBufferSize(); |
557 | 575 |
|
558 | 576 | // If "maxPings is > ping the CLAMD server to check if it is up |
@@ -733,7 +751,7 @@ public boolean hasVirus(InputStream mimeMessage) throws IOException { |
733 | 751 | try (Socket socket = getClamdSocket(); |
734 | 752 | OutputStream clamAVOutputStream = new BufferedOutputStream(socket.getOutputStream(), getStreamBufferSize()); |
735 | 753 | InputStream clamAvInputStream = socket.getInputStream()) { |
736 | | - socket.setSoTimeout(2000); |
| 754 | + socket.setSoTimeout(socketTimeoutMilli); |
737 | 755 | clamAVOutputStream.write("zINSTREAM\0".getBytes()); |
738 | 756 | clamAVOutputStream.flush(); |
739 | 757 |
|
|
0 commit comments