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;
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.NOT_EXTRACT_BOOKMARKS_TEXT;
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.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.
*
@ -117,43 +118,50 @@ public class TikaController extends AbstractTransformerController
@PostMapping(value = "/transform", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Resource> transform(HttpServletRequest request,
@RequestParam("file") MultipartFile sourceMultipartFile,
@RequestParam("sourceMimetype") String sourceMimetype,
@RequestParam("targetExtension") String targetExtension,
@RequestParam("targetMimetype") String targetMimetype,
@RequestParam("targetEncoding") String targetEncoding,
@RequestParam("file") final MultipartFile sourceMultipartFile,
@RequestParam("sourceMimetype") final String sourceMimetype,
@RequestParam("targetExtension") final String targetExtension,
@RequestParam("targetMimetype") final String targetMimetype,
@RequestParam("targetEncoding") final String targetEncoding,
@RequestParam(value = "timeout", required = false) Long timeout,
@RequestParam(value = "testDelay", required = false) Long testDelay,
@RequestParam(value = "timeout", required = false) final Long timeout,
@RequestParam(value = "testDelay", required = false) final Long testDelay,
@RequestParam(value = "includeContents", required = false) Boolean includeContents,
@RequestParam(value = "notExtractBookmarksText", required = false) Boolean notExtractBookmarksText)
@RequestParam(value = "includeContents", required = false) final Boolean includeContents,
@RequestParam(value = "notExtractBookmarksText", required = false) final Boolean notExtractBookmarksText)
{
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(),
targetExtension);
final String targetFilename = createTargetFileName(
sourceMultipartFile.getOriginalFilename(), targetExtension);
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
// 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
Map<String, String> transformOptions = createTransformOptions(
"includeContents", includeContents,
"notExtractBookmarksText", notExtractBookmarksText,
"targetEncoding", targetEncoding);
String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype, transformOptions);
final Map<String, String> transformOptions = createTransformOptions(
"includeContents", includeContents,
"notExtractBookmarksText", notExtractBookmarksText,
"targetEncoding", targetEncoding);
final String transform = getTransformerName(sourceFile, sourceMimetype, targetMimetype,
transformOptions);
javaExecutor.call(sourceFile, targetFile, transform,
includeContents != null && includeContents ? INCLUDE_CONTENTS : null,
notExtractBookmarksText != null && notExtractBookmarksText ? NOT_EXTRACT_BOOKMARKS_TEXT : null,
TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + targetEncoding);
final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile);
LogEntry.setTargetSize(targetFile.length());
long time = LogEntry.setStatusCodeAndMessage(OK.value(), "Success");
time += LogEntry.addDelay(testDelay);
getProbeTestTransform().recordTransformTime(time);
return body;
}
@ -165,11 +173,13 @@ public class TikaController extends AbstractTransformerController
logger.debug("Processing request with: sourceFile '{}', targetFile '{}', transformOptions" +
" '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout);
final Boolean includeContents = stringToBoolean("includeContents");
final Boolean notExtractBookmarksText = stringToBoolean("notExtractBookmarksText");
final Boolean includeContents = stringToBoolean(transformOptions.get("includeContents"));
final Boolean notExtractBookmarksText = stringToBoolean(
transformOptions.get("notExtractBookmarksText"));
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,
includeContents != null && includeContents ? INCLUDE_CONTENTS : null,
notExtractBookmarksText != null && notExtractBookmarksText ? NOT_EXTRACT_BOOKMARKS_TEXT : null,

View File

@ -26,7 +26,34 @@
*/
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.exception.TikaException;
import org.apache.tika.extractor.DocumentSelector;
@ -46,32 +73,7 @@ import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
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 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;
import com.google.common.collect.ImmutableList;
/**
* 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;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
/**
* Tests TikaController with a server test harness.
*/
@ -63,16 +64,13 @@ public class TikaHttpRequestTest extends AbstractHttpRequestTest
LinkedMultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
if (addFile)
{
parameters.add("file",
new org.springframework.core.io.ClassPathResource("quick." + getSourceExtension()));
parameters.add("file", new ClassPathResource("quick." + getSourceExtension()));
}
parameters.add("sourceMimetype", "application/pdf");
parameters.add("sourceMimetype", "application/pdf");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(parameters,
headers);
headers);
super.sendTranformationRequest(entity, errorMessage);
}
}

