MNT-23108 - Manage contentStream resource closure in RemoteTransformerClient request (#1559)

* Manage contentStream resource closure so we don't have connections hanging when we lose connection to AIO
This commit is contained in:
evasques
2022-11-16 10:14:55 +00:00
committed by GitHub
parent 03f80ace94
commit 783efca1d2

View File

@@ -25,6 +25,10 @@
*/ */
package org.alfresco.repo.content.transform; package org.alfresco.repo.content.transform;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringJoiner;
import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter; import org.alfresco.service.cmr.repository.ContentWriter;
@@ -44,9 +48,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.StringJoiner;
/** /**
* Client class that transfers content (from a ContentReader) to a remote transformation agent together with * Client class that transfers content (from a ContentReader) to a remote transformation agent together with
* request parameters that will be used to transform the content. The transformed content is then returned and * request parameters that will be used to transform the content. The transformed content is then returned and
@@ -88,42 +89,31 @@ public class RemoteTransformerClient
public void request(ContentReader reader, ContentWriter writer, String sourceMimetype, String sourceExtension, public void request(ContentReader reader, ContentWriter writer, String sourceMimetype, String sourceExtension,
String targetExtension, long timeoutMs, Log logger, String... args) String targetExtension, long timeoutMs, Log logger, String... args)
{ {
if (args.length % 2 != 0) if (args.length % 2 != 0)
{ {
throw new IllegalArgumentException("There should be a value for each request property"); throw new IllegalArgumentException("There should be a value for each request property");
} }
StringJoiner sj = new StringJoiner(" "); StringJoiner sj = new StringJoiner(" ");
HttpEntity reqEntity = getRequestEntity(reader, sourceMimetype, sourceExtension, targetExtension, timeoutMs, args, sj);
try (InputStream contentStream = reader.getContentInputStream())
{
HttpEntity reqEntity = getRequestEntity(contentStream, sourceMimetype, sourceExtension, targetExtension, timeoutMs,
args, sj);
request(logger, sourceExtension, targetExtension, reqEntity, writer, sj.toString()); request(logger, sourceExtension, targetExtension, reqEntity, writer, sj.toString());
} }
catch (IOException e)
HttpEntity getRequestEntity(ContentReader reader, String sourceMimetype, String sourceExtension,
String targetExtension, long timeoutMs, String[] args, StringJoiner sj)
{ {
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); throw new AlfrescoRuntimeException("Failed to read content from reader", e);
ContentType contentType = ContentType.create(sourceMimetype);
builder.addBinaryBody("file", reader.getContentInputStream(), contentType, "tmp."+sourceExtension);
builder.addTextBody("targetExtension", targetExtension);
sj.add("targetExtension" + '=' + targetExtension);
for (int i=0; i< args.length; i+=2)
{
if (args[i+1] != null)
{
builder.addTextBody(args[i], args[i + 1]);
sj.add(args[i] + '=' + args[i + 1]);
} }
} }
if (timeoutMs > 0) HttpEntity getRequestEntity(ContentReader reader, String sourceMimetype, String sourceExtension, String targetExtension,
long timeoutMs, String[] args, StringJoiner sj)
{ {
String timeoutMsString = Long.toString(timeoutMs); return getRequestEntity(reader.getContentInputStream(), sourceMimetype, sourceExtension, targetExtension, timeoutMs, args, sj);
builder.addTextBody("timeout", timeoutMsString);
sj.add("timeout=" + timeoutMsString);
}
return builder.build();
} }
void request(Log logger, String sourceExtension, String targetExtension, HttpEntity reqEntity, ContentWriter writer, String args) void request(Log logger, String sourceExtension, String targetExtension, HttpEntity reqEntity, ContentWriter writer, String args)
@@ -331,6 +321,33 @@ public class RemoteTransformerClient
return httpclient.execute(httpGet); return httpclient.execute(httpGet);
} }
private HttpEntity getRequestEntity(InputStream contentStream, String sourceMimetype, String sourceExtension,
String targetExtension, long timeoutMs, String[] args, StringJoiner sj)
{
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
ContentType contentType = ContentType.create(sourceMimetype);
builder.addBinaryBody("file", contentStream, contentType, "tmp." + sourceExtension);
builder.addTextBody("targetExtension", targetExtension);
sj.add("targetExtension" + '=' + targetExtension);
for (int i = 0; i < args.length; i += 2)
{
if (args[i + 1] != null)
{
builder.addTextBody(args[i], args[i + 1]);
sj.add(args[i] + '=' + args[i + 1]);
}
}
if (timeoutMs > 0)
{
String timeoutMsString = Long.toString(timeoutMs);
builder.addTextBody("timeout", timeoutMsString);
sj.add("timeout=" + timeoutMsString);
}
return builder.build();
}
// Strip out just the error message in the response // Strip out just the error message in the response
private String getErrorMessage(HttpEntity resEntity) throws IOException private String getErrorMessage(HttpEntity resEntity) throws IOException
{ {