mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-08-07 17:48:35 +00:00
ATS-531 : Reformat code
This commit is contained in:
@@ -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,18 +34,21 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
SpringApplication.run(Application.class, args);
|
||||
|
@@ -59,20 +59,20 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
*
|
||||
* Status Codes:
|
||||
*
|
||||
* 200 Success
|
||||
* 400 Bad Request: Request parameter <name> is missing (missing mandatory parameter)
|
||||
* 400 Bad Request: Request parameter <name> is of the wrong type
|
||||
* 400 Bad Request: Transformer exit code was not 0 (possible problem with the source file)
|
||||
* 400 Bad Request: The source filename was not supplied
|
||||
* 500 Internal Server Error: (no message with low level IO problems)
|
||||
* 500 Internal Server Error: The target filename was not supplied (should not happen as targetExtension is checked)
|
||||
* 500 Internal Server Error: Transformer version check exit code was not 0
|
||||
* 500 Internal Server Error: Transformer version check failed to create any output
|
||||
* 500 Internal Server Error: Could not read the target file
|
||||
* 500 Internal Server Error: The target filename was malformed (should not happen because of other checks)
|
||||
* 500 Internal Server Error: Transformer failed to create an output file (the exit code was 0, so there should be some content)
|
||||
* 500 Internal Server Error: Filename encoding error
|
||||
* 507 Insufficient Storage: Failed to store the source file
|
||||
* 200 Success
|
||||
* 400 Bad Request: Request parameter <name> is missing (missing mandatory parameter)
|
||||
* 400 Bad Request: Request parameter <name> is of the wrong type
|
||||
* 400 Bad Request: Transformer exit code was not 0 (possible problem with the source file)
|
||||
* 400 Bad Request: The source filename was not supplied
|
||||
* 500 Internal Server Error: (no message with low level IO problems)
|
||||
* 500 Internal Server Error: The target filename was not supplied (should not happen as targetExtension is checked)
|
||||
* 500 Internal Server Error: Transformer version check exit code was not 0
|
||||
* 500 Internal Server Error: Transformer version check failed to create any output
|
||||
* 500 Internal Server Error: Could not read the target file
|
||||
* 500 Internal Server Error: The target filename was malformed (should not happen because of other checks)
|
||||
* 500 Internal Server Error: Transformer failed to create an output file (the exit code was 0, so there should be some content)
|
||||
* 500 Internal Server Error: Filename encoding error
|
||||
* 507 Insufficient Storage: Failed to store the source file
|
||||
*/
|
||||
@Controller
|
||||
public class LibreOfficeController extends AbstractTransformerController
|
||||
@@ -81,14 +81,17 @@ public class LibreOfficeController extends AbstractTransformerController
|
||||
|
||||
@Autowired
|
||||
private LibreOfficeJavaExecutor javaExecutor;
|
||||
|
||||
|
||||
@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)
|
||||
@@ -121,12 +124,13 @@ public class LibreOfficeController extends AbstractTransformerController
|
||||
//todo: the "timeout" request parameter is ignored; the timeout is preset at JodConverter creation
|
||||
@PostMapping(value = "/transform", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public ResponseEntity<Resource> transform(HttpServletRequest request,
|
||||
@RequestParam("file") MultipartFile sourceMultipartFile,
|
||||
@RequestParam("targetExtension") String targetExtension,
|
||||
@RequestParam(value = "timeout", required = false) Long timeout,
|
||||
@RequestParam(value = "testDelay", required = false) Long testDelay)
|
||||
@RequestParam("file") MultipartFile sourceMultipartFile,
|
||||
@RequestParam("targetExtension") String targetExtension,
|
||||
@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);
|
||||
|
@@ -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();
|
||||
|
@@ -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;
|
||||
}
|
||||
@@ -176,7 +177,7 @@ public class JodConverterSharedInstance implements JodConverter
|
||||
|
||||
void setConnectTimeout(String connectTimeout)
|
||||
{
|
||||
this.connectTimeout = parseStringForLong(connectTimeout.trim());
|
||||
this.connectTimeout = parseStringForLong(connectTimeout.trim());
|
||||
}
|
||||
|
||||
void setEnabled(final String enabledStr)
|
||||
@@ -254,8 +255,8 @@ public class JodConverterSharedInstance implements JodConverter
|
||||
private int[] getPortNumbers()
|
||||
{
|
||||
return (enabled == null || !enabled) && deprecatedOooEnabled != null && deprecatedOooEnabled
|
||||
? deprecatedOooPortNumbers
|
||||
: portNumbers;
|
||||
? deprecatedOooPortNumbers
|
||||
: portNumbers;
|
||||
}
|
||||
|
||||
private Long parseStringForLong(String string)
|
||||
@@ -287,14 +288,15 @@ public class JodConverterSharedInstance implements JodConverter
|
||||
@PostConstruct
|
||||
public void afterPropertiesSet()
|
||||
{
|
||||
// isAvailable defaults to false afterPropertiesSet. It only becomes true on successful completion of this method.
|
||||
this.isAvailable = false;
|
||||
// isAvailable defaults to false afterPropertiesSet. It only becomes true on successful completion of this method.
|
||||
this.isAvailable = false;
|
||||
|
||||
int[] portNumbers = getPortNumbers();
|
||||
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)
|
||||
@@ -404,78 +406,77 @@ public class JodConverterSharedInstance implements JodConverter
|
||||
|
||||
private void logAllSofficeFilesUnderOfficeHome()
|
||||
{
|
||||
if (!logger.isDebugEnabled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!logger.isDebugEnabled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
String officeHome = getOfficeHome();
|
||||
File requestedOfficeHome = new File(officeHome);
|
||||
String officeHome = getOfficeHome();
|
||||
File requestedOfficeHome = new File(officeHome);
|
||||
|
||||
logger.debug("Some information on soffice* files and their permissions");
|
||||
logger.debug("Some information on soffice* files and their permissions");
|
||||
|
||||
logFileInfo(requestedOfficeHome);
|
||||
logFileInfo(requestedOfficeHome);
|
||||
|
||||
for (File f : findSofficePrograms(requestedOfficeHome, new ArrayList<>(), 2))
|
||||
{
|
||||
logFileInfo(f);
|
||||
}
|
||||
}
|
||||
|
||||
private List<File> findSofficePrograms(File searchRoot, List<File> results, int maxRecursionDepth)
|
||||
{
|
||||
return this.findSofficePrograms(searchRoot, results, 0, maxRecursionDepth);
|
||||
for (File f : findSofficePrograms(requestedOfficeHome, new ArrayList<>(), 2))
|
||||
{
|
||||
logFileInfo(f);
|
||||
}
|
||||
}
|
||||
|
||||
private List<File> findSofficePrograms(File searchRoot, List<File> results,
|
||||
int currentRecursionDepth, int maxRecursionDepth)
|
||||
int maxRecursionDepth)
|
||||
{
|
||||
if (currentRecursionDepth >= maxRecursionDepth)
|
||||
{
|
||||
return results;
|
||||
}
|
||||
return this.findSofficePrograms(searchRoot, results, 0, maxRecursionDepth);
|
||||
}
|
||||
|
||||
File[] matchingFiles = searchRoot.listFiles((dir, name) -> name.startsWith("soffice"));
|
||||
private List<File> findSofficePrograms(File searchRoot, List<File> results,
|
||||
int currentRecursionDepth, int maxRecursionDepth)
|
||||
{
|
||||
if (currentRecursionDepth >= maxRecursionDepth)
|
||||
{
|
||||
return results;
|
||||
}
|
||||
|
||||
File[] matchingFiles = searchRoot.listFiles((dir, name) -> name.startsWith("soffice"));
|
||||
results.addAll(asList(matchingFiles));
|
||||
|
||||
for (File dir : requireNonNull(searchRoot.listFiles(File::isDirectory)))
|
||||
{
|
||||
findSofficePrograms(dir, results, currentRecursionDepth + 1, maxRecursionDepth);
|
||||
}
|
||||
{
|
||||
findSofficePrograms(dir, results, currentRecursionDepth + 1, maxRecursionDepth);
|
||||
}
|
||||
|
||||
return results;
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
if (!logger.isDebugEnabled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!logger.isDebugEnabled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder msg = new StringBuilder();
|
||||
msg.append(f).append(" ");
|
||||
if (f.exists())
|
||||
{
|
||||
if (f.canRead())
|
||||
{
|
||||
msg.append("(")
|
||||
.append(f.isDirectory() ? "d" : "-")
|
||||
.append(f.canRead() ? "r" : "-")
|
||||
.append(f.canWrite() ? "w" : "-")
|
||||
.append(f.canExecute() ? "x" : "-")
|
||||
.append(")");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
msg.append("does not exist");
|
||||
}
|
||||
logger.debug(msg.toString());
|
||||
StringBuilder msg = new StringBuilder();
|
||||
msg.append(f).append(" ");
|
||||
if (f.exists() && f.canRead())
|
||||
{
|
||||
msg.append("(")
|
||||
.append(f.isDirectory() ? "d" : "-")
|
||||
.append(f.canRead() ? "r" : "-")
|
||||
.append(f.canWrite() ? "w" : "-")
|
||||
.append(f.canExecute() ? "x" : "-")
|
||||
.append(")");
|
||||
}
|
||||
else
|
||||
{
|
||||
msg.append("does not exist");
|
||||
}
|
||||
logger.debug(msg.toString());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -485,19 +486,19 @@ public class JodConverterSharedInstance implements JodConverter
|
||||
@PreDestroy
|
||||
public void destroy()
|
||||
{
|
||||
this.isAvailable = false;
|
||||
if (officeManager != null)
|
||||
{
|
||||
// If there is an OfficeException when stopping the officeManager below, then there is
|
||||
// little that can be done other than logging the exception and carrying on. The JodConverter-based
|
||||
// libraries will not be used in any case, as isAvailable is false.
|
||||
//
|
||||
// Any exception thrown out of this method will be logged and swallowed by Spring
|
||||
// (see javadoc for method declaration). Therefore there is no handling here for
|
||||
// exceptions from jodConverter.
|
||||
officeManager.stop();
|
||||
}
|
||||
}
|
||||
this.isAvailable = false;
|
||||
if (officeManager != null)
|
||||
{
|
||||
// If there is an OfficeException when stopping the officeManager below, then there is
|
||||
// little that can be done other than logging the exception and carrying on. The JodConverter-based
|
||||
// libraries will not be used in any case, as isAvailable is false.
|
||||
//
|
||||
// Any exception thrown out of this method will be logged and swallowed by Spring
|
||||
// (see javadoc for method declaration). Therefore there is no handling here for
|
||||
// exceptions from jodConverter.
|
||||
officeManager.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.content.JodConverterWorker#getOfficeManager()
|
||||
|
@@ -35,20 +35,20 @@ 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;
|
||||
|
||||
/**
|
||||
* JavaExecutor implementation for running LibreOffice transformations. It loads the
|
||||
* JavaExecutor implementation for running LibreOffice transformations. It loads the
|
||||
* transformation logic in the same JVM (check the {@link JodConverter} implementation).
|
||||
*/
|
||||
@Component
|
||||
|
@@ -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")
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", "xxx"))
|
||||
.andExpect(status().is(400))
|
||||
.andExpect(status().reason(containsString("LibreOffice - LibreOffice server conversion failed:")));
|
||||
mockMvc
|
||||
.perform(MockMvcRequestBuilders
|
||||
.multipart("/transform")
|
||||
.file(sourceFile)
|
||||
.param("targetExtension", "xxx"))
|
||||
.andExpect(status().is(400))
|
||||
.andExpect(status().reason(
|
||||
containsString("LibreOffice - LibreOffice server conversion failed:")));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user