ATS-531 : Reformat code

This commit is contained in:
Cezar.Leahu
2019-08-14 22:21:06 +03:00
committed by CezarLeahu
parent 70ab0241dd
commit 485347729b
58 changed files with 1310 additions and 1074 deletions

View File

@@ -76,7 +76,8 @@ import org.springframework.web.multipart.MultipartFile;
@Controller
public class AlfrescoPdfRendererController extends AbstractTransformerController
{
private static final Logger logger = LoggerFactory.getLogger(AlfrescoPdfRendererController.class);
private static final Logger logger = LoggerFactory.getLogger(
AlfrescoPdfRendererController.class);
@Autowired
private PdfRendererCommandExecutor commandExecutor;
@@ -84,10 +85,13 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
@Autowired
public AlfrescoPdfRendererController()
{
logger.info("-----------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"-----------------------------------------------------------------------------------------------------------------------------------------------------------");
Arrays.stream(LICENCE.split("\\n")).forEach(logger::info);
logger.info("alfresco-pdf-renderer uses the PDFium library from Google Inc. See the license at https://pdfium.googlesource.com/pdfium/+/master/LICENSE or in /pdfium.txt");
logger.info("-----------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"alfresco-pdf-renderer uses the PDFium library from Google Inc. See the license at https://pdfium.googlesource.com/pdfium/+/master/LICENSE or in /pdfium.txt");
logger.info(
"-----------------------------------------------------------------------------------------------------------------------------------------------------------");
}
@Override
@@ -150,7 +154,8 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
@RequestParam(value = "allowPdfEnlargement", required = false) Boolean allowPdfEnlargement,
@RequestParam(value = "maintainPdfAspectRatio", required = false) Boolean maintainPdfAspectRatio)
{
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), targetExtension);
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(),
targetExtension);
getProbeTestTransform().incrementTransformerCount();
File sourceFile = createSourceFile(request, sourceMultipartFile);
File targetFile = createTargetFile(request, targetFilename);

View File

@@ -37,14 +37,15 @@ import org.springframework.context.annotation.Bean;
import io.micrometer.core.instrument.MeterRegistry;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Application
{
@Value("${container.name}")
private String containerName;
@Bean
public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags()
{
return registry -> registry.config().commonTags("containerName", containerName);
}

View File

@@ -118,7 +118,8 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
expectedSourceSuffix = null;
expectedSourceFileBytes = readTestFile(sourceExtension);
expectedTargetFileBytes = readTargetFileBytes ? readTestFile(targetExtension) : null;
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes);
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype,
expectedSourceFileBytes);
when(mockTransformCommand.execute(any(), anyLong())).thenAnswer(
(Answer<RuntimeExec.ExecutionResult>) invocation -> {
@@ -134,8 +135,11 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
assertNotNull(actualTarget);
if (expectedSourceSuffix != null)
{
assertTrue("The source file \""+actualSource+"\" should have ended in \""+expectedSourceSuffix+"\"", actualSource.endsWith(expectedSourceSuffix));
actualSource = actualSource.substring(0, actualSource.length()-expectedSourceSuffix.length());
assertTrue("The source file \"" + actualSource +
"\" should have ended in \"" + expectedSourceSuffix + "\"",
actualSource.endsWith(expectedSourceSuffix));
actualSource = actualSource.substring(0,
actualSource.length() - expectedSourceSuffix.length());
}
assertNotNull(actualOptions);
@@ -155,7 +159,7 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
int i = actualTarget.lastIndexOf('_');
if (i >= 0)
{
String testFilename = actualTarget.substring(i+1);
String testFilename = actualTarget.substring(i + 1);
File testFile = getTestFile(testFilename, false);
File targetFile = new File(actualTarget);
generateTargetFileFromResourceFile(actualTargetExtension, testFile,
@@ -164,7 +168,8 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
// Check the supplied source file has not been changed.
byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath());
assertTrue("Source file is not the same", Arrays.equals(expectedSourceFileBytes, actualSourceFileBytes));
assertTrue("Source file is not the same",
Arrays.equals(expectedSourceFileBytes, actualSourceFileBytes));
return mockExecutionResult;
});
@@ -174,7 +179,6 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
when(mockExecutionResult.getStdOut()).thenReturn("STDOUT");
}
@Override
protected AbstractTransformerController getController()
{
@@ -185,7 +189,9 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
public void optionsTest() throws Exception
{
expectedOptions = "--width=321 --height=654 --allow-enlargement --maintain-aspect-ratio --page=2";
mockMvc.perform(MockMvcRequestBuilders.multipart("/transform")
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")
.file(sourceFile)
.param("targetExtension", targetExtension)
@@ -195,17 +201,19 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
.param("height", "654")
.param("allowPdfEnlargement", "true")
.param("maintainPdfAspectRatio", "true"))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Test
public void optionsNegateBooleansTest() throws Exception
{
expectedOptions = "--width=321 --height=654 --page=2";
mockMvc.perform(MockMvcRequestBuilders.multipart("/transform")
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")
.file(sourceFile)
.param("targetExtension", targetExtension)
@@ -215,10 +223,10 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
.param("height", "654")
.param("allowPdfEnlargement", "false")
.param("maintainPdfAspectRatio", "false"))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Override
@@ -237,7 +245,8 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", "xxx"))
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("Transformer exit code was not 0: \nSTDERR")));
.andExpect(status()
.reason(containsString("Transformer exit code was not 0: \nSTDERR")));
}
@Test
@@ -248,7 +257,6 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
File sourceFile = getTestFile("quick." + sourceExtension, true);
String targetFileRef = UUID.randomUUID().toString();
// Transformation Request POJO
TransformRequest transformRequest = new TransformRequest();
transformRequest.setRequestId("1");
@@ -262,12 +270,14 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
// HTTP Request
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension);
headers.set(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=quick." + sourceExtension);
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(
new FileRefResponse(new FileRefEntity(targetFileRef)));
when(mockExecutionResult.getExitValue()).thenReturn(0);
// Update the Transformation Request with any specific params before sending it
@@ -275,18 +285,20 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
// Serialize and call the transformer
String tr = objectMapper.writeValueAsString(transformRequest);
String transformationReplyAsString = mockMvc.perform(MockMvcRequestBuilders.post("/transform")
String transformationReplyAsString = mockMvc
.perform(MockMvcRequestBuilders
.post("/transform")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).content(tr))
.andExpect(status().is(HttpStatus.CREATED.value()))
.andReturn().getResponse().getContentAsString();
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, TransformReply.class);
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString,
TransformReply.class);
// Assert the reply
assertEquals(transformRequest.getRequestId(), transformReply.getRequestId());
assertEquals(transformRequest.getClientData(), transformReply.getClientData());
assertEquals(transformRequest.getSchema(), transformReply.getSchema());
}
}

View File

@@ -48,7 +48,8 @@ public class AlfrescoPdfRendererQueueTransformServiceIT extends AbstractQueueTra
@Override
protected TransformRequest buildRequest()
{
return TransformRequest.builder()
return TransformRequest
.builder()
.withRequestId(UUID.randomUUID().toString())
.withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING)
.withTargetMediaType(MIMETYPE_PDF)

View File

@@ -37,14 +37,15 @@ import org.springframework.context.annotation.Bean;
import io.micrometer.core.instrument.MeterRegistry;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Application
{
@Value("${container.name}")
private String containerName;
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags()
{
return registry -> registry.config().commonTags("containerName", containerName);
}

View File

@@ -40,11 +40,11 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.alfresco.transformer.executors.ImageMagickCommandExecutor;
import org.alfresco.transformer.logging.LogEntry;
import org.alfresco.transformer.probes.ProbeTestTransform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
@@ -87,10 +87,13 @@ public class ImageMagickController extends AbstractTransformerController
@Autowired
public ImageMagickController()
{
logger.info("--------------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"--------------------------------------------------------------------------------------------------------------------------------------------------------------");
Arrays.stream(LICENCE.split("\\n")).forEach(logger::info);
logger.info("This transformer uses ImageMagick from ImageMagick Studio LLC. See the license at http://www.imagemagick.org/script/license.php or in /ImageMagick-license.txt");
logger.info("--------------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"This transformer uses ImageMagick from ImageMagick Studio LLC. See the license at http://www.imagemagick.org/script/license.php or in /ImageMagick-license.txt");
logger.info(
"--------------------------------------------------------------------------------------------------------------------------------------------------------------");
}
@Override
@@ -110,7 +113,7 @@ public class ImageMagickController extends AbstractTransformerController
{
// See the Javadoc on this method and Probes.md for the choice of these values.
return new ProbeTestTransform(this, "quick.jpg", "quick.png",
35593, 1024, 150, 1024, 60*15+1,60*15)
35593, 1024, 150, 1024, 60 * 15 + 1, 60 * 15)
{
@Override
protected void executeTransformCommand(File sourceFile, File targetFile)
@@ -240,9 +243,9 @@ public class ImageMagickController extends AbstractTransformerController
return startPage == null
? endPage == null
? ""
: "["+endPage+']'
: "[" + endPage + ']'
: endPage == null || startPage.equals(endPage)
? "["+startPage+']'
: "["+startPage+'-'+endPage+']';
? "[" + startPage + ']'
: "[" + startPage + '-' + endPage + ']';
}
}

View File

@@ -117,7 +117,8 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
expectedSourceSuffix = null;
expectedSourceFileBytes = readTestFile(sourceExtension);
expectedTargetFileBytes = readTargetFileBytes ? readTestFile(targetExtension) : null;
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes);
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype,
expectedSourceFileBytes);
when(mockTransformCommand.execute(any(), anyLong())).thenAnswer(
(Answer<RuntimeExec.ExecutionResult>) invocation -> {
@@ -133,8 +134,11 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
assertNotNull(actualTarget);
if (expectedSourceSuffix != null)
{
assertTrue("The source file \""+actualSource+"\" should have ended in \""+expectedSourceSuffix+"\"", actualSource.endsWith(expectedSourceSuffix));
actualSource = actualSource.substring(0, actualSource.length()-expectedSourceSuffix.length());
assertTrue(
"The source file \"" + actualSource + "\" should have ended in \"" + expectedSourceSuffix + "\"",
actualSource.endsWith(expectedSourceSuffix));
actualSource = actualSource.substring(0,
actualSource.length() - expectedSourceSuffix.length());
}
assertNotNull(actualOptions);
@@ -154,7 +158,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
int i = actualTarget.lastIndexOf('_');
if (i >= 0)
{
String testFilename = actualTarget.substring(i+1);
String testFilename = actualTarget.substring(i + 1);
File testFile = getTestFile(testFilename, false);
File targetFile = new File(actualTarget);
generateTargetFileFromResourceFile(actualTargetExtension, testFile,
@@ -163,7 +167,8 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
// Check the supplied source file has not been changed.
byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath());
assertTrue("Source file is not the same", Arrays.equals(expectedSourceFileBytes, actualSourceFileBytes));
assertTrue("Source file is not the same",
Arrays.equals(expectedSourceFileBytes, actualSourceFileBytes));
return mockExecutionResult;
});
@@ -173,7 +178,6 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
when(mockExecutionResult.getStdOut()).thenReturn("STDOUT");
}
@Override
protected AbstractTransformerController getController()
{
@@ -183,23 +187,28 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
@Test
public void cropGravityGoodTest() throws Exception
{
for (String value: new String[] {"North", "NorthEast", "East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center"})
for (String value : new String[]{"North", "NorthEast", "East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center"})
{
expectedOptions = "-gravity "+value+" +repage";
mockMvc.perform(MockMvcRequestBuilders.multipart("/transform")
expectedOptions = "-gravity " + value + " +repage";
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")
.file(sourceFile)
.param("targetExtension", targetExtension)
.param("cropGravity", value))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
}
@Test
public void cropGravityBadTest() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.multipart("/transform")
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")
.file(sourceFile)
.param("targetExtension", targetExtension)
.param("cropGravity", "badValue"))
@@ -211,7 +220,9 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
{
expectedOptions = "-alpha remove -gravity SouthEast -crop 123x456%+90+12 +repage -thumbnail 321x654%!";
expectedSourceSuffix = "[2-3]";
mockMvc.perform(MockMvcRequestBuilders.multipart("/transform")
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")
.file(sourceFile)
.param("targetExtension", targetExtension)
@@ -234,10 +245,10 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
.param("resizePercentage", "true")
.param("allowEnlargement", "true")
.param("maintainAspectRatio", "true"))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Test
@@ -245,7 +256,9 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
{
expectedOptions = "-auto-orient -gravity SouthEast -crop 123x456+90+12 +repage -resize 321x654>";
expectedSourceSuffix = "[2-3]";
mockMvc.perform(MockMvcRequestBuilders.multipart("/transform")
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")
.file(sourceFile)
.param("targetExtension", targetExtension)
@@ -268,10 +281,10 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
.param("resizePercentage", "false")
.param("allowEnlargement", "false")
.param("maintainAspectRatio", "false"))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Test
@@ -279,7 +292,9 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
{
// Example of why the commandOptions parameter is a bad idea.
expectedOptions = "( horrible command / ); -resize 321x654>";
mockMvc.perform(MockMvcRequestBuilders.multipart("/transform")
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")
.file(sourceFile)
.param("targetExtension", targetExtension)
.param("thumbnail", "false")
@@ -288,7 +303,8 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
.param("commandOptions", "( horrible command / );"))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick."+targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Override
@@ -307,7 +323,8 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", "xxx"))
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("Transformer exit code was not 0: \nSTDERR")));
.andExpect(
status().reason(containsString("Transformer exit code was not 0: \nSTDERR")));
}
@Test
@@ -318,7 +335,6 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
File sourceFile = getTestFile("quick." + sourceExtension, true);
String targetFileRef = UUID.randomUUID().toString();
// Transformation Request POJO
TransformRequest transformRequest = new TransformRequest();
transformRequest.setRequestId("1");
@@ -332,12 +348,14 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
// HTTP Request
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension);
headers.set(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=quick." + sourceExtension);
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(
new FileRefResponse(new FileRefEntity(targetFileRef)));
when(mockExecutionResult.getExitValue()).thenReturn(0);
// Update the Transformation Request with any specific params before sending it
@@ -345,18 +363,21 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
// Serialize and call the transformer
String tr = objectMapper.writeValueAsString(transformRequest);
String transformationReplyAsString = mockMvc.perform(MockMvcRequestBuilders.post("/transform")
String transformationReplyAsString = mockMvc
.perform(MockMvcRequestBuilders
.post("/transform")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).content(tr))
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE).content(tr))
.andExpect(status().is(HttpStatus.CREATED.value()))
.andReturn().getResponse().getContentAsString();
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, TransformReply.class);
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString,
TransformReply.class);
// Assert the reply
assertEquals(transformRequest.getRequestId(), transformReply.getRequestId());
assertEquals(transformRequest.getClientData(), transformReply.getClientData());
assertEquals(transformRequest.getSchema(), transformReply.getSchema());
}
}

