ATS-545: Code formatting and small improvements (#113)

This commit is contained in:
CezarLeahu 2019-09-12 16:46:48 +03:00 committed by GitHub
parent e71bf39905
commit c650bf292c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 278 additions and 246 deletions

View File

@ -26,23 +26,6 @@
*/ */
package org.alfresco.transformer; package org.alfresco.transformer;
import org.alfresco.transformer.executors.TikaJavaExecutor;
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.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;
import static org.alfresco.transformer.executors.Tika.INCLUDE_CONTENTS; import static org.alfresco.transformer.executors.Tika.INCLUDE_CONTENTS;
import static org.alfresco.transformer.executors.Tika.NOT_EXTRACT_BOOKMARKS_TEXT; import static org.alfresco.transformer.executors.Tika.NOT_EXTRACT_BOOKMARKS_TEXT;
import static org.alfresco.transformer.executors.Tika.PDF_BOX; import static org.alfresco.transformer.executors.Tika.PDF_BOX;
@ -57,6 +40,24 @@ import static org.alfresco.transformer.util.Util.stringToBoolean;
import static org.springframework.http.HttpStatus.OK; import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE; import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
import java.io.File;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.transformer.executors.TikaJavaExecutor;
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.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
/** /**
* Controller for the Docker based Tika transformers. * Controller for the Docker based Tika transformers.
* *
@ -117,43 +118,50 @@ public class TikaController extends AbstractTransformerController
@PostMapping(value = "/transform", consumes = MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "/transform", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Resource> transform(HttpServletRequest request, public ResponseEntity<Resource> transform(HttpServletRequest request,
@RequestParam("file") MultipartFile sourceMultipartFile, @RequestParam("file") final MultipartFile sourceMultipartFile,
@RequestParam("sourceMimetype") String sourceMimetype, @RequestParam("sourceMimetype") final String sourceMimetype,
@RequestParam("targetExtension") String targetExtension, @RequestParam("targetExtension") final String targetExtension,
@RequestParam("targetMimetype") String targetMimetype, @RequestParam("targetMimetype") final String targetMimetype,
@RequestParam("targetEncoding") String targetEncoding, @RequestParam("targetEncoding") final String targetEncoding,
@RequestParam(value = "timeout", required = false) Long timeout, @RequestParam(value = "timeout", required = false) final Long timeout,
@RequestParam(value = "testDelay", required = false) Long testDelay, @RequestParam(value = "testDelay", required = false) final Long testDelay,
@RequestParam(value = "includeContents", required = false) Boolean includeContents, @RequestParam(value = "includeContents", required = false) final Boolean includeContents,
@RequestParam(value = "notExtractBookmarksText", required = false) Boolean notExtractBookmarksText) @RequestParam(value = "notExtractBookmarksText", required = false) final Boolean notExtractBookmarksText)
{ {
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), final String targetFilename = createTargetFileName(
targetExtension); sourceMultipartFile.getOriginalFilename(), targetExtension);
getProbeTestTransform().incrementTransformerCount(); getProbeTestTransform().incrementTransformerCount();
File sourceFile = createSourceFile(request, sourceMultipartFile);
File targetFile = createTargetFile(request, targetFilename); final File sourceFile = createSourceFile(request, sourceMultipartFile);
final File targetFile = createTargetFile(request, targetFilename);
// Both files are deleted by TransformInterceptor.afterCompletion // Both files are deleted by TransformInterceptor.afterCompletion
// TODO Consider streaming the request and response rather than using temporary files // TODO Consider streaming the request and response rather than using temporary files
// https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/streaming-response-body.html // https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/streaming-response-body.html
Map<String, String> transformOptions = createTransformOptions( final Map<String, String> transformOptions = createTransformOptions(
"includeContents", includeContents, "includeContents", includeContents,
"notExtractBookmarksText", notExtractBookmarksText, "notExtractBookmarksText", notExtractBookmarksText,
"targetEncoding", targetEncoding); "targetEncoding", targetEncoding);
String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype, transformOptions);
final String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype,
transformOptions);
javaExecutor.call(sourceFile, targetFile, transform, javaExecutor.call(sourceFile, targetFile, transform,
includeContents != null && includeContents ? INCLUDE_CONTENTS : null, includeContents != null && includeContents ? INCLUDE_CONTENTS : null,
notExtractBookmarksText != null && notExtractBookmarksText ? NOT_EXTRACT_BOOKMARKS_TEXT : null, notExtractBookmarksText != null && notExtractBookmarksText ? NOT_EXTRACT_BOOKMARKS_TEXT : null,
TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + targetEncoding); TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + targetEncoding);
final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile); final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile);
LogEntry.setTargetSize(targetFile.length()); LogEntry.setTargetSize(targetFile.length());
long time = LogEntry.setStatusCodeAndMessage(OK.value(), "Success"); long time = LogEntry.setStatusCodeAndMessage(OK.value(), "Success");
time += LogEntry.addDelay(testDelay); time += LogEntry.addDelay(testDelay);
getProbeTestTransform().recordTransformTime(time); getProbeTestTransform().recordTransformTime(time);
return body; return body;
} }
@ -165,11 +173,13 @@ public class TikaController extends AbstractTransformerController
logger.debug("Processing request with: sourceFile '{}', targetFile '{}', transformOptions" + logger.debug("Processing request with: sourceFile '{}', targetFile '{}', transformOptions" +
" '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout); " '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout);
final Boolean includeContents = stringToBoolean("includeContents"); final Boolean includeContents = stringToBoolean(transformOptions.get("includeContents"));
final Boolean notExtractBookmarksText = stringToBoolean("notExtractBookmarksText"); final Boolean notExtractBookmarksText = stringToBoolean(
transformOptions.get("notExtractBookmarksText"));
final String targetEncoding = transformOptions.get("targetEncoding"); final String targetEncoding = transformOptions.get("targetEncoding");
String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype, transformOptions); final String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype,
transformOptions);
javaExecutor.call(sourceFile, targetFile, transform, javaExecutor.call(sourceFile, targetFile, transform,
includeContents != null && includeContents ? INCLUDE_CONTENTS : null, includeContents != null && includeContents ? INCLUDE_CONTENTS : null,
notExtractBookmarksText != null && notExtractBookmarksText ? NOT_EXTRACT_BOOKMARKS_TEXT : null, notExtractBookmarksText != null && notExtractBookmarksText ? NOT_EXTRACT_BOOKMARKS_TEXT : null,

View File

@ -26,7 +26,34 @@
*/ */
package org.alfresco.transformer.executors; package org.alfresco.transformer.executors;
import com.google.common.collect.ImmutableList; import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_HTML;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_IMAGE_JPEG;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_IMAGE_PNG;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_IMAGE_TIFF;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_TEXT_CSV;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_TEXT_PLAIN;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_XHTML;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_XML;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.util.List;
import java.util.regex.Pattern;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.apache.tika.config.TikaConfig; import org.apache.tika.config.TikaConfig;
import org.apache.tika.exception.TikaException; import org.apache.tika.exception.TikaException;
import org.apache.tika.extractor.DocumentSelector; import org.apache.tika.extractor.DocumentSelector;
@ -46,32 +73,7 @@ import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler; import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import javax.xml.transform.OutputKeys; import com.google.common.collect.ImmutableList;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.util.List;
import java.util.regex.Pattern;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_HTML;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_IMAGE_JPEG;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_IMAGE_PNG;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_IMAGE_TIFF;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_TEXT_CSV;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_TEXT_PLAIN;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_XHTML;
import static org.alfresco.transformer.util.MimetypeMap.MIMETYPE_XML;
/** /**
* Stripped down command line Tika transformers. Not actually run as a separate process, but the code fits the patten * Stripped down command line Tika transformers. Not actually run as a separate process, but the code fits the patten

View File

@ -26,16 +26,17 @@
*/ */
package org.alfresco.transformer; package org.alfresco.transformer;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
/** /**
* Tests TikaController with a server test harness. * Tests TikaController with a server test harness.
*/ */
@ -63,16 +64,13 @@ public class TikaHttpRequestTest extends AbstractHttpRequestTest
LinkedMultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
if (addFile) if (addFile)
{ {
parameters.add("file", parameters.add("file", new ClassPathResource("quick." + getSourceExtension()));
new org.springframework.core.io.ClassPathResource("quick." + getSourceExtension()));
} }
parameters.add("sourceMimetype", "application/pdf"); parameters.add("sourceMimetype", "application/pdf");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MULTIPART_FORM_DATA); headers.setContentType(MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(parameters, HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(parameters,
headers); headers);
super.sendTranformationRequest(entity, errorMessage); super.sendTranformationRequest(entity, errorMessage);
} }
} }

