ACS-9835-Improve code quality in alfresco-transform-core (#1116)

This commit is contained in:
Arindam Roy
2025-07-18 15:46:21 +05:30
committed by GitHub
parent ac477aea65
commit f075c0da9a
239 changed files with 8956 additions and 9395 deletions

View File

@@ -44,7 +44,8 @@ public final class PdfRendererOptionsBuilder
private Boolean allowPdfEnlargement;
private Boolean maintainPdfAspectRatio;
private PdfRendererOptionsBuilder() {}
private PdfRendererOptionsBuilder()
{}
public PdfRendererOptionsBuilder withPage(final String page)
{

View File

@@ -26,19 +26,20 @@
*/
package org.alfresco.transform.pdfrenderer;
import org.alfresco.transform.base.TransformEngine;
import org.alfresco.transform.base.probes.ProbeTransform;
import org.alfresco.transform.config.reader.TransformConfigResourceReader;
import org.alfresco.transform.config.TransformConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Collections;
import static org.alfresco.transform.base.logging.StandardMessages.COMMUNITY_LICENCE;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG;
import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.alfresco.transform.base.TransformEngine;
import org.alfresco.transform.base.probes.ProbeTransform;
import org.alfresco.transform.config.TransformConfig;
import org.alfresco.transform.config.reader.TransformConfigResourceReader;
@Component
public class PdfRendererTransformEngine implements TransformEngine
{
@@ -52,9 +53,10 @@ public class PdfRendererTransformEngine implements TransformEngine
}
@Override
public String getStartupMessage() {
public String getStartupMessage()
{
return COMMUNITY_LICENCE +
"This transformer uses alfresco-pdf-renderer which uses the PDFium library from Google Inc. "+
"This transformer uses alfresco-pdf-renderer which uses the PDFium library from Google Inc. " +
"See the license at https://pdfium.googlesource.com/pdfium/+/master/LICENSE or in /pdfium.txt";
}
@@ -70,4 +72,4 @@ public class PdfRendererTransformEngine implements TransformEngine
return new ProbeTransform("probe.pdf", MIMETYPE_PDF, MIMETYPE_IMAGE_PNG, Collections.emptyMap(),
7455, 1024, 150, 10240, 60 * 20 + 1, 60 * 15 - 15);
}
}
}

View File

