ATS-213 : Transformers should have unique option name

This commit is contained in:
Cezar Leahu 2018-12-11 16:02:04 +00:00
parent d50cc236ab
commit 54a7b07e55
8 changed files with 159 additions and 87 deletions

View File

@ -21,7 +21,6 @@ import static org.springframework.http.HttpStatus.OK;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.StringJoiner;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -110,25 +109,14 @@ public class AlfrescoPdfRendererController 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);
String page = transformOptions.get("page"); final String options = OptionsBuilder
Integer pageOption = page == null ? null : Integer.parseInt(page); .builder()
.withPage(transformOptions.get("page"))
String width = transformOptions.get("width"); .withWidth(transformOptions.get("width"))
Integer widthOption = width == null ? null : Integer.parseInt(width); .withHeight(transformOptions.get("height"))
.withAllowPdfEnlargement(transformOptions.get("allowPdfEnlargement"))
String height = transformOptions.get("height"); .withMaintainPdfAspectRatio(transformOptions.get("maintainPdfAspectRatio"))
Integer heightOption = height == null ? null : Integer.parseInt(height); .build();
String allowEnlargement = transformOptions.get("allowEnlargement");
Boolean allowEnlargementOption =
allowEnlargement == null ? null : Boolean.parseBoolean(allowEnlargement);
String maintainAspectRatio = transformOptions.get("maintainAspectRatio");
Boolean maintainAspectRatioOption =
maintainAspectRatio == null ? null : Boolean.parseBoolean(maintainAspectRatio);
String options = buildTransformOptions(pageOption, widthOption, heightOption,
allowEnlargementOption, maintainAspectRatioOption);
commandExecutor.run(options, sourceFile, targetFile, timeout); commandExecutor.run(options, sourceFile, targetFile, timeout);
} }
@ -144,8 +132,8 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "width", required = false) Integer width, @RequestParam(value = "width", required = false) Integer width,
@RequestParam(value = "height", required = false) Integer height, @RequestParam(value = "height", required = false) Integer height,
@RequestParam(value = "allowEnlargement", required = false) Boolean allowEnlargement, @RequestParam(value = "allowPdfEnlargement", required = false) Boolean allowPdfEnlargement,
@RequestParam(value = "maintainAspectRatio", required = false) Boolean maintainAspectRatio) @RequestParam(value = "maintainPdfAspectRatio", required = false) Boolean maintainPdfAspectRatio)
{ {
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), targetExtension); String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(), targetExtension);
getProbeTestTransform().incrementTransformerCount(); getProbeTestTransform().incrementTransformerCount();
@ -153,8 +141,15 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
File targetFile = createTargetFile(request, targetFilename); File targetFile = createTargetFile(request, targetFilename);
// Both files are deleted by TransformInterceptor.afterCompletion // Both files are deleted by TransformInterceptor.afterCompletion
String options = buildTransformOptions(page, width, height, allowEnlargement, final String options = OptionsBuilder
maintainAspectRatio); .builder()
.withPage(page)
.withWidth(width)
.withHeight(height)
.withAllowPdfEnlargement(allowPdfEnlargement)
.withMaintainPdfAspectRatio(maintainPdfAspectRatio)
.build();
commandExecutor.run(options, sourceFile, targetFile, timeout); commandExecutor.run(options, sourceFile, targetFile, timeout);
final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile); final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile);
@ -164,31 +159,4 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
getProbeTestTransform().recordTransformTime(time); getProbeTestTransform().recordTransformTime(time);
return body; return body;
} }
private static String buildTransformOptions(Integer page, Integer width, Integer height, Boolean
allowEnlargement, Boolean maintainAspectRatio)
{
StringJoiner args = new StringJoiner(" ");
if (width != null && width >= 0)
{
args.add("--width=" + width);
}
if (height != null && height >= 0)
{
args.add("--height=" + height);
}
if (allowEnlargement != null && allowEnlargement)
{
args.add("--allow-enlargement");
}
if (maintainAspectRatio != null && maintainAspectRatio)
{
args.add("--maintain-aspect-ratio");
}
if (page != null && page >= 0)
{
args.add("--page=" + page);
}
return args.toString();
}
} }

View File

