diff --git a/docs/external-engine-configuration.md b/docs/external-engine-configuration.md index 606c787f..4521dd45 100644 --- a/docs/external-engine-configuration.md +++ b/docs/external-engine-configuration.md @@ -40,6 +40,7 @@ The following externalized T-engines properties are available: | FILE_STORE_URL | T-Engine Port. | http://localhost:8099/alfresco/api/-default-/private/sfs/versions/1/file | | TRANSFORM_ENGINE_REQUEST_QUEUE | T-Engine queue used for async requests. | org.alfresco.transform.engine.misc.acs | | MISC_PDFBOX_DEFAULT_FONT | Default font used by PdfBox | NotoSans-Regular | +| MISC_HTML_COLLAPSE | Html Collasping Option for HTML to TXT transformation | true | ## Libreoffice | Property | Description | Default value | @@ -98,4 +99,5 @@ The following externalized T-engines properties are available: | IMAGEMAGICK_EXE | Path to Imagemagick EXE. | /usr/bin/convert | | IMAGEMAGICK_CODERS | Path to Imagemagick custom coders. | | | IMAGEMAGICK_CONFIG | Path to Imagemagick custom config. | | -| MISC_PDFBOX_DEFAULT_FONT | Default font used by PdfBox | NotoSans-Regular | \ No newline at end of file +| MISC_PDFBOX_DEFAULT_FONT | Default font used by PdfBox | NotoSans-Regular | +| MISC_HTML_COLLAPSE | Html Collasping Option for HTML to TXT transformation explicitly for Misc Engine | true | \ No newline at end of file diff --git a/engines/aio/src/main/resources/application-default.yaml b/engines/aio/src/main/resources/application-default.yaml index e6b06b77..27ba6837 100644 --- a/engines/aio/src/main/resources/application-default.yaml +++ b/engines/aio/src/main/resources/application-default.yaml @@ -26,4 +26,6 @@ transform: unixOS: 'env FOO=#{"$"}{OUTPUT} exiftool -args -G1 -sep "|||" #{"$"}{INPUT}' misc: pdfBox: - defaultFont: ${MISC_PDFBOX_DEFAULT_FONT:NotoSans-Regular} \ No newline at end of file + defaultFont: ${MISC_PDFBOX_DEFAULT_FONT:NotoSans-Regular} + htmlOptions: + collapseHtml: ${MISC_HTML_COLLAPSE:true} \ No newline at end of file diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTest.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTest.java index ce08d9bd..ca3d6d3f 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTest.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTest.java @@ -1,126 +1,128 @@ -/* - * #%L - * Alfresco Transform Core - * %% - * Copyright (C) 2005 - 2022 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.transform.aio; - -import org.alfresco.transform.base.AbstractBaseTest; -import org.alfresco.transform.base.TransformController; -import org.alfresco.transform.config.TransformConfig; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; - -import java.nio.file.Files; -import java.util.StringJoiner; - -import static org.alfresco.transform.base.TransformControllerTest.getLogMessagesFor; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; -import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION_DEFAULT; -import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION_LATEST; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; - -/** - * Test All-In-One. - */ -public class AIOTest extends AbstractBaseTest -{ - @Autowired - private String coreVersion; - - @BeforeEach - public void before() throws Exception - { - sourceMimetype = MIMETYPE_HTML; - targetMimetype = MIMETYPE_TEXT_PLAIN; - sourceExtension = "html"; - targetExtension = "txt"; - expectedOptions = null; - expectedSourceSuffix = null; - sourceFileBytes = readTestFile(sourceExtension); - expectedTargetFileBytes = Files.readAllBytes(getTestFile("quick2." + targetExtension, true).toPath()); - sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, sourceFileBytes); - } - - @Override - // Add extra required parameters to the request. - protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params) - { - return super.mockMvcRequest(url, sourceFile, params) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype); - } - - @Test - public void coreVersionNotSetInOriginalConfigTest() - { - ResponseEntity responseEntity = controller.transformConfig(Integer.valueOf(CONFIG_VERSION_DEFAULT)); - responseEntity.getBody().getTransformers().forEach(transformer -> { - assertNull(transformer.getCoreVersion(), transformer.getTransformerName() + - " should have had a null coreValue but was " + transformer.getCoreVersion()); - }); - } - - @Test - public void coreVersionSetInLatestConfigTest() - { - ResponseEntity responseEntity = controller.transformConfig(CONFIG_VERSION_LATEST); - responseEntity.getBody().getTransformers().forEach(transformer -> { - assertNotNull(transformer.getCoreVersion(), transformer.getTransformerName() + - " should have had a coreValue but was null. Should have been " + coreVersion); - }); - } - - @Test - public void testStartupLogsIncludeEngineMessages() - { - StringJoiner controllerLogMessages = getLogMessagesFor(TransformController.class); - - controller.startup(); - - assertEquals( - "--------------------------------------------------------------------------------------------------------------------------------------------------------------\n" - + "If the Alfresco software was purchased under a paid Alfresco license, the terms of the paid license agreement \n" - + "will prevail. Otherwise, the software is provided under terms of the GNU LGPL v3 license. \n" - + "See the license at http://www.gnu.org/licenses/lgpl-3.0.txt. or in /LICENSE.txt \n" - + "\n" - + "This transformer uses ImageMagick from ImageMagick Studio LLC. See the license at http://www.imagemagick.org/script/license.php or in /ImageMagick-license.txt\n" - + "This transformer uses LibreOffice from The Document Foundation. See the license at https://www.libreoffice.org/download/license/ or in /libreoffice.txt\n" - + "This transformer uses libraries from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\\\ 2.0.txt\n" - + "This transformer uses htmlparser. See the license at http://htmlparser.sourceforge.net/license.html\n" - + "This transformer uses alfresco-pdf-renderer which uses the PDFium library from Google Inc. See the license at https://pdfium.googlesource.com/pdfium/+/master/LICENSE or in /pdfium.txt\n" - + "This transformer uses Tika from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\ 2.0.txt\n" - + "This transformer uses ExifTool by Phil Harvey. See license at https://exiftool.org/#license. or in /Perl-Artistic-License.txt\n" - + "--------------------------------------------------------------------------------------------------------------------------------------------------------------\n" - + "Starting application components... Done", - controllerLogMessages.toString()); - } -} \ No newline at end of file +/* + * #%L + * Alfresco Transform Core + * %% + * Copyright (C) 2005 - 2025 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.transform.aio; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import static org.alfresco.transform.base.TransformControllerTest.getLogMessagesFor; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +import static org.alfresco.transform.common.RequestParamMap.*; + +import java.nio.file.Files; +import java.util.StringJoiner; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; + +import org.alfresco.transform.base.AbstractBaseTest; +import org.alfresco.transform.base.TransformController; +import org.alfresco.transform.config.TransformConfig; + +/** + * Test All-In-One + */ +public class AIOTest extends AbstractBaseTest +{ + @Autowired + private String coreVersion; + + @BeforeEach + public void before() throws Exception + { + sourceMimetype = MIMETYPE_HTML; + targetMimetype = MIMETYPE_TEXT_PLAIN; + sourceExtension = "html"; + targetExtension = "txt"; + expectedOptions = null; + expectedSourceSuffix = null; + sourceFileBytes = readTestFile(sourceExtension); + expectedTargetFileBytes = Files.readAllBytes(getTestFile("quick3." + targetExtension, true).toPath()); + sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, sourceFileBytes); + } + + @Override + // Add extra required parameters to the request. + protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params) + { + return super.mockMvcRequest(url, sourceFile, params) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype) + .param(HTML_COLLAPSE, "true"); + } + + @Test + public void coreVersionNotSetInOriginalConfigTest() + { + ResponseEntity responseEntity = controller.transformConfig(Integer.valueOf(CONFIG_VERSION_DEFAULT)); + responseEntity.getBody().getTransformers().forEach(transformer -> { + assertNull(transformer.getCoreVersion(), transformer.getTransformerName() + + " should have had a null coreValue but was " + transformer.getCoreVersion()); + }); + } + + @Test + public void coreVersionSetInLatestConfigTest() + { + ResponseEntity responseEntity = controller.transformConfig(CONFIG_VERSION_LATEST); + responseEntity.getBody().getTransformers().forEach(transformer -> { + assertNotNull(transformer.getCoreVersion(), transformer.getTransformerName() + + " should have had a coreValue but was null. Should have been " + coreVersion); + }); + } + + @Test + public void testStartupLogsIncludeEngineMessages() + { + StringJoiner controllerLogMessages = getLogMessagesFor(TransformController.class); + + controller.startup(); + + assertEquals( + "--------------------------------------------------------------------------------------------------------------------------------------------------------------\n" + + "If the Alfresco software was purchased under a paid Alfresco license, the terms of the paid license agreement \n" + + "will prevail. Otherwise, the software is provided under terms of the GNU LGPL v3 license. \n" + + "See the license at http://www.gnu.org/licenses/lgpl-3.0.txt. or in /LICENSE.txt \n" + + "\n" + + "This transformer uses ImageMagick from ImageMagick Studio LLC. See the license at http://www.imagemagick.org/script/license.php or in /ImageMagick-license.txt\n" + + "This transformer uses LibreOffice from The Document Foundation. See the license at https://www.libreoffice.org/download/license/ or in /libreoffice.txt\n" + + "This transformer uses libraries from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\\\ 2.0.txt\n" + + "This transformer uses htmlparser. See the license at http://htmlparser.sourceforge.net/license.html\n" + + "This transformer uses alfresco-pdf-renderer which uses the PDFium library from Google Inc. See the license at https://pdfium.googlesource.com/pdfium/+/master/LICENSE or in /pdfium.txt\n" + + "This transformer uses Tika from Apache. See the license at http://www.apache.org/licenses/LICENSE-2.0. or in /Apache\\ 2.0.txt\n" + + "This transformer uses ExifTool by Phil Harvey. See license at https://exiftool.org/#license. or in /Perl-Artistic-License.txt\n" + + "--------------------------------------------------------------------------------------------------------------------------------------------------------------\n" + + "Starting application components... Done", + controllerLogMessages.toString()); + } +} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaTest.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaTest.java index 40b3068e..696339c4 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaTest.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaTest.java @@ -1,80 +1,82 @@ -/* - * #%L - * Alfresco Transform Core - * %% - * Copyright (C) 2005 - 2022 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.transform.aio; - -import com.google.common.collect.ImmutableSet; -import org.alfresco.transform.tika.TikaTest; -import org.junit.jupiter.api.Test; - -import static org.alfresco.transform.base.html.OptionsHelper.getOptionNames; -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * Test Tika functionality in All-In-One. - */ -public class AIOTikaTest extends TikaTest -{ - @Test - public void optionListTest() - { - assertEquals(ImmutableSet.of( - "allowEnlargement", - "allowPdfEnlargement", - "alphaRemove", - "autoOrient", - "commandOptions", - "cropGravity", - "cropHeight", - "cropPercentage", - "cropWidth", - "cropXOffset", - "cropYOffset", - "endPage", - "extractMapping", - "height", - "includeContents", - "maintainAspectRatio", - "maintainPdfAspectRatio", - "metadata", - "notExtractBookmarksText", - "page", - "pageLimit", - "pdfFormat", - "pdfOrientation", - "resizeHeight", - "resizePercentage", - "resizeWidth", - "startPage", - "targetEncoding", - "thumbnail", - "width", - "pdfFont", - "pdfFontSize" - ), - getOptionNames(controller.transformConfig(0).getBody().getTransformOptions())); - } -} \ No newline at end of file +/* + * #%L + * Alfresco Transform Core + * %% + * Copyright (C) 2005 - 2025 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.transform.aio; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import static org.alfresco.transform.base.html.OptionsHelper.getOptionNames; + +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.Test; + +import org.alfresco.transform.tika.TikaTest; + +/** + * Test Tika functionality in All-In-One. + */ +public class AIOTikaTest extends TikaTest +{ + @Test + public void optionListTest() + { + assertEquals(ImmutableSet.of( + "allowEnlargement", + "allowPdfEnlargement", + "alphaRemove", + "autoOrient", + "commandOptions", + "cropGravity", + "cropHeight", + "cropPercentage", + "cropWidth", + "cropXOffset", + "cropYOffset", + "endPage", + "extractMapping", + "height", + "includeContents", + "maintainAspectRatio", + "maintainPdfAspectRatio", + "metadata", + "notExtractBookmarksText", + "page", + "pageLimit", + "pdfFormat", + "pdfOrientation", + "resizeHeight", + "resizePercentage", + "resizeWidth", + "startPage", + "targetEncoding", + "thumbnail", + "width", + "pdfFont", + "pdfFontSize", + "collapseHtml"), + getOptionNames(controller.transformConfig(0).getBody().getTransformOptions())); + } +} diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/MiscTransformEngine.java b/engines/misc/src/main/java/org/alfresco/transform/misc/MiscTransformEngine.java index 6fc0276a..edc50900 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/MiscTransformEngine.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/MiscTransformEngine.java @@ -2,7 +2,7 @@ * #%L * Alfresco Transform Core * %% - * Copyright (C) 2005 - 2022 Alfresco Software Limited + * Copyright (C) 2005 - 2025 Alfresco Software Limited * %% * This file is part of the Alfresco software. * - @@ -26,21 +26,22 @@ */ package org.alfresco.transform.misc; -import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.base.TransformEngine; -import org.alfresco.transform.base.probes.ProbeTransform; -import org.alfresco.transform.config.reader.TransformConfigResourceReader; -import org.alfresco.transform.config.TransformConfig; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.Map; - import static org.alfresco.transform.base.logging.StandardMessages.COMMUNITY_LICENCE; import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; +import java.util.Map; + +import com.google.common.collect.ImmutableMap; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.TransformEngine; +import org.alfresco.transform.base.probes.ProbeTransform; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.reader.TransformConfigResourceReader; + @Component public class MiscTransformEngine implements TransformEngine { @@ -74,6 +75,6 @@ public class MiscTransformEngine implements TransformEngine public ProbeTransform getProbeTransform() { return new ProbeTransform("probe.html", MIMETYPE_HTML, MIMETYPE_TEXT_PLAIN, transformOptions, - 119, 30, 150, 1024, 60 * 2 + 1, 60 * 2); + 107, 30, 150, 1024, 60 * 2 + 1, 60 * 2); } } diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/HtmlParserContentTransformer.java b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/HtmlParserContentTransformer.java index 46bee139..e49b9255 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/HtmlParserContentTransformer.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/HtmlParserContentTransformer.java @@ -1,203 +1,215 @@ -/* - * #%L - * Alfresco Transform Core - * %% - * Copyright (C) 2005 - 2022 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.transform.misc.transformers; - -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; -import org.htmlparser.Parser; -import org.htmlparser.beans.StringBean; -import org.htmlparser.util.ParserException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.net.URLConnection; -import java.nio.charset.Charset; -import java.nio.charset.IllegalCharsetNameException; -import java.util.Map; - -import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; - -/** - * Content transformer which wraps the HTML Parser library for - * parsing HTML content. - * - *

