mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-07-31 17:38:33 +00:00
REPO-3422 Startup check for LibreOffice
Also included the transformer name on the log and error pages.
This commit is contained in:
@@ -66,6 +66,12 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
|
||||
setCheckCommand(createCheckCommand());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTransformerName()
|
||||
{
|
||||
return "Alfresco PDF Renderer";
|
||||
}
|
||||
|
||||
private static RuntimeExec createTransformCommand()
|
||||
{
|
||||
RuntimeExec runtimeExec = new RuntimeExec();
|
||||
|
@@ -1,10 +1,6 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<div th:if="${message}">
|
||||
<h2 th:text="${message}"/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h2>Alfresco PDF Renderer Test Transformation</h2>
|
||||
<form method="POST" enctype="multipart/form-data" action="/transform">
|
||||
|
@@ -71,6 +71,12 @@ public class ImageMagickController extends AbstractTransformerController
|
||||
setCheckCommand(createCheckCommand());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTransformerName()
|
||||
{
|
||||
return "ImageMagick";
|
||||
}
|
||||
|
||||
private static RuntimeExec createTransformCommand()
|
||||
{
|
||||
RuntimeExec runtimeExec = new RuntimeExec();
|
||||
|
@@ -1,10 +1,6 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<div th:if="${message}">
|
||||
<h2 th:text="${message}"/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h2>ImageMagick Test Transformation</h2>
|
||||
<form method="POST" enctype="multipart/form-data" action="/transform">
|
||||
|
@@ -96,6 +96,24 @@ public class LibreOfficeController extends AbstractTransformerController
|
||||
this.jodconverter = jodconverter;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTransformerName()
|
||||
{
|
||||
return "LibreOffice";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String version()
|
||||
{
|
||||
// This method is simply used to check the availability in the case of LibreOffice.
|
||||
if (!jodconverter.isAvailable())
|
||||
{
|
||||
throw new TransformException(500, "LibreOffice is not yet available");
|
||||
}
|
||||
|
||||
return "LibreOffice available";
|
||||
}
|
||||
|
||||
@PostMapping("/transform")
|
||||
public ResponseEntity<Resource> transform(HttpServletRequest request,
|
||||
@RequestParam("file") MultipartFile sourceMultipartFile,
|
||||
|
@@ -1,10 +1,6 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<div th:if="${message}">
|
||||
<h2 th:text="${message}"/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h2>LibreOffice Test Transformation</h2>
|
||||
<form method="POST" enctype="multipart/form-data" action="/transform">
|
||||
|
@@ -108,9 +108,11 @@ public abstract class AbstractTransformerController
|
||||
logger.info("");
|
||||
}
|
||||
|
||||
protected abstract String getTransformerName();
|
||||
|
||||
@RequestMapping("/version")
|
||||
@ResponseBody
|
||||
String version()
|
||||
protected String version()
|
||||
{
|
||||
String version = "Version not checked";
|
||||
if (checkCommand != null)
|
||||
@@ -140,6 +142,7 @@ public abstract class AbstractTransformerController
|
||||
@GetMapping("/log")
|
||||
public String log(Model model)
|
||||
{
|
||||
model.addAttribute("title", getTransformerName() + " Log Entries");
|
||||
Collection<LogEntry> log = LogEntry.getLog();
|
||||
if (!log.isEmpty())
|
||||
{
|
||||
@@ -148,9 +151,16 @@ public abstract class AbstractTransformerController
|
||||
return "log"; // the name of the template
|
||||
}
|
||||
|
||||
@GetMapping("/error")
|
||||
public String error()
|
||||
{
|
||||
return "error"; // the name of the template
|
||||
}
|
||||
|
||||
@ExceptionHandler(TypeMismatchException.class)
|
||||
public void handleParamsTypeMismatch(HttpServletResponse response, MissingServletRequestParameterException e) throws IOException
|
||||
{
|
||||
String transformerName = getTransformerName();
|
||||
String name = e.getParameterName();
|
||||
String message = "Request parameter " + name + " is of the wrong type";
|
||||
int statusCode = 400;
|
||||
@@ -162,12 +172,13 @@ public abstract class AbstractTransformerController
|
||||
|
||||
LogEntry.setStatusCodeAndMessage(statusCode, message);
|
||||
|
||||
response.sendError(statusCode, message);
|
||||
response.sendError(statusCode, transformerName+" - "+message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MissingServletRequestParameterException.class)
|
||||
public void handleMissingParams(HttpServletResponse response, MissingServletRequestParameterException e) throws IOException
|
||||
{
|
||||
String transformerName = getTransformerName();
|
||||
String name = e.getParameterName();
|
||||
String message = "Request parameter " + name + " is missing";
|
||||
int statusCode = 400;
|
||||
@@ -179,12 +190,13 @@ public abstract class AbstractTransformerController
|
||||
|
||||
LogEntry.setStatusCodeAndMessage(statusCode, message);
|
||||
|
||||
response.sendError(statusCode, message);
|
||||
response.sendError(statusCode, transformerName+" - "+message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(TransformException.class)
|
||||
public void transformExceptionWithMessage(HttpServletResponse response, TransformException e) throws IOException
|
||||
{
|
||||
String transformerName = getTransformerName();
|
||||
String message = e.getMessage();
|
||||
int statusCode = e.getStatusCode();
|
||||
|
||||
@@ -195,9 +207,32 @@ public abstract class AbstractTransformerController
|
||||
|
||||
LogEntry.setStatusCodeAndMessage(statusCode, message);
|
||||
|
||||
response.sendError(statusCode, message);
|
||||
// Forced to include the transformer name in the message (see commented out version of this method)
|
||||
response.sendError(statusCode, transformerName+" - "+message);
|
||||
}
|
||||
|
||||
// Results in HTML rather than json but there is an error in the log about "template might not exist or might
|
||||
// not be accessible by any of the configured Template Resolvers" for the transformer.html (which is correct
|
||||
// because that failed). Looks like Spring only supports returning json or XML when returning an Object or even
|
||||
// a ResponseEntity without this logged exception, which is a shame as it would have been nicer to have just
|
||||
// added the transformerName to the Object.
|
||||
// @ExceptionHandler(TransformException.class)
|
||||
// public final Map<String, Object> transformExceptionWithMessage(HttpServletResponse response, TransformException e, WebRequest request)
|
||||
// {
|
||||
// String transformerName = getTransformerName();
|
||||
// String message = e.getMessage();
|
||||
// int statusCode = e.getStatusCode();
|
||||
//
|
||||
// LogEntry.setStatusCodeAndMessage(statusCode, message);
|
||||
//
|
||||
// Map<String, Object> errorAttributes = new HashMap<>();
|
||||
// errorAttributes.put("title", transformerName);
|
||||
// errorAttributes.put("message", message);
|
||||
// errorAttributes.put("status", Integer.toString(statusCode));
|
||||
// errorAttributes.put("error", HttpStatus.valueOf(statusCode).getReasonPhrase());
|
||||
// return errorAttributes;
|
||||
// }
|
||||
|
||||
protected String createTargetFileName(MultipartFile sourceMultipartFile, String targetExtension)
|
||||
{
|
||||
String targetFilename = null;
|
||||
|
@@ -25,7 +25,10 @@
|
||||
*/
|
||||
package org.alfresco.transformer.base;
|
||||
|
||||
import java.util.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Deque;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@@ -40,6 +43,7 @@ public class LogEntry
|
||||
private static final AtomicInteger count = new AtomicInteger(0);
|
||||
private static final Deque<LogEntry> log = new ConcurrentLinkedDeque<>();
|
||||
private static final int MAX_LOG_SIZE = 10;
|
||||
private static final SimpleDateFormat HH_MM_SS = new SimpleDateFormat("HH:mm:ss");
|
||||
|
||||
private static ThreadLocal<LogEntry> currentLogEntry = new ThreadLocal<LogEntry>()
|
||||
{
|
||||
@@ -71,6 +75,32 @@ public class LogEntry
|
||||
private String options;
|
||||
private String message;
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getId());
|
||||
sb.append(' ');
|
||||
sb.append(HH_MM_SS.format(getDate()));
|
||||
sb.append(' ');
|
||||
sb.append(getStatusCode());
|
||||
sb.append(' ');
|
||||
sb.append(getDuration());
|
||||
sb.append(' ');
|
||||
sb.append(getSource());
|
||||
sb.append(' ');
|
||||
sb.append(getSourceSize());
|
||||
sb.append(' ');
|
||||
sb.append(getTarget());
|
||||
sb.append(' ');
|
||||
sb.append(getTargetSize());
|
||||
sb.append(' ');
|
||||
sb.append(getOptions());
|
||||
sb.append(' ');
|
||||
sb.append(getMessage());
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static Collection<LogEntry> getLog()
|
||||
{
|
||||
return log;
|
||||
@@ -127,6 +157,11 @@ public class LogEntry
|
||||
LogEntry logEntry = currentLogEntry.get();
|
||||
logEntry.durationStreamOut = System.currentTimeMillis() - logEntry.start - logEntry.durationStreamIn - logEntry.durationTransform;
|
||||
currentLogEntry.remove();
|
||||
|
||||
if (AbstractTransformerController.logger != null && AbstractTransformerController.logger.isDebugEnabled())
|
||||
{
|
||||
AbstractTransformerController.logger.debug(logEntry.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public int getId()
|
||||
|
@@ -0,0 +1,17 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<div>
|
||||
<h2 th:text="${#strings.substring(message,0,#strings.indexOf(message,' - '))} + ' Error Page'"></h2>
|
||||
<h3 th:text="${#strings.substring(message,#strings.indexOf(message,' - ')+3)}"></h3>
|
||||
<p th:text="${status} + ' - ' + ${error}"></p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<br/>
|
||||
<a href="/">Test Transformation</a>
|
||||
<a href="/log">Log entries</a>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
@@ -1,41 +1,38 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<div th:if="${message}">
|
||||
<h2 th:text="${message}"/>
|
||||
<div>
|
||||
<h2 th:text="${title}"></h2>
|
||||
<div th:if="${log}">
|
||||
<table>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<th>Time</th>
|
||||
<th>Status Code</th>
|
||||
<th>Duration (ms)</th>
|
||||
<th>Source</th>
|
||||
<th></th>
|
||||
<th>Target</th>
|
||||
<th></th>
|
||||
<th>Options</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
<tr th:each="entry : ${log}">
|
||||
<td th:text="${entry.id}"></td>
|
||||
<td th:text="${#dates.format(entry.date, 'HH:mm:ss')}"></td>
|
||||
<td th:text="${entry.statusCode}"></td>
|
||||
<td th:text="${entry.duration}"></td>
|
||||
<td th:text="${entry.source}"></td>
|
||||
<td th:text="${entry.sourceSize}"></td>
|
||||
<td th:text="${entry.target}"></td>
|
||||
<td th:text="${entry.targetSize}"></td>
|
||||
<td th:text="${entry.options}"></td>
|
||||
<td th:text="${entry.message}"></td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Log entries</h2>
|
||||
<div th:if="${log}">
|
||||
<table>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<th>Time</th>
|
||||
<th>Status Code</th>
|
||||
<th>Duration (ms)</th>
|
||||
<th>Source</th>
|
||||
<th></th>
|
||||
<th>Target</th>
|
||||
<th></th>
|
||||
<th>Options</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
<tr th:each="entry : ${log}">
|
||||
<td th:text="${entry.id}"></td>
|
||||
<td th:text="${#dates.format(entry.date, 'HH:mm:ss')}"></td>
|
||||
<td th:text="${entry.statusCode}"></td>
|
||||
<td th:text="${entry.duration}"></td>
|
||||
<td th:text="${entry.source}"></td>
|
||||
<td th:text="${entry.sourceSize}"></td>
|
||||
<td th:text="${entry.target}"></td>
|
||||
<td th:text="${entry.targetSize}"></td>
|
||||
<td th:text="${entry.options}"></td>
|
||||
<td th:text="${entry.message}"></td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
|
||||
<div>
|
||||
<br/>
|
||||
<a href="/">Test Transformation</a>
|
||||
|
Reference in New Issue
Block a user