@@ -26,20 +26,6 @@
*/
package org.alfresco.transform.pdfrenderer.transformers;
import org.alfresco.transform.base.TransformManager;
import org.alfresco.transform.base.executors.AbstractCommandExecutor;
import org.alfresco.transform.base.executors.RuntimeExec;
import org.alfresco.transform.base.util.CustomTransformerFileAdaptor;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transform.pdfrenderer.PdfRendererOptionsBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import jakarta.annotation.PostConstruct;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import static org.alfresco.transform.base.util.Util.stringToLong;
import static org.alfresco.transform.common.RequestParamMap.ALLOW_PDF_ENLARGEMENT;
import static org.alfresco.transform.common.RequestParamMap.HEIGHT_REQUEST_PARAM;
@@ -48,9 +34,23 @@ import static org.alfresco.transform.common.RequestParamMap.PAGE_REQUEST_PARAM;
import static org.alfresco.transform.common.RequestParamMap.TIMEOUT;
import static org.alfresco.transform.common.RequestParamMap.WIDTH_REQUEST_PARAM;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.alfresco.transform.base.TransformManager;
import org.alfresco.transform.base.executors.AbstractCommandExecutor;
import org.alfresco.transform.base.executors.RuntimeExec;
import org.alfresco.transform.base.util.CustomTransformerFileAdaptor;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transform.pdfrenderer.PdfRendererOptionsBuilder;
/**
* CommandExecutor implementation for running PDF Renderer transformations. It runs the
* transformation logic as a separate Shell process.
* CommandExecutor implementation for running PDF Renderer transformations. It runs the transformation logic as a separate Shell process.
*/
@Component
public class PdfRendererTransformer extends AbstractCommandExecutor implements CustomTransformerFileAdaptor
@@ -69,7 +69,8 @@ public class PdfRendererTransformer extends AbstractCommandExecutor implements C
super.checkCommand = createCheckCommand();
}
@Override public String getTransformerName()
@Override
public String getTransformerName()
{
return "pdfrenderer";
}
@@ -103,7 +104,7 @@ public class PdfRendererTransformer extends AbstractCommandExecutor implements C
@Override
public void transform(String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
File sourceFile, File targetFile, TransformManager transformManager) throws TransformException
File sourceFile, File targetFile, TransformManager transformManager) throws TransformException
{
final String options = PdfRendererOptionsBuilder
.builder()

View File

@@ -29,9 +29,11 @@ package org.alfresco.transform.pdfrenderer;
import org.alfresco.transform.base.LivenessReadinessProbeTest;
public class PdfRendererLivenessReadinessProbeIT extends LivenessReadinessProbeTest {
public class PdfRendererLivenessReadinessProbeIT extends LivenessReadinessProbeTest
{
@Override
protected ImagesForTests getImageForTest() {
protected ImagesForTests getImageForTest()
{
return new ImagesForTests("alfresco-pdf-renderer", "application/pdf", "image/png", "quick.pdf");
}
}

View File

@@ -31,12 +31,11 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF;
import java.util.UUID;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transform.base.messaging.AbstractQueueIT;
import org.alfresco.transform.client.model.TransformRequest;
/**
* @author Lucian Tuca
* created on 15/01/2019
* @author Lucian Tuca created on 15/01/2019
*/
public class PdfRendererQueueIT extends AbstractQueueIT
{
@@ -44,16 +43,16 @@ public class PdfRendererQueueIT extends AbstractQueueIT
protected TransformRequest buildRequest()
{
return TransformRequest
.builder()
.withRequestId(UUID.randomUUID().toString())
.withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING)
.withTargetMediaType(MIMETYPE_PDF)
.withTargetExtension("pdf")
.withSchema(1)
.withClientData("ACS")
.withSourceReference(UUID.randomUUID().toString())
.withSourceSize(32L)
.withInternalContextForTransformEngineTests()
.build();
.builder()
.withRequestId(UUID.randomUUID().toString())
.withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING)
.withTargetMediaType(MIMETYPE_PDF)
.withTargetExtension("pdf")
.withSchema(1)
.withClientData("ACS")
.withSourceReference(UUID.randomUUID().toString())
.withSourceSize(32L)
.withInternalContextForTransformEngineTests()
.build();
}
}

View File

@@ -26,37 +26,6 @@
*/
package org.alfresco.transform.pdfrenderer;
import org.alfresco.transform.base.AbstractBaseTest;
import org.alfresco.transform.base.executors.RuntimeExec;
import org.alfresco.transform.base.executors.RuntimeExec.ExecutionResult;
import org.alfresco.transform.base.model.FileRefEntity;
import org.alfresco.transform.base.model.FileRefResponse;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transform.pdfrenderer.transformers.PdfRendererTransformer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -78,6 +47,39 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.util.StringUtils.getFilenameExtension;
import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.alfresco.transform.base.AbstractBaseTest;
import org.alfresco.transform.base.executors.RuntimeExec;
import org.alfresco.transform.base.executors.RuntimeExec.ExecutionResult;
import org.alfresco.transform.base.model.FileRefEntity;
import org.alfresco.transform.base.model.FileRefResponse;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transform.pdfrenderer.transformers.PdfRendererTransformer;
/**
* Test PdfRenderer with mocked external command.
*/
@@ -118,8 +120,8 @@ public class PdfRendererTest extends AbstractBaseTest
@Override
public void mockTransformCommand(String sourceExtension,
String targetExtension, String sourceMimetype,
boolean readTargetFileBytes) throws IOException
String targetExtension, String sourceMimetype,
boolean readTargetFileBytes) throws IOException
{
this.sourceExtension = sourceExtension;
this.targetExtension = targetExtension;
@@ -133,57 +135,57 @@ public class PdfRendererTest extends AbstractBaseTest
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, sourceFileBytes);
when(mockTransformCommand.execute(any(), anyLong())).thenAnswer(
(Answer<RuntimeExec.ExecutionResult>) invocation -> {
Map<String, String> actualProperties = invocation.getArgument(0);
assertEquals(3, actualProperties.size(), "There should be 3 properties");
(Answer<RuntimeExec.ExecutionResult>) invocation -> {
Map<String, String> actualProperties = invocation.getArgument(0);
assertEquals(3, actualProperties.size(), "There should be 3 properties");
String actualOptions = actualProperties.get("options");
String actualSource = actualProperties.get("source");
String actualTarget = actualProperties.get("target");
String actualTargetExtension = getFilenameExtension(actualTarget);
String actualOptions = actualProperties.get("options");
String actualSource = actualProperties.get("source");
String actualTarget = actualProperties.get("target");
String actualTargetExtension = getFilenameExtension(actualTarget);
assertNotNull(actualSource);
assertNotNull(actualTarget);
if (expectedSourceSuffix != null)
{
assertTrue(actualSource.endsWith(expectedSourceSuffix),
"The source file \"" + actualSource +
"\" should have ended in \"" + expectedSourceSuffix + "\"");
actualSource = actualSource.substring(0,
actualSource.length() - expectedSourceSuffix.length());
}
assertNotNull(actualSource);
assertNotNull(actualTarget);
if (expectedSourceSuffix != null)
{
assertTrue(actualSource.endsWith(expectedSourceSuffix),
"The source file \"" + actualSource +
"\" should have ended in \"" + expectedSourceSuffix + "\"");
actualSource = actualSource.substring(0,
actualSource.length() - expectedSourceSuffix.length());
}
assertNotNull(actualOptions);
if (expectedOptions != null)
{
assertEquals(expectedOptions, actualOptions,"expectedOptions");
}
assertNotNull(actualOptions);
if (expectedOptions != null)
{
assertEquals(expectedOptions, actualOptions, "expectedOptions");
}
Long actualTimeout = invocation.getArgument(1);
assertNotNull(actualTimeout);
if (expectedTimeout != null)
{
assertEquals(expectedTimeout, actualTimeout,"expectedTimeout");
}
Long actualTimeout = invocation.getArgument(1);
assertNotNull(actualTimeout);
if (expectedTimeout != null)
{
assertEquals(expectedTimeout, actualTimeout, "expectedTimeout");
}
// Copy a test file into the target file location if it exists
int i = actualTarget.lastIndexOf('_');
if (i >= 0)
{
String testFilename = actualTarget.substring(i + 1);
File testFile = getTestFile(testFilename, false);
File targetFile = new File(actualTarget);
generateTargetFileFromResourceFile(actualTargetExtension, testFile,
targetFile);
}
// Copy a test file into the target file location if it exists
int i = actualTarget.lastIndexOf('_');
if (i >= 0)
{
String testFilename = actualTarget.substring(i + 1);
File testFile = getTestFile(testFilename, false);
File targetFile = new File(actualTarget);
generateTargetFileFromResourceFile(actualTargetExtension, testFile,
targetFile);
}
// Check the supplied source file has not been changed.
byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath());
assertTrue(Arrays.equals(sourceFileBytes, actualSourceFileBytes),
"Source file is not the same");
// Check the supplied source file has not been changed.
byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath());
assertTrue(Arrays.equals(sourceFileBytes, actualSourceFileBytes),
"Source file is not the same");
return mockExecutionResult;
});
return mockExecutionResult;
});
when(mockExecutionResult.getExitValue()).thenReturn(0);
when(mockExecutionResult.getStdErr()).thenReturn("STDERROR");
@@ -195,23 +197,23 @@ public class PdfRendererTest extends AbstractBaseTest
{
expectedOptions = "--width=321 --height=654 --allow-enlargement --maintain-aspect-ratio --page=2";
mockMvc
.perform(MockMvcRequestBuilders
.multipart(ENDPOINT_TRANSFORM)
.file(sourceFile)
.param("targetExtension", targetExtension)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype)
.perform(MockMvcRequestBuilders
.multipart(ENDPOINT_TRANSFORM)
.file(sourceFile)
.param("targetExtension", targetExtension)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype)
.param("page", "2")
.param("page", "2")
.param("width", "321")
.param("height", "654")
.param("allowPdfEnlargement", "true")
.param("maintainPdfAspectRatio", "true"))
.andExpect(status().isOk())
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition",
"attachment; filename*=UTF-8''transform." + targetExtension));
.param("width", "321")
.param("height", "654")
.param("allowPdfEnlargement", "true")
.param("maintainPdfAspectRatio", "true"))
.andExpect(status().isOk())
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition",
"attachment; filename*=UTF-8''transform." + targetExtension));
}
@Test
@@ -219,23 +221,23 @@ public class PdfRendererTest extends AbstractBaseTest
{
expectedOptions = "--width=321 --height=654 --page=2";
mockMvc
.perform(MockMvcRequestBuilders
.multipart(ENDPOINT_TRANSFORM)
.file(sourceFile)
.param("targetExtension", targetExtension)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype)
.perform(MockMvcRequestBuilders
.multipart(ENDPOINT_TRANSFORM)
.file(sourceFile)
.param("targetExtension", targetExtension)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype)
.param("page", "2")
.param("page", "2")
.param("width", "321")
.param("height", "654")
.param("allowPdfEnlargement", "false")
.param("maintainPdfAspectRatio", "false"))
.andExpect(status().isOk())
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition",
"attachment; filename*=UTF-8''transform." + targetExtension));
.param("width", "321")
.param("height", "654")
.param("allowPdfEnlargement", "false")
.param("maintainPdfAspectRatio", "false"))
.andExpect(status().isOk())
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition",
"attachment; filename*=UTF-8''transform." + targetExtension));
}
@Override
@@ -253,9 +255,9 @@ public class PdfRendererTest extends AbstractBaseTest
when(mockExecutionResult.getExitValue()).thenReturn(1);
mockMvc.perform(mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", "xxx"))
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status()
.reason(containsString("Transformer exit code was not 0: \nSTDERR")));
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status()
.reason(containsString("Transformer exit code was not 0: \nSTDERR")));
}
@Test
@@ -272,11 +274,11 @@ public class PdfRendererTest extends AbstractBaseTest
HttpHeaders headers = new HttpHeaders();
headers.set(CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension);
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, OK);
sourceFile), headers, OK);
when(sharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(sharedFileStoreClient.saveFile(any()))
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
when(mockExecutionResult.getExitValue()).thenReturn(0);
// Update the Transformation Request with any specific params before sending it
@@ -285,16 +287,16 @@ public class PdfRendererTest extends AbstractBaseTest
// Serialize and call the transformer
String tr = objectMapper.writeValueAsString(transformRequest);
String transformationReplyAsString = mockMvc
.perform(MockMvcRequestBuilders
.post(ENDPOINT_TRANSFORM)
.header(ACCEPT, APPLICATION_JSON_VALUE)
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.content(tr))
.andExpect(status().is(CREATED.value()))
.andReturn().getResponse().getContentAsString();
.perform(MockMvcRequestBuilders
.post(ENDPOINT_TRANSFORM)
.header(ACCEPT, APPLICATION_JSON_VALUE)
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.content(tr))
.andExpect(status().is(CREATED.value()))
.andReturn().getResponse().getContentAsString();
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString,
TransformReply.class);
TransformReply.class);
// Assert the reply
assertEquals(transformRequest.getRequestId(), transformReply.getRequestId());
@@ -305,7 +307,7 @@ public class PdfRendererTest extends AbstractBaseTest
@Test
public void testOverridingExecutorPaths()
{
//System test property value can me modified in the pom.xml
// System test property value can me modified in the pom.xml
assertEquals(execPath, System.getProperty("PDF_RENDERER_EXE"));
}
}

