This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 25628a8 (chores) fix resource leaks in camel-grape and camel-minio
(#6005)
25628a8 is described below
commit 25628a8919c403b25ce89dd485fe59278b7effd7
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Mon Aug 30 10:56:11 2021 +0200
(chores) fix resource leaks in camel-grape and camel-minio (#6005)
---
.../component/grape/FilePatchesRepository.java | 4 +-
.../camel/component/minio/MinioProducer.java | 103 +++++++++++++--------
2 files changed, 67 insertions(+), 40 deletions(-)
diff --git
a/components/camel-grape/src/main/java/org/apache/camel/component/grape/FilePatchesRepository.java
b/components/camel-grape/src/main/java/org/apache/camel/component/grape/FilePatchesRepository.java
index ed50910..fc75039 100644
---
a/components/camel-grape/src/main/java/org/apache/camel/component/grape/FilePatchesRepository.java
+++
b/components/camel-grape/src/main/java/org/apache/camel/component/grape/FilePatchesRepository.java
@@ -62,8 +62,8 @@ public class FilePatchesRepository implements
PatchesRepository {
@Override
public List<String> listPatches() {
- try {
- return IOUtils.readLines(new FileReader(repository));
+ try (FileReader reader = new FileReader(repository)) {
+ return IOUtils.readLines(reader);
} catch (IOException e) {
throw new IOError(e);
}
diff --git
a/components/camel-minio/src/main/java/org/apache/camel/component/minio/MinioProducer.java
b/components/camel-minio/src/main/java/org/apache/camel/component/minio/MinioProducer.java
index 5ed607a..a0a1c29 100644
---
a/components/camel-minio/src/main/java/org/apache/camel/component/minio/MinioProducer.java
+++
b/components/camel-minio/src/main/java/org/apache/camel/component/minio/MinioProducer.java
@@ -22,6 +22,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -37,6 +39,12 @@ import io.minio.RemoveBucketArgs;
import io.minio.RemoveObjectArgs;
import io.minio.RemoveObjectsArgs;
import io.minio.Result;
+import io.minio.errors.ErrorResponseException;
+import io.minio.errors.InsufficientDataException;
+import io.minio.errors.InternalException;
+import io.minio.errors.InvalidResponseException;
+import io.minio.errors.ServerException;
+import io.minio.errors.XmlParserException;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.apache.camel.Endpoint;
@@ -128,62 +136,81 @@ public class MinioProducer extends DefaultProducer {
Map<String, String> extraHeaders = determineExtraHeaders(exchange);
File filePayload = null;
- InputStream inputStream;
- ByteArrayOutputStream baos;
+
Object object = exchange.getIn().getMandatoryBody();
// Need to check if the message body is WrappedFile
if (object instanceof WrappedFile) {
object = ((WrappedFile<?>) object).getFile();
}
- if (object instanceof File) {
- filePayload = (File) object;
- inputStream = new FileInputStream(filePayload);
- } else {
- inputStream =
exchange.getIn().getMandatoryBody(InputStream.class);
- if (objectMetadata.containsKey(Exchange.CONTENT_LENGTH)) {
- if (objectMetadata.get("Content-Length").equals("0")
- &&
isEmpty(exchange.getProperty(Exchange.CONTENT_LENGTH))) {
- LOG.debug(
- "The content length is not defined. It needs
to be determined by reading the data into memory");
- baos = determineLengthInputStream(inputStream);
- objectMetadata.put("Content-Length",
String.valueOf(baos.size()));
- inputStream = new
ByteArrayInputStream(baos.toByteArray());
- } else {
- if
(isNotEmpty(exchange.getProperty(Exchange.CONTENT_LENGTH))) {
- objectMetadata.put("Content-Length",
exchange.getProperty(Exchange.CONTENT_LENGTH, String.class));
- }
- }
+
+ InputStream inputStream = null;
+ try {
+ if (object instanceof File) {
+ filePayload = (File) object;
+ inputStream = new FileInputStream(filePayload);
+ } else {
+ inputStream = getInputStreamFromExchange(exchange,
objectMetadata);
}
+
+ doPutObject(exchange, bucketName, objectName, objectMetadata,
extraHeaders, inputStream);
+ } finally {
+ IOHelper.close(inputStream);
}
- PutObjectArgs.Builder putObjectRequest = PutObjectArgs.builder()
- .stream(inputStream, inputStream.available(), -1)
- .bucket(bucketName)
- .object(objectName)
- .userMetadata(objectMetadata);
- if (!extraHeaders.isEmpty()) {
- putObjectRequest.extraHeaders(extraHeaders);
+ if (getConfiguration().isDeleteAfterWrite() &&
isNotEmpty(filePayload)) {
+ FileUtil.deleteFile(filePayload);
}
+ }
+ }
- LOG.trace("Put object from exchange...");
+ private void doPutObject(
+ Exchange exchange, String bucketName, String objectName,
Map<String, String> objectMetadata,
+ Map<String, String> extraHeaders, InputStream inputStream)
+ throws IOException, ErrorResponseException,
InsufficientDataException, InternalException, InvalidKeyException,
+ InvalidResponseException, NoSuchAlgorithmException,
ServerException, XmlParserException {
+ PutObjectArgs.Builder putObjectRequest = PutObjectArgs.builder()
+ .stream(inputStream, inputStream.available(), -1)
+ .bucket(bucketName)
+ .object(objectName)
+ .userMetadata(objectMetadata);
+
+ if (!extraHeaders.isEmpty()) {
+ putObjectRequest.extraHeaders(extraHeaders);
+ }
- ObjectWriteResponse putObjectResult =
getEndpoint().getMinioClient().putObject(putObjectRequest.build());
+ LOG.trace("Put object from exchange...");
- LOG.trace("Received result...");
+ ObjectWriteResponse putObjectResult =
getEndpoint().getMinioClient().putObject(putObjectRequest.build());
- Message message = getMessageForResponse(exchange);
- message.setHeader(MinioConstants.E_TAG, putObjectResult.etag());
- if (isNotEmpty(putObjectResult.versionId())) {
- message.setHeader(MinioConstants.VERSION_ID,
putObjectResult.versionId());
- }
+ LOG.trace("Received result...");
- IOHelper.close(inputStream);
+ Message message = getMessageForResponse(exchange);
+ message.setHeader(MinioConstants.E_TAG, putObjectResult.etag());
+ if (isNotEmpty(putObjectResult.versionId())) {
+ message.setHeader(MinioConstants.VERSION_ID,
putObjectResult.versionId());
+ }
+ }
- if (getConfiguration().isDeleteAfterWrite() &&
isNotEmpty(filePayload)) {
- FileUtil.deleteFile(filePayload);
+ private InputStream getInputStreamFromExchange(Exchange exchange,
Map<String, String> objectMetadata)
+ throws InvalidPayloadException, IOException {
+ InputStream inputStream =
exchange.getIn().getMandatoryBody(InputStream.class);
+
+ if (objectMetadata.containsKey(Exchange.CONTENT_LENGTH)) {
+ if (objectMetadata.get("Content-Length").equals("0")
+ && isEmpty(exchange.getProperty(Exchange.CONTENT_LENGTH)))
{
+ LOG.debug(
+ "The content length is not defined. It needs to be
determined by reading the data into memory");
+ ByteArrayOutputStream baos =
determineLengthInputStream(inputStream);
+ objectMetadata.put("Content-Length",
String.valueOf(baos.size()));
+ inputStream = new ByteArrayInputStream(baos.toByteArray());
+ } else {
+ if (isNotEmpty(exchange.getProperty(Exchange.CONTENT_LENGTH)))
{
+ objectMetadata.put("Content-Length",
exchange.getProperty(Exchange.CONTENT_LENGTH, String.class));
+ }
}
}
+ return inputStream;
}
private Map<String, String> determineExtraHeaders(Exchange exchange) {