- * This code is based on a class of the same name originally implemented in alfresco-repository. - *

- * - *

- * Since HTML Parser was updated from v1.6 to v2.1, META tags - * defining an encoding for the content via http-equiv=Content-Type - * will ONLY be respected if the encoding of the content item - * itself is set to ISO-8859-1. - *

- * - *

- * Tika Note - could be converted to use the Tika HTML parser, - * but we'd potentially need a custom text handler to replicate - * the current settings around links and non-breaking spaces. - *

- * - * @author Derek Hulley - * @author eknizat - * @see http://htmlparser.sourceforge.net - * @see org.htmlparser.beans.StringBean - * @see HTML Parser - */ -@Component -public class HtmlParserContentTransformer implements CustomTransformerFileAdaptor -{ - private static final Logger logger = LoggerFactory.getLogger( - HtmlParserContentTransformer.class); - - @Override - public String getTransformerName() - { - return "html"; - } - - @Override - public void transform(final String sourceMimetype, final String targetMimetype, - final Map transformOptions, - final File sourceFile, final File targetFile, TransformManager transformManager) throws Exception - { - String sourceEncoding = transformOptions.get(SOURCE_ENCODING); - checkEncodingParameter(sourceEncoding, SOURCE_ENCODING); - - if (logger.isDebugEnabled()) - { - logger.debug("Performing HTML to text transform with sourceEncoding=" + sourceEncoding); - } - - // Create the extractor - EncodingAwareStringBean extractor = new EncodingAwareStringBean(); - extractor.setCollapse(false); - extractor.setLinks(false); - extractor.setReplaceNonBreakingSpaces(false); - extractor.setURL(sourceFile, sourceEncoding); - // get the text - String text = extractor.getStrings(); - - // write it to the writer - try (Writer writer = new BufferedWriter( - new OutputStreamWriter(new FileOutputStream(targetFile)))) - { - writer.write(text); - } - } - - private void checkEncodingParameter(String encoding, String parameterName) - { - try - { - if (encoding != null && !Charset.isSupported(encoding)) - { - throw new IllegalArgumentException( - parameterName + "=" + encoding + " is not supported by the JVM."); - } - } - catch (IllegalCharsetNameException e) - { - throw new IllegalArgumentException( - parameterName + "=" + encoding + " is not a valid encoding."); - } - } - - /** - *

- * This code is based on a class of the same name, originally implemented in alfresco-repository. - *

