From commits-return-80159-archive-asf-public=cust-asf.ponee.io@sling.apache.org Fri May 31 18:49:42 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 4610F180638 for ; Fri, 31 May 2019 20:49:42 +0200 (CEST) Received: (qmail 95668 invoked by uid 500); 31 May 2019 18:49:41 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 95639 invoked by uid 99); 31 May 2019 18:49:41 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 May 2019 18:49:41 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 54E6D8A55B; Fri, 31 May 2019 18:49:41 +0000 (UTC) Date: Fri, 31 May 2019 18:49:42 +0000 To: "commits@sling.apache.org" Subject: [sling-org-apache-sling-commons-clam] 01/02: SLING-8453 Add size of sent bytes to ScanResult MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: olli@apache.org In-Reply-To: <155932858128.21786.8132390531237064193@gitbox.apache.org> References: <155932858128.21786.8132390531237064193@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: sling-org-apache-sling-commons-clam X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Rev: b58911c92e1d989fae0f9de47c32dfa6f828b8df X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20190531184941.54E6D8A55B@gitbox.apache.org> 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 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; } }