parameters) throws Exception
diff --git a/alfresco-docker-transform-misc/src/main/resources/engine_config.json b/alfresco-docker-transform-misc/src/main/resources/engine_config.json
index 15d7eac5..5948332f 100644
--- a/alfresco-docker-transform-misc/src/main/resources/engine_config.json
+++ b/alfresco-docker-transform-misc/src/main/resources/engine_config.json
@@ -16,7 +16,7 @@
{
"transformerName": "html",
"supportedSourceAndTargetList": [
- {"sourceMediaType": "text/html", "targetMediaType": "text/plain"}
+ {"sourceMediaType": "text/html", "targetMediaType": "text/plain"}
],
"transformOptions": [
"htmlOptions"
@@ -25,23 +25,22 @@
{
"transformerName": "string",
"supportedSourceAndTargetList": [
- {"sourceMediaType": "text/plain", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/mediawiki", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/css", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/csv", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/javascript", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/xml", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/html", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/richtext", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/sgml", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/tab-separated-values", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/x-setext", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/x-java-source", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/x-jsp", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/x-markdown", "targetMediaType": "text/plain"},
- {"sourceMediaType": "text/calendar", "targetMediaType": "text/plain"},
- {"sourceMediaType": "application/x-javascript", "targetMediaType": "text/plain"},
- {"sourceMediaType": "application/dita+xml", "targetMediaType": "text/plain"}
+ {"sourceMediaType": "text/plain", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/mediawiki", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/css", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/csv", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/xml", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/html", "priority": 55, "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/richtext", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/sgml", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/tab-separated-values", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/x-setext", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/x-java-source", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/x-jsp", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/x-markdown", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "text/calendar", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "application/x-javascript", "targetMediaType": "text/plain"},
+ {"sourceMediaType": "application/dita+xml", "targetMediaType": "text/plain"}
],
"transformOptions": [
"stringOptions"
@@ -50,9 +49,9 @@
{
"transformerName": "appleIWorks",
"supportedSourceAndTargetList": [
- {"sourceMediaType": "application/vnd.apple.keynote", "targetMediaType": "image/jpeg"},
- {"sourceMediaType": "application/vnd.apple.numbers", "targetMediaType": "image/jpeg"},
- {"sourceMediaType": "application/vnd.apple.pages", "targetMediaType": "image/jpeg"}
+ {"sourceMediaType": "application/vnd.apple.keynote", "targetMediaType": "image/jpeg"},
+ {"sourceMediaType": "application/vnd.apple.numbers", "targetMediaType": "image/jpeg"},
+ {"sourceMediaType": "application/vnd.apple.pages", "targetMediaType": "image/jpeg"}
],
"transformOptions": [
]
@@ -60,10 +59,10 @@
{
"transformerName": "textToPdf",
"supportedSourceAndTargetList": [
- {"sourceMediaType": "text/plain", "targetMediaType": "application/pdf"},
- {"sourceMediaType": "text/csv", "targetMediaType": "application/pdf"},
- {"sourceMediaType": "application/dita+xml", "targetMediaType": "application/pdf"},
- {"sourceMediaType": "text/xml", "targetMediaType": "application/pdf"}
+ {"sourceMediaType": "text/plain", "priority": 55, "targetMediaType": "application/pdf"},
+ {"sourceMediaType": "text/csv", "targetMediaType": "application/pdf"},
+ {"sourceMediaType": "application/dita+xml", "targetMediaType": "application/pdf"},
+ {"sourceMediaType": "text/xml", "targetMediaType": "application/pdf"}
],
"transformOptions": [
"textToPdfOptions"
@@ -72,7 +71,7 @@
{
"transformerName": "rfc822",
"supportedSourceAndTargetList": [
- {"sourceMediaType": "message/rfc822", "targetMediaType": "text/plain"}
+ {"sourceMediaType": "message/rfc822", "targetMediaType": "text/plain"}
],
"transformOptions": [
]
diff --git a/alfresco-docker-transform-misc/src/test/java/org/alfresco/transformer/MiscControllerTest.java b/alfresco-docker-transform-misc/src/test/java/org/alfresco/transformer/MiscControllerTest.java
index 7830a804..bc5ed784 100644
--- a/alfresco-docker-transform-misc/src/test/java/org/alfresco/transformer/MiscControllerTest.java
+++ b/alfresco-docker-transform-misc/src/test/java/org/alfresco/transformer/MiscControllerTest.java
@@ -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,11 +108,18 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile,
String... params)
{
- return super.mockMvcRequest(url, sourceFile, params)
- .param("targetEncoding", targetEncoding)
- .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 (!"message/rfc822".equals(sourceMimetype) && !"text/html".equals(sourceMimetype))
+ {
+ builder.param("targetEncoding", targetEncoding);
+ }
+ return builder;
}
@Test
@@ -132,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));
}
/**
@@ -150,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));
}
/**
@@ -167,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));
}
@@ -187,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));
}
/**
@@ -207,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));
}
/**
@@ -225,12 +232,12 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
{
String expected = " ";
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));
}
@@ -254,7 +261,7 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
MIMETYPE_HTML,
"txt",
MIMETYPE_TEXT_PLAIN,
- "UTF-8",
+ null,
expected.getBytes());
String contentResult = new String(result.getResponse().getContentAsByteArray(),
@@ -325,7 +332,7 @@ public class MiscControllerTest extends AbstractTransformerControllerTest
MIMETYPE_TEXT_PLAIN,
"pdf",
MIMETYPE_PDF,
- "UTF-8",
+ null,
expected.getBytes());
// Read back in the PDF and check it
@@ -345,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", "");
diff --git a/alfresco-docker-transform-misc/src/test/java/org/alfresco/transformer/MiscTransformsIT.java b/alfresco-docker-transform-misc/src/test/java/org/alfresco/transformer/MiscTransformsIT.java
index 00d3e383..47d929d0 100644
--- a/alfresco-docker-transform-misc/src/test/java/org/alfresco/transformer/MiscTransformsIT.java
+++ b/alfresco-docker-transform-misc/src/test/java/org/alfresco/transformer/MiscTransformsIT.java
@@ -139,7 +139,6 @@ public class MiscTransformsIT
SourceTarget.of("text/mediawiki", "text/plain"),
SourceTarget.of("text/css", "text/plain"),
SourceTarget.of("text/csv", "text/plain"),
- SourceTarget.of("text/javascript", "text/plain"),
SourceTarget.of("text/xml", "text/plain"),
SourceTarget.of("text/html", "text/plain"),
SourceTarget.of("text/richtext", "text/plain"),
diff --git a/alfresco-transformer-base/src/main/java/org/alfresco/transformer/AbstractTransformerController.java b/alfresco-transformer-base/src/main/java/org/alfresco/transformer/AbstractTransformerController.java
index 8c3b8587..862d50ae 100644
--- a/alfresco-transformer-base/src/main/java/org/alfresco/transformer/AbstractTransformerController.java
+++ b/alfresco-transformer-base/src/main/java/org/alfresco/transformer/AbstractTransformerController.java
@@ -41,13 +41,14 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.util.StringUtils.getFilenameExtension;
import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transform.client.model.TransformRequestValidator;
import org.alfresco.transform.client.model.config.TransformConfig;
+import org.alfresco.transform.client.registry.TransformServiceRegistry;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient;
import org.alfresco.transformer.logging.LogEntry;
@@ -55,7 +56,6 @@ import org.alfresco.transformer.model.FileRefResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@@ -70,8 +70,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.HttpClientErrorException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
/**
* Abstract Controller, provides structure and helper methods to sub-class transformer controllers.
*
@@ -105,8 +103,6 @@ public abstract class AbstractTransformerController implements TransformControll
private static final Logger logger = LoggerFactory.getLogger(
AbstractTransformerController.class);
- private static String ENGINE_CONFIG = "engine_config.json";
-
@Autowired
private AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient;
@@ -114,26 +110,15 @@ public abstract class AbstractTransformerController implements TransformControll
private TransformRequestValidator transformRequestValidator;
@Autowired
- private ObjectMapper objectMapper;
+ private TransformServiceRegistry transformRegistry;
@GetMapping(value = "/transform/config")
public ResponseEntity info()
{
logger.info("GET Transform Config.");
- try
- {
- ClassPathResource classPathResource = new ClassPathResource(ENGINE_CONFIG);
- InputStream engineConfigFile = classPathResource.getInputStream();
-
- TransformConfig transformConfig = objectMapper.readValue(engineConfigFile,
- TransformConfig.class);
- return new ResponseEntity<>(transformConfig, OK);
- }
- catch (IOException e)
- {
- throw new TransformException(INTERNAL_SERVER_ERROR.value(),
- "Could not read Transform Config file.", e);
- }
+ final TransformConfig transformConfig =
+ ((TransformRegistryImpl) transformRegistry).getTransformConfig();
+ return new ResponseEntity<>(transformConfig, OK);
}
/**
@@ -352,4 +337,39 @@ public abstract class AbstractTransformerController implements TransformControll
return sb.toString();
}
+
+ protected String getTransformerName(final File sourceFile, final String sourceMimetype,
+ final String targetMimetype, final Map transformOptions)
+ {
+ 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");
+ }
+ return transformerName;
+ }
+
+ protected Map createTransformOptions(Object... namesAndValues)
+ {
+ if (namesAndValues.length % 2 != 0)
+ {
+ logger.error(
+ "Incorrect number of parameters. Should have an even number as they are names and values.");
+ }
+
+ Map transformOptions = new HashMap<>();
+ for (int i = 0; i < namesAndValues.length; i += 2)
+ {
+ String name = namesAndValues[i].toString();
+ Object value = namesAndValues[i + 1];
+ if (value != null)
+ {
+ transformOptions.put(name, value.toString());
+ }
+ }
+ return transformOptions;
+ }
}
diff --git a/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformRegistryImpl.java b/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformRegistryImpl.java
new file mode 100644
index 00000000..f494685c
--- /dev/null
+++ b/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformRegistryImpl.java
@@ -0,0 +1,97 @@
+/*
+ * #%L
+ * Alfresco Repository
+ * %%
+ * Copyright (C) 2005 - 2018 Alfresco Software Limited
+ * %%
+ * This file is part of the Alfresco software.
+ * If the software was purchased under a paid Alfresco license, the terms of
+ * the paid license agreement will prevail. Otherwise, the software is
+ * provided under the following open source license terms:
+ *
+ * Alfresco is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Alfresco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Alfresco. If not, see .
+ * #L%
+ */
+package org.alfresco.transformer;
+
+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;
+import org.alfresco.transform.exceptions.TransformException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.Resource;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * Used by clients to work out if a transformation is supported based on the engine_config.json.
+ */
+public class TransformRegistryImpl extends AbstractTransformRegistry
+{
+ private static final Logger log = LoggerFactory.getLogger(TransformRegistryImpl.class);
+
+ private static final String ENGINE_CONFIG_JSON = "classpath:engine_config.json";
+
+ @Value(ENGINE_CONFIG_JSON)
+ private Resource engineConfig;
+
+ // Holds the structures used by AbstractTransformRegistry to look up what is supported.
+ // Unlike other sub classes this class does not extend Data or replace it at run time.
+ private TransformCache data = new TransformCache();
+
+ private ObjectMapper jsonObjectMapper = new ObjectMapper();
+
+ TransformConfig getTransformConfig()
+ {
+ try (Reader reader = new InputStreamReader(engineConfig.getInputStream(), UTF_8))
+ {
+ return jsonObjectMapper.readValue(reader, TransformConfig.class);
+ }
+ catch (IOException e)
+ {
+ throw new TransformException(INTERNAL_SERVER_ERROR.value(),
+ "Could not read " + ENGINE_CONFIG_JSON, e);
+ }
+ }
+
+ @PostConstruct
+ public void afterPropertiesSet()
+ {
+ TransformConfig transformConfig = getTransformConfig();
+ registerAll(transformConfig, null, ENGINE_CONFIG_JSON);
+ }
+
+ @Override
+ public TransformCache getData()
+ {
+ return data;
+ }
+
+ @Override
+ protected void logError(String msg)
+ {
+ log.error(msg);
+ }
+}
diff --git a/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/WebApplicationConfig.java b/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/WebApplicationConfig.java
index 1d6f7ab1..d8dad96c 100644
--- a/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/WebApplicationConfig.java
+++ b/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/WebApplicationConfig.java
@@ -27,7 +27,9 @@
package org.alfresco.transformer.config;
import org.alfresco.transform.client.model.TransformRequestValidator;
+import org.alfresco.transform.client.registry.TransformServiceRegistry;
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;
@@ -42,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
@@ -69,4 +72,10 @@ public class WebApplicationConfig implements WebMvcConfigurer
{
return new TransformRequestValidator();
}
+
+ @Bean
+ public TransformServiceRegistry transformRegistry()
+ {
+ return new TransformRegistryImpl();
+ }
}
diff --git a/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/Util.java b/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/Util.java
index 61631770..2f24b1f0 100644
--- a/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/Util.java
+++ b/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/Util.java
@@ -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);
}
diff --git a/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractHttpRequestTest.java b/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractHttpRequestTest.java
index c9d88a00..74f4d58b 100644
--- a/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractHttpRequestTest.java
+++ b/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractHttpRequestTest.java
@@ -95,7 +95,7 @@ public abstract class AbstractHttpRequestTest
}
@Test
- public void noTargetExtensionError() throws Exception
+ public void noTargetExtensionError()
{
assertMissingParameter("targetExtension");
}
@@ -106,7 +106,7 @@ public abstract class AbstractHttpRequestTest
getTransformerName() + " - Request parameter '" + name + "' is missing");
}
- private void assertTransformError(boolean addFile, String errorMessage)
+ protected void assertTransformError(boolean addFile, String errorMessage)
{
LinkedMultiValueMap parameters = new LinkedMultiValueMap<>();
if (addFile)
@@ -118,7 +118,14 @@ public abstract class AbstractHttpRequestTest
headers.setContentType(MULTIPART_FORM_DATA);
HttpEntity> entity = new HttpEntity<>(parameters,
headers);
- ResponseEntity response = restTemplate.exchange("/transform", POST, entity,
+
+ sendTranformationRequest(entity, errorMessage);
+ }
+
+ protected void sendTranformationRequest(
+ final HttpEntity> entity, final String errorMessage)
+ {
+ final ResponseEntity response = restTemplate.exchange("/transform", POST, entity,
String.class, "");
assertEquals(errorMessage, getErrorMessage(response.getBody()));
}
diff --git a/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractTransformerControllerTest.java b/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractTransformerControllerTest.java
index c59fb765..47eb8c6e 100644
--- a/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractTransformerControllerTest.java
+++ b/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractTransformerControllerTest.java
@@ -60,11 +60,13 @@ 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.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;
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.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;
@@ -91,6 +93,9 @@ public abstract class AbstractTransformerControllerTest
@MockBean
protected AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient;
+ @SpyBean
+ protected TransformServiceRegistry transformRegistry;
+
protected String sourceExtension;
protected String targetExtension;
protected String sourceMimetype;
@@ -100,6 +105,13 @@ public abstract class AbstractTransformerControllerTest
protected String expectedSourceSuffix;
protected Long expectedTimeout = 0L;
protected byte[] expectedSourceFileBytes;
+
+ /**
+ * 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.
+ */
protected byte[] expectedTargetFileBytes;
// Called by sub class
@@ -228,7 +240,7 @@ public abstract class AbstractTransformerControllerTest
}
@Test
- // Is okay, as the target filename is built up from the whole source filename and the targetExtenstion
+ // Is okay, as the target filename is built up from the whole source filename and the targetExtension
public void noExtensionSourceFilenameTest() throws Exception
{
sourceFile = new MockMultipartFile("file", "../quick", sourceMimetype,
@@ -334,8 +346,8 @@ public abstract class AbstractTransformerControllerTest
.readValue(new ClassPathResource("engine_config.json").getFile(),
TransformConfig.class);
- ReflectionTestUtils
- .setField(AbstractTransformerController.class, "ENGINE_CONFIG", "engine_config.json");
+ ReflectionTestUtils.setField(transformRegistry, "engineConfig",
+ new ClassPathResource("engine_config.json"));
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
@@ -353,8 +365,8 @@ public abstract class AbstractTransformerControllerTest
{
TransformConfig expectedResult = buildCompleteTransformConfig();
- ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
- "engine_config_with_duplicates.json");
+ ReflectionTestUtils.setField(transformRegistry, "engineConfig",
+ new ClassPathResource("engine_config_with_duplicates.json"));
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
@@ -380,8 +392,8 @@ public abstract class AbstractTransformerControllerTest
TransformConfig expectedResult = new TransformConfig();
expectedResult.setTransformers(ImmutableList.of(transformer));
- ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
- "engine_config_incomplete.json");
+ ReflectionTestUtils.setField(transformRegistry, "engineConfig",
+ new ClassPathResource("engine_config_incomplete.json"));
String response = mockMvc
.perform(MockMvcRequestBuilders.get("/transform/config"))
@@ -403,8 +415,8 @@ public abstract class AbstractTransformerControllerTest
TransformConfig expectedResult = new TransformConfig();
expectedResult.setTransformers(ImmutableList.of(transformer));
- ReflectionTestUtils.setField(AbstractTransformerController.class, "ENGINE_CONFIG",
- "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"))
diff --git a/pom.xml b/pom.xml
index b97e6626..cad7bd5d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
2.0.16
3.0.1.1
${project.version}
- 1.0.2.6
+ 1.0.2.8-SNAPSHOT
5.15.9
2.9.9
2.9.9.3
@@ -152,7 +152,7 @@
org.apache.commons
commons-compress
- 1.18
+ 1.19
@@ -160,6 +160,12 @@
jsoup
1.12.1
+
+
+ commons-codec
+ commons-codec
+ 1.13
+