View File

@@ -48,7 +48,8 @@ public class ImageMagickQueueTransformServiceIT extends AbstractQueueTransformSe
@Override
protected TransformRequest buildRequest()
{
return TransformRequest.builder()
return TransformRequest
.builder()
.withRequestId(UUID.randomUUID().toString())
.withSourceMediaType(MIMETYPE_IMAGE_PNG)
.withTargetMediaType(MIMETYPE_IMAGE_JPEG)

View File

@@ -26,8 +26,6 @@
*/
package org.alfresco.transformer;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
@@ -36,15 +34,18 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import io.micrometer.core.instrument.MeterRegistry;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Application
{
@Value("${container.name}")
private String containerName;
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags()
{
return registry -> registry.config().commonTags("containerName", containerName);
}

View File

@@ -85,10 +85,13 @@ public class LibreOfficeController extends AbstractTransformerController
@Autowired
public LibreOfficeController()
{
logger.info("-------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"-------------------------------------------------------------------------------------------------------------------------------------------------------");
Arrays.stream(LICENCE.split("\\n")).forEach(logger::info);
logger.info("This transformer uses LibreOffice from The Document Foundation. See the license at https://www.libreoffice.org/download/license/ or in /libreoffice.txt");
logger.info("-------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"This transformer uses LibreOffice from The Document Foundation. See the license at https://www.libreoffice.org/download/license/ or in /libreoffice.txt");
logger.info(
"-------------------------------------------------------------------------------------------------------------------------------------------------------");
}
@Override
@@ -108,7 +111,7 @@ public class LibreOfficeController extends AbstractTransformerController
{
// See the Javadoc on this method and Probes.md for the choice of these values.
return new ProbeTestTransform(this, "quick.doc", "quick.pdf",
11817, 1024, 150, 10240, 60*30+1, 60*15+20)
11817, 1024, 150, 10240, 60 * 30 + 1, 60 * 15 + 20)
{
@Override
protected void executeTransformCommand(File sourceFile, File targetFile)
@@ -126,7 +129,8 @@ public class LibreOfficeController extends AbstractTransformerController
@RequestParam(value = "timeout", required = false) Long timeout,
@RequestParam(value = "testDelay", required = false) Long testDelay)
{
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), targetExtension);
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(),
targetExtension);
getProbeTestTransform().incrementTransformerCount();
File sourceFile = createSourceFile(request, sourceMultipartFile);
File targetFile = createTargetFile(request, targetFilename);

View File

@@ -34,12 +34,14 @@ public interface JodConverter
{
/**
* Gets the JodConverter OfficeManager.
*
* @return
*/
OfficeManager getOfficeManager();
/**
* This method returns a boolean indicating whether the JodConverter connection to OOo is available.
*
* @return <code>true</code> if available, else <code>false</code>
*/
boolean isAvailable();

View File

@@ -128,7 +128,7 @@ public class JodConverterSharedInstance implements JodConverter
StringTokenizer tokenizer = new StringTokenizer(s, ",");
int tokenCount = tokenizer.countTokens();
portNumbers = new int[tokenCount];
for (int i = 0;tokenizer.hasMoreTokens();i++)
for (int i = 0; tokenizer.hasMoreTokens(); i++)
{
try
{
@@ -163,7 +163,8 @@ public class JodConverterSharedInstance implements JodConverter
File tmp = new File(templateProfileDir);
if (!tmp.isDirectory())
{
throw new AlfrescoRuntimeException("OpenOffice template profile directory "+templateProfileDir+" does not exist.");
throw new AlfrescoRuntimeException(
"OpenOffice template profile directory " + templateProfileDir + " does not exist.");
}
this.templateProfileDir = tmp;
}
@@ -294,7 +295,8 @@ public class JodConverterSharedInstance implements JodConverter
String officeHome = getOfficeHome();
if (logger.isDebugEnabled())
{
logger.debug("JodConverter settings (null settings will be replaced by jodconverter defaults):");
logger.debug(
"JodConverter settings (null settings will be replaced by jodconverter defaults):");
logger.debug(" officeHome = {}", officeHome);
logger.debug(" enabled = {}", isEnabled());
logger.debug(" portNumbers = {}", getString(portNumbers));
@@ -390,7 +392,7 @@ public class JodConverterSharedInstance implements JodConverter
StringBuilder portInfo = new StringBuilder();
if (portNumbers != null)
{
for (int i = 0;i < portNumbers.length;i++)
for (int i = 0; i < portNumbers.length; i++)
{
portInfo.append(portNumbers[i]);
if (i < portNumbers.length - 1)
@@ -422,7 +424,8 @@ public class JodConverterSharedInstance implements JodConverter
}
}
private List<File> findSofficePrograms(File searchRoot, List<File> results, int maxRecursionDepth)
private List<File> findSofficePrograms(File searchRoot, List<File> results,
int maxRecursionDepth)
{
return this.findSofficePrograms(searchRoot, results, 0, maxRecursionDepth);
}
@@ -448,6 +451,7 @@ public class JodConverterSharedInstance implements JodConverter
/**
* Logs some information on the specified file, including name and r/w/x permissions.
*
* @param f the file to log.
*/
private void logFileInfo(File f)
@@ -459,9 +463,7 @@ public class JodConverterSharedInstance implements JodConverter
StringBuilder msg = new StringBuilder();
msg.append(f).append(" ");
if (f.exists())
{
if (f.canRead())
if (f.exists() && f.canRead())
{
msg.append("(")
.append(f.isDirectory() ? "d" : "-")
@@ -470,7 +472,6 @@ public class JodConverterSharedInstance implements JodConverter
.append(f.canExecute() ? "x" : "-")
.append(")");
}
}
else
{
msg.append("does not exist");

View File

@@ -35,14 +35,14 @@ import java.io.IOException;
import javax.annotation.PostConstruct;
import org.alfresco.transform.exceptions.TransformException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.OfficeException;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.sun.star.task.ErrorCodeIOException;

View File

@@ -94,15 +94,19 @@ public class LibreOfficeControllerTest extends AbstractTransformerControllerTest
// The following is based on super.mockTransformCommand(...)
// This is because LibreOffice used JodConverter rather than a RuntimeExec
expectedSourceFileBytes = Files.readAllBytes(getTestFile("quick." + sourceExtension, true).toPath());
expectedTargetFileBytes = Files.readAllBytes(getTestFile("quick." + targetExtension, true).toPath());
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes);
expectedSourceFileBytes = Files.readAllBytes(
getTestFile("quick." + sourceExtension, true).toPath());
expectedTargetFileBytes = Files.readAllBytes(
getTestFile("quick." + targetExtension, true).toPath());
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype,
expectedSourceFileBytes);
doAnswer(invocation ->
{
File sourceFile = invocation.getArgument(0);
File targetFile = invocation.getArgument(1);
String actualTargetExtension = StringUtils.getFilenameExtension(targetFile.getAbsolutePath());
String actualTargetExtension = StringUtils.getFilenameExtension(
targetFile.getAbsolutePath());
assertNotNull(sourceFile);
assertNotNull(targetFile);
@@ -119,7 +123,8 @@ public class LibreOfficeControllerTest extends AbstractTransformerControllerTest
// Check the supplied source file has not been changed.
byte[] actualSourceFileBytes = Files.readAllBytes(sourceFile.toPath());
assertTrue("Source file is not the same", Arrays.equals(expectedSourceFileBytes, actualSourceFileBytes));
assertTrue("Source file is not the same",
Arrays.equals(expectedSourceFileBytes, actualSourceFileBytes));
return null;
}).when(javaExecutor).convert(any(), any());
@@ -143,11 +148,14 @@ public class LibreOfficeControllerTest extends AbstractTransformerControllerTest
{
doThrow(OfficeException.class).when(javaExecutor).convert(any(), any());
mockMvc.perform(MockMvcRequestBuilders.multipart("/transform")
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")
.file(sourceFile)
.param("targetExtension", "xxx"))
.andExpect(status().is(400))
.andExpect(status().reason(containsString("LibreOffice - LibreOffice server conversion failed:")));
.andExpect(status().reason(
containsString("LibreOffice - LibreOffice server conversion failed:")));
}
@Override

View File

@@ -47,7 +47,8 @@ public class LibreOfficeQueueTransformServiceIT extends AbstractQueueTransformSe
@Override
protected TransformRequest buildRequest()
{
return TransformRequest.builder()
return TransformRequest
.builder()
.withRequestId(UUID.randomUUID().toString())
.withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING)
.withTargetMediaType(MIMETYPE_OPENXML_WORDPROCESSING)

View File

@@ -37,7 +37,7 @@ import org.springframework.context.annotation.Bean;
import io.micrometer.core.instrument.MeterRegistry;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Application
{
@Value("${container.name}")

View File

@@ -95,10 +95,13 @@ public class TikaController extends AbstractTransformerController
@Autowired
public TikaController()
{
logger.info("--------------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"--------------------------------------------------------------------------------------------------------------------------------------------------------------");
Arrays.stream(LICENCE.split("\\n")).forEach(logger::info);
logger.info("Tika is from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\ 2.0.txt");
logger.info("--------------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"Tika is from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\ 2.0.txt");
logger.info(
"--------------------------------------------------------------------------------------------------------------------------------------------------------------");
}
@Override
@@ -149,7 +152,8 @@ public class TikaController extends AbstractTransformerController
throw new TransformException(BAD_REQUEST.value(), "Invalid transform value");
}
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), targetExtension);
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(),
targetExtension);
getProbeTestTransform().incrementTransformerCount();
File sourceFile = createSourceFile(request, sourceMultipartFile);
File targetFile = createTargetFile(request, targetFilename);

View File

@@ -486,7 +486,8 @@ public class Tika
private DocumentSelector pdfBoxEmbededDocumentSelector = new DocumentSelector()
{
private final List<String> disabledMediaTypes = asList(MIMETYPE_IMAGE_JPEG, MIMETYPE_IMAGE_TIFF, MIMETYPE_IMAGE_PNG);
private final List<String> disabledMediaTypes = asList(MIMETYPE_IMAGE_JPEG,
MIMETYPE_IMAGE_TIFF, MIMETYPE_IMAGE_PNG);
@Override
public boolean select(Metadata metadata)
@@ -518,16 +519,16 @@ public class Tika
}
catch (IllegalArgumentException e)
{
System.err.println("ERROR "+e.getMessage());
System.err.println("ERROR " + e.getMessage());
System.exit(-1);
}
catch (IllegalStateException | TikaException | IOException | SAXException e)
{
System.err.println("ERROR "+e.getMessage());
System.err.println("ERROR " + e.getMessage());
e.printStackTrace();
System.exit(-2);
}
System.out.println("Finished in "+(System.currentTimeMillis()-start)+"ms");
System.out.println("Finished in " + (System.currentTimeMillis() - start) + "ms");
}
// Extracts parameters form args
@@ -541,7 +542,7 @@ public class Tika
Boolean includeContents = null;
Boolean notExtractBookmarksText = null;
for (String arg: args)
for (String arg : args)
{
if (arg.startsWith("--"))
{
@@ -565,7 +566,7 @@ public class Tika
}
else
{
throw new IllegalArgumentException("Unexpected argument "+arg);
throw new IllegalArgumentException("Unexpected argument " + arg);
}
}
else
@@ -584,7 +585,7 @@ public class Tika
}
else
{
throw new IllegalArgumentException("Unexpected argument "+arg);
throw new IllegalArgumentException("Unexpected argument " + arg);
}
}
}
@@ -595,23 +596,24 @@ public class Tika
includeContents = includeContents == null ? false : includeContents;
notExtractBookmarksText = notExtractBookmarksText == null ? false : notExtractBookmarksText;
transform(transform, includeContents, notExtractBookmarksText, sourceFilename, targetFilename, targetMimetype, targetEncoding);
transform(transform, includeContents, notExtractBookmarksText, sourceFilename,
targetFilename, targetMimetype, targetEncoding);
}
private String getValue(String arg, boolean valueExpected, Object value, String optionName)
{
if (value != null)
{
throw new IllegalArgumentException("Duplicate "+optionName);
throw new IllegalArgumentException("Duplicate " + optionName);
}
String stringValue = arg.substring(optionName.length()).trim();
if (!valueExpected && stringValue.length() > 0)
{
throw new IllegalArgumentException("Unexpected value with "+optionName);
throw new IllegalArgumentException("Unexpected value with " + optionName);
}
if (valueExpected && stringValue.length() == 0)
{
throw new IllegalArgumentException("Expected value with "+optionName);
throw new IllegalArgumentException("Expected value with " + optionName);
}
return stringValue;
}
@@ -625,7 +627,7 @@ public class Tika
Parser parser = null;
DocumentSelector documentSelector = null;
switch(transform)
switch (transform)
{
case ARCHIVE:
parser = packageParser;
@@ -650,11 +652,12 @@ public class Tika
break;
}
transform(parser, documentSelector, includeContents, notExtractBookmarksText, sourceFilename, targetFilename, targetMimetype, targetEncoding);
transform(parser, documentSelector, includeContents, notExtractBookmarksText,
sourceFilename, targetFilename, targetMimetype, targetEncoding);
}
private void transform(Parser parser, DocumentSelector documentSelector, Boolean includeContents,
private void transform(Parser parser, DocumentSelector documentSelector,
Boolean includeContents,
Boolean notExtractBookmarksText,
String sourceFilename,
String targetFilename, String targetMimetype, String targetEncoding)
@@ -688,7 +691,7 @@ public class Tika
}
else
{
SAXTransformerFactory factory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler transformerHandler;
transformerHandler = factory.newTransformerHandler();
transformerHandler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
@@ -725,42 +728,52 @@ public class Tika
/**
* A wrapper around the normal Tika BodyContentHandler for CSV rather encoding than tab separated.
*/
protected static class CsvContentHandler extends BodyContentHandler {
private static final char[] comma = new char[]{ ',' };
protected static class CsvContentHandler extends BodyContentHandler
{
private static final char[] comma = new char[]{','};
private static final Pattern all_nums = Pattern.compile("[\\d\\.\\-\\+]+");
private boolean inCell = false;
private boolean needsComma = false;
protected CsvContentHandler(Writer output) {
protected CsvContentHandler(Writer output)
{
super(output);
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
if(length == 1 && ch[0] == '\t') {
throws SAXException
{
if (length == 1 && ch[0] == '\t')
{
// Ignore tabs, as they mess up the CSV output
} else {
}
else
{
super.ignorableWhitespace(ch, start, length);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(inCell) {
StringBuffer t = new StringBuffer(new String(ch,start,length));
throws SAXException
{
if (inCell)
{
StringBuffer t = new StringBuffer(new String(ch, start, length));
// Quote if not all numbers
if(all_nums.matcher(t).matches())
if (all_nums.matcher(t).matches())
{
super.characters(ch, start, length);
}
else
{
for(int i=t.length()-1; i>=0; i--) {
if(t.charAt(i) == '\"') {
for (int i = t.length() - 1; i >= 0; i--)
{
if (t.charAt(i) == '\"')
{
// Double up double quotes
t.insert(i, '\"');
i--;
@@ -771,33 +784,45 @@ public class Tika
char[] c = t.toString().toCharArray();
super.characters(c, 0, c.length);
}
} else {
}
else
{
super.characters(ch, start, length);
}
}
@Override
public void startElement(String uri, String localName, String name,
Attributes atts) throws SAXException {
if(localName.equals("td")) {
Attributes atts) throws SAXException
{
if (localName.equals("td"))
{
inCell = true;
if(needsComma) {
if (needsComma)
{
super.characters(comma, 0, 1);
needsComma = true;
}
} else {
}
else
{
super.startElement(uri, localName, name, atts);
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
if(localName.equals("td")) {
throws SAXException
{
if (localName.equals("td"))
{
needsComma = true;
inCell = false;
} else {
if(localName.equals("tr")) {
}
else
{
if (localName.equals("tr"))
{
needsComma = false;
}
super.endElement(uri, localName, name);
@@ -830,5 +855,4 @@ public class Tika
return context;
}
}

View File

@@ -58,11 +58,13 @@ import org.xml.sax.SAXException;
*
* @author Nick Burch
*/
public class TikaOfficeDetectParser implements Parser {
public class TikaOfficeDetectParser implements Parser
{
private final Parser ole2Parser = new OfficeParser();
private final Parser ooxmlParser = new OOXMLParser();
public Set<MediaType> getSupportedTypes(ParseContext parseContext) {
public Set<MediaType> getSupportedTypes(ParseContext parseContext)
{
Set<MediaType> types = new HashSet<>();
types.addAll(ole2Parser.getSupportedTypes(parseContext));
types.addAll(ooxmlParser.getSupportedTypes(parseContext));
@@ -93,7 +95,7 @@ public class TikaOfficeDetectParser implements Parser {
}
// Which is it?
if(initial4[0] == POIFSConstants.OOXML_FILE_HEADER[0] &&
if (initial4[0] == POIFSConstants.OOXML_FILE_HEADER[0] &&
initial4[1] == POIFSConstants.OOXML_FILE_HEADER[1] &&
initial4[2] == POIFSConstants.OOXML_FILE_HEADER[2] &&
initial4[3] == POIFSConstants.OOXML_FILE_HEADER[3])

View File

@@ -234,21 +234,26 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
this.transform = transform;
this.targetMimetype = targetMimetype;
System.out.println("Test "+transform+" "+ sourceExtension +" to "+targetExtension);
System.out.println("Test " + transform + " " + sourceExtension + " to " + targetExtension);
MockHttpServletRequestBuilder requestBuilder = includeContents == null
? mockMvcRequest("/transform", sourceFile, "targetExtension", this.targetExtension)
: mockMvcRequest("/transform", sourceFile, "targetExtension", this.targetExtension, "includeContents", includeContents.toString());
? mockMvcRequest("/transform", sourceFile,
"targetExtension", this.targetExtension)
: mockMvcRequest("/transform", sourceFile,
"targetExtension", this.targetExtension, "includeContents", includeContents.toString());
MvcResult result = mockMvc.perform(requestBuilder)
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + this.targetExtension)).
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + this.targetExtension)).
andReturn();
String content = result.getResponse().getContentAsString();
assertTrue("The content did not include \""+expectedContentContains, content.contains(expectedContentContains));
assertTrue("The content did not include \"" + expectedContentContains,
content.contains(expectedContentContains));
}
@Override
// Add extra required parameters to the request.
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params)
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile,
String... params)
{
return super.mockMvcRequest(url, sourceFile, params)
.param("transform", transform)
@@ -337,7 +342,8 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
{
mockTransformCommand(PDF, TXT, MIMETYPE_PDF, true);
targetEncoding = "rubbish";
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(INTERNAL_SERVER_ERROR.value()));
}
@@ -346,7 +352,7 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
@Test
public void zipToTextArchiveTest() throws Exception
{
transform(ARCHIVE, ZIP, TXT, MIMETYPE_ZIP, MIMETYPE_TEXT_PLAIN,false,
transform(ARCHIVE, ZIP, TXT, MIMETYPE_ZIP, MIMETYPE_TEXT_PLAIN, false,
"quick.html\n" +
"\n" +
"\n" +
@@ -358,7 +364,7 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
@Test
public void zipToTextIncludeArchiveTest() throws Exception
{
transform(ARCHIVE, ZIP, TXT, MIMETYPE_ZIP, MIMETYPE_TEXT_PLAIN,true,
transform(ARCHIVE, ZIP, TXT, MIMETYPE_ZIP, MIMETYPE_TEXT_PLAIN, true,
"quick.html\n" +
"\n" +
"\n" +
@@ -403,7 +409,8 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
@Test
public void msgToTxtOutlookMsgTest() throws Exception
{
transform(OUTLOOK_MSG, MSG, TXT, MIMETYPE_OUTLOOK_MSG, MIMETYPE_TEXT_PLAIN, null, EXPECTED_MSG_CONTENT_CONTAINS);
transform(OUTLOOK_MSG, MSG, TXT, MIMETYPE_OUTLOOK_MSG, MIMETYPE_TEXT_PLAIN, null,
EXPECTED_MSG_CONTENT_CONTAINS);
}
// --- PdfBox ---
@@ -411,31 +418,36 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
@Test
public void pdfToTxtPdfBoxTest() throws Exception
{
transform(PDF_BOX, PDF, TXT, MIMETYPE_PDF, MIMETYPE_TEXT_PLAIN, null, EXPECTED_TEXT_CONTENT_CONTAINS);
transform(PDF_BOX, PDF, TXT, MIMETYPE_PDF, MIMETYPE_TEXT_PLAIN, null,
EXPECTED_TEXT_CONTENT_CONTAINS);
}
@Test
public void pdfToCsvPdfBoxTest() throws Exception
{
transform(PDF_BOX, PDF, CSV, MIMETYPE_PDF, MIMETYPE_TEXT_CSV, null, EXPECTED_TEXT_CONTENT_CONTAINS); // Yes it is just text
transform(PDF_BOX, PDF, CSV, MIMETYPE_PDF, MIMETYPE_TEXT_CSV, null,
EXPECTED_TEXT_CONTENT_CONTAINS); // Yes it is just text
}
@Test
public void pdfToXmlPdfBoxTest() throws Exception
{
transform(PDF_BOX, PDF, XML, MIMETYPE_PDF, MIMETYPE_XML, null, EXPECTED_XHTML_CONTENT_CONTAINS); // Yes it is just XHTML
transform(PDF_BOX, PDF, XML, MIMETYPE_PDF, MIMETYPE_XML, null,
EXPECTED_XHTML_CONTENT_CONTAINS); // Yes it is just XHTML
}
@Test
public void pdfToXhtmlPdfBoxTest() throws Exception
{
transform(PDF_BOX, PDF, XHTML, MIMETYPE_PDF, MIMETYPE_XHTML, null, EXPECTED_XHTML_CONTENT_CONTAINS);
transform(PDF_BOX, PDF, XHTML, MIMETYPE_PDF, MIMETYPE_XHTML, null,
EXPECTED_XHTML_CONTENT_CONTAINS);
}
@Test
public void pdfToHtmlPdfBoxTest() throws Exception
{
transform(PDF_BOX, PDF, HTML, MIMETYPE_PDF, MIMETYPE_HTML, null, EXPECTED_XHTML_CONTENT_CONTAINS); // Yes it is just XHTML
transform(PDF_BOX, PDF, HTML, MIMETYPE_PDF, MIMETYPE_HTML, null,
EXPECTED_XHTML_CONTENT_CONTAINS); // Yes it is just XHTML
}
// --- Office ---
@@ -443,13 +455,15 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
@Test
public void msgToTxtOfficeTest() throws Exception
{
transform(POI_OFFICE, MSG, TXT, MIMETYPE_OUTLOOK_MSG, MIMETYPE_TEXT_PLAIN, null, EXPECTED_MSG_CONTENT_CONTAINS);
transform(POI_OFFICE, MSG, TXT, MIMETYPE_OUTLOOK_MSG, MIMETYPE_TEXT_PLAIN, null,
EXPECTED_MSG_CONTENT_CONTAINS);
}
@Test
public void docToTxtOfficeTest() throws Exception
{
transform(POI_OFFICE, DOC, TXT, MIMETYPE_WORD, MIMETYPE_TEXT_PLAIN, null, EXPECTED_TEXT_CONTENT_CONTAINS);
transform(POI_OFFICE, DOC, TXT, MIMETYPE_WORD, MIMETYPE_TEXT_PLAIN, null,
EXPECTED_TEXT_CONTENT_CONTAINS);
}
// --- Poi ---
@@ -457,7 +471,8 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
@Test
public void xslxToCsvPoiTest() throws Exception
{
transform(POI, XSLX, CSV, MIMETYPE_OPENXML_SPREADSHEET, MIMETYPE_TEXT_CSV, null, EXPECTED_CSV_CONTENT_CONTAINS);
transform(POI, XSLX, CSV, MIMETYPE_OPENXML_SPREADSHEET, MIMETYPE_TEXT_CSV, null,
EXPECTED_CSV_CONTENT_CONTAINS);
}
// --- OOXML ---
@@ -465,13 +480,15 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
@Test
public void docxToTxtOoXmlTest() throws Exception
{
transform(POI_OO_XML, DOCX, TXT, MIMETYPE_OPENXML_WORDPROCESSING, MIMETYPE_TEXT_PLAIN, null, EXPECTED_TEXT_CONTENT_CONTAINS);
transform(POI_OO_XML, DOCX, TXT, MIMETYPE_OPENXML_WORDPROCESSING, MIMETYPE_TEXT_PLAIN, null,
EXPECTED_TEXT_CONTENT_CONTAINS);
}
@Test
public void pptxToTxtOoXmlTest() throws Exception
{
transform(POI_OO_XML, PPTX, TXT, MIMETYPE_OPENXML_PRESENTATION, MIMETYPE_TEXT_PLAIN, null, EXPECTED_TEXT_CONTENT_CONTAINS);
transform(POI_OO_XML, PPTX, TXT, MIMETYPE_OPENXML_PRESENTATION, MIMETYPE_TEXT_PLAIN, null,
EXPECTED_TEXT_CONTENT_CONTAINS);
}
// --- TikaAuto ---
@@ -479,13 +496,15 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
@Test
public void ppxtToTxtTikaAutoTest() throws Exception
{
transform(TIKA_AUTO, PPTX, TXT, MIMETYPE_OPENXML_PRESENTATION, MIMETYPE_TEXT_PLAIN, null, EXPECTED_TEXT_CONTENT_CONTAINS);
transform(TIKA_AUTO, PPTX, TXT, MIMETYPE_OPENXML_PRESENTATION, MIMETYPE_TEXT_PLAIN, null,
EXPECTED_TEXT_CONTENT_CONTAINS);
}
@Test
public void doctToTxtTikaAutoTest() throws Exception
{
transform(TIKA_AUTO, DOCX, TXT, MIMETYPE_OPENXML_WORDPROCESSING, MIMETYPE_TEXT_PLAIN, null, EXPECTED_TEXT_CONTENT_CONTAINS);
transform(TIKA_AUTO, DOCX, TXT, MIMETYPE_OPENXML_WORDPROCESSING, MIMETYPE_TEXT_PLAIN, null,
EXPECTED_TEXT_CONTENT_CONTAINS);
}
// --- TextMining ---
@@ -493,16 +512,20 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
@Test
public void docToTxtTextMiningTest() throws Exception
{
transform(TEXT_MINING, DOC, TXT, MIMETYPE_WORD, MIMETYPE_TEXT_PLAIN, null, EXPECTED_TEXT_CONTENT_CONTAINS);
transform(TEXT_MINING, DOC, TXT, MIMETYPE_WORD, MIMETYPE_TEXT_PLAIN, null,
EXPECTED_TEXT_CONTENT_CONTAINS);
}
@Test
public void pdfToTxtExtractBookmarksTest() throws Exception
{
mockTransformCommand(PDF, TXT, MIMETYPE_PDF, true);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension).param("notExtractBookmarksText", "true"))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension).param(
"notExtractBookmarksText", "true"))
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Override
@@ -513,7 +536,8 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
transformRequest.setSourceMediaType(MediaType.APPLICATION_PDF_VALUE);
transformRequest.setTargetMediaType(MediaType.TEXT_PLAIN_VALUE);
transformRequest.getTransformRequestOptions().put("transform", "PdfBox");
transformRequest.getTransformRequestOptions().put("targetMimetype", MediaType.TEXT_PLAIN_VALUE);
transformRequest.getTransformRequestOptions().put("targetMimetype",
MediaType.TEXT_PLAIN_VALUE);
transformRequest.getTransformRequestOptions().put("targetEncoding", "UTF-8");
}
@@ -525,7 +549,6 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
File sourceFile = getTestFile("quick." + sourceExtension, true);
String targetFileRef = UUID.randomUUID().toString();
// Transformation Request POJO
TransformRequest transformRequest = new TransformRequest();
transformRequest.setRequestId("1");
@@ -539,12 +562,14 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
// HTTP Request
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension);
headers.set(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=quick." + sourceExtension);
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
when(alfrescoSharedFileStoreClient.saveFile(any())).thenReturn(
new FileRefResponse(new FileRefEntity(targetFileRef)));
when(mockExecutionResult.getExitValue()).thenReturn(0);
// Update the Transformation Request with any specific params before sending it
@@ -552,18 +577,21 @@ public class TikaControllerTest extends AbstractTransformerControllerTest
// Serialize and call the transformer
String tr = objectMapper.writeValueAsString(transformRequest);
String transformationReplyAsString = mockMvc.perform(MockMvcRequestBuilders.post("/transform")
String transformationReplyAsString = mockMvc.perform(
MockMvcRequestBuilders.post("/transform")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).content(tr))
.andExpect(status().is(HttpStatus.CREATED.value()))
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE).content(tr))
.andExpect(
status().is(HttpStatus.CREATED.value()))
.andReturn().getResponse().getContentAsString();
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, TransformReply.class);
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString,
TransformReply.class);
// Assert the reply
assertEquals(transformRequest.getRequestId(), transformReply.getRequestId());
assertEquals(transformRequest.getClientData(), transformReply.getClientData());
assertEquals(transformRequest.getSchema(), transformReply.getSchema());
}
}

View File

@@ -48,7 +48,8 @@ public class TikaQueueTransformServiceIT extends AbstractQueueTransformServiceIT
@Override
protected TransformRequest buildRequest()
{
return TransformRequest.builder()
return TransformRequest
.builder()
.withRequestId(UUID.randomUUID().toString())
.withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING)
.withTargetMediaType(MIMETYPE_TEXT_PLAIN)

View File

@@ -26,7 +26,6 @@
*/
package org.alfresco.transformer;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
@@ -35,15 +34,18 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import io.micrometer.core.instrument.MeterRegistry;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Application
{
@Value("${container.name}")
private String containerName;
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags()
{
return registry -> registry.config().commonTags("containerName", containerName);
}

View File

@@ -66,11 +66,14 @@ public class MiscController extends AbstractTransformerController
public MiscController()
{
logger.info("--------------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info("The transformers in this project use libraries from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\\\ 2.0.txt");
logger.info(
"--------------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"The transformers in this project use libraries from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\\\ 2.0.txt");
logger.info("Additional libraries used:");
logger.info("* htmlparser http://htmlparser.sourceforge.net/license.html");
logger.info("--------------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info(
"--------------------------------------------------------------------------------------------------------------------------------------------------------------");
}
@Override
@@ -92,7 +95,7 @@ public class MiscController extends AbstractTransformerController
// See the Javadoc on this method and Probes.md for the choice of these values.
return new ProbeTestTransform(this, "quick.html", "quick.txt",
119, 30, 150, 1024,
60*2+1,60*2)
60 * 2 + 1, 60 * 2)
{
@Override
protected void executeTransformCommand(File sourceFile, File targetFile)
@@ -100,22 +103,27 @@ public class MiscController extends AbstractTransformerController
Map<String, String> parameters = new HashMap<>();
parameters.put(SOURCE_ENCODING, "UTF-8");
parameters.put(TARGET_ENCODING, "UTF-8");
transformer.transform(sourceFile, targetFile, MIMETYPE_HTML, MIMETYPE_TEXT_PLAIN, parameters);
transformer.transform(sourceFile, targetFile, MIMETYPE_HTML, MIMETYPE_TEXT_PLAIN,
parameters);
}
};
}
@Override
public void processTransform(File sourceFile, File targetFile, Map<String, String> transformOptions, Long timeout)
public void processTransform(File sourceFile, File targetFile,
Map<String, String> transformOptions, Long timeout)
{
if (logger.isDebugEnabled())
{
logger.debug("Processing request with: sourceFile '{}', targetFile '{}', transformOptions" +
logger.debug(
"Processing request with: sourceFile '{}', targetFile '{}', transformOptions" +
" '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout);
}
String sourceMimetype = transformOptions.get("sourceMimetype");
String targetMimetype = transformOptions.get("targetMimetype");
transformer.transform(sourceFile, targetFile, sourceMimetype, targetMimetype, transformOptions);
transformer.transform(sourceFile, targetFile, sourceMimetype, targetMimetype,
transformOptions);
}
@PostMapping(value = "/transform", consumes = MULTIPART_FORM_DATA_VALUE)
@@ -129,11 +137,13 @@ public class MiscController extends AbstractTransformerController
{
if (logger.isDebugEnabled())
{
logger.debug("Processing request with: sourceMimetype '{}', targetMimetype '{}' , targetExtension '{}' " +
logger.debug(
"Processing request with: sourceMimetype '{}', targetMimetype '{}' , targetExtension '{}' " +
", parameters '{}'", sourceMimetype, targetMimetype, targetExtension, parameters);
}
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), targetExtension);
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(),
targetExtension);
getProbeTestTransform().incrementTransformerCount();
File sourceFile = createSourceFile(request, sourceMultipartFile);
File targetFile = createTargetFile(request, targetFilename);

View File

@@ -26,12 +26,9 @@
*/
package org.alfresco.transformer.transformers;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.transform.client.model.Mimetype;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_KEYNOTE;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_NUMBERS;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_PAGES;
import java.io.BufferedInputStream;
import java.io.File;
@@ -43,9 +40,12 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_KEYNOTE;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_NUMBERS;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_PAGES;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.transform.client.model.Mimetype;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Converts Apple iWorks files to JPEGs for thumbnailing & previewing.
@@ -60,7 +60,8 @@ import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_PAGES;
*/
public class AppleIWorksContentTransformer implements SelectableTransformer
{
private static final Logger logger = LoggerFactory.getLogger(AppleIWorksContentTransformer.class);
private static final Logger logger = LoggerFactory.getLogger(
AppleIWorksContentTransformer.class);
// Apple's zip entry names for previews in iWorks have changed over time.
private static final List<String> PDF_PATHS = Arrays.asList(
@@ -72,7 +73,8 @@ public class AppleIWorksContentTransformer implements SelectableTransformer
// (53 x 41) preview-micro.jpg
@Override
public boolean isTransformable(String sourceMimetype, String targetMimetype, Map<String, String> parameters)
public boolean isTransformable(String sourceMimetype, String targetMimetype,
Map<String, String> parameters)
{
boolean transformable = MIMETYPE_IWORK_KEYNOTE.equals(sourceMimetype)
|| MIMETYPE_IWORK_NUMBERS.equals(sourceMimetype)
@@ -86,20 +88,22 @@ public class AppleIWorksContentTransformer implements SelectableTransformer
final String sourceMimetype = parameters.get(SOURCE_MIMETYPE);
final String targetMimetype = parameters.get(TARGET_MIMETYPE);
if(logger.isDebugEnabled())
if (logger.isDebugEnabled())
{
logger.debug("Performing IWorks to jpeg transform with sourceMimetype=" + sourceMimetype
+ " targetMimetype=" + targetMimetype);
}
// iWorks files are zip (or package) files.
// If it's not a zip file, the resultant ZipException will be caught as an IOException below.
try (ZipArchiveInputStream iWorksZip = new ZipArchiveInputStream( new BufferedInputStream( new FileInputStream(sourceFile))))
try (ZipArchiveInputStream iWorksZip = new ZipArchiveInputStream(
new BufferedInputStream(new FileInputStream(sourceFile))))
{
// Look through the zip file entries for the preview/thumbnail.
List<String> paths = Mimetype.MIMETYPE_IMAGE_JPEG.equals(targetMimetype) ? JPG_PATHS : PDF_PATHS;
List<String> paths = Mimetype.MIMETYPE_IMAGE_JPEG.equals(
targetMimetype) ? JPG_PATHS : PDF_PATHS;
ZipArchiveEntry entry;
boolean found = false;
while ((entry=iWorksZip.getNextZipEntry()) != null)
while ((entry = iWorksZip.getNextZipEntry()) != null)
{
String name = entry.getName();
if (paths.contains(name))
@@ -110,14 +114,17 @@ public class AppleIWorksContentTransformer implements SelectableTransformer
}
}
if (! found)
if (!found)
{
throw new AlfrescoRuntimeException("The source " + sourceMimetype + " file did not contain a " + targetMimetype + " preview");
throw new AlfrescoRuntimeException(
"The source " + sourceMimetype + " file did not contain a " + targetMimetype + " preview");
}
}
catch (IOException e)
{
throw new AlfrescoRuntimeException("Unable to transform " + sourceMimetype + " file. It should have been a zip format file.", e);
throw new AlfrescoRuntimeException(
"Unable to transform " + sourceMimetype + " file. It should have been a zip format file.",
e);
}
}
}

View File

@@ -26,16 +26,12 @@
*/
package org.alfresco.transformer.transformers;
import org.htmlparser.Parser;
import org.htmlparser.beans.StringBean;
import org.htmlparser.util.ParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_HTML;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLConnection;
@@ -43,8 +39,11 @@ import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.util.Map;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_HTML;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import org.htmlparser.Parser;
import org.htmlparser.beans.StringBean;
import org.htmlparser.util.ParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Content transformer which wraps the HTML Parser library for
@@ -67,30 +66,32 @@ import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
* the current settings around links and non-breaking spaces.
* </p>
*
* @author Derek Hulley
* @author eknizat
* @see <a href="http://htmlparser.sourceforge.net/">http://htmlparser.sourceforge.net</a>
* @see org.htmlparser.beans.StringBean
* @see <a href="http://sourceforge.net/tracker/?func=detail&aid=1644504&group_id=24399&atid=381401">HTML Parser</a>
*
* @author Derek Hulley
* @author eknizat
*/
public class HtmlParserContentTransformer implements SelectableTransformer
{
private static final Logger logger = LoggerFactory.getLogger(HtmlParserContentTransformer.class);
private static final Logger logger = LoggerFactory.getLogger(
HtmlParserContentTransformer.class);
@Override
public boolean isTransformable(String sourceMimetype, String targetMimetype, Map<String, String> parameters)
public boolean isTransformable(String sourceMimetype, String targetMimetype,
Map<String, String> parameters)
{
return MIMETYPE_HTML.equals(sourceMimetype) && MIMETYPE_TEXT_PLAIN.equals(targetMimetype);
}
@Override
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws Exception
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
Exception
{
String sourceEncoding = parameters.get(SOURCE_ENCODING);
checkEncodingParameter(sourceEncoding, SOURCE_ENCODING);
if(logger.isDebugEnabled())
if (logger.isDebugEnabled())
{
logger.debug("Performing HTML to text transform with sourceEncoding=" + sourceEncoding);
}
@@ -105,7 +106,8 @@ public class HtmlParserContentTransformer implements SelectableTransformer
String text = extractor.getStrings();
// write it to the writer
try ( Writer writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(targetFile))))
try (Writer writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(targetFile))))
{
writer.write(text);
}
@@ -117,17 +119,18 @@ public class HtmlParserContentTransformer implements SelectableTransformer
{
if (encoding != null && !Charset.isSupported(encoding))
{
throw new IllegalArgumentException(paramterName + "=" + encoding + " is not supported by the JVM.");
throw new IllegalArgumentException(
paramterName + "=" + encoding + " is not supported by the JVM.");
}
}
catch (IllegalCharsetNameException e)
{
throw new IllegalArgumentException(paramterName + "=" + encoding + " is not a valid encoding.");
throw new IllegalArgumentException(
paramterName + "=" + encoding + " is not a valid encoding.");
}
}
/**
*
* <p>
* This code is based on a class of the same name, originally implemented in alfresco-repository.
* </p>
@@ -139,8 +142,6 @@ public class HtmlParserContentTransformer implements SelectableTransformer
* This allows us to correctly handle HTML files where the encoding
* is specified against the content property (rather than in the
* HTML Head Meta), see ALF-10466 for details.
*
*
*/
private class EncodingAwareStringBean extends StringBean
{
@@ -158,7 +159,7 @@ public class HtmlParserContentTransformer implements SelectableTransformer
String previousURL = getURL();
String newURL = file.getAbsolutePath();
if ( (previousURL == null) || (!newURL.equals(previousURL)) )
if ((previousURL == null) || (!newURL.equals(previousURL)))
{
try
{
@@ -178,8 +179,10 @@ public class HtmlParserContentTransformer implements SelectableTransformer
mParser.setEncoding(encoding);
}
mPropertySupport.firePropertyChange(StringBean.PROP_URL_PROPERTY, previousURL, getURL());
mPropertySupport.firePropertyChange(StringBean.PROP_CONNECTION_PROPERTY, conn, mParser.getConnection());
mPropertySupport.firePropertyChange(StringBean.PROP_URL_PROPERTY, previousURL,
getURL());
mPropertySupport.firePropertyChange(StringBean.PROP_CONNECTION_PROPERTY, conn,
mParser.getConnection());
setStrings();
}
catch (ParserException pe)
@@ -189,7 +192,8 @@ public class HtmlParserContentTransformer implements SelectableTransformer
}
}
public String getEncoding(){
public String getEncoding()
{
return mParser.getEncoding();
}
}

View File

@@ -26,6 +26,15 @@
*/
package org.alfresco.transformer.transformers;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.transform.client.model.Mimetype;
import org.apache.poi.openxml4j.opc.OPCPackage;
@@ -36,16 +45,6 @@ import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Extracts out Thumbnail JPEGs from OOXML files for thumbnailing & previewing.
* This transformer will only work for OOXML files where thumbnailing was enabled,
@@ -53,14 +52,13 @@ import java.util.Map;
*
* @author Nick Burch
* @author eknizat
*
*/
public class OOXMLThumbnailContentTransformer implements SelectableTransformer
{
private static final Logger logger = LoggerFactory.getLogger(OOXMLThumbnailContentTransformer.class);
private static final Logger logger = LoggerFactory.getLogger(
OOXMLThumbnailContentTransformer.class);
private static final List<String> OOXML_MIMETYPES = Arrays.asList(new String[]{
Mimetype.MIMETYPE_OPENXML_WORDPROCESSING,
private static final List<String> OOXML_MIMETYPES = Arrays.asList(Mimetype.MIMETYPE_OPENXML_WORDPROCESSING,
Mimetype.MIMETYPE_OPENXML_WORDPROCESSING_MACRO,
Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE,
Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO,
@@ -78,22 +76,25 @@ public class OOXMLThumbnailContentTransformer implements SelectableTransformer
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_MACRO,
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO,
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_ADDIN_MACRO,
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO});
Mimetype.MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO);
@Override
public boolean isTransformable(String sourceMimetype, String targetMimetype, Map<String, String> parameters)
public boolean isTransformable(String sourceMimetype, String targetMimetype,
Map<String, String> parameters)
{
// only support [OOXML] -> JPEG
return Mimetype.MIMETYPE_IMAGE_JPEG.equals(targetMimetype) && OOXML_MIMETYPES.contains(sourceMimetype);
return Mimetype.MIMETYPE_IMAGE_JPEG.equals(targetMimetype) && OOXML_MIMETYPES.contains(
sourceMimetype);
}
@Override
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws Exception
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
Exception
{
final String sourceMimetype = parameters.get(SOURCE_MIMETYPE);
final String targetMimetype = parameters.get(TARGET_MIMETYPE);
if(logger.isDebugEnabled())
if (logger.isDebugEnabled())
{
logger.debug("Performing OOXML to jpeg transform with sourceMimetype=" + sourceMimetype
+ " targetMimetype=" + targetMimetype);
@@ -103,7 +104,8 @@ public class OOXMLThumbnailContentTransformer implements SelectableTransformer
{
// Does it have a thumbnail?
PackageRelationshipCollection rels = pkg.getRelationshipsByType(PackageRelationshipTypes.THUMBNAIL);
PackageRelationshipCollection rels = pkg.getRelationshipsByType(
PackageRelationshipTypes.THUMBNAIL);
if (rels.size() > 0)
{
// Get the thumbnail part
@@ -118,7 +120,8 @@ public class OOXMLThumbnailContentTransformer implements SelectableTransformer
else
{
logger.debug("No thumbnail present in file.");
throw new Exception("No thumbnail present in file, unable to generate " + targetMimetype);
throw new Exception(
"No thumbnail present in file, unable to generate " + targetMimetype);
}
}
catch (IOException e)

View File

@@ -26,15 +26,10 @@
*/
package org.alfresco.transformer.transformers;
import org.alfresco.transformer.logging.LogEntry;
import java.io.File;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
/**
*
* Implemented by transformers used by {@link SelectingTransformer}.
*
* @author eknizat
@@ -48,21 +43,23 @@ public interface SelectableTransformer
/**
* Implementation of the actual transformation.
*
* @param sourceFile
* @param targetFile
* @param parameters
* @throws Exception
*/
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws Exception;
void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
Exception;
/**
* Determine whether this transformer is applicable for the given MIME types.
*
* @param sourceMimetype
* @param targetMimetype
* @param parameters
* @return
*/
public boolean isTransformable(String sourceMimetype, String targetMimetype, Map<String, String> parameters);
boolean isTransformable(String sourceMimetype, String targetMimetype,
Map<String, String> parameters);
}

View File

@@ -26,12 +26,8 @@
*/
package org.alfresco.transformer.transformers;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transformer.logging.LogEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import java.io.File;
import java.util.LinkedList;
@@ -39,16 +35,18 @@ import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transformer.logging.LogEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
*
* The SelectingTransformer selects a registered {@link SelectableTransformer}
* and delegates the transformation to its implementation.
*
* @author eknizat
*
*/
@Component
public class SelectingTransformer
@@ -68,17 +66,19 @@ public class SelectingTransformer
/**
* Performs a transform using a transformer selected based on the provided sourceMimetype and targetMimetype
*
* @param sourceFile File to transform from
* @param targetFile File to transform to
* @param sourceMimetype Mimetype of the source file
* @throws TransformException
*/
public void transform(File sourceFile, File targetFile, String sourceMimetype, String targetMimetype,
Map<String, String> parameters) throws TransformException
public void transform(File sourceFile, File targetFile, String sourceMimetype,
String targetMimetype, Map<String, String> parameters) throws TransformException
{
try
{
SelectableTransformer transformer = selectTransformer(sourceMimetype, targetMimetype, parameters);
SelectableTransformer transformer = selectTransformer(sourceMimetype, targetMimetype,
parameters);
logOptions(sourceFile, targetFile, parameters);
transformer.transform(sourceFile, targetFile, parameters);
}
@@ -112,12 +112,13 @@ public class SelectingTransformer
if (logger.isDebugEnabled())
{
logger.debug("Using " + transformer.getClass().getName()
+ " to transform from " + sourceMimetype + " to " + targetMimetype );
+ " to transform from " + sourceMimetype + " to " + targetMimetype);
}
return transformer;
}
}
throw new AlfrescoRuntimeException( "Could not select a transformer for sourceMimetype=" + sourceMimetype
throw new AlfrescoRuntimeException(
"Could not select a transformer for sourceMimetype=" + sourceMimetype
+ " targetMimetype=" + targetMimetype);
}
@@ -129,7 +130,8 @@ public class SelectingTransformer
private void logOptions(File sourceFile, File targetFile, Map<String, String> parameters)
{
StringJoiner sj = new StringJoiner(" ");
parameters.forEach( (k, v) -> sj.add("--" + k + "=" + v)); // keeping the existing style used in other T-Engines
parameters.forEach((k, v) -> sj.add(
"--" + k + "=" + v)); // keeping the existing style used in other T-Engines
sj.add(getExtension(sourceFile));
sj.add(getExtension(targetFile));
LogEntry.setOptions(sj.toString());

View File

@@ -26,29 +26,25 @@
*/
package org.alfresco.transformer.transformers;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_DITA;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_JAVASCRIPT;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.util.Map;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_DITA;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_JAVASCRIPT;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Converts any textual format to plain text.
@@ -60,7 +56,6 @@ import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
* This code is based on a class of the same name originally implemented in alfresco-repository.
* </p>
*
*
* @author Derek Hulley
* @author eknizat
*/
@@ -70,7 +65,8 @@ public class StringExtractingContentTransformer implements SelectableTransformer
private static final Log logger = LogFactory.getLog(StringExtractingContentTransformer.class);
@Override
public boolean isTransformable(String sourceMimetype, String targetMimetype, Map<String, String> parameters)
public boolean isTransformable(String sourceMimetype, String targetMimetype,
Map<String, String> parameters)
{
boolean transformable = (sourceMimetype.startsWith("text/")
|| MIMETYPE_JAVASCRIPT.equals(sourceMimetype)
@@ -88,13 +84,14 @@ public class StringExtractingContentTransformer implements SelectableTransformer
* be unformatted but valid.
*/
@Override
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws Exception
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
Exception
{
String sourceEncoding = parameters.get(SOURCE_ENCODING);
String targetEncoding = parameters.get(TARGET_ENCODING);
if(logger.isDebugEnabled())
if (logger.isDebugEnabled())
{
logger.debug("Performing text to text transform with sourceEncoding=" + sourceEncoding
+ " targetEncoding=" + targetEncoding);
@@ -107,23 +104,27 @@ public class StringExtractingContentTransformer implements SelectableTransformer
// Build reader
if (sourceEncoding == null)
{
charReader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile)));
charReader = new BufferedReader(
new InputStreamReader(new FileInputStream(sourceFile)));
}
else
{
checkEncodingParameter(sourceEncoding, SOURCE_ENCODING);
charReader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile), sourceEncoding));
charReader = new BufferedReader(
new InputStreamReader(new FileInputStream(sourceFile), sourceEncoding));
}
// Build writer
if (targetEncoding == null)
{
charWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile)));
charWriter = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(targetFile)));
}
else
{
checkEncodingParameter( targetEncoding, TARGET_ENCODING);
charWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), targetEncoding));
checkEncodingParameter(targetEncoding, TARGET_ENCODING);
charWriter = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(targetFile), targetEncoding));
}
// copy from the one to the other
@@ -157,12 +158,14 @@ public class StringExtractingContentTransformer implements SelectableTransformer
{
if (!Charset.isSupported(encoding))
{
throw new IllegalArgumentException(paramterName + "=" + encoding + " is not supported by the JVM.");
throw new IllegalArgumentException(
paramterName + "=" + encoding + " is not supported by the JVM.");
}
}
catch (IllegalCharsetNameException e)
{
throw new IllegalArgumentException(paramterName + "=" + encoding + " is not a valid encoding.");
throw new IllegalArgumentException(
paramterName + "=" + encoding + " is not a valid encoding.");
}
}
}

