ongoing [skip ci]

This commit is contained in:
alandavis
2022-08-14 16:30:56 +01:00
parent e37b2c9beb
commit d704617b9c
21 changed files with 138 additions and 106 deletions

View File

@@ -39,6 +39,11 @@ import java.util.Map;
*/
public interface TransformManager
{
/**
* @return the id of the request.
*/
String getRequestId();
/**
* Allows a {@link CustomTransformer} to use a local source {@code File} rather than the supplied {@code InputStream}.
* The file will be deleted once the request is completed. To avoid creating extra files, if a File has already

View File

@@ -26,13 +26,8 @@
*/
package org.alfresco.transform.base.messaging;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import org.alfresco.transform.messages.TransformRequestValidator;
import org.apache.activemq.command.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
@@ -45,6 +40,9 @@ import org.springframework.lang.NonNull;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.transaction.PlatformTransactionManager;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
/**
* JMS and messaging configuration for the T-Engines. Contains the basic config in order to have the
* T-Engine able to read from queues and send a reply back.

View File

@@ -37,6 +37,7 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Component
public class CustomTransformers
@@ -48,8 +49,6 @@ public class CustomTransformers
private final Map<String, CustomTransformer> customTransformersByName = new HashMap<>();
private CustomTransformer defaultCustomTransformer;
@PostConstruct
private void initCustomTransformersByName()
{
@@ -61,19 +60,17 @@ public class CustomTransformers
logger.info("Transformers:");
customTransformers.stream()
.sorted(Comparator.comparing(CustomTransformer::getTransformerName))
.map(customTransformer -> " "+customTransformer.getTransformerName()).forEach(logger::info);
.map(customTransformer -> customTransformer.getTransformerName())
.filter(Objects::nonNull)
.map(name -> " "+name)
.forEach(logger::info);
}
}
public void setDefaultCustomTransformer(CustomTransformer defaultCustomTransformer)
{
this.defaultCustomTransformer = defaultCustomTransformer;
}
public CustomTransformer get(String name)
{
CustomTransformer customTransformer = customTransformersByName.get(name);
return customTransformer == null ? defaultCustomTransformer : customTransformer;
return customTransformer == null ? customTransformersByName.get(null) : customTransformer;
}
public void put(String name, CustomTransformer customTransformer)

View File

@@ -108,6 +108,12 @@ abstract class ProcessHandler extends FragmentHandler
super.init();
}
public String getReference()
{
return reference;
}
public void handleTransformRequest()
{
LogEntry.start();

View File

@@ -71,6 +71,11 @@ public class TransformManagerImpl implements TransformManager
this.processHandler = processHandler;
}
@Override public String getRequestId()
{
return processHandler.getReference();
}
public InputStream setInputStream(InputStream inputStream)
{
this.inputStream = inputStream;
@@ -130,14 +135,13 @@ public class TransformManagerImpl implements TransformManager
return targetFile;
}
File setTargetFile(File targetFile)
public void setTargetFile(File targetFile)
{
this.targetFile = targetFile;
if (startedWithTargetFile == null)
{
startedWithTargetFile = true;
}
return targetFile;
}
public void keepTargetFile()
@@ -179,7 +183,7 @@ public class TransformManagerImpl implements TransformManager
{
if (targetFile != null)
{
if (startedWithTargetFile == false)
if (!startedWithTargetFile)
{
FileManager.copyFileToOutputStream(targetFile, outputStreamLengthRecorder);
}

View File

@@ -47,9 +47,9 @@ public interface CustomTransformerFileAdaptor extends CustomTransformer
{
File sourceFile = transformManager.createSourceFile();
File targetFile = transformManager.createTargetFile();
transform(sourceMimetype, targetMimetype, transformOptions, sourceFile, targetFile);
transform(sourceMimetype, targetMimetype, transformOptions, sourceFile, targetFile, transformManager);
}
void transform(String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
File sourceFile, File targetFile) throws Exception;
File sourceFile, File targetFile, TransformManager transformManager) throws Exception;
}

View File

@@ -8,7 +8,7 @@
<tr><td><div style="text-align:right">directAccessUrl</div></td><td><input type="text" name="directAccessUrl"/></td></tr>
<tr><td><div style="text-align:right">sourceMimetype</div></td><td><input type="text" name="sourceMimetype" value="" /></td>
<td><select name="_sourceMimetype">
<option value="" >-- file extension --</option>
<option value="" >-- by file extension --</option>
<option value="image/jpeg" >jpg</option>
<option value="image/png">png</option>
<option value="application/pdf">pdf</option>
@@ -19,7 +19,7 @@
</select></td></tr>
<tr><td><div style="text-align:right">targetMimetype</div></td><td><input type="text" name="targetMimetype" value="" /></td>
<td><select name="_targetMimetype">
<option value="" >-- file extension --</option>
<option value="" >-- by file extension --</option>
<option value="image/jpeg" >jpg</option>
<option value="image/png">png</option>
<option value="application/pdf">pdf</option>

View File

@@ -37,8 +37,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
@@ -152,7 +150,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C
@Override
public void transform(String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
File sourceFile, File targetFile) throws TransformException
File sourceFile, File targetFile, TransformManager transformManager) throws TransformException
{
final String options = ImageMagickOptionsBuilder
.builder()

View File

@@ -47,8 +47,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Map;
@@ -133,7 +131,7 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi
@Override
public void transform(String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
File sourceFile, File targetFile)
File sourceFile, File targetFile, TransformManager transformManager)
{
call(sourceFile, targetFile);
}

View File

@@ -39,8 +39,6 @@ import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
@@ -86,7 +84,7 @@ public class AppleIWorksContentTransformer implements CustomTransformerFileAdapt
@Override
public void transform(String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
File sourceFile, File targetFile)
File sourceFile, File targetFile, TransformManager transformManager)
{
logger.debug("Performing IWorks to jpeg transform with sourceMimetype={} targetMimetype={}",
sourceMimetype, targetMimetype);

View File

@@ -46,7 +46,6 @@ import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
@@ -84,7 +83,7 @@ public class EMLTransformer implements CustomTransformerFileAdaptor
@Override
public void transform(String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
File sourceFile, File targetFile) throws Exception
File sourceFile, File targetFile, TransformManager transformManager) throws Exception
{
logger.debug("Performing RFC822 to text transform.");
// Use try with resource

View File

@@ -26,6 +26,7 @@
*/
package org.alfresco.transform.misc.transformers;
import org.alfresco.transform.base.TransformManager;
import org.alfresco.transform.base.util.CustomTransformerFileAdaptor;
import org.htmlparser.Parser;
import org.htmlparser.beans.StringBean;
@@ -88,7 +89,7 @@ public class HtmlParserContentTransformer implements CustomTransformerFileAdapto
@Override
public void transform(final String sourceMimetype, final String targetMimetype,
final Map<String, String> transformOptions,
final File sourceFile, final File targetFile) throws Exception
final File sourceFile, final File targetFile, TransformManager transformManager) throws Exception
{
String sourceEncoding = transformOptions.get(SOURCE_ENCODING);
checkEncodingParameter(sourceEncoding, SOURCE_ENCODING);

View File

@@ -40,7 +40,6 @@ import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Map;
@@ -70,7 +69,7 @@ public class OOXMLThumbnailContentTransformer implements CustomTransformerFileAd
@Override
public void transform(final String sourceMimetype, final String targetMimetype, final Map<String, String> parameters,
final File sourceFile, final File targetFile) throws Exception
final File sourceFile, final File targetFile, TransformManager transformManager) throws Exception
{
if (logger.isDebugEnabled())
{

View File

@@ -26,6 +26,7 @@
*/
package org.alfresco.transform.misc.transformers;
import org.alfresco.transform.base.TransformManager;
import org.alfresco.transform.base.util.CustomTransformerFileAdaptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -80,7 +81,7 @@ public class StringExtractingContentTransformer implements CustomTransformerFile
*/
@Override
public void transform(final String sourceMimetype, final String targetMimetype, final Map<String, String> transformOptions,
final File sourceFile, final File targetFile) throws Exception
final File sourceFile, final File targetFile, TransformManager transformManager) throws Exception
{
String sourceEncoding = transformOptions.get(SOURCE_ENCODING);
String targetEncoding = transformOptions.get(TARGET_ENCODING);

View File

@@ -116,7 +116,7 @@ public class TextToPdfContentTransformer implements CustomTransformerFileAdaptor
@Override
public void transform(final String sourceMimetype, final String targetMimetype, final Map<String,
String> transformOptions,
final File sourceFile, final File targetFile) throws Exception
final File sourceFile, final File targetFile, TransformManager transformManager) throws Exception
{
String sourceEncoding = transformOptions.get(SOURCE_ENCODING);
String stringPageLimit = transformOptions.get(PAGE_LIMIT);

View File

@@ -81,7 +81,7 @@ public class HtmlParserContentTransformerTest
Map<String, String> parameters = new HashMap<>();
parameters.put(SOURCE_ENCODING, "ISO-8859-1");
transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD);
transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, transformManager);
assertEquals(expected, readFromFile(tmpD, "UTF-8"));
tmpS.delete();
@@ -94,7 +94,7 @@ public class HtmlParserContentTransformerTest
tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt");
parameters = new HashMap<>();
parameters.put(SOURCE_ENCODING, "UTF-8");
transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD);
transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, transformManager);
assertEquals(expected, readFromFile(tmpD, "UTF-8"));
tmpS.delete();
tmpD.delete();
@@ -106,7 +106,7 @@ public class HtmlParserContentTransformerTest
tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt");
parameters = new HashMap<>();
parameters.put(SOURCE_ENCODING, "UTF-16");
transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD);
transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, transformManager);
assertEquals(expected, readFromFile(tmpD, "UTF-8"));
tmpS.delete();
tmpD.delete();
@@ -131,7 +131,7 @@ public class HtmlParserContentTransformerTest
parameters = new HashMap<>();
parameters.put(SOURCE_ENCODING, "ISO-8859-1");
transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD);
transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, transformManager);
assertEquals(expected, readFromFile(tmpD, "UTF-8"));
tmpS.delete();
tmpD.delete();