@ -0,0 +1,110 @@
package org.alfresco.transformer;
import static org.alfresco.transformer.util.Util.stringToBoolean;
import static org.alfresco.transformer.util.Util.stringToInteger;
import java.util.StringJoiner;
/**
* PdfRenderer options builder.
*
* @author Cezar Leahu
*/
final class OptionsBuilder
{
private Integer page;
private Integer width;
private Integer height;
private Boolean allowPdfEnlargement;
private Boolean maintainPdfAspectRatio;
private OptionsBuilder()
{
}
public OptionsBuilder withPage(final String page)
{
return withPage(stringToInteger(page));
}
public OptionsBuilder withPage(final Integer page)
{
this.page = page;
return this;
}
public OptionsBuilder withWidth(final String width)
{
return withWidth(stringToInteger(width));
}
public OptionsBuilder withWidth(final Integer width)
{
this.width = width;
return this;
}
public OptionsBuilder withHeight(final String height)
{
return withHeight(stringToInteger(height));
}
public OptionsBuilder withHeight(final Integer height)
{
this.height = height;
return this;
}
public OptionsBuilder withAllowPdfEnlargement(final String allowPdfEnlargement)
{
return withAllowPdfEnlargement(stringToBoolean(allowPdfEnlargement));
}
public OptionsBuilder withAllowPdfEnlargement(final Boolean allowPdfEnlargement)
{
this.allowPdfEnlargement = allowPdfEnlargement;
return this;
}
public OptionsBuilder withMaintainPdfAspectRatio(final String maintainPdfAspectRatio)
{
return withMaintainPdfAspectRatio(stringToBoolean(maintainPdfAspectRatio));
}
public OptionsBuilder withMaintainPdfAspectRatio(final Boolean maintainPdfAspectRatio)
{
this.maintainPdfAspectRatio = maintainPdfAspectRatio;
return this;
}
public String build()
{
StringJoiner args = new StringJoiner(" ");
if (width != null && width >= 0)
{
args.add("--width=" + width);
}
if (height != null && height >= 0)
{
args.add("--height=" + height);
}
if (allowPdfEnlargement != null && allowPdfEnlargement)
{
args.add("--allow-enlargement");
}
if (maintainPdfAspectRatio != null && maintainPdfAspectRatio)
{
args.add("--maintain-aspect-ratio");
}
if (page != null && page >= 0)
{
args.add("--page=" + page);
}
return args.toString();
}
public static OptionsBuilder builder()
{
return new OptionsBuilder();
}
}

View File

@ -14,8 +14,8 @@
<tr><td><div style="text-align:right">width</div></td><td><input type="text" name="width" value="" /></td></tr> <tr><td><div style="text-align:right">width</div></td><td><input type="text" name="width" value="" /></td></tr>
<tr><td><div style="text-align:right">height</div></td><td><input type="text" name="height" value="" /></td></tr> <tr><td><div style="text-align:right">height</div></td><td><input type="text" name="height" value="" /></td></tr>
<tr><td><div style="text-align:right">allowEnlargement</div></td><td><input type="checkbox" name="allowEnlargement" value="true" /></td></tr> <tr><td><div style="text-align:right">allowPdfEnlargement</div></td><td><input type="checkbox" name="allowPdfEnlargement" value="true" /></td></tr>
<tr><td><div style="text-align:right">maintainAspectRatio</div></td><td><input type="checkbox" name="maintainAspectRatio" value="true" /></td></tr> <tr><td><div style="text-align:right">maintainPdfAspectRatio</div></td><td><input type="checkbox" name="maintainPdfAspectRatio" value="true" /></td></tr>
<tr><td></td><td><input type="submit" value="Transform" /></td></tr> <tr><td></td><td><input type="submit" value="Transform" /></td></tr>
</table> </table>

View File