View File

@ -88,7 +88,8 @@ public class TikaTransformationIT
return Stream
.of(
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.jar", "application/java-archive"),
allTargets("quick.java", "text/x-java-source"),
@ -122,12 +123,14 @@ public class TikaTransformationIT
),
allTargets("quick.pdf", "application/pdf"),
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.txt", "text/plain"),
allTargets("quick.vsd", "application/vnd.visio"),
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.tar", "application/x-tar"),
allTargets("sample.rtf", "application/rtf"),

View File

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

View File

@ -26,10 +26,6 @@
*/
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_MULTIPART_ALTERNATIVE;
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.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
@ -54,6 +49,10 @@ import javax.mail.Part;
import javax.mail.Session;
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
* messages. Searches for all text content parts, and returns them. Any
@ -65,32 +64,33 @@ import javax.mail.internet.MimeMessage;
* <p>
* This code is based on a class of the same name originally implemented in alfresco-repository.
* </p>
*
*/
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 DEFAULT_ENCODING = "UTF-8";
@Override
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.");
// 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)))
{
MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()), contentInputStream);
MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()),
contentInputStream);
final StringBuilder sb = new StringBuilder();
Object content = mimeMessage.getContent();
if (content instanceof Multipart)
{
processMultiPart((Multipart) content,sb);
processMultiPart((Multipart) content, sb);
}
else
{
@ -104,13 +104,15 @@ public class EMLTransformer implements SelectableTransformer
* Find "text" parts of message recursively and appends it to sb StringBuilder
*
* @param multipart Multipart to process
* @param sb StringBuilder
* @param sb StringBuilder
* @throws MessagingException
* @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)
{
processAlternativeMultipart(multipart, sb);
@ -140,7 +142,8 @@ public class EMLTransformer implements SelectableTransformer
* @throws IOException
* @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;
for (int i = 0, n = multipart.getCount(); i < n; i++)
@ -151,7 +154,7 @@ public class EMLTransformer implements SelectableTransformer
partToUse = part;
break;
}
else if (part.getContentType().contains(MIMETYPE_HTML))
else if (part.getContentType().contains(MIMETYPE_HTML))
{
partToUse = part;
}
@ -194,9 +197,11 @@ public class EMLTransformer implements SelectableTransformer
String mailPartContent = part.getContent().toString();
//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);
try (OutputStreamWriter osWriter = new OutputStreamWriter( new FileOutputStream( tempHtmlFile), encoding))
try (OutputStreamWriter osWriter = new OutputStreamWriter(
new FileOutputStream(tempHtmlFile), encoding))
{
osWriter.write(mailPartContent);
}
@ -220,9 +225,9 @@ public class EMLTransformer implements SelectableTransformer
int startIndex = contentType.indexOf(CHARSET);
if (startIndex > 0)
{
encoding = contentType.substring(startIndex + CHARSET.length() + 1).replaceAll("\"", "");
encoding = contentType.substring(startIndex + CHARSET.length() + 1)
.replaceAll("\"", "");
}
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 java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
@ -40,6 +39,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.google.common.collect.ImmutableMap;
/**
* The SelectingTransformer selects a registered {@link SelectableTransformer}
* and delegates the transformation to its implementation.
@ -51,23 +52,19 @@ public class SelectingTransformer
{
private static final Logger logger = LoggerFactory.getLogger(SelectingTransformer.class);
private final Map<String, SelectableTransformer> transformers;
public SelectingTransformer()
{
transformers = new HashMap<>();
transformers.put("appleIWorks", new AppleIWorksContentTransformer());
transformers.put("html", new HtmlParserContentTransformer());
transformers.put("string", new StringExtractingContentTransformer());
transformers.put("textToPdf", new TextToPdfContentTransformer());
transformers.put("rfc822", new EMLTransformer());
transformers.put("ooXmlThumbnail", new OOXMLThumbnailContentTransformer());
}
private final Map<String, SelectableTransformer> transformers = ImmutableMap
.<String, SelectableTransformer>builder()
.put("appleIWorks", new AppleIWorksContentTransformer())
.put("html", new HtmlParserContentTransformer())
.put("string", new StringExtractingContentTransformer())
.put("textToPdf", new TextToPdfContentTransformer())
.put("rfc822", new EMLTransformer())
.put("ooXmlThumbnail", new OOXMLThumbnailContentTransformer())
.build();
/**
* Performs a transform using a transformer selected based on the provided sourceMimetype and targetMimetype
*
*
* @param transform the name of the transformer
* @param sourceFile File to transform from
* @param targetFile File to transform to
@ -75,7 +72,7 @@ public class SelectingTransformer
* @throws TransformException if there was a problem internally
*/
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
{

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_OPENXML_WORDPROCESSING;
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_TEXT_PLAIN;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
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.status;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
@ -90,7 +89,7 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Override
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,
String... params)
{
MockHttpServletRequestBuilder builder = super.mockMvcRequest(url, sourceFile, params)
.param("sourceEncoding", sourceEncoding)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype);
final MockHttpServletRequestBuilder builder = super
.mockMvcRequest(url, sourceFile, params)
.param("sourceEncoding", sourceEncoding)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype);
// 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);
}
@ -139,14 +138,14 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
{
String expected = "Gym class featuring a brown fox and lazy dog";
MvcResult result = sendRequest("eml",
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("eml"));
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("eml"));
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";
MvcResult result = sendRequest("eml",
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("spanish.eml"));
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("spanish.eml"));
String contentResult = new String(result.getResponse().getContentAsByteArray(), "UTF-8");
assertTrue("Content from eml transform didn't contain expected value. ", contentResult.contains(expected));
String contentResult = new String(result.getResponse().getContentAsByteArray(), UTF_8);
assertTrue("Content from eml transform didn't contain expected value. ",
contentResult.contains(expected));
}
/**
@ -174,17 +174,17 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Test
public void testRFC822WithAttachmentToText() throws Exception
{
String expected = "Mail with attachment content";
String notExpected = "File attachment content";
String expected = "Mail with attachment content";
String notExpected = "File attachment content";
MvcResult result = sendRequest("eml",
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("attachment.eml"));
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("attachment.eml"));
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));
}
@ -194,16 +194,16 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
@Test
public void testRFC822AlternativeToText() throws Exception
{
String expected = "alternative plain text";
String expected = "alternative plain text";
MvcResult result = sendRequest("eml",
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("alternative.eml"));
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("alternative.eml"));
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";
MvcResult result = sendRequest("eml",
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("nested.alternative.eml"));
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("nested.alternative.eml"));
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;";
MvcResult result = sendRequest("eml",
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("htmlChars.eml"));
null,
MIMETYPE_RFC822,
"txt",
MIMETYPE_TEXT_PLAIN,
null,
readTestFile("htmlChars.eml"));
assertFalse(result.getResponse().getContentAsString().contains(expected));
}
@ -352,68 +352,74 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
public void testAppleIWorksPages() throws Exception
{
MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS,
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("pages"));
assertTrue("Expected image content but content is empty.",result.getResponse().getContentLengthLong() > 0L);
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("pages"));
assertTrue("Expected image content but content is empty.",
result.getResponse().getContentLengthLong() > 0L);
}
@Test
public void testAppleIWorksNumbers() throws Exception
{
MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS,
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("numbers"));
assertTrue("Expected image content but content is empty.",result.getResponse().getContentLengthLong() > 0L);
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("numbers"));
assertTrue("Expected image content but content is empty.",
result.getResponse().getContentLengthLong() > 0L);
}
@Test
public void testAppleIWorksKey() throws Exception
{
MvcResult result = sendRequest("key", null, MIMETYPE_IWORK_KEYNOTE,
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("key"));
assertTrue("Expected image content but content is empty.",result.getResponse().getContentLengthLong() > 0L);
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("key"));
assertTrue("Expected image content but content is empty.",
result.getResponse().getContentLengthLong() > 0L);
}
// @Test
// @Test
// TODO Doesn't work with java 11, enable when fixed
public void testOOXML() throws Exception
{
MvcResult result = sendRequest("docx",null, MIMETYPE_OPENXML_WORDPROCESSING,
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("docx"));
assertTrue("Expected image content but content is empty.",result.getResponse().getContentLengthLong() > 0L);
MvcResult result = sendRequest("docx", null, MIMETYPE_OPENXML_WORDPROCESSING,
"jpeg", MIMETYPE_IMAGE_JPEG, null, readTestFile("docx"));
assertTrue("Expected image content but content is empty.",
result.getResponse().getContentLengthLong() > 0L);
}
private MvcResult sendRequest(String sourceExtension,
String sourceEncoding,
String sourceMimetype,
String targetExtension,
String targetMimetype,
String targetEncoding,
byte[] content) throws Exception
String sourceEncoding,
String sourceMimetype,
String targetExtension,
String targetMimetype,
String targetEncoding,
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)
.param("targetExtension", targetExtension)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype);
final MockHttpServletRequestBuilder requestBuilder = super
.mockMvcRequest("/transform", sourceFile)
.param("targetExtension", targetExtension)
.param("targetMimetype", targetMimetype)
.param("sourceMimetype", sourceMimetype);
if (sourceEncoding!=null)
if (sourceEncoding != null)
{
requestBuilder.param("sourceEncoding", sourceEncoding);
}
if (targetEncoding!=null)
if (targetEncoding != null)
{
requestBuilder.param("targetEncoding", targetEncoding);
}
MvcResult result = mockMvc.perform(requestBuilder)
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition", "attachment; filename*= "
+(targetEncoding==null ? "UTF-8" : targetEncoding)+"''test_file." + targetExtension)).
andReturn();
return result;
return mockMvc.perform(requestBuilder)
.andExpect(status().is(OK.value()))
.andExpect(header().string("Content-Disposition",
"attachment; filename*= " +
(targetEncoding == null ? "UTF-8" : targetEncoding) +
"''test_file." + targetExtension))
.andReturn();
}
private String clean(String text)
{
text = text.replaceAll("\\s+\\r", "");

View File

@ -116,7 +116,8 @@ public abstract class AbstractTransformerController implements TransformControll
public ResponseEntity<TransformConfig> info()
{
logger.info("GET Transform Config.");
TransformConfig transformConfig = ((TransformRegistryImpl)transformRegistry).getTransformConfig();
final TransformConfig transformConfig =
((TransformRegistryImpl) transformRegistry).getTransformConfig();
return new ResponseEntity<>(transformConfig, OK);
}
@ -337,15 +338,16 @@ public abstract class AbstractTransformerController implements TransformControll
return sb.toString();
}
protected String getTransformerName(File sourceFile, String sourceMimetype, String targetMimetype,
Map<String, String> transformOptions)
protected String getTransformerName(final File sourceFile, final String sourceMimetype,
final String targetMimetype, final Map<String, String> transformOptions)
{
long sourceSizeInBytes = sourceFile.length();
String transformerName = transformRegistry.findTransformerName(sourceMimetype, sourceSizeInBytes,
targetMimetype, transformOptions, null);
final long sourceSizeInBytes = sourceFile.length();
final String transformerName = transformRegistry.findTransformerName(sourceMimetype,
sourceSizeInBytes, targetMimetype, transformOptions, 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;
}
@ -354,11 +356,12 @@ public abstract class AbstractTransformerController implements TransformControll
{
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<>();
for (int i=0; i<namesAndValues.length; i+=2)
for (int i = 0; i < namesAndValues.length; i += 2)
{
String name = namesAndValues[i].toString();
Object value = namesAndValues[i + 1];

View File

@ -25,7 +25,15 @@
*/
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.registry.AbstractTransformRegistry;
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.core.io.Resource;
import javax.annotation.PostConstruct;
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;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* 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)
{
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.registry.TransformServiceRegistry;
import org.alfresco.transformer.TransformRegistryImpl;
import org.alfresco.transformer.TransformInterceptor;
import org.alfresco.transformer.TransformRegistryImpl;
import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -44,8 +44,9 @@ public class WebApplicationConfig implements WebMvcConfigurer
@Override
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(transformInterceptor()).addPathPatterns("/transform", "/live",
"/ready");
registry
.addInterceptor(transformInterceptor())
.addPathPatterns("/transform", "/live", "/ready");
}
@Bean

