mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
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:
@@ -25,6 +25,10 @@
|
||||
*/
|
||||
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.service.cmr.repository.ContentReader;
|
||||
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.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
|
||||
* request parameters that will be used to transform the content. The transformed content is then returned and
|
||||
@@ -86,44 +87,33 @@ public class RemoteTransformerClient
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
throw new IllegalArgumentException("There should be a value for each request property");
|
||||
}
|
||||
|
||||
StringJoiner sj = new StringJoiner(" ");
|
||||
HttpEntity reqEntity = getRequestEntity(reader, sourceMimetype, sourceExtension, targetExtension, timeoutMs, args, sj);
|
||||
|
||||
request(logger, sourceExtension, targetExtension, reqEntity, writer, sj.toString());
|
||||
try (InputStream contentStream = reader.getContentInputStream())
|
||||
{
|
||||
HttpEntity reqEntity = getRequestEntity(contentStream, sourceMimetype, sourceExtension, targetExtension, timeoutMs,
|
||||
args, sj);
|
||||
|
||||
request(logger, sourceExtension, targetExtension, reqEntity, writer, sj.toString());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Failed to read content from reader", e);
|
||||
}
|
||||
}
|
||||
|
||||
HttpEntity getRequestEntity(ContentReader reader, String sourceMimetype, String sourceExtension,
|
||||
String targetExtension, long timeoutMs, String[] args, StringJoiner sj)
|
||||
HttpEntity getRequestEntity(ContentReader reader, String sourceMimetype, String sourceExtension, String targetExtension,
|
||||
long timeoutMs, String[] args, StringJoiner sj)
|
||||
{
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
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)
|
||||
{
|
||||
String timeoutMsString = Long.toString(timeoutMs);
|
||||
builder.addTextBody("timeout", timeoutMsString);
|
||||
sj.add("timeout=" + timeoutMsString);
|
||||
}
|
||||
return builder.build();
|
||||
return getRequestEntity(reader.getContentInputStream(), sourceMimetype, sourceExtension, targetExtension, timeoutMs, args, sj);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
private String getErrorMessage(HttpEntity resEntity) throws IOException
|
||||
{
|
||||
|
Reference in New Issue
Block a user