feature/ATS-16

This commit is contained in:
Lucian Tuca
2018-08-17 09:32:25 +01:00
parent c4ca88d876
commit dda632c7a5
22 changed files with 983 additions and 145 deletions

View File

@@ -11,7 +11,12 @@
*/
package org.alfresco.transformer;
import com.sun.star.task.ErrorCodeIOException;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
@@ -21,15 +26,14 @@ import org.artofsolving.jodconverter.office.OfficeException;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
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.io.IOException;
import com.sun.star.task.ErrorCodeIOException;
/**
* Controller for the Docker based LibreOffice transformer.
@@ -135,14 +139,14 @@ public class LibreOfficeController extends AbstractTransformerController
};
}
@PostMapping("/transform")
@PostMapping(value = "/transform", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Resource> transform(HttpServletRequest request,
@RequestParam("file") MultipartFile sourceMultipartFile,
@RequestParam("targetExtension") String targetExtension,
@RequestParam(value = "timeout", required = false) Long timeout,
@RequestParam(value = "testDelay", required = false) Long testDelay)
{
String targetFilename = createTargetFileName(sourceMultipartFile, targetExtension);
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), targetExtension);
File sourceFile = createSourceFile(request, sourceMultipartFile);
File targetFile = createTargetFile(request, targetFilename);
// Both files are deleted by TransformInterceptor.afterCompletion
@@ -152,6 +156,13 @@ public class LibreOfficeController extends AbstractTransformerController
return createAttachment(targetFilename, targetFile, testDelay);
}
@Override
protected void processTransform(File sourceFile, File targetFile,
Map<String, String> transformOptions, Long timeout)
{
executeTransformCommand(sourceFile, targetFile, timeout);
}
protected void executeTransformCommand(File sourceFile, File targetFile, Long timeout)
{
timeout = timeout != null && timeout > 0 ? timeout : 0;

View File

@@ -25,6 +25,22 @@
*/
package org.alfresco.transformer;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import org.alfresco.transform.client.model.TransformRequest;
import org.artofsolving.jodconverter.office.OfficeException;
import org.junit.Before;
import org.junit.Test;
@@ -35,22 +51,7 @@ import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.Arrays;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.util.StringUtils;
/**
* Test the LibreOfficeController without a server.
@@ -66,6 +67,7 @@ public class LibreOfficeControllerTest extends AbstractTransformerControllerTest
@Before
public void before() throws IOException
{
controller.setAlfrescoSharedFileStoreClient(alfrescoSharedFileStoreClient);
super.controller = controller;
sourceExtension = "doc";
@@ -83,6 +85,7 @@ public class LibreOfficeControllerTest extends AbstractTransformerControllerTest
{
File sourceFile = invocation.getArgumentAt(0, File.class);
File targetFile = invocation.getArgumentAt(1, File.class);
String actualTargetExtension = StringUtils.getFilenameExtension(targetFile.getAbsolutePath());
assertNotNull(sourceFile);
assertNotNull(targetFile);
@@ -101,12 +104,7 @@ public class LibreOfficeControllerTest extends AbstractTransformerControllerTest
{
String testFilename = actualTarget.substring(i+1);
File testFile = getTestFile(testFilename, false);
if (testFile != null)
{
FileChannel source = new FileInputStream(testFile).getChannel();
FileChannel target = new FileOutputStream(targetFile).getChannel();
target.transferFrom(source, 0, source.size());
}
generateTargetFileFromResourceFile(actualTargetExtension, testFile, targetFile);
}
// Check the supplied source file has not been changed.
@@ -129,4 +127,11 @@ public class LibreOfficeControllerTest extends AbstractTransformerControllerTest
.andExpect(status().is(400))
.andExpect(status().reason(containsString("LibreOffice - LibreOffice server conversion failed:")));
}
@Override
protected void updateTransformRequestWithSpecificOptions(TransformRequest transformRequest)
{
transformRequest.setSourceExtension("doc");
transformRequest.setTargetExtension("pdf");
}
}