View File

@ -88,7 +88,8 @@ public class TikaTransformationIT
return Stream return Stream
.of( .of(
allTargets("quick.doc", "application/msword"), allTargets("quick.doc", "application/msword"),
allTargets("quick.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"), allTargets("quick.docx",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
allTargets("quick.html", "text/html"), allTargets("quick.html", "text/html"),
allTargets("quick.jar", "application/java-archive"), allTargets("quick.jar", "application/java-archive"),
allTargets("quick.java", "text/x-java-source"), allTargets("quick.java", "text/x-java-source"),
@ -122,12 +123,14 @@ public class TikaTransformationIT
), ),
allTargets("quick.pdf", "application/pdf"), allTargets("quick.pdf", "application/pdf"),
allTargets("quick.ppt", "application/vnd.ms-powerpoint"), allTargets("quick.ppt", "application/vnd.ms-powerpoint"),
allTargets("quick.pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"), allTargets("quick.pptx",
"application/vnd.openxmlformats-officedocument.presentationml.presentation"),
allTargets("quick.sxw", "application/vnd.sun.xml.writer"), allTargets("quick.sxw", "application/vnd.sun.xml.writer"),
allTargets("quick.txt", "text/plain"), allTargets("quick.txt", "text/plain"),
allTargets("quick.vsd", "application/vnd.visio"), allTargets("quick.vsd", "application/vnd.visio"),
allTargets("quick.xls", "application/vnd.ms-excel"), allTargets("quick.xls", "application/vnd.ms-excel"),
allTargets("quick.xslx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), allTargets("quick.xslx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
allTargets("quick.zip", "application/zip"), allTargets("quick.zip", "application/zip"),
allTargets("quick.tar", "application/x-tar"), allTargets("quick.tar", "application/x-tar"),
allTargets("sample.rtf", "application/rtf"), allTargets("sample.rtf", "application/rtf"),

View File

@ -89,8 +89,8 @@ public class MiscController extends AbstractTransformerController
{ {
Map<String, String> parameters = new HashMap<>(); Map<String, String> parameters = new HashMap<>();
parameters.put(SOURCE_ENCODING, "UTF-8"); parameters.put(SOURCE_ENCODING, "UTF-8");
transformer.transform("html", sourceFile, targetFile, MIMETYPE_HTML, MIMETYPE_TEXT_PLAIN, transformer.transform("html", sourceFile, targetFile, MIMETYPE_HTML,
parameters); MIMETYPE_TEXT_PLAIN, parameters);
} }
}; };
} }
@ -107,8 +107,10 @@ public class MiscController extends AbstractTransformerController
" '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout); " '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout);
} }
String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype, transformOptions); final String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype,
transformer.transform(transform, sourceFile, targetFile, sourceMimetype, targetMimetype, transformOptions); transformOptions);
transformer.transform(transform, sourceFile, targetFile, sourceMimetype, targetMimetype,
transformOptions);
} }
@PostMapping(value = "/transform", consumes = MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "/transform", consumes = MULTIPART_FORM_DATA_VALUE)
@ -127,7 +129,8 @@ public class MiscController extends AbstractTransformerController
logger.debug( logger.debug(
"Processing request with: sourceMimetype '{}', sourceEncoding '{}', " + "Processing request with: sourceMimetype '{}', sourceEncoding '{}', " +
"targetMimetype '{}', targetExtension '{}', targetEncoding '{}', pageLimit '{}'", "targetMimetype '{}', targetExtension '{}', targetEncoding '{}', pageLimit '{}'",
sourceMimetype, sourceEncoding, targetMimetype, targetExtension, targetEncoding, pageLimit); sourceMimetype, sourceEncoding, targetMimetype, targetExtension, targetEncoding,
pageLimit);
} }
final String targetFilename = createTargetFileName( final String targetFilename = createTargetFileName(
@ -136,13 +139,15 @@ public class MiscController extends AbstractTransformerController
final File sourceFile = createSourceFile(request, sourceMultipartFile); final File sourceFile = createSourceFile(request, sourceMultipartFile);
final File targetFile = createTargetFile(request, targetFilename); final File targetFile = createTargetFile(request, targetFilename);
Map<String, String> transformOptions = createTransformOptions( final Map<String, String> transformOptions = createTransformOptions(
"sourceEncoding", sourceEncoding, "sourceEncoding", sourceEncoding,
"targetEncoding", targetEncoding, "targetEncoding", targetEncoding,
"pageLimit", pageLimit); "pageLimit", pageLimit);
String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype, transformOptions); final String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype,
transformer.transform(transform, sourceFile, targetFile, sourceMimetype, targetMimetype, transformOptions); transformOptions);
transformer.transform(transform, sourceFile, targetFile, sourceMimetype, targetMimetype,
transformOptions);
final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile); final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile);
LogEntry.setTargetSize(targetFile.length()); LogEntry.setTargetSize(targetFile.length());