- * - * A version of {@link StringBean} which allows control of the - * encoding in the underlying HTML Parser. - * Unfortunately, StringBean doesn't allow easy over-riding of - * this, so we have to duplicate some code to control this. - * This allows us to correctly handle HTML files where the encoding - * is specified against the content property (rather than in the - * HTML Head Meta), see ALF-10466 for details. - */ - public static class EncodingAwareStringBean extends StringBean - { - private static final long serialVersionUID = -9033414360428669553L; - - /** - * Sets the File to extract strings from, and the encoding - * it's in (if known to Alfresco) - * - * @param file The File that text should be fetched from. - * @param encoding The encoding of the input - */ - public void setURL(File file, String encoding) - { - String previousURL = getURL(); - String newURL = file.getAbsolutePath(); - - if (previousURL == null || !newURL.equals(previousURL)) - { - try - { - URLConnection conn = getConnection(); - - if (null == mParser) - { - mParser = new Parser(newURL); - } - else - { - mParser.setURL(newURL); - } - - if (encoding != null) - { - mParser.setEncoding(encoding); - } - - mPropertySupport.firePropertyChange(StringBean.PROP_URL_PROPERTY, previousURL, - getURL()); - mPropertySupport.firePropertyChange(StringBean.PROP_CONNECTION_PROPERTY, conn, - mParser.getConnection()); - setStrings(); - } - catch (ParserException pe) - { - updateStrings(pe.toString()); - } - } - } - - public String getEncoding() - { - return mParser.getEncoding(); - } - } -} +/* + * #%L + * Alfresco Transform Core + * %% + * Copyright (C) 2005 - 2025 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.transform.misc.transformers; + +import static org.alfresco.transform.common.RequestParamMap.HTML_COLLAPSE; +import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.URLConnection; +import java.nio.charset.Charset; +import java.nio.charset.IllegalCharsetNameException; +import java.util.Map; + +import org.htmlparser.Parser; +import org.htmlparser.beans.StringBean; +import org.htmlparser.util.ParserException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; + +/** + * Content transformer which wraps the HTML Parser library for parsing HTML content. + * + *

+ * This code is based on a class of the same name originally implemented in alfresco-repository. + *

+ * + *

+ * Since HTML Parser was updated from v1.6 to v2.1, META tags defining an encoding for the content via http-equiv=Content-Type will ONLY be respected if the encoding of the content item itself is set to ISO-8859-1. + *

+ * + *

+ * Tika Note - could be converted to use the Tika HTML parser, but we'd potentially need a custom text handler to replicate the current settings around links and non-breaking spaces. + *

+ * + * @author Derek Hulley + * @author eknizat + * @see http://htmlparser.sourceforge.net + * @see org.htmlparser.beans.StringBean + * @see HTML Parser + */ +@Component +public class HtmlParserContentTransformer implements CustomTransformerFileAdaptor +{ + private static final Logger logger = LoggerFactory.getLogger( + HtmlParserContentTransformer.class); + + @Value("${transform.core.misc.htmlOptions.collapseHtml:true}") + private String collapseOptionDefault; + + @Override + public String getTransformerName() + { + return "html"; + } + + @Override + public void transform(final String sourceMimetype, final String targetMimetype, + final Map transformOptions, + final File sourceFile, final File targetFile, TransformManager transformManager) throws Exception + { + String sourceEncoding = transformOptions.get(SOURCE_ENCODING); + checkEncodingParameter(sourceEncoding, SOURCE_ENCODING); + boolean collapse; + + var collapseOption = transformOptions.get(HTML_COLLAPSE); + // If the collapse option is set, use it, otherwise use the default value + if (collapseOption != null && (collapseOption.trim().equalsIgnoreCase("true") || collapseOption.trim().equalsIgnoreCase("false"))) + { + collapse = Boolean.parseBoolean(collapseOption); + } + else + { + // Use the default value from the configuration + collapse = collapseOptionDefault == null || Boolean.parseBoolean(collapseOptionDefault); + if (logger.isDebugEnabled()) + { + logger.debug("Using default html collapse option: " + collapseOptionDefault); + } + } + + if (logger.isDebugEnabled()) + { + logger.debug("Performing HTML to text transform with sourceEncoding=" + sourceEncoding); + } + + // Create the extractor + EncodingAwareStringBean extractor = new EncodingAwareStringBean(); + extractor.setCollapse(collapse); + extractor.setLinks(false); + extractor.setReplaceNonBreakingSpaces(false); + extractor.setURL(sourceFile, sourceEncoding); + // get the text + String text = extractor.getStrings(); + + // write it to the writer + try (Writer writer = new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(targetFile)))) + { + writer.write(text); + } + } + + private void checkEncodingParameter(String encoding, String parameterName) + { + try + { + if (encoding != null && !Charset.isSupported(encoding)) + { + throw new IllegalArgumentException( + parameterName + "=" + encoding + " is not supported by the JVM."); + } + } + catch (IllegalCharsetNameException e) + { + throw new IllegalArgumentException( + parameterName + "=" + encoding + " is not a valid encoding."); + } + } + + /** + *

+ * This code is based on a class of the same name, originally implemented in alfresco-repository. + *

+ * + * A version of {@link StringBean} which allows control of the encoding in the underlying HTML Parser. Unfortunately, StringBean doesn't allow easy over-riding of this, so we have to duplicate some code to control this. This allows us to correctly handle HTML files where the encoding is specified against the content property (rather than in the HTML Head Meta), see ALF-10466 for details. + */ + public static class EncodingAwareStringBean extends StringBean + { + private static final long serialVersionUID = -9033414360428669553L; + + /** + * Sets the File to extract strings from, and the encoding it's in (if known to Alfresco) + * + * @param file + * The File that text should be fetched from. + * @param encoding + * The encoding of the input + */ + public void setURL(File file, String encoding) + { + String previousURL = getURL(); + String newURL = file.getAbsolutePath(); + + if (previousURL == null || !newURL.equals(previousURL)) + { + try + { + URLConnection conn = getConnection(); + + if (null == mParser) + { + mParser = new Parser(newURL); + } + else + { + mParser.setURL(newURL); + } + + if (encoding != null) + { + mParser.setEncoding(encoding); + } + + mPropertySupport.firePropertyChange(StringBean.PROP_URL_PROPERTY, previousURL, + getURL()); + mPropertySupport.firePropertyChange(StringBean.PROP_CONNECTION_PROPERTY, conn, + mParser.getConnection()); + setStrings(); + } + catch (ParserException pe) + { + updateStrings(pe.toString()); + } + } + } + + public String getEncoding() + { + return mParser.getEncoding(); + } + } +} diff --git a/engines/misc/src/main/resources/application-default.yaml b/engines/misc/src/main/resources/application-default.yaml index 54cc2404..6366402b 100644 --- a/engines/misc/src/main/resources/application-default.yaml +++ b/engines/misc/src/main/resources/application-default.yaml @@ -4,4 +4,6 @@ transform: core: misc: pdfBox: - defaultFont: ${MISC_PDFBOX_DEFAULT_FONT:NotoSans-Regular} \ No newline at end of file + defaultFont: ${MISC_PDFBOX_DEFAULT_FONT:NotoSans-Regular} + htmlOptions: + collapseHtml: ${MISC_HTML_COLLAPSE:true} diff --git a/engines/misc/src/main/resources/misc_engine_config.json b/engines/misc/src/main/resources/misc_engine_config.json index 0dbd327f..5b6ddd28 100644 --- a/engines/misc/src/main/resources/misc_engine_config.json +++ b/engines/misc/src/main/resources/misc_engine_config.json @@ -1,5 +1,8 @@ { "transformOptions": { + "htmlOptions": [ + {"value": {"name": "collapseHtml"}} + ], "textToPdfOptions": [ {"value": {"name": "pageLimit"}}, {"value": {"name": "pdfFont"}}, @@ -24,8 +27,7 @@ "supportedSourceAndTargetList": [ {"sourceMediaType": "text/html", "targetMediaType": "text/plain"} ], - "transformOptions": [ - ] + "transformOptions": ["htmlOptions"] }, { "transformerName": "string", diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTest.java b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTest.java index 777f0d4a..37dc0db5 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTest.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTest.java @@ -1,501 +1,501 @@ -/* - * #%L - * Alfresco Transform Core - * %% - * Copyright (C) 2005 - 2022 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.transform.misc; - -import org.alfresco.transform.base.AbstractBaseTest; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.text.PDFTextStripper; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; - -import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.nio.file.Files; - -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IWORK_KEYNOTE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IWORK_NUMBERS; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORDPROCESSING; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_RFC822; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; -import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE; -import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -/** - * Test Misc. Includes calling the 3rd party libraries. - */ -public class MiscTest extends AbstractBaseTest -{ - protected final String sourceEncoding = "UTF-8"; - protected final String targetEncoding = "UTF-8"; - protected final String targetMimetype = MIMETYPE_TEXT_PLAIN; - - @BeforeEach - public void before() throws Exception - { - sourceMimetype = MIMETYPE_HTML; - sourceExtension = "html"; - targetExtension = "txt"; - expectedOptions = null; - expectedSourceSuffix = null; - sourceFileBytes = readTestFile(sourceExtension); - expectedTargetFileBytes = Files.readAllBytes(getTestFile("quick2." + targetExtension, true).toPath()); - sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, sourceFileBytes); - } - - @Override - protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params) - { - 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 transforming a valid eml file to text - */ - @Test - public void testRFC822ToText() throws Exception - { - String expected = "Gym class featuring a brown fox and lazy dog"; - MvcResult result = sendRequest("eml", - null, - MIMETYPE_RFC822, - "txt", - MIMETYPE_TEXT_PLAIN, - null, - null, - null, - readTestFile("eml")); - assertTrue(result.getResponse().getContentAsString().contains(expected), - "Content from eml transform didn't contain expected value. "); - } - - /** - * Test transforming a non-ascii eml file to text - */ - @Test - public void testNonAsciiRFC822ToText() throws Exception - { - 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, - null, - null, readTestFile("spanish.eml")); - - String contentResult = new String(result.getResponse().getContentAsByteArray(), UTF_8); - assertTrue(contentResult.contains(expected), - "Content from eml transform didn't contain expected value. "); - } - - /** - * Test transforming a valid eml with an attachment to text; attachment should be ignored - */ - @Test - public void testRFC822WithAttachmentToText() throws Exception - { - String expected = "Mail with attachment content"; - String notExpected = "File attachment content"; - MvcResult result = sendRequest("eml", - null, - MIMETYPE_RFC822, - "txt", - MIMETYPE_TEXT_PLAIN, - null, - null, - null, - readTestFile("attachment.eml")); - assertTrue(result.getResponse().getContentAsString().contains(expected), - "Content from eml transform didn't contain expected value. "); - assertFalse(result.getResponse().getContentAsString().contains(notExpected)); - } - - /** - * Test transforming a valid eml with minetype multipart/alternative to text - */ - @Test - public void testRFC822AlternativeToText() throws Exception - { - String expected = "alternative plain text"; - MvcResult result = sendRequest("eml", - null, - MIMETYPE_RFC822, - "txt", - MIMETYPE_TEXT_PLAIN, - null, - null, - null, - readTestFile("alternative.eml")); - assertTrue(result.getResponse().getContentAsString().contains(expected), - "Content from eml transform didn't contain expected value. "); - } - - /** - * Test transforming a valid eml with nested mimetype multipart/alternative to text - */ - @Test - public void testRFC822NestedAlternativeToText() throws Exception - { - String expected = "nested alternative plain text"; - MvcResult result = sendRequest("eml", - null, - MIMETYPE_RFC822, - "txt", - MIMETYPE_TEXT_PLAIN, - null, - null, - null, - readTestFile("nested.alternative.eml")); - assertTrue(result.getResponse().getContentAsString().contains(expected), - "Content from eml transform didn't contain expected value. "); - } - - /** - * Test extracting default metadata from a valid eml file - */ - @Test - public void testExtractMetadataRFC822() throws Exception - { - String expected = - "{"+ - "\"{http://www.alfresco.org/model/content/1.0}addressee\":\"Nevin Nollop \","+ - "\"{http://www.alfresco.org/model/content/1.0}addressees\":\"Nevin Nollop \","+ - "\"{http://www.alfresco.org/model/content/1.0}description\":\"The quick brown fox jumps over the lazy dog\","+ - "\"{http://www.alfresco.org/model/content/1.0}originator\":\"Nevin Nollop \","+ - "\"{http://www.alfresco.org/model/content/1.0}sentdate\":1086351802000,"+ - "\"{http://www.alfresco.org/model/content/1.0}subjectline\":\"The quick brown fox jumps over the lazy dog\","+ - "\"{http://www.alfresco.org/model/content/1.0}title\":\"The quick brown fox jumps over the lazy dog\","+ - "\"{http://www.alfresco.org/model/imap/1.0}dateSent\":1086351802000,"+ - "\"{http://www.alfresco.org/model/imap/1.0}messageCc\":\"Nevin Nollop \","+ - "\"{http://www.alfresco.org/model/imap/1.0}messageFrom\":\"Nevin Nollop \","+ - "\"{http://www.alfresco.org/model/imap/1.0}messageId\":\"<20040604122322.GV1905@phoenix.home>\","+ - "\"{http://www.alfresco.org/model/imap/1.0}messageSubject\":\"The quick brown fox jumps over the lazy dog\","+ - "\"{http://www.alfresco.org/model/imap/1.0}messageTo\":\"Nevin Nollop \""+ - "}"; - MvcResult result = sendRequest("eml", - null, - MIMETYPE_RFC822, - "json", - "alfresco-metadata-extract", - null, - null, - null, - readTestFile("eml")); - String metadata = result.getResponse().getContentAsString(); - assertEquals(expected, metadata, "Metadata extract"); - } - - /** - * Test extracting metadata specified in an option from a valid eml file - */ - @Test - public void testExtractMetadataOptionRFC822() throws Exception - { - // {"messageSubject":["{http://www.alfresco.org/model/imap/1.0}messageSubject","{http://www.alfresco.org/model/content/1.0}subjectline","{http://www.alfresco.org/model/content/1.0}description","{http://www.alfresco.org/model/content/1.0}title"],"Thread-Index":["{http://www.alfresco.org/model/imap/1.0}threadIndex"],"messageTo":["{http://www.alfresco.org/model/imap/1.0}messageTo","{http://www.alfresco.org/model/content/1.0}addressee"],"messageSent":["{http://www.alfresco.org/model/content/1.0}sentdate","{http://www.alfresco.org/model/imap/1.0}dateSent"],"Message-ID":["{http://www.alfresco.org/model/imap/1.0}messageId"],"messageCc":["{http://www.alfresco.org/model/imap/1.0}messageCc","{http://www.alfresco.org/model/content/1.0}addressees"],"messageReceived":["{http://www.alfresco.org/model/imap/1.0}dateReceived"],"messageFrom":["{http://www.alfresco.org/model/imap/1.0}messageFrom","{http://www.alfresco.org/model/content/1.0}originator"]} - String extractMapping = - "{\"messageSubject\":[" + - "\"{http://www.alfresco.org/model/imap/1.0}messageSubject\"," + - "\"{http://www.alfresco.org/model/content/1.0}title\"]," + - "\"Thread-Index\":[" + - "\"{http://www.alfresco.org/model/imap/1.0}threadIndex\"]," + - "\"messageFrom\":[" + - "\"{http://www.alfresco.org/model/dod5015/1.0}dodProp1\"]}\n"; - String expected = - "{\"{http://www.alfresco.org/model/content/1.0}title\":\"The quick brown fox jumps over the lazy dog\","+ - "\"{http://www.alfresco.org/model/dod5015/1.0}dodProp1\":\"Nevin Nollop \"," + - "\"{http://www.alfresco.org/model/imap/1.0}messageSubject\":\"The quick brown fox jumps over the lazy dog\"}"; - MvcResult result = sendRequest("eml", - null, - MIMETYPE_RFC822, - "json", - "alfresco-metadata-extract", - null, - null, - extractMapping, - readTestFile("eml")); - String metadata = result.getResponse().getContentAsString(); - assertEquals(expected, metadata, "Option metadata extract"); - } - - /** - * Test transforming a valid eml with a html part containing html special characters to text - */ - @Test - public void testHtmlSpecialCharsToText() throws Exception - { - String expected = " "; - MvcResult result = sendRequest("eml", - null, - MIMETYPE_RFC822, - "txt", - MIMETYPE_TEXT_PLAIN, - null, - null, - null, - readTestFile("htmlChars.eml")); - assertFalse(result.getResponse().getContentAsString().contains(expected)); - } - - @Test - public void testHTMLtoString() throws Exception - { - final String NEWLINE = System.getProperty("line.separator"); - final String TITLE = "Testing!"; - final String TEXT_P1 = "This is some text in English"; - final String TEXT_P2 = "This is more text in English"; - final String TEXT_P3 = "C'est en Fran\u00e7ais et Espa\u00f1ol"; - String partA = "" + TITLE + "" + NEWLINE; - String partB = "

" + TEXT_P1 + "

" + NEWLINE + - "

" + TEXT_P2 + "

" + NEWLINE + - "

" + TEXT_P3 + "

" + NEWLINE; - String partC = ""; - final String expected = TITLE + NEWLINE + TEXT_P1 + NEWLINE + TEXT_P2 + NEWLINE + TEXT_P3 + NEWLINE; - - MvcResult result = sendRequest("html", - "UTF-8", - MIMETYPE_HTML, - "txt", - MIMETYPE_TEXT_PLAIN, - null, - null, - null, - expected.getBytes()); - - String contentResult = new String(result.getResponse().getContentAsByteArray(), - targetEncoding); - assertTrue(contentResult.contains(expected), "The content did not include \"" + expected); - } - - @Test - public void testStringToString() throws Exception - { - String expected; - byte[] content; - try - { - content = "azAz10!�$%^&*()\t\r\n".getBytes(UTF_8); - expected = new String(content, "MacDingbat"); - } - catch (UnsupportedEncodingException e) - { - throw new RuntimeException("Encoding not recognised", e); - } - - MvcResult result = sendRequest("txt", - "MacDingbat", - MIMETYPE_TEXT_PLAIN, - "txt", - MIMETYPE_TEXT_PLAIN, - "UTF-8", - null, - null, - content); - - String contentResult = new String(result.getResponse().getContentAsByteArray(), - targetEncoding); - assertTrue(contentResult.contains(expected), "The content did not include \"" + expected); - } - - @Test - public void testEmptyTextFileReturnsEmptyFile() throws Exception - { - // Use empty content to create an empty source file - byte[] content = new byte[0]; - - MvcResult result = sendRequest("txt", - "UTF-8", - MIMETYPE_TEXT_PLAIN, - "txt", - MIMETYPE_TEXT_PLAIN, - "UTF-8", - null, - null, - content); - - assertEquals(0, result.getResponse().getContentLength(), - "Returned content should be empty for an empty source file"); - } - - @Test - public void textToPdf() throws Exception - { - StringBuilder sb = new StringBuilder(); - for (int i = 1; i <= 5; i++) - { - sb.append(Integer.toString(i)); - sb.append(" I must not talk in class or feed my homework to my cat.\n"); - } - sb.append("\nBart\n"); - String expected = sb.toString(); - - MvcResult result = sendRequest("txt", - "UTF-8", - MIMETYPE_TEXT_PLAIN, - "pdf", - MIMETYPE_PDF, - null, - "1", - null, - expected.getBytes()); - - // Read back in the PDF and check it - PDDocument doc = PDDocument.load(result.getResponse().getContentAsByteArray()); - PDFTextStripper textStripper = new PDFTextStripper(); - StringWriter textWriter = new StringWriter(); - textStripper.writeText(doc, textWriter); - doc.close(); - - expected = clean(expected); - String actual = clean(textWriter.toString()); - - assertEquals(expected, actual, "The content did not match."); - } - - @Test - public void testAppleIWorksPages() throws Exception - { - MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS, - "jpeg", MIMETYPE_IMAGE_JPEG, null, null, null, readTestFile("pages")); - assertTrue(result.getResponse().getContentAsByteArray().length > 0L, - "Expected image content but content is empty."); - } - - @Test - public void testAppleIWorksNumbers() throws Exception - { - MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS, - "jpeg", MIMETYPE_IMAGE_JPEG, null, null, null, readTestFile("numbers")); - assertTrue(result.getResponse().getContentAsByteArray().length > 0L, - "Expected image content but content is empty."); - } - - @Test - public void testAppleIWorksKey() throws Exception - { - MvcResult result = sendRequest("key", null, MIMETYPE_IWORK_KEYNOTE, - "jpeg", MIMETYPE_IMAGE_JPEG, null, null, null, readTestFile("key")); - assertTrue(result.getResponse().getContentAsByteArray().length > 0L, - "Expected image content but content is empty."); - } - -// @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, null, null, readTestFile("docx")); - assertTrue(result.getResponse().getContentAsByteArray().length > 0L, - "Expected image content but content is empty."); - } - - private MvcResult sendRequest(String sourceExtension, - String sourceEncoding, - String sourceMimetype, - String targetExtension, - String targetMimetype, - String targetEncoding, - String pageLimit, - String extractMapping, - byte[] content) throws Exception - { - final MockMultipartFile sourceFile = new MockMultipartFile("file", - "test_file." + sourceExtension, sourceMimetype, content); - - final MockHttpServletRequestBuilder requestBuilder = super - .mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile) - .param(TARGET_MIMETYPE, targetMimetype) - .param(SOURCE_MIMETYPE, sourceMimetype); - - // SourceEncoding is available in the options but is not used to select the transformer as it is a known - // like the source mimetype. - if (sourceEncoding != null) - { - requestBuilder.param("sourceEncoding", sourceEncoding); - } - if (targetEncoding != null) - { - requestBuilder.param("targetEncoding", targetEncoding); - } - if (pageLimit != null) - { - requestBuilder.param("pageLimit", pageLimit); - } - if (extractMapping != null) - { - requestBuilder.param("extractMapping", extractMapping); - } - - return mockMvc.perform(requestBuilder) - .andExpect(status().isOk()) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=" + - (targetEncoding == null ? "UTF-8" : targetEncoding) + - "''transform." + targetExtension)) - .andReturn(); - } - - private String clean(String text) - { - text = text.replaceAll("\\s+\\r", ""); - text = text.replaceAll("\\s+\\n", ""); - text = text.replaceAll("\\r", ""); - text = text.replaceAll("\\n", ""); - return text; - } - - @Test - @Override - public void queueTransformRequestUsingDirectAccessUrlTest() throws Exception - { - super.targetMimetype = this.targetMimetype; - super.queueTransformRequestUsingDirectAccessUrlTest(); - } -} \ No newline at end of file +/* + * #%L + * Alfresco Transform Core + * %% + * Copyright (C) 2005 - 2025 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.transform.misc; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IWORK_KEYNOTE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IWORK_NUMBERS; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_RFC822; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; +import static org.alfresco.transform.common.RequestParamMap.HTML_COLLAPSE; +import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE; +import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE; + +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import java.nio.file.Files; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.text.PDFTextStripper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; + +import org.alfresco.transform.base.AbstractBaseTest; + +/** + * Test Misc. Includes calling the 3rd party libraries. + */ +public class MiscTest extends AbstractBaseTest +{ + protected static final String sourceEncoding = "UTF-8"; + protected static final String targetEncoding = "UTF-8"; + protected final String targetMimetype = MIMETYPE_TEXT_PLAIN; + + @BeforeEach + public void before() throws Exception + { + sourceMimetype = MIMETYPE_HTML; + sourceExtension = "html"; + targetExtension = "txt"; + expectedOptions = null; + expectedSourceSuffix = null; + sourceFileBytes = readTestFile(sourceExtension); + expectedTargetFileBytes = Files.readAllBytes(getTestFile("quick3." + targetExtension, true).toPath()); + sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, sourceFileBytes); + } + + @Override + protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params) + { + final MockHttpServletRequestBuilder builder = super.mockMvcRequest(url, sourceFile, params) + .param("sourceEncoding", sourceEncoding) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype) + .param(HTML_COLLAPSE, "true"); + + // Only the 'string' transformer should have the targetEncoding. + if (!"message/rfc822".equals(sourceMimetype) && !"text/html".equals(sourceMimetype)) + { + builder.param("targetEncoding", targetEncoding); + } + return builder; + } + + /** + * Test transforming a valid eml file to text + */ + @Test + public void testRFC822ToText() throws Exception + { + String expected = "Gym class featuring a brown fox and lazy dog"; + MvcResult result = sendRequest("eml", + null, + MIMETYPE_RFC822, + "txt", + MIMETYPE_TEXT_PLAIN, + null, + null, + null, + readTestFile("eml")); + assertTrue(result.getResponse().getContentAsString().contains(expected), + "Content from eml transform didn't contain expected value. "); + } + + /** + * Test transforming a non-ascii eml file to text + */ + @Test + public void testNonAsciiRFC822ToText() throws Exception + { + 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, + null, + null, readTestFile("spanish.eml")); + + String contentResult = new String(result.getResponse().getContentAsByteArray(), UTF_8); + assertTrue(contentResult.contains(expected), + "Content from eml transform didn't contain expected value. "); + } + + /** + * Test transforming a valid eml with an attachment to text; attachment should be ignored + */ + @Test + public void testRFC822WithAttachmentToText() throws Exception + { + String expected = "Mail with attachment content"; + String notExpected = "File attachment content"; + MvcResult result = sendRequest("eml", + null, + MIMETYPE_RFC822, + "txt", + MIMETYPE_TEXT_PLAIN, + null, + null, + null, + readTestFile("attachment.eml")); + assertTrue(result.getResponse().getContentAsString().contains(expected), + "Content from eml transform didn't contain expected value. "); + assertFalse(result.getResponse().getContentAsString().contains(notExpected)); + } + + /** + * Test transforming a valid eml with minetype multipart/alternative to text + */ + @Test + public void testRFC822AlternativeToText() throws Exception + { + String expected = "alternative plain text"; + MvcResult result = sendRequest("eml", + null, + MIMETYPE_RFC822, + "txt", + MIMETYPE_TEXT_PLAIN, + null, + null, + null, + readTestFile("alternative.eml")); + assertTrue(result.getResponse().getContentAsString().contains(expected), + "Content from eml transform didn't contain expected value. "); + } + + /** + * Test transforming a valid eml with nested mimetype multipart/alternative to text + */ + @Test + public void testRFC822NestedAlternativeToText() throws Exception + { + String expected = "nested alternative plain text"; + MvcResult result = sendRequest("eml", + null, + MIMETYPE_RFC822, + "txt", + MIMETYPE_TEXT_PLAIN, + null, + null, + null, + readTestFile("nested.alternative.eml")); + assertTrue(result.getResponse().getContentAsString().contains(expected), + "Content from eml transform didn't contain expected value. "); + } + + /** + * Test extracting default metadata from a valid eml file + */ + @Test + public void testExtractMetadataRFC822() throws Exception + { + String expected = "{" + + "\"{http://www.alfresco.org/model/content/1.0}addressee\":\"Nevin Nollop \"," + + "\"{http://www.alfresco.org/model/content/1.0}addressees\":\"Nevin Nollop \"," + + "\"{http://www.alfresco.org/model/content/1.0}description\":\"The quick brown fox jumps over the lazy dog\"," + + "\"{http://www.alfresco.org/model/content/1.0}originator\":\"Nevin Nollop \"," + + "\"{http://www.alfresco.org/model/content/1.0}sentdate\":1086351802000," + + "\"{http://www.alfresco.org/model/content/1.0}subjectline\":\"The quick brown fox jumps over the lazy dog\"," + + "\"{http://www.alfresco.org/model/content/1.0}title\":\"The quick brown fox jumps over the lazy dog\"," + + "\"{http://www.alfresco.org/model/imap/1.0}dateSent\":1086351802000," + + "\"{http://www.alfresco.org/model/imap/1.0}messageCc\":\"Nevin Nollop \"," + + "\"{http://www.alfresco.org/model/imap/1.0}messageFrom\":\"Nevin Nollop \"," + + "\"{http://www.alfresco.org/model/imap/1.0}messageId\":\"<20040604122322.GV1905@phoenix.home>\"," + + "\"{http://www.alfresco.org/model/imap/1.0}messageSubject\":\"The quick brown fox jumps over the lazy dog\"," + + "\"{http://www.alfresco.org/model/imap/1.0}messageTo\":\"Nevin Nollop \"" + + "}"; + MvcResult result = sendRequest("eml", + null, + MIMETYPE_RFC822, + "json", + "alfresco-metadata-extract", + null, + null, + null, + readTestFile("eml")); + String metadata = result.getResponse().getContentAsString(); + assertEquals(expected, metadata, "Metadata extract"); + } + + /** + * Test extracting metadata specified in an option from a valid eml file + */ + @Test + public void testExtractMetadataOptionRFC822() throws Exception + { + // {"messageSubject":["{http://www.alfresco.org/model/imap/1.0}messageSubject","{http://www.alfresco.org/model/content/1.0}subjectline","{http://www.alfresco.org/model/content/1.0}description","{http://www.alfresco.org/model/content/1.0}title"],"Thread-Index":["{http://www.alfresco.org/model/imap/1.0}threadIndex"],"messageTo":["{http://www.alfresco.org/model/imap/1.0}messageTo","{http://www.alfresco.org/model/content/1.0}addressee"],"messageSent":["{http://www.alfresco.org/model/content/1.0}sentdate","{http://www.alfresco.org/model/imap/1.0}dateSent"],"Message-ID":["{http://www.alfresco.org/model/imap/1.0}messageId"],"messageCc":["{http://www.alfresco.org/model/imap/1.0}messageCc","{http://www.alfresco.org/model/content/1.0}addressees"],"messageReceived":["{http://www.alfresco.org/model/imap/1.0}dateReceived"],"messageFrom":["{http://www.alfresco.org/model/imap/1.0}messageFrom","{http://www.alfresco.org/model/content/1.0}originator"]} + String extractMapping = "{\"messageSubject\":[" + + "\"{http://www.alfresco.org/model/imap/1.0}messageSubject\"," + + "\"{http://www.alfresco.org/model/content/1.0}title\"]," + + "\"Thread-Index\":[" + + "\"{http://www.alfresco.org/model/imap/1.0}threadIndex\"]," + + "\"messageFrom\":[" + + "\"{http://www.alfresco.org/model/dod5015/1.0}dodProp1\"]}\n"; + String expected = "{\"{http://www.alfresco.org/model/content/1.0}title\":\"The quick brown fox jumps over the lazy dog\"," + + "\"{http://www.alfresco.org/model/dod5015/1.0}dodProp1\":\"Nevin Nollop \"," + + "\"{http://www.alfresco.org/model/imap/1.0}messageSubject\":\"The quick brown fox jumps over the lazy dog\"}"; + MvcResult result = sendRequest("eml", + null, + MIMETYPE_RFC822, + "json", + "alfresco-metadata-extract", + null, + null, + extractMapping, + readTestFile("eml")); + String metadata = result.getResponse().getContentAsString(); + assertEquals(expected, metadata, "Option metadata extract"); + } + + /** + * Test transforming a valid eml with a html part containing html special characters to text + */ + @Test + public void testHtmlSpecialCharsToText() throws Exception + { + String expected = " "; + MvcResult result = sendRequest("eml", + null, + MIMETYPE_RFC822, + "txt", + MIMETYPE_TEXT_PLAIN, + null, + null, + null, + readTestFile("htmlChars.eml")); + assertFalse(result.getResponse().getContentAsString().contains(expected)); + } + + @Test + public void testHTMLtoString() throws Exception + { + final String NEWLINE = System.getProperty("line.separator"); + final String TITLE = "Testing!"; + final String TEXT_P1 = "This is some text in English"; + final String TEXT_P2 = "This is more text in English"; + final String TEXT_P3 = "C'est en Fran\u00e7ais et Espa\u00f1ol"; + String partA = "" + TITLE + "" + NEWLINE; + String partB = "

" + TEXT_P1 + "

" + NEWLINE + + "

" + TEXT_P2 + "

" + NEWLINE + + "

" + TEXT_P3 + "

" + NEWLINE; + String partC = ""; + final String expected = TITLE + " " + TEXT_P1 + " " + TEXT_P2 + " " + TEXT_P3; + + MvcResult result = sendRequest("html", + "UTF-8", + MIMETYPE_HTML, + "txt", + MIMETYPE_TEXT_PLAIN, + null, + null, + null, + expected.getBytes()); + + String contentResult = new String(result.getResponse().getContentAsByteArray(), + targetEncoding); + assertTrue(contentResult.contains(expected), "The content did not include \"" + expected); + } + + @Test + public void testStringToString() throws Exception + { + String expected; + byte[] content; + try + { + content = "azAz10!�$%^&*()\t\r\n".getBytes(UTF_8); + expected = new String(content, "MacDingbat"); + } + catch (UnsupportedEncodingException e) + { + throw new RuntimeException("Encoding not recognised", e); + } + + MvcResult result = sendRequest("txt", + "MacDingbat", + MIMETYPE_TEXT_PLAIN, + "txt", + MIMETYPE_TEXT_PLAIN, + "UTF-8", + null, + null, + content); + + String contentResult = new String(result.getResponse().getContentAsByteArray(), + targetEncoding); + assertTrue(contentResult.contains(expected), "The content did not include \"" + expected); + } + + @Test + public void testEmptyTextFileReturnsEmptyFile() throws Exception + { + // Use empty content to create an empty source file + byte[] content = new byte[0]; + + MvcResult result = sendRequest("txt", + "UTF-8", + MIMETYPE_TEXT_PLAIN, + "txt", + MIMETYPE_TEXT_PLAIN, + "UTF-8", + null, + null, + content); + + assertEquals(0, result.getResponse().getContentLength(), + "Returned content should be empty for an empty source file"); + } + + @Test + public void textToPdf() throws Exception + { + StringBuilder sb = new StringBuilder(); + for (int i = 1; i <= 5; i++) + { + sb.append(Integer.toString(i)); + sb.append(" I must not talk in class or feed my homework to my cat.\n"); + } + sb.append("\nBart\n"); + String expected = sb.toString(); + + MvcResult result = sendRequest("txt", + "UTF-8", + MIMETYPE_TEXT_PLAIN, + "pdf", + MIMETYPE_PDF, + null, + "1", + null, + expected.getBytes()); + + // Read back in the PDF and check it + PDDocument doc = PDDocument.load(result.getResponse().getContentAsByteArray()); + PDFTextStripper textStripper = new PDFTextStripper(); + StringWriter textWriter = new StringWriter(); + textStripper.writeText(doc, textWriter); + doc.close(); + + expected = clean(expected); + String actual = clean(textWriter.toString()); + + assertEquals(expected, actual, "The content did not match."); + } + + @Test + public void testAppleIWorksPages() throws Exception + { + MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS, + "jpeg", MIMETYPE_IMAGE_JPEG, null, null, null, readTestFile("pages")); + assertTrue(result.getResponse().getContentAsByteArray().length > 0L, + "Expected image content but content is empty."); + } + + @Test + public void testAppleIWorksNumbers() throws Exception + { + MvcResult result = sendRequest("numbers", null, MIMETYPE_IWORK_NUMBERS, + "jpeg", MIMETYPE_IMAGE_JPEG, null, null, null, readTestFile("numbers")); + assertTrue(result.getResponse().getContentAsByteArray().length > 0L, + "Expected image content but content is empty."); + } + + @Test + public void testAppleIWorksKey() throws Exception + { + MvcResult result = sendRequest("key", null, MIMETYPE_IWORK_KEYNOTE, + "jpeg", MIMETYPE_IMAGE_JPEG, null, null, null, readTestFile("key")); + assertTrue(result.getResponse().getContentAsByteArray().length > 0L, + "Expected image content but content is empty."); + } + + // @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, null, null, readTestFile("docx")); + // assertTrue(result.getResponse().getContentAsByteArray().length > 0L, + // "Expected image content but content is empty."); + // } + + private MvcResult sendRequest(String sourceExtension, + String sourceEncoding, + String sourceMimetype, + String targetExtension, + String targetMimetype, + String targetEncoding, + String pageLimit, + String extractMapping, + byte[] content) throws Exception + { + final MockMultipartFile sourceFile = new MockMultipartFile("file", + "test_file." + sourceExtension, sourceMimetype, content); + + final MockHttpServletRequestBuilder requestBuilder = super.mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile) + .param(TARGET_MIMETYPE, targetMimetype) + .param(SOURCE_MIMETYPE, sourceMimetype); + + // SourceEncoding is available in the options but is not used to select the transformer as it is a known + // like the source mimetype. + if (sourceEncoding != null) + { + requestBuilder.param("sourceEncoding", sourceEncoding); + } + if (targetEncoding != null) + { + requestBuilder.param("targetEncoding", targetEncoding); + } + if (pageLimit != null) + { + requestBuilder.param("pageLimit", pageLimit); + } + if (extractMapping != null) + { + requestBuilder.param("extractMapping", extractMapping); + } + + return mockMvc.perform(requestBuilder) + .andExpect(status().isOk()) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=" + + (targetEncoding == null ? "UTF-8" : targetEncoding) + + "''transform." + targetExtension)) + .andReturn(); + } + + private String clean(String text) + { + text = text.replaceAll("\\s+\\r", ""); + text = text.replaceAll("\\s+\\n", ""); + text = text.replaceAll("\\r", ""); + text = text.replaceAll("\\n", ""); + return text; + } + + @Test + @Override + public void queueTransformRequestUsingDirectAccessUrlTest() throws Exception + { + super.targetMimetype = this.targetMimetype; + super.queueTransformRequestUsingDirectAccessUrlTest(); + } +} diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/HtmlParserContentTransformerTest.java b/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/HtmlParserContentTransformerTest.java index 3375f9bb..4698c7c0 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/HtmlParserContentTransformerTest.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/HtmlParserContentTransformerTest.java @@ -1,162 +1,300 @@ -/* - * #%L - * Alfresco Transform Core - * %% - * Copyright (C) 2005 - 2022 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.transform.misc.transformers; - -import org.junit.jupiter.api.Test; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStreamWriter; -import java.nio.file.Files; -import java.util.HashMap; -import java.util.Map; - -import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class HtmlParserContentTransformerTest -{ - private static final String SOURCE_MIMETYPE = "text/html"; - private static final String TARGET_MIMETYPE = "text/plain"; - - HtmlParserContentTransformer transformer = new HtmlParserContentTransformer(); - - /** - * Checks that we correctly handle text in different encodings, - * no matter if the encoding is specified on the Content Property - * or in a meta tag within the HTML itself. (ALF-10466) - * - * On Windows, org.htmlparser.beans.StringBean.carriageReturn() appends a new system dependent new line - * so we must be careful when checking the returned text - */ - @Test - public void testEncodingHandling() throws Exception - { - final String NEWLINE = System.getProperty("line.separator"); - final String TITLE = "Testing!"; - final String TEXT_P1 = "This is some text in English"; - final String TEXT_P2 = "This is more text in English"; - final String TEXT_P3 = "C'est en Fran\u00e7ais et Espa\u00f1ol"; - String partA = "" + TITLE + "" + NEWLINE; - String partB = "

" + TEXT_P1 + "

" + NEWLINE + - "

" + TEXT_P2 + "

" + NEWLINE + - "

" + TEXT_P3 + "

" + NEWLINE; - String partC = ""; - final String expected = TITLE + NEWLINE + TEXT_P1 + NEWLINE + TEXT_P2 + NEWLINE + TEXT_P3 + NEWLINE; - - File tmpS = null; - File tmpD = null; - - try - { - // Content set to ISO 8859-1 - tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); - writeToFile(tmpS, partA + partB + partC, "ISO-8859-1"); - - tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); - - Map parameters = new HashMap<>(); - parameters.put(SOURCE_ENCODING, "ISO-8859-1"); - transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); - - assertEquals(expected, readFromFile(tmpD, "UTF-8")); - tmpS.delete(); - tmpD.delete(); - - // Content set to UTF-8 - tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); - writeToFile(tmpS, partA + partB + partC, "UTF-8"); - - tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); - parameters = new HashMap<>(); - parameters.put(SOURCE_ENCODING, "UTF-8"); - transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); - assertEquals(expected, readFromFile(tmpD, "UTF-8")); - tmpS.delete(); - tmpD.delete(); - - // Content set to UTF-16 - tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); - writeToFile(tmpS, partA + partB + partC, "UTF-16"); - - tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); - parameters = new HashMap<>(); - parameters.put(SOURCE_ENCODING, "UTF-16"); - transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); - assertEquals(expected, readFromFile(tmpD, "UTF-8")); - tmpS.delete(); - tmpD.delete(); - - // Note - since HTML Parser 2.0 META tags specifying the - // document encoding will ONLY be respected if the original - // content type was set to ISO-8859-1. - // - // This means there is now only one test which we can perform - // to ensure that this now-limited overriding of the encoding - // takes effect. - - // Content set to ISO 8859-1, meta set to UTF-8 - tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); - String str = partA + - "" + - partB + partC; - - writeToFile(tmpS, str, "UTF-8"); - - tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); - - parameters = new HashMap<>(); - parameters.put(SOURCE_ENCODING, "ISO-8859-1"); - transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); - assertEquals(expected, readFromFile(tmpD, "UTF-8")); - tmpS.delete(); - tmpD.delete(); - - // Note - we can't test UTF-16 with only a meta encoding, - // because without that the parser won't know about the - // 2 byte format so won't be able to identify the meta tag - } - finally - { - if (tmpS != null && tmpS.exists()) tmpS.delete(); - if (tmpD != null && tmpD.exists()) tmpD.delete(); - } - } - - private void writeToFile(File file, String content, String encoding) throws Exception - { - try (OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file), encoding)) - { - ow.append(content); - } - } - - private String readFromFile(File file, final String encoding) throws Exception - { - return new String(Files.readAllBytes(file.toPath()), encoding); - } -} \ No newline at end of file +/* + * #%L + * Alfresco Transform Core + * %% + * Copyright (C) 2005 - 2025 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.transform.misc.transformers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +import static org.alfresco.transform.common.RequestParamMap.HTML_COLLAPSE; +import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class HtmlParserContentTransformerTest +{ + private static final String SOURCE_MIMETYPE = "text/html"; + private static final String TARGET_MIMETYPE = "text/plain"; + + /** + * Checks that we correctly handle text in different encodings, no matter if the encoding is specified on the Content Property or in a meta tag within the HTML itself. (ALF-10466) + * + * On Windows, org.htmlparser.beans.StringBean.carriageReturn() appends a new system dependent new line so we must be careful when checking the returned text + */ + @Test + public void testEncodingHandling() throws Exception + { + final HtmlParserContentTransformer transformer = new HtmlParserContentTransformer(); + final String newline = System.getProperty("line.separator"); + final String title = "Testing!"; + final String textp1 = "This is some text in English"; + final String textp2 = "This is more text in English"; + final String textp3 = "C'est en Fran\u00e7ais et Espa\u00f1ol"; + String partA = "" + title + "" + newline; + String partB = "

" + textp1 + "

" + newline + + "

" + textp2 + "

" + newline + + "

" + textp3 + "

" + newline; + String partC = ""; + final String expected = title + newline + textp1 + newline + textp2 + newline + textp3; + + File tmpS = null; + File tmpD = null; + + try + { + // Content set to ISO 8859-1 + tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); + writeToFile(tmpS, partA + partB + partC, "ISO-8859-1"); + + tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); + + Map parameters = new HashMap<>(); + parameters.put(SOURCE_ENCODING, "ISO-8859-1"); + parameters.put(HTML_COLLAPSE, String.valueOf(true)); + transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); + + assertEquals(expected, readFromFile(tmpD, "UTF-8")); + tmpS.delete(); + tmpD.delete(); + + // Content set to UTF-8 + tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); + writeToFile(tmpS, partA + partB + partC, "UTF-8"); + + tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); + parameters = new HashMap<>(); + parameters.put(SOURCE_ENCODING, "UTF-8"); + parameters.put(HTML_COLLAPSE, String.valueOf(true)); + transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); + assertEquals(expected, readFromFile(tmpD, "UTF-8")); + tmpS.delete(); + tmpD.delete(); + + // Content set to UTF-16 + tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); + writeToFile(tmpS, partA + partB + partC, "UTF-16"); + + tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); + parameters = new HashMap<>(); + parameters.put(HTML_COLLAPSE, String.valueOf(true)); + parameters.put(SOURCE_ENCODING, "UTF-16"); + transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); + assertEquals(expected, readFromFile(tmpD, "UTF-8")); + tmpS.delete(); + tmpD.delete(); + + // Note - since HTML Parser 2.0 META tags specifying the + // document encoding will ONLY be respected if the original + // content type was set to ISO-8859-1. + // + // This means there is now only one test which we can perform + // to ensure that this now-limited overriding of the encoding + // takes effect. + + // Content set to ISO 8859-1, meta set to UTF-8 + tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); + String str = partA + + "" + + partB + partC; + + writeToFile(tmpS, str, "UTF-8"); + + tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); + + parameters = new HashMap<>(); + parameters.put(SOURCE_ENCODING, "ISO-8859-1"); + parameters.put(HTML_COLLAPSE, String.valueOf(true)); + transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); + assertEquals(expected, readFromFile(tmpD, "UTF-8")); + tmpS.delete(); + tmpD.delete(); + + // Note - we can't test UTF-16 with only a meta encoding, + // because without that the parser won't know about the + // 2 byte format so won't be able to identify the meta tag + } + catch (Exception e) + { + fail("Test Failed: " + e.getMessage()); // fail the test if any exception occurs + } + finally + { + if (tmpS != null && tmpS.exists()) + { + tmpS.delete(); + } + if (tmpD != null && tmpD.exists()) + { + tmpD.delete(); + } + } + } + + /** + * Tests the transformer with different collapsing methods. If the collapsing is set to false, it should not collapse the new lines between paragraphs. If the collapsing is set to true, it should collapse the new lines. + */ + @ParameterizedTest + @ValueSource(booleans = {true, false}) + public void testTransformerWithDifferentCollapsingMethods(boolean shouldCollapse) + { + final HtmlParserContentTransformer transformer = new HtmlParserContentTransformer(); + + final String newline = System.getProperty("line.separator"); + final String title = "Testing!"; + final String textp1 = "This is some text in English"; + final String textp2 = "This is more text in English"; + final String textp3 = "C'est en Fran\u00e7ais et Espa\u00f1ol"; + String partA = "" + title + "" + newline; + String partB = "

" + textp1 + "

" + newline + + "

" + textp2 + "

" + newline + + "

" + textp3 + "

" + newline; + String partC = ""; + final String expected = title + newline + textp1 + newline + textp2 + newline + textp3 + (shouldCollapse ? "" : newline); // Just a added newline if collapsing is not collapsing + + File tmpS = null; + File tmpD = null; + + try + { + tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); + writeToFile(tmpS, partA + partB + partC, "UTF-8"); + + tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); + Map parameters = new HashMap<>(); + parameters.put(SOURCE_ENCODING, "UTF-8"); + parameters.put(HTML_COLLAPSE, String.valueOf(shouldCollapse)); + transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); + assertEquals(expected, readFromFile(tmpD, "UTF-8")); + tmpS.delete(); + tmpD.delete(); + } + catch (Exception e) + { + fail("Test Failed: " + e.getMessage()); // fail the test if any exception occurs + } + finally + { + if (tmpS != null && tmpS.exists()) + { + tmpS.delete(); + } + if (tmpD != null && tmpD.exists()) + { + tmpD.delete(); + } + } + } + + /** + * Tests the transformer with wrong boolean values for the collapse option. It should not throw an exception and should use the default value for collapsing. + */ + + @ParameterizedTest + @ValueSource(strings = {"cat", "dog", "", "1234abcd", "@#$%"}) + public void testTransformerWithWrongBooleanValues(String booleanValues) + { + final HtmlParserContentTransformer transformer = new HtmlParserContentTransformer(); + + final String newline = System.getProperty("line.separator"); + final String title = "Testing!"; + final String textp1 = "This is some text in English"; + final String textp2 = "This is more text in English"; + final String textp3 = "C'est en Fran\u00e7ais et Espa\u00f1ol"; + String partA = "" + title + "" + newline; + String partB = "

" + textp1 + "

" + newline + + "

" + textp2 + "

" + newline + + "

" + textp3 + "

" + newline; + String partC = ""; + final String expected = title + newline + textp1 + newline + textp2 + newline + textp3; + + File tmpS = null; + File tmpD = null; + + try + { + tmpS = File.createTempFile("AlfrescoTestSource_", ".html"); + writeToFile(tmpS, partA + partB + partC, "UTF-8"); + + tmpD = File.createTempFile("AlfrescoTestTarget_", ".txt"); + Map parameters = new HashMap<>(); + parameters.put(SOURCE_ENCODING, "UTF-8"); + parameters.put(HTML_COLLAPSE, booleanValues); + transformer.transform(SOURCE_MIMETYPE, TARGET_MIMETYPE, parameters, tmpS, tmpD, null); + assertEquals(expected, readFromFile(tmpD, "UTF-8")); + tmpS.delete(); + tmpD.delete(); + } + catch (Exception e) + { + fail("Test Failed: " + e.getMessage()); // fail the test if any exception occurs + } + finally + { + if (tmpS != null && tmpS.exists()) + { + tmpS.delete(); + } + if (tmpD != null && tmpD.exists()) + { + tmpD.delete(); + } + } + } + + private void writeToFile(File file, String content, String encoding) + { + try (OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file), encoding)) + { + ow.append(content); + } + catch (Exception e) + { + fail("Failed to write to file: " + e.getMessage()); // fail the test if any exception occurs + } + } + + private String readFromFile(File file, final String encoding) + { + try + { + return new String(Files.readAllBytes(file.toPath()), encoding); + } + catch (Exception e) + { + fail("Failed to read from file: " + e.getMessage()); + return null; // Return null if there is an error reading the file + } + } +} diff --git a/engines/misc/src/test/resources/misc_engine_config.json b/engines/misc/src/test/resources/misc_engine_config.json index 37fbafe4..a4e21d26 100644 --- a/engines/misc/src/test/resources/misc_engine_config.json +++ b/engines/misc/src/test/resources/misc_engine_config.json @@ -1,5 +1,8 @@ { "transformOptions": { + "htmlOptions": [ + {"value": {"name": "collapseHtml"}} + ], "textToPdfOptions": [ {"value": {"name": "pageLimit"}} ], @@ -17,6 +20,7 @@ {"sourceMediaType": "text/html", "targetMediaType": "text/plain"} ], "transformOptions": [ + "htmlOptions" ] }, { diff --git a/engines/misc/src/test/resources/quick3.txt b/engines/misc/src/test/resources/quick3.txt new file mode 100644 index 00000000..6d8c17c1 --- /dev/null +++ b/engines/misc/src/test/resources/quick3.txt @@ -0,0 +1,2 @@ +The quick brown fox jumps over the lazy dog +The quick brown fox jumps over the lazy dog \ No newline at end of file diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTest.java b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTest.java index 82dd68d7..8a768aa2 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTest.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTest.java @@ -2,7 +2,7 @@ * #%L * Alfresco Transform Core * %% - * Copyright (C) 2005 - 2022 Alfresco Software Limited + * Copyright (C) 2005 - 2025 Alfresco Software Limited * %% * This file is part of the Alfresco software. * - @@ -26,31 +26,21 @@ */ package org.alfresco.transform.tika; -import com.google.common.collect.ImmutableSet; -import org.alfresco.transform.base.AbstractBaseTest; -import org.alfresco.transform.base.executors.RuntimeExec; -import org.alfresco.transform.base.model.FileRefEntity; -import org.alfresco.transform.base.model.FileRefResponse; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.apache.poi.ooxml.POIXMLProperties; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.springframework.core.io.FileSystemResource; -import org.springframework.core.io.Resource; -import org.springframework.http.HttpHeaders; -import org.springframework.http.ResponseEntity; -import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.util.UUID; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.springframework.http.HttpHeaders.ACCEPT; +import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION; +import static org.springframework.http.HttpHeaders.CONTENT_TYPE; +import static org.springframework.http.HttpStatus.CREATED; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.springframework.http.HttpStatus.OK; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; +import static org.springframework.http.MediaType.APPLICATION_PDF_VALUE; +import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.alfresco.transform.base.html.OptionsHelper.getOptionNames; import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; @@ -89,21 +79,33 @@ import static org.alfresco.transform.tika.transformers.Tika.XHTML; import static org.alfresco.transform.tika.transformers.Tika.XLSX; import static org.alfresco.transform.tika.transformers.Tika.XML; import static org.alfresco.transform.tika.transformers.Tika.ZIP; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; -import static org.springframework.http.HttpHeaders.ACCEPT; -import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION; -import static org.springframework.http.HttpHeaders.CONTENT_TYPE; -import static org.springframework.http.HttpStatus.CREATED; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; -import static org.springframework.http.HttpStatus.OK; -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; -import static org.springframework.http.MediaType.APPLICATION_PDF_VALUE; -import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.util.UUID; + +import com.google.common.collect.ImmutableSet; +import org.apache.poi.ooxml.POIXMLProperties; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import org.alfresco.transform.base.AbstractBaseTest; +import org.alfresco.transform.base.executors.RuntimeExec; +import org.alfresco.transform.base.model.FileRefEntity; +import org.alfresco.transform.base.model.FileRefResponse; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; /** * Test Tika. @@ -113,9 +115,9 @@ public class TikaTest extends AbstractBaseTest private static final String EXPECTED_XHTML_CONTENT_CONTAINS = "

The quick brown fox jumps over the lazy dog

"; private static final String EXPECTED_TEXT_CONTENT_CONTAINS = "The quick brown fox jumps over the lazy dog"; private static final String EXPECTED_MSG_CONTENT_CONTAINS = "Recipients\n" + - "\tmark.rogers@alfresco.com; speedy@quick.com; mrquick@nowhere.com\n" + - "\n" + - "The quick brown fox jumps over the lazy dogs"; + "\tmark.rogers@alfresco.com; speedy@quick.com; mrquick@nowhere.com\n" + + "\n" + + "The quick brown fox jumps over the lazy dogs"; private static final String EXPECTED_CSV_CONTENT_CONTAINS = "\"The\",\"quick\",\"brown\",\"fox\""; @Mock @@ -139,8 +141,8 @@ public class TikaTest extends AbstractBaseTest @Override protected void mockTransformCommand(String sourceExtension, - String targetExtension, String sourceMimetype, - boolean readTargetFileBytes) throws IOException + String targetExtension, String sourceMimetype, + boolean readTargetFileBytes) throws IOException { // Tika transform is not mocked. It is run for real. @@ -160,8 +162,8 @@ public class TikaTest extends AbstractBaseTest } private void transform(String transform, String sourceExtension, String targetExtension, - String sourceMimetype, String targetMimetype, - Boolean includeContents, String expectedContentContains) throws Exception + String sourceMimetype, String targetMimetype, + Boolean includeContents, String expectedContentContains) throws Exception { // We don't use targetFileBytes as some of the transforms contain different date text based on the os being used. mockTransformCommand(sourceExtension, targetExtension, sourceMimetype, false); @@ -169,18 +171,18 @@ public class TikaTest extends AbstractBaseTest System.out.println("Test " + transform + " " + sourceExtension + " to " + targetExtension); MockHttpServletRequestBuilder requestBuilder = includeContents == null - ? mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, - "targetExtension", this.targetExtension) - : mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, - "targetExtension", this.targetExtension, INCLUDE_CONTENTS, includeContents.toString()); + ? mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, + "targetExtension", this.targetExtension) + : mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, + "targetExtension", this.targetExtension, INCLUDE_CONTENTS, includeContents.toString()); MvcResult result = mockMvc.perform(requestBuilder) - .andExpect(status().is(OK.value())) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + this.targetExtension)) - .andReturn(); + .andExpect(status().is(OK.value())) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + this.targetExtension)) + .andReturn(); String content = result.getResponse().getContentAsString(); assertTrue(content.contains(expectedContentContains), - "The content did not include \"" + expectedContentContains); + "The content did not include \"" + expectedContentContains); } @Override @@ -188,9 +190,9 @@ public class TikaTest extends AbstractBaseTest protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params) { return super.mockMvcRequest(url, sourceFile, params) - .param("targetEncoding", targetEncoding) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype); + .param("targetEncoding", targetEncoding) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype); } @Test @@ -199,8 +201,8 @@ public class TikaTest extends AbstractBaseTest mockTransformCommand(PDF, TXT, MIMETYPE_PDF, true); targetEncoding = "rubbish"; mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) - .andExpect(status().is(INTERNAL_SERVER_ERROR.value())); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) + .andExpect(status().is(INTERNAL_SERVER_ERROR.value())); } // --- Archive --- @@ -209,55 +211,55 @@ public class TikaTest extends AbstractBaseTest public void zipToTextArchiveTest() throws Exception { transform(ARCHIVE, ZIP, TXT, MIMETYPE_ZIP, MIMETYPE_TEXT_PLAIN, false, - "quick.html\n" + - "\n" + - "\n" + - "quick.pdf\n" + - "\n" + - "\n"); + "quick.html\n" + + "\n" + + "\n" + + "quick.pdf\n" + + "\n" + + "\n"); } @Test public void zipToTextIncludeArchiveTest() throws Exception { transform(ARCHIVE, ZIP, TXT, MIMETYPE_ZIP, MIMETYPE_TEXT_PLAIN, true, - "quick.html\n" + - "\n" + - "\n" + - "The quick brown fox jumps over the lazy dog\n" + - "\n" + - "\n" + - "\n" + - "quick.pdf\n" + - "\n" + - "\n" + - "The quick brown fox jumps over the lazy dog" + - "\n" + - "\n"); + "quick.html\n" + + "\n" + + "\n" + + "The quick brown fox jumps over the lazy dog\n" + + "\n" + + "\n" + + "\n" + + "quick.pdf\n" + + "\n" + + "\n" + + "The quick brown fox jumps over the lazy dog" + + "\n" + + "\n"); } @Test public void zipToTextExcludeArchiveTest() throws Exception { transform(ARCHIVE, ZIP, TXT, MIMETYPE_ZIP, MIMETYPE_TEXT_PLAIN, - false, "\n" + - "folder/subfolder/quick.jpg\n" + - "\n" + - "\n" + - "quick.doc\n" + - "\n" + - "\n" + - "quick.html\n" + - "\n" + - "\n" + - "quick.pdf\n" + - "\n" + - "\n" + - "quick.txt\n" + - "\n" + - "\n" + - "quick.xml\n" + - "\n"); + false, "\n" + + "folder/subfolder/quick.jpg\n" + + "\n" + + "\n" + + "quick.doc\n" + + "\n" + + "\n" + + "quick.html\n" + + "\n" + + "\n" + + "quick.pdf\n" + + "\n" + + "\n" + + "quick.txt\n" + + "\n" + + "\n" + + "quick.xml\n" + + "\n"); } // --- OutlookMsg --- @@ -266,7 +268,7 @@ public class TikaTest extends AbstractBaseTest public void msgToTxtOutlookMsgTest() throws Exception { transform(OUTLOOK_MSG, MSG, TXT, MIMETYPE_OUTLOOK_MSG, MIMETYPE_TEXT_PLAIN, null, - EXPECTED_MSG_CONTENT_CONTAINS); + EXPECTED_MSG_CONTENT_CONTAINS); } // --- PdfBox --- @@ -275,35 +277,35 @@ public class TikaTest extends AbstractBaseTest public void pdfToTxtPdfBoxTest() throws Exception { transform(PDF_BOX, PDF, TXT, MIMETYPE_PDF, MIMETYPE_TEXT_PLAIN, null, - EXPECTED_TEXT_CONTENT_CONTAINS); + EXPECTED_TEXT_CONTENT_CONTAINS); } @Test public void pdfToCsvPdfBoxTest() throws Exception { transform(PDF_BOX, PDF, CSV, MIMETYPE_PDF, MIMETYPE_TEXT_CSV, null, - EXPECTED_TEXT_CONTENT_CONTAINS); // Yes it is just text + EXPECTED_TEXT_CONTENT_CONTAINS); // Yes it is just text } @Test public void pdfToXmlPdfBoxTest() throws Exception { transform(PDF_BOX, PDF, XML, MIMETYPE_PDF, MIMETYPE_XML, null, - EXPECTED_XHTML_CONTENT_CONTAINS); // Yes it is just XHTML + EXPECTED_XHTML_CONTENT_CONTAINS); // Yes it is just XHTML } @Test public void pdfToXhtmlPdfBoxTest() throws Exception { transform(PDF_BOX, PDF, XHTML, MIMETYPE_PDF, MIMETYPE_XHTML, null, - EXPECTED_XHTML_CONTENT_CONTAINS); + EXPECTED_XHTML_CONTENT_CONTAINS); } @Test public void pdfToHtmlPdfBoxTest() throws Exception { transform(PDF_BOX, PDF, HTML, MIMETYPE_PDF, MIMETYPE_HTML, null, - EXPECTED_XHTML_CONTENT_CONTAINS); // Yes it is just XHTML + EXPECTED_XHTML_CONTENT_CONTAINS); // Yes it is just XHTML } // --- Office --- @@ -312,14 +314,14 @@ public class TikaTest extends AbstractBaseTest public void msgToTxtOfficeTest() throws Exception { transform(OFFICE, MSG, TXT, MIMETYPE_OUTLOOK_MSG, MIMETYPE_TEXT_PLAIN, null, - EXPECTED_MSG_CONTENT_CONTAINS); + EXPECTED_MSG_CONTENT_CONTAINS); } @Test public void docToTxtOfficeTest() throws Exception { transform(OFFICE, DOC, TXT, MIMETYPE_WORD, MIMETYPE_TEXT_PLAIN, null, - EXPECTED_TEXT_CONTENT_CONTAINS); + EXPECTED_TEXT_CONTENT_CONTAINS); } // --- Poi --- @@ -328,7 +330,7 @@ public class TikaTest extends AbstractBaseTest public void xslxToCsvPoiTest() throws Exception { transform(POI, XLSX, CSV, MIMETYPE_OPENXML_SPREADSHEET, MIMETYPE_TEXT_CSV, null, - EXPECTED_CSV_CONTENT_CONTAINS); + EXPECTED_CSV_CONTENT_CONTAINS); } // --- OOXML --- @@ -337,14 +339,14 @@ public class TikaTest extends AbstractBaseTest public void docxToTxtOoXmlTest() throws Exception { transform(OOXML, DOCX, TXT, MIMETYPE_OPENXML_WORDPROCESSING, MIMETYPE_TEXT_PLAIN, null, - EXPECTED_TEXT_CONTENT_CONTAINS); + EXPECTED_TEXT_CONTENT_CONTAINS); } @Test public void pptxToTxtOoXmlTest() throws Exception { transform(OOXML, PPTX, TXT, MIMETYPE_OPENXML_PRESENTATION, MIMETYPE_TEXT_PLAIN, null, - EXPECTED_TEXT_CONTENT_CONTAINS); + EXPECTED_TEXT_CONTENT_CONTAINS); } // --- TikaAuto --- @@ -353,14 +355,14 @@ public class TikaTest extends AbstractBaseTest public void ppxtToTxtTikaAutoTest() throws Exception { transform(TIKA_AUTO, PPTX, TXT, MIMETYPE_OPENXML_PRESENTATION, MIMETYPE_TEXT_PLAIN, null, - EXPECTED_TEXT_CONTENT_CONTAINS); + EXPECTED_TEXT_CONTENT_CONTAINS); } @Test public void doctToTxtTikaAutoTest() throws Exception { transform(TIKA_AUTO, DOCX, TXT, MIMETYPE_OPENXML_WORDPROCESSING, MIMETYPE_TEXT_PLAIN, null, - EXPECTED_TEXT_CONTENT_CONTAINS); + EXPECTED_TEXT_CONTENT_CONTAINS); } // --- TextMining --- @@ -369,7 +371,7 @@ public class TikaTest extends AbstractBaseTest public void docToTxtTextMiningTest() throws Exception { transform(TEXT_MINING, DOC, TXT, MIMETYPE_WORD, MIMETYPE_TEXT_PLAIN, null, - EXPECTED_TEXT_CONTENT_CONTAINS); + EXPECTED_TEXT_CONTENT_CONTAINS); } @Test @@ -377,24 +379,22 @@ public class TikaTest extends AbstractBaseTest { mockTransformCommand(XLSX, XLSX, MIMETYPE_OPENXML_SPREADSHEET, false); - String metadata = - "{\"{http://www.alfresco.org/model/content/1.0}author\":\"author1\"," + - "\"{http://www.alfresco.org/model/content/1.0}title\":\"title1\"," + - "\"{http://www.alfresco.org/model/content/1.0}description\":[\"desc1\",\"desc2\"]," + - "\"{http://www.alfresco.org/model/content/1.0}created\":\"created1\"}"; + String metadata = "{\"{http://www.alfresco.org/model/content/1.0}author\":\"author1\"," + + "\"{http://www.alfresco.org/model/content/1.0}title\":\"title1\"," + + "\"{http://www.alfresco.org/model/content/1.0}description\":[\"desc1\",\"desc2\"]," + + "\"{http://www.alfresco.org/model/content/1.0}created\":\"created1\"}"; - MockHttpServletRequestBuilder requestBuilder = - super.mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, - "targetExtension", XLSX, - "metadata", metadata, - "targetMimetype", MIMETYPE_METADATA_EMBED, - "sourceMimetype", MIMETYPE_OPENXML_SPREADSHEET); + MockHttpServletRequestBuilder requestBuilder = super.mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, + "targetExtension", XLSX, + "metadata", metadata, + "targetMimetype", MIMETYPE_METADATA_EMBED, + "sourceMimetype", MIMETYPE_OPENXML_SPREADSHEET); MvcResult result = mockMvc.perform(requestBuilder) .andExpect(status().is(OK.value())) .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)). - andReturn(); + "attachment; filename*=UTF-8''transform." + targetExtension)) + .andReturn(); byte[] bytes = result.getResponse().getContentAsByteArray(); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); @@ -414,11 +414,11 @@ public class TikaTest extends AbstractBaseTest { mockTransformCommand(PDF, TXT, MIMETYPE_PDF, true); mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension).param( - NOT_EXTRACT_BOOKMARKS_TEXT, "true")) - .andExpect(status().is(OK.value())) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension).param( + NOT_EXTRACT_BOOKMARKS_TEXT, "true")) + .andExpect(status().is(OK.value())) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Override @@ -445,11 +445,11 @@ public class TikaTest extends AbstractBaseTest HttpHeaders headers = new HttpHeaders(); headers.set(CONTENT_DISPOSITION, "attachment; filename=quick." + sourceExtension); ResponseEntity response = new ResponseEntity<>(new FileSystemResource( - sourceFile), headers, OK); + sourceFile), headers, OK); when(sharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response); when(sharedFileStoreClient.saveFile(any())) - .thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); + .thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); when(mockExecutionResult.getExitValue()).thenReturn(0); // Update the Transformation Request with any specific params before sending it @@ -458,16 +458,16 @@ public class TikaTest extends AbstractBaseTest // Serialize and call the transformer String tr = objectMapper.writeValueAsString(transformRequest); String transformationReplyAsString = mockMvc - .perform(MockMvcRequestBuilders - .post(ENDPOINT_TRANSFORM) - .header(ACCEPT, APPLICATION_JSON_VALUE) - .header(CONTENT_TYPE, APPLICATION_JSON_VALUE) - .content(tr)) - .andExpect(status().is(CREATED.value())) - .andReturn().getResponse().getContentAsString(); + .perform(MockMvcRequestBuilders + .post(ENDPOINT_TRANSFORM) + .header(ACCEPT, APPLICATION_JSON_VALUE) + .header(CONTENT_TYPE, APPLICATION_JSON_VALUE) + .content(tr)) + .andExpect(status().is(CREATED.value())) + .andReturn().getResponse().getContentAsString(); TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, - TransformReply.class); + TransformReply.class); // Assert the reply assertEquals(transformRequest.getRequestId(), transformReply.getRequestId()); @@ -492,6 +492,6 @@ public class TikaTest extends AbstractBaseTest "extractMapping", "notExtractBookmarksText", "metadata"), - getOptionNames(controller.transformConfig(0).getBody().getTransformOptions())); + getOptionNames(controller.transformConfig(0).getBody().getTransformOptions())); } } diff --git a/model/src/main/java/org/alfresco/transform/common/RequestParamMap.java b/model/src/main/java/org/alfresco/transform/common/RequestParamMap.java index eb13e483..d266f99c 100644 --- a/model/src/main/java/org/alfresco/transform/common/RequestParamMap.java +++ b/model/src/main/java/org/alfresco/transform/common/RequestParamMap.java @@ -1,93 +1,96 @@ -/* - * #%L - * Alfresco Transform Model - * %% - * Copyright (C) 2005 - 2022 Alfresco Software Limited - * %% - * This program 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. - * - * This program 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 General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * . - * #L% - */ -package org.alfresco.transform.common; - -import org.alfresco.transform.config.CoreVersionDecorator; - -/** - * Request parameters and transform options used in the core transformers. - */ -public interface RequestParamMap -{ - // html parameter names - String FILE = "file"; - String SOURCE_EXTENSION = "sourceExtension"; - String TARGET_EXTENSION = "targetExtension"; - String SOURCE_MIMETYPE = "sourceMimetype"; - String TARGET_MIMETYPE = "targetMimetype"; - - // Transform options used in the core transformers. - String SOURCE_ENCODING = "sourceEncoding"; - String TARGET_ENCODING = "targetEncoding"; - String PAGE_REQUEST_PARAM = "page"; - String WIDTH_REQUEST_PARAM = "width"; - String HEIGHT_REQUEST_PARAM = "height"; - String ALLOW_PDF_ENLARGEMENT = "allowPdfEnlargement"; - String MAINTAIN_PDF_ASPECT_RATIO = "maintainPdfAspectRatio"; - String START_PAGE = "startPage"; - String END_PAGE = "endPage"; - String ALPHA_REMOVE = "alphaRemove"; - String AUTO_ORIENT = "autoOrient"; - String CROP_GRAVITY = "cropGravity"; - String CROP_WIDTH = "cropWidth"; - String CROP_HEIGHT = "cropHeight"; - String CROP_PERCENTAGE = "cropPercentage"; - String CROP_X_OFFSET = "cropXOffset"; - String CROP_Y_OFFSET = "cropYOffset"; - String THUMBNAIL = "thumbnail"; - String RESIZE_WIDTH = "resizeWidth"; - String RESIZE_HEIGHT = "resizeHeight"; - String RESIZE_PERCENTAGE = "resizePercentage"; - String ALLOW_ENLARGEMENT = "allowEnlargement"; - String MAINTAIN_ASPECT_RATIO = "maintainAspectRatio"; - String COMMAND_OPTIONS = "commandOptions"; - String TIMEOUT = "timeout"; - String INCLUDE_CONTENTS = "includeContents"; - String NOT_EXTRACT_BOOKMARKS_TEXT = "notExtractBookmarksText"; - String PAGE_LIMIT = "pageLimit"; - String PDF_FORMAT = "pdfFormat"; - String PDF_ORIENTATION = "pdfOrientation"; - String PDF_FONT = "pdfFont"; - String PDF_FONT_SIZE = "pdfFontSize"; - - // Parameters interpreted by the TransformController - String DIRECT_ACCESS_URL = "directAccessUrl"; - - // An optional parameter (defaults to 1) to be included in the request to the t-engine {@code /transform/config} - // endpoint to specify what version (of the schema) to return. Provides the flexibility to introduce changes - // without getting deserialization issues when we have components at different versions. - String CONFIG_VERSION = "configVersion"; - String CONFIG_VERSION_DEFAULT = "1"; - int CONFIG_VERSION_LATEST = CoreVersionDecorator.CONFIG_VERSION_INCLUDES_CORE_VERSION; - - // Endpoints - String ENDPOINT_TRANSFORM = "/transform"; - String ENDPOINT_TEST = "/test"; - String ENDPOINT_TRANSFORM_CONFIG = "/transform/config"; - String ENDPOINT_TRANSFORM_CONFIG_LATEST = ENDPOINT_TRANSFORM_CONFIG + "?" + CONFIG_VERSION + "=" + CONFIG_VERSION_LATEST; - String ENDPOINT_VERSION = "/version"; - String ENDPOINT_READY = "/ready"; - String ENDPOINT_LIVE = "/live"; - String ENDPOINT_ERROR = "/error"; - String ENDPOINT_LOG = "/log"; - String ENDPOINT_ROOT = "/"; -} +/* + * #%L + * Alfresco Transform Model + * %% + * Copyright (C) 2005 - 2025 Alfresco Software Limited + * %% + * This program 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. + * + * This program 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 General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ +package org.alfresco.transform.common; + +import org.alfresco.transform.config.CoreVersionDecorator; + +/** + * Request parameters and transform options used in the core transformers. + */ +public interface RequestParamMap +{ + // html parameter names + String FILE = "file"; + String SOURCE_EXTENSION = "sourceExtension"; + String TARGET_EXTENSION = "targetExtension"; + String SOURCE_MIMETYPE = "sourceMimetype"; + String TARGET_MIMETYPE = "targetMimetype"; + + // Transform options used in the core transformers. + String SOURCE_ENCODING = "sourceEncoding"; + String TARGET_ENCODING = "targetEncoding"; + String PAGE_REQUEST_PARAM = "page"; + String WIDTH_REQUEST_PARAM = "width"; + String HEIGHT_REQUEST_PARAM = "height"; + String ALLOW_PDF_ENLARGEMENT = "allowPdfEnlargement"; + String MAINTAIN_PDF_ASPECT_RATIO = "maintainPdfAspectRatio"; + String START_PAGE = "startPage"; + String END_PAGE = "endPage"; + String ALPHA_REMOVE = "alphaRemove"; + String AUTO_ORIENT = "autoOrient"; + String CROP_GRAVITY = "cropGravity"; + String CROP_WIDTH = "cropWidth"; + String CROP_HEIGHT = "cropHeight"; + String CROP_PERCENTAGE = "cropPercentage"; + String CROP_X_OFFSET = "cropXOffset"; + String CROP_Y_OFFSET = "cropYOffset"; + String THUMBNAIL = "thumbnail"; + String RESIZE_WIDTH = "resizeWidth"; + String RESIZE_HEIGHT = "resizeHeight"; + String RESIZE_PERCENTAGE = "resizePercentage"; + String ALLOW_ENLARGEMENT = "allowEnlargement"; + String MAINTAIN_ASPECT_RATIO = "maintainAspectRatio"; + String COMMAND_OPTIONS = "commandOptions"; + String TIMEOUT = "timeout"; + String INCLUDE_CONTENTS = "includeContents"; + String NOT_EXTRACT_BOOKMARKS_TEXT = "notExtractBookmarksText"; + String PAGE_LIMIT = "pageLimit"; + String PDF_FORMAT = "pdfFormat"; + String PDF_ORIENTATION = "pdfOrientation"; + String PDF_FONT = "pdfFont"; + String PDF_FONT_SIZE = "pdfFontSize"; + + // Html parameter names for the transform config + String HTML_COLLAPSE = "collapseHtml"; + + // Parameters interpreted by the TransformController + String DIRECT_ACCESS_URL = "directAccessUrl"; + + // An optional parameter (defaults to 1) to be included in the request to the t-engine {@code /transform/config} + // endpoint to specify what version (of the schema) to return. Provides the flexibility to introduce changes + // without getting deserialization issues when we have components at different versions. + String CONFIG_VERSION = "configVersion"; + String CONFIG_VERSION_DEFAULT = "1"; + int CONFIG_VERSION_LATEST = CoreVersionDecorator.CONFIG_VERSION_INCLUDES_CORE_VERSION; + + // Endpoints + String ENDPOINT_TRANSFORM = "/transform"; + String ENDPOINT_TEST = "/test"; + String ENDPOINT_TRANSFORM_CONFIG = "/transform/config"; + String ENDPOINT_TRANSFORM_CONFIG_LATEST = ENDPOINT_TRANSFORM_CONFIG + "?" + CONFIG_VERSION + "=" + CONFIG_VERSION_LATEST; + String ENDPOINT_VERSION = "/version"; + String ENDPOINT_READY = "/ready"; + String ENDPOINT_LIVE = "/live"; + String ENDPOINT_ERROR = "/error"; + String ENDPOINT_LOG = "/log"; + String ENDPOINT_ROOT = "/"; +}