@ -52,6 +52,7 @@ import org.alfresco.transformer.executors.PdfRendererCommandExecutor;
import org.alfresco.transformer.model.FileRefEntity; import org.alfresco.transformer.model.FileRefEntity;
import org.alfresco.transformer.model.FileRefResponse; import org.alfresco.transformer.model.FileRefResponse;
import org.alfresco.util.exec.RuntimeExec; import org.alfresco.util.exec.RuntimeExec;
import org.alfresco.util.exec.RuntimeExec.ExecutionResult;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -67,6 +68,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -79,7 +81,7 @@ import org.springframework.util.StringUtils;
public class AlfrescoPdfRendererControllerTest extends AbstractTransformerControllerTest public class AlfrescoPdfRendererControllerTest extends AbstractTransformerControllerTest
{ {
@Mock @Mock
private RuntimeExec.ExecutionResult mockExecutionResult; private ExecutionResult mockExecutionResult;
@Mock @Mock
private RuntimeExec mockTransformCommand; private RuntimeExec mockTransformCommand;
@ -96,8 +98,8 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
@Before @Before
public void before() throws IOException public void before() throws IOException
{ {
commandExecutor.setTransformCommand(mockTransformCommand); ReflectionTestUtils.setField(commandExecutor, "transformCommand", mockTransformCommand);
commandExecutor.setCheckCommand(mockCheckCommand); ReflectionTestUtils.setField(commandExecutor, "checkCommand", mockCheckCommand);
mockTransformCommand("pdf", "png", "application/pdf", true); mockTransformCommand("pdf", "png", "application/pdf", true);
} }
@ -190,8 +192,8 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
.param("width", "321") .param("width", "321")
.param("height", "654") .param("height", "654")
.param("allowEnlargement", "true") .param("allowPdfEnlargement", "true")
.param("maintainAspectRatio", "true")) .param("maintainPdfAspectRatio", "true"))
.andExpect(status().is(OK.value())) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))
@ -210,8 +212,8 @@ public class AlfrescoPdfRendererControllerTest extends AbstractTransformerContro
.param("width", "321") .param("width", "321")
.param("height", "654") .param("height", "654")
.param("allowEnlargement", "false") .param("allowPdfEnlargement", "false")
.param("maintainAspectRatio", "false")) .param("maintainPdfAspectRatio", "false"))
.andExpect(status().is(OK.value())) .andExpect(status().is(OK.value()))
.andExpect(content().bytes(expectedTargetFileBytes)) .andExpect(content().bytes(expectedTargetFileBytes))

View File