View File

@ -26,10 +26,6 @@
*/ */
package org.alfresco.transformer.transformers; package org.alfresco.transformer.transformers;
import org.alfresco.transformer.fs.FileManager;
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_HTML;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_MULTIPART_ALTERNATIVE; import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_MULTIPART_ALTERNATIVE;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN; import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
@ -37,7 +33,6 @@ import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
@ -54,6 +49,10 @@ import javax.mail.Part;
import javax.mail.Session; import javax.mail.Session;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import org.alfresco.transformer.fs.FileManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Uses javax.mail.MimeMessage to generate plain text versions of RFC822 email * Uses javax.mail.MimeMessage to generate plain text versions of RFC822 email
* messages. Searches for all text content parts, and returns them. Any * messages. Searches for all text content parts, and returns them. Any
@ -65,32 +64,33 @@ import javax.mail.internet.MimeMessage;
* <p> * <p>
* This code is based on a class of the same name originally implemented in alfresco-repository. * This code is based on a class of the same name originally implemented in alfresco-repository.
* </p> * </p>
*
*/ */
public class EMLTransformer implements SelectableTransformer public class EMLTransformer implements SelectableTransformer
{ {
private static final Logger logger = LoggerFactory.getLogger( EMLTransformer.class); private static final Logger logger = LoggerFactory.getLogger(EMLTransformer.class);
private static final String CHARSET = "charset"; private static final String CHARSET = "charset";
private static final String DEFAULT_ENCODING = "UTF-8"; private static final String DEFAULT_ENCODING = "UTF-8";
@Override @Override
public void transform(final File sourceFile, final File targetFile, final String sourceMimetype, public void transform(final File sourceFile, final File targetFile, final String sourceMimetype,
final String targetMimetype, final Map<String, String> parameters) throws Exception final String targetMimetype, final Map<String, String> parameters) throws Exception
{ {
logger.debug("Performing RFC822 to text transform."); logger.debug("Performing RFC822 to text transform.");
// Use try with resource // Use try with resource
try (InputStream contentInputStream = new BufferedInputStream( new FileInputStream(sourceFile)); try (InputStream contentInputStream = new BufferedInputStream(
new FileInputStream(sourceFile));
Writer bufferedFileWriter = new BufferedWriter(new FileWriter(targetFile))) Writer bufferedFileWriter = new BufferedWriter(new FileWriter(targetFile)))
{ {
MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()), contentInputStream); MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()),
contentInputStream);
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
Object content = mimeMessage.getContent(); Object content = mimeMessage.getContent();
if (content instanceof Multipart) if (content instanceof Multipart)
{ {
processMultiPart((Multipart) content,sb); processMultiPart((Multipart) content, sb);
} }
else else
{ {
@ -104,13 +104,15 @@ public class EMLTransformer implements SelectableTransformer
* Find "text" parts of message recursively and appends it to sb StringBuilder * Find "text" parts of message recursively and appends it to sb StringBuilder
* *
* @param multipart Multipart to process * @param multipart Multipart to process
* @param sb StringBuilder * @param sb StringBuilder
* @throws MessagingException * @throws MessagingException
* @throws IOException * @throws IOException
*/ */
private void processMultiPart(Multipart multipart, StringBuilder sb) throws MessagingException, IOException private void processMultiPart(Multipart multipart, StringBuilder sb) throws MessagingException,
IOException
{ {
boolean isAlternativeMultipart = multipart.getContentType().contains(MIMETYPE_MULTIPART_ALTERNATIVE); boolean isAlternativeMultipart = multipart.getContentType().contains(
MIMETYPE_MULTIPART_ALTERNATIVE);
if (isAlternativeMultipart) if (isAlternativeMultipart)
{ {
processAlternativeMultipart(multipart, sb); processAlternativeMultipart(multipart, sb);
@ -140,7 +142,8 @@ public class EMLTransformer implements SelectableTransformer
* @throws IOException * @throws IOException
* @throws MessagingException * @throws MessagingException
*/ */
private void processAlternativeMultipart(Multipart multipart, StringBuilder sb) throws IOException, MessagingException private void processAlternativeMultipart(Multipart multipart, StringBuilder sb) throws
IOException, MessagingException
{ {
Part partToUse = null; Part partToUse = null;
for (int i = 0, n = multipart.getCount(); i < n; i++) for (int i = 0, n = multipart.getCount(); i < n; i++)
@ -151,7 +154,7 @@ public class EMLTransformer implements SelectableTransformer
partToUse = part; partToUse = part;
break; break;
} }
else if (part.getContentType().contains(MIMETYPE_HTML)) else if (part.getContentType().contains(MIMETYPE_HTML))
{ {
partToUse = part; partToUse = part;
} }
@ -194,9 +197,11 @@ public class EMLTransformer implements SelectableTransformer
String mailPartContent = part.getContent().toString(); String mailPartContent = part.getContent().toString();
//create a temporary html file with same mail part content and encoding //create a temporary html file with same mail part content and encoding
File tempHtmlFile = FileManager.TempFileProvider.createTempFile("EMLTransformer_", ".html"); File tempHtmlFile = FileManager.TempFileProvider.createTempFile("EMLTransformer_",
".html");
String encoding = getMailPartContentEncoding(part); String encoding = getMailPartContentEncoding(part);
try (OutputStreamWriter osWriter = new OutputStreamWriter( new FileOutputStream( tempHtmlFile), encoding)) try (OutputStreamWriter osWriter = new OutputStreamWriter(
new FileOutputStream(tempHtmlFile), encoding))
{ {
osWriter.write(mailPartContent); osWriter.write(mailPartContent);
} }
@ -220,9 +225,9 @@ public class EMLTransformer implements SelectableTransformer
int startIndex = contentType.indexOf(CHARSET); int startIndex = contentType.indexOf(CHARSET);
if (startIndex > 0) if (startIndex > 0)
{ {
encoding = contentType.substring(startIndex + CHARSET.length() + 1).replaceAll("\"", ""); encoding = contentType.substring(startIndex + CHARSET.length() + 1)
.replaceAll("\"", "");
} }
return encoding; return encoding;
} }
} }

View File

@ -30,7 +30,6 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.StringJoiner; import java.util.StringJoiner;
@ -40,6 +39,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.google.common.collect.ImmutableMap;
/** /**
* The SelectingTransformer selects a registered {@link SelectableTransformer} * The SelectingTransformer selects a registered {@link SelectableTransformer}
* and delegates the transformation to its implementation. * and delegates the transformation to its implementation.
@ -51,23 +52,19 @@ public class SelectingTransformer
{ {
private static final Logger logger = LoggerFactory.getLogger(SelectingTransformer.class); private static final Logger logger = LoggerFactory.getLogger(SelectingTransformer.class);
private final Map<String, SelectableTransformer> transformers; private final Map<String, SelectableTransformer> transformers = ImmutableMap
.<String, SelectableTransformer>builder()
public SelectingTransformer() .put("appleIWorks", new AppleIWorksContentTransformer())
{ .put("html", new HtmlParserContentTransformer())
transformers = new HashMap<>(); .put("string", new StringExtractingContentTransformer())
transformers.put("appleIWorks", new AppleIWorksContentTransformer()); .put("textToPdf", new TextToPdfContentTransformer())
transformers.put("html", new HtmlParserContentTransformer()); .put("rfc822", new EMLTransformer())
transformers.put("string", new StringExtractingContentTransformer()); .put("ooXmlThumbnail", new OOXMLThumbnailContentTransformer())
transformers.put("textToPdf", new TextToPdfContentTransformer()); .build();
transformers.put("rfc822", new EMLTransformer());
transformers.put("ooXmlThumbnail", new OOXMLThumbnailContentTransformer());
}
/** /**
* Performs a transform using a transformer selected based on the provided sourceMimetype and targetMimetype * Performs a transform using a transformer selected based on the provided sourceMimetype and targetMimetype
* *
*
* @param transform the name of the transformer * @param transform the name of the transformer
* @param sourceFile File to transform from * @param sourceFile File to transform from
* @param targetFile File to transform to * @param targetFile File to transform to
@ -75,7 +72,7 @@ public class SelectingTransformer
* @throws TransformException if there was a problem internally * @throws TransformException if there was a problem internally
*/ */
public void transform(String transform, File sourceFile, File targetFile, String sourceMimetype, public void transform(String transform, File sourceFile, File targetFile, String sourceMimetype,
String targetMimetype, Map<String, String> parameters) throws TransformException String targetMimetype, Map<String, String> parameters) throws TransformException
{ {
try try
{ {

View File

@ -33,8 +33,8 @@ import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_KEYNOT
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_NUMBERS; import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_IWORK_NUMBERS;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_WORDPROCESSING; import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_OPENXML_WORDPROCESSING;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_PDF; import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_PDF;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_RFC822; import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_RFC822;
import static org.alfresco.transform.client.model.Mimetype.MIMETYPE_TEXT_PLAIN;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -42,7 +42,6 @@ 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.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -90,7 +89,7 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Override @Override
protected void mockTransformCommand(String sourceExtension, String targetExtension, protected void mockTransformCommand(String sourceExtension, String targetExtension,
String sourceMimetype, boolean readTargetFileBytes) throws IOException String sourceMimetype, boolean readTargetFileBytes)
{ {
} }
@ -109,14 +108,14 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile,
String... params) String... params)
{ {
final MockHttpServletRequestBuilder builder = super
MockHttpServletRequestBuilder builder = super.mockMvcRequest(url, sourceFile, params) .mockMvcRequest(url, sourceFile, params)
.param("sourceEncoding", sourceEncoding) .param("sourceEncoding", sourceEncoding)
.param("targetMimetype", targetMimetype) .param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype); .param("sourceMimetype", sourceMimetype);
// Only the 'string' transformer should have the targetEncoding. // Only the 'string' transformer should have the targetEncoding.
if ("text/plain".equals(targetMimetype) && !"message/rfc822".equals(sourceMimetype) && !"text/html".equals(sourceMimetype)) if (!"message/rfc822".equals(sourceMimetype) && !"text/html".equals(sourceMimetype))
{ {
builder.param("targetEncoding", targetEncoding); builder.param("targetEncoding", targetEncoding);
} }
@ -139,14 +138,14 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
{ {
String expected = "Gym class featuring a brown fox and lazy dog"; String expected = "Gym class featuring a brown fox and lazy dog";
MvcResult result = sendRequest("eml", MvcResult result = sendRequest("eml",
null, null,
MIMETYPE_RFC822, MIMETYPE_RFC822,
"txt", "txt",
MIMETYPE_TEXT_PLAIN, MIMETYPE_TEXT_PLAIN,
null, null,
readTestFile("eml")); readTestFile("eml"));
assertTrue("Content from eml transform didn't contain expected value. ", assertTrue("Content from eml transform didn't contain expected value. ",
result.getResponse().getContentAsString().contains(expected)); result.getResponse().getContentAsString().contains(expected));
} }
/** /**
@ -157,15 +156,16 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
{ {
String expected = "El r\u00E1pido zorro marr\u00F3n salta sobre el perro perezoso"; String expected = "El r\u00E1pido zorro marr\u00F3n salta sobre el perro perezoso";
MvcResult result = sendRequest("eml", MvcResult result = sendRequest("eml",
null, null,
MIMETYPE_RFC822, MIMETYPE_RFC822,
"txt", "txt",
MIMETYPE_TEXT_PLAIN, MIMETYPE_TEXT_PLAIN,
null, null,
readTestFile("spanish.eml")); readTestFile("spanish.eml"));
String contentResult = new String(result.getResponse().getContentAsByteArray(), "UTF-8"); String contentResult = new String(result.getResponse().getContentAsByteArray(), UTF_8);
assertTrue("Content from eml transform didn't contain expected value. ", contentResult.contains(expected)); assertTrue("Content from eml transform didn't contain expected value. ",
contentResult.contains(expected));
} }
/** /**
@ -174,17 +174,17 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Test @Test
public void testRFC822WithAttachmentToText() throws Exception public void testRFC822WithAttachmentToText() throws Exception
{ {
String expected = "Mail with attachment content"; String expected = "Mail with attachment content";
String notExpected = "File attachment content"; String notExpected = "File attachment content";
MvcResult result = sendRequest("eml", MvcResult result = sendRequest("eml",
null, null,
MIMETYPE_RFC822, MIMETYPE_RFC822,
"txt", "txt",
MIMETYPE_TEXT_PLAIN, MIMETYPE_TEXT_PLAIN,
null, null,
readTestFile("attachment.eml")); readTestFile("attachment.eml"));
assertTrue("Content from eml transform didn't contain expected value. ", assertTrue("Content from eml transform didn't contain expected value. ",
result.getResponse().getContentAsString().contains(expected)); result.getResponse().getContentAsString().contains(expected));
assertFalse(result.getResponse().getContentAsString().contains(notExpected)); assertFalse(result.getResponse().getContentAsString().contains(notExpected));
} }
@ -194,16 +194,16 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Test @Test
public void testRFC822AlternativeToText() throws Exception public void testRFC822AlternativeToText() throws Exception
{ {
String expected = "alternative plain text"; String expected = "alternative plain text";
MvcResult result = sendRequest("eml", MvcResult result = sendRequest("eml",
null, null,
MIMETYPE_RFC822, MIMETYPE_RFC822,
"txt", "txt",
MIMETYPE_TEXT_PLAIN, MIMETYPE_TEXT_PLAIN,
null, null,
readTestFile("alternative.eml")); readTestFile("alternative.eml"));
assertTrue("Content from eml transform didn't contain expected value. ", assertTrue("Content from eml transform didn't contain expected value. ",
result.getResponse().getContentAsString().contains(expected)); result.getResponse().getContentAsString().contains(expected));
} }
/** /**
@ -214,14 +214,14 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
{ {
String expected = "nested alternative plain text"; String expected = "nested alternative plain text";
MvcResult result = sendRequest("eml", MvcResult result = sendRequest("eml",
null, null,
MIMETYPE_RFC822, MIMETYPE_RFC822,
"txt", "txt",
MIMETYPE_TEXT_PLAIN, MIMETYPE_TEXT_PLAIN,
null, null,
readTestFile("nested.alternative.eml")); readTestFile("nested.alternative.eml"));
assertTrue("Content from eml transform didn't contain expected value. ", assertTrue("Content from eml transform didn't contain expected value. ",
result.getResponse().getContentAsString().contains(expected)); result.getResponse().getContentAsString().contains(expected));
} }
/** /**
@ -232,12 +232,12 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
{ {
String expected = "&nbsp;"; String expected = "&nbsp;";
MvcResult result = sendRequest("eml", MvcResult result = sendRequest("eml",
null, null,
MIMETYPE_RFC822, MIMETYPE_RFC822,
"txt", "txt",
MIMETYPE_TEXT_PLAIN, MIMETYPE_TEXT_PLAIN,
null, null,
readTestFile("htmlChars.eml")); readTestFile("htmlChars.eml"));
assertFalse(result.getResponse().getContentAsString().contains(expected)); assertFalse(result.getResponse().getContentAsString().contains(expected));
} }
@ -352,68 +352,74 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
public void testAppleIWorksPages() throws Exception public void testAppleIWorksPages() throws Exception
{ {
MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS, MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS,
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("pages")); "jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("pages"));
assertTrue("Expected image content but content is empty.",result.getResponse().getContentLengthLong() > 0L); assertTrue("Expected image content but content is empty.",
result.getResponse().getContentLengthLong() > 0L);
} }
@Test @Test
public void testAppleIWorksNumbers() throws Exception public void testAppleIWorksNumbers() throws Exception
{ {
MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS, MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS,
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("numbers")); "jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("numbers"));
assertTrue("Expected image content but content is empty.",result.getResponse().getContentLengthLong() > 0L); assertTrue("Expected image content but content is empty.",
result.getResponse().getContentLengthLong() > 0L);
} }
@Test @Test
public void testAppleIWorksKey() throws Exception public void testAppleIWorksKey() throws Exception
{ {
MvcResult result = sendRequest("key", null, MIMETYPE_IWORK_KEYNOTE, MvcResult result = sendRequest("key", null, MIMETYPE_IWORK_KEYNOTE,
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("key")); "jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("key"));
assertTrue("Expected image content but content is empty.",result.getResponse().getContentLengthLong() > 0L); assertTrue("Expected image content but content is empty.",
result.getResponse().getContentLengthLong() > 0L);
} }
// @Test // @Test
// TODO Doesn't work with java 11, enable when fixed // TODO Doesn't work with java 11, enable when fixed
public void testOOXML() throws Exception public void testOOXML() throws Exception
{ {
MvcResult result = sendRequest("docx",null, MIMETYPE_OPENXML_WORDPROCESSING, MvcResult result = sendRequest("docx", null, MIMETYPE_OPENXML_WORDPROCESSING,
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("docx")); "jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("docx"));
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 sendRequest(String sourceExtension, private MvcResult sendRequest(String sourceExtension,
String sourceEncoding, String sourceEncoding,
String sourceMimetype, String sourceMimetype,
String targetExtension, String targetExtension,
String targetMimetype, String targetMimetype,
String targetEncoding, String targetEncoding,
byte[] content) throws Exception byte[] content) throws Exception
{ {
MockMultipartFile sourceFile = new MockMultipartFile("file", "test_file." + sourceExtension, sourceMimetype, content); final MockMultipartFile sourceFile = new MockMultipartFile("file",
"test_file." + sourceExtension, sourceMimetype, content);
MockHttpServletRequestBuilder requestBuilder = super.mockMvcRequest("/transform", sourceFile) final MockHttpServletRequestBuilder requestBuilder = super
.param("targetExtension", targetExtension) .mockMvcRequest("/transform", sourceFile)
.param("targetMimetype", targetMimetype) .param("targetExtension", targetExtension)
.param("sourceMimetype", sourceMimetype); .param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype);
if (sourceEncoding!=null) if (sourceEncoding != null)
{ {
requestBuilder.param("sourceEncoding", sourceEncoding); requestBuilder.param("sourceEncoding", sourceEncoding);
} }
if (targetEncoding!=null) if (targetEncoding != null)
{ {
requestBuilder.param("targetEncoding", targetEncoding); requestBuilder.param("targetEncoding", targetEncoding);
} }
MvcResult result = mockMvc.perform(requestBuilder) return mockMvc.perform(requestBuilder)
.andExpect(status().is(OK.value())) .andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= " .andExpect(header().string("Content-Disposition",
+(targetEncoding==null ? "UTF-8" : targetEncoding)+"''test_file." + targetExtension)). "attachment; filename*= " +
andReturn(); (targetEncoding == null ? "UTF-8" : targetEncoding) +
return result; "''test_file." + targetExtension))
.andReturn();
} }
private String clean(String text) private String clean(String text)
{ {
text = text.replaceAll("\\s+\\r", ""); text = text.replaceAll("\\s+\\r", "");

View File

@ -116,7 +116,8 @@ public abstract class AbstractTransformerController implements TransformControll
public ResponseEntity<TransformConfig> info() public ResponseEntity<TransformConfig> info()
{ {
logger.info("GET Transform Config."); logger.info("GET Transform Config.");
TransformConfig transformConfig = ((TransformRegistryImpl)transformRegistry).getTransformConfig(); final TransformConfig transformConfig =
((TransformRegistryImpl) transformRegistry).getTransformConfig();
return new ResponseEntity<>(transformConfig, OK); return new ResponseEntity<>(transformConfig, OK);
} }
@ -337,15 +338,16 @@ public abstract class AbstractTransformerController implements TransformControll
return sb.toString(); return sb.toString();
} }
protected String getTransformerName(File sourceFile, String sourceMimetype, String targetMimetype, protected String getTransformerName(final File sourceFile, final String sourceMimetype,
Map<String, String> transformOptions) final String targetMimetype, final Map<String, String> transformOptions)
{ {
long sourceSizeInBytes = sourceFile.length(); final long sourceSizeInBytes = sourceFile.length();
String transformerName = transformRegistry.findTransformerName(sourceMimetype, sourceSizeInBytes, final String transformerName = transformRegistry.findTransformerName(sourceMimetype,
targetMimetype, transformOptions, null); sourceSizeInBytes, targetMimetype, transformOptions, null);
if (transformerName == null) if (transformerName == null)
{ {
throw new TransformException(BAD_REQUEST.value(), "No transforms were able to handle the request"); throw new TransformException(BAD_REQUEST.value(),
"No transforms were able to handle the request");
} }
return transformerName; return transformerName;
} }
@ -354,11 +356,12 @@ public abstract class AbstractTransformerController implements TransformControll
{ {
if (namesAndValues.length % 2 != 0) if (namesAndValues.length % 2 != 0)
{ {
logger.error("Incorrect number of parameters. Should have an even number as they are names and values."); logger.error(
"Incorrect number of parameters. Should have an even number as they are names and values.");
} }
Map<String, String> transformOptions = new HashMap<>(); Map<String, String> transformOptions = new HashMap<>();
for (int i=0; i<namesAndValues.length; i+=2) for (int i = 0; i < namesAndValues.length; i += 2)
{ {
String name = namesAndValues[i].toString(); String name = namesAndValues[i].toString();
Object value = namesAndValues[i + 1]; Object value = namesAndValues[i + 1];

View File

@ -25,7 +25,15 @@
*/ */
package org.alfresco.transformer; package org.alfresco.transformer;
import com.fasterxml.jackson.databind.ObjectMapper; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.annotation.PostConstruct;
import org.alfresco.transform.client.model.config.TransformConfig; import org.alfresco.transform.client.model.config.TransformConfig;
import org.alfresco.transform.client.registry.AbstractTransformRegistry; import org.alfresco.transform.client.registry.AbstractTransformRegistry;
import org.alfresco.transform.client.registry.TransformCache; import org.alfresco.transform.client.registry.TransformCache;
@ -35,13 +43,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import javax.annotation.PostConstruct; import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
/** /**
* Used by clients to work out if a transformation is supported based on the engine_config.json. * Used by clients to work out if a transformation is supported based on the engine_config.json.
@ -70,7 +72,7 @@ public class TransformRegistryImpl extends AbstractTransformRegistry
catch (IOException e) catch (IOException e)
{ {
throw new TransformException(INTERNAL_SERVER_ERROR.value(), throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Could not read "+ ENGINE_CONFIG_JSON, e); "Could not read " + ENGINE_CONFIG_JSON, e);
} }
} }

View File

@ -28,8 +28,8 @@ package org.alfresco.transformer.config;
import org.alfresco.transform.client.model.TransformRequestValidator; import org.alfresco.transform.client.model.TransformRequestValidator;
import org.alfresco.transform.client.registry.TransformServiceRegistry; import org.alfresco.transform.client.registry.TransformServiceRegistry;
import org.alfresco.transformer.TransformRegistryImpl;
import org.alfresco.transformer.TransformInterceptor; import org.alfresco.transformer.TransformInterceptor;
import org.alfresco.transformer.TransformRegistryImpl;
import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient; import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -44,8 +44,9 @@ public class WebApplicationConfig implements WebMvcConfigurer
@Override @Override
public void addInterceptors(InterceptorRegistry registry) public void addInterceptors(InterceptorRegistry registry)
{ {
registry.addInterceptor(transformInterceptor()).addPathPatterns("/transform", "/live", registry
"/ready"); .addInterceptor(transformInterceptor())
.addPathPatterns("/transform", "/live", "/ready");
} }
@Bean @Bean

View File

@ -37,18 +37,18 @@ public class Util
* @param param String to be converted * @param param String to be converted
* @return Null if param is null or converted value as {@link Integer} * @return Null if param is null or converted value as {@link Integer}
*/ */
public static Integer stringToInteger(String param) public static Integer stringToInteger(final String param)
{ {
return param == null ? null : Integer.parseInt(param); return param == null ? null : Integer.parseInt(param);
} }
/** /**
* Safely converts a {@link String} to an {@link Integer} * Safely converts a {@link String} to a {@link Boolean}
* *
* @param param String to be converted * @param param String to be converted
* @return Null if param is null or converted value as {@link Boolean} * @return Null if param is null or converted value as {@link Boolean}
*/ */
public static Boolean stringToBoolean(String param) public static Boolean stringToBoolean(final String param)
{ {
return param == null ? null : Boolean.parseBoolean(param); return param == null ? null : Boolean.parseBoolean(param);
} }

View File

@ -95,7 +95,7 @@ public abstract class AbstractHttpRequestTest
} }
@Test @Test
public void noTargetExtensionError() throws Exception public void noTargetExtensionError()
{ {
assertMissingParameter("targetExtension"); assertMissingParameter("targetExtension");
} }
@ -122,10 +122,11 @@ public abstract class AbstractHttpRequestTest
sendTranformationRequest(entity, errorMessage); sendTranformationRequest(entity, errorMessage);
} }
protected void sendTranformationRequest(HttpEntity<LinkedMultiValueMap<String, Object>> entity, String errorMessage) protected void sendTranformationRequest(
final HttpEntity<LinkedMultiValueMap<String, Object>> entity, final String errorMessage)
{ {
ResponseEntity<String> response = restTemplate.exchange("/transform", POST, entity, final ResponseEntity<String> response = restTemplate.exchange("/transform", POST, entity,
String.class, ""); String.class, "");
assertEquals(errorMessage, getErrorMessage(response.getBody())); assertEquals(errorMessage, getErrorMessage(response.getBody()));
} }

View File

@ -59,8 +59,8 @@ import org.alfresco.transform.client.model.config.TransformConfig;
import org.alfresco.transform.client.model.config.TransformOption; import org.alfresco.transform.client.model.config.TransformOption;
import org.alfresco.transform.client.model.config.TransformOptionGroup; import org.alfresco.transform.client.model.config.TransformOptionGroup;
import org.alfresco.transform.client.model.config.TransformOptionValue; import org.alfresco.transform.client.model.config.TransformOptionValue;
import org.alfresco.transform.client.registry.TransformServiceRegistry;
import org.alfresco.transform.client.model.config.Transformer; import org.alfresco.transform.client.model.config.Transformer;
import org.alfresco.transform.client.registry.TransformServiceRegistry;
import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient; import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient;
import org.alfresco.transformer.probes.ProbeTestTransform; import org.alfresco.transformer.probes.ProbeTestTransform;
import org.junit.Test; import org.junit.Test;
@ -68,7 +68,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
@ -111,7 +110,7 @@ public abstract class AbstractTransformerControllerTest
* The expected result. Taken resting target quick file's bytes. * The expected result. Taken resting target quick file's bytes.
* *
* Note: These checks generally don't work on Windows (Mac and Linux are okay). Possibly to do with byte order * Note: These checks generally don't work on Windows (Mac and Linux are okay). Possibly to do with byte order
* loading. * loading.
*/ */
protected byte[] expectedTargetFileBytes; protected byte[] expectedTargetFileBytes;
@ -347,8 +346,8 @@ public abstract class AbstractTransformerControllerTest
.readValue(new ClassPathResource("engine_config.json").getFile(), .readValue(new ClassPathResource("engine_config.json").getFile(),
TransformConfig.class); TransformConfig.class);
ReflectionTestUtils.setField(transformRegistry,"engineConfig", ReflectionTestUtils.setField(transformRegistry, "engineConfig",
new ClassPathResource("engine_config.json")); new ClassPathResource("engine_config.json"));
String response = mockMvc String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config")) .perform(MockMvcRequestBuilders.get("/transform/config"))
@ -366,8 +365,8 @@ public abstract class AbstractTransformerControllerTest
{ {
TransformConfig expectedResult = buildCompleteTransformConfig(); TransformConfig expectedResult = buildCompleteTransformConfig();
ReflectionTestUtils.setField(transformRegistry,"engineConfig", ReflectionTestUtils.setField(transformRegistry, "engineConfig",
new ClassPathResource("engine_config_with_duplicates.json")); new ClassPathResource("engine_config_with_duplicates.json"));
String response = mockMvc String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config")) .perform(MockMvcRequestBuilders.get("/transform/config"))
@ -393,8 +392,8 @@ public abstract class AbstractTransformerControllerTest
TransformConfig expectedResult = new TransformConfig(); TransformConfig expectedResult = new TransformConfig();
expectedResult.setTransformers(ImmutableList.of(transformer)); expectedResult.setTransformers(ImmutableList.of(transformer));
ReflectionTestUtils.setField(transformRegistry,"engineConfig", ReflectionTestUtils.setField(transformRegistry, "engineConfig",
new ClassPathResource("engine_config_incomplete.json")); new ClassPathResource("engine_config_incomplete.json"));
String response = mockMvc String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config")) .perform(MockMvcRequestBuilders.get("/transform/config"))
@ -416,8 +415,8 @@ public abstract class AbstractTransformerControllerTest
TransformConfig expectedResult = new TransformConfig(); TransformConfig expectedResult = new TransformConfig();
expectedResult.setTransformers(ImmutableList.of(transformer)); expectedResult.setTransformers(ImmutableList.of(transformer));
ReflectionTestUtils.setField(transformRegistry,"engineConfig", ReflectionTestUtils.setField(transformRegistry, "engineConfig",
new ClassPathResource("engine_config_no_transform_options.json")); new ClassPathResource("engine_config_no_transform_options.json"));
String response = mockMvc String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config")) .perform(MockMvcRequestBuilders.get("/transform/config"))

View File

@ -23,7 +23,7 @@
<dependency.pdfbox.version>2.0.16</dependency.pdfbox.version> <dependency.pdfbox.version>2.0.16</dependency.pdfbox.version>
<dependency.alfresco-jodconverter-core.version>3.0.1.1</dependency.alfresco-jodconverter-core.version> <dependency.alfresco-jodconverter-core.version>3.0.1.1</dependency.alfresco-jodconverter-core.version>
<env.project_version>${project.version}</env.project_version> <env.project_version>${project.version}</env.project_version>
<dependency.alfresco-transform-model.version>1.0.2.7-ATS545-2</dependency.alfresco-transform-model.version> <dependency.alfresco-transform-model.version>1.0.2.8-SNAPSHOT</dependency.alfresco-transform-model.version>
<dependency.activemq.version>5.15.9</dependency.activemq.version> <dependency.activemq.version>5.15.9</dependency.activemq.version>
<dependency.jackson.version>2.9.9</dependency.jackson.version> <dependency.jackson.version>2.9.9</dependency.jackson.version>
<dependency.jackson-databind.version>2.9.9.3</dependency.jackson-databind.version> <dependency.jackson-databind.version>2.9.9.3</dependency.jackson-databind.version>