View File

@ -37,18 +37,18 @@ public class Util
* @param param String to be converted
* @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);
}
/**
* Safely converts a {@link String} to an {@link Integer}
* Safely converts a {@link String} to a {@link Boolean}
*
* @param param String to be converted
* @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);
}

View File

@ -95,7 +95,7 @@ public abstract class AbstractHttpRequestTest
}
@Test
public void noTargetExtensionError() throws Exception
public void noTargetExtensionError()
{
assertMissingParameter("targetExtension");
}
@ -122,10 +122,11 @@ public abstract class AbstractHttpRequestTest
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,
String.class, "");
final ResponseEntity<String> response = restTemplate.exchange("/transform", POST, entity,
String.class, "");
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.TransformOptionGroup;
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.registry.TransformServiceRegistry;
import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient;
import org.alfresco.transformer.probes.ProbeTestTransform;
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.SpyBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;
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.
*
* 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;
@ -347,8 +346,8 @@ public abstract class AbstractTransformerControllerTest
.readValue(new ClassPathResource("engine_config.json").getFile(),
TransformConfig.class);
ReflectionTestUtils.setField(transformRegistry,"engineConfig",
new ClassPathResource("engine_config.json"));
ReflectionTestUtils.setField(transformRegistry, "engineConfig",
new ClassPathResource("engine_config.json"));
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
@ -366,8 +365,8 @@ public abstract class AbstractTransformerControllerTest
{
TransformConfig expectedResult = buildCompleteTransformConfig();
ReflectionTestUtils.setField(transformRegistry,"engineConfig",
new ClassPathResource("engine_config_with_duplicates.json"));
ReflectionTestUtils.setField(transformRegistry, "engineConfig",
new ClassPathResource("engine_config_with_duplicates.json"));
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
@ -393,8 +392,8 @@ public abstract class AbstractTransformerControllerTest
TransformConfig expectedResult = new TransformConfig();
expectedResult.setTransformers(ImmutableList.of(transformer));
ReflectionTestUtils.setField(transformRegistry,"engineConfig",
new ClassPathResource("engine_config_incomplete.json"));
ReflectionTestUtils.setField(transformRegistry, "engineConfig",
new ClassPathResource("engine_config_incomplete.json"));
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
@ -416,8 +415,8 @@ public abstract class AbstractTransformerControllerTest
TransformConfig expectedResult = new TransformConfig();
expectedResult.setTransformers(ImmutableList.of(transformer));
ReflectionTestUtils.setField(transformRegistry,"engineConfig",
new ClassPathResource("engine_config_no_transform_options.json"));
ReflectionTestUtils.setField(transformRegistry, "engineConfig",
new ClassPathResource("engine_config_no_transform_options.json"));
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))

View File

@ -23,7 +23,7 @@
<dependency.pdfbox.version>2.0.16</dependency.pdfbox.version>
<dependency.alfresco-jodconverter-core.version>3.0.1.1</dependency.alfresco-jodconverter-core.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.jackson.version>2.9.9</dependency.jackson.version>
<dependency.jackson-databind.version>2.9.9.3</dependency.jackson-databind.version>