GH-3238: Fix Unmarshaller to close File resource

Fixes https://github.com/spring-projects/spring-integration/issues/3238

* Extract an `InputStream` from a `File` payload in the `UnmarshallingTransformer`
before parsing an XML.
Close this `InputStream` in the `finally` block to release the file resource

**Cherry-pick to 5.2.x, 5.1.x & 4.3.x**
This commit is contained in:
Artem Bilan
2020-04-02 13:04:15 -04:00
parent 79f647990e
commit 24e6be6f01
2 changed files with 65 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,9 @@ package org.springframework.integration.xml.transformer;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import javax.xml.transform.Source;
@@ -37,11 +39,10 @@ import org.springframework.xml.transform.StringSource;
/**
* An implementation of {@link org.springframework.integration.transformer.Transformer}
* that delegates to an OXM
* {@link Unmarshaller}. Expects the payload to be of type {@link Document},
* {@link String}, {@link File}, {@link Source} or to have an instance of
* {@link SourceFactory} that can convert to a {@link Source}. If
* {@link #alwaysUseSourceFactory} is set to true, then the {@link SourceFactory}
* that delegates to an OXM {@link Unmarshaller}.
* Expects the payload to be of type {@link Document}, {@link String}, {@link File}, {@link Source}
* or to have an instance of {@link SourceFactory} that can convert to a {@link Source}.
* If {@link #alwaysUseSourceFactory} is set to true, then the {@link SourceFactory}
* will be used to create the {@link Source} regardless of payload type.
* <p>
* The {@link #alwaysUseSourceFactory} is ignored if payload is
@@ -97,6 +98,7 @@ public class UnmarshallingTransformer extends AbstractPayloadTransformer<Object,
@Override
public Object transformPayload(Object payload) {
Source source;
InputStream inputStream = null;
try {
if (this.mimeMessageUnmarshallerHelper != null) {
Object result = this.mimeMessageUnmarshallerHelper.maybeUnmarshalMimeMessage(payload);
@@ -115,7 +117,9 @@ public class UnmarshallingTransformer extends AbstractPayloadTransformer<Object,
source = new StreamSource(new ByteArrayInputStream((byte[]) payload));
}
else if (payload instanceof File) {
source = new StreamSource((File) payload);
File file = (File) payload;
inputStream = new FileInputStream(file);
source = new StreamSource(inputStream, file.toURI().toASCIIString());
}
else if (payload instanceof Document) {
source = new DOMSource((Document) payload);
@@ -135,6 +139,16 @@ public class UnmarshallingTransformer extends AbstractPayloadTransformer<Object,
catch (IOException e) {
throw new UncheckedIOException("failed to unmarshal payload", e);
}
finally {
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
// Ignore
}
}
}
}
private static class MimeMessageUnmarshallerHelper {