@ -15,7 +15,7 @@ import org.alfresco.transformer.exceptions.TransformException;
* *
* @author Cezar Leahu * @author Cezar Leahu
*/ */
public final class OptionsBuilder final class OptionsBuilder
{ {
private static final List<String> GRAVITY_VALUES = asList("North", "NorthEast", "East", private static final List<String> GRAVITY_VALUES = asList("North", "NorthEast", "East",
"SouthEast", "South", "SouthWest", "West", "NorthWest", "Center"); "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center");

View File

@ -67,6 +67,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -96,8 +97,8 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
@Before @Before
public void before() throws IOException public void before() throws IOException
{ {
commandExecutor.setTransformCommand(mockTransformCommand); ReflectionTestUtils.setField(commandExecutor, "transformCommand", mockTransformCommand);
commandExecutor.setCheckCommand(mockCheckCommand); ReflectionTestUtils.setField(commandExecutor, "checkCommand", mockCheckCommand);
mockTransformCommand("jpg", "png", "image/jpg", true); mockTransformCommand("jpg", "png", "image/jpg", true);
} }

View File

@ -28,8 +28,8 @@ src/main/java/org/alfresco/transformer/Application.java
<tr><td><div style="text-align:right">targetFilename *</div></td><td><input type="text" name="targetFilename" value="" /></td></tr> <tr><td><div style="text-align:right">targetFilename *</div></td><td><input type="text" name="targetFilename" value="" /></td></tr>
<tr><td><div style="text-align:right">width</div></td><td><input type="text" name="width" value="" /></td></tr> <tr><td><div style="text-align:right">width</div></td><td><input type="text" name="width" value="" /></td></tr>
<tr><td><div style="text-align:right">height</div></td><td><input type="text" name="height" value="" /></td></tr> <tr><td><div style="text-align:right">height</div></td><td><input type="text" name="height" value="" /></td></tr>
<tr><td><div style="text-align:right">allowEnlargement</div></td><td><input type="checkbox" name="allowEnlargement" value="true" /></td></tr> <tr><td><div style="text-align:right">allowPdfEnlargement</div></td><td><input type="checkbox" name="allowPdfEnlargement" value="true" /></td></tr>
<tr><td><div style="text-align:right">maintainAspectRatio</div></td><td><input type="checkbox" name="maintainAspectRatio" value="true" /></td></tr> <tr><td><div style="text-align:right">maintainPdfAspectRatio</div></td><td><input type="checkbox" name="maintainPdfAspectRatio" value="true" /></td></tr>
<tr><td><div style="text-align:right">page</div></td><td><input type="text" name="page" value="" /></td></tr> <tr><td><div style="text-align:right">page</div></td><td><input type="text" name="page" value="" /></td></tr>
<tr><td><div style="text-align:right">timeout</div></td><td><input type="text" name="timeout" value="" /></td></tr> <tr><td><div style="text-align:right">timeout</div></td><td><input type="text" name="timeout" value="" /></td></tr>
<tr><td></td><td><input type="submit" value="Transform" /></td></tr> <tr><td></td><td><input type="submit" value="Transform" /></td></tr>
@ -59,8 +59,8 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
@RequestParam("targetFilename") String targetFilename, @RequestParam("targetFilename") String targetFilename,
@RequestParam(value = "width", required = false) Integer width, @RequestParam(value = "width", required = false) Integer width,
@RequestParam(value = "height", required = false) Integer height, @RequestParam(value = "height", required = false) Integer height,
@RequestParam(value = "allowEnlargement", required = false) Boolean allowEnlargement, @RequestParam(value = "allowPdfEnlargement", required = false) Boolean allowPdfEnlargement,
@RequestParam(value = "maintainAspectRatio", required = false) Boolean maintainAspectRatio, @RequestParam(value = "maintainPdfAspectRatio", required = false) Boolean maintainPdfAspectRatio,
@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "timeout", required = false) Long timeout) @RequestParam(value = "timeout", required = false) Long timeout)
{ {
@ -79,11 +79,11 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
{ {
args.add("--height=" + height); args.add("--height=" + height);
} }
if (allowEnlargement != null && allowEnlargement) if (allowPdfEnlargement != null && allowPdfEnlargement)
{ {
args.add("--allow-enlargement"); args.add("--allow-enlargement");
} }
if (maintainAspectRatio != null && maintainAspectRatio) if (maintainPdfAspectRatio != null && maintainPdfAspectRatio)
{ {
args.add("--maintain-aspect-ratio"); args.add("--maintain-aspect-ratio");
} }

View File

@ -8,32 +8,24 @@ import java.util.Map;
import org.alfresco.transformer.exceptions.TransformException; import org.alfresco.transformer.exceptions.TransformException;
import org.alfresco.util.exec.RuntimeExec; import org.alfresco.util.exec.RuntimeExec;
import org.alfresco.util.exec.RuntimeExec.ExecutionResult;
/** /**
*/ */
public abstract class AbstractCommandExecutor implements CommandExecutor public abstract class AbstractCommandExecutor implements CommandExecutor
{ {
private RuntimeExec transformCommand = createTransformCommand(); private final RuntimeExec transformCommand = createTransformCommand();
private RuntimeExec checkCommand = createCheckCommand(); private final RuntimeExec checkCommand = createCheckCommand();
protected abstract RuntimeExec createTransformCommand(); protected abstract RuntimeExec createTransformCommand();
protected abstract RuntimeExec createCheckCommand(); protected abstract RuntimeExec createCheckCommand();
// todo remove these setters and and make the fields final
public void setTransformCommand(RuntimeExec re) {
transformCommand = re;
}
public void setCheckCommand(RuntimeExec re) {
checkCommand = re;
}
@Override @Override
public void run(Map<String, String> properties, File targetFile, Long timeout) public void run(Map<String, String> properties, File targetFile, Long timeout)
{ {
timeout = timeout != null && timeout > 0 ? timeout : 0; timeout = timeout != null && timeout > 0 ? timeout : 0;
RuntimeExec.ExecutionResult result = transformCommand.execute(properties, timeout); final ExecutionResult result = transformCommand.execute(properties, timeout);
if (result.getExitValue() != 0 && result.getStdErr() != null && result.getStdErr().length() > 0) if (result.getExitValue() != 0 && result.getStdErr() != null && result.getStdErr().length() > 0)
{ {
@ -51,24 +43,23 @@ public abstract class AbstractCommandExecutor implements CommandExecutor
@Override @Override
public String version() public String version()
{ {
String version = "Version not checked";
if (checkCommand != null) if (checkCommand != null)
{ {
RuntimeExec.ExecutionResult result = checkCommand.execute(); final ExecutionResult result = checkCommand.execute();
if (result.getExitValue() != 0 && result.getStdErr() != null && result.getStdErr().length() > 0) if (result.getExitValue() != 0 && result.getStdErr() != null && result.getStdErr().length() > 0)
{ {
throw new TransformException(INTERNAL_SERVER_ERROR.value(), throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Transformer version check exit code was not 0: \n" + result); "Transformer version check exit code was not 0: \n" + result);
} }
version = result.getStdOut().trim(); final String version = result.getStdOut().trim();
if (version.isEmpty()) if (version.isEmpty())
{ {
throw new TransformException(INTERNAL_SERVER_ERROR.value(), throw new TransformException(INTERNAL_SERVER_ERROR.value(),
"Transformer version check failed to create any output"); "Transformer version check failed to create any output");
} }
return version;
} }
return "Version not checked";
return version;
} }
} }