View File

@@ -29,16 +29,17 @@ package org.alfresco.transform.pdfrenderer;
import static java.text.MessageFormat.format;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static org.alfresco.transform.base.clients.HttpClient.sendTRequest;
import static org.alfresco.transform.base.clients.FileInfo.testFile;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.springframework.http.HttpStatus.OK;
import static org.alfresco.transform.base.clients.FileInfo.testFile;
import static org.alfresco.transform.base.clients.HttpClient.sendTRequest;
import java.util.Map;
import java.util.stream.Stream;
import org.alfresco.transform.base.clients.FileInfo;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
@@ -46,6 +47,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.alfresco.transform.base.clients.FileInfo;
/**
* @author Cezar Leahu
*/
@@ -55,18 +58,16 @@ public class PdfRendererTransformationIT
private static final String ENGINE_URL = "http://localhost:8090";
private static final Map<String, FileInfo> TEST_FILES = Stream.of(
testFile("application/pdf","pdf","quick.pdf"),
testFile("application/illustrator","ai","quickCS3.ai") ,
testFile("application/illustrator","ai","quickCS5.ai")
).collect(toMap(FileInfo::getPath, identity()));
testFile("application/pdf", "pdf", "quick.pdf"),
testFile("application/illustrator", "ai", "quickCS3.ai"),
testFile("application/illustrator", "ai", "quickCS5.ai")).collect(toMap(FileInfo::getPath, identity()));
public static Stream<String> engineTransformations()
{
return Stream.of(
"quick.pdf",
"quickCS3.ai",
"quickCS5.ai"
);
"quick.pdf",
"quickCS3.ai",
"quickCS5.ai");
}
@ParameterizedTest
@@ -76,13 +77,13 @@ public class PdfRendererTransformationIT
final String sourceMimetype = TEST_FILES.get(sourceFile).getMimeType();
final String descriptor = format("Transform ({0}, {1} -> {2}, {3})",
sourceFile, sourceMimetype, "image/png", "png");
sourceFile, sourceMimetype, "image/png", "png");
try
{
final ResponseEntity<Resource> response = sendTRequest(ENGINE_URL, sourceFile, sourceMimetype,
"image/png", "png");
assertEquals(OK, response.getStatusCode(),descriptor);
"image/png", "png");
assertEquals(OK, response.getStatusCode(), descriptor);
}
catch (Exception e)
{