sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From o...@apache.org
Subject [sling-org-apache-sling-commons-clam] 01/02: SLING-8453 Add size of sent bytes to ScanResult
Date Fri, 31 May 2019 18:49:42 GMT
This is an automated email from the ASF dual-hosted git repository.

olli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-clam.git

commit b58911c92e1d989fae0f9de47c32dfa6f828b8df
Author: Oliver Lietz <olli@apache.org>
AuthorDate: Fri May 31 17:13:35 2019 +0200

    SLING-8453 Add size of sent bytes to ScanResult
---
 .../org/apache/sling/commons/clam/ScanResult.java  |  9 +++++++-
 .../sling/commons/clam/internal/ClamdService.java  | 26 +++++++++++-----------
 .../InstreamSizeLimitExceededException.java        | 16 ++++++++++++-
 3 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/clam/ScanResult.java b/src/main/java/org/apache/sling/commons/clam/ScanResult.java
index b2dbe58..27c9cad 100644
--- a/src/main/java/org/apache/sling/commons/clam/ScanResult.java
+++ b/src/main/java/org/apache/sling/commons/clam/ScanResult.java
@@ -32,10 +32,13 @@ public class ScanResult {
 
     private final long started;
 
-    public ScanResult(@NotNull final Status status, @NotNull final String message, long started)
{
+    private final long size;
+
+    public ScanResult(@NotNull final Status status, @NotNull final String message, long started,
long size) {
         this.status = status;
         this.message = message;
         this.started = started;
+        this.size = size;
     }
 
     public long getTimestamp() {
@@ -56,6 +59,10 @@ public class ScanResult {
         return started;
     }
 
+    public long getSize() {
+        return size;
+    }
+
     public boolean isOk() {
         return Status.OK.equals(status);
     }
diff --git a/src/main/java/org/apache/sling/commons/clam/internal/ClamdService.java b/src/main/java/org/apache/sling/commons/clam/internal/ClamdService.java
index f17f385..78cd189 100644
--- a/src/main/java/org/apache/sling/commons/clam/internal/ClamdService.java
+++ b/src/main/java/org/apache/sling/commons/clam/internal/ClamdService.java
@@ -96,13 +96,11 @@ public class ClamdService implements ClamService {
     @Override
     @NotNull
     public ScanResult scan(@NotNull final InputStream inputStream) throws IOException {
-        final long started = System.currentTimeMillis();
         try {
-            final byte[] reply = doInstream(inputStream);
-            return parseClamdReply(reply, started);
+            return doInstream(inputStream);
         } catch (InstreamSizeLimitExceededException e) {
             logger.error("doing INSTREAM failed", e);
-            return new ScanResult(ScanResult.Status.ERROR, e.getMessage(), started);
+            return new ScanResult(ScanResult.Status.ERROR, e.getMessage(), e.getStarted(),
e.getSize());
         }
     }
 
@@ -149,10 +147,11 @@ public class ClamdService implements ClamService {
      * limit exceeded and close the connection.
      *
      * @param inputStream data sent to clamd in chunks
-     * @return reply from clamd
+     * @return scan result from clamd
      */
-    private byte[] doInstream(final InputStream inputStream) throws IOException, InstreamSizeLimitExceededException
{
+    private ScanResult doInstream(final InputStream inputStream) throws IOException, InstreamSizeLimitExceededException
{
         logger.info("connecting to clam daemon at {}:{} for scanning", configuration.clamd_host(),
configuration.clamd_port());
+        final long started = System.currentTimeMillis();
         try (final Socket socket = new Socket(configuration.clamd_host(), configuration.clamd_port());
              final OutputStream out = new BufferedOutputStream(socket.getOutputStream());
              final InputStream in = socket.getInputStream()) {
@@ -179,7 +178,7 @@ public class ClamdService implements ClamService {
                 if (in.available() > 0) {
                     logger.info("total bytes sent: {}", total);
                     final byte[] reply = IOUtils.toByteArray(in);
-                    throw new InstreamSizeLimitExceededException(reply);
+                    throw new InstreamSizeLimitExceededException(reply, started, total);
                 }
 
                 read = inputStream.read(data);
@@ -192,21 +191,22 @@ public class ClamdService implements ClamService {
             out.flush();
 
             // return reply on complete
-            return IOUtils.toByteArray(in);
+            final byte[] reply = IOUtils.toByteArray(in);
+            return parseClamdReply(reply, started, total);
         }
     }
 
-    private ScanResult parseClamdReply(final byte[] reply, final long started) {
+    private ScanResult parseClamdReply(final byte[] reply, final long started, final long
size) {
         final String message = new String(reply, StandardCharsets.US_ASCII).trim();
         logger.info("reply message from clam daemon: '{}'", message);
         if (message.matches(OK_REPLY_PATTERN)) {
-            return new ScanResult(Status.OK, message, started);
+            return new ScanResult(Status.OK, message, started, size);
         } else if (message.matches(FOUND_REPLY_PATTERN)) {
-            return new ScanResult(Status.FOUND, message, started);
+            return new ScanResult(Status.FOUND, message, started, size);
         } else if (message.matches(INSTREAM_SIZE_LIMIT_EXCEEDED_PATTERN)) {
-            return new ScanResult(Status.ERROR, message, started);
+            return new ScanResult(Status.ERROR, message, started, size);
         } else {
-            return new ScanResult(Status.UNKNOWN, message, started);
+            return new ScanResult(Status.UNKNOWN, message, started, size);
         }
     }
 
diff --git a/src/main/java/org/apache/sling/commons/clam/internal/InstreamSizeLimitExceededException.java
b/src/main/java/org/apache/sling/commons/clam/internal/InstreamSizeLimitExceededException.java
index ec7aeef..afdf95f 100644
--- a/src/main/java/org/apache/sling/commons/clam/internal/InstreamSizeLimitExceededException.java
+++ b/src/main/java/org/apache/sling/commons/clam/internal/InstreamSizeLimitExceededException.java
@@ -22,8 +22,22 @@ import java.nio.charset.StandardCharsets;
 
 class InstreamSizeLimitExceededException extends Exception {
 
-    InstreamSizeLimitExceededException(final byte[] reply) {
+    private final long started;
+
+    private final long size;
+
+    InstreamSizeLimitExceededException(final byte[] reply, long started, long size) {
         super(new String(reply, StandardCharsets.US_ASCII).trim());
+        this.started = started;
+        this.size = size;
+    }
+
+    long getStarted() {
+        return started;
+    }
+
+    long getSize() {
+        return size;
     }
 
 }


Mime
View raw message