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
commit 45f64986f79a850ca53a0cf175b8eb2262981f0a Author: Benoit Tellier <[email protected]> AuthorDate: Thu Nov 25 14:22:16 2021 +0700 JAMES-3676 Hunt some missing InputStream.close Method: Systematic code review using IDE inspection. Might have missed some... --- .../src/main/java/org/apache/james/blob/api/Store.java | 4 ++-- .../james/blob/export/file/LocalFileBlobExportMechanism.java | 5 ++++- .../scala/org/apache/james/jmap/method/MDNParseMethod.scala | 12 +++++++----- .../apache/james/linshare/LinshareBlobExportMechanism.java | 5 +++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/server/blob/blob-common/src/main/java/org/apache/james/blob/api/Store.java b/server/blob/blob-common/src/main/java/org/apache/james/blob/api/Store.java index 385719c..f987fef 100644 --- a/server/blob/blob-common/src/main/java/org/apache/james/blob/api/Store.java +++ b/server/blob/blob-common/src/main/java/org/apache/james/blob/api/Store.java @@ -110,8 +110,8 @@ public interface Store<T, I> { private CloseableByteSource readByteSource(BucketName bucketName, BlobId blobId, StoragePolicy storagePolicy) { FileBackedOutputStream out = new FileBackedOutputStream(FILE_THRESHOLD); - try { - blobStore.read(bucketName, blobId, storagePolicy).transferTo(out); + try (InputStream in = blobStore.read(bucketName, blobId, storagePolicy)) { + in.transferTo(out); return new DelegateCloseableByteSource(out.asByteSource(), out::reset); } catch (IOException e) { throw new RuntimeException(e); diff --git a/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java b/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java index 4925775..683a73f 100644 --- a/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java +++ b/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java @@ -23,6 +23,7 @@ import static org.apache.james.blob.api.BlobStore.StoragePolicy.LOW_COST; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.UnknownHostException; import java.util.Objects; import java.util.Optional; @@ -124,7 +125,9 @@ public class LocalFileBlobExportMechanism implements BlobExportMechanism { String fileName = ExportedFileNamesGenerator.generateFileName(fileCustomPrefix, blobId, fileExtension); String fileURL = configuration.exportDirectory + "/" + fileName; File file = fileSystem.getFile(fileURL); - FileUtils.copyToFile(blobStore.read(blobStore.getDefaultBucketName(), blobId, LOW_COST), file); + try (InputStream in = blobStore.read(blobStore.getDefaultBucketName(), blobId, LOW_COST)) { + FileUtils.copyToFile(in, file); + } return file.getAbsolutePath(); } catch (IOException e) { diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MDNParseMethod.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MDNParseMethod.scala index d0e52c5..bc194af 100644 --- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MDNParseMethod.scala +++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MDNParseMethod.scala @@ -40,7 +40,7 @@ import play.api.libs.json.{JsError, JsObject, JsSuccess, Json} import reactor.core.scala.publisher.{SFlux, SMono} import scala.jdk.OptionConverters._ -import scala.util.Try +import scala.util.{Try, Using} class MDNParseMethod @Inject()(serializer: MDNSerializer, val blobResolvers: BlobResolvers, @@ -92,10 +92,12 @@ class MDNParseMethod @Inject()(serializer: MDNSerializer, private def toParseResults(blobId: BlobId, mailboxSession: MailboxSession): SMono[MDNParseResults] = blobResolvers.resolve(blobId, mailboxSession) - .flatMap(blob => parse(blob.blobId, blob.content) - .flatMap { - case (mdn, message) => buildMDNParseResults(blobId, mdn, message, mailboxSession) - }) + .flatMap(blob => Using(blob.content) { + parse(blob.blobId, _) + }.fold(e => SMono.error[(MDN, Message)](e), result => result)) + .flatMap { + case (mdn, message) => buildMDNParseResults(blobId, mdn, message, mailboxSession) + } .onErrorResume { case e: BlobNotFoundException => SMono.just(MDNParseResults.notFound(e.blobId)) case e: BlobUnParsableException => SMono.just(MDNParseResults.notParse(e.blobId)) diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java index 7f39129..b99e3a5 100644 --- a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java +++ b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java @@ -23,6 +23,7 @@ import static org.apache.james.blob.api.BlobStore.StoragePolicy.LOW_COST; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.Optional; import javax.inject.Inject; @@ -68,8 +69,8 @@ public class LinshareBlobExportMechanism implements BlobExportMechanism { Optional<FileExtension> fileExtension) throws IOException { String fileName = ExportedFileNamesGenerator.generateFileName(fileCustomPrefix, blobId, fileExtension); File tempFile = new File(tempDir, fileName); - try { - FileUtils.copyInputStreamToFile(blobStore.read(blobStore.getDefaultBucketName(), blobId, LOW_COST), tempFile); + try (InputStream in = blobStore.read(blobStore.getDefaultBucketName(), blobId, LOW_COST)) { + FileUtils.copyInputStreamToFile(in, tempFile); uploadDocumentToTargetMail(mailAddress, tempFile); } finally { FileUtils.forceDelete(tempFile); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