View File

@@ -201,7 +201,7 @@ public class TextToPdfContentTransformerTest
Map<String, String> parameters = new HashMap<>();
parameters.put(PAGE_LIMIT, pageLimit);
parameters.put(SOURCE_ENCODING, encoding);
transformer.transform("text/plain", "application/pdf", parameters, sourceFile, targetFile);
transformer.transform("text/plain", "application/pdf", parameters, sourceFile, targetFile, transformManager);
// Read back in the PDF and check it
PDDocument doc = PDDocument.load(targetFile);

View File

@@ -37,8 +37,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
@@ -105,7 +103,7 @@ public class PdfRendererTransformer extends AbstractCommandExecutor implements C
@Override
public void transform(String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
File sourceFile, File targetFile) throws TransformException
File sourceFile, File targetFile, TransformManager transformManager) throws TransformException
{
final String options = PdfRendererOptionsBuilder
.builder()

View File

@@ -28,14 +28,20 @@ import java.util.stream.Stream;
* The client data supplied and echoed back to content repository (the client). May be modified to include
* TransformerDebug.
*/
public class RepositoryClientData
{
public class RepositoryClientData {
public static final String CLIENT_DATA_SEPARATOR = "\u23D0";
public static final String DEBUG_SEPARATOR = "\u23D1";
static final String REPO_ID = "Repo";
public static final String REPO_ID = "Repo";
public static final String DEBUG = "debug:";
private static final int REPO_INDEX = 0;
private static final int RENDITION_INDEX = 2;
private static final int REQUEST_ID_INDEX = 6;
private static final int DEBUG_INDEX = 9;
private static final int EXPECTED_ELEMENTS = 10;
private final String origClientData;
private final String[] split;
public RepositoryClientData(String clientData)
@@ -46,53 +52,102 @@ public class RepositoryClientData
private boolean isRepositoryClientData()
{
return split != null && split.length == 10 && split[0].startsWith(REPO_ID);
return split != null && split.length == EXPECTED_ELEMENTS && split[REPO_INDEX].startsWith(REPO_ID);
}
public String getAcsVersion()
{
return isRepositoryClientData() ? split[0].substring(REPO_ID.length()) : "";
return isRepositoryClientData() ? split[REPO_INDEX].substring(REPO_ID.length()) : "";
}
public int getRequestId()
{
try
{
return isRepositoryClientData() ? Integer.parseInt(split[6]) : -1;
}
catch (NumberFormatException e)
{
try {
return isRepositoryClientData() ? Integer.parseInt(split[REQUEST_ID_INDEX]) : -1;
} catch (NumberFormatException e) {
return -1;
}
}
public String getRenditionName()
{
return isRepositoryClientData() ? split[2] : "";
public String getRenditionName() {
return isRepositoryClientData() ? split[RENDITION_INDEX] : "";
}
public void appendDebug(String message)
{
public void appendDebug(String message) {
if (isDebugRequested())
{
split[9] += DEBUG_SEPARATOR+ message;
split[DEBUG_INDEX] += DEBUG_SEPARATOR + message;
}
}
public boolean isDebugRequested()
{
public boolean isDebugRequested() {
return isRepositoryClientData() && split[9].startsWith(DEBUG);
}
@Override
public String toString()
{
if (split == null)
{
@Override public String toString() {
if (split == null) {
return origClientData;
}
StringJoiner sj = new StringJoiner(CLIENT_DATA_SEPARATOR);
Stream.of(split).forEach(element -> sj.add(element));
Stream.of(split).forEach(sj::add);
return sj.toString();
}
public static Builder builder() {
return new Builder();
}
public static class Builder
{
private final RepositoryClientData clientData = new RepositoryClientData(emptyClientData());
private Builder()
{
}
private static String emptyClientData()
{
StringJoiner sj = new StringJoiner(CLIENT_DATA_SEPARATOR, REPO_ID+"ACS1234"+CLIENT_DATA_SEPARATOR, "");
for (int i=1; i<EXPECTED_ELEMENTS; i++)
{
sj.add(Integer.toString(i));
}
return sj.toString();
}
public Builder withRepoId(final String version)
{
clientData.split[REPO_INDEX] = REPO_ID+version;
return this;
}
public Builder withRequestId(final int requestId)
{
clientData.split[REQUEST_ID_INDEX] = Integer.toString(requestId);
return this;
}
public Builder withRenditionName(final String renditionName)
{
clientData.split[RENDITION_INDEX] = renditionName;
return this;
}
public Builder withDebug()
{
clientData.split[DEBUG_INDEX]=DEBUG;
return this;
}
public Builder withDebugMessage(final String message)
{
clientData.split[DEBUG_INDEX]=DEBUG+DEBUG_SEPARATOR+message;
return this;
}
public RepositoryClientData build()
{
return clientData;
}
}
}

View File

@@ -40,19 +40,13 @@ public class RepositoryClientDataTest
@Test
void AcsClientDataWithDebugTest()
{
String clientData = new StringJoiner(CLIENT_DATA_SEPARATOR)
.add(REPO_ID + "ACS1234")
.add("1")
.add("renditionName")
.add("3")
.add("4")
.add("5")
.add("54321")
.add("7")
.add("8")
.add(DEBUG)
.toString();
repositoryClientData = new RepositoryClientData(clientData);
repositoryClientData = RepositoryClientData.builder()
.withRepoId("ACS1234")
.withRenditionName("renditionName")
.withRequestId(54321)
.withDebug()
.build();
String clientData = repositoryClientData.toString();
assertEquals("ACS1234", repositoryClientData.getAcsVersion());
assertEquals("renditionName", repositoryClientData.getRenditionName());

View File

@@ -39,11 +39,8 @@ import java.util.StringJoiner;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD;
import static org.alfresco.transform.common.RepositoryClientData.CLIENT_DATA_SEPARATOR;
import static org.alfresco.transform.common.RepositoryClientData.DEBUG;
import static org.alfresco.transform.common.RepositoryClientData.DEBUG_SEPARATOR;
import static org.alfresco.transform.common.RepositoryClientData.REPO_ID;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Tests TransformerDebug. AbstractRouterTest in the t-router contains more complete end to end tests. The tests in this
@@ -72,7 +69,7 @@ class TransformerDebugTest
TransformRequest request = TransformRequest.builder()
.withSourceSize(sourceSize)
.withInternalContext(InternalContext.initialise(null))
.withClientData(clientDataWithDebugRequest(renditionName))
.withClientData(RepositoryClientData.builder().withRenditionName(renditionName).build().toString())
.build();
TransformStack.setInitialSourceReference(request.getInternalContext(), "fileRef");
@@ -108,22 +105,6 @@ class TransformerDebugTest
}
}
private String clientDataWithDebugRequest(String renditionName)
{
return new StringJoiner(CLIENT_DATA_SEPARATOR)
.add(REPO_ID + "ACS1234")
.add("1")
.add(renditionName)
.add("3")
.add("4")
.add("5")
.add("54321")
.add("7")
.add("8")
.add(DEBUG)
.toString();
}
private void monitorLogs(Level logLevel)
{
Logger logger = (Logger)LoggerFactory.getLogger(TransformerDebug.class);
@@ -289,7 +270,7 @@ class TransformerDebugTest
{
monitorLogs(Level.TRACE);
String origClientData = clientDataWithDebugRequest("");
String origClientData = RepositoryClientData.builder().build().toString();
TransformReply reply = TransformReply.builder()
.withInternalContext(InternalContext.initialise(null))
.withErrorDetails("T-Request was null - a major error")
@@ -309,7 +290,7 @@ class TransformerDebugTest
{
monitorLogs(Level.TRACE);
String origClientData = clientDataWithDebugRequest("");
String origClientData = RepositoryClientData.builder().withDebug().build().toString();
TransformReply reply = TransformReply.builder()
.withInternalContext(InternalContext.initialise(null))
.withErrorDetails("T-Request was null - a major error")