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
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"),