View File

@@ -26,14 +26,11 @@
*/
package org.alfresco.transformer.transformers;
import org.alfresco.error.AlfrescoRuntimeException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.tools.TextToPDF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_DITA;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_PDF;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_CSV;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_XML;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
@@ -49,14 +46,16 @@ import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_DITA;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_PDF;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_CSV;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_XML;
import org.alfresco.error.AlfrescoRuntimeException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.tools.TextToPDF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* <p>
* This code is based on a class of the same name originally implemented in alfresco-repository.
* </p>
@@ -65,7 +64,7 @@ import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_XML;
*
* @author Derek Hulley
* @author eknizat
*/
*/
public class TextToPdfContentTransformer implements SelectableTransformer
{
private static final Logger logger = LoggerFactory.getLogger(TextToPdfContentTransformer.class);
@@ -83,11 +82,12 @@ public class TextToPdfContentTransformer implements SelectableTransformer
{
try
{
transformer.setFont(transformer.getStandardFont(fontName));
transformer.setFont(PagedTextToPDF.getStandardFont(fontName));
}
catch (Throwable e)
{
throw new AlfrescoRuntimeException("Unable to set Standard Font for PDF generation: " + fontName, e);
throw new AlfrescoRuntimeException(
"Unable to set Standard Font for PDF generation: " + fontName, e);
}
}
@@ -99,29 +99,32 @@ public class TextToPdfContentTransformer implements SelectableTransformer
}
catch (Throwable e)
{
throw new AlfrescoRuntimeException("Unable to set Font Size for PDF generation: " + fontSize);
throw new AlfrescoRuntimeException(
"Unable to set Font Size for PDF generation: " + fontSize);
}
}
@Override
public boolean isTransformable(String sourceMimetype, String targetMimetype, Map<String, String> parameters)
public boolean isTransformable(String sourceMimetype, String targetMimetype,
Map<String, String> parameters)
{
boolean transformable = ( (MIMETYPE_TEXT_PLAIN.equals(sourceMimetype)
boolean transformable = ((MIMETYPE_TEXT_PLAIN.equals(sourceMimetype)
|| MIMETYPE_TEXT_CSV.equals(sourceMimetype)
|| MIMETYPE_DITA.equals(sourceMimetype)
|| MIMETYPE_XML.equals(sourceMimetype) )
|| MIMETYPE_XML.equals(sourceMimetype))
&& MIMETYPE_PDF.equals(targetMimetype));
return transformable;
}
@Override
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws Exception
public void transform(File sourceFile, File targetFile, Map<String, String> parameters) throws
Exception
{
String sourceEncoding = parameters.get(SOURCE_ENCODING);
String stringPageLimit = parameters.get(PAGE_LIMIT);
int pageLimit = -1;
if ( stringPageLimit != null)
if (stringPageLimit != null)
{
pageLimit = parseInt(stringPageLimit, PAGE_LIMIT);
}
@@ -148,18 +151,19 @@ public class TextToPdfContentTransformer implements SelectableTransformer
protected InputStreamReader buildReader(InputStream is, String encoding)
{
// If they gave an encoding, try to use it
if(encoding != null)
if (encoding != null)
{
Charset charset = null;
try
{
charset = Charset.forName(encoding);
} catch(Exception e)
}
catch (Exception e)
{
logger.warn("JVM doesn't understand encoding '" + encoding +
"' when transforming text to pdf");
}
if(charset != null)
if (charset != null)
{
logger.debug("Processing plain text in encoding " + charset.displayName());
return new InputStreamReader(is, charset);
@@ -175,24 +179,31 @@ public class TextToPdfContentTransformer implements SelectableTransformer
{
// REPO-1066: duplicating the following lines from org.apache.pdfbox.tools.TextToPDF because they made them private
// before the upgrade to pdfbox 2.0.8, in pdfbox 1.8, this piece of code was public in org.apache.pdfbox.pdmodel.font.PDType1Font
static PDType1Font getStandardFont(String name) {
return (PDType1Font)STANDARD_14.get(name);
static PDType1Font getStandardFont(String name)
{
return STANDARD_14.get(name);
}
private static final Map<String, PDType1Font> STANDARD_14 = new HashMap<String, PDType1Font>();
static
{
STANDARD_14.put(PDType1Font.TIMES_ROMAN.getBaseFont(), PDType1Font.TIMES_ROMAN);
STANDARD_14.put(PDType1Font.TIMES_BOLD.getBaseFont(), PDType1Font.TIMES_BOLD);
STANDARD_14.put(PDType1Font.TIMES_ITALIC.getBaseFont(), PDType1Font.TIMES_ITALIC);
STANDARD_14.put(PDType1Font.TIMES_BOLD_ITALIC.getBaseFont(), PDType1Font.TIMES_BOLD_ITALIC);
STANDARD_14.put(PDType1Font.TIMES_BOLD_ITALIC.getBaseFont(),
PDType1Font.TIMES_BOLD_ITALIC);
STANDARD_14.put(PDType1Font.HELVETICA.getBaseFont(), PDType1Font.HELVETICA);
STANDARD_14.put(PDType1Font.HELVETICA_BOLD.getBaseFont(), PDType1Font.HELVETICA_BOLD);
STANDARD_14.put(PDType1Font.HELVETICA_OBLIQUE.getBaseFont(), PDType1Font.HELVETICA_OBLIQUE);
STANDARD_14.put(PDType1Font.HELVETICA_BOLD_OBLIQUE.getBaseFont(), PDType1Font.HELVETICA_BOLD_OBLIQUE);
STANDARD_14.put(PDType1Font.HELVETICA_OBLIQUE.getBaseFont(),
PDType1Font.HELVETICA_OBLIQUE);
STANDARD_14.put(PDType1Font.HELVETICA_BOLD_OBLIQUE.getBaseFont(),
PDType1Font.HELVETICA_BOLD_OBLIQUE);
STANDARD_14.put(PDType1Font.COURIER.getBaseFont(), PDType1Font.COURIER);
STANDARD_14.put(PDType1Font.COURIER_BOLD.getBaseFont(), PDType1Font.COURIER_BOLD);
STANDARD_14.put(PDType1Font.COURIER_OBLIQUE.getBaseFont(), PDType1Font.COURIER_OBLIQUE);
STANDARD_14.put(PDType1Font.COURIER_BOLD_OBLIQUE.getBaseFont(), PDType1Font.COURIER_BOLD_OBLIQUE);
STANDARD_14.put(PDType1Font.COURIER_BOLD_OBLIQUE.getBaseFont(),
PDType1Font.COURIER_BOLD_OBLIQUE);
STANDARD_14.put(PDType1Font.SYMBOL.getBaseFont(), PDType1Font.SYMBOL);
STANDARD_14.put(PDType1Font.ZAPF_DINGBATS.getBaseFont(), PDType1Font.ZAPF_DINGBATS);
}
@@ -210,23 +221,23 @@ public class TextToPdfContentTransformer implements SelectableTransformer
try
{
final int margin = 40;
float height = getFont().getFontDescriptor().getFontBoundingBox().getHeight()/1000;
float height = getFont().getFontDescriptor().getFontBoundingBox().getHeight() / 1000;
//calculate font height and increase by 5 percent.
height = height*getFontSize()*1.05f;
height = height * getFontSize() * 1.05f;
doc = new PDDocument();
BufferedReader data = new BufferedReader( text );
BufferedReader data = new BufferedReader(text);
String nextLine = null;
PDPage page = new PDPage();
PDPageContentStream contentStream = null;
float y = -1;
float maxStringLength = page.getMediaBox().getWidth() - 2*margin;
float maxStringLength = page.getMediaBox().getWidth() - 2 * margin;
// There is a special case of creating a PDF document from an empty string.
boolean textIsEmpty = true;
outer:
while( (nextLine = data.readLine()) != null )
while ((nextLine = data.readLine()) != null)
{
// The input text is nonEmpty. New pages will be created and added
@@ -234,29 +245,30 @@ public class TextToPdfContentTransformer implements SelectableTransformer
// the text.
textIsEmpty = false;
String[] lineWords = nextLine.trim().split( " " );
String[] lineWords = nextLine.trim().split(" ");
int lineIndex = 0;
while( lineIndex < lineWords.length )
while (lineIndex < lineWords.length)
{
StringBuffer nextLineToDraw = new StringBuffer();
float lengthIfUsingNextWord = 0;
do
{
nextLineToDraw.append( lineWords[lineIndex] );
nextLineToDraw.append( " " );
nextLineToDraw.append(lineWords[lineIndex]);
nextLineToDraw.append(" ");
lineIndex++;
if( lineIndex < lineWords.length )
if (lineIndex < lineWords.length)
{
String lineWithNextWord = nextLineToDraw.toString() + lineWords[lineIndex];
lengthIfUsingNextWord =
(getFont().getStringWidth( lineWithNextWord )/1000) * getFontSize();
(getFont().getStringWidth(
lineWithNextWord) / 1000) * getFontSize();
}
}
while( lineIndex < lineWords.length &&
lengthIfUsingNextWord < maxStringLength );
if( y < margin )
while (lineIndex < lineWords.length &&
lengthIfUsingNextWord < maxStringLength);
if (y < margin)
{
int test = pageCount +1;
int test = pageCount + 1;
if (pageLimit > 0 && (pageCount++ >= pageLimit))
{
// pageLimits.getAction().throwIOExceptionIfRequired("Page limit ("+pageLimit+
@@ -267,8 +279,8 @@ public class TextToPdfContentTransformer implements SelectableTransformer
// We have crossed the end-of-page boundary and need to extend the
// document by another page.
page = new PDPage();
doc.addPage( page );
if( contentStream != null )
doc.addPage(page);
if (contentStream != null)
{
contentStream.endText();
contentStream.close();
@@ -278,17 +290,17 @@ public class TextToPdfContentTransformer implements SelectableTransformer
contentStream.beginText();
y = page.getMediaBox().getHeight() - margin + height;
contentStream.moveTextPositionByAmount(
margin, y );
margin, y);
}
//System.out.println( "Drawing string at " + x + "," + y );
if( contentStream == null )
if (contentStream == null)
{
throw new IOException( "Error:Expected non-null content stream." );
throw new IOException("Error:Expected non-null content stream.");
}
contentStream.moveTextPositionByAmount( 0, -height);
contentStream.moveTextPositionByAmount(0, -height);
y -= height;
contentStream.drawString( nextLineToDraw.toString() );
contentStream.drawString(nextLineToDraw.toString());
}
}
@@ -300,15 +312,15 @@ public class TextToPdfContentTransformer implements SelectableTransformer
doc.addPage(page);
}
if( contentStream != null )
if (contentStream != null)
{
contentStream.endText();
contentStream.close();
}
}
catch( IOException io )
catch (IOException io)
{
if( doc != null )
if (doc != null)
{
doc.close();
}

View File

@@ -26,30 +26,7 @@
*/
package org.alfresco.transformer;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transformer.transformers.AppleIWorksContentTransformer;
import org.alfresco.transformer.transformers.HtmlParserContentTransformer;
import org.alfresco.transformer.transformers.SelectingTransformer;
import org.alfresco.transformer.transformers.StringExtractingContentTransformer;
import org.alfresco.transformer.transformers.TextToPdfContentTransformer;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_HTML;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IMAGE_JPEG;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_KEYNOTE;
@@ -64,6 +41,26 @@ import static org.springframework.http.HttpStatus.OK;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transformer.transformers.SelectingTransformer;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
@RunWith(SpringRunner.class)
@WebMvcTest(MiscController.class)
@Import({SelectingTransformer.class})
@@ -88,12 +85,13 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
expectedSourceFileBytes = readTestFile(sourceExtension);
expectedTargetFileBytes = readTestFile(targetExtension);
//expectedTargetFileBytes = null;
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes);
sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype,
expectedSourceFileBytes);
}
@Override
protected void mockTransformCommand(String sourceExtension, String targetExtension, String sourceMimetype, boolean readTargetFileBytes) throws IOException
protected void mockTransformCommand(String sourceExtension, String targetExtension,
String sourceMimetype, boolean readTargetFileBytes) throws IOException
{
}
@@ -110,7 +108,8 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Override
// Add extra required parameters to the request.
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params)
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile,
String... params)
{
return super.mockMvcRequest(url, sourceFile, params)
.param("targetEncoding", targetEncoding)
@@ -130,7 +129,7 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Test
public void testHTMLtoString() throws Exception
{
final String NEWLINE = System.getProperty ("line.separator");
final String NEWLINE = System.getProperty("line.separator");
final String TITLE = "Testing!";
final String TEXT_P1 = "This is some text in English";
final String TEXT_P2 = "This is more text in English";
@@ -150,8 +149,9 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
"UTF-8",
expected.getBytes());
String contentResult = new String(result.getResponse().getContentAsByteArray(), targetEncoding);
assertTrue("The content did not include \""+expected, contentResult.contains(expected));
String contentResult = new String(result.getResponse().getContentAsByteArray(),
targetEncoding);
assertTrue("The content did not include \"" + expected, contentResult.contains(expected));
}
@Test
@@ -161,7 +161,7 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
byte[] content = null;
try
{
content = "azAz10!<21>$%^&*()\t\r\n".getBytes("UTF-8");
content = "azAz10!<21>$%^&*()\t\r\n".getBytes(UTF_8);
expected = new String(content, "MacDingbat");
}
catch (UnsupportedEncodingException e)
@@ -177,8 +177,9 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
"UTF-8",
content);
String contentResult = new String(result.getResponse().getContentAsByteArray(), targetEncoding);
assertTrue("The content did not include \""+expected, contentResult.contains(expected));
String contentResult = new String(result.getResponse().getContentAsByteArray(),
targetEncoding);
assertTrue("The content did not include \"" + expected, contentResult.contains(expected));
}
@Test
@@ -195,7 +196,8 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
"UTF-8",
content);
assertEquals("Returned content should be empty for an empty source file", 0, result.getResponse().getContentLength());
assertEquals("Returned content should be empty for an empty source file", 0,
result.getResponse().getContentLength());
}
@Test
@@ -203,7 +205,7 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
{
StringBuilder sb = new StringBuilder();
String expected = null;
for (int i=1; i<=5; i++)
for (int i = 1; i <= 5; i++)
{
sb.append(i);
sb.append(" I must not talk in class or feed my homework to my cat.\n");
@@ -257,23 +259,29 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
imageBasedTransform("docx", MIMETYPE_OPENXML_WORDPROCESSING, MIMETYPE_IMAGE_JPEG, "jpeg");
}
private void imageBasedTransform(String sourceExtension, String sourceMimetype, String targetMimetype, String targetExtension) throws Exception
private void imageBasedTransform(String sourceExtension, String sourceMimetype,
String targetMimetype, String targetExtension) throws Exception
{
MockMultipartFile sourceFilex = new MockMultipartFile("file", "test_file." + sourceExtension, sourceMimetype, readTestFile(sourceExtension));
MockMultipartFile sourceFilex = new MockMultipartFile("file",
"test_file." + sourceExtension, sourceMimetype, readTestFile(sourceExtension));
MockHttpServletRequestBuilder requestBuilder = super.mockMvcRequest("/transform", sourceFilex)
MockHttpServletRequestBuilder requestBuilder = super
.mockMvcRequest("/transform", sourceFilex)
.param("targetExtension", "jpeg")
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype);
.param("sourceMimetype",
sourceMimetype);
MvcResult result = mockMvc.perform(requestBuilder)
MvcResult result = mockMvc
.perform(requestBuilder)
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''test_file." + targetExtension))
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''test_file." + targetExtension))
.andReturn();
assertTrue("Expected image content but content is empty.",result.getResponse().getContentLengthLong() > 0L);
assertTrue("Expected image content but content is empty.",
result.getResponse().getContentLengthLong() > 0L);
}
private MvcResult sendText(String sourceExtension,
String sourceEncoding,
String sourceMimetype,
@@ -282,20 +290,23 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
String targetEncoding,
byte[] content) throws Exception
{
MockMultipartFile sourceFilex = new MockMultipartFile("file", "test_file." + sourceExtension, sourceMimetype, content);
MockMultipartFile sourceFilex = new MockMultipartFile("file",
"test_file." + sourceExtension, sourceMimetype, content);
MockHttpServletRequestBuilder requestBuilder = super.mockMvcRequest("/transform", sourceFilex)
MockHttpServletRequestBuilder requestBuilder = super
.mockMvcRequest("/transform", sourceFilex)
.param("targetExtension", targetExtension)
.param("targetEncoding", targetEncoding)
.param("targetMimetype", targetMimetype)
.param("sourceEncoding", sourceEncoding)
.param("sourceMimetype", sourceMimetype);
MvcResult result = mockMvc.perform(requestBuilder)
MvcResult result = mockMvc
.perform(requestBuilder)
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= "+targetEncoding+"''test_file." + targetExtension)).
andReturn();
.andExpect(header().string("Content-Disposition",
"attachment; filename*= " + targetEncoding + "''test_file." + targetExtension))
.andReturn();
return result;
}
@@ -307,5 +318,4 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
text = text.replaceAll("\\n", "");
return text;
}
}

View File

@@ -26,16 +26,16 @@
*/
package org.alfresco.transformer;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_HTML;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import java.util.UUID;
import org.alfresco.transform.client.model.TransformRequest;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.UUID;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_HTML;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {"activemq.url=nio://localhost:61616"})
@@ -44,7 +44,8 @@ public class MiscQueueTransformServiceIT extends AbstractQueueTransformServiceIT
@Override
protected TransformRequest buildRequest()
{
return TransformRequest.builder()
return TransformRequest
.builder()
.withRequestId(UUID.randomUUID().toString())
.withSourceMediaType(MIMETYPE_HTML)
.withTargetMediaType(MIMETYPE_TEXT_PLAIN)

View File

@@ -26,13 +26,8 @@
*/
package org.alfresco.transformer.transformers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import static org.alfresco.transformer.transformers.StringExtractingContentTransformer.SOURCE_ENCODING;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileOutputStream;
@@ -41,9 +36,11 @@ import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import static org.alfresco.transformer.transformers.StringExtractingContentTransformer.SOURCE_ENCODING;
import static org.alfresco.transformer.transformers.StringExtractingContentTransformer.TARGET_ENCODING;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@Import(HtmlParserContentTransformer.class)
@@ -63,7 +60,7 @@ public class HtmlParserContentTransformerTest
@Test
public void testEncodingHandling() throws Exception
{
final String NEWLINE = System.getProperty ("line.separator");
final String NEWLINE = System.getProperty("line.separator");
final String TITLE = "Testing!";
final String TEXT_P1 = "This is some text in English";
final String TEXT_P2 = "This is more text in English";
@@ -82,7 +79,7 @@ public class HtmlParserContentTransformerTest
{
// Content set to ISO 8859-1
tmpS = File.createTempFile("AlfrescoTestSource_", ".html");
writeToFile(tmpS, partA+partB+partC, "ISO-8859-1");
writeToFile(tmpS, partA + partB + partC, "ISO-8859-1");
tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt");
@@ -96,7 +93,7 @@ public class HtmlParserContentTransformerTest
// Content set to UTF-8
tmpS = File.createTempFile("AlfrescoTestSource_", ".html");
writeToFile(tmpS, partA+partB+partC, "UTF-8");
writeToFile(tmpS, partA + partB + partC, "UTF-8");
tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt");
parameters = new HashMap<>();
@@ -106,10 +103,9 @@ public class HtmlParserContentTransformerTest
tmpS.delete();
tmpD.delete();
// Content set to UTF-16
tmpS = File.createTempFile("AlfrescoTestSource_", ".html");
writeToFile(tmpS, partA+partB+partC, "UTF-16");
writeToFile(tmpS, partA + partB + partC, "UTF-16");
tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt");
parameters = new HashMap<>();
@@ -129,9 +125,9 @@ public class HtmlParserContentTransformerTest
// Content set to ISO 8859-1, meta set to UTF-8
tmpS = File.createTempFile("AlfrescoTestSource_", ".html");
String str = partA+
String str = partA +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" +
partB+partC;
partB + partC;
writeToFile(tmpS, str, "UTF-8");
@@ -144,7 +140,6 @@ public class HtmlParserContentTransformerTest
tmpS.delete();
tmpD.delete();
// Note - we can't test UTF-16 with only a meta encoding,
// because without that the parser won't know about the
// 2 byte format so won't be able to identify the meta tag

View File

@@ -26,15 +26,8 @@
*/
package org.alfresco.transformer.transformers;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import static org.alfresco.transformer.transformers.TextToPdfContentTransformer.PAGE_LIMIT;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileOutputStream;
@@ -43,8 +36,14 @@ import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import static org.alfresco.transformer.transformers.TextToPdfContentTransformer.PAGE_LIMIT;
import static org.junit.Assert.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@Import(TextToPdfContentTransformer.class)
@@ -60,7 +59,6 @@ public class TextToPdfContentTransformerTest
transformer.setFontSize(20);
}
@Test
public void testUnlimitedPages() throws Exception
{
@@ -88,11 +86,11 @@ public class TextToPdfContentTransformerTest
private void transformTextAndCheckPageLength(int pageLimit) throws Exception
{
int pageLength = 32;
int lines = (pageLength+10) * ((pageLimit > 0) ? pageLimit : 1);
int lines = (pageLength + 10) * ((pageLimit > 0) ? pageLimit : 1);
StringBuilder sb = new StringBuilder();
String checkText = null;
int cutoff = pageLimit * pageLength;
for (int i=1; i<=lines; i++)
for (int i = 1; i <= lines; i++)
{
sb.append(i);
sb.append(" I must not talk in class or feed my homework to my cat.\n");
@@ -105,12 +103,12 @@ public class TextToPdfContentTransformerTest
transformTextAndCheck(text, "UTF-8", checkText, String.valueOf(pageLimit));
}
private void transformTextAndCheck(String text, String encoding, String checkText, String pageLimit) throws Exception
private void transformTextAndCheck(String text, String encoding, String checkText,
String pageLimit) throws Exception
{
// Get a reader for the text
File sourceFile = File.createTempFile("AlfrescoTestSource_", ".txt");
writeToFile(sourceFile,text, encoding);
writeToFile(sourceFile, text, encoding);
// And a temp writer
File targetFile = File.createTempFile("AlfrescoTestTarget_", ".pdf");

View File

@@ -26,6 +26,7 @@
*/
package org.alfresco.transformer;
import static java.util.stream.Collectors.joining;
import static org.alfresco.transformer.fs.FileManager.buildFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFileName;
import static org.alfresco.transformer.fs.FileManager.deleteFile;
@@ -39,7 +40,6 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Collectors;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
@@ -101,7 +101,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
*/
public abstract class AbstractTransformerController implements TransformController
{
private static final Logger logger = LoggerFactory.getLogger(AbstractTransformerController.class);
private static final Logger logger = LoggerFactory.getLogger(
AbstractTransformerController.class);
private static String ENGINE_CONFIG = "engine_config.json";
@Autowired
@@ -161,8 +163,11 @@ public abstract class AbstractTransformerController implements TransformControll
if (!errors.getAllErrors().isEmpty())
{
reply.setStatus(HttpStatus.BAD_REQUEST.value());
reply.setErrorDetails(errors.getAllErrors().stream().map(Object::toString)
.collect(Collectors.joining(", ")));
reply.setErrorDetails(errors
.getAllErrors()
.stream()
.map(Object::toString)
.collect(joining(", ")));
logger.error("Invalid request, sending {}", reply);
return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()));
@@ -246,7 +251,7 @@ public abstract class AbstractTransformerController implements TransformControll
reply.setStatus(e.getStatusCode().value());
reply.setErrorDetails(messageWithCause("Failed at writing the transformed file. ", e));
logger.error("Failed to save target file (HttpClientErrorException), sending {}" +
logger.error("Failed to save target file (HttpClientErrorException), sending " +
reply, e);
return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()));
}

View File

@@ -26,6 +26,8 @@
*/
package org.alfresco.transformer;
import java.util.Optional;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
@@ -44,8 +46,6 @@ import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.stereotype.Component;
import java.util.Optional;
/**
* Queue Transformer service.
* This service reads all the requests for the particular engine, forwards them to the worker
@@ -56,12 +56,11 @@ import java.util.Optional;
* created on 18/12/2018
*/
@Component
@ConditionalOnProperty(name="activemq.url")
@ConditionalOnProperty(name = "activemq.url")
public class QueueTransformService
{
private static final Logger logger = LoggerFactory.getLogger(QueueTransformService.class);
// TODO: I know this is not smart but all the the transformation logic is in the Controller.
// The controller also manages the probes. There's tons of refactoring needed there, hence this. Sorry.
@Autowired
@@ -98,7 +97,9 @@ public class QueueTransformService
}
catch (JMSException e)
{
logger.error("Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ", correlationId);
logger.error(
"Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ",
correlationId);
return;
}
@@ -171,15 +172,18 @@ public class QueueTransformService
}
}
private void replyWithInternalSvErr(final Destination destination, final String msg, final String correlationId)
private void replyWithInternalSvErr(final Destination destination, final String msg,
final String correlationId)
{
replyWithError(destination, HttpStatus.INTERNAL_SERVER_ERROR, msg, correlationId);
}
private void replyWithError(final Destination destination, final HttpStatus status, final String msg,
private void replyWithError(final Destination destination, final HttpStatus status,
final String msg,
final String correlationId)
{
final TransformReply reply = TransformReply.builder()
final TransformReply reply = TransformReply
.builder()
.withStatus(status.value())
.withErrorDetails(msg)
.build();

View File

@@ -26,12 +26,12 @@
*/
package org.alfresco.transformer.config;
import org.alfresco.transform.client.model.TransformRequestValidator;
import org.alfresco.transformer.TransformInterceptor;
import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.alfresco.transform.client.model.TransformRequestValidator;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -40,16 +40,18 @@ public class WebApplicationConfig implements WebMvcConfigurer
{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(transformInterceptor()).addPathPatterns("/transform", "/live", "/ready");
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(transformInterceptor()).addPathPatterns("/transform", "/live",
"/ready");
}
@Bean
public TransformInterceptor transformInterceptor() {
public TransformInterceptor transformInterceptor()
{
return new TransformInterceptor();
}
@Bean
public RestTemplate restTemplate()
{
@@ -57,7 +59,8 @@ public class WebApplicationConfig implements WebMvcConfigurer
}
@Bean
public AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient(){
public AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient()
{
return new AlfrescoSharedFileStoreClient();
}

View File

@@ -37,6 +37,7 @@ import org.alfresco.util.exec.RuntimeExec;
import org.alfresco.util.exec.RuntimeExec.ExecutionResult;
/**
*
*/
public abstract class AbstractCommandExecutor implements CommandExecutor
{

View File

@@ -51,6 +51,7 @@ import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils;
/**
*
*/
public class FileManager
{
@@ -76,7 +77,7 @@ public class FileManager
public static File buildFile(String filename)
{
filename = checkFilename( false, filename);
filename = checkFilename(false, filename);
LogEntry.setTarget(filename);
return TempFileProvider.createTempFile("target_", "_" + filename);
}
@@ -103,7 +104,8 @@ public class FileManager
{
String sourceOrTarget = source ? "source" : "target";
int statusCode = source ? BAD_REQUEST.value() : INTERNAL_SERVER_ERROR.value();
throw new TransformException(statusCode, "The " + sourceOrTarget + " filename was not supplied");
throw new TransformException(statusCode,
"The " + sourceOrTarget + " filename was not supplied");
}
return filename;
}
@@ -112,7 +114,8 @@ public class FileManager
{
try
{
Files.copy(multipartFile.getInputStream(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.copy(multipartFile.getInputStream(), file.toPath(),
StandardCopyOption.REPLACE_EXISTING);
}
catch (IOException e)
{
@@ -129,7 +132,8 @@ public class FileManager
}
catch (IOException e)
{
throw new TransformException(INSUFFICIENT_STORAGE.value(), "Failed to store the source file", e);
throw new TransformException(INSUFFICIENT_STORAGE.value(),
"Failed to store the source file", e);
}
}
@@ -171,7 +175,6 @@ public class FileManager
return filename;
}
/**
* Returns the file name for the target file
*
@@ -179,20 +182,24 @@ public class FileManager
* @param targetExtension File extension
* @return Target file name
*/
public static String createTargetFileName(String fileName, String targetExtension)
public static String createTargetFileName(final String fileName, final String targetExtension)
{
String targetFilename = null;
String sourceFilename = fileName;
sourceFilename = StringUtils.getFilename(sourceFilename);
if (sourceFilename != null && !sourceFilename.isEmpty())
final String sourceFilename = StringUtils.getFilename(fileName);
if (sourceFilename == null || sourceFilename.isEmpty())
{
String ext = StringUtils.getFilenameExtension(sourceFilename);
targetFilename = (ext != null && !ext.isEmpty()
? sourceFilename.substring(0, sourceFilename.length()-ext.length()-1)
: sourceFilename)+
'.'+targetExtension;
return null;
}
return targetFilename;
final String ext = StringUtils.getFilenameExtension(sourceFilename);
if (ext == null || ext.isEmpty())
{
return sourceFilename + '.' + targetExtension;
}
return sourceFilename.substring(0, sourceFilename.length() - ext.length() - 1) +
'.' + targetExtension;
}
/**
@@ -207,7 +214,7 @@ public class FileManager
{
String filename = multipartFile.getOriginalFilename();
long size = multipartFile.getSize();
filename = checkFilename( true, filename);
filename = checkFilename(true, filename);
File file = TempFileProvider.createTempFile("source_", "_" + filename);
request.setAttribute(SOURCE_FILE, file);
save(multipartFile, file);

View File

@@ -135,7 +135,7 @@ public final class LogEntry
int i = filename.lastIndexOf('.');
if (i != -1)
{
filename = filename.substring(i+1);
filename = filename.substring(i + 1);
}
return filename;
}
@@ -201,7 +201,8 @@ public final class LogEntry
if (logEntry.statusCode == OK.value())
{
logEntry.durationStreamOut = System.currentTimeMillis() - logEntry.start -
logEntry.durationStreamIn - max(logEntry.durationTransform, 0) - max(logEntry.durationDelay, 0);
logEntry.durationStreamIn - max(logEntry.durationTransform,
0) - max(logEntry.durationDelay, 0);
}
currentLogEntry.remove();
@@ -228,16 +229,18 @@ public final class LogEntry
public String getDuration()
{
long duration = durationStreamIn + max(durationTransform, 0) + max(durationDelay, 0) + max(durationStreamOut, 0);
long duration = durationStreamIn + max(durationTransform, 0) + max(durationDelay, 0) + max(
durationStreamOut, 0);
return duration <= 5
? ""
: time(duration)+
" ("+
(time(durationStreamIn)+' '+
time(durationTransform)+' '+
: time(duration) +
" (" +
(time(durationStreamIn) + ' ' +
time(durationTransform) + ' ' +
(durationDelay > 0
? time(durationDelay)+' '+(durationStreamOut < 0 ? "-" : time(durationStreamOut))
: time(durationStreamOut))).trim()+
? time(durationDelay) + ' ' + (durationStreamOut < 0 ? "-" : time(
durationStreamOut))
: time(durationStreamOut))).trim() +
")";
}
@@ -274,16 +277,16 @@ public final class LogEntry
private String time(long ms)
{
return ms == -1 ? "" : size(ms, "1ms",
new String[] { "ms", "s", "min", "hr" },
new long[] { 1000, 60*1000, 60*60*1000, Long.MAX_VALUE});
new String[]{"ms", "s", "min", "hr"},
new long[]{1000, 60 * 1000, 60 * 60 * 1000, Long.MAX_VALUE});
}
private String size(long size)
{
// TODO fix numeric overflow in TB expression
return size == -1 ? "" : size(size, "1 byte",
new String[] { "bytes", " KB", " MB", " GB", " TB" },
new long[] { 1024, 1024*1024, 1024*1024*1024, 1024*1024*1024*1024, Long.MAX_VALUE });
new String[]{"bytes", " KB", " MB", " GB", " TB"},
new long[]{1024, 1024 * 1024, 1024 * 1024 * 1024, 1024 * 1024 * 1024 * 1024, Long.MAX_VALUE});
}
private String size(long size, String singleValue, String[] units, long[] dividers)
@@ -293,16 +296,16 @@ public final class LogEntry
return singleValue;
}
long divider = 1;
for(int i = 0; i < units.length-1; i++)
for (int i = 0; i < units.length - 1; i++)
{
long nextDivider = dividers[i];
if(size < nextDivider)
if (size < nextDivider)
{
return unitFormat(size, divider, units[i]);
}
divider = nextDivider;
}
return unitFormat(size, divider, units[units.length-1]);
return unitFormat(size, divider, units[units.length - 1]);
}
private String unitFormat(long size, long divider, String unit)
@@ -311,7 +314,7 @@ public final class LogEntry
int decimalPoint = (int) size % 10;
StringBuilder sb = new StringBuilder();
sb.append(size/10);
sb.append(size / 10);
if (decimalPoint != 0)
{
sb.append(".");

View File

@@ -31,5 +31,5 @@ public interface StandardMessages
String LICENCE =
"License rights for this program may be obtained from Alfresco Software, Ltd. pursuant to a written agreement\n" +
"and any use of this program without such an agreement is prohibited.\n" +
"\n" ;
"\n";
}

View File

@@ -98,7 +98,8 @@ public class MessagingConfig implements JmsListenerConfigurer
@Bean
@ConditionalOnProperty(name = "activemq.url")
public Queue engineRequestQueue(@Value("${queue.engineRequestQueue}") String engineRequestQueueValue)
public Queue engineRequestQueue(
@Value("${queue.engineRequestQueue}") String engineRequestQueueValue)
{
return new ActiveMQQueue(engineRequestQueueValue);
}

View File

@@ -56,7 +56,8 @@ public class TransformReplySender
send(destination, reply, reply.getRequestId());
}
public void send(final Destination destination, final TransformReply reply, final String correlationId)
public void send(final Destination destination, final TransformReply reply,
final String correlationId)
{
try
{

View File

@@ -37,18 +37,18 @@ public class FileRefEntity
{
private String fileRef;
public FileRefEntity()
{
}
public FileRefEntity() {}
public FileRefEntity(String fileRef)
{
this.fileRef = fileRef;
}
public void setFileRef(String fileRef){
public void setFileRef(String fileRef)
{
this.fileRef = fileRef;
}
public String getFileRef()
{
return fileRef;

View File

@@ -43,8 +43,8 @@ import java.util.concurrent.atomic.AtomicLong;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.transformer.AbstractTransformerController;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transformer.AbstractTransformerController;
import org.alfresco.transformer.logging.LogEntry;
import org.alfresco.util.TempFileProvider;
import org.slf4j.Logger;
@@ -111,6 +111,7 @@ public abstract class ProbeTestTransform
/**
* See Probes.md for more info.
*
* @param expectedLength was the length of the target file during testing
* @param plusOrMinus simply allows for some variation in the transformed size caused by new versions of dates
* @param livenessPercent indicates that for this type of transform a variation up to 2 and a half times is not
@@ -126,13 +127,14 @@ public abstract class ProbeTestTransform
{
this.sourceFilename = sourceFilename;
this.targetFilename = targetFilename;
minExpectedLength = Math.max(0, expectedLength-plusOrMinus);
maxExpectedLength = expectedLength+plusOrMinus;
minExpectedLength = Math.max(0, expectedLength - plusOrMinus);
maxExpectedLength = expectedLength + plusOrMinus;
this.livenessPercent = (int) getPositiveLongEnv("livenessPercent", livenessPercent);
maxTransformCount = getPositiveLongEnv("maxTransforms", maxTransforms);
maxTransformTime = getPositiveLongEnv("maxTransformSeconds", maxTransformSeconds)*1000;
livenessTransformPeriod = getPositiveLongEnv("livenessTransformPeriodSeconds", livenessTransformPeriodSeconds)*1000;
maxTransformTime = getPositiveLongEnv("maxTransformSeconds", maxTransformSeconds) * 1000;
livenessTransformPeriod = getPositiveLongEnv("livenessTransformPeriodSeconds",
livenessTransformPeriodSeconds) * 1000;
livenessTransformEnabled = getBooleanEnvVar("livenessTransformEnabled", false);
}
@@ -238,39 +240,39 @@ public abstract class ProbeTestTransform
checkMaxTransformTimeAndCount(isLiveProbe);
return getProbeMessage(isLiveProbe)+message;
return getProbeMessage(isLiveProbe) + message;
}
private void checkMaxTransformTimeAndCount(boolean isLiveProbe)
{
if (die.get())
{
throw new TransformException(TOO_MANY_REQUESTS.value(), getMessagePrefix(isLiveProbe) +
"Transformer requested to die. A transform took longer than "+
(maxTransformTime *1000)+" seconds");
throw new TransformException(TOO_MANY_REQUESTS.value(),
getMessagePrefix(isLiveProbe) + "Transformer requested to die. A transform took " +
"longer than " + (maxTransformTime * 1000) + " seconds");
}
if (maxTransformCount > 0 && transformCount.get() > maxTransformCount)
{
throw new TransformException(TOO_MANY_REQUESTS.value(), getMessagePrefix(isLiveProbe) +
"Transformer requested to die. It has performed more than "+
maxTransformCount+" transformations");
throw new TransformException(TOO_MANY_REQUESTS.value(),
getMessagePrefix(isLiveProbe) + "Transformer requested to die. It has performed " +
"more than " + maxTransformCount + " transformations");
}
}
private File getSourceFile(HttpServletRequest request, boolean isLiveProbe)
{
incrementTransformerCount();
File sourceFile = TempFileProvider.createTempFile("source_", "_"+ sourceFilename);
File sourceFile = TempFileProvider.createTempFile("source_", "_" + sourceFilename);
request.setAttribute(SOURCE_FILE, sourceFile);
try (InputStream inputStream = this.getClass().getResourceAsStream('/'+sourceFilename))
try (InputStream inputStream = this.getClass().getResourceAsStream('/' + sourceFilename))
{
Files.copy(inputStream, sourceFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
catch (IOException e)
{
throw new TransformException(INSUFFICIENT_STORAGE.value(), getMessagePrefix(isLiveProbe)+
"Failed to store the source file", e);
throw new TransformException(INSUFFICIENT_STORAGE.value(),
getMessagePrefix(isLiveProbe) + "Failed to store the source file", e);
}
long length = sourceFile.length();
LogEntry.setSource(sourceFilename, length);
@@ -279,7 +281,7 @@ public abstract class ProbeTestTransform
private File getTargetFile(HttpServletRequest request)
{
File targetFile = TempFileProvider.createTempFile("target_", "_"+targetFilename);
File targetFile = TempFileProvider.createTempFile("target_", "_" + targetFilename);
request.setAttribute(TARGET_FILE, targetFile);
LogEntry.setTarget(targetFilename);
return targetFile;
@@ -302,13 +304,15 @@ public abstract class ProbeTestTransform
String message = getMessagePrefix(isLiveProbe) + "Success - Transform " + time + "ms";
if (++transCount > 1)
{
normalTime = (normalTime * (transCount-2) + time) / (transCount-1);
normalTime = (normalTime * (transCount - 2) + time) / (transCount - 1);
maxTime = (normalTime * (livenessPercent + 100)) / 100;
if ((!isLiveProbe && !readySent.getAndSet(true)) || transCount > AVERAGE_OVER_TRANSFORMS)
if ((!isLiveProbe && !readySent.getAndSet(
true)) || transCount > AVERAGE_OVER_TRANSFORMS)
{
nextTransformTime = System.currentTimeMillis() + livenessTransformPeriod;
logger.trace("{} - {}ms+{}%={}ms", message, normalTime, livenessPercent, maxTime);
logger.trace("{} - {}ms+{}%={}ms", message, normalTime, livenessPercent,
maxTime);
}
}
else if (!isLiveProbe && !readySent.getAndSet(true))
@@ -362,7 +366,6 @@ public abstract class ProbeTestTransform
public long getNormalTime()
{
return normalTime;
}
}

View File

@@ -27,6 +27,7 @@
package org.alfresco.transformer.util;
/**
*
*/
public class Util
{
@@ -49,6 +50,6 @@ public class Util
*/
public static Boolean stringToBoolean(String param)
{
return param == null? null : Boolean.parseBoolean(param);
return param == null ? null : Boolean.parseBoolean(param);
}
}

View File

@@ -26,16 +26,20 @@
*/
package org.alfresco.transformer;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
/**
* Super class for testing controllers with a server. Includes tests for the AbstractTransformerController itself.
* Note: Currently uses json rather than HTML as json is returned by this spring boot test harness.
@@ -64,7 +68,8 @@ public abstract class AbstractHttpRequestTest
@Test
public void logPageExists()
{
String result = restTemplate.getForObject("http://localhost:" + port + "/log", String.class);
String result = restTemplate.getForObject("http://localhost:" + port + "/log",
String.class);
String title = getTransformerName() + ' ' + "Log";
assertTrue("\"" + title + "\" should be part of the page title", result.contains(title));
@@ -73,10 +78,12 @@ public abstract class AbstractHttpRequestTest
@Test
public void errorPageExists()
{
String result = restTemplate.getForObject("http://localhost:" + port + "/error", String.class);
String result = restTemplate.getForObject("http://localhost:" + port + "/error",
String.class);
String title = getTransformerName() + ' ' + "Error Page";
assertTrue("\"" + title + "\" should be part of the page title", result.contains("Error Page"));
assertTrue("\"" + title + "\" should be part of the page title",
result.contains("Error Page"));
}
@Test
@@ -104,12 +111,15 @@ public abstract class AbstractHttpRequestTest
LinkedMultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
if (addFile)
{
parameters.add("file", new org.springframework.core.io.ClassPathResource("quick."+getSourceExtension()));
parameters.add("file",
new org.springframework.core.io.ClassPathResource("quick." + getSourceExtension()));
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(parameters, headers);
ResponseEntity<String> response = restTemplate.exchange("/transform", HttpMethod.POST, entity, String.class, "");
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(parameters,
headers);
ResponseEntity<String> response = restTemplate.exchange("/transform", HttpMethod.POST,
entity, String.class, "");
assertEquals(errorMessage, getErrorMessage(response.getBody()));
}
@@ -125,7 +135,7 @@ public abstract class AbstractHttpRequestTest
int j = content.indexOf("\",\"path\":", i);
if (j != -1)
{
message = content.substring(i+11, j);
message = content.substring(i + 11, j);
}
}
return message;

View File

@@ -54,11 +54,12 @@ public abstract class AbstractQueueTransformServiceIT
@Autowired
private JmsTemplate jmsTemplate;
private final ActiveMQQueue testingQueue = new ActiveMQQueue("org.alfresco.transform.engine.IT");
private final ActiveMQQueue testingQueue = new ActiveMQQueue(
"org.alfresco.transform.engine.IT");
@Test
public void queueTransformServiceIT() {
public void queueTransformServiceIT()
{
TransformRequest request = buildRequest();
jmsTemplate.convertAndSend(engineRequestQueue, request, m -> {

View File

@@ -30,9 +30,11 @@ import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -107,7 +109,8 @@ public abstract class AbstractTransformerControllerTest
protected abstract AbstractTransformerController getController();
protected abstract void updateTransformRequestWithSpecificOptions(TransformRequest transformRequest);
protected abstract void updateTransformRequestWithSpecificOptions(
TransformRequest transformRequest);
/**
* This method ends up being the core of the mock.
@@ -151,14 +154,17 @@ public abstract class AbstractTransformerControllerTest
URL testFileUrl = classLoader.getResource(testFilename);
if (required && testFileUrl == null)
{
throw new IOException("The test file " + testFilename + " does not exist in the resources directory");
throw new IOException("The test file " + testFilename +
" does not exist in the resources directory");
}
return testFileUrl == null ? null : new File(testFileUrl.getFile());
}
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params)
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile,
String... params)
{
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart("/transform").file(sourceFile);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart("/transform").file(
sourceFile);
if (params.length % 2 != 0)
{
@@ -175,20 +181,24 @@ public abstract class AbstractTransformerControllerTest
@Test
public void simpleTransformTest() throws Exception
{
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Test
public void testDelayTest() throws Exception
{
long start = System.currentTimeMillis();
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension, "testDelay", "400"))
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension,
"testDelay", "400"))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
long ms = System.currentTimeMillis() - start;
System.out.println("Transform incluing test delay was " + ms);
assertTrue("Delay sending the result back was too small " + ms, ms >= 400);
@@ -206,24 +216,30 @@ public abstract class AbstractTransformerControllerTest
// Looks dangerous but is okay as we only use the final filename
public void dotDotSourceFilenameTest() throws Exception
{
sourceFile = new MockMultipartFile("file", "../quick." + sourceExtension, sourceMimetype, expectedSourceFileBytes);
sourceFile = new MockMultipartFile("file", "../quick." + sourceExtension, sourceMimetype,
expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Test
// Is okay, as the target filename is built up from the whole source filename and the targetExtenstion
public void noExtensionSourceFilenameTest() throws Exception
{
sourceFile = new MockMultipartFile("file", "../quick", sourceMimetype, expectedSourceFileBytes);
sourceFile = new MockMultipartFile("file", "../quick", sourceMimetype,
expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes))
.andExpect(header().string("Content-Disposition", "attachment; filename*= UTF-8''quick." + targetExtension));
.andExpect(header().string("Content-Disposition",
"attachment; filename*= UTF-8''quick." + targetExtension));
}
@Test
@@ -232,7 +248,8 @@ public abstract class AbstractTransformerControllerTest
{
sourceFile = new MockMultipartFile("file", "abc/", sourceMimetype, expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("The source filename was not supplied")));
}
@@ -242,7 +259,8 @@ public abstract class AbstractTransformerControllerTest
{
sourceFile = new MockMultipartFile("file", "", sourceMimetype, expectedSourceFileBytes);
mockMvc.perform(mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
mockMvc.perform(
mockMvcRequest("/transform", sourceFile, "targetExtension", targetExtension))
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("The source filename was not supplied")));
}
@@ -252,7 +270,8 @@ public abstract class AbstractTransformerControllerTest
{
mockMvc.perform(mockMvcRequest("/transform", sourceFile))
.andExpect(status().is(BAD_REQUEST.value()))
.andExpect(status().reason(containsString("Request parameter 'targetExtension' is missing")));
.andExpect(status().reason(
containsString("Request parameter 'targetExtension' is missing")));
}
@Test
@@ -296,12 +315,13 @@ public abstract class AbstractTransformerControllerTest
.perform(MockMvcRequestBuilders
.post("/transform")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.content(tr))
.andExpect(status().is(BAD_REQUEST.value()))
.andReturn().getResponse().getContentAsString();
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, TransformReply.class);
TransformReply transformReply = objectMapper.readValue(transformationReplyAsString,
TransformReply.class);
// Assert the reply
assertEquals(BAD_REQUEST.value(), transformReply.getStatus());
@@ -317,9 +337,10 @@ public abstract class AbstractTransformerControllerTest
ReflectionTestUtils
.setField(AbstractTransformerController.class, "ENGINE_CONFIG", "engine_config.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value()))
.andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
@@ -335,9 +356,10 @@ public abstract class AbstractTransformerControllerTest
ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
"engine_config_with_duplicates.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value()))
.andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
@@ -349,7 +371,6 @@ public abstract class AbstractTransformerControllerTest
transformConfig.getTransformers().get(0).getSupportedSourceAndTargetList().size());
assertEquals(1,
transformConfig.getTransformers().get(0).getTransformOptions().size());
}
@Test
@@ -364,7 +385,7 @@ public abstract class AbstractTransformerControllerTest
String response = mockMvc.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
header().string(CONTENT_TYPE, APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
@@ -384,9 +405,10 @@ public abstract class AbstractTransformerControllerTest
ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
"engine_config_no_transform_options.json");
String response = mockMvc.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value())).andExpect(
header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE))
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
.andExpect(status().is(OK.value()))
.andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_UTF8_VALUE))
.andReturn().getResponse().getContentAsString();
TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class);
@@ -405,7 +427,8 @@ public abstract class AbstractTransformerControllerTest
new TransformOptionValue(false, "page"),
new TransformOptionValue(false, "width"),
new TransformOptionGroup(false, transformOptionGroup));
Map<String, Set<TransformOption>> transformOptionsMap = ImmutableMap.of("engineXOptions", transformOptions);
Map<String, Set<TransformOption>> transformOptionsMap = ImmutableMap.of("engineXOptions",
transformOptions);
Transformer transformer = buildTransformer("application/pdf", "image/png", "engineXOptions",
"engineX");

View File

@@ -27,12 +27,16 @@
package org.alfresco.transformer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transformer.messaging.TransformMessageConverter;
@@ -48,10 +52,6 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jms.support.converter.MessageConversionException;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
public class QueueTransformServiceTest
{
@Mock
@@ -98,10 +98,13 @@ public class QueueTransformServiceTest
ActiveMQQueue destination = new ActiveMQQueue();
msg.setJMSReplyTo(destination);
TransformReply reply = TransformReply.builder()
.withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()).withErrorDetails(
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
.withErrorDetails(
"JMS exception during T-Request deserialization of message with correlationID "
+ msg.getCorrelationId() + ": null").build();
+ msg.getCorrelationId() + ": null")
.build();
doReturn(null).when(transformMessageConverter).fromMessage(msg);
@@ -122,10 +125,13 @@ public class QueueTransformServiceTest
ActiveMQQueue destination = new ActiveMQQueue();
msg.setJMSReplyTo(destination);
TransformReply reply = TransformReply.builder().withStatus(HttpStatus.BAD_REQUEST.value())
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.BAD_REQUEST.value())
.withErrorDetails(
"Message conversion exception during T-Request deserialization of message with correlationID"
+ msg.getCorrelationId() + ": null").build();
+ msg.getCorrelationId() + ": null")
.build();
doThrow(MessageConversionException.class).when(transformMessageConverter).fromMessage(msg);
@@ -146,10 +152,12 @@ public class QueueTransformServiceTest
ActiveMQQueue destination = new ActiveMQQueue();
msg.setJMSReplyTo(destination);
TransformReply reply = TransformReply.builder()
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()).withErrorDetails(
"JMSException during T-Request deserialization of message with correlationID " + msg
.getCorrelationId() + ": null").build();
.getCorrelationId() + ": null")
.build();
doThrow(JMSException.class).when(transformMessageConverter).fromMessage(msg);
@@ -169,7 +177,9 @@ public class QueueTransformServiceTest
msg.setJMSReplyTo(destination);
TransformRequest request = new TransformRequest();
TransformReply reply = TransformReply.builder().withStatus(HttpStatus.CREATED.value())
TransformReply reply = TransformReply
.builder()
.withStatus(HttpStatus.CREATED.value())
.build();
doReturn(request).when(transformMessageConverter).fromMessage(msg);
@@ -208,7 +218,8 @@ public class QueueTransformServiceTest
doReturn(destination).when(msg).getJMSReplyTo();
TransformRequest request = new TransformRequest();
TransformReply reply = TransformReply.builder().withStatus(HttpStatus.CREATED.value())
TransformReply reply = TransformReply.builder()
.withStatus(HttpStatus.CREATED.value())
.build();
doReturn(request).when(transformMessageConverter).fromMessage(msg);

View File

@@ -14,7 +14,6 @@
<version>2.1.0-EA5-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<image.tag>latest</image.tag>
@@ -47,7 +46,6 @@
</scm>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.alfresco</groupId>
@@ -69,7 +67,6 @@
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.alfresco</groupId>
<artifactId>alfresco-data-model</artifactId>