diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 86c34e9b..9ae55011 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,29 +38,23 @@ jobs: pre_commit: runs-on: ubuntu-latest steps: - - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.14.1 - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - id: changed-files - uses: Alfresco/alfresco-build-tools/.github/actions/github-list-changes@v8.14.1 - with: - write-list-to-env: true - - uses: Alfresco/alfresco-build-tools/.github/actions/pre-commit@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/pre-commit@v8.26.0 veracode_sca: name: "Veracode - Source Clear Scan (SCA)" runs-on: ubuntu-latest + needs: + - pre_commit if: > github.ref_name == 'master' || github.event_name == 'pull_request' steps: - uses: actions/checkout@v4 - - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.14.1 - - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.26.0 + - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.26.0 - name: "Clean-up SNAPSHOT artifacts" run: find "${HOME}/.m2/repository/" -type d -name "*-SNAPSHOT*" | xargs -r -l rm -rf - - uses: Alfresco/alfresco-build-tools/.github/actions/veracode@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/veracode@v8.26.0 continue-on-error: true with: srcclr-api-token: ${{ secrets.SRCCLR_API_TOKEN }} @@ -68,14 +62,16 @@ jobs: veracode_sast: name: "Pipeline SAST Scan" runs-on: ubuntu-latest + needs: + - pre_commit if: > (github.ref_name == 'master' || startsWith(github.ref_name, 'SP/') || startsWith(github.ref_name, 'HF/') || github.event_name == 'pull_request') && github.actor != 'dependabot[bot]' && !contains(github.event.head_commit.message, '[skip tests]') steps: - uses: actions/checkout@v4 - - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.14.1 - - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.26.0 + - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.26.0 - name: "Login to Docker Hub" uses: docker/login-action@v3 with: @@ -87,7 +83,7 @@ jobs: registry: quay.io username: ${{ secrets.QUAY_USERNAME }} password: ${{ secrets.QUAY_PASSWORD }} - - uses: Alfresco/alfresco-build-tools/.github/actions/github-download-file@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/github-download-file@v8.26.0 with: token: ${{ secrets.BOT_GITHUB_TOKEN }} repository: "Alfresco/veracode-baseline-archive" @@ -141,8 +137,8 @@ jobs: !contains(github.event.head_commit.message, '[skip tests]') steps: - uses: actions/checkout@v4 - - uses: Alfresco/alfresco-build-tools/.github/actions/free-hosted-runner-disk-space@v8.14.1 - - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/free-hosted-runner-disk-space@v8.26.0 + - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.26.0 - uses: Alfresco/ya-pmd-scan@v4.1.0 with: classpath-build-command: "mvn -ntp package -DskipTests" @@ -150,13 +146,15 @@ jobs: build_and_test: name: "Core & Base Snapshot deployment" runs-on: ubuntu-latest + needs: + - pre_commit if: > github.ref_name == 'master' && github.event_name != 'pull_request' steps: - uses: actions/checkout@v4 - - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.14.1 - - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.26.0 + - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.26.0 - name: "Login to Docker Hub" uses: docker/login-action@v3 with: @@ -186,6 +184,8 @@ jobs: all_tests_matrix: name: ${{ matrix.testName }} runs-on: ubuntu-latest + needs: + - pre_commit strategy: fail-fast: false matrix: @@ -210,8 +210,8 @@ jobs: testProfile: aio-test steps: - uses: actions/checkout@v4 - - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.14.1 - - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.26.0 + - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.26.0 - name: "Login to Docker Hub" uses: docker/login-action@v3 with: @@ -251,8 +251,8 @@ jobs: - uses: actions/checkout@v4 with: persist-credentials: false - - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.14.1 - - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.26.0 + - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.26.0 - name: Set up QEMU uses: docker/setup-qemu-action@v3 with: @@ -274,7 +274,7 @@ jobs: sudo service docker restart - name: "Clean-up SNAPSHOT artifacts" run: find "${HOME}/.m2/repository/" -type d -name "*-SNAPSHOT*" | xargs -r -l rm -rf - - uses: Alfresco/alfresco-build-tools/.github/actions/configure-git-author@v8.14.1 + - uses: Alfresco/alfresco-build-tools/.github/actions/configure-git-author@v8.26.0 with: username: ${{ env.GIT_USERNAME }} email: ${{ env.GIT_EMAIL }} diff --git a/.github/workflows/precommit_formatter.yml b/.github/workflows/precommit_formatter.yml new file mode 100644 index 00000000..3205fe70 --- /dev/null +++ b/.github/workflows/precommit_formatter.yml @@ -0,0 +1,32 @@ +name: Pre-Commit formatter + +on: + push: + branches: + - precommit/** + +jobs: + format-code: + name: "Reformat code" + runs-on: ubuntu-latest + if: contains(github.event.head_commit.message, '[reformat code]') + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ inputs.python-version }} + uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0 + with: + python-version: "3.9" + - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 + continue-on-error: true + with: + extra_args: --all-files + - name: Update secrets baseline + run: pip install detect-secrets && detect-secrets scan --baseline .secrets.baseline + - uses: Alfresco/alfresco-build-tools/.github/actions/git-commit-changes@v8.26.0 + with: + username: ${{ secrets.BOT_GITHUB_USERNAME }} + add-options: -u + commit-message: "Apply Pre-Commit code formatting" + skip-if-no-changes: true + - name: Push changes + run: git push diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 486dcce6..3f28bf9f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: hooks: - id: check-format-and-headers name: Check format and headers and fix if necessary - entry: ./scripts/check-format-and-headers.sh + entry: ./scripts/hooks/check-format-and-headers.sh language: script files: ".*.java" pass_filenames: false diff --git a/.secrets.baseline b/.secrets.baseline index 0794f6e9..ba972247 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -133,8 +133,7 @@ "filename": "deprecated/alfresco-transformer-base/src/main/resources/application.yaml", "hashed_secret": "8c1ab56bc026c89468942df91166c35a277455a7", "is_verified": false, - "line_number": 10, - "is_secret": false + "line_number": 10 } ], "engines/base/src/main/resources/application.yaml": [ @@ -143,8 +142,7 @@ "filename": "engines/base/src/main/resources/application.yaml", "hashed_secret": "8c1ab56bc026c89468942df91166c35a277455a7", "is_verified": false, - "line_number": 10, - "is_secret": false + "line_number": 10 } ], "engines/libreoffice/src/test/resources/quick.vdx": [ @@ -153,10 +151,9 @@ "filename": "engines/libreoffice/src/test/resources/quick.vdx", "hashed_secret": "37c332070388a4797ccf1fa64f7029a193ce0371", "is_verified": false, - "line_number": 2, - "is_secret": false + "line_number": 2 } ] }, - "generated_at": "2024-09-26T14:01:33Z" + "generated_at": "2025-07-18T08:03:08Z" } diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..2344814a --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,51 @@ +### Contributing +Thanks for your interest in contributing to this project! + +The following is a set of guidelines for contributing to this library. Most of them will make the life of the reviewer easier and therefore decrease the time required for the patch be included in the next version. + +The project uses [pre-commit](https://pre-commit.com/) to format code (with [Spotless](https://github.com/diffplug/spotless)), validate license headers and check for secrets (with [detect-secrets](https://github.com/Yelp/detect-secrets)). To install the pre-commit hooks then first install pre-commit and then run: +```shell +pre-commit install +``` +When you make a commit then these hooks will run and check the modified files. If it makes changes then you can review them and then `git commit` again to accept the changes. + +#### Code Quality +This project uses `spotless` that enforces `alfresco-formatter.xml` to ensure code quality. +The code style definition file is taken always form the `master` branch of `alfresco-community-repo`. +All downstream projects use this code style definition file as well. + +To check code-style violations you can use: +```bash +mvn spotless:check +``` +To reformat files you can use: +```bash +mvn spotless:apply +``` + +#### Secret Detection + +We are using [detect-secrets](https://github.com/Yelp/detect-secrets) to try to avoid accidentally publishing secret keys. +If you have pre-commit installed then this should run automatically when making a commit. Usually there should be no issues, +but if it finds a potential issue (e.g. a high entropy string) then you will see the following: + +```shell +Detect secrets...........................................................Failed +- hook id: detect-secrets +- exit code: 1 + +ERROR: Potential secrets about to be committed to git repo! + +Secret Type: Secret Keyword +Location: test.txt:1 +``` + +If this is a false positive and you actually want to commit the string then run these two commands: + +```shell +detect-secrets scan --baseline .secrets.baseline +detect-secrets audit .secrets.baseline +``` + +This will update the baseline file to include your new code and then allow you to review the detected secret and mark it as a false positive. +Once you are finished then you can add `.secrets.baseline` to the staged changes and you should be able to create a commit. diff --git a/alfresco-formatter.xml b/alfresco-formatter.xml deleted file mode 100644 index 9d6b8943..00000000 --- a/alfresco-formatter.xml +++ /dev/null @@ -1,401 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/AbstractTransformerController.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/AbstractTransformerController.java index 68210b6e..58f5d52b 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/AbstractTransformerController.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/AbstractTransformerController.java @@ -26,17 +26,50 @@ */ package org.alfresco.transformer; -import org.alfresco.transform.common.TransformerDebug; -import org.alfresco.transform.client.model.InternalContext; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.messages.TransformRequestValidator; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.registry.TransformServiceRegistry; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient; -import org.alfresco.transformer.logging.LogEntry; -import org.alfresco.transformer.model.FileRefResponse; +import static java.util.stream.Collectors.joining; + +import static org.springframework.http.HttpStatus.BAD_REQUEST; +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.MULTIPART_FORM_DATA_VALUE; +import static org.springframework.util.StringUtils.getFilenameExtension; + +import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION; +import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION_DEFAULT; +import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG; +import static org.alfresco.transform.config.CoreVersionDecorator.setOrClearCoreVersion; +import static org.alfresco.transformer.fs.FileManager.TempFileProvider.createTempFile; +import static org.alfresco.transformer.fs.FileManager.buildFile; +import static org.alfresco.transformer.fs.FileManager.createAttachment; +import static org.alfresco.transformer.fs.FileManager.createSourceFile; +import static org.alfresco.transformer.fs.FileManager.createTargetFile; +import static org.alfresco.transformer.fs.FileManager.createTargetFileName; +import static org.alfresco.transformer.fs.FileManager.deleteFile; +import static org.alfresco.transformer.fs.FileManager.getFilenameFromContentDisposition; +import static org.alfresco.transformer.fs.FileManager.save; +import static org.alfresco.transformer.util.RequestParamMap.FILE; +import static org.alfresco.transformer.util.RequestParamMap.SOURCE_ENCODING; +import static org.alfresco.transformer.util.RequestParamMap.SOURCE_EXTENSION; +import static org.alfresco.transformer.util.RequestParamMap.SOURCE_MIMETYPE; +import static org.alfresco.transformer.util.RequestParamMap.TARGET_EXTENSION; +import static org.alfresco.transformer.util.RequestParamMap.TARGET_MIMETYPE; +import static org.alfresco.transformer.util.RequestParamMap.TEST_DELAY; +import static org.alfresco.transformer.util.RequestParamMap.TRANSFORM_NAME_PROPERTY; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import jakarta.servlet.http.HttpServletRequest; + import org.codehaus.plexus.util.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,85 +89,57 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.multipart.MultipartFile; -import jakarta.servlet.http.HttpServletRequest; -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - -import static java.util.stream.Collectors.joining; -import static org.alfresco.transform.config.CoreVersionDecorator.setOrClearCoreVersion; -import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; -import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION; -import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION_DEFAULT; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG; -import static org.alfresco.transformer.fs.FileManager.TempFileProvider.createTempFile; -import static org.alfresco.transformer.fs.FileManager.buildFile; -import static org.alfresco.transformer.fs.FileManager.createAttachment; -import static org.alfresco.transformer.fs.FileManager.createSourceFile; -import static org.alfresco.transformer.fs.FileManager.createTargetFile; -import static org.alfresco.transformer.fs.FileManager.createTargetFileName; -import static org.alfresco.transformer.fs.FileManager.deleteFile; -import static org.alfresco.transformer.fs.FileManager.getFilenameFromContentDisposition; -import static org.alfresco.transformer.fs.FileManager.save; -import static org.alfresco.transformer.util.RequestParamMap.FILE; -import static org.alfresco.transformer.util.RequestParamMap.SOURCE_ENCODING; -import static org.alfresco.transformer.util.RequestParamMap.SOURCE_EXTENSION; -import static org.alfresco.transformer.util.RequestParamMap.SOURCE_MIMETYPE; -import static org.alfresco.transformer.util.RequestParamMap.TARGET_EXTENSION; -import static org.alfresco.transformer.util.RequestParamMap.TARGET_MIMETYPE; -import static org.alfresco.transformer.util.RequestParamMap.TEST_DELAY; -import static org.alfresco.transformer.util.RequestParamMap.TRANSFORM_NAME_PROPERTY; -import static org.springframework.http.HttpStatus.BAD_REQUEST; -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.MULTIPART_FORM_DATA_VALUE; -import static org.springframework.util.StringUtils.getFilenameExtension; +import org.alfresco.transform.client.model.InternalContext; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.common.TransformerDebug; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.exceptions.TransformException; +import org.alfresco.transform.messages.TransformRequestValidator; +import org.alfresco.transform.registry.TransformServiceRegistry; +import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient; +import org.alfresco.transformer.logging.LogEntry; +import org.alfresco.transformer.model.FileRefResponse; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - *

Abstract Controller, provides structure and helper methods to sub-class transformer controllers. Sub classes - * should implement {@link #transformImpl(String, String, String, Map, File, File)} and unimplemented methods from - * {@link TransformController}.

+ *

+ * Abstract Controller, provides structure and helper methods to sub-class transformer controllers. Sub classes should implement {@link #transformImpl(String, String, String, Map, File, File)} and unimplemented methods from {@link TransformController}. + *

* - *

Status Codes:

- * + *

+ * Provides methods to help super classes perform /transform requests. Also responses to /version, /ready and /live requests. + *

*/ @Deprecated public abstract class AbstractTransformerController implements TransformController { private static final Logger logger = LoggerFactory.getLogger( - AbstractTransformerController.class); + AbstractTransformerController.class); // Request parameters that are not part of transform options public static final List NON_TRANSFORM_OPTION_REQUEST_PARAMETERS = Arrays.asList(SOURCE_EXTENSION, @@ -166,16 +171,16 @@ public abstract class AbstractTransformerController implements TransformControll @PostMapping(value = ENDPOINT_TRANSFORM, consumes = MULTIPART_FORM_DATA_VALUE) public ResponseEntity transform(HttpServletRequest request, - @RequestParam(value = FILE, required = false) MultipartFile sourceMultipartFile, - @RequestParam(TARGET_EXTENSION) String targetExtension, - @RequestParam(value = SOURCE_MIMETYPE, required = false) String sourceMimetype, - @RequestParam(value = TARGET_MIMETYPE, required = false) String targetMimetype, - @RequestParam Map requestParameters, - @RequestParam(value = TEST_DELAY, required = false) Long testDelay, + @RequestParam(value = FILE, required = false) MultipartFile sourceMultipartFile, + @RequestParam(TARGET_EXTENSION) String targetExtension, + @RequestParam(value = SOURCE_MIMETYPE, required = false) String sourceMimetype, + @RequestParam(value = TARGET_MIMETYPE, required = false) String targetMimetype, + @RequestParam Map requestParameters, + @RequestParam(value = TEST_DELAY, required = false) Long testDelay, - // The TRANSFORM_NAME_PROPERTY param allows ACS legacy transformers to specify which transform to use, - // It can be removed once legacy transformers are removed from ACS. - @RequestParam(value = TRANSFORM_NAME_PROPERTY, required = false) String requestTransformName) + // The TRANSFORM_NAME_PROPERTY param allows ACS legacy transformers to specify which transform to use, + // It can be removed once legacy transformers are removed from ACS. + @RequestParam(value = TRANSFORM_NAME_PROPERTY, required = false) String requestTransformName) { if (logger.isDebugEnabled()) { @@ -189,7 +194,7 @@ public abstract class AbstractTransformerController implements TransformControll String sourceFilename; if (directUrl.isBlank()) { - if (sourceMultipartFile == null) + if (sourceMultipartFile == null) { throw new TransformException(BAD_REQUEST, "Required request part 'file' is not present"); } @@ -208,7 +213,7 @@ public abstract class AbstractTransformerController implements TransformControll Map transformOptions = getTransformOptions(requestParameters); String transformName = getTransformerName(sourceMimetype, targetMimetype, requestTransformName, sourceFile, transformOptions); - String reference = "e"+httpRequestCount.getAndIncrement(); + String reference = "e" + httpRequestCount.getAndIncrement(); transformerDebug.pushTransform(reference, sourceMimetype, targetMimetype, sourceFile, transformName); transformerDebug.logOptions(reference, requestParameters); try @@ -260,17 +265,18 @@ public abstract class AbstractTransformerController implements TransformControll /** * '/transform' endpoint which consumes and produces 'application/json' * - * This is the way to tell Spring to redirect the request to this endpoint - * instead of the one which produces 'html' + * This is the way to tell Spring to redirect the request to this endpoint instead of the one which produces 'html' * - * @param request The transformation request - * @param timeout Transformation timeout + * @param request + * The transformation request + * @param timeout + * Transformation timeout * @return A transformation reply */ @PostMapping(value = ENDPOINT_TRANSFORM, produces = APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity transform(@RequestBody TransformRequest request, - @RequestParam(value = "timeout", required = false) Long timeout) + @RequestParam(value = "timeout", required = false) Long timeout) { logger.trace("Received {}, timeout {} ms", request, timeout); @@ -288,10 +294,10 @@ public abstract class AbstractTransformerController implements TransformControll { reply.setStatus(BAD_REQUEST.value()); reply.setErrorDetails(errors - .getAllErrors() - .stream() - .map(Object::toString) - .collect(joining(", "))); + .getAllErrors() + .stream() + .map(Object::toString) + .collect(joining(", "))); transformerDebug.logFailure(reply); logger.trace("Invalid request, sending {}", reply); @@ -343,7 +349,7 @@ public abstract class AbstractTransformerController implements TransformControll // Create local temp target file in order to run the transformation final String targetFilename = createTargetFileName(sourceFile.getName(), - request.getTargetExtension()); + request.getTargetExtension()); final File targetFile = buildFile(targetFilename); // Run the transformation @@ -417,7 +423,7 @@ public abstract class AbstractTransformerController implements TransformControll catch (Exception e) { logger.error("Failed to delete local temp target file '{}'. Error will be ignored ", - targetFile, e); + targetFile, e); } try { @@ -461,14 +467,16 @@ public abstract class AbstractTransformerController implements TransformControll /** * Loads the file with the specified sourceReference from Alfresco Shared File Store * - * @param sourceReference reference to the file in Alfresco Shared File Store - * @param sourceExtension default extension if the file in Alfresco Shared File Store has none + * @param sourceReference + * reference to the file in Alfresco Shared File Store + * @param sourceExtension + * default extension if the file in Alfresco Shared File Store has none * @return the file containing the source content for the transformation */ private File loadSourceFile(final String sourceReference, final String sourceExtension) { ResponseEntity responseEntity = alfrescoSharedFileStoreClient - .retrieveFile(sourceReference); + .retrieveFile(sourceReference); getProbeTestTransform().incrementTransformerCount(); HttpHeaders headers = responseEntity.getHeaders(); @@ -482,14 +490,14 @@ public abstract class AbstractTransformerController implements TransformControll if (body == null) { String message = "Source file with reference: " + sourceReference + " is null or empty. " - + "Transformation will fail and stop now as there is no content to be transformed."; + + "Transformation will fail and stop now as there is no content to be transformed."; logger.warn(message); throw new TransformException(BAD_REQUEST, message); } final File file = createTempFile("source_", "." + extension); logger.debug("Read source content {} length={} contentType={}", - sourceReference, size, contentType); + sourceReference, size, contentType); save(body, file); LogEntry.setSource(filename, size); @@ -500,23 +508,23 @@ public abstract class AbstractTransformerController implements TransformControll { final StringBuilder sb = new StringBuilder(); sb.append(prefix).append(" - ") - .append(e.getClass().getSimpleName()).append(": ") - .append(e.getMessage()); + .append(e.getClass().getSimpleName()).append(": ") + .append(e.getMessage()); while (e.getCause() != null) { e = e.getCause(); sb.append(", cause ") - .append(e.getClass().getSimpleName()).append(": ") - .append(e.getMessage()); + .append(e.getClass().getSimpleName()).append(": ") + .append(e.getMessage()); } return sb.toString(); } private String getTransformerName(String sourceMimetype, String targetMimetype, - String requestTransformName, File sourceFile, - Map transformOptions) + String requestTransformName, File sourceFile, + Map transformOptions) { // Check if transformName was provided in the request (this can happen for ACS legacy transformers) String transformName = requestTransformName; @@ -532,7 +540,7 @@ public abstract class AbstractTransformerController implements TransformControll } protected String getTransformerName(final File sourceFile, final String sourceMimetype, - final String targetMimetype, final Map transformOptions) + final String targetMimetype, final Map transformOptions) { // The transformOptions always contains sourceEncoding when sent to a T-Engine, even though it should not be // used to select a transformer. Similar to source and target mimetypes and extensions, but these are not @@ -563,7 +571,7 @@ public abstract class AbstractTransformerController implements TransformControll if (namesAndValues.length % 2 != 0) { logger.error( - "Incorrect number of parameters. Should have an even number as they are names and values."); + "Incorrect number of parameters. Should have an even number as they are names and values."); } Map transformOptions = new HashMap<>(); @@ -571,7 +579,7 @@ public abstract class AbstractTransformerController implements TransformControll { String name = namesAndValues[i].toString(); Object value = namesAndValues[i + 1]; - if (value != null && (!(value instanceof String) || !((String)value).isBlank())) + if (value != null && (!(value instanceof String) || !((String) value).isBlank())) { transformOptions.put(name, value.toString()); } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/QueueTransformService.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/QueueTransformService.java index 95c8f5f8..7f728f2e 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/QueueTransformService.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/QueueTransformService.java @@ -30,16 +30,10 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; import java.util.Optional; - import jakarta.jms.Destination; import jakarta.jms.JMSException; import jakarta.jms.Message; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transformer.messaging.TransformMessageConverter; -import org.alfresco.transformer.messaging.TransformReplySender; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -49,16 +43,18 @@ import org.springframework.jms.annotation.JmsListener; import org.springframework.jms.support.converter.MessageConversionException; import org.springframework.stereotype.Component; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.exceptions.TransformException; +import org.alfresco.transformer.messaging.TransformMessageConverter; +import org.alfresco.transformer.messaging.TransformReplySender; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Queue Transformer service. - * This service reads all the requests for the particular engine, forwards them to the worker - * component (at this time the injected controller - to be refactored) and sends back the reply - * to the {@link Message#getJMSReplyTo()} value. If this value is missing we've got to a dead end. + * Queue Transformer service. This service reads all the requests for the particular engine, forwards them to the worker component (at this time the injected controller - to be refactored) and sends back the reply to the {@link Message#getJMSReplyTo()} value. If this value is missing we've got to a dead end. * - * @author Lucian Tuca - * created on 18/12/2018 + * @author Lucian Tuca created on 18/12/2018 */ @Deprecated @Component @@ -96,16 +92,16 @@ public class QueueTransformService if (replyToDestinationQueue == null) { logger.error( - "Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ", - correlationId); + "Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ", + correlationId); return; } } catch (JMSException e) { logger.error( - "Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ", - correlationId); + "Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ", + correlationId); return; } @@ -120,7 +116,7 @@ public class QueueTransformService { logger.error(e.getMessage(), e); replyWithError(replyToDestinationQueue, HttpStatus.valueOf(e.getStatusCode()), - e.getMessage(), correlationId); + e.getMessage(), correlationId); return; } @@ -128,21 +124,21 @@ public class QueueTransformService { logger.error("T-Request from message with correlationID {} is null!", correlationId); replyWithInternalSvErr(replyToDestinationQueue, - "JMS exception during T-Request deserialization: ", correlationId); + "JMS exception during T-Request deserialization: ", correlationId); return; } TransformReply reply = transformController.transform(transformRequest.get(), null) - .getBody(); + .getBody(); transformReplySender.send(replyToDestinationQueue, reply); } /** - * Tries to convert the JMS {@link Message} to a {@link TransformRequest} - * If any error occurs, a {@link TransformException} is thrown + * Tries to convert the JMS {@link Message} to a {@link TransformRequest} If any error occurs, a {@link TransformException} is thrown * - * @param msg Message to be deserialized + * @param msg + * Message to be deserialized * @return The converted {@link TransformRequest} instance */ private Optional convert(final Message msg, String correlationId) @@ -154,42 +150,39 @@ public class QueueTransformService } catch (MessageConversionException e) { - String message = - "MessageConversionException during T-Request deserialization of message with correlationID " - + correlationId + ": "; + String message = "MessageConversionException during T-Request deserialization of message with correlationID " + + correlationId + ": "; throw new TransformException(BAD_REQUEST, message + e.getMessage()); } catch (JMSException e) { - String message = - "JMSException during T-Request deserialization of message with correlationID " - + correlationId + ": "; + String message = "JMSException during T-Request deserialization of message with correlationID " + + correlationId + ": "; throw new TransformException(INTERNAL_SERVER_ERROR, message + e.getMessage()); } catch (Exception e) { - String message = - "Exception during T-Request deserialization of message with correlationID " - + correlationId + ": "; + String message = "Exception during T-Request deserialization of message with correlationID " + + correlationId + ": "; throw new TransformException(INTERNAL_SERVER_ERROR, message + e.getMessage()); } } private void replyWithInternalSvErr(final Destination destination, final String msg, - final String correlationId) + final String correlationId) { replyWithError(destination, INTERNAL_SERVER_ERROR, msg, correlationId); } private void replyWithError(final Destination destination, final HttpStatus status, - final String msg, - final String correlationId) + final String msg, + final String correlationId) { final TransformReply reply = TransformReply - .builder() - .withStatus(status.value()) - .withErrorDetails(msg) - .build(); + .builder() + .withStatus(status.value()) + .withErrorDetails(msg) + .build(); transformReplySender.send(destination, reply, correlationId); } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformController.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformController.java index 77cfbe47..dc6a2055 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformController.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformController.java @@ -27,21 +27,16 @@ package org.alfresco.transformer; import static java.text.MessageFormat.format; + import static org.springframework.http.HttpStatus.BAD_REQUEST; import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.Map; - import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transformer.logging.LogEntry; -import org.alfresco.transformer.probes.ProbeTestTransform; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.TypeMismatchException; @@ -53,13 +48,17 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.exceptions.TransformException; +import org.alfresco.transformer.logging.LogEntry; +import org.alfresco.transformer.probes.ProbeTestTransform; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * TransformController interface. - *
- * It contains much of the common boilerplate code that each of - * its concrete implementations need as default methods. + * TransformController interface.
+ * It contains much of the common boilerplate code that each of its concrete implementations need as default methods. */ @Deprecated public interface TransformController @@ -69,19 +68,24 @@ public interface TransformController /** * Should be overridden in subclasses to initiate the transformation. * - * @param transformName the name of the transformer in the engine_config.json file - * @param sourceMimetype mimetype of the source - * @param targetMimetype mimetype of the target - * @param transformOptions transform options from the client - * @param sourceFile the source file - * @param targetFile the target file + * @param transformName + * the name of the transformer in the engine_config.json file + * @param sourceMimetype + * mimetype of the source + * @param targetMimetype + * mimetype of the target + * @param transformOptions + * transform options from the client + * @param sourceFile + * the source file + * @param targetFile + * the target file */ void transformImpl(String transformName, String sourceMimetype, String targetMimetype, - Map transformOptions, File sourceFile, File targetFile); + Map transformOptions, File sourceFile, File targetFile); /** - * @deprecated use {@link #transformImpl(String, String, String, Map, File, File)} and timeout should be part of - * the transformOptions created from the TransformRequest. + * @deprecated use {@link #transformImpl(String, String, String, Map, File, File)} and timeout should be part of the transformOptions created from the TransformRequest. */ @Deprecated ResponseEntity transform(TransformRequest transformRequest, Long timeout); @@ -91,10 +95,9 @@ public interface TransformController */ @Deprecated default void processTransform(final File sourceFile, final File targetFile, - final String sourceMimetype, final String targetMimetype, - final Map transformOptions, final Long timeout) - { - } + final String sourceMimetype, final String targetMimetype, + final Map transformOptions, final Long timeout) + {} /** * @return a friendly name for the T-Engine. @@ -131,8 +134,7 @@ public interface TransformController } /** - * @return the name of a template to display when there is an error when using the test UI for the T-Engine. - * Defaults to {@code "error"}. + * @return the name of a template to display when there is an error when using the test UI for the T-Engine. Defaults to {@code "error"}. * @See #transformForm */ @GetMapping("/error") @@ -142,8 +144,7 @@ public interface TransformController } /** - * @return the name of a template to display log messages when using the test UI for the T-Engine. - * Defaults to {@code "log"}. + * @return the name of a template to display log messages when using the test UI for the T-Engine. Defaults to {@code "log"}. * @See #transformForm */ @GetMapping("/log") @@ -178,13 +179,13 @@ public interface TransformController return probe(request, true); } - //region [Exception Handlers] + // region [Exception Handlers] @ExceptionHandler(TypeMismatchException.class) default void handleParamsTypeMismatch(HttpServletResponse response, - MissingServletRequestParameterException e) throws IOException + MissingServletRequestParameterException e) throws IOException { final String message = format("Request parameter ''{0}'' is of the wrong type", e - .getParameterName()); + .getParameterName()); final int statusCode = BAD_REQUEST.value(); logger.error(message, e); @@ -196,7 +197,7 @@ public interface TransformController @ExceptionHandler(MissingServletRequestParameterException.class) default void handleMissingParams(HttpServletResponse response, - MissingServletRequestParameterException e) throws IOException + MissingServletRequestParameterException e) throws IOException { final String message = format("Request parameter ''{0}'' is missing", e.getParameterName()); final int statusCode = BAD_REQUEST.value(); @@ -210,7 +211,7 @@ public interface TransformController @ExceptionHandler(TransformException.class) default void transformExceptionWithMessage(HttpServletResponse response, - TransformException e) throws IOException + TransformException e) throws IOException { final String message = e.getMessage(); final int statusCode = e.getStatus().value(); @@ -223,5 +224,5 @@ public interface TransformController response.sendError(statusCode, getTransformerName() + " - " + message); } - //endregion + // endregion } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformInterceptor.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformInterceptor.java index 63dc112a..b431fdd2 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformInterceptor.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformInterceptor.java @@ -33,22 +33,22 @@ import static org.alfresco.transformer.fs.FileManager.deleteFile; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.alfresco.transformer.logging.LogEntry; import org.springframework.web.servlet.AsyncHandlerInterceptor; +import org.alfresco.transformer.logging.LogEntry; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * TransformInterceptor - *
- * Handles ThreadLocal Log entries for each request. + * TransformInterceptor
+ * Handles ThreadLocal Log entries for each request. */ @Deprecated public class TransformInterceptor implements AsyncHandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, - HttpServletResponse response, Object handler) + HttpServletResponse response, Object handler) { LogEntry.start(); return true; @@ -56,7 +56,7 @@ public class TransformInterceptor implements AsyncHandlerInterceptor @Override public void afterCompletion(HttpServletRequest request, - HttpServletResponse response, Object handler, Exception ex) + HttpServletResponse response, Object handler, Exception ex) { // TargetFile cannot be deleted until completion, otherwise 0 bytes are sent. deleteFile(request, SOURCE_FILE); diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformRegistryImpl.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformRegistryImpl.java index 80a2a146..4ef420e2 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformRegistryImpl.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/TransformRegistryImpl.java @@ -26,33 +26,34 @@ package org.alfresco.transformer; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.alfresco.transform.config.CoreVersionDecorator.setCoreVersionOnSingleStepTransformers; + import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.alfresco.transform.config.CoreVersionDecorator.setCoreVersionOnSingleStepTransformers; + import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; - import jakarta.annotation.PostConstruct; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.registry.AbstractTransformRegistry; -import org.alfresco.transform.registry.CombinedTransformConfig; -import org.alfresco.transform.registry.TransformCache; -import org.alfresco.transform.exceptions.TransformException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resource; - -import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.core.io.ResourceLoader; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.exceptions.TransformException; +import org.alfresco.transform.registry.AbstractTransformRegistry; +import org.alfresco.transform.registry.CombinedTransformConfig; +import org.alfresco.transform.registry.TransformCache; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Used by clients to work out if a transformation is supported based on the engine_config.json. + * Used by clients to work out if a transformation is supported based on the engine_config.json. */ @Deprecated public class TransformRegistryImpl extends AbstractTransformRegistry diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/clients/AlfrescoSharedFileStoreClient.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/clients/AlfrescoSharedFileStoreClient.java index a0fb26f0..5047fc38 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/clients/AlfrescoSharedFileStoreClient.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/clients/AlfrescoSharedFileStoreClient.java @@ -31,8 +31,6 @@ import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; import java.io.File; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transformer.model.FileRefResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; @@ -45,10 +43,13 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; +import org.alfresco.transform.exceptions.TransformException; +import org.alfresco.transformer.model.FileRefResponse; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Simple Rest client that call Alfresco Shared File Store + * Simple Rest client that call Alfresco Shared File Store */ @Deprecated public class AlfrescoSharedFileStoreClient @@ -62,7 +63,8 @@ public class AlfrescoSharedFileStoreClient /** * Retrieves a file from Shared File Store using given file reference * - * @param fileRef File reference + * @param fileRef + * File reference * @return ResponseEntity */ public ResponseEntity retrieveFile(String fileRef) @@ -70,7 +72,7 @@ public class AlfrescoSharedFileStoreClient try { return restTemplate.getForEntity(fileStoreUrl + "/" + fileRef, - org.springframework.core.io.Resource.class); + org.springframework.core.io.Resource.class); } catch (HttpClientErrorException e) { @@ -81,7 +83,8 @@ public class AlfrescoSharedFileStoreClient /** * Stores given file in Shared File Store * - * @param file File to be stored + * @param file + * File to be stored * @return A FileRefResponse containing detail about file's reference */ public FileRefResponse saveFile(File file) @@ -94,9 +97,9 @@ public class AlfrescoSharedFileStoreClient HttpHeaders headers = new HttpHeaders(); headers.setContentType(MULTIPART_FORM_DATA); HttpEntity> requestEntity = new HttpEntity<>(map, - headers); + headers); ResponseEntity responseEntity = restTemplate - .exchange(fileStoreUrl, POST, requestEntity, FileRefResponse.class); + .exchange(fileStoreUrl, POST, requestEntity, FileRefResponse.class); return responseEntity.getBody(); } catch (HttpClientErrorException e) diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/MTLSConfig.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/MTLSConfig.java index 17fae475..251fd571 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/MTLSConfig.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/MTLSConfig.java @@ -26,6 +26,15 @@ */ package org.alfresco.transformer.config; +import java.io.IOException; +import java.io.InputStream; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; + import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClients; @@ -46,18 +55,10 @@ import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; -import java.io.IOException; -import java.io.InputStream; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; - @Deprecated @Configuration -public class MTLSConfig { +public class MTLSConfig +{ @Value("${client.ssl.key-store:#{null}}") private Resource keyStoreResource; @@ -83,23 +84,26 @@ public class MTLSConfig { @Bean public RestTemplate restTemplate(SSLContextBuilder apacheSSLContextBuilder) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, UnrecoverableKeyException { - if(isTlsOrMtlsConfigured()) + if (isTlsOrMtlsConfigured()) { return createRestTemplateWithSslContext(apacheSSLContextBuilder); - } else { + } + else + { return new RestTemplate(); } } @Bean - public SSLContextBuilder apacheSSLContextBuilder() throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, UnrecoverableKeyException { + public SSLContextBuilder apacheSSLContextBuilder() throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, UnrecoverableKeyException + { SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); - if(isKeystoreConfigured()) + if (isKeystoreConfigured()) { KeyStore keyStore = getKeyStore(keyStoreType, keyStoreResource, keyStorePassword); sslContextBuilder.loadKeyMaterial(keyStore, keyStorePassword); } - if(isTruststoreConfigured()) + if (isTruststoreConfigured()) { sslContextBuilder .setKeyStoreType(trustStoreType) @@ -124,20 +128,20 @@ public class MTLSConfig { return keyStoreResource != null; } - private RestTemplate createRestTemplateWithSslContext(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException { - final SSLConnectionSocketFactoryBuilder sslConnectionSocketFactoryBuilder = - SSLConnectionSocketFactoryBuilder.create() - .setSslContext(sslContextBuilder.build()) - .setTlsVersions(TLS.V_1_2, TLS.V_1_3); - if (hostNameVerificationDisabled) { + private RestTemplate createRestTemplateWithSslContext(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException + { + final SSLConnectionSocketFactoryBuilder sslConnectionSocketFactoryBuilder = SSLConnectionSocketFactoryBuilder.create() + .setSslContext(sslContextBuilder.build()) + .setTlsVersions(TLS.V_1_2, TLS.V_1_3); + if (hostNameVerificationDisabled) + { sslConnectionSocketFactoryBuilder.setHostnameVerifier(NoopHostnameVerifier.INSTANCE); } final SSLConnectionSocketFactory sslConnectionSocketFactory = sslConnectionSocketFactoryBuilder.build(); - final Registry sslSocketFactoryRegistry = - RegistryBuilder. create() - .register("https", sslConnectionSocketFactory) - .build(); + final Registry sslSocketFactoryRegistry = RegistryBuilder. create() + .register("https", sslConnectionSocketFactory) + .build(); final PoolingHttpClientConnectionManager sslConnectionManager = new PoolingHttpClientConnectionManager(sslSocketFactoryRegistry); diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/WebApplicationConfig.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/WebApplicationConfig.java index d3af804a..8ad9db0f 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/WebApplicationConfig.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/config/WebApplicationConfig.java @@ -26,18 +26,19 @@ */ package org.alfresco.transformer.config; -import org.alfresco.transform.messages.TransformRequestValidator; -import org.alfresco.transform.registry.TransformServiceRegistry; -import org.alfresco.transform.common.TransformerDebug; -import org.alfresco.transformer.TransformInterceptor; -import org.alfresco.transformer.TransformRegistryImpl; -import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; +import org.alfresco.transform.common.TransformerDebug; +import org.alfresco.transform.messages.TransformRequestValidator; +import org.alfresco.transform.registry.TransformServiceRegistry; +import org.alfresco.transformer.TransformInterceptor; +import org.alfresco.transformer.TransformRegistryImpl; +import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. @@ -50,8 +51,8 @@ public class WebApplicationConfig implements WebMvcConfigurer public void addInterceptors(InterceptorRegistry registry) { registry - .addInterceptor(transformInterceptor()) - .addPathPatterns(ENDPOINT_TRANSFORM, "/live", "/ready"); + .addInterceptor(transformInterceptor()) + .addPathPatterns(ENDPOINT_TRANSFORM, "/live", "/ready"); } @Bean diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/AbstractCommandExecutor.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/AbstractCommandExecutor.java index 99835b19..32d914fc 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/AbstractCommandExecutor.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/AbstractCommandExecutor.java @@ -26,10 +26,11 @@ */ package org.alfresco.transformer.executors; -import static org.alfresco.transformer.executors.RuntimeExec.ExecutionResult; import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.alfresco.transformer.executors.RuntimeExec.ExecutionResult; + import java.io.File; import java.util.Map; @@ -81,7 +82,7 @@ public abstract class AbstractCommandExecutor implements CommandExecutor if (version.isEmpty()) { throw new TransformException(INTERNAL_SERVER_ERROR, - "Transformer version check failed to create any output"); + "Transformer version check failed to create any output"); } return version; } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/CommandExecutor.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/CommandExecutor.java index 35b23c04..05af28fd 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/CommandExecutor.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/CommandExecutor.java @@ -26,16 +26,16 @@ */ package org.alfresco.transformer.executors; -import org.alfresco.transformer.logging.LogEntry; - import java.io.File; import java.util.HashMap; import java.util.Map; +import org.alfresco.transformer.logging.LogEntry; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Basic interface for executing transformations via Shell commands + * Basic interface for executing transformations via Shell commands * * @author Cezar Leahu */ @@ -47,7 +47,7 @@ public interface CommandExecutor extends Transformer String version(); default void run(String options, File sourceFile, File targetFile, - Long timeout) + Long timeout) { LogEntry.setOptions(options); @@ -60,7 +60,7 @@ public interface CommandExecutor extends Transformer } default void run(String options, File sourceFile, String pageRange, File targetFile, - Long timeout) + Long timeout) { LogEntry.setOptions(pageRange + (pageRange.isEmpty() ? "" : " ") + options); diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/ExecParameterTokenizer.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/ExecParameterTokenizer.java index 8fc2a4dc..3b668347 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/ExecParameterTokenizer.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/ExecParameterTokenizer.java @@ -37,28 +37,21 @@ import java.util.StringTokenizer; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * DUPLICATED FROM *alfresco-core*. + * DUPLICATED FROM *alfresco-core*. * - * This class is used to tokenize strings used as parameters for {@link RuntimeExec} objects. - * Examples of such strings are as follows (ImageMagick-like parameters): - *
    - *
  • -font Helvetica -pointsize 50
  • - *
  • -font Helvetica -pointsize 50 -draw "circle 100,100 150,150"
  • - *
  • -font Helvetica -pointsize 50 -draw "gravity south fill black text 0,12 'CopyRight'"
  • - *
- * The first is the simple case which would be parsed into Strings as follows: - * "-font", "Helvetica", "-pointsize", "50" - *

- * The second is more complex in that it includes a quoted parameter, which would be parsed as a single String: - * "-font", "Helvetica", "-pointsize", "50", "circle 100,100 150,150" - * Note however that the quotation characters will be stripped from the token. - *

- * The third shows an example with embedded quotation marks, which would parse to: - * "-font", "Helvetica", "-pointsize", "50", "gravity south fill black text 0,12 'CopyRight'" - * In this case, the embedded quotation marks (which must be different from those surrounding the parameter) - * are preserved in the extracted token. - *

- * The class does not understand escaped quotes such as p1 p2 "a b c \"hello\" d" p4 + * This class is used to tokenize strings used as parameters for {@link RuntimeExec} objects. Examples of such strings are as follows (ImageMagick-like parameters): + *

    + *
  • -font Helvetica -pointsize 50
  • + *
  • -font Helvetica -pointsize 50 -draw "circle 100,100 150,150"
  • + *
  • -font Helvetica -pointsize 50 -draw "gravity south fill black text 0,12 'CopyRight'"
  • + *
+ * The first is the simple case which would be parsed into Strings as follows: "-font", "Helvetica", "-pointsize", "50" + *

+ * The second is more complex in that it includes a quoted parameter, which would be parsed as a single String: "-font", "Helvetica", "-pointsize", "50", "circle 100,100 150,150" Note however that the quotation characters will be stripped from the token. + *

+ * The third shows an example with embedded quotation marks, which would parse to: "-font", "Helvetica", "-pointsize", "50", "gravity south fill black text 0,12 'CopyRight'" In this case, the embedded quotation marks (which must be different from those surrounding the parameter) are preserved in the extracted token. + *

+ * The class does not understand escaped quotes such as p1 p2 "a b c \"hello\" d" p4 * * @author Neil Mc Erlean * @since 3.4.2 @@ -82,15 +75,12 @@ public class ExecParameterTokenizer } /** - * This method returns the tokens in a parameter string. - * Any tokens not contained within single or double quotes will be tokenized in the normal - * way i.e. by using whitespace separators and the standard StringTokenizer algorithm. - * Any tokens which are contained within single or double quotes will be returned as single - * String instances and will have their quote marks removed. + * This method returns the tokens in a parameter string. Any tokens not contained within single or double quotes will be tokenized in the normal way i.e. by using whitespace separators and the standard StringTokenizer algorithm. Any tokens which are contained within single or double quotes will be returned as single String instances and will have their quote marks removed. *

* See above for examples. * - * @throws NullPointerException if the string to be tokenized was null. + * @throws NullPointerException + * if the string to be tokenized was null. */ public List getAllTokens() { @@ -108,7 +98,7 @@ public class ExecParameterTokenizer { // Contains no quotes. for (StringTokenizer standardTokenizer = new StringTokenizer( - str); standardTokenizer.hasMoreTokens(); ) + str); standardTokenizer.hasMoreTokens();) { tokens.add(standardTokenizer.nextToken()); } @@ -119,7 +109,7 @@ public class ExecParameterTokenizer // So we need to identify the quoted regions within the string. List> quotedRegions = new ArrayList<>(); - for (Pair next = identifyNextQuotedRegion(str, 0); next != null; ) + for (Pair next = identifyNextQuotedRegion(str, 0); next != null;) { quotedRegions.add(next); next = identifyNextQuotedRegion(str, next.getSecond() + 1); @@ -140,12 +130,10 @@ public class ExecParameterTokenizer } /** - * The substrings will be a list of quoted and unquoted substrings. - * The unquoted ones need to be further tokenized in the normal way. - * The quoted ones must not be tokenized, but need their quotes stripped off. + * The substrings will be a list of quoted and unquoted substrings. The unquoted ones need to be further tokenized in the normal way. The quoted ones must not be tokenized, but need their quotes stripped off. */ private List getSubstrings(String str, - List> quotedRegionIndices) + List> quotedRegionIndices) { List result = new ArrayList<>(); @@ -156,10 +144,10 @@ public class ExecParameterTokenizer { int startIndexOfNextQuotedRegion = nextQuotedRegionIndices.getFirst() - 1; result.add(new UnquotedSubstring( - str.substring(cursorPosition, startIndexOfNextQuotedRegion))); + str.substring(cursorPosition, startIndexOfNextQuotedRegion))); } result.add(new QuotedSubstring(str.substring(nextQuotedRegionIndices.getFirst(), - nextQuotedRegionIndices.getSecond()))); + nextQuotedRegionIndices.getSecond()))); cursorPosition = nextQuotedRegionIndices.getSecond(); } @@ -203,12 +191,12 @@ public class ExecParameterTokenizer } private Pair findIndexOfClosingQuote(String str, int indexOfStartingQuote, - char quoteChar) + char quoteChar) { // So we know which type of quote char we're dealing with. Either ' or ". // Now we need to find the closing quote. int indexAfterClosingQuote = str.indexOf(quoteChar, - indexOfStartingQuote + 1) + 1; // + 1 to search after opening quote. + 1 to give result including closing quote. + indexOfStartingQuote + 1) + 1; // + 1 to search after opening quote. + 1 to give result including closing quote. if (indexAfterClosingQuote == 0) // -1 + 1 { @@ -300,8 +288,10 @@ public class ExecParameterTokenizer /** * Make a new one. * - * @param first The first member. - * @param second The second member. + * @param first + * The first member. + * @param second + * The second member. */ public Pair(F first, S second) { @@ -339,16 +329,20 @@ public class ExecParameterTokenizer this.second = second; } - @Override public boolean equals(Object o) + @Override + public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; Pair pair = (Pair) o; return Objects.equals(first, pair.first) && - Objects.equals(second, pair.second); + Objects.equals(second, pair.second); } - @Override public int hashCode() + @Override + public int hashCode() { return Objects.hash(first, second); } @@ -360,4 +354,3 @@ public class ExecParameterTokenizer } } } - diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/JavaExecutor.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/JavaExecutor.java index 5ba89ab7..da0cb85f 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/JavaExecutor.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/JavaExecutor.java @@ -31,7 +31,7 @@ import java.io.File; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Basic interface for executing transformations inside Java/JVM. + * Basic interface for executing transformations inside Java/JVM. * * @author Cezar Leahu * @author adavis diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/RuntimeExec.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/RuntimeExec.java index 3704e845..92387fda 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/RuntimeExec.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/RuntimeExec.java @@ -52,53 +52,52 @@ import org.slf4j.LoggerFactory; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * DUPLICATED FROM *alfresco-core*. + * DUPLICATED FROM *alfresco-core*. * - * This acts as a session similar to the java.lang.Process, but - * logs the system standard and error streams. - *

- * The bean can be configured to execute a command directly, or be given a map - * of commands keyed by the os.name Java system property. In this map, - * the default key that is used when no match is found is the - * {@link #KEY_OS_DEFAULT *} key. - *

- * Use the {@link #setProcessDirectory(String) processDirectory} property to change the default location - * from which the command executes. The process's environment can be configured using the - * {@link #setProcessProperties(Map) processProperties} property. - *

- * Commands may use placeholders, e.g. - *


+ *             This acts as a session similar to the java.lang.Process, but logs the system standard and error streams.
+ *             

+ * The bean can be configured to execute a command directly, or be given a map of commands keyed by the os.name Java system property. In this map, the default key that is used when no match is found is the {@link #KEY_OS_DEFAULT *} key. + *

+ * Use the {@link #setProcessDirectory(String) processDirectory} property to change the default location from which the command executes. The process's environment can be configured using the {@link #setProcessProperties(Map) processProperties} property. + *

+ * Commands may use placeholders, e.g. + * + *

+ * 
  *    find
  *    -name
  *    ${filename}
- * 
- * The filename property will be substituted for any supplied value prior to - * each execution of the command. Currently, no checks are made to get or check the - * properties contained within the command string. It is up to the client code to - * dynamically extract the properties required if the required properties are not - * known up front. - *

- * Sometimes, a variable may contain several arguments. . In this case, the arguments - * need to be tokenized using a standard StringTokenizer. To force tokenization - * of a value, use: - *


+ * 
+ *             
+ * + * The filename property will be substituted for any supplied value prior to each execution of the command. Currently, no checks are made to get or check the properties contained within the command string. It is up to the client code to dynamically extract the properties required if the required properties are not known up front. + *

+ * Sometimes, a variable may contain several arguments. . In this case, the arguments need to be tokenized using a standard StringTokenizer. To force tokenization of a value, use: + * + *

+ * 
  *    SPLIT:${userArgs}
- * 
- * You should not use this just to split up arguments that are known to require tokenization - * up front. The SPLIT: directive works for the entire argument and will not do anything - * if it is not at the beginning of the argument. Do not use SPLIT: to break up arguments - * that are fixed, so avoid doing this: - *

+ * 
+ *             
+ * + * You should not use this just to split up arguments that are known to require tokenization up front. The SPLIT: directive works for the entire argument and will not do anything if it is not at the beginning of the argument. Do not use SPLIT: to break up arguments that are fixed, so avoid doing this: + * + *
+ * 
  *    SPLIT:ls -lih
- * 
- * Instead, break the command up explicitly: - *

+ * 
+ *             
+ * + * Instead, break the command up explicitly: + * + *
+ * 
  *    ls
  *    -lih
- * 
+ *
+ *
* - * Tokenization of quoted parameter values is handled by ExecParameterTokenizer, which - * describes the support in more detail. + * Tokenization of quoted parameter values is handled by ExecParameterTokenizer, which describes the support in more detail. * * @author Derek Hulley */ @@ -128,7 +127,7 @@ public class RuntimeExec private final Timer timer = new Timer(true); /** - * Default constructor. Initialize this instance by setting individual properties. + * Default constructor. Initialize this instance by setting individual properties. */ public RuntimeExec() { @@ -156,15 +155,16 @@ public class RuntimeExec sb.append("\n"); } sb.append(" env props: ").append(Arrays.toString(processProperties)).append("\n") - .append(" dir: ").append(processDirectory).append("\n") - .append(" os: ").append(System.getProperty(KEY_OS_NAME)).append("\n"); + .append(" dir: ").append(processDirectory).append("\n") + .append(" os: ").append(System.getProperty(KEY_OS_NAME)).append("\n"); return sb.toString(); } /** * Set the command to execute regardless of operating system * - * @param command an array of strings representing the command (first entry) and arguments + * @param command + * an array of strings representing the command (first entry) and arguments * @since 3.0 */ public void setCommand(String[] command) @@ -173,11 +173,12 @@ public class RuntimeExec } /** - * Sets the assumed charset of OUT and ERR streams generated by the executed command. - * This defaults to the system default charset: {@link Charset#defaultCharset()}. + * Sets the assumed charset of OUT and ERR streams generated by the executed command. This defaults to the system default charset: {@link Charset#defaultCharset()}. * - * @param charsetCode a supported character set code - * @throws UnsupportedCharsetException if the characterset code is not recognised by Java + * @param charsetCode + * a supported character set code + * @throws UnsupportedCharsetException + * if the characterset code is not recognised by Java */ public void setCharset(String charsetCode) { @@ -185,14 +186,10 @@ public class RuntimeExec } /** - * Set whether to wait for completion of the command or not. If there is no wait for completion, - * then the return value of out and err buffers cannot be relied upon as the - * command may still be in progress. Failure is therefore not possible unless the calling thread - * waits for execution. + * Set whether to wait for completion of the command or not. If there is no wait for completion, then the return value of out and err buffers cannot be relied upon as the command may still be in progress. Failure is therefore not possible unless the calling thread waits for execution. * - * @param waitForCompletion true (default) is to wait for the command to exit, - * or false to just return an exit code of 0 and whatever - * output is available at that point. + * @param waitForCompletion + * true (default) is to wait for the command to exit, or false to just return an exit code of 0 and whatever output is available at that point. * @since 2.1 */ public void setWaitForCompletion(boolean waitForCompletion) @@ -201,26 +198,17 @@ public class RuntimeExec } /** - * Supply a choice of commands to execute based on a mapping from the os.name system - * property to the command to execute. The {@link #KEY_OS_DEFAULT *} key can be used - * to get a command where there is not direct match to the operating system key. + * Supply a choice of commands to execute based on a mapping from the os.name system property to the command to execute. The {@link #KEY_OS_DEFAULT *} key can be used to get a command where there is not direct match to the operating system key. *

- * Each command is an array of strings, the first of which represents the command and all subsequent - * entries in the array represent the arguments. All elements of the array will be checked for - * the presence of any substitution parameters (e.g. '{dir}'). The parameters can be set using the - * {@link #setDefaultProperties(Map) defaults} or by passing the substitution values into the - * {@link #execute(Map)} command. + * Each command is an array of strings, the first of which represents the command and all subsequent entries in the array represent the arguments. All elements of the array will be checked for the presence of any substitution parameters (e.g. '{dir}'). The parameters can be set using the {@link #setDefaultProperties(Map) defaults} or by passing the substitution values into the {@link #execute(Map)} command. *

- * If parameters passed may be multiple arguments, or if the values provided in the map are themselves - * collections of arguments (not recommended), then prefix the value with SPLIT: to ensure that - * the value is tokenized before being passed to the command. Any values that are not split, will be - * passed to the command as single arguments. For example:
+ * If parameters passed may be multiple arguments, or if the values provided in the map are themselves collections of arguments (not recommended), then prefix the value with SPLIT: to ensure that the value is tokenized before being passed to the command. Any values that are not split, will be passed to the command as single arguments. For example:
* 'SPLIT: dir . ..' becomes 'dir', '.' and '..'.
* 'SPLIT: dir ${path}' (if path is '. ..') becomes 'dir', '.' and '..'.
- * The splitting occurs post-subtitution. Where the arguments are known, it is advisable to avoid - * SPLIT:. + * The splitting occurs post-subtitution. Where the arguments are known, it is advisable to avoid SPLIT:. * - * @param commandsByOS a map of command string arrays, keyed by operating system names + * @param commandsByOS + * a map of command string arrays, keyed by operating system names * @see #setDefaultProperties(Map) * @since 3.0 */ @@ -235,7 +223,7 @@ public class RuntimeExec // go through the commands keys, looking for one that matches by regular expression matching for (String osName : commandsByOS.keySet()) { - // Ignore * options. It is dealt with later. + // Ignore * options. It is dealt with later. if (osName.equals(KEY_OS_DEFAULT)) { continue; @@ -257,26 +245,25 @@ public class RuntimeExec if (command == null) { throw new RuntimeException( - "No command found for OS " + serverOs + " or '" + KEY_OS_DEFAULT + "': \n" + - " commands: " + commandsByOS); + "No command found for OS " + serverOs + " or '" + KEY_OS_DEFAULT + "': \n" + + " commands: " + commandsByOS); } this.command = command; } /** - * Supply a choice of commands to execute based on a mapping from the os.name system - * property to the command to execute. The {@link #KEY_OS_DEFAULT *} key can be used - * to get a command where there is not direct match to the operating system key. + * Supply a choice of commands to execute based on a mapping from the os.name system property to the command to execute. The {@link #KEY_OS_DEFAULT *} key can be used to get a command where there is not direct match to the operating system key. * - * @param commandsByOS a map of command string keyed by operating system names + * @param commandsByOS + * a map of command string keyed by operating system names * @deprecated Use {@link #setCommandsAndArguments(Map)} */ public void setCommandMap(Map commandsByOS) { // This is deprecated, so issue a warning logger.warn( - "The bean RuntimeExec property 'commandMap' has been deprecated;" + - " use 'commandsAndArguments' instead. See https://issues.alfresco.com/jira/browse/ETHREEOH-579."); + "The bean RuntimeExec property 'commandMap' has been deprecated;" + + " use 'commandsAndArguments' instead. See https://issues.alfresco.com/jira/browse/ETHREEOH-579."); Map fixed = new LinkedHashMap<>(); for (Map.Entry entry : commandsByOS.entrySet()) { @@ -294,14 +281,12 @@ public class RuntimeExec } /** - * Set the default command-line properties to use when executing the command. - * These are properties that substitute variables defined in the command string itself. - * Properties supplied during execution will overwrite the default properties. + * Set the default command-line properties to use when executing the command. These are properties that substitute variables defined in the command string itself. Properties supplied during execution will overwrite the default properties. *

- * null properties will be treated as an empty string for substitution - * purposes. + * null properties will be treated as an empty string for substitution purposes. * - * @param defaultProperties property values + * @param defaultProperties + * property values */ public void setDefaultProperties(Map defaultProperties) { @@ -309,14 +294,12 @@ public class RuntimeExec } /** - * Set additional runtime properties (environment properties) that will used - * by the executing process. + * Set additional runtime properties (environment properties) that will used by the executing process. *

- * Any keys or properties that start and end with ${...} will be removed on the assumption - * that these are unset properties. null values are translated to empty strings. - * All keys and values are trimmed of leading and trailing whitespace. + * Any keys or properties that start and end with ${...} will be removed on the assumption that these are unset properties. null values are translated to empty strings. All keys and values are trimmed of leading and trailing whitespace. * - * @param processProperties Runtime process properties + * @param processProperties + * Runtime process properties * @see Runtime#exec(String, String[], java.io.File) */ public void setProcessProperties(Map processProperties) @@ -373,13 +356,12 @@ public class RuntimeExec } /** - * Adds a property to existed processProperties. - * Property should not be null or empty. - * If property with the same value already exists then no change is made. - * If property exists with a different value then old value is replaced with the new one. + * Adds a property to existed processProperties. Property should not be null or empty. If property with the same value already exists then no change is made. If property exists with a different value then old value is replaced with the new one. * - * @param name - property name - * @param value - property value + * @param name + * - property name + * @param value + * - property value */ public void setProcessProperty(String name, String value) { @@ -429,10 +411,10 @@ public class RuntimeExec /** * Set the runtime location from which the command is executed. *

- * If the value is an unsubsititued variable (${...}) then it is ignored. - * If the location is not visible at the time of setting, a warning is issued only. + * If the value is an unsubsititued variable (${...}) then it is ignored. If the location is not visible at the time of setting, a warning is issued only. * - * @param processDirectory the runtime location from which to execute the command + * @param processDirectory + * the runtime location from which to execute the command */ public void setProcessDirectory(String processDirectory) { @@ -446,17 +428,18 @@ public class RuntimeExec if (!this.processDirectory.exists()) { logger.warn( - "The runtime process directory is not visible when setting property " + - "'processDirectory': \n{}", this); + "The runtime process directory is not visible when setting property " + + "'processDirectory': \n{}", + this); } } } /** - * A comma or space separated list of values that, if returned by the executed command, - * indicate an error value. This defaults to "1, 2". + * A comma or space separated list of values that, if returned by the executed command, indicate an error value. This defaults to "1, 2". * - * @param errCodesStr the error codes for the execution + * @param errCodesStr + * the error codes for the execution */ public void setErrorCodes(String errCodesStr) { @@ -474,7 +457,7 @@ public class RuntimeExec catch (NumberFormatException e) { throw new RuntimeException( - "Property 'errorCodes' must be comma-separated list of integers: " + errCodesStr); + "Property 'errorCodes' must be comma-separated list of integers: " + errCodesStr); } } } @@ -492,9 +475,8 @@ public class RuntimeExec /** * Executes the statement that this instance was constructed with. * - * @param properties the properties that the command might be executed with. - * null properties will be treated as an empty string for substitution - * purposes. + * @param properties + * the properties that the command might be executed with. null properties will be treated as an empty string for substitution purposes. * @return Returns the full execution results */ public ExecutionResult execute(Map properties) @@ -503,15 +485,12 @@ public class RuntimeExec } /** - * Executes the statement that this instance was constructed with an optional - * timeout after which the command is asked to + * Executes the statement that this instance was constructed with an optional timeout after which the command is asked to * - * @param properties the properties that the command might be executed with. - * null properties will be treated as an empty string for substitution - * purposes. - * @param timeoutMs a timeout after which {@link Process#destroy()} is called. - * ignored if less than or equal to zero. Note this method does not guarantee - * to terminate the process (it is not a kill -9). + * @param properties + * the properties that the command might be executed with. null properties will be treated as an empty string for substitution purposes. + * @param timeoutMs + * a timeout after which {@link Process#destroy()} is called. ignored if less than or equal to zero. Note this method does not guarantee to terminate the process (it is not a kill -9). * @return Returns the full execution results */ public ExecutionResult execute(Map properties, final long timeoutMs) @@ -533,13 +512,12 @@ public class RuntimeExec // execute the command with full property replacement commandToExecute = getCommand(properties); final Process thisProcess = runtime.exec(commandToExecute, processProperties, - processDirectory); + processDirectory); process = thisProcess; if (timeoutMs > 0) { final String[] command = commandToExecute; - timer.schedule(new TimerTask() - { + timer.schedule(new TimerTask() { @Override public void run() { @@ -551,8 +529,8 @@ public class RuntimeExec catch (IllegalThreadStateException stillRunning) { logger.debug( - "Process has taken too long ({} seconds). Killing process {}", - timeoutMs / 1000, Arrays.deepToString(command)); + "Process has taken too long ({} seconds). Killing process {}", + timeoutMs / 1000, Arrays.deepToString(command)); } } }, timeoutMs); @@ -564,16 +542,16 @@ public class RuntimeExec String execOut = ""; String execErr = e.getMessage(); ExecutionResult result = new ExecutionResult(null, commandToExecute, errCodes, - defaultFailureExitValue, execOut, execErr); + defaultFailureExitValue, execOut, execErr); logFullEnvironmentDump(result); return result; } // create the stream gobblers InputStreamReaderThread stdOutGobbler = new InputStreamReaderThread( - process.getInputStream(), charset); + process.getInputStream(), charset); InputStreamReaderThread stdErrGobbler = new InputStreamReaderThread( - process.getErrorStream(), charset); + process.getErrorStream(), charset); // start gobbling stdOutGobbler.start(); @@ -608,7 +586,7 @@ public class RuntimeExec // construct the return value ExecutionResult result = new ExecutionResult(process, commandToExecute, errCodes, exitValue, - execOut, execErr); + execOut, execErr); // done logFullEnvironmentDump(result); @@ -651,12 +629,11 @@ public class RuntimeExec } logger.debug("Result: " + result.toString()); - // close output stream (connected to input stream of native subprocess) + // close output stream (connected to input stream of native subprocess) } /** - * @return Returns the command that will be executed if no additional properties - * were to be supplied + * @return Returns the command that will be executed if no additional properties were to be supplied */ public String[] getCommand() { @@ -666,12 +643,11 @@ public class RuntimeExec /** * Get the command that will be executed post substitution. *

- * null properties will be treated as an empty string for substitution - * purposes. + * null properties will be treated as an empty string for substitution purposes. * - * @param properties the properties that the command might be executed with - * @return Returns the command that will be executed should the additional properties - * be supplied + * @param properties + * the properties that the command might be executed with + * @return Returns the command that will be executed should the additional properties be supplied */ public String[] getCommand(Map properties) { @@ -720,7 +696,7 @@ public class RuntimeExec // There may be quoted arguments here (see ALF-7482) ExecParameterTokenizer quoteAwareTokenizer = new ExecParameterTokenizer( - unsplitAdjustedValue); + unsplitAdjustedValue); List tokens = quoteAwareTokenizer.getAllTokens(); adjustedCommandElements.addAll(tokens); } @@ -748,15 +724,16 @@ public class RuntimeExec private final String stdErr; /** - * @param process the process attached to Java - null is allowed + * @param process + * the process attached to Java - null is allowed */ private ExecutionResult( - final Process process, - final String[] command, - final Set errCodes, - final int exitValue, - final String stdOut, - final String stdErr) + final Process process, + final String[] command, + final Set errCodes, + final int exitValue, + final String stdOut, + final String stdErr) { this.process = process; this.command = command; @@ -774,20 +751,18 @@ public class RuntimeExec StringBuilder sb = new StringBuilder(128); sb.append("Execution result: \n") - .append(" os: ").append(System.getProperty(KEY_OS_NAME)).append("\n") - .append(" command: "); + .append(" os: ").append(System.getProperty(KEY_OS_NAME)).append("\n") + .append(" command: "); appendCommand(sb, command).append("\n") - .append(" succeeded: ").append(getSuccess()).append("\n") - .append(" exit code: ").append(exitValue).append("\n") - .append(" out: ").append(out).append("\n") - .append(" err: ").append(err); + .append(" succeeded: ").append(getSuccess()).append("\n") + .append(" exit code: ").append(exitValue).append("\n") + .append(" out: ").append(out).append("\n") + .append(" err: ").append(err); return sb.toString(); } /** - * Appends the command in a form that make running from the command line simpler. - * It is not a real attempt at making a command given all the operating system - * and shell options, but makes copy, paste and edit a bit simpler. + * Appends the command in a form that make running from the command line simpler. It is not a real attempt at making a command given all the operating system and shell options, but makes copy, paste and edit a bit simpler. */ private StringBuilder appendCommand(StringBuilder sb, String[] command) { @@ -823,10 +798,7 @@ public class RuntimeExec } /** - * A helper method to force a kill of the process that generated this result. This is - * useful in cases where the process started is not expected to exit, or doesn't exit - * quickly. If the {@linkplain RuntimeExec#setWaitForCompletion(boolean) "wait for completion"} - * flag is false then the process may still be running when this result is returned. + * A helper method to force a kill of the process that generated this result. This is useful in cases where the process started is not expected to exit, or doesn't exit quickly. If the {@linkplain RuntimeExec#setWaitForCompletion(boolean) "wait for completion"} flag is false then the process may still be running when this result is returned. * * @return true if the process was killed, otherwise false */ @@ -849,7 +821,8 @@ public class RuntimeExec } /** - * @param exitValue the command exit value + * @param exitValue + * the command exit value * @return Returns true if the code is a listed failure code * @see #setErrorCodes(String) */ @@ -859,8 +832,7 @@ public class RuntimeExec } /** - * @return Returns true if the command was deemed to be successful according to the - * failure codes returned by the execution. + * @return Returns true if the command was deemed to be successful according to the failure codes returned by the execution. */ public boolean getSuccess() { @@ -884,8 +856,7 @@ public class RuntimeExec } /** - * Gobbles an InputStream and writes it into a - * StringBuffer + * Gobbles an InputStream and writes it into a StringBuffer *

* The reading of the input stream is buffered. */ @@ -893,12 +864,12 @@ public class RuntimeExec { private final InputStream is; private final Charset charset; - private final StringBuffer buffer; // we require the synchronization + private final StringBuffer buffer; // we require the synchronization private boolean completed; /** - * @param is an input stream to read - it will be wrapped in a buffer - * for reading + * @param is + * an input stream to read - it will be wrapped in a buffer for reading */ public InputStreamReaderThread(InputStream is, Charset charset) { @@ -940,7 +911,7 @@ public class RuntimeExec // The thread has finished consuming the stream completed = true; // Notify waiters - this.notifyAll(); // Note: Method is synchronized + this.notifyAll(); // Note: Method is synchronized } } @@ -959,13 +930,13 @@ public class RuntimeExec this.wait(1000L); // 200 ms } catch (InterruptedException ignore) - { - } + {} } } /** - * @param msg the message to add to the buffer + * @param msg + * the message to add to the buffer */ public void addToBuffer(String msg) { @@ -986,4 +957,3 @@ public class RuntimeExec } } } - diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/Transformer.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/Transformer.java index 935f51cd..f007c994 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/Transformer.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/executors/Transformer.java @@ -27,21 +27,22 @@ package org.alfresco.transformer.executors; * #L% */ -import org.alfresco.transform.exceptions.TransformException; - -import java.io.File; -import java.util.Map; +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; import static org.alfresco.transform.common.Mimetype.MIMETYPE_METADATA_EMBED; import static org.alfresco.transform.common.Mimetype.MIMETYPE_METADATA_EXTRACT; import static org.alfresco.transformer.util.RequestParamMap.TRANSFORM_NAME_PARAMETER; -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +import java.io.File; +import java.util.Map; + +import org.alfresco.transform.exceptions.TransformException; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Basic interface for executing transforms and metadata extract or embed actions. + * Basic interface for executing transforms and metadata extract or embed actions. * * @author adavis */ @@ -55,14 +56,15 @@ public interface Transformer String getTransformerId(); default void transform(String sourceMimetype, String targetMimetype, Map transformOptions, - File sourceFile, File targetFile) throws TransformException { + File sourceFile, File targetFile) throws TransformException + { final String transformName = transformOptions.remove(TRANSFORM_NAME_PARAMETER); transformExtractOrEmbed(transformName, sourceMimetype, targetMimetype, transformOptions, sourceFile, targetFile); } default void transformExtractOrEmbed(String transformName, String sourceMimetype, String targetMimetype, - Map transformOptions, - File sourceFile, File targetFile) throws TransformException + Map transformOptions, + File sourceFile, File targetFile) throws TransformException { try { @@ -109,25 +111,20 @@ public interface Transformer } default void transform(String transformName, String sourceMimetype, String targetMimetype, - Map transformOptions, - File sourceFile, File targetFile) throws Exception - { - } + Map transformOptions, + File sourceFile, File targetFile) throws Exception + {} default void extractMetadata(String transformName, String sourceMimetype, String targetMimetype, - Map transformOptions, - File sourceFile, File targetFile) throws Exception - { - } + Map transformOptions, + File sourceFile, File targetFile) throws Exception + {} /** - * @deprecated The content repository has no non test embed metadata implementations. - * This code exists in case there are custom implementations, that need to be converted to T-Engines. - * It is simply a copy and paste from the content repository and has received limited testing. + * @deprecated The content repository has no non test embed metadata implementations. This code exists in case there are custom implementations, that need to be converted to T-Engines. It is simply a copy and paste from the content repository and has received limited testing. */ default void embedMetadata(String transformName, String sourceMimetype, String targetMimetype, - Map transformOptions, - File sourceFile, File targetFile) throws Exception - { - } + Map transformOptions, + File sourceFile, File targetFile) throws Exception + {} } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/fs/FileManager.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/fs/FileManager.java index dbc9c50e..2b4c9ed3 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/fs/FileManager.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/fs/FileManager.java @@ -39,11 +39,8 @@ import java.net.MalformedURLException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Arrays; - import jakarta.servlet.http.HttpServletRequest; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transformer.logging.LogEntry; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.http.HttpHeaders; @@ -52,6 +49,9 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.util.UriUtils; +import org.alfresco.transform.exceptions.TransformException; +import org.alfresco.transformer.logging.LogEntry; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. */ @@ -66,10 +66,11 @@ public class FileManager * Returns a File to be used to store the result of a transformation. * * @param request - * @param filename The targetFilename supplied in the request. Only the filename if a path is used as part of the - * temporary filename. + * @param filename + * The targetFilename supplied in the request. Only the filename if a path is used as part of the temporary filename. * @return a temporary File. - * @throws TransformException if there was no target filename. + * @throws TransformException + * if there was no target filename. */ public static File createTargetFile(HttpServletRequest request, String filename) { @@ -96,9 +97,11 @@ public class FileManager /** * Checks the filename is okay to uses in a temporary file name. * - * @param filename or path to be checked. + * @param filename + * or path to be checked. * @return the filename part of the supplied filename if it was a path. - * @throws TransformException if there was no target filename. + * @throws TransformException + * if there was no target filename. */ private static String checkFilename(boolean source, String filename) { @@ -117,7 +120,7 @@ public class FileManager try { Files.copy(multipartFile.getInputStream(), file.toPath(), - StandardCopyOption.REPLACE_EXISTING); + StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { @@ -149,13 +152,13 @@ public class FileManager else { throw new TransformException(INTERNAL_SERVER_ERROR, - "Could not read the target file: " + file.getPath()); + "Could not read the target file: " + file.getPath()); } } catch (MalformedURLException e) { throw new TransformException(INTERNAL_SERVER_ERROR, - "The target filename was malformed: " + file.getPath(), e); + "The target filename was malformed: " + file.getPath(), e); } } @@ -167,10 +170,10 @@ public class FileManager { String[] strings = contentDisposition.split("; *"); filename = Arrays.stream(strings) - .filter(s -> s.startsWith(FILENAME)) - .findFirst() - .map(s -> s.substring(FILENAME.length())) - .orElse(""); + .filter(s -> s.startsWith(FILENAME)) + .findFirst() + .map(s -> s.substring(FILENAME.length())) + .orElse(""); } return filename; } @@ -178,8 +181,10 @@ public class FileManager /** * Returns the file name for the target file * - * @param fileName Desired file name - * @param targetExtension File extension + * @param fileName + * Desired file name + * @param targetExtension + * File extension * @return Target file name */ public static String createTargetFileName(final String fileName, final String targetExtension) @@ -199,16 +204,18 @@ public class FileManager } return sourceFilename.substring(0, sourceFilename.length() - ext.length() - 1) + - '.' + targetExtension; + '.' + targetExtension; } /** * Returns a File that holds the source content for a transformation. * * @param request - * @param multipartFile from the request + * @param multipartFile + * from the request * @return a temporary File. - * @throws TransformException if there was no source filename. + * @throws TransformException + * if there was no source filename. */ public static File createSourceFile(HttpServletRequest request, MultipartFile multipartFile) { @@ -231,13 +238,12 @@ public class FileManager } } - public static ResponseEntity createAttachment(String targetFilename, File - targetFile) + public static ResponseEntity createAttachment(String targetFilename, File targetFile) { Resource targetResource = load(targetFile); targetFilename = UriUtils.encodePath(getFilename(targetFilename), "UTF-8"); return ResponseEntity.ok().header(CONTENT_DISPOSITION, - "attachment; filename*=UTF-8''" + targetFilename).body(targetResource); + "attachment; filename*=UTF-8''" + targetFilename).body(targetResource); } /** @@ -255,8 +261,9 @@ public class FileManager catch (IOException e) { throw new RuntimeException( - "Failed to created temp file: \n prefix: " + prefix + - "\n suffix: " + suffix + "\n directory: " + directory, e); + "Failed to created temp file: \n prefix: " + prefix + + "\n suffix: " + suffix + "\n directory: " + directory, + e); } } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/logging/LogEntry.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/logging/LogEntry.java index 057305eb..6e8b42ea 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/logging/LogEntry.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/logging/LogEntry.java @@ -27,6 +27,7 @@ package org.alfresco.transformer.logging; import static java.lang.Math.max; + import static org.springframework.http.HttpStatus.OK; import java.text.SimpleDateFormat; @@ -42,10 +43,7 @@ import org.slf4j.LoggerFactory; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Provides setter and getter methods to allow the current Thread to set various log properties and for these - * values to be retrieved. The {@link #complete()} method should be called at the end of a request to flush the - * current entry to an internal log Collection of the latest entries. The {@link #getLog()} method is used to obtain - * access to this collection. + * Provides setter and getter methods to allow the current Thread to set various log properties and for these values to be retrieved. The {@link #complete()} method should be called at the end of a request to flush the current entry to an internal log Collection of the latest entries. The {@link #getLog()} method is used to obtain access to this collection. */ @Deprecated public final class LogEntry @@ -204,8 +202,9 @@ public final class LogEntry if (logEntry.statusCode == OK.value()) { logEntry.durationStreamOut = System.currentTimeMillis() - logEntry.start - - logEntry.durationStreamIn - max(logEntry.durationTransform, - 0) - max(logEntry.durationDelay, 0); + logEntry.durationStreamIn - max(logEntry.durationTransform, + 0) + - max(logEntry.durationDelay, 0); } currentLogEntry.remove(); @@ -233,18 +232,20 @@ public final class LogEntry public String getDuration() { long duration = durationStreamIn + max(durationTransform, 0) + max(durationDelay, 0) + max( - durationStreamOut, 0); + durationStreamOut, 0); return duration <= 5 - ? "" - : time(duration) + - " (" + - (time(durationStreamIn) + ' ' + - time(durationTransform) + ' ' + - (durationDelay > 0 - ? time(durationDelay) + ' ' + (durationStreamOut < 0 ? "-" : time( - durationStreamOut)) - : time(durationStreamOut))).trim() + - ")"; + ? "" + : time(duration) + + " (" + + (time(durationStreamIn) + ' ' + + time(durationTransform) + ' ' + + (durationDelay > 0 + ? time(durationDelay) + ' ' + (durationStreamOut < 0 ? "-" + : time( + durationStreamOut)) + : time(durationStreamOut))).trim() + + + ")"; } public String getSource() @@ -279,17 +280,19 @@ public final class LogEntry private String time(long ms) { - return ms == -1 ? "" : size(ms, "1ms", - new String[]{"ms", "s", "min", "hr"}, - new long[]{1000, 60 * 1000, 60 * 60 * 1000, Long.MAX_VALUE}); + return ms == -1 ? "" + : size(ms, "1ms", + new String[]{"ms", "s", "min", "hr"}, + new long[]{1000, 60 * 1000, 60 * 60 * 1000, Long.MAX_VALUE}); } private String size(long size) { // TODO fix numeric overflow in TB expression - return size == -1 ? "" : size(size, "1 byte", - new String[]{"bytes", " KB", " MB", " GB", " TB"}, - new long[]{1024, 1024 * 1024, 1024 * 1024 * 1024, 1024 * 1024 * 1024 * 1024, Long.MAX_VALUE}); + return size == -1 ? "" + : size(size, "1 byte", + new String[]{"bytes", " KB", " MB", " GB", " TB"}, + new long[]{1024, 1024 * 1024, 1024 * 1024 * 1024, 1024 * 1024 * 1024 * 1024, Long.MAX_VALUE}); } private String size(long size, String singleValue, String[] units, long[] dividers) diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/logging/StandardMessages.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/logging/StandardMessages.java index 9cbcda23..b1c53c89 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/logging/StandardMessages.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/logging/StandardMessages.java @@ -32,8 +32,7 @@ package org.alfresco.transformer.logging; @Deprecated public interface StandardMessages { - String LICENCE = - "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"; + String LICENCE = "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"; } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/MessagingConfig.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/MessagingConfig.java index cb401b16..9a73d0d8 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/MessagingConfig.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/MessagingConfig.java @@ -29,7 +29,6 @@ package org.alfresco.transformer.messaging; import jakarta.jms.ConnectionFactory; import jakarta.jms.Queue; -import org.alfresco.transform.messages.TransformRequestValidator; import org.apache.activemq.command.ActiveMQQueue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,14 +44,14 @@ import org.springframework.lang.NonNull; import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory; import org.springframework.transaction.PlatformTransactionManager; +import org.alfresco.transform.messages.TransformRequestValidator; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * JMS and messaging configuration for the T-Engines. Contains the basic config in order to have the - * T-Engine able to read from queues and send a reply back. + * JMS and messaging configuration for the T-Engines. Contains the basic config in order to have the T-Engine able to read from queues and send a reply back. * - * @author Lucian Tuca - * created on 18/12/2018 + * @author Lucian Tuca created on 18/12/2018 */ @Deprecated @Configuration @@ -79,8 +78,8 @@ public class MessagingConfig implements JmsListenerConfigurer @Bean @ConditionalOnProperty(name = "activemq.url") public DefaultJmsListenerContainerFactory jmsListenerContainerFactory( - final ConnectionFactory connectionFactory, - final TransformMessageConverter transformMessageConverter) + final ConnectionFactory connectionFactory, + final TransformMessageConverter transformMessageConverter) { final DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); @@ -102,10 +101,8 @@ public class MessagingConfig implements JmsListenerConfigurer @Bean @ConditionalOnProperty(name = "activemq.url") public Queue engineRequestQueue( - @Value("${queue.engineRequestQueue}") String engineRequestQueueValue) + @Value("${queue.engineRequestQueue}") String engineRequestQueueValue) { return new ActiveMQQueue(engineRequestQueueValue); } } - - diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/MessagingInfo.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/MessagingInfo.java index 41242e89..37f638f4 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/MessagingInfo.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/MessagingInfo.java @@ -36,7 +36,7 @@ import org.springframework.context.annotation.Configuration; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Prints JMS status information at application startup. + * Prints JMS status information at application startup. * * @author Cezar Leahu */ @@ -58,7 +58,7 @@ public class MessagingInfo // Note: as per application.yaml the broker url is appended with "?jms.watchTopicAdvisories=false". If this needs to be fully // overridden then it would require explicitly setting both "spring.activemq.broker-url" *and* "activemq.url" (latter to non-false value). - if ((activemqUrl != null) && (! activemqUrl.equals("false"))) + if ((activemqUrl != null) && (!activemqUrl.equals("false"))) { logger.info("JMS client is ENABLED - ACTIVEMQ_URL ='{}'", activemqUrl); } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/TransformMessageConverter.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/TransformMessageConverter.java index fc135b0d..6e89a72f 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/TransformMessageConverter.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/TransformMessageConverter.java @@ -27,11 +27,13 @@ package org.alfresco.transformer.messaging; +import jakarta.jms.JMSException; +import jakarta.jms.Message; +import jakarta.jms.Session; + import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.type.TypeFactory; import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; import org.springframework.jms.support.converter.MappingJackson2MessageConverter; import org.springframework.jms.support.converter.MessageConversionException; import org.springframework.jms.support.converter.MessageConverter; @@ -39,15 +41,13 @@ import org.springframework.jms.support.converter.MessageType; import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; -import jakarta.jms.JMSException; -import jakarta.jms.Message; -import jakarta.jms.Session; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * TODO: Duplicated from the Router - * Custom wrapper over MappingJackson2MessageConverter for T-Request/T-Reply objects. + * TODO: Duplicated from the Router Custom wrapper over MappingJackson2MessageConverter for T-Request/T-Reply objects. * * @author Cezar Leahu */ @@ -56,16 +56,13 @@ import jakarta.jms.Session; public class TransformMessageConverter implements MessageConverter { private static final MappingJackson2MessageConverter converter; - private static final JavaType TRANSFORM_REQUEST_TYPE = - TypeFactory.defaultInstance().constructType(TransformRequest.class); + private static final JavaType TRANSFORM_REQUEST_TYPE = TypeFactory.defaultInstance().constructType(TransformRequest.class); static { - converter = new MappingJackson2MessageConverter() - { + converter = new MappingJackson2MessageConverter() { @Override - @NonNull - protected JavaType getJavaTypeForMessage(final Message message) throws JMSException + @NonNull protected JavaType getJavaTypeForMessage(final Message message) throws JMSException { if (message.getStringProperty("_type") == null) { @@ -77,23 +74,20 @@ public class TransformMessageConverter implements MessageConverter converter.setTargetType(MessageType.BYTES); converter.setTypeIdPropertyName("_type"); converter.setTypeIdMappings(ImmutableMap.of( - TransformRequest.class.getName(), TransformRequest.class, - TransformReply.class.getName(), TransformReply.class) - ); + TransformRequest.class.getName(), TransformRequest.class, + TransformReply.class.getName(), TransformReply.class)); } @Override - @NonNull - public Message toMessage( - @NonNull final Object object, - @NonNull final Session session) throws JMSException, MessageConversionException + @NonNull public Message toMessage( + @NonNull final Object object, + @NonNull final Session session) throws JMSException, MessageConversionException { return converter.toMessage(object, session); } @Override - @NonNull - public Object fromMessage(@NonNull final Message message) throws JMSException + @NonNull public Object fromMessage(@NonNull final Message message) throws JMSException { return converter.fromMessage(message); } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/TransformReplySender.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/TransformReplySender.java index 47078157..031b089b 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/TransformReplySender.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/messaging/TransformReplySender.java @@ -28,20 +28,20 @@ package org.alfresco.transformer.messaging; import jakarta.jms.Destination; -import org.alfresco.transform.client.model.TransformReply; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Component; +import org.alfresco.transform.client.model.TransformReply; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * TODO: Duplicated from the Router - * TransformReplySender Bean - *

- * JMS message sender/publisher + * TODO: Duplicated from the Router TransformReplySender Bean + *

+ * JMS message sender/publisher * * @author Cezar Leahu */ @@ -65,7 +65,7 @@ public class TransformReplySender { try { - //jmsTemplate.setSessionTransacted(true); // do we need this? + // jmsTemplate.setSessionTransacted(true); // do we need this? jmsTemplate.convertAndSend(destination, reply, m -> { m.setJMSCorrelationID(correlationId); return m; diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/metadataExtractors/AbstractMetadataExtractor.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/metadataExtractors/AbstractMetadataExtractor.java index 2c7cf216..26e319b5 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/metadataExtractors/AbstractMetadataExtractor.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/metadataExtractors/AbstractMetadataExtractor.java @@ -26,11 +26,6 @@ */ package org.alfresco.transformer.metadataExtractors; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.slf4j.Logger; - import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -48,46 +43,39 @@ import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Helper methods for metadata extract and embed. - *

- * Much of the code is based on AbstractMappingMetadataExtracter from the - * content repository. The code has been simplified to only set up mapping one way. - *

- * If a transform specifies that it can convert from {@code ""} to {@code "alfresco-metadata-extract"} - * (specified in the {@code engine_config.json}), it is indicating that it can extract metadata from {@code }. + * Helper methods for metadata extract and embed. + *

+ * Much of the code is based on AbstractMappingMetadataExtracter from the content repository. The code has been simplified to only set up mapping one way. + *

+ * If a transform specifies that it can convert from {@code ""} to {@code "alfresco-metadata-extract"} (specified in the {@code engine_config.json}), it is indicating that it can extract metadata from {@code }. * - * The transform results in a Map of extracted properties encoded as json being returned to the content repository. - *

    - *
  • The content repository will use a transform in preference to any metadata extractors it might have defined - * locally for the same MIMETYPE.
  • - *
  • The T-Engine's Controller class will call a method in a class that extends {@link AbstractMetadataExtractor} - * based on the source and target mediatypes in the normal way.
  • - *
  • The method extracts ALL available metadata is extracted from the document and then calls - * {@link #mapMetadataAndWrite(File, Map, Map)}.
  • - *
  • Selected values from the available metadata are mapped into content repository property names and values, - * depending on what is defined in a {@code "_metadata_extract.properties"} file.
  • - *
  • The selected values are set back to the content repository as a JSON representation of a Map, where the values - * are applied to the source node.
  • - *
- * To support the same functionality as metadata extractors configured inside the content repository, - * extra key value pairs may be returned from {@link #extractMetadata}. These are: - *
    - *
  • {@code "sys:overwritePolicy"} which can specify the - * {@code org.alfresco.repo.content.metadata.MetadataExtracter.OverwritePolicy} name. Defaults to "PRAGMATIC".
  • - *
  • {@code "sys:enableStringTagging"} if {@code "true"} finds or creates tags for each string mapped to - * {@code cm:taggable}. Defaults to {@code "false"} to ignore mapping strings to tags.
  • - *
  • {@code "sys:carryAspectProperties"}
  • - *
  • {@code "sys:stringTaggingSeparators"}
  • - *
+ * The transform results in a Map of extracted properties encoded as json being returned to the content repository. + *
    + *
  • The content repository will use a transform in preference to any metadata extractors it might have defined locally for the same MIMETYPE.
  • + *
  • The T-Engine's Controller class will call a method in a class that extends {@link AbstractMetadataExtractor} based on the source and target mediatypes in the normal way.
  • + *
  • The method extracts ALL available metadata is extracted from the document and then calls {@link #mapMetadataAndWrite(File, Map, Map)}.
  • + *
  • Selected values from the available metadata are mapped into content repository property names and values, depending on what is defined in a {@code "_metadata_extract.properties"} file.
  • + *
  • The selected values are set back to the content repository as a JSON representation of a Map, where the values are applied to the source node.
  • + *
+ * To support the same functionality as metadata extractors configured inside the content repository, extra key value pairs may be returned from {@link #extractMetadata}. These are: + *
    + *
  • {@code "sys:overwritePolicy"} which can specify the {@code org.alfresco.repo.content.metadata.MetadataExtracter.OverwritePolicy} name. Defaults to "PRAGMATIC".
  • + *
  • {@code "sys:enableStringTagging"} if {@code "true"} finds or creates tags for each string mapped to {@code cm:taggable}. Defaults to {@code "false"} to ignore mapping strings to tags.
  • + *
  • {@code "sys:carryAspectProperties"}
  • + *
  • {@code "sys:stringTaggingSeparators"}
  • + *
* - * If a transform specifies that it can convert from {@code ""} to {@code "alfresco-metadata-embed"}, it is - * indicating that it can embed metadata in {@code }. + * If a transform specifies that it can convert from {@code ""} to {@code "alfresco-metadata-embed"}, it is indicating that it can embed metadata in {@code }. * - * The transform results in a new version of supplied source file that contains the metadata supplied in the transform - * options. + * The transform results in a new version of supplied source file that contains the metadata supplied in the transform options. * * @author Jesper Steen Møller * @author Derek Hulley @@ -136,10 +124,10 @@ public abstract class AbstractMetadataExtractor } public abstract Map extractMetadata(String sourceMimetype, Map transformOptions, - File sourceFile) throws Exception; + File sourceFile) throws Exception; public void embedMetadata(String sourceMimetype, String targetMimetype, Map transformOptions, - File sourceFile, File targetFile) throws Exception + File sourceFile, File targetFile) throws Exception { // Default nothing, as embedding is not supported in most cases } @@ -208,9 +196,8 @@ public abstract class AbstractMetadataExtractor /** * Based on AbstractMappingMetadataExtracter#getDefaultMapping. * - * This method provides a mapping of where to store the values extracted from the documents. The list of - * properties need not include all metadata values extracted from the document. This mapping should be - * defined in a file based on the class name: {@code "_metadata_extract.properties"} + * This method provides a mapping of where to store the values extracted from the documents. The list of properties need not include all metadata values extracted from the document. This mapping should be defined in a file based on the class name: {@code "_metadata_extract.properties"} + * * @return Returns a static mapping. It may not be null. */ private Map> buildExtractMapping() @@ -219,7 +206,7 @@ public abstract class AbstractMetadataExtractor Properties properties = readProperties(filename); if (properties == null) { - logger.error("Failed to read "+filename); + logger.error("Failed to read " + filename); } Map namespacesByPrefix = getNamespaces(properties); @@ -260,13 +247,10 @@ public abstract class AbstractMetadataExtractor /** * Based on AbstractMappingMetadataExtracter#getDefaultEmbedMapping. * - * This method provides a mapping of model properties that should be embedded in the content. The list of - * properties need not include all properties. This mapping should be defined in a file based on the class - * name: {@code "_metadata_embed.properties"} + * This method provides a mapping of model properties that should be embedded in the content. The list of properties need not include all properties. This mapping should be defined in a file based on the class name: {@code "_metadata_embed.properties"} *

- * If no {@code "_metadata_embed.properties"} file is found, a reverse of the - * {@code "_metadata_extract.properties"} will be assumed. A last win approach will be used for handling - * duplicates. + * If no {@code "_metadata_embed.properties"} file is found, a reverse of the {@code "_metadata_extract.properties"} will be assumed. A last win approach will be used for handling duplicates. + * * @return Returns a static mapping. It may not be null. */ private Map> buildEmbedMapping() @@ -306,7 +290,8 @@ public abstract class AbstractMetadataExtractor modelProperty = getQNameString(namespacesByPrefix, entry, modelProperty, EMBED); String[] metadataKeysArray = metadataKeysString.split(","); Set metadataKeys = new HashSet(metadataKeysArray.length); - for (String metadataKey : metadataKeysArray) { + for (String metadataKey : metadataKeysArray) + { metadataKeys.add(metadataKey.trim()); } // Create the entry @@ -367,8 +352,7 @@ public abstract class AbstractMetadataExtractor } } catch (IOException ignore) - { - } + {} return properties; } @@ -410,21 +394,21 @@ public abstract class AbstractMetadataExtractor } /** - * Adds a value to the map, conserving null values. Values are converted to null if: + * Adds a value to the map, conserving null values. Values are converted to null if: *

    - *
  • it is an empty string value after trimming
  • - *
  • it is an empty collection
  • - *
  • it is an empty array
  • + *
  • it is an empty string value after trimming
  • + *
  • it is an empty collection
  • + *
  • it is an empty array
  • *
- * String values are trimmed before being put into the map. - * Otherwise, it is up to the extracter to ensure that the value is a Serializable. - * It is not appropriate to implicitly convert values in order to make them Serializable - * - the best conversion method will depend on the value's specific meaning. + * String values are trimmed before being put into the map. Otherwise, it is up to the extracter to ensure that the value is a Serializable. It is not appropriate to implicitly convert values in order to make them Serializable - the best conversion method will depend on the value's specific meaning. * - * @param key the destination key - * @param value the serializable value - * @param destination the map to put values into - * @return Returns true if set, otherwise false + * @param key + * the destination key + * @param value + * the serializable value + * @param destination + * the map to put values into + * @return Returns true if set, otherwise false */ // Copied from the content repository's AbstractMappingMetadataExtracter. protected boolean putRawValue(String key, Serializable value, Map destination) @@ -471,11 +455,10 @@ public abstract class AbstractMetadataExtractor } /** - * The {@code transformOptions} may contain a replacement set of mappings. These will be used in place of the - * default mappings from read from file if supplied. + * The {@code transformOptions} may contain a replacement set of mappings. These will be used in place of the default mappings from read from file if supplied. */ public void extractMetadata(String sourceMimetype, Map transformOptions, File sourceFile, - File targetFile) throws Exception + File targetFile) throws Exception { Map> mapping = getExtractMappingFromOptions(transformOptions, defaultExtractMapping); @@ -493,8 +476,7 @@ public abstract class AbstractMetadataExtractor } } - private Map> getExtractMappingFromOptions(Map transformOptions, Map> defaultExtractMapping) + private Map> getExtractMappingFromOptions(Map transformOptions, Map> defaultExtractMapping) { String extractMappingOption = transformOptions.get(EXTRACT_MAPPING); if (extractMappingOption != null) @@ -506,15 +488,14 @@ public abstract class AbstractMetadataExtractor } catch (JsonProcessingException e) { - throw new IllegalArgumentException("Failed to read "+ EXTRACT_MAPPING +" from request", e); + throw new IllegalArgumentException("Failed to read " + EXTRACT_MAPPING + " from request", e); } } return defaultExtractMapping; } /** - * @deprecated use {@link #extractMetadata(String, Map, File, File)} rather than calling this method. - * By default call the overloaded method with the default {@code extractMapping}. + * @deprecated use {@link #extractMetadata(String, Map, File, File)} rather than calling this method. By default call the overloaded method with the default {@code extractMapping}. */ @Deprecated public void mapMetadataAndWrite(File targetFile, Map metadata) throws IOException @@ -523,12 +504,12 @@ public abstract class AbstractMetadataExtractor } public void mapMetadataAndWrite(File targetFile, Map metadata, - Map> extractMapping) throws IOException + Map> extractMapping) throws IOException { if (logger.isDebugEnabled()) { logger.debug("Raw metadata:"); - metadata.forEach((k,v) -> logger.debug(" "+k+"="+v)); + metadata.forEach((k, v) -> logger.debug(" " + k + "=" + v)); } metadata = mapRawToSystem(metadata, extractMapping); @@ -538,12 +519,14 @@ public abstract class AbstractMetadataExtractor /** * Based on AbstractMappingMetadataExtracter#mapRawToSystem. * - * @param rawMetadata Metadata keyed by document properties - * @param extractMapping Mapping between document ans system properties - * @return Returns the metadata keyed by the system properties + * @param rawMetadata + * Metadata keyed by document properties + * @param extractMapping + * Mapping between document ans system properties + * @return Returns the metadata keyed by the system properties */ private Map mapRawToSystem(Map rawMetadata, - Map> extractMapping) + Map> extractMapping) { boolean debugEnabled = logger.isDebugEnabled(); if (debugEnabled) @@ -571,12 +554,12 @@ public abstract class AbstractMetadataExtractor continue; } - Set systemQNames = extractMapping.get(documentKey); + Set systemQNames = extractMapping.get(documentKey); for (String systemQName : systemQNames) { if (debugEnabled) { - logger.debug(" "+systemQName+"="+documentValue+" ("+documentKey+")"); + logger.debug(" " + systemQName + "=" + documentValue + " (" + documentKey + ")"); } systemProperties.put(systemQName, documentValue); } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/model/FileRefEntity.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/model/FileRefEntity.java index e34d44aa..a5aded29 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/model/FileRefEntity.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/model/FileRefEntity.java @@ -31,16 +31,17 @@ import java.util.Objects; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * TODO: Copied from org.alfresco.store.entity (alfresco-shared-file-store). To be discussed + * TODO: Copied from org.alfresco.store.entity (alfresco-shared-file-store). To be discussed * - * POJO that represents content reference ({@link java.util.UUID}) + * POJO that represents content reference ({@link java.util.UUID}) */ @Deprecated public class FileRefEntity { private String fileRef; - public FileRefEntity() {} + public FileRefEntity() + {} public FileRefEntity(String fileRef) { @@ -60,8 +61,10 @@ public class FileRefEntity @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; FileRefEntity that = (FileRefEntity) o; return Objects.equals(fileRef, that.fileRef); } diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/model/FileRefResponse.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/model/FileRefResponse.java index 7ac8c990..dde84445 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/model/FileRefResponse.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/model/FileRefResponse.java @@ -29,16 +29,17 @@ package org.alfresco.transformer.model; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * TODO: Copied from org.alfresco.store.entity (alfresco-shared-file-store). To be discussed + * TODO: Copied from org.alfresco.store.entity (alfresco-shared-file-store). To be discussed * - * POJO that describes the ContentRefEntry response, contains {@link FileRefEntity} according to API spec + * POJO that describes the ContentRefEntry response, contains {@link FileRefEntity} according to API spec */ @Deprecated public class FileRefResponse { private FileRefEntity entry; - public FileRefResponse() {} + public FileRefResponse() + {} public FileRefResponse(FileRefEntity entry) { diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/probes/ProbeTestTransform.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/probes/ProbeTestTransform.java index 698a7ac3..1680d64a 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/probes/ProbeTestTransform.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/probes/ProbeTestTransform.java @@ -26,14 +26,15 @@ */ package org.alfresco.transformer.probes; -import static org.alfresco.transformer.fs.FileManager.SOURCE_FILE; -import static org.alfresco.transformer.fs.FileManager.TARGET_FILE; -import static org.alfresco.transformer.fs.FileManager.TempFileProvider.createTempFile; import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE; import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; import static org.springframework.http.HttpStatus.OK; import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; +import static org.alfresco.transformer.fs.FileManager.SOURCE_FILE; +import static org.alfresco.transformer.fs.FileManager.TARGET_FILE; +import static org.alfresco.transformer.fs.FileManager.TempFileProvider.createTempFile; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -41,38 +42,33 @@ import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; - import jakarta.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.alfresco.transform.exceptions.TransformException; import org.alfresco.transformer.AbstractTransformerController; import org.alfresco.transformer.logging.LogEntry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Provides the logic performing test transformations by the live and ready probes. + * Provides the logic performing test transformations by the live and ready probes. * - *

K8s probes: A readiness probe indicates if the pod should accept request. It does not indicate that a pod is - * ready after startup. The liveness probe indicates when to kill the pod. Both probes are called throughout the - * lifetime of the pod and a liveness probes can take place before a readiness probe. The k8s - * initialDelaySeconds field is not fully honoured as it is multiplied by a random number, so is - * actually a maximum initial delay in seconds, but could be 0.

+ *

+ * K8s probes: A readiness probe indicates if the pod should accept request. It does not indicate that a pod is ready after startup. The liveness probe indicates when to kill the pod. Both probes are called throughout the lifetime of the pod and a liveness probes can take place before a readiness probe. The k8s initialDelaySeconds field is not fully honoured as it is multiplied by a random number, so is actually a maximum initial delay in seconds, but could be 0. + *

* - *

Live and readiness probes might do test transforms. The first 6 requests result in a transformation - * of a small test file. The average time is remembered, but excludes the first one which is normally longer. This is - * used in future requests to discover if transformations are becoming slower. The request also returns a non 200 status - * code resulting in the k8s pod being terminated, after a predefined number of transformations have been performed or - * if any transformation takes a long time. These are controlled by environment variables:

- *
    - *
  • livenessPercent - The percentage slower the small test transform must be to indicate there is a problem.
  • - *
  • livenessTransformPeriodSeconds - As liveness probes should be frequent, not every request should result in - * a test transformation. This value defines the gap between transformations.
  • - *
  • maxTransforms - the maximum number of transformation to be performed before a restart.
  • - *
  • maxTransformSeconds - the maximum time for a transformation, including failed ones.
  • - *
+ *

+ * Live and readiness probes might do test transforms. The first 6 requests result in a transformation of a small test file. The average time is remembered, but excludes the first one which is normally longer. This is used in future requests to discover if transformations are becoming slower. The request also returns a non 200 status code resulting in the k8s pod being terminated, after a predefined number of transformations have been performed or if any transformation takes a long time. These are controlled by environment variables: + *

+ *
    + *
  • livenessPercent - The percentage slower the small test transform must be to indicate there is a problem.
  • + *
  • livenessTransformPeriodSeconds - As liveness probes should be frequent, not every request should result in a test transformation. This value defines the gap between transformations.
  • + *
  • maxTransforms - the maximum number of transformation to be performed before a restart.
  • + *
  • maxTransformSeconds - the maximum time for a transformation, including failed ones.
  • + *
*/ @Deprecated public abstract class ProbeTestTransform @@ -115,18 +111,23 @@ public abstract class ProbeTestTransform /** * See Probes.md for more info. * - * @param expectedLength was the length of the target file during testing - * @param plusOrMinus simply allows for some variation in the transformed size caused by new versions of dates - * @param livenessPercent indicates that for this type of transform a variation up to 2 and a half times is not - * unreasonable under load - * @param maxTransforms default values normally supplied by helm. Not identical so we can be sure which value is used. - * @param maxTransformSeconds default values normally supplied by helm. Not identical so we can be sure which value is used. - * @param livenessTransformPeriodSeconds default values normally supplied by helm. Not identical so we can be sure which value is used. + * @param expectedLength + * was the length of the target file during testing + * @param plusOrMinus + * simply allows for some variation in the transformed size caused by new versions of dates + * @param livenessPercent + * indicates that for this type of transform a variation up to 2 and a half times is not unreasonable under load + * @param maxTransforms + * default values normally supplied by helm. Not identical so we can be sure which value is used. + * @param maxTransformSeconds + * default values normally supplied by helm. Not identical so we can be sure which value is used. + * @param livenessTransformPeriodSeconds + * default values normally supplied by helm. Not identical so we can be sure which value is used. */ public ProbeTestTransform(AbstractTransformerController controller, - String sourceFilename, String targetFilename, long expectedLength, long plusOrMinus, - int livenessPercent, long maxTransforms, long maxTransformSeconds, - long livenessTransformPeriodSeconds) + String sourceFilename, String targetFilename, long expectedLength, long plusOrMinus, + int livenessPercent, long maxTransforms, long maxTransformSeconds, + long livenessTransformPeriodSeconds) { this.sourceFilename = sourceFilename; this.targetFilename = targetFilename; @@ -137,7 +138,7 @@ public abstract class ProbeTestTransform maxTransformCount = getPositiveLongEnv("maxTransforms", maxTransforms); maxTransformTime = getPositiveLongEnv("maxTransformSeconds", maxTransformSeconds) * 1000; livenessTransformPeriod = getPositiveLongEnv("livenessTransformPeriodSeconds", - livenessTransformPeriodSeconds) * 1000; + livenessTransformPeriodSeconds) * 1000; livenessTransformEnabled = getBooleanEnvVar("livenessTransformEnabled", false); } @@ -148,8 +149,7 @@ public abstract class ProbeTestTransform return Boolean.parseBoolean(System.getenv(name)); } catch (Exception ignore) - { - } + {} return defaultValue; } @@ -164,8 +164,7 @@ public abstract class ProbeTestTransform l = Long.parseLong(env); } catch (NumberFormatException ignore) - { - } + {} } if (l <= 0) { @@ -187,9 +186,9 @@ public abstract class ProbeTestTransform } return (isLiveProbe && livenessTransformPeriod > 0 && (transCount <= AVERAGE_OVER_TRANSFORMS || nextTransformTime < System.currentTimeMillis())) - || !initialised.get() - ? doTransform(request, isLiveProbe) - : doNothing(isLiveProbe); + || !initialised.get() + ? doTransform(request, isLiveProbe) + : doNothing(isLiveProbe); } private String doNothing(boolean isLiveProbe) @@ -215,8 +214,7 @@ public abstract class ProbeTestTransform do { nextTransformTime += livenessTransformPeriod; - } - while (nextTransformTime < start); + } while (nextTransformTime < start); } File sourceFile = getSourceFile(request, isLiveProbe); @@ -233,9 +231,9 @@ public abstract class ProbeTestTransform if (time > maxTime) { throw new TransformException(INTERNAL_SERVER_ERROR, - getMessagePrefix(isLiveProbe) + - message + " which is more than " + livenessPercent + - "% slower than the normal value of " + normalTime + "ms"); + getMessagePrefix(isLiveProbe) + + message + " which is more than " + livenessPercent + + "% slower than the normal value of " + normalTime + "ms"); } // We don't care if the ready or live probe works out if we are 'ready' to take requests. @@ -251,15 +249,15 @@ public abstract class ProbeTestTransform if (die.get()) { throw new TransformException(TOO_MANY_REQUESTS, - getMessagePrefix(isLiveProbe) + "Transformer requested to die. A transform took " + - "longer than " + (maxTransformTime / 1000) + " seconds"); + getMessagePrefix(isLiveProbe) + "Transformer requested to die. A transform took " + + "longer than " + (maxTransformTime / 1000) + " seconds"); } if (maxTransformCount > 0 && transformCount.get() > maxTransformCount) { throw new TransformException(TOO_MANY_REQUESTS, - getMessagePrefix(isLiveProbe) + "Transformer requested to die. It has performed " + - "more than " + maxTransformCount + " transformations"); + getMessagePrefix(isLiveProbe) + "Transformer requested to die. It has performed " + + "more than " + maxTransformCount + " transformations"); } } @@ -311,11 +309,11 @@ public abstract class ProbeTestTransform maxTime = (normalTime * (livenessPercent + 100)) / 100; if ((!isLiveProbe && !readySent.getAndSet( - true)) || transCount > AVERAGE_OVER_TRANSFORMS) + true)) || transCount > AVERAGE_OVER_TRANSFORMS) { nextTransformTime = System.currentTimeMillis() + livenessTransformPeriod; logger.trace("{} - {}ms+{}%={}ms", message, normalTime, livenessPercent, - maxTime); + maxTime); } } else if (!isLiveProbe && !readySent.getAndSet(true)) @@ -333,15 +331,15 @@ public abstract class ProbeTestTransform if (!targetFile.exists() || !targetFile.isFile()) { throw new TransformException(INTERNAL_SERVER_ERROR, - probeMessage + "Target File \"" + targetFile.getAbsolutePath() + "\" did not exist"); + probeMessage + "Target File \"" + targetFile.getAbsolutePath() + "\" did not exist"); } long length = targetFile.length(); if (length < minExpectedLength || length > maxExpectedLength) { throw new TransformException(INTERNAL_SERVER_ERROR, - probeMessage + "Target File \"" + targetFile.getAbsolutePath() + - "\" was the wrong size (" + length + "). Needed to be between " + - minExpectedLength + " and " + maxExpectedLength); + probeMessage + "Target File \"" + targetFile.getAbsolutePath() + + "\" was the wrong size (" + length + "). Needed to be between " + + minExpectedLength + " and " + maxExpectedLength); } LogEntry.setTargetSize(length); LogEntry.setStatusCodeAndMessage(OK.value(), probeMessage + "Success - " + message); diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/MimetypeMap.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/MimetypeMap.java index e6dfddee..f7bc4c62 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/MimetypeMap.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/MimetypeMap.java @@ -29,9 +29,8 @@ package org.alfresco.transformer.util; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Partially duplicated from *alfresco-data-model*. + * Partially duplicated from *alfresco-data-model*. */ @Deprecated public interface MimetypeMap extends org.alfresco.transform.common.Mimetype -{ -} +{} diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/RequestParamMap.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/RequestParamMap.java index b9e06566..92d34368 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/RequestParamMap.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/RequestParamMap.java @@ -1,48 +1,47 @@ -/* - * #%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.transformer.util; - -/** - * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. - * - * Extends the list of transform options with historic request parameters or 'extra' parameters used in testing - * or communication in the all-in-one transformer. - */ -@Deprecated -public interface RequestParamMap extends org.alfresco.transform.client.util.RequestParamMap -{ - // This property can be sent by acs repository's legacy transformers to force a transform, - // instead of letting this T-Engine determine it based on the request parameters. - // This allows clients to specify transform names as they appear in the engine config files, for example: - // imagemagick, libreoffice, PdfBox, TikaAuto, .... - // See ATS-731. - @Deprecated - String TRANSFORM_NAME_PROPERTY = "transformName"; - String TRANSFORM_NAME_PARAMETER = "alfresco.transform-name-parameter"; - - String TEST_DELAY = "testDelay"; -} +/* + * #%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.transformer.util; + +/** + * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. + * + * Extends the list of transform options with historic request parameters or 'extra' parameters used in testing or communication in the all-in-one transformer. + */ +@Deprecated +public interface RequestParamMap extends org.alfresco.transform.client.util.RequestParamMap +{ + // This property can be sent by acs repository's legacy transformers to force a transform, + // instead of letting this T-Engine determine it based on the request parameters. + // This allows clients to specify transform names as they appear in the engine config files, for example: + // imagemagick, libreoffice, PdfBox, TikaAuto, .... + // See ATS-731. + @Deprecated + String TRANSFORM_NAME_PROPERTY = "transformName"; + String TRANSFORM_NAME_PARAMETER = "alfresco.transform-name-parameter"; + + String TEST_DELAY = "testDelay"; +} diff --git a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/Util.java b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/Util.java index e7d40fd1..d110224a 100644 --- a/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/Util.java +++ b/deprecated/alfresco-transformer-base/src/main/java/org/alfresco/transformer/util/Util.java @@ -35,7 +35,8 @@ public class Util /** * Safely converts a {@link String} to an {@link Integer} * - * @param param String to be converted + * @param param + * String to be converted * @return Null if param is null or converted value as {@link Integer} */ public static Integer stringToInteger(final String param) @@ -46,7 +47,8 @@ public class Util /** * Safely converts a {@link String} to a {@link Boolean} * - * @param param String to be converted + * @param param + * String to be converted * @return Null if param is null or converted value as {@link Boolean} */ public static Boolean stringToBoolean(final String param) @@ -57,7 +59,8 @@ public class Util /** * Safely converts a {@link String} to a {@link Long} * - * @param param String to be converted + * @param param + * String to be converted * @return Null if param is null or converted value as {@link Boolean} */ public static Long stringToLong(final String param) diff --git a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractHttpRequestTest.java b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractHttpRequestTest.java index 1b1bf854..1448fb03 100644 --- a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractHttpRequestTest.java +++ b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractHttpRequestTest.java @@ -26,13 +26,14 @@ */ package org.alfresco.transformer; -import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; import static org.springframework.test.util.AssertionErrors.assertTrue; +import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; + import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -45,8 +46,7 @@ import org.springframework.util.LinkedMultiValueMap; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Super class for testing controllers with a server. Includes tests for the AbstractTransformerController itself. - * Note: Currently uses json rather than HTML as json is returned by this spring boot test harness. + * Super class for testing controllers with a server. Includes tests for the AbstractTransformerController itself. Note: Currently uses json rather than HTML as json is returned by this spring boot test harness. */ @Deprecated public abstract class AbstractHttpRequestTest @@ -74,7 +74,7 @@ public abstract class AbstractHttpRequestTest public void logPageExists() { String result = restTemplate.getForObject("http://localhost:" + port + "/log", - String.class); + String.class); String title = getTransformerName() + ' ' + "Log"; assertTrue("\"" + title + "\" should be part of the page title", result.contains(title)); @@ -84,11 +84,11 @@ public abstract class AbstractHttpRequestTest public void errorPageExists() { String result = restTemplate.getForObject("http://localhost:" + port + "/error", - String.class); + String.class); String title = getTransformerName() + ' ' + "Error Page"; assertTrue("\"" + title + "\" should be part of the page title", - result.contains("Error Page")); + result.contains("Error Page")); } @Test @@ -111,18 +111,18 @@ public abstract class AbstractHttpRequestTest private void assertMissingParameter(String name) { assertTransformError(true, - getTransformerName() + " - Request parameter '" + name + "' is missing", null); + getTransformerName() + " - Request parameter '" + name + "' is missing", null); } protected void assertTransformError(boolean addFile, - String errorMessage, - LinkedMultiValueMap additionalParams) + String errorMessage, + LinkedMultiValueMap additionalParams) { LinkedMultiValueMap parameters = new LinkedMultiValueMap<>(); if (addFile) { parameters.add("file", - new org.springframework.core.io.ClassPathResource("quick." + getSourceExtension())); + new org.springframework.core.io.ClassPathResource("quick." + getSourceExtension())); } if (additionalParams != null) { @@ -132,7 +132,7 @@ public abstract class AbstractHttpRequestTest HttpHeaders headers = new HttpHeaders(); headers.setContentType(MULTIPART_FORM_DATA); HttpEntity> entity = new HttpEntity<>(parameters, - headers); + headers); sendTranformationRequest(entity, errorMessage); } @@ -153,10 +153,10 @@ public abstract class AbstractHttpRequestTest } protected void sendTranformationRequest( - final HttpEntity> entity, final String errorMessage) + final HttpEntity> entity, final String errorMessage) { final ResponseEntity response = restTemplate.exchange(ENDPOINT_TRANSFORM, POST, entity, - String.class, ""); + String.class, ""); assertEquals(errorMessage, getErrorMessage(response.getBody())); } diff --git a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractMetadataExtractsIT.java b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractMetadataExtractsIT.java index bcdf8cad..2a163f89 100644 --- a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractMetadataExtractsIT.java +++ b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractMetadataExtractsIT.java @@ -27,12 +27,14 @@ package org.alfresco.transformer; import static java.text.MessageFormat.format; -import static org.alfresco.transformer.EngineClient.sendTRequest; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_METADATA_EXTRACT; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.springframework.http.HttpStatus.OK; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_METADATA_EXTRACT; +import static org.alfresco.transformer.EngineClient.sendTRequest; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -42,22 +44,21 @@ import java.util.Map; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; - import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Super class of metadata integration tests. Sub classes should provide the following: - *

- *

    - *
  • A method providing a - * Stream of test files: {@code public static Stream engineTransformations()};
  • - *
  • Provide expected json files (<sourceFilename>"_metadata.json") as resources on the classpath.
  • - *
  • Override the method {@code testTransformation(TestFileInfo testFileInfo)} such that it calls - * the super method as a {@code @ParameterizedTest} for example:
- *
+ *             Super class of metadata integration tests. Sub classes should provide the following:
+ *             

+ *

    + *
  • A method providing a Stream of test files: {@code public static Stream engineTransformations()};
  • + *
  • Provide expected json files (<sourceFilename>"_metadata.json") as resources on the classpath.
  • + *
  • Override the method {@code testTransformation(TestFileInfo testFileInfo)} such that it calls the super method as a {@code @ParameterizedTest} for example:
  • + *
+ * + *
  * @ParameterizedTest
  * 
  * @MethodSource("engineTransformations")
@@ -69,7 +70,7 @@ import org.springframework.http.ResponseEntity;
  * { 
  *      super.testTransformation(TestFileInfo testFileInfo)
  * }
- * 
+ *
* * @author adavis * @author dedwards @@ -84,7 +85,6 @@ public abstract class AbstractMetadataExtractsIT private final ObjectMapper jsonObjectMapper = new ObjectMapper(); - public void testTransformation(TestFileInfo testFileInfo) { final String sourceMimetype = testFileInfo.getMimeType(); @@ -106,7 +106,7 @@ public abstract class AbstractMetadataExtractsIT Map expectedMetadata = readExpectedMetadata(metadataFilename, actualMetadataFile); assertEquals(expectedMetadata, actualMetadata, - sourceFile+": The metadata did not match the expected value. It has been saved in "+actualMetadataFile.getAbsolutePath()); + sourceFile + ": The metadata did not match the expected value. It has been saved in " + actualMetadataFile.getAbsolutePath()); actualMetadataFile.delete(); } catch (Exception e) @@ -122,8 +122,8 @@ public abstract class AbstractMetadataExtractsIT { if (inputStream == null) { - fail("The expected metadata file "+filename+" did not exist.\n"+ - "The actual metadata has been saved in "+actualMetadataFile.getAbsoluteFile()); + fail("The expected metadata file " + filename + " did not exist.\n" + + "The actual metadata has been saved in " + actualMetadataFile.getAbsoluteFile()); } return readMetadata(inputStream); } diff --git a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractQueueTransformServiceIT.java b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractQueueTransformServiceIT.java index 8cd096e1..6bdeceef 100644 --- a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractQueueTransformServiceIT.java +++ b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractQueueTransformServiceIT.java @@ -30,19 +30,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import jakarta.jms.Queue; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; import org.apache.activemq.command.ActiveMQQueue; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jms.core.JmsTemplate; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * @author Lucian Tuca - * created on 15/01/2019 + * @author Lucian Tuca created on 15/01/2019 */ @Deprecated @SpringBootTest(properties = {"activemq.url=nio://localhost:61616"}) @@ -55,7 +55,7 @@ public abstract class AbstractQueueTransformServiceIT private JmsTemplate jmsTemplate; private final ActiveMQQueue testingQueue = new ActiveMQQueue( - "org.alfresco.transform.engine.IT"); + "org.alfresco.transform.engine.IT"); @Test public void queueTransformServiceIT() diff --git a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractTransformerControllerTest.java b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractTransformerControllerTest.java index fbfa337d..a16d8fce 100644 --- a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractTransformerControllerTest.java +++ b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/AbstractTransformerControllerTest.java @@ -26,38 +26,26 @@ */ package org.alfresco.transformer; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import org.alfresco.transform.client.model.InternalContext; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.config.SupportedSourceAndTarget; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.config.TransformOption; -import org.alfresco.transform.config.TransformOptionGroup; -import org.alfresco.transform.config.TransformOptionValue; -import org.alfresco.transform.config.Transformer; -import org.alfresco.transform.messages.TransformStack; -import org.alfresco.transform.registry.TransformServiceRegistry; -import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient; -import org.alfresco.transformer.model.FileRefEntity; -import org.alfresco.transformer.model.FileRefResponse; -import org.alfresco.transformer.probes.ProbeTestTransform; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.core.io.ClassPathResource; -import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultActions; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.http.HttpHeaders.ACCEPT; +import static org.springframework.http.HttpHeaders.CONTENT_TYPE; +import static org.springframework.http.HttpStatus.BAD_REQUEST; +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.test.web.servlet.result.MockMvcResultMatchers.content; +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.RequestParamMap.DIRECT_ACCESS_URL; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG_LATEST; import java.io.File; import java.io.FileInputStream; @@ -70,34 +58,42 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.UUID; -import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; -import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG_LATEST; -import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -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_TYPE; -import static org.springframework.http.HttpStatus.BAD_REQUEST; -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.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.core.io.ClassPathResource; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import org.alfresco.transform.client.model.InternalContext; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.config.SupportedSourceAndTarget; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.TransformOptionGroup; +import org.alfresco.transform.config.TransformOptionValue; +import org.alfresco.transform.config.Transformer; +import org.alfresco.transform.messages.TransformStack; +import org.alfresco.transform.registry.TransformServiceRegistry; +import org.alfresco.transformer.clients.AlfrescoSharedFileStoreClient; +import org.alfresco.transformer.probes.ProbeTestTransform; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Super class for testing controllers without a server. Includes tests for the AbstractTransformerController itself. + * Super class for testing controllers without a server. Includes tests for the AbstractTransformerController itself. */ @Deprecated public abstract class AbstractTransformerControllerTest @@ -135,44 +131,46 @@ public abstract class AbstractTransformerControllerTest /** * The expected result. Taken resting target quick file's bytes. * - * Note: These checks generally don't work on Windows (Mac and Linux are okay). Possibly to do with byte order - * loading. + * Note: These checks generally don't work on Windows (Mac and Linux are okay). Possibly to do with byte order loading. */ protected byte[] expectedTargetFileBytes; // Called by sub class protected abstract void mockTransformCommand(String sourceExtension, - String targetExtension, String sourceMimetype, - boolean readTargetFileBytes) throws IOException; + String targetExtension, String sourceMimetype, + boolean readTargetFileBytes) throws IOException; protected abstract AbstractTransformerController getController(); protected abstract void updateTransformRequestWithSpecificOptions( - TransformRequest transformRequest); + TransformRequest transformRequest); /** - * This method ends up being the core of the mock. - * It copies content from an existing file in the resources folder to the desired location - * in order to simulate a successful transformation. + * This method ends up being the core of the mock. It copies content from an existing file in the resources folder to the desired location in order to simulate a successful transformation. * - * @param actualTargetExtension Requested extension. - * @param testFile The test file (transformed) - basically the result. - * @param targetFile The location where the content from the testFile should be copied - * @throws IOException in case of any errors. + * @param actualTargetExtension + * Requested extension. + * @param testFile + * The test file (transformed) - basically the result. + * @param targetFile + * The location where the content from the testFile should be copied + * @throws IOException + * in case of any errors. */ void generateTargetFileFromResourceFile(String actualTargetExtension, File testFile, - File targetFile) throws IOException + File targetFile) throws IOException { if (testFile != null) { try (var inputStream = new FileInputStream(testFile); - var outputStream = new FileOutputStream(targetFile)) + var outputStream = new FileOutputStream(targetFile)) { FileChannel source = inputStream.getChannel(); FileChannel target = outputStream.getChannel(); target.transferFrom(source, 0, source.size()); - } catch (Exception e) + } + catch (Exception e) { throw e; } @@ -183,13 +181,14 @@ public abstract class AbstractTransformerControllerTest if (testFile != null) { try (var inputStream = new FileInputStream(testFile); - var outputStream = new FileOutputStream(targetFile)) + var outputStream = new FileOutputStream(targetFile)) { FileChannel source = inputStream.getChannel(); FileChannel target = outputStream.getChannel(); target.transferFrom(source, 0, source.size()); - } catch (Exception e) + } + catch (Exception e) { throw e; } @@ -213,11 +212,11 @@ public abstract class AbstractTransformerControllerTest " does not exist in the resources directory"); } // added as part of ATS-702 to allow test resources to be read from the imported jar files to prevent test resource duplication - if (testFileUrl!=null) + if (testFileUrl != null) { // Each use of the tempDir should result in a unique directory being used testFile = new File(tempDir, testFilename); - Files.copy(classLoader.getResourceAsStream(testFilename), testFile.toPath(),REPLACE_EXISTING); + Files.copy(classLoader.getResourceAsStream(testFilename), testFile.toPath(), REPLACE_EXISTING); } return testFileUrl == null ? null : testFile; @@ -236,7 +235,7 @@ public abstract class AbstractTransformerControllerTest } private MockHttpServletRequestBuilder mockMvcRequestWithoutMockMultipartFile(String url, - String... params) + String... params) { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM); @@ -253,10 +252,10 @@ public abstract class AbstractTransformerControllerTest } private MockHttpServletRequestBuilder mockMvcRequestWithMockMultipartFile(String url, MockMultipartFile sourceFile, - String... params) + String... params) { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM).file( - sourceFile); + sourceFile); if (params.length % 2 != 0) { @@ -298,11 +297,11 @@ public abstract class AbstractTransformerControllerTest public void simpleTransformTest() throws Exception { mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) - .andExpect(status().is(OK.value())) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*= UTF-8''quick." + targetExtension)); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) + .andExpect(status().is(OK.value())) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*= UTF-8''quick." + targetExtension)); } @Test @@ -310,22 +309,22 @@ public abstract class AbstractTransformerControllerTest { long start = System.currentTimeMillis(); mockMvc.perform(mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension, - "testDelay", "400")) - .andExpect(status().is(OK.value())) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*= UTF-8''quick." + targetExtension)); + "testDelay", "400")) + .andExpect(status().is(OK.value())) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*= UTF-8''quick." + targetExtension)); long ms = System.currentTimeMillis() - start; System.out.println("Transform incluing test delay was " + ms); assertTrue(ms >= 400, "Delay sending the result back was too small " + ms); - assertTrue(ms <= 500,"Delay sending the result back was too big " + ms); + assertTrue(ms <= 500, "Delay sending the result back was too big " + ms); } @Test public void noTargetFileTest() throws Exception { mockMvc.perform(mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", "xxx")) - .andExpect(status().is(INTERNAL_SERVER_ERROR.value())); + .andExpect(status().is(INTERNAL_SERVER_ERROR.value())); } @Test @@ -333,14 +332,14 @@ public abstract class AbstractTransformerControllerTest public void dotDotSourceFilenameTest() throws Exception { sourceFile = new MockMultipartFile("file", "../quick." + sourceExtension, sourceMimetype, - expectedSourceFileBytes); + expectedSourceFileBytes); mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) - .andExpect(status().is(OK.value())) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*= UTF-8''quick." + targetExtension)); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) + .andExpect(status().is(OK.value())) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*= UTF-8''quick." + targetExtension)); } @Test @@ -348,14 +347,14 @@ public abstract class AbstractTransformerControllerTest public void noExtensionSourceFilenameTest() throws Exception { sourceFile = new MockMultipartFile("file", "../quick", sourceMimetype, - expectedSourceFileBytes); + expectedSourceFileBytes); mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) - .andExpect(status().is(OK.value())) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*= UTF-8''quick." + targetExtension)); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) + .andExpect(status().is(OK.value())) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*= UTF-8''quick." + targetExtension)); } @Test @@ -365,9 +364,9 @@ public abstract class AbstractTransformerControllerTest sourceFile = new MockMultipartFile("file", "abc/", sourceMimetype, expectedSourceFileBytes); mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) - .andExpect(status().is(BAD_REQUEST.value())) - .andExpect(status().reason(containsString("The source filename was not supplied"))); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) + .andExpect(status().is(BAD_REQUEST.value())) + .andExpect(status().reason(containsString("The source filename was not supplied"))); } @Test @@ -376,17 +375,17 @@ public abstract class AbstractTransformerControllerTest sourceFile = new MockMultipartFile("file", "", sourceMimetype, expectedSourceFileBytes); mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) - .andExpect(status().is(BAD_REQUEST.value())); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", targetExtension)) + .andExpect(status().is(BAD_REQUEST.value())); } @Test public void noTargetExtensionTest() throws Exception { mockMvc.perform(mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile)) - .andExpect(status().is(BAD_REQUEST.value())) - .andExpect(status().reason( - containsString("Request parameter 'targetExtension' is missing"))); + .andExpect(status().is(BAD_REQUEST.value())) + .andExpect(status().reason( + containsString("Request parameter 'targetExtension' is missing"))); } @Test @@ -396,14 +395,14 @@ public abstract class AbstractTransformerControllerTest probeTestTransform.setLivenessPercent(110); long[][] values = new long[][]{ - {5000, 0, Long.MAX_VALUE}, // 1st transform is ignored - {1000, 1000, 2100}, // 1000 + 1000*1.1 - {3000, 2000, 4200}, // 2000 + 2000*1.1 - {2000, 2000, 4200}, - {6000, 3000, 6300}, - {8000, 4000, 8400}, - {4444, 4000, 8400}, // no longer in the first few, so normal and max times don't change - {5555, 4000, 8400} + {5000, 0, Long.MAX_VALUE}, // 1st transform is ignored + {1000, 1000, 2100}, // 1000 + 1000*1.1 + {3000, 2000, 4200}, // 2000 + 2000*1.1 + {2000, 2000, 4200}, + {6000, 3000, 6300}, + {8000, 4000, 8400}, + {4444, 4000, 8400}, // no longer in the first few, so normal and max times don't change + {5555, 4000, 8400} }; for (long[] v : values) @@ -427,16 +426,16 @@ public abstract class AbstractTransformerControllerTest // 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(BAD_REQUEST.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(BAD_REQUEST.value())) + .andReturn().getResponse().getContentAsString(); TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, - TransformReply.class); + TransformReply.class); // Assert the reply assertEquals(BAD_REQUEST.value(), transformReply.getStatus()); @@ -455,8 +454,8 @@ public abstract class AbstractTransformerControllerTest public void testGetTransformConfigInfo() throws Exception { TransformConfig expectedTransformConfig = objectMapper - .readValue(getTestFile(getEngineConfigName(), true), - TransformConfig.class); + .readValue(getTestFile(getEngineConfigName(), true), + TransformConfig.class); expectedTransformConfig.getTransformers().forEach(transformer -> { transformer.setCoreVersion(coreVersion); transformer.getTransformOptions().add(DIRECT_ACCESS_URL); @@ -464,13 +463,13 @@ public abstract class AbstractTransformerControllerTest expectedTransformConfig.getTransformOptions().put(DIRECT_ACCESS_URL, Set.of(new TransformOptionValue(false, DIRECT_ACCESS_URL))); ReflectionTestUtils.setField(transformRegistry, "engineConfig", - new ClassPathResource(getEngineConfigName())); + new ClassPathResource(getEngineConfigName())); String response = mockMvc - .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG_LATEST)) - .andExpect(status().is(OK.value())) - .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) - .andReturn().getResponse().getContentAsString(); + .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG_LATEST)) + .andExpect(status().is(OK.value())) + .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) + .andReturn().getResponse().getContentAsString(); TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class); assertEquals(expectedTransformConfig, transformConfig); @@ -481,17 +480,17 @@ public abstract class AbstractTransformerControllerTest public void testGetTransformConfigInfoExcludingCoreVersion() throws Exception { TransformConfig expectedTransformConfig = objectMapper - .readValue(getTestFile(getEngineConfigName(), true), - TransformConfig.class); + .readValue(getTestFile(getEngineConfigName(), true), + TransformConfig.class); ReflectionTestUtils.setField(transformRegistry, "engineConfig", - new ClassPathResource(getEngineConfigName())); + new ClassPathResource(getEngineConfigName())); String response = mockMvc - .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG)) - .andExpect(status().is(OK.value())) - .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) - .andReturn().getResponse().getContentAsString(); + .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG)) + .andExpect(status().is(OK.value())) + .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) + .andReturn().getResponse().getContentAsString(); TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class); assertEquals(expectedTransformConfig, transformConfig); @@ -503,13 +502,13 @@ public abstract class AbstractTransformerControllerTest TransformConfig expectedResult = buildCompleteTransformConfig(); ReflectionTestUtils.setField(transformRegistry, "engineConfig", - new ClassPathResource("engine_config_with_duplicates.json")); + new ClassPathResource("engine_config_with_duplicates.json")); String response = mockMvc - .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG)) - .andExpect(status().is(OK.value())) - .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) - .andReturn().getResponse().getContentAsString(); + .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG)) + .andExpect(status().is(OK.value())) + .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) + .andReturn().getResponse().getContentAsString(); TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class); @@ -517,9 +516,9 @@ public abstract class AbstractTransformerControllerTest assertEquals(expectedResult, transformConfig); assertEquals(3, transformConfig.getTransformOptions().get("engineXOptions").size()); assertEquals(1, - transformConfig.getTransformers().get(0).getSupportedSourceAndTargetList().size()); + transformConfig.getTransformers().get(0).getSupportedSourceAndTargetList().size()); assertEquals(1, - transformConfig.getTransformers().get(0).getTransformOptions().size()); + transformConfig.getTransformers().get(0).getTransformOptions().size()); } @Test @@ -530,13 +529,13 @@ public abstract class AbstractTransformerControllerTest expectedResult.setTransformers(ImmutableList.of(transformer)); ReflectionTestUtils.setField(transformRegistry, "engineConfig", - new ClassPathResource("engine_config_incomplete.json")); + new ClassPathResource("engine_config_incomplete.json")); String response = mockMvc - .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG)) - .andExpect(status().is(OK.value())) - .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) - .andReturn().getResponse().getContentAsString(); + .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG)) + .andExpect(status().is(OK.value())) + .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) + .andReturn().getResponse().getContentAsString(); TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class); @@ -553,13 +552,13 @@ public abstract class AbstractTransformerControllerTest expectedResult.setTransformers(ImmutableList.of(transformer)); ReflectionTestUtils.setField(transformRegistry, "engineConfig", - new ClassPathResource("engine_config_no_transform_options.json")); + new ClassPathResource("engine_config_no_transform_options.json")); String response = mockMvc - .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG)) - .andExpect(status().is(OK.value())) - .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) - .andReturn().getResponse().getContentAsString(); + .perform(MockMvcRequestBuilders.get(ENDPOINT_TRANSFORM_CONFIG)) + .andExpect(status().is(OK.value())) + .andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE)) + .andReturn().getResponse().getContentAsString(); TransformConfig transformConfig = objectMapper.readValue(response, TransformConfig.class); @@ -572,16 +571,16 @@ public abstract class AbstractTransformerControllerTest TransformConfig expectedResult = new TransformConfig(); Set transformOptionGroup = ImmutableSet.of( - new TransformOptionValue(false, "cropGravity")); + new TransformOptionValue(false, "cropGravity")); Set transformOptions = ImmutableSet.of( - new TransformOptionValue(false, "page"), - new TransformOptionValue(false, "width"), - new TransformOptionGroup(false, transformOptionGroup)); + new TransformOptionValue(false, "page"), + new TransformOptionValue(false, "width"), + new TransformOptionGroup(false, transformOptionGroup)); Map> transformOptionsMap = ImmutableMap.of("engineXOptions", - transformOptions); + transformOptions); Transformer transformer = buildTransformer("application/pdf", "image/png", "engineXOptions", - "engineX"); + "engineX"); List transformers = ImmutableList.of(transformer); expectedResult.setTransformOptions(transformOptionsMap); @@ -591,7 +590,7 @@ public abstract class AbstractTransformerControllerTest } private Transformer buildTransformer(String sourceMediaType, String targetMediaType, - String transformOptions, String transformerName) + String transformOptions, String transformerName) { Transformer transformer = buildTransformer(sourceMediaType, targetMediaType); transformer.setTransformerName(transformerName); @@ -603,10 +602,10 @@ public abstract class AbstractTransformerControllerTest private Transformer buildTransformer(String sourceMediaType, String targetMediaType) { Set supportedSourceAndTargetList = ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType(sourceMediaType) - .withTargetMediaType(targetMediaType) - .build()); + SupportedSourceAndTarget.builder() + .withSourceMediaType(sourceMediaType) + .withTargetMediaType(targetMediaType) + .build()); Transformer transformer = new Transformer(); transformer.setSupportedSourceAndTargetList(supportedSourceAndTargetList); diff --git a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/EngineClient.java b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/EngineClient.java index 92453ec5..a1cc87a1 100644 --- a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/EngineClient.java +++ b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/EngineClient.java @@ -8,9 +8,11 @@ package org.alfresco.transformer; import static java.util.Collections.emptyMap; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; + import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; + import java.util.Map; import org.springframework.core.io.ClassPathResource; @@ -31,23 +33,23 @@ import org.springframework.web.client.RestTemplate; public class EngineClient { private static final RestTemplate REST_TEMPLATE = new RestTemplate(); - + public static ResponseEntity sendTRequest( - final String engineUrl, final String sourceFile, - final String sourceMimetype, final String targetMimetype, final String targetExtension) + final String engineUrl, final String sourceFile, + final String sourceMimetype, final String targetMimetype, final String targetExtension) { return sendTRequest(engineUrl, sourceFile, sourceMimetype, targetMimetype, targetExtension, - emptyMap()); + emptyMap()); } public static ResponseEntity sendTRequest( - final String engineUrl, final String sourceFile, - final String sourceMimetype, final String targetMimetype, final String targetExtension, - final Map transformOptions) + final String engineUrl, final String sourceFile, + final String sourceMimetype, final String targetMimetype, final String targetExtension, + final Map transformOptions) { final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MULTIPART_FORM_DATA); - //headers.setAccept(ImmutableList.of(MULTIPART_FORM_DATA)); + // headers.setAccept(ImmutableList.of(MULTIPART_FORM_DATA)); final MultiValueMap body = new LinkedMultiValueMap<>(); body.add("file", new ClassPathResource(sourceFile)); diff --git a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/QueueTransformServiceTest.java b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/QueueTransformServiceTest.java index c53b2d19..4ce6ccc9 100644 --- a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/QueueTransformServiceTest.java +++ b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/QueueTransformServiceTest.java @@ -40,10 +40,6 @@ import jakarta.jms.Destination; import jakarta.jms.JMSException; import jakarta.jms.Message; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transformer.messaging.TransformMessageConverter; -import org.alfresco.transformer.messaging.TransformReplySender; import org.apache.activemq.command.ActiveMQObjectMessage; import org.apache.activemq.command.ActiveMQQueue; import org.junit.jupiter.api.BeforeEach; @@ -55,6 +51,11 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.jms.support.converter.MessageConversionException; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transformer.messaging.TransformMessageConverter; +import org.alfresco.transformer.messaging.TransformReplySender; + /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. */ @@ -106,12 +107,12 @@ public class QueueTransformServiceTest msg.setJMSReplyTo(destination); TransformReply reply = TransformReply - .builder() - .withStatus(INTERNAL_SERVER_ERROR.value()) - .withErrorDetails( - "JMS exception during T-Request deserialization of message with correlationID " - + msg.getCorrelationId() + ": null") - .build(); + .builder() + .withStatus(INTERNAL_SERVER_ERROR.value()) + .withErrorDetails( + "JMS exception during T-Request deserialization of message with correlationID " + + msg.getCorrelationId() + ": null") + .build(); doReturn(null).when(transformMessageConverter).fromMessage(msg); @@ -125,7 +126,7 @@ public class QueueTransformServiceTest @Test public void testConvertMessageThrowsMessageConversionExceptionThenReplyWithBadRequest() - throws JMSException + throws JMSException { ActiveMQObjectMessage msg = new ActiveMQObjectMessage(); msg.setCorrelationId("1234"); @@ -133,12 +134,12 @@ public class QueueTransformServiceTest msg.setJMSReplyTo(destination); TransformReply reply = TransformReply - .builder() - .withStatus(BAD_REQUEST.value()) - .withErrorDetails( - "Message conversion exception during T-Request deserialization of message with correlationID" - + msg.getCorrelationId() + ": null") - .build(); + .builder() + .withStatus(BAD_REQUEST.value()) + .withErrorDetails( + "Message conversion exception during T-Request deserialization of message with correlationID" + + msg.getCorrelationId() + ": null") + .build(); doThrow(MessageConversionException.class).when(transformMessageConverter).fromMessage(msg); @@ -152,7 +153,7 @@ public class QueueTransformServiceTest @Test public void testConvertMessageThrowsJMSExceptionThenReplyWithInternalServerError() - throws JMSException + throws JMSException { ActiveMQObjectMessage msg = new ActiveMQObjectMessage(); msg.setCorrelationId("1234"); @@ -160,12 +161,12 @@ public class QueueTransformServiceTest msg.setJMSReplyTo(destination); TransformReply reply = TransformReply - .builder() - .withStatus(INTERNAL_SERVER_ERROR.value()) - .withErrorDetails( - "JMSException during T-Request deserialization of message with correlationID " + - msg.getCorrelationId() + ": null") - .build(); + .builder() + .withStatus(INTERNAL_SERVER_ERROR.value()) + .withErrorDetails( + "JMSException during T-Request deserialization of message with correlationID " + + msg.getCorrelationId() + ": null") + .build(); doThrow(JMSException.class).when(transformMessageConverter).fromMessage(msg); @@ -186,13 +187,13 @@ public class QueueTransformServiceTest TransformRequest request = new TransformRequest(); TransformReply reply = TransformReply - .builder() - .withStatus(CREATED.value()) - .build(); + .builder() + .withStatus(CREATED.value()) + .build(); doReturn(request).when(transformMessageConverter).fromMessage(msg); doReturn(new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()))) - .when(transformController).transform(request, null); + .when(transformController).transform(request, null); queueTransformService.receive(msg); @@ -217,7 +218,7 @@ public class QueueTransformServiceTest @Test public void testWhenExceptionOnCorrelationIdIsThrownThenContinueFlowWithNullCorrelationId() - throws JMSException + throws JMSException { Message msg = mock(Message.class); Destination destination = mock(Destination.class); @@ -227,13 +228,13 @@ public class QueueTransformServiceTest TransformRequest request = new TransformRequest(); TransformReply reply = TransformReply - .builder() - .withStatus(CREATED.value()) - .build(); + .builder() + .withStatus(CREATED.value()) + .build(); doReturn(request).when(transformMessageConverter).fromMessage(msg); doReturn(new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus()))) - .when(transformController).transform(request, null); + .when(transformController).transform(request, null); queueTransformService.receive(msg); diff --git a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/SourceTarget.java b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/SourceTarget.java index aa300888..98b6ebec 100644 --- a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/SourceTarget.java +++ b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/SourceTarget.java @@ -31,7 +31,7 @@ import java.util.Objects; /** * @deprecated will be removed in a future release. Replaced by alfresco-base-t-engine. * - * Source & Target media type pair + * Source & Target media type pair * * @author Cezar Leahu */ @@ -50,11 +50,13 @@ public class SourceTarget @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; SourceTarget that = (SourceTarget) o; return Objects.equals(source, that.source) && - Objects.equals(target, that.target); + Objects.equals(target, that.target); } @Override diff --git a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/TestFileInfo.java b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/TestFileInfo.java index cd309214..48121ac7 100644 --- a/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/TestFileInfo.java +++ b/deprecated/alfresco-transformer-base/src/test/java/org/alfresco/transformer/TestFileInfo.java @@ -39,7 +39,7 @@ public class TestFileInfo private final boolean exactMimeType; public TestFileInfo(final String mimeType, final String extension, final String path, - final boolean exactMimeType) + final boolean exactMimeType) { this.mimeType = mimeType; this.extension = extension; @@ -68,13 +68,13 @@ public class TestFileInfo } public static TestFileInfo testFile(final String mimeType, final String extension, - final String path, final boolean exactMimeType) + final String path, final boolean exactMimeType) { return new TestFileInfo(mimeType, extension, path, exactMimeType); } public static TestFileInfo testFile(final String mimeType, final String extension, - final String path) + final String path) { return new TestFileInfo(mimeType, extension, path, false); } diff --git a/engines/aio/src/main/java/org/alfresco/transform/coreaio/AIOTransformEngine.java b/engines/aio/src/main/java/org/alfresco/transform/coreaio/AIOTransformEngine.java index 1ce4864a..329fda2c 100644 --- a/engines/aio/src/main/java/org/alfresco/transform/coreaio/AIOTransformEngine.java +++ b/engines/aio/src/main/java/org/alfresco/transform/coreaio/AIOTransformEngine.java @@ -26,19 +26,20 @@ */ package org.alfresco.transform.coreaio; -import org.alfresco.transform.base.TransformEngine; -import org.alfresco.transform.base.probes.ProbeTransform; -import org.alfresco.transform.config.TransformConfig; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import static org.alfresco.transform.base.logging.StandardMessages.COMMUNITY_LICENCE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; -import static org.alfresco.transform.base.logging.StandardMessages.COMMUNITY_LICENCE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +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; @Component public class AIOTransformEngine implements TransformEngine @@ -62,7 +63,7 @@ public class AIOTransformEngine implements TransformEngine message = transformEngines.stream() .filter(transformEngine -> transformEngine != this) .map(transformEngine -> transformEngine.getStartupMessage()) - .collect( Collectors.joining("\n")); + .collect(Collectors.joining("\n")); message = message.replace(COMMUNITY_LICENCE, ""); } return COMMUNITY_LICENCE + message; diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOImageMagickIT.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOImageMagickIT.java index 500c220e..34a2413b 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOImageMagickIT.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOImageMagickIT.java @@ -1,33 +1,32 @@ -/* - * #%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.imagemagick.ImageMagickTransformationIT; - -public class AIOImageMagickIT extends ImageMagickTransformationIT -{ -} +/* + * #%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.imagemagick.ImageMagickTransformationIT; + +public class AIOImageMagickIT extends ImageMagickTransformationIT +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOImageMagickTest.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOImageMagickTest.java index af0a544c..c23cc944 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOImageMagickTest.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOImageMagickTest.java @@ -1,36 +1,35 @@ -/* - * #%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.imagemagick.ImageMagickTest; - -/** - * Test ImageMagick functionality in All-In-One. - */ -public class AIOImageMagickTest extends ImageMagickTest -{ -} +/* + * #%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.imagemagick.ImageMagickTest; + +/** + * Test ImageMagick functionality in All-In-One. + */ +public class AIOImageMagickTest extends ImageMagickTest +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLibreOfficeTest.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLibreOfficeTest.java index 6c30d7b8..7c5d90c1 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLibreOfficeTest.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLibreOfficeTest.java @@ -1,36 +1,35 @@ -/* - * #%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.libreoffice.LibreOfficeTest; - -/** - * Test LibreOffice functionality in All-In-One. - */ -public class AIOLibreOfficeTest extends LibreOfficeTest -{ -} \ No newline at end of file +/* + * #%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.libreoffice.LibreOfficeTest; + +/** + * Test LibreOffice functionality in All-In-One. + */ +public class AIOLibreOfficeTest extends LibreOfficeTest +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLibreOfficeTransformationIT.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLibreOfficeTransformationIT.java index 7fef48ea..48e006fb 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLibreOfficeTransformationIT.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLibreOfficeTransformationIT.java @@ -1,33 +1,32 @@ -/* - * #%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.libreoffice.LibreOfficeTransformationIT; - -public class AIOLibreOfficeTransformationIT extends LibreOfficeTransformationIT -{ -} \ No newline at end of file +/* + * #%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.libreoffice.LibreOfficeTransformationIT; + +public class AIOLibreOfficeTransformationIT extends LibreOfficeTransformationIT +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLivenessReadinessProbeIT.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLivenessReadinessProbeIT.java index 5f18b308..a1bdfd1f 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLivenessReadinessProbeIT.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOLivenessReadinessProbeIT.java @@ -29,9 +29,11 @@ package org.alfresco.transform.aio; import org.alfresco.transform.base.LivenessReadinessProbeTest; -public class AIOLivenessReadinessProbeIT extends LivenessReadinessProbeTest { +public class AIOLivenessReadinessProbeIT extends LivenessReadinessProbeTest +{ @Override - protected LivenessReadinessProbeTest.ImagesForTests getImageForTest() { + protected LivenessReadinessProbeTest.ImagesForTests getImageForTest() + { return new ImagesForTests("alfresco-transform-core-aio", "text/plain", "text/plain", "original.txt"); } } diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscMetadataExtractsIT.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscMetadataExtractsIT.java index 2c8f691f..3f92cc35 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscMetadataExtractsIT.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscMetadataExtractsIT.java @@ -29,5 +29,4 @@ package org.alfresco.transform.aio; import org.alfresco.transform.misc.MiscMetadataExtractsIT; public class AIOMiscMetadataExtractsIT extends MiscMetadataExtractsIT -{ -} +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscTest.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscTest.java index 9cd3250c..66354c01 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscTest.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscTest.java @@ -1,36 +1,35 @@ -/* - * #%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.misc.MiscTest; - -/** - * Test Misc functionality in All-In-One. - */ -public class AIOMiscTest extends MiscTest -{ -} \ No newline at end of file +/* + * #%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.misc.MiscTest; + +/** + * Test Misc functionality in All-In-One. + */ +public class AIOMiscTest extends MiscTest +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscTransformsIT.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscTransformsIT.java index 35d6a759..21c0e156 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscTransformsIT.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOMiscTransformsIT.java @@ -1,33 +1,32 @@ -/* - * #%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.misc.MiscTransformsIT; - -public class AIOMiscTransformsIT extends MiscTransformsIT -{ -} +/* + * #%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.misc.MiscTransformsIT; + +public class AIOMiscTransformsIT extends MiscTransformsIT +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOPdfRendererIT.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOPdfRendererIT.java index f3685e87..51278f2a 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOPdfRendererIT.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOPdfRendererIT.java @@ -1,33 +1,32 @@ -/* - * #%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.pdfrenderer.PdfRendererTransformationIT; - -public class AIOPdfRendererIT extends PdfRendererTransformationIT -{ -} +/* + * #%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.pdfrenderer.PdfRendererTransformationIT; + +public class AIOPdfRendererIT extends PdfRendererTransformationIT +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOPdfRendererTest.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOPdfRendererTest.java index a0aed934..2c32636c 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOPdfRendererTest.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOPdfRendererTest.java @@ -1,36 +1,35 @@ -/* - * #%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.pdfrenderer.PdfRendererTest; - -/** - * Test PdfRenderer functionality in All-In-One. - */ -public class AIOPdfRendererTest extends PdfRendererTest -{ -} \ No newline at end of file +/* + * #%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.pdfrenderer.PdfRendererTest; + +/** + * Test PdfRenderer functionality in All-In-One. + */ +public class AIOPdfRendererTest extends PdfRendererTest +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOQueueIT.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOQueueIT.java index 8cdd6d5a..d827d0ca 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOQueueIT.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOQueueIT.java @@ -1,59 +1,58 @@ -/* - * #%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.messaging.AbstractQueueIT; -import org.alfresco.transform.client.model.TransformRequest; - -import java.util.UUID; - -import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; - -/** - * @author David Edwards - * created on 21/04/2020 - */ -public class AIOQueueIT extends AbstractQueueIT -{ - @Override - protected TransformRequest buildRequest() - { - return TransformRequest - .builder() - .withRequestId(UUID.randomUUID().toString()) - .withSourceMediaType(MIMETYPE_HTML) - .withTargetMediaType(MIMETYPE_TEXT_PLAIN) - .withTargetExtension("txt") - .withSchema(1) - .withClientData("ACS") - .withSourceReference(UUID.randomUUID().toString()) - .withSourceSize(32L) - .withInternalContextForTransformEngineTests() - .build(); - } -} \ No newline at end of file +/* + * #%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 static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; + +import java.util.UUID; + +import org.alfresco.transform.base.messaging.AbstractQueueIT; +import org.alfresco.transform.client.model.TransformRequest; + +/** + * @author David Edwards created on 21/04/2020 + */ +public class AIOQueueIT extends AbstractQueueIT +{ + @Override + protected TransformRequest buildRequest() + { + return TransformRequest + .builder() + .withRequestId(UUID.randomUUID().toString()) + .withSourceMediaType(MIMETYPE_HTML) + .withTargetMediaType(MIMETYPE_TEXT_PLAIN) + .withTargetExtension("txt") + .withSchema(1) + .withClientData("ACS") + .withSourceReference(UUID.randomUUID().toString()) + .withSourceSize(32L) + .withInternalContextForTransformEngineTests() + .build(); + } +} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaMetadataExtractsIT.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaMetadataExtractsIT.java index 0f03ccba..74c2ab49 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaMetadataExtractsIT.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaMetadataExtractsIT.java @@ -29,5 +29,4 @@ package org.alfresco.transform.aio; import org.alfresco.transform.tika.TikaMetadataExtractsIT; public class AIOTikaMetadataExtractsIT extends TikaMetadataExtractsIT -{ -} +{} diff --git a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaTransformationIT.java b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaTransformationIT.java index cab93c9b..e0d1661d 100644 --- a/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaTransformationIT.java +++ b/engines/aio/src/test/java/org/alfresco/transform/aio/AIOTikaTransformationIT.java @@ -1,33 +1,32 @@ -/* - * #%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.tika.TikaTransformationIT; - -public class AIOTikaTransformationIT extends TikaTransformationIT -{ -} \ No newline at end of file +/* + * #%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.tika.TikaTransformationIT; + +public class AIOTikaTransformationIT extends TikaTransformationIT +{} diff --git a/engines/base/src/main/java/org/alfresco/transform/base/Application.java b/engines/base/src/main/java/org/alfresco/transform/base/Application.java index ba43dca4..2f33ad29 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/Application.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/Application.java @@ -27,8 +27,6 @@ package org.alfresco.transform.base; import io.micrometer.core.instrument.MeterRegistry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer; diff --git a/engines/base/src/main/java/org/alfresco/transform/base/CustomTransformer.java b/engines/base/src/main/java/org/alfresco/transform/base/CustomTransformer.java index 30983a4f..7af85bd9 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/CustomTransformer.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/CustomTransformer.java @@ -26,16 +26,14 @@ */ package org.alfresco.transform.base; -import org.alfresco.transform.config.TransformConfig; - import java.io.InputStream; import java.io.OutputStream; import java.util.Map; +import org.alfresco.transform.config.TransformConfig; + /** - * Interface to be implemented by transform specific code. The {@code transformerName} should match the transformerName - * in the {@link TransformConfig} returned by the {@link TransformEngine}. So that it is automatically picked up, it - * must exist in a package under {@code org.alfresco.transform} and have the Spring {@code @Component} annotation. + * Interface to be implemented by transform specific code. The {@code transformerName} should match the transformerName in the {@link TransformConfig} returned by the {@link TransformEngine}. So that it is automatically picked up, it must exist in a package under {@code org.alfresco.transform} and have the Spring {@code @Component} annotation. * * Implementations may also use the {@link TransformManager} if they wish to interact with the base t-engine. */ @@ -44,6 +42,6 @@ public interface CustomTransformer String getTransformerName(); void transform(String sourceMimetype, InputStream inputStream, - String targetMimetype, OutputStream outputStream, - Map transformOptions, TransformManager transformManager) throws Exception; + String targetMimetype, OutputStream outputStream, + Map transformOptions, TransformManager transformManager) throws Exception; } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java b/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java index afd44b32..16e46133 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java @@ -26,15 +26,42 @@ */ package org.alfresco.transform.base; -import org.alfresco.transform.base.logging.LogEntry; -import org.alfresco.transform.base.probes.ProbeTransform; -import org.alfresco.transform.base.registry.TransformRegistry; -import org.alfresco.transform.base.transform.TransformHandler; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.registry.TransformServiceRegistry; +import static java.text.MessageFormat.format; + +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; +import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE; + +import static org.alfresco.transform.base.html.OptionsHelper.getOptionNames; +import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION; +import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION_DEFAULT; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ERROR; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LIVE; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LOG; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_READY; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ROOT; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TEST; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_VERSION; +import static org.alfresco.transform.common.RequestParamMap.FILE; +import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE; +import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE; +import static org.alfresco.transform.config.CoreVersionDecorator.setOrClearCoreVersion; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; +import jakarta.annotation.PostConstruct; +import jakarta.jms.Destination; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -57,39 +84,14 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; -import jakarta.annotation.PostConstruct; -import jakarta.jms.Destination; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; - -import static java.text.MessageFormat.format; -import static org.alfresco.transform.base.html.OptionsHelper.getOptionNames; -import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION; -import static org.alfresco.transform.common.RequestParamMap.CONFIG_VERSION_DEFAULT; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ERROR; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LIVE; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LOG; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_READY; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ROOT; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TEST; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_VERSION; -import static org.alfresco.transform.common.RequestParamMap.FILE; -import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE; -import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE; -import static org.alfresco.transform.config.CoreVersionDecorator.setOrClearCoreVersion; -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; -import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE; +import org.alfresco.transform.base.logging.LogEntry; +import org.alfresco.transform.base.probes.ProbeTransform; +import org.alfresco.transform.base.registry.TransformRegistry; +import org.alfresco.transform.base.transform.TransformHandler; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.exceptions.TransformException; /** * Provides the main endpoints into the t-engine. @@ -126,9 +128,9 @@ public class TransformController transformEngine = getTransformEngine(); logger.info("TransformEngine: {}", transformEngine.getTransformEngineName()); transformEngines.stream() - .filter(transformEngineFromStream -> transformEngineFromStream != transformEngine) - .sorted(Comparator.comparing(TransformEngine::getTransformEngineName)) - .map(sortedTransformEngine -> " "+sortedTransformEngine.getTransformEngineName()).forEach(logger::info); + .filter(transformEngineFromStream -> transformEngineFromStream != transformEngine) + .sorted(Comparator.comparing(TransformEngine::getTransformEngineName)) + .map(sortedTransformEngine -> " " + sortedTransformEngine.getTransformEngineName()).forEach(logger::info); } } @@ -138,9 +140,9 @@ public class TransformController // CustomTransform code from many t-engines into a single t-engine. In this case, there should be a wrapper // TransformEngine (it has no TransformConfig of its own). return transformEngines.stream() - .filter(transformEngineFromStream -> transformEngineFromStream.getTransformConfig() == null) - .findFirst() - .orElse(transformEngines.get(0)); + .filter(transformEngineFromStream -> transformEngineFromStream.getTransformConfig() == null) + .findFirst() + .orElse(transformEngines.get(0)); } @EventListener(ApplicationReadyEvent.class) @@ -285,8 +287,8 @@ public class TransformController @PostMapping(value = ENDPOINT_TRANSFORM, produces = APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity transform(@RequestBody TransformRequest request, - @RequestParam(value = "timeout", required = false) Long timeout, - @RequestParam(value = "replyToQueue", required = false) Destination replyToQueue) + @RequestParam(value = "timeout", required = false) Long timeout, + @RequestParam(value = "replyToQueue", required = false) Destination replyToQueue) { TransformReply reply = transformHandler.handleMessageRequest(request, timeout, replyToQueue, getProbeTransform()); return new ResponseEntity<>(reply, HttpStatus.valueOf(reply.getStatus())); @@ -317,8 +319,7 @@ public class TransformController Map requestParameters = new HashMap<>(); sourceMimetype = overrideMimetypeFromExtension(origRequestParameters, SOURCE_MIMETYPE, sourceMimetype); targetMimetype = overrideMimetypeFromExtension(origRequestParameters, TARGET_MIMETYPE, targetMimetype); - origRequestParameters.forEach((name, value) -> - { + origRequestParameters.forEach((name, value) -> { if (!name.startsWith("value")) { if (name.startsWith("name")) @@ -338,7 +339,7 @@ public class TransformController private String overrideMimetypeFromExtension(Map origRequestParameters, String name, String value) { - String override = origRequestParameters.remove("_"+ name); + String override = origRequestParameters.remove("_" + name); if (override != null && !override.isBlank()) { value = override; @@ -349,7 +350,7 @@ public class TransformController @ExceptionHandler(MissingServletRequestParameterException.class) public void handleMissingParams(HttpServletResponse response, MissingServletRequestParameterException e) - throws IOException + throws IOException { final String message = format("Request parameter ''{0}'' is missing", e.getParameterName()); logger.error(message, e); diff --git a/engines/base/src/main/java/org/alfresco/transform/base/TransformEngine.java b/engines/base/src/main/java/org/alfresco/transform/base/TransformEngine.java index 07d6ea06..63a361c8 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/TransformEngine.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/TransformEngine.java @@ -27,14 +27,11 @@ package org.alfresco.transform.base; import org.alfresco.transform.base.probes.ProbeTransform; -import org.alfresco.transform.config.reader.TransformConfigResourceReader; import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.reader.TransformConfigResourceReader; /** - * Interface to be implemented by transform specific code. Provides information about the t-engine as a whole. - * Also see {@link CustomTransformer} which provides the code that performs transformation. There may be several - * in a single t-engine. So that it is automatically picked up, it must exist in a package under - * {@code org.alfresco.transform} and have the Spring {@code @Component} annotation. + * Interface to be implemented by transform specific code. Provides information about the t-engine as a whole. Also see {@link CustomTransformer} which provides the code that performs transformation. There may be several in a single t-engine. So that it is automatically picked up, it must exist in a package under {@code org.alfresco.transform} and have the Spring {@code @Component} annotation. */ public interface TransformEngine { @@ -49,10 +46,7 @@ public interface TransformEngine String getStartupMessage(); /** - * @return a definition of what the t-engine supports. Normally read from a json Resource on the classpath using a - * {@link TransformConfigResourceReader}. To combine to code from multiple t-engine into a single t-engine - * include all the TransformEngines and CustomTransform implementations, plus a wrapper TransformEngine for the - * others. The wrapper should return {@code null} from this method. + * @return a definition of what the t-engine supports. Normally read from a json Resource on the classpath using a {@link TransformConfigResourceReader}. To combine to code from multiple t-engine into a single t-engine include all the TransformEngines and CustomTransform implementations, plus a wrapper TransformEngine for the others. The wrapper should return {@code null} from this method. */ TransformConfig getTransformConfig(); diff --git a/engines/base/src/main/java/org/alfresco/transform/base/TransformManager.java b/engines/base/src/main/java/org/alfresco/transform/base/TransformManager.java index 7ebf7663..2fa7bc57 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/TransformManager.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/TransformManager.java @@ -26,14 +26,14 @@ */ package org.alfresco.transform.base; -import org.alfresco.transform.exceptions.TransformException; - import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Map; +import org.alfresco.transform.exceptions.TransformException; + /** * Allows {@link CustomTransformer} implementations to interact with the base t-engine. */ @@ -45,42 +45,33 @@ public interface TransformManager String getRequestId(); /** - * Allows a {@link CustomTransformer} to use a local source {@code File} rather than the supplied {@code InputStream}. - * The file will be deleted once the request is completed. To avoid creating extra files, if a File has already - * been created by the base t-engine, it is returned. - * If possible this method should be avoided as it is better not to leave content on disk. - * @throws IllegalStateException if this method has already been called. + * Allows a {@link CustomTransformer} to use a local source {@code File} rather than the supplied {@code InputStream}. The file will be deleted once the request is completed. To avoid creating extra files, if a File has already been created by the base t-engine, it is returned. If possible this method should be avoided as it is better not to leave content on disk. + * + * @throws IllegalStateException + * if this method has already been called. */ File createSourceFile() throws IllegalStateException; /** - * Allows a {@link CustomTransformer} to use a local target {@code File} rather than the supplied {@code OutputStream}. - * The file will be deleted once the request is completed. To avoid creating extra files, if a File has already - * been created by the base t-engine, it is returned. - * If possible this method should be avoided as it is better not to leave content on disk. - * @throws IllegalStateException if this method has already been called. A call to {@link #respondWithFragment(Integer, boolean)} - * allows the method to be called again. + * Allows a {@link CustomTransformer} to use a local target {@code File} rather than the supplied {@code OutputStream}. The file will be deleted once the request is completed. To avoid creating extra files, if a File has already been created by the base t-engine, it is returned. If possible this method should be avoided as it is better not to leave content on disk. + * + * @throws IllegalStateException + * if this method has already been called. A call to {@link #respondWithFragment(Integer, boolean)} allows the method to be called again. */ File createTargetFile() throws IllegalStateException; /** - * Allows a single transform request to have multiple transform responses. For example, images from a video at - * different time offsets or different pages of a document. Following a call to this method a transform response is - * made with the data sent to the current {@code OutputStream}. If this method has been called, there will not be - * another response when {@link CustomTransformer#transform(String, InputStream, String, OutputStream, Map, - * TransformManager)} returns and any data written to the final {@code OutputStream} will be ignored. - * @param index returned with the response, so that the fragment may be distinguished from other responses. - * Renditions use the index as an offset into elements. A {@code null} value indicates that there - * is no more output and any data sent to the current {@code outputStream} will be ignored. - * @param finished indicates this is the final fragment. {@code False} indicates that it is expected there will be - * more fragments. There need not be a call with this parameter set to {@code true}. - * @return a new {@code OutputStream} for the next fragment. A {@code null} will be returned if {@code index} was - * {@code null} or {@code finished} was {@code true}. - * @throws TransformException if a synchronous (http) request has been made as this only works with requests - * on queues, or the first call to this method indicated there was no output, or - * another call is made after it has been indicated that there should be no more - * fragments. - * @throws IOException if there was a problem sending the response. + * Allows a single transform request to have multiple transform responses. For example, images from a video at different time offsets or different pages of a document. Following a call to this method a transform response is made with the data sent to the current {@code OutputStream}. If this method has been called, there will not be another response when {@link CustomTransformer#transform(String, InputStream, String, OutputStream, Map, TransformManager)} returns and any data written to the final {@code OutputStream} will be ignored. + * + * @param index + * returned with the response, so that the fragment may be distinguished from other responses. Renditions use the index as an offset into elements. A {@code null} value indicates that there is no more output and any data sent to the current {@code outputStream} will be ignored. + * @param finished + * indicates this is the final fragment. {@code False} indicates that it is expected there will be more fragments. There need not be a call with this parameter set to {@code true}. + * @return a new {@code OutputStream} for the next fragment. A {@code null} will be returned if {@code index} was {@code null} or {@code finished} was {@code true}. + * @throws TransformException + * if a synchronous (http) request has been made as this only works with requests on queues, or the first call to this method indicated there was no output, or another call is made after it has been indicated that there should be no more fragments. + * @throws IOException + * if there was a problem sending the response. */ // This works because all the state is in the TransformResponse and the t-router will just see each response as // something to either return to the client or pass to the next stage in a pipeline. We might be able to enhance diff --git a/engines/base/src/main/java/org/alfresco/transform/base/config/MTLSConfig.java b/engines/base/src/main/java/org/alfresco/transform/base/config/MTLSConfig.java index dd305d10..b2bfbec1 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/config/MTLSConfig.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/config/MTLSConfig.java @@ -26,7 +26,15 @@ */ package org.alfresco.transform.base.config; -import org.alfresco.transform.base.WebClientBuilderAdjuster; +import java.io.IOException; +import java.io.InputStream; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; + import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClients; @@ -52,17 +60,11 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.reactive.JettyClientHttpConnector; import org.springframework.web.client.RestTemplate; -import java.io.IOException; -import java.io.InputStream; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; +import org.alfresco.transform.base.WebClientBuilderAdjuster; @Configuration -public class MTLSConfig { +public class MTLSConfig +{ @Value("${client.ssl.key-store:#{null}}") private Resource keyStoreResource; @@ -89,7 +91,7 @@ public class MTLSConfig { public WebClientBuilderAdjuster webClientBuilderAdjuster(SslContextFactory.Client sslContextFactory) { return builder -> { - if(isTlsOrMtlsConfigured()) + if (isTlsOrMtlsConfigured()) { ClientConnector clientConnector = new ClientConnector(); clientConnector.setSslContextFactory(sslContextFactory); @@ -103,23 +105,26 @@ public class MTLSConfig { @Bean public RestTemplate restTemplate(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException { - if(isTlsOrMtlsConfigured()) + if (isTlsOrMtlsConfigured()) { return createRestTemplateWithSslContext(sslContextBuilder); - } else { + } + else + { return new RestTemplate(); } } @Bean - public SSLContextBuilder sslContextBuilder() throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, UnrecoverableKeyException { + public SSLContextBuilder sslContextBuilder() throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, UnrecoverableKeyException + { SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); - if(isKeystoreConfigured()) + if (isKeystoreConfigured()) { KeyStore keyStore = getKeyStore(keyStoreType, keyStoreResource, keyStorePassword); sslContextBuilder.loadKeyMaterial(keyStore, keyStorePassword); } - if(isTruststoreConfigured()) + if (isTruststoreConfigured()) { sslContextBuilder .setKeyStoreType(trustStoreType) @@ -156,20 +161,20 @@ public class MTLSConfig { return keyStoreResource != null; } - private RestTemplate createRestTemplateWithSslContext(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException { - final SSLConnectionSocketFactoryBuilder sslConnectionSocketFactoryBuilder = - SSLConnectionSocketFactoryBuilder.create() - .setSslContext(sslContextBuilder.build()) - .setTlsVersions(TLS.V_1_2, TLS.V_1_3); - if (hostNameVerificationDisabled) { + private RestTemplate createRestTemplateWithSslContext(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException + { + final SSLConnectionSocketFactoryBuilder sslConnectionSocketFactoryBuilder = SSLConnectionSocketFactoryBuilder.create() + .setSslContext(sslContextBuilder.build()) + .setTlsVersions(TLS.V_1_2, TLS.V_1_3); + if (hostNameVerificationDisabled) + { sslConnectionSocketFactoryBuilder.setHostnameVerifier(NoopHostnameVerifier.INSTANCE); } final SSLConnectionSocketFactory sslConnectionSocketFactory = sslConnectionSocketFactoryBuilder.build(); - final Registry sslSocketFactoryRegistry = - RegistryBuilder. create() - .register("https", sslConnectionSocketFactory) - .build(); + final Registry sslSocketFactoryRegistry = RegistryBuilder. create() + .register("https", sslConnectionSocketFactory) + .build(); final PoolingHttpClientConnectionManager sslConnectionManager = new PoolingHttpClientConnectionManager(sslSocketFactoryRegistry); diff --git a/engines/base/src/main/java/org/alfresco/transform/base/config/WebApplicationConfig.java b/engines/base/src/main/java/org/alfresco/transform/base/config/WebApplicationConfig.java index 57f61390..61210978 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/config/WebApplicationConfig.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/config/WebApplicationConfig.java @@ -26,10 +26,12 @@ */ package org.alfresco.transform.base.config; -import org.alfresco.transform.base.html.TransformInterceptor; -import org.alfresco.transform.base.registry.TransformConfigSource; -import org.alfresco.transform.common.TransformerDebug; -import org.alfresco.transform.messages.TransformRequestValidator; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; +import static org.alfresco.transform.config.CoreFunction.standardizeCoreVersion; + +import java.util.ArrayList; +import java.util.List; + import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -38,11 +40,10 @@ import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import java.util.ArrayList; -import java.util.List; - -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; -import static org.alfresco.transform.config.CoreFunction.standardizeCoreVersion; +import org.alfresco.transform.base.html.TransformInterceptor; +import org.alfresco.transform.base.registry.TransformConfigSource; +import org.alfresco.transform.common.TransformerDebug; +import org.alfresco.transform.messages.TransformRequestValidator; @Configuration @ComponentScan( diff --git a/engines/base/src/main/java/org/alfresco/transform/base/executors/AbstractCommandExecutor.java b/engines/base/src/main/java/org/alfresco/transform/base/executors/AbstractCommandExecutor.java index e024ac7f..93053dd1 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/executors/AbstractCommandExecutor.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/executors/AbstractCommandExecutor.java @@ -26,10 +26,11 @@ */ package org.alfresco.transform.base.executors; -import static org.alfresco.transform.base.executors.RuntimeExec.ExecutionResult; import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.alfresco.transform.base.executors.RuntimeExec.ExecutionResult; + import java.io.File; import java.util.Map; diff --git a/engines/base/src/main/java/org/alfresco/transform/base/executors/CommandExecutor.java b/engines/base/src/main/java/org/alfresco/transform/base/executors/CommandExecutor.java index 5e279e1a..9c3809a7 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/executors/CommandExecutor.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/executors/CommandExecutor.java @@ -26,12 +26,12 @@ */ package org.alfresco.transform.base.executors; -import org.alfresco.transform.base.logging.LogEntry; - import java.io.File; import java.util.HashMap; import java.util.Map; +import org.alfresco.transform.base.logging.LogEntry; + /** * Basic interface for executing transformations via Shell commands * diff --git a/engines/base/src/main/java/org/alfresco/transform/base/executors/ExecParameterTokenizer.java b/engines/base/src/main/java/org/alfresco/transform/base/executors/ExecParameterTokenizer.java index 4ea7af49..e48b7c55 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/executors/ExecParameterTokenizer.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/executors/ExecParameterTokenizer.java @@ -35,24 +35,17 @@ import java.util.Objects; import java.util.StringTokenizer; /** - * This class is used to tokenize strings used as parameters for {@link RuntimeExec} objects. - * Examples of such strings are as follows (ImageMagick-like parameters): + * This class is used to tokenize strings used as parameters for {@link RuntimeExec} objects. Examples of such strings are as follows (ImageMagick-like parameters): *
    *
  • -font Helvetica -pointsize 50
  • *
  • -font Helvetica -pointsize 50 -draw "circle 100,100 150,150"
  • *
  • -font Helvetica -pointsize 50 -draw "gravity south fill black text 0,12 'CopyRight'"
  • *
- * The first is the simple case which would be parsed into Strings as follows: - * "-font", "Helvetica", "-pointsize", "50" + * The first is the simple case which would be parsed into Strings as follows: "-font", "Helvetica", "-pointsize", "50" *

- * The second is more complex in that it includes a quoted parameter, which would be parsed as a single String: - * "-font", "Helvetica", "-pointsize", "50", "circle 100,100 150,150" - * Note however that the quotation characters will be stripped from the token. + * The second is more complex in that it includes a quoted parameter, which would be parsed as a single String: "-font", "Helvetica", "-pointsize", "50", "circle 100,100 150,150" Note however that the quotation characters will be stripped from the token. *

- * The third shows an example with embedded quotation marks, which would parse to: - * "-font", "Helvetica", "-pointsize", "50", "gravity south fill black text 0,12 'CopyRight'" - * In this case, the embedded quotation marks (which must be different from those surrounding the parameter) - * are preserved in the extracted token. + * The third shows an example with embedded quotation marks, which would parse to: "-font", "Helvetica", "-pointsize", "50", "gravity south fill black text 0,12 'CopyRight'" In this case, the embedded quotation marks (which must be different from those surrounding the parameter) are preserved in the extracted token. *

* The class does not understand escaped quotes such as p1 p2 "a b c \"hello\" d" p4 * @@ -77,15 +70,12 @@ public class ExecParameterTokenizer } /** - * This method returns the tokens in a parameter string. - * Any tokens not contained within single or double quotes will be tokenized in the normal - * way i.e. by using whitespace separators and the standard StringTokenizer algorithm. - * Any tokens which are contained within single or double quotes will be returned as single - * String instances and will have their quote marks removed. + * This method returns the tokens in a parameter string. Any tokens not contained within single or double quotes will be tokenized in the normal way i.e. by using whitespace separators and the standard StringTokenizer algorithm. Any tokens which are contained within single or double quotes will be returned as single String instances and will have their quote marks removed. *

* See above for examples. * - * @throws NullPointerException if the string to be tokenized was null. + * @throws NullPointerException + * if the string to be tokenized was null. */ public List getAllTokens() { @@ -103,7 +93,7 @@ public class ExecParameterTokenizer { // Contains no quotes. for (StringTokenizer standardTokenizer = new StringTokenizer( - str); standardTokenizer.hasMoreTokens(); ) + str); standardTokenizer.hasMoreTokens();) { tokens.add(standardTokenizer.nextToken()); } @@ -114,7 +104,7 @@ public class ExecParameterTokenizer // So we need to identify the quoted regions within the string. List> quotedRegions = new ArrayList<>(); - for (Pair next = identifyNextQuotedRegion(str, 0); next != null; ) + for (Pair next = identifyNextQuotedRegion(str, 0); next != null;) { quotedRegions.add(next); next = identifyNextQuotedRegion(str, next.getSecond() + 1); @@ -135,12 +125,10 @@ public class ExecParameterTokenizer } /** - * The substrings will be a list of quoted and unquoted substrings. - * The unquoted ones need to be further tokenized in the normal way. - * The quoted ones must not be tokenized, but need their quotes stripped off. + * The substrings will be a list of quoted and unquoted substrings. The unquoted ones need to be further tokenized in the normal way. The quoted ones must not be tokenized, but need their quotes stripped off. */ private List getSubstrings(String str, - List> quotedRegionIndices) + List> quotedRegionIndices) { List result = new ArrayList<>(); @@ -151,10 +139,10 @@ public class ExecParameterTokenizer { int startIndexOfNextQuotedRegion = nextQuotedRegionIndices.getFirst() - 1; result.add(new UnquotedSubstring( - str.substring(cursorPosition, startIndexOfNextQuotedRegion))); + str.substring(cursorPosition, startIndexOfNextQuotedRegion))); } result.add(new QuotedSubstring(str.substring(nextQuotedRegionIndices.getFirst(), - nextQuotedRegionIndices.getSecond()))); + nextQuotedRegionIndices.getSecond()))); cursorPosition = nextQuotedRegionIndices.getSecond(); } @@ -198,12 +186,12 @@ public class ExecParameterTokenizer } private Pair findIndexOfClosingQuote(String str, int indexOfStartingQuote, - char quoteChar) + char quoteChar) { // So we know which type of quote char we're dealing with. Either ' or ". // Now we need to find the closing quote. int indexAfterClosingQuote = str.indexOf(quoteChar, - indexOfStartingQuote + 1) + 1; // + 1 to search after opening quote. + 1 to give result including closing quote. + indexOfStartingQuote + 1) + 1; // + 1 to search after opening quote. + 1 to give result including closing quote. if (indexAfterClosingQuote == 0) // -1 + 1 { @@ -295,8 +283,10 @@ public class ExecParameterTokenizer /** * Make a new one. * - * @param first The first member. - * @param second The second member. + * @param first + * The first member. + * @param second + * The second member. */ public Pair(F first, S second) { @@ -334,16 +324,20 @@ public class ExecParameterTokenizer this.second = second; } - @Override public boolean equals(Object o) + @Override + public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; Pair pair = (Pair) o; return Objects.equals(first, pair.first) && - Objects.equals(second, pair.second); + Objects.equals(second, pair.second); } - @Override public int hashCode() + @Override + public int hashCode() { return Objects.hash(first, second); } @@ -355,4 +349,3 @@ public class ExecParameterTokenizer } } } - diff --git a/engines/base/src/main/java/org/alfresco/transform/base/executors/RuntimeExec.java b/engines/base/src/main/java/org/alfresco/transform/base/executors/RuntimeExec.java index d578c947..672ba677 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/executors/RuntimeExec.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/executors/RuntimeExec.java @@ -50,51 +50,50 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * This acts as a session similar to the java.lang.Process, but - * logs the system standard and error streams. + * This acts as a session similar to the java.lang.Process, but logs the system standard and error streams. *

- * The bean can be configured to execute a command directly, or be given a map - * of commands keyed by the os.name Java system property. In this map, - * the default key that is used when no match is found is the - * {@link #KEY_OS_DEFAULT *} key. + * The bean can be configured to execute a command directly, or be given a map of commands keyed by the os.name Java system property. In this map, the default key that is used when no match is found is the {@link #KEY_OS_DEFAULT *} key. *

- * Use the {@link #setProcessDirectory(String) processDirectory} property to change the default location - * from which the command executes. The process's environment can be configured using the - * {@link #setProcessProperties(Map) processProperties} property. + * Use the {@link #setProcessDirectory(String) processDirectory} property to change the default location from which the command executes. The process's environment can be configured using the {@link #setProcessProperties(Map) processProperties} property. *

* Commands may use placeholders, e.g. - *


+ * 
+ * 
+ * 
  *    find
  *    -name
  *    ${filename}
- * 
- * The filename property will be substituted for any supplied value prior to - * each execution of the command. Currently, no checks are made to get or check the - * properties contained within the command string. It is up to the client code to - * dynamically extract the properties required if the required properties are not - * known up front. + *
+ *
+ * + * The filename property will be substituted for any supplied value prior to each execution of the command. Currently, no checks are made to get or check the properties contained within the command string. It is up to the client code to dynamically extract the properties required if the required properties are not known up front. *

- * Sometimes, a variable may contain several arguments. . In this case, the arguments - * need to be tokenized using a standard StringTokenizer. To force tokenization - * of a value, use: - *


+ * Sometimes, a variable may contain several arguments. . In this case, the arguments need to be tokenized using a standard StringTokenizer. To force tokenization of a value, use:
+ * 
+ * 
+ * 
  *    SPLIT:${userArgs}
- * 
- * You should not use this just to split up arguments that are known to require tokenization - * up front. The SPLIT: directive works for the entire argument and will not do anything - * if it is not at the beginning of the argument. Do not use SPLIT: to break up arguments - * that are fixed, so avoid doing this: - *

+ * 
+ * 
+ * + * You should not use this just to split up arguments that are known to require tokenization up front. The SPLIT: directive works for the entire argument and will not do anything if it is not at the beginning of the argument. Do not use SPLIT: to break up arguments that are fixed, so avoid doing this: + * + *
+ * 
  *    SPLIT:ls -lih
- * 
+ *
+ *
+ * * Instead, break the command up explicitly: - *

+ * 
+ * 
+ * 
  *    ls
  *    -lih
- * 
+ *
+ *
* - * Tokenization of quoted parameter values is handled by ExecParameterTokenizer, which - * describes the support in more detail. + * Tokenization of quoted parameter values is handled by ExecParameterTokenizer, which describes the support in more detail. * * @author Derek Hulley */ @@ -123,7 +122,7 @@ public class RuntimeExec private final Timer timer = new Timer(true); /** - * Default constructor. Initialize this instance by setting individual properties. + * Default constructor. Initialize this instance by setting individual properties. */ public RuntimeExec() { @@ -151,15 +150,16 @@ public class RuntimeExec sb.append("\n"); } sb.append(" env props: ").append(Arrays.toString(processProperties)).append("\n") - .append(" dir: ").append(processDirectory).append("\n") - .append(" os: ").append(System.getProperty(KEY_OS_NAME)).append("\n"); + .append(" dir: ").append(processDirectory).append("\n") + .append(" os: ").append(System.getProperty(KEY_OS_NAME)).append("\n"); return sb.toString(); } /** * Set the command to execute regardless of operating system * - * @param command an array of strings representing the command (first entry) and arguments + * @param command + * an array of strings representing the command (first entry) and arguments * @since 3.0 */ public void setCommand(String[] command) @@ -168,11 +168,12 @@ public class RuntimeExec } /** - * Sets the assumed charset of OUT and ERR streams generated by the executed command. - * This defaults to the system default charset: {@link Charset#defaultCharset()}. + * Sets the assumed charset of OUT and ERR streams generated by the executed command. This defaults to the system default charset: {@link Charset#defaultCharset()}. * - * @param charsetCode a supported character set code - * @throws UnsupportedCharsetException if the characterset code is not recognised by Java + * @param charsetCode + * a supported character set code + * @throws UnsupportedCharsetException + * if the characterset code is not recognised by Java */ public void setCharset(String charsetCode) { @@ -180,14 +181,10 @@ public class RuntimeExec } /** - * Set whether to wait for completion of the command or not. If there is no wait for completion, - * then the return value of out and err buffers cannot be relied upon as the - * command may still be in progress. Failure is therefore not possible unless the calling thread - * waits for execution. + * Set whether to wait for completion of the command or not. If there is no wait for completion, then the return value of out and err buffers cannot be relied upon as the command may still be in progress. Failure is therefore not possible unless the calling thread waits for execution. * - * @param waitForCompletion true (default) is to wait for the command to exit, - * or false to just return an exit code of 0 and whatever - * output is available at that point. + * @param waitForCompletion + * true (default) is to wait for the command to exit, or false to just return an exit code of 0 and whatever output is available at that point. * @since 2.1 */ public void setWaitForCompletion(boolean waitForCompletion) @@ -196,26 +193,17 @@ public class RuntimeExec } /** - * Supply a choice of commands to execute based on a mapping from the os.name system - * property to the command to execute. The {@link #KEY_OS_DEFAULT *} key can be used - * to get a command where there is not direct match to the operating system key. + * Supply a choice of commands to execute based on a mapping from the os.name system property to the command to execute. The {@link #KEY_OS_DEFAULT *} key can be used to get a command where there is not direct match to the operating system key. *

- * Each command is an array of strings, the first of which represents the command and all subsequent - * entries in the array represent the arguments. All elements of the array will be checked for - * the presence of any substitution parameters (e.g. '{dir}'). The parameters can be set using the - * {@link #setDefaultProperties(Map) defaults} or by passing the substitution values into the - * {@link #execute(Map)} command. + * Each command is an array of strings, the first of which represents the command and all subsequent entries in the array represent the arguments. All elements of the array will be checked for the presence of any substitution parameters (e.g. '{dir}'). The parameters can be set using the {@link #setDefaultProperties(Map) defaults} or by passing the substitution values into the {@link #execute(Map)} command. *

- * If parameters passed may be multiple arguments, or if the values provided in the map are themselves - * collections of arguments (not recommended), then prefix the value with SPLIT: to ensure that - * the value is tokenized before being passed to the command. Any values that are not split, will be - * passed to the command as single arguments. For example:
+ * If parameters passed may be multiple arguments, or if the values provided in the map are themselves collections of arguments (not recommended), then prefix the value with SPLIT: to ensure that the value is tokenized before being passed to the command. Any values that are not split, will be passed to the command as single arguments. For example:
* 'SPLIT: dir . ..' becomes 'dir', '.' and '..'.
* 'SPLIT: dir ${path}' (if path is '. ..') becomes 'dir', '.' and '..'.
- * The splitting occurs post-subtitution. Where the arguments are known, it is advisable to avoid - * SPLIT:. + * The splitting occurs post-subtitution. Where the arguments are known, it is advisable to avoid SPLIT:. * - * @param commandsByOS a map of command string arrays, keyed by operating system names + * @param commandsByOS + * a map of command string arrays, keyed by operating system names * @see #setDefaultProperties(Map) * @since 3.0 */ @@ -230,7 +218,7 @@ public class RuntimeExec // go through the commands keys, looking for one that matches by regular expression matching for (String osName : commandsByOS.keySet()) { - // Ignore * options. It is dealt with later. + // Ignore * options. It is dealt with later. if (osName.equals(KEY_OS_DEFAULT)) { continue; @@ -252,26 +240,25 @@ public class RuntimeExec if (command == null) { throw new RuntimeException( - "No command found for OS " + serverOs + " or '" + KEY_OS_DEFAULT + "': \n" + - " commands: " + commandsByOS); + "No command found for OS " + serverOs + " or '" + KEY_OS_DEFAULT + "': \n" + + " commands: " + commandsByOS); } this.command = command; } /** - * Supply a choice of commands to execute based on a mapping from the os.name system - * property to the command to execute. The {@link #KEY_OS_DEFAULT *} key can be used - * to get a command where there is not direct match to the operating system key. + * Supply a choice of commands to execute based on a mapping from the os.name system property to the command to execute. The {@link #KEY_OS_DEFAULT *} key can be used to get a command where there is not direct match to the operating system key. * - * @param commandsByOS a map of command string keyed by operating system names + * @param commandsByOS + * a map of command string keyed by operating system names * @deprecated Use {@link #setCommandsAndArguments(Map)} */ public void setCommandMap(Map commandsByOS) { // This is deprecated, so issue a warning logger.warn( - "The bean RuntimeExec property 'commandMap' has been deprecated;" + - " use 'commandsAndArguments' instead. See https://issues.alfresco.com/jira/browse/ETHREEOH-579."); + "The bean RuntimeExec property 'commandMap' has been deprecated;" + + " use 'commandsAndArguments' instead. See https://issues.alfresco.com/jira/browse/ETHREEOH-579."); Map fixed = new LinkedHashMap<>(); for (Map.Entry entry : commandsByOS.entrySet()) { @@ -289,14 +276,12 @@ public class RuntimeExec } /** - * Set the default command-line properties to use when executing the command. - * These are properties that substitute variables defined in the command string itself. - * Properties supplied during execution will overwrite the default properties. + * Set the default command-line properties to use when executing the command. These are properties that substitute variables defined in the command string itself. Properties supplied during execution will overwrite the default properties. *

- * null properties will be treated as an empty string for substitution - * purposes. + * null properties will be treated as an empty string for substitution purposes. * - * @param defaultProperties property values + * @param defaultProperties + * property values */ public void setDefaultProperties(Map defaultProperties) { @@ -304,14 +289,12 @@ public class RuntimeExec } /** - * Set additional runtime properties (environment properties) that will used - * by the executing process. + * Set additional runtime properties (environment properties) that will used by the executing process. *

- * Any keys or properties that start and end with ${...} will be removed on the assumption - * that these are unset properties. null values are translated to empty strings. - * All keys and values are trimmed of leading and trailing whitespace. + * Any keys or properties that start and end with ${...} will be removed on the assumption that these are unset properties. null values are translated to empty strings. All keys and values are trimmed of leading and trailing whitespace. * - * @param processProperties Runtime process properties + * @param processProperties + * Runtime process properties * @see Runtime#exec(String, String[], java.io.File) */ public void setProcessProperties(Map processProperties) @@ -368,13 +351,12 @@ public class RuntimeExec } /** - * Adds a property to existed processProperties. - * Property should not be null or empty. - * If property with the same value already exists then no change is made. - * If property exists with a different value then old value is replaced with the new one. + * Adds a property to existed processProperties. Property should not be null or empty. If property with the same value already exists then no change is made. If property exists with a different value then old value is replaced with the new one. * - * @param name - property name - * @param value - property value + * @param name + * - property name + * @param value + * - property value */ public void setProcessProperty(String name, String value) { @@ -424,10 +406,10 @@ public class RuntimeExec /** * Set the runtime location from which the command is executed. *

- * If the value is an unsubsititued variable (${...}) then it is ignored. - * If the location is not visible at the time of setting, a warning is issued only. + * If the value is an unsubsititued variable (${...}) then it is ignored. If the location is not visible at the time of setting, a warning is issued only. * - * @param processDirectory the runtime location from which to execute the command + * @param processDirectory + * the runtime location from which to execute the command */ public void setProcessDirectory(String processDirectory) { @@ -441,17 +423,18 @@ public class RuntimeExec if (!this.processDirectory.exists()) { logger.warn( - "The runtime process directory is not visible when setting property " + - "'processDirectory': \n{}", this); + "The runtime process directory is not visible when setting property " + + "'processDirectory': \n{}", + this); } } } /** - * A comma or space separated list of values that, if returned by the executed command, - * indicate an error value. This defaults to "1, 2". + * A comma or space separated list of values that, if returned by the executed command, indicate an error value. This defaults to "1, 2". * - * @param errCodesStr the error codes for the execution + * @param errCodesStr + * the error codes for the execution */ public void setErrorCodes(String errCodesStr) { @@ -469,7 +452,7 @@ public class RuntimeExec catch (NumberFormatException e) { throw new RuntimeException( - "Property 'errorCodes' must be comma-separated list of integers: " + errCodesStr); + "Property 'errorCodes' must be comma-separated list of integers: " + errCodesStr); } } } @@ -487,9 +470,8 @@ public class RuntimeExec /** * Executes the statement that this instance was constructed with. * - * @param properties the properties that the command might be executed with. - * null properties will be treated as an empty string for substitution - * purposes. + * @param properties + * the properties that the command might be executed with. null properties will be treated as an empty string for substitution purposes. * @return Returns the full execution results */ public ExecutionResult execute(Map properties) @@ -498,15 +480,12 @@ public class RuntimeExec } /** - * Executes the statement that this instance was constructed with an optional - * timeout after which the command is asked to + * Executes the statement that this instance was constructed with an optional timeout after which the command is asked to * - * @param properties the properties that the command might be executed with. - * null properties will be treated as an empty string for substitution - * purposes. - * @param timeoutMs a timeout after which {@link Process#destroy()} is called. - * ignored if less than or equal to zero. Note this method does not guarantee - * to terminate the process (it is not a kill -9). + * @param properties + * the properties that the command might be executed with. null properties will be treated as an empty string for substitution purposes. + * @param timeoutMs + * a timeout after which {@link Process#destroy()} is called. ignored if less than or equal to zero. Note this method does not guarantee to terminate the process (it is not a kill -9). * @return Returns the full execution results */ public ExecutionResult execute(Map properties, final long timeoutMs) @@ -528,13 +507,12 @@ public class RuntimeExec // execute the command with full property replacement commandToExecute = getCommand(properties); final Process thisProcess = runtime.exec(commandToExecute, processProperties, - processDirectory); + processDirectory); process = thisProcess; if (timeoutMs > 0) { final String[] command = commandToExecute; - timer.schedule(new TimerTask() - { + timer.schedule(new TimerTask() { @Override public void run() { @@ -546,8 +524,8 @@ public class RuntimeExec catch (IllegalThreadStateException stillRunning) { logger.debug( - "Process has taken too long ({} seconds). Killing process {}", - timeoutMs / 1000, Arrays.deepToString(command)); + "Process has taken too long ({} seconds). Killing process {}", + timeoutMs / 1000, Arrays.deepToString(command)); } } }, timeoutMs); @@ -559,16 +537,16 @@ public class RuntimeExec String execOut = ""; String execErr = e.getMessage(); ExecutionResult result = new ExecutionResult(null, commandToExecute, errCodes, - defaultFailureExitValue, execOut, execErr); + defaultFailureExitValue, execOut, execErr); logFullEnvironmentDump(result); return result; } // create the stream gobblers InputStreamReaderThread stdOutGobbler = new InputStreamReaderThread( - process.getInputStream(), charset); + process.getInputStream(), charset); InputStreamReaderThread stdErrGobbler = new InputStreamReaderThread( - process.getErrorStream(), charset); + process.getErrorStream(), charset); // start gobbling stdOutGobbler.start(); @@ -603,7 +581,7 @@ public class RuntimeExec // construct the return value ExecutionResult result = new ExecutionResult(process, commandToExecute, errCodes, exitValue, - execOut, execErr); + execOut, execErr); // done logFullEnvironmentDump(result); @@ -646,12 +624,11 @@ public class RuntimeExec } logger.debug("Result: " + result.toString()); - // close output stream (connected to input stream of native subprocess) + // close output stream (connected to input stream of native subprocess) } /** - * @return Returns the command that will be executed if no additional properties - * were to be supplied + * @return Returns the command that will be executed if no additional properties were to be supplied */ public String[] getCommand() { @@ -661,12 +638,11 @@ public class RuntimeExec /** * Get the command that will be executed post substitution. *

- * null properties will be treated as an empty string for substitution - * purposes. + * null properties will be treated as an empty string for substitution purposes. * - * @param properties the properties that the command might be executed with - * @return Returns the command that will be executed should the additional properties - * be supplied + * @param properties + * the properties that the command might be executed with + * @return Returns the command that will be executed should the additional properties be supplied */ public String[] getCommand(Map properties) { @@ -715,7 +691,7 @@ public class RuntimeExec // There may be quoted arguments here (see ALF-7482) ExecParameterTokenizer quoteAwareTokenizer = new ExecParameterTokenizer( - unsplitAdjustedValue); + unsplitAdjustedValue); List tokens = quoteAwareTokenizer.getAllTokens(); adjustedCommandElements.addAll(tokens); } @@ -743,15 +719,16 @@ public class RuntimeExec private final String stdErr; /** - * @param process the process attached to Java - null is allowed + * @param process + * the process attached to Java - null is allowed */ private ExecutionResult( - final Process process, - final String[] command, - final Set errCodes, - final int exitValue, - final String stdOut, - final String stdErr) + final Process process, + final String[] command, + final Set errCodes, + final int exitValue, + final String stdOut, + final String stdErr) { this.process = process; this.command = command; @@ -769,20 +746,18 @@ public class RuntimeExec StringBuilder sb = new StringBuilder(128); sb.append("Execution result: \n") - .append(" os: ").append(System.getProperty(KEY_OS_NAME)).append("\n") - .append(" command: "); + .append(" os: ").append(System.getProperty(KEY_OS_NAME)).append("\n") + .append(" command: "); appendCommand(sb, command).append("\n") - .append(" succeeded: ").append(getSuccess()).append("\n") - .append(" exit code: ").append(exitValue).append("\n") - .append(" out: ").append(out).append("\n") - .append(" err: ").append(err); + .append(" succeeded: ").append(getSuccess()).append("\n") + .append(" exit code: ").append(exitValue).append("\n") + .append(" out: ").append(out).append("\n") + .append(" err: ").append(err); return sb.toString(); } /** - * Appends the command in a form that make running from the command line simpler. - * It is not a real attempt at making a command given all the operating system - * and shell options, but makes copy, paste and edit a bit simpler. + * Appends the command in a form that make running from the command line simpler. It is not a real attempt at making a command given all the operating system and shell options, but makes copy, paste and edit a bit simpler. */ private StringBuilder appendCommand(StringBuilder sb, String[] command) { @@ -818,10 +793,7 @@ public class RuntimeExec } /** - * A helper method to force a kill of the process that generated this result. This is - * useful in cases where the process started is not expected to exit, or doesn't exit - * quickly. If the {@linkplain RuntimeExec#setWaitForCompletion(boolean) "wait for completion"} - * flag is false then the process may still be running when this result is returned. + * A helper method to force a kill of the process that generated this result. This is useful in cases where the process started is not expected to exit, or doesn't exit quickly. If the {@linkplain RuntimeExec#setWaitForCompletion(boolean) "wait for completion"} flag is false then the process may still be running when this result is returned. * * @return true if the process was killed, otherwise false */ @@ -844,7 +816,8 @@ public class RuntimeExec } /** - * @param exitValue the command exit value + * @param exitValue + * the command exit value * @return Returns true if the code is a listed failure code * @see #setErrorCodes(String) */ @@ -854,8 +827,7 @@ public class RuntimeExec } /** - * @return Returns true if the command was deemed to be successful according to the - * failure codes returned by the execution. + * @return Returns true if the command was deemed to be successful according to the failure codes returned by the execution. */ public boolean getSuccess() { @@ -879,8 +851,7 @@ public class RuntimeExec } /** - * Gobbles an InputStream and writes it into a - * StringBuffer + * Gobbles an InputStream and writes it into a StringBuffer *

* The reading of the input stream is buffered. */ @@ -888,12 +859,12 @@ public class RuntimeExec { private final InputStream is; private final Charset charset; - private final StringBuffer buffer; // we require the synchronization + private final StringBuffer buffer; // we require the synchronization private boolean completed; /** - * @param is an input stream to read - it will be wrapped in a buffer - * for reading + * @param is + * an input stream to read - it will be wrapped in a buffer for reading */ public InputStreamReaderThread(InputStream is, Charset charset) { @@ -935,7 +906,7 @@ public class RuntimeExec // The thread has finished consuming the stream completed = true; // Notify waiters - this.notifyAll(); // Note: Method is synchronized + this.notifyAll(); // Note: Method is synchronized } } @@ -954,13 +925,13 @@ public class RuntimeExec this.wait(1000L); // 200 ms } catch (InterruptedException ignore) - { - } + {} } } /** - * @param msg the message to add to the buffer + * @param msg + * the message to add to the buffer */ public void addToBuffer(String msg) { @@ -981,4 +952,3 @@ public class RuntimeExec } } } - diff --git a/engines/base/src/main/java/org/alfresco/transform/base/html/OptionsHelper.java b/engines/base/src/main/java/org/alfresco/transform/base/html/OptionsHelper.java index a995ad3c..a3a977a2 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/html/OptionsHelper.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/html/OptionsHelper.java @@ -26,15 +26,16 @@ */ package org.alfresco.transform.base.html; -import org.alfresco.transform.config.TransformOption; -import org.alfresco.transform.config.TransformOptionGroup; -import org.alfresco.transform.config.TransformOptionValue; -import org.springframework.stereotype.Component; - import java.util.Map; import java.util.Set; import java.util.TreeSet; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.TransformOptionGroup; +import org.alfresco.transform.config.TransformOptionValue; + /** * Used in the html test page, which provides a list of known transform option names. */ @@ -42,14 +43,12 @@ import java.util.TreeSet; public class OptionsHelper { private OptionsHelper() - { - } + {} public static Set getOptionNames(Map> transformOptionsByName) { Set set = new TreeSet<>(); - transformOptionsByName.forEach(((optionName, optionSet) -> - optionSet.stream().forEach(option -> addOption(set, option)))); + transformOptionsByName.forEach(((optionName, optionSet) -> optionSet.stream().forEach(option -> addOption(set, option)))); return set; } @@ -57,11 +56,11 @@ public class OptionsHelper { if (option instanceof TransformOptionGroup) { - addGroup(set, (TransformOptionGroup)option); + addGroup(set, (TransformOptionGroup) option); } else { - addValue(set, (TransformOptionValue)option); + addValue(set, (TransformOptionValue) option); } } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/html/TransformInterceptor.java b/engines/base/src/main/java/org/alfresco/transform/base/html/TransformInterceptor.java index 0bac36e8..c8a09c58 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/html/TransformInterceptor.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/html/TransformInterceptor.java @@ -42,7 +42,7 @@ public class TransformInterceptor implements AsyncHandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, - HttpServletResponse response, Object handler, Exception ex) + HttpServletResponse response, Object handler, Exception ex) { deleteFile(request, SOURCE_FILE); deleteFile(request, TARGET_FILE); diff --git a/engines/base/src/main/java/org/alfresco/transform/base/logging/LogEntry.java b/engines/base/src/main/java/org/alfresco/transform/base/logging/LogEntry.java index eb4c4bdf..b5757743 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/logging/LogEntry.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/logging/LogEntry.java @@ -26,10 +26,9 @@ */ package org.alfresco.transform.base.logging; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; +import static java.lang.Math.max; + +import static org.springframework.http.HttpStatus.OK; import java.text.SimpleDateFormat; import java.util.Collection; @@ -38,14 +37,13 @@ import java.util.Deque; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.atomic.AtomicInteger; -import static java.lang.Math.max; -import static org.springframework.http.HttpStatus.OK; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; /** - * Provides setter and getter methods to allow the current Thread to set various log properties and for these - * values to be retrieved. The {@link #complete()} method should be called at the end of a request to flush the - * current entry to an internal log Collection of the latest entries. The {@link #getLog()} method is used to obtain - * access to this collection. + * Provides setter and getter methods to allow the current Thread to set various log properties and for these values to be retrieved. The {@link #complete()} method should be called at the end of a request to flush the current entry to an internal log Collection of the latest entries. The {@link #getLog()} method is used to obtain access to this collection. */ public final class LogEntry { @@ -166,7 +164,7 @@ public final class LogEntry if (logEntry.statusCode == OK.value()) { logEntry.durationStreamOut = System.currentTimeMillis() - logEntry.start - - logEntry.durationStreamIn - max(logEntry.durationTransform, 0); + logEntry.durationStreamIn - max(logEntry.durationTransform, 0); } currentLogEntry.remove(); @@ -195,13 +193,14 @@ public final class LogEntry { long duration = durationStreamIn + max(durationTransform, 0) + max(durationStreamOut, 0); return duration <= 5 - ? "" - : time(duration) + - " (" + - (time(durationStreamIn) + ' ' + - time(durationTransform) + ' ' + - time(durationStreamOut)).trim() + - ")"; + ? "" + : time(duration) + + " (" + + (time(durationStreamIn) + ' ' + + time(durationTransform) + ' ' + + time(durationStreamOut)).trim() + + + ")"; } public String getSource() @@ -236,16 +235,18 @@ public final class LogEntry private String time(long ms) { - return ms == -1 ? "" : size(ms, "1ms", - new String[]{"ms", "s", "min", "hr"}, - new long[]{1000, 60 * 1000, 60 * 60 * 1000, Long.MAX_VALUE}); + return ms == -1 ? "" + : size(ms, "1ms", + new String[]{"ms", "s", "min", "hr"}, + new long[]{1000, 60 * 1000, 60 * 60 * 1000, Long.MAX_VALUE}); } private String size(long size) { - return size == -1 ? "" : size(size, "1 byte", - new String[]{"bytes", " KB", " MB", " GB", " TB"}, - new long[]{1024, 1024 * 1024, 1024 * 1024 * 1024, 1024L * 1024 * 1024 * 1024, Long.MAX_VALUE}); + return size == -1 ? "" + : size(size, "1 byte", + new String[]{"bytes", " KB", " MB", " GB", " TB"}, + new long[]{1024, 1024 * 1024, 1024 * 1024 * 1024, 1024L * 1024 * 1024 * 1024, Long.MAX_VALUE}); } private String size(long size, String singleValue, String[] units, long[] dividers) diff --git a/engines/base/src/main/java/org/alfresco/transform/base/logging/StandardMessages.java b/engines/base/src/main/java/org/alfresco/transform/base/logging/StandardMessages.java index b0f3cdc7..a7fd7f66 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/logging/StandardMessages.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/logging/StandardMessages.java @@ -29,19 +29,16 @@ package org.alfresco.transform.base.logging; public final class StandardMessages { private StandardMessages() - { - } + {} - public static String COMMUNITY_LICENCE = - "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"; + public static String COMMUNITY_LICENCE = "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"; - public static String ENTERPRISE_LICENCE = - "This image is only intended to be used with the Alfresco Enterprise Content Repository which is covered by\n" + - "https://www.alfresco.com/legal/agreements and https://www.alfresco.com/terms-use\n" + - "\n" + - "License rights for this program may be obtained from Alfresco Software, Ltd. pursuant to a written agreement\n" + - "and any use of this program without such an agreement is prohibited.\n" + - "\n"; + public static String ENTERPRISE_LICENCE = "This image is only intended to be used with the Alfresco Enterprise Content Repository which is covered by\n" + + "https://www.alfresco.com/legal/agreements and https://www.alfresco.com/terms-use\n" + + "\n" + + "License rights for this program may be obtained from Alfresco Software, Ltd. pursuant to a written agreement\n" + + "and any use of this program without such an agreement is prohibited.\n" + + "\n"; } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/messaging/MessagingConfig.java b/engines/base/src/main/java/org/alfresco/transform/base/messaging/MessagingConfig.java index d2bd5420..b8f4c4d4 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/messaging/MessagingConfig.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/messaging/MessagingConfig.java @@ -26,7 +26,9 @@ */ package org.alfresco.transform.base.messaging; -import org.alfresco.transform.messages.TransformRequestValidator; +import jakarta.jms.ConnectionFactory; +import jakarta.jms.Queue; + import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -40,15 +42,12 @@ import org.springframework.lang.NonNull; import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory; import org.springframework.transaction.PlatformTransactionManager; -import jakarta.jms.ConnectionFactory; -import jakarta.jms.Queue; +import org.alfresco.transform.messages.TransformRequestValidator; /** - * JMS and messaging configuration for the T-Engines. Contains the basic config in order to have the - * T-Engine able to read from queues and send a reply back. + * JMS and messaging configuration for the T-Engines. Contains the basic config in order to have the T-Engine able to read from queues and send a reply back. * - * @author Lucian Tuca - * created on 18/12/2018 + * @author Lucian Tuca created on 18/12/2018 */ @Configuration @ConditionalOnProperty(name = "activemq.url") @@ -70,9 +69,9 @@ public class MessagingConfig implements JmsListenerConfigurer @Bean public DefaultJmsListenerContainerFactory jmsListenerContainerFactory( - final ConnectionFactory connectionFactory, - final TransformMessageConverter transformMessageConverter, - final MessagingErrorHandler messagingErrorHandler) + final ConnectionFactory connectionFactory, + final TransformMessageConverter transformMessageConverter, + final MessagingErrorHandler messagingErrorHandler) { final DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); @@ -92,10 +91,8 @@ public class MessagingConfig implements JmsListenerConfigurer @Bean public Queue engineRequestQueue( - @Value("${queue.engineRequestQueue}") String engineRequestQueueValue) + @Value("${queue.engineRequestQueue}") String engineRequestQueueValue) { return new ActiveMQQueue(engineRequestQueueValue); } } - - diff --git a/engines/base/src/main/java/org/alfresco/transform/base/messaging/MessagingInfo.java b/engines/base/src/main/java/org/alfresco/transform/base/messaging/MessagingInfo.java index 77aebdd0..8695234b 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/messaging/MessagingInfo.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/messaging/MessagingInfo.java @@ -55,7 +55,7 @@ public class MessagingInfo // Note: as per application.yaml the broker url is appended with "?jms.watchTopicAdvisories=false". If this needs to be fully // overridden then it would require explicitly setting both "spring.activemq.broker-url" *and* "activemq.url" (latter to non-false value). - if ((activemqUrl != null) && (! activemqUrl.equals("false"))) + if ((activemqUrl != null) && (!activemqUrl.equals("false"))) { logger.info("JMS client is ENABLED - ACTIVEMQ_URL ='{}'", activemqUrl); } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/messaging/QueueTransformService.java b/engines/base/src/main/java/org/alfresco/transform/base/messaging/QueueTransformService.java index 5e90a582..d49de7af 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/messaging/QueueTransformService.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/messaging/QueueTransformService.java @@ -26,10 +26,14 @@ */ package org.alfresco.transform.base.messaging; -import org.alfresco.transform.base.TransformController; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.exceptions.TransformException; +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +import java.util.Optional; +import jakarta.jms.Destination; +import jakarta.jms.JMSException; +import jakarta.jms.Message; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -39,22 +43,15 @@ import org.springframework.jms.annotation.JmsListener; import org.springframework.jms.support.converter.MessageConversionException; import org.springframework.stereotype.Component; -import jakarta.jms.Destination; -import jakarta.jms.JMSException; -import jakarta.jms.Message; -import java.util.Optional; - -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import org.alfresco.transform.base.TransformController; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.exceptions.TransformException; /** - * Queue Transformer service. - * This service reads all the requests for the particular engine, forwards them to the worker - * component (at this time the injected controller - to be refactored) and sends back the reply - * to the {@link Message#getJMSReplyTo()} value. If this value is missing we've got to a dead end. + * Queue Transformer service. This service reads all the requests for the particular engine, forwards them to the worker component (at this time the injected controller - to be refactored) and sends back the reply to the {@link Message#getJMSReplyTo()} value. If this value is missing we've got to a dead end. * - * @author Lucian Tuca - * created on 18/12/2018 + * @author Lucian Tuca created on 18/12/2018 */ @Component @ConditionalOnProperty(name = "activemq.url") @@ -87,16 +84,16 @@ public class QueueTransformService if (replyToQueue == null) { logger.error( - "Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ", - correlationId); + "Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ", + correlationId); return; } } catch (JMSException e) { logger.error( - "Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ", - correlationId); + "Cannot find 'replyTo' destination queue for message with correlationID {}. Stopping. ", + correlationId); return; } @@ -111,7 +108,7 @@ public class QueueTransformService { logger.error(e.getMessage(), e); replyWithError(replyToQueue, HttpStatus.valueOf(e.getStatus().value()), - e.getMessage(), correlationId); + e.getMessage(), correlationId); return; } @@ -119,7 +116,7 @@ public class QueueTransformService { logger.error("T-Request from message with correlationID {} is null!", correlationId); replyWithInternalSvErr(replyToQueue, - "JMS exception during T-Request deserialization: ", correlationId); + "JMS exception during T-Request deserialization: ", correlationId); return; } @@ -127,10 +124,10 @@ public class QueueTransformService } /** - * Tries to convert the JMS {@link Message} to a {@link TransformRequest} - * If any error occurs, a {@link TransformException} is thrown + * Tries to convert the JMS {@link Message} to a {@link TransformRequest} If any error occurs, a {@link TransformException} is thrown * - * @param msg Message to be deserialized + * @param msg + * Message to be deserialized * @return The converted {@link TransformRequest} instance */ private Optional convert(final Message msg, String correlationId) @@ -142,42 +139,39 @@ public class QueueTransformService } catch (MessageConversionException e) { - String message = - "MessageConversionException during T-Request deserialization of message with correlationID " - + correlationId + ": "; + String message = "MessageConversionException during T-Request deserialization of message with correlationID " + + correlationId + ": "; throw new TransformException(BAD_REQUEST, message + e.getMessage()); } catch (JMSException e) { - String message = - "JMSException during T-Request deserialization of message with correlationID " - + correlationId + ": "; + String message = "JMSException during T-Request deserialization of message with correlationID " + + correlationId + ": "; throw new TransformException(INTERNAL_SERVER_ERROR, message + e.getMessage()); } catch (Exception e) { - String message = - "Exception during T-Request deserialization of message with correlationID " - + correlationId + ": "; + String message = "Exception during T-Request deserialization of message with correlationID " + + correlationId + ": "; throw new TransformException(INTERNAL_SERVER_ERROR, message + e.getMessage()); } } private void replyWithInternalSvErr(final Destination destination, final String msg, - final String correlationId) + final String correlationId) { replyWithError(destination, INTERNAL_SERVER_ERROR, msg, correlationId); } private void replyWithError(final Destination replyToQueue, final HttpStatus status, - final String msg, - final String correlationId) + final String msg, + final String correlationId) { final TransformReply reply = TransformReply - .builder() - .withStatus(status.value()) - .withErrorDetails(msg) - .build(); + .builder() + .withStatus(status.value()) + .withErrorDetails(msg) + .build(); transformReplySender.send(replyToQueue, reply, correlationId); } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/messaging/TransformMessageConverter.java b/engines/base/src/main/java/org/alfresco/transform/base/messaging/TransformMessageConverter.java index 55df26d0..f98cc78f 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/messaging/TransformMessageConverter.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/messaging/TransformMessageConverter.java @@ -27,11 +27,13 @@ package org.alfresco.transform.base.messaging; +import jakarta.jms.JMSException; +import jakarta.jms.Message; +import jakarta.jms.Session; + import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.type.TypeFactory; import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; import org.springframework.jms.support.converter.MappingJackson2MessageConverter; import org.springframework.jms.support.converter.MessageConversionException; import org.springframework.jms.support.converter.MessageConverter; @@ -39,13 +41,11 @@ import org.springframework.jms.support.converter.MessageType; import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; -import jakarta.jms.JMSException; -import jakarta.jms.Message; -import jakarta.jms.Session; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; /** - * Copied from the t-router. We would need to create a common dependency between t-engine base and t-router that - * knows about jms to remove this duplication. + * Copied from the t-router. We would need to create a common dependency between t-engine base and t-router that knows about jms to remove this duplication. * * @author Cezar Leahu */ @@ -53,16 +53,13 @@ import jakarta.jms.Session; public class TransformMessageConverter implements MessageConverter { private static final MappingJackson2MessageConverter converter; - private static final JavaType TRANSFORM_REQUEST_TYPE = - TypeFactory.defaultInstance().constructType(TransformRequest.class); + private static final JavaType TRANSFORM_REQUEST_TYPE = TypeFactory.defaultInstance().constructType(TransformRequest.class); static { - converter = new MappingJackson2MessageConverter() - { + converter = new MappingJackson2MessageConverter() { @Override - @NonNull - protected JavaType getJavaTypeForMessage(final Message message) throws JMSException + @NonNull protected JavaType getJavaTypeForMessage(final Message message) throws JMSException { if (message.getStringProperty("_type") == null) { @@ -74,23 +71,20 @@ public class TransformMessageConverter implements MessageConverter converter.setTargetType(MessageType.BYTES); converter.setTypeIdPropertyName("_type"); converter.setTypeIdMappings(ImmutableMap.of( - TransformRequest.class.getName(), TransformRequest.class, - TransformReply.class.getName(), TransformReply.class) - ); + TransformRequest.class.getName(), TransformRequest.class, + TransformReply.class.getName(), TransformReply.class)); } @Override - @NonNull - public Message toMessage( - @NonNull final Object object, - @NonNull final Session session) throws JMSException, MessageConversionException + @NonNull public Message toMessage( + @NonNull final Object object, + @NonNull final Session session) throws JMSException, MessageConversionException { return converter.toMessage(object, session); } @Override - @NonNull - public Object fromMessage(@NonNull final Message message) throws JMSException + @NonNull public Object fromMessage(@NonNull final Message message) throws JMSException { return converter.fromMessage(message); } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/messaging/TransformReplySender.java b/engines/base/src/main/java/org/alfresco/transform/base/messaging/TransformReplySender.java index 30af2e30..3f7e567b 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/messaging/TransformReplySender.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/messaging/TransformReplySender.java @@ -28,13 +28,14 @@ package org.alfresco.transform.base.messaging; import jakarta.jms.Destination; -import org.alfresco.transform.client.model.TransformReply; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Component; +import org.alfresco.transform.client.model.TransformReply; + /** * Copied from the t-router. * diff --git a/engines/base/src/main/java/org/alfresco/transform/base/metadata/AbstractMetadataExtractorEmbedder.java b/engines/base/src/main/java/org/alfresco/transform/base/metadata/AbstractMetadataExtractorEmbedder.java index faa6b12d..6da99b15 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/metadata/AbstractMetadataExtractorEmbedder.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/metadata/AbstractMetadataExtractorEmbedder.java @@ -26,12 +26,7 @@ */ package org.alfresco.transform.base.metadata; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.alfresco.transform.base.CustomTransformer; -import org.alfresco.transform.base.TransformManager; -import org.slf4j.Logger; +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EMBEDDER; import java.io.IOException; import java.io.InputStream; @@ -50,45 +45,38 @@ import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EMBEDDER; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; + +import org.alfresco.transform.base.CustomTransformer; +import org.alfresco.transform.base.TransformManager; /** * Helper methods for metadata extract and embed. *

- * Much of the code is based on AbstractMappingMetadataExtracter from the - * content repository. The code has been simplified to only set up mapping one way. + * Much of the code is based on AbstractMappingMetadataExtracter from the content repository. The code has been simplified to only set up mapping one way. *

- * If a transform specifies that it can convert from {@code ""} to {@code "alfresco-metadata-extract"} - * (specified in the {@code engine_config.json}), it is indicating that it can extract metadata from {@code }. + * If a transform specifies that it can convert from {@code ""} to {@code "alfresco-metadata-extract"} (specified in the {@code engine_config.json}), it is indicating that it can extract metadata from {@code }. * * The transform results in a Map of extracted properties encoded as json being returned to the content repository. *

    - *
  • The method extracts ALL available metadata from the document with - * {@link #extractMetadata(String, InputStream, String, OutputStream, Map, TransformManager)} and then calls - * {@link #mapMetadataAndWrite(OutputStream, Map, Map)}.
  • - *
  • Selected values from the available metadata are mapped into content repository property names and values, - * depending on what is defined in a {@code "_metadata_extract.properties"} file.
  • - *
  • The selected values are set back to the content repository as a JSON representation of a Map, where the values - * are applied to the source node.
  • + *
  • The method extracts ALL available metadata from the document with {@link #extractMetadata(String, InputStream, String, OutputStream, Map, TransformManager)} and then calls {@link #mapMetadataAndWrite(OutputStream, Map, Map)}.
  • + *
  • Selected values from the available metadata are mapped into content repository property names and values, depending on what is defined in a {@code "_metadata_extract.properties"} file.
  • + *
  • The selected values are set back to the content repository as a JSON representation of a Map, where the values are applied to the source node.
  • *
- * To support the same functionality as metadata extractors configured inside the content repository, - * extra key value pairs may be returned from {@link #extractMetadata(String, InputStream, String, OutputStream, Map, TransformManager)}. - * These are: + * To support the same functionality as metadata extractors configured inside the content repository, extra key value pairs may be returned from {@link #extractMetadata(String, InputStream, String, OutputStream, Map, TransformManager)}. These are: *
    - *
  • {@code "sys:overwritePolicy"} which can specify the - * {@code org.alfresco.repo.content.metadata.MetadataExtracter.OverwritePolicy} name. Defaults to "PRAGMATIC".
  • - *
  • {@code "sys:enableStringTagging"} if {@code "true"} finds or creates tags for each string mapped to - * {@code cm:taggable}. Defaults to {@code "false"} to ignore mapping strings to tags.
  • - *
  • {@code "sys:carryAspectProperties"}
  • - *
  • {@code "sys:stringTaggingSeparators"}
  • + *
  • {@code "sys:overwritePolicy"} which can specify the {@code org.alfresco.repo.content.metadata.MetadataExtracter.OverwritePolicy} name. Defaults to "PRAGMATIC".
  • + *
  • {@code "sys:enableStringTagging"} if {@code "true"} finds or creates tags for each string mapped to {@code cm:taggable}. Defaults to {@code "false"} to ignore mapping strings to tags.
  • + *
  • {@code "sys:carryAspectProperties"}
  • + *
  • {@code "sys:stringTaggingSeparators"}
  • *
* - * If a transform specifies that it can convert from {@code ""} to {@code "alfresco-metadata-embed"}, it is - * indicating that it can embed metadata in {@code }. + * If a transform specifies that it can convert from {@code ""} to {@code "alfresco-metadata-embed"}, it is indicating that it can embed metadata in {@code }. * - * The transform calls {@link #embedMetadata(String, InputStream, String, OutputStream, Map, TransformManager)} - * which should results in a new version of supplied source file that contains the metadata supplied in the transform - * options. + * The transform calls {@link #embedMetadata(String, InputStream, String, OutputStream, Map, TransformManager)} which should results in a new version of supplied source file that contains the metadata supplied in the transform options. * * @author Jesper Steen Møller * @author Derek Hulley @@ -213,7 +201,8 @@ public abstract class AbstractMetadataExtractorEmbedder implements CustomTransfo logger.debug( "Converted system model values to metadata values: \n" + " System Properties: {}\n" + - " Metadata Properties: {}", systemMetadata, metadataProperties); + " Metadata Properties: {}", + systemMetadata, metadataProperties); } return metadataProperties; } @@ -226,9 +215,8 @@ public abstract class AbstractMetadataExtractorEmbedder implements CustomTransfo /** * Based on AbstractMappingMetadataExtracter#getDefaultMapping. * - * This method provides a mapping of where to store the values extracted from the documents. The list of - * properties need not include all metadata values extracted from the document. This mapping should be - * defined in a file based on the class name: {@code "_metadata_extract.properties"} + * This method provides a mapping of where to store the values extracted from the documents. The list of properties need not include all metadata values extracted from the document. This mapping should be defined in a file based on the class name: {@code "_metadata_extract.properties"} + * * @return Returns a static mapping. It may not be null. */ private Map> buildExtractMapping() @@ -278,13 +266,10 @@ public abstract class AbstractMetadataExtractorEmbedder implements CustomTransfo /** * Based on AbstractMappingMetadataExtracter#getDefaultEmbedMapping. * - * This method provides a mapping of model properties that should be embedded in the content. The list of - * properties need not include all properties. This mapping should be defined in a file based on the class - * name: {@code "_metadata_embed.properties"} + * This method provides a mapping of model properties that should be embedded in the content. The list of properties need not include all properties. This mapping should be defined in a file based on the class name: {@code "_metadata_embed.properties"} *

- * If no {@code "_metadata_embed.properties"} file is found, a reverse of the - * {@code "_metadata_extract.properties"} will be assumed. A last win approach will be used for handling - * duplicates. + * If no {@code "_metadata_embed.properties"} file is found, a reverse of the {@code "_metadata_extract.properties"} will be assumed. A last win approach will be used for handling duplicates. + * * @return Returns a static mapping. It may not be null. */ private Map> buildEmbedMapping() @@ -388,8 +373,7 @@ public abstract class AbstractMetadataExtractorEmbedder implements CustomTransfo } } catch (IOException ignore) - { - } + {} return properties; } @@ -431,21 +415,21 @@ public abstract class AbstractMetadataExtractorEmbedder implements CustomTransfo } /** - * Adds a value to the map, conserving null values. Values are converted to null if: + * Adds a value to the map, conserving null values. Values are converted to null if: *

    - *
  • it is an empty string value after trimming
  • - *
  • it is an empty collection
  • - *
  • it is an empty array
  • + *
  • it is an empty string value after trimming
  • + *
  • it is an empty collection
  • + *
  • it is an empty array
  • *
- * String values are trimmed before being put into the map. - * Otherwise, it is up to the extracter to ensure that the value is a Serializable. - * It is not appropriate to implicitly convert values in order to make them Serializable - * - the best conversion method will depend on the value's specific meaning. + * String values are trimmed before being put into the map. Otherwise, it is up to the extracter to ensure that the value is a Serializable. It is not appropriate to implicitly convert values in order to make them Serializable - the best conversion method will depend on the value's specific meaning. * - * @param key the destination key - * @param value the serializable value - * @param destination the map to put values into - * @return Returns true if set, otherwise false + * @param key + * the destination key + * @param value + * the serializable value + * @param destination + * the map to put values into + * @return Returns true if set, otherwise false */ // Copied from the content repository's AbstractMappingMetadataExtracter. protected boolean putRawValue(String key, Serializable value, Map destination) @@ -514,8 +498,7 @@ public abstract class AbstractMetadataExtractorEmbedder implements CustomTransfo String targetMimetype, OutputStream outputStream, Map transformOptions, TransformManager transformManager) throws Exception; - private Map> getExtractMappingFromOptions(Map transformOptions, Map> defaultExtractMapping) + private Map> getExtractMappingFromOptions(Map transformOptions, Map> defaultExtractMapping) { String extractMappingOption = transformOptions.get(EXTRACT_MAPPING); if (extractMappingOption != null) @@ -527,19 +510,19 @@ public abstract class AbstractMetadataExtractorEmbedder implements CustomTransfo } catch (JsonProcessingException e) { - throw new IllegalArgumentException("Failed to read "+ EXTRACT_MAPPING +" from request", e); + throw new IllegalArgumentException("Failed to read " + EXTRACT_MAPPING + " from request", e); } } return defaultExtractMapping; } public void mapMetadataAndWrite(OutputStream outputStream, Map metadata, - Map> extractMapping) throws IOException + Map> extractMapping) throws IOException { if (logger.isDebugEnabled()) { logger.debug("Raw metadata:"); - metadata.forEach((k,v) -> logger.debug(" {}={}", k, v)); + metadata.forEach((k, v) -> logger.debug(" {}={}", k, v)); } metadata = mapRawToSystem(metadata, extractMapping); @@ -549,12 +532,14 @@ public abstract class AbstractMetadataExtractorEmbedder implements CustomTransfo /** * Based on AbstractMappingMetadataExtracter#mapRawToSystem. * - * @param rawMetadata Metadata keyed by document properties - * @param extractMapping Mapping between document ans system properties - * @return Returns the metadata keyed by the system properties + * @param rawMetadata + * Metadata keyed by document properties + * @param extractMapping + * Mapping between document ans system properties + * @return Returns the metadata keyed by the system properties */ private Map mapRawToSystem(Map rawMetadata, - Map> extractMapping) + Map> extractMapping) { boolean debugEnabled = logger.isDebugEnabled(); if (debugEnabled) diff --git a/engines/base/src/main/java/org/alfresco/transform/base/model/FileRefEntity.java b/engines/base/src/main/java/org/alfresco/transform/base/model/FileRefEntity.java index 22aa2bc9..8947bd47 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/model/FileRefEntity.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/model/FileRefEntity.java @@ -37,7 +37,8 @@ public class FileRefEntity { private String fileRef; - public FileRefEntity() {} + public FileRefEntity() + {} public FileRefEntity(String fileRef) { @@ -57,8 +58,10 @@ public class FileRefEntity @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; FileRefEntity that = (FileRefEntity) o; return Objects.equals(fileRef, that.fileRef); } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/model/FileRefResponse.java b/engines/base/src/main/java/org/alfresco/transform/base/model/FileRefResponse.java index 5d80d441..ec860bb7 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/model/FileRefResponse.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/model/FileRefResponse.java @@ -35,7 +35,8 @@ public class FileRefResponse { private FileRefEntity entry; - public FileRefResponse() {} + public FileRefResponse() + {} public FileRefResponse(FileRefEntity entry) { diff --git a/engines/base/src/main/java/org/alfresco/transform/base/probes/ProbeTransform.java b/engines/base/src/main/java/org/alfresco/transform/base/probes/ProbeTransform.java index 28ca153a..ecd8f605 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/probes/ProbeTransform.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/probes/ProbeTransform.java @@ -26,11 +26,11 @@ */ package org.alfresco.transform.base.probes; -import org.alfresco.transform.base.transform.TransformHandler; -import org.alfresco.transform.base.logging.LogEntry; -import org.alfresco.transform.exceptions.TransformException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; + +import static org.alfresco.transform.base.fs.FileManager.TempFileProvider.createTempFile; import java.io.File; import java.io.IOException; @@ -42,35 +42,33 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; -import static org.alfresco.transform.base.fs.FileManager.TempFileProvider.createTempFile; -import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; -import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.alfresco.transform.base.logging.LogEntry; +import org.alfresco.transform.base.transform.TransformHandler; +import org.alfresco.transform.exceptions.TransformException; /** * Provides test transformations and the logic used by k8 liveness and readiness probes. * - *

K8s probes: A readiness probe indicates if the pod should accept request. It does not indicate that a - * pod is ready after startup. The liveness probe indicates when to kill the pod. Both probes are called - * throughout the lifetime of the pod and a liveness probes can take place before a readiness probe. The k8s - * initialDelaySeconds field is not fully honoured as it is multiplied by a random number, so is - * actually a maximum initial delay in seconds, but could be 0.

+ *

+ * K8s probes: A readiness probe indicates if the pod should accept request. It does not indicate that a pod is ready after startup. The liveness probe indicates when to kill the pod. Both probes are called throughout the lifetime of the pod and a liveness probes can take place before a readiness probe. The k8s initialDelaySeconds field is not fully honoured as it is multiplied by a random number, so is actually a maximum initial delay in seconds, but could be 0. + *

* - *

Live and readiness probes do test transforms. The first 6 requests result in a transformation of a small test - * file. The average time and size is remembered, but excludes the first one as it is normally slower. This is - * used in future requests to discover if transformations are becoming slower or unexpectedly change size.

+ *

+ * Live and readiness probes do test transforms. The first 6 requests result in a transformation of a small test file. The average time and size is remembered, but excludes the first one as it is normally slower. This is used in future requests to discover if transformations are becoming slower or unexpectedly change size. + *

* - *

If a transform longer than a maximum time, a maximum number of transforms have been performed, a test transform is - * an unexpected size or a test transform takes an unexpected time, then a non 200 status code is returned resulting in - * k8s terminating the pod. These are controlled by:

+ *

+ * If a transform longer than a maximum time, a maximum number of transforms have been performed, a test transform is an unexpected size or a test transform takes an unexpected time, then a non 200 status code is returned resulting in k8s terminating the pod. These are controlled by: + *

*
    - *
  • expectedLength the expected length of the target file after a test transform
  • - *
  • plusOrMinus allows for variation in the transformed size - generally caused by dates
  • - *
  • livenessPercent allows for variation in transform time. Up to 2 and a half times is not - * unreasonable under load
  • - *
  • maxTransforms the maximum number of transforms (not just test ones) before a restart is triggered
  • - *
  • maxTransformSeconds a maximum time any transform (not just test ones) is allowed to take before - * a restart is triggered.
  • + *
  • expectedLength the expected length of the target file after a test transform
  • + *
  • plusOrMinus allows for variation in the transformed size - generally caused by dates
  • + *
  • livenessPercent allows for variation in transform time. Up to 2 and a half times is not unreasonable under load
  • + *
  • maxTransforms the maximum number of transforms (not just test ones) before a restart is triggered
  • + *
  • maxTransformSeconds a maximum time any transform (not just test ones) is allowed to take before a restart is triggered.
  • *
  • livenessTransformPeriodSeconds The number of seconds between test transforms done for live probes
  • *
*/ @@ -114,8 +112,8 @@ public class ProbeTransform } public ProbeTransform(String sourceFilename, String sourceMimetype, String targetMimetype, Map transformOptions, - long expectedLength, long plusOrMinus, int livenessPercent, long maxTransforms, long maxTransformSeconds, - long livenessTransformPeriodSeconds) + long expectedLength, long plusOrMinus, int livenessPercent, long maxTransforms, long maxTransformSeconds, + long livenessTransformPeriodSeconds) { this.sourceFilename = sourceFilename; this.sourceMimetype = sourceMimetype; @@ -128,7 +126,7 @@ public class ProbeTransform maxTransformCount = getPositiveLongEnv("maxTransforms", maxTransforms); maxTransformTime = getPositiveLongEnv("maxTransformSeconds", maxTransformSeconds) * 1000; livenessTransformPeriod = getPositiveLongEnv("livenessTransformPeriodSeconds", - livenessTransformPeriodSeconds) * 1000; + livenessTransformPeriodSeconds) * 1000; livenessTransformEnabled = getBooleanEnvVar("livenessTransformEnabled", false); } @@ -139,8 +137,7 @@ public class ProbeTransform return Boolean.parseBoolean(System.getenv(name)); } catch (Exception ignore) - { - } + {} return defaultValue; } @@ -155,8 +152,7 @@ public class ProbeTransform l = Long.parseLong(env); } catch (NumberFormatException ignore) - { - } + {} } if (l <= 0) { @@ -182,9 +178,9 @@ public class ProbeTransform String result = (isLiveProbe && livenessTransformPeriod > 0 && (transCount <= AVERAGE_OVER_TRANSFORMS || nextTransformTime < System.currentTimeMillis())) - || !initialised.get() - ? doTransform(isLiveProbe, transformHandler) - : doNothing(isLiveProbe); + || !initialised.get() + ? doTransform(isLiveProbe, transformHandler) + : doNothing(isLiveProbe); // Check if the test transformation was too slow. checkMaxTransformTimeAndCount(isLiveProbe); @@ -211,8 +207,7 @@ public class ProbeTransform do { nextTransformTime += livenessTransformPeriod; - } - while (nextTransformTime < start); + } while (nextTransformTime < start); } File sourceFile = getSourceFile(isLiveProbe); @@ -228,15 +223,15 @@ public class ProbeTransform if (time > maxTime) { throw new TransformException(INTERNAL_SERVER_ERROR, - getMessagePrefix(isLiveProbe) + - message + " which is more than " + livenessPercent + - "% slower than the normal value of " + normalTime + "ms"); + getMessagePrefix(isLiveProbe) + + message + " which is more than " + livenessPercent + + "% slower than the normal value of " + normalTime + "ms"); } // We don't care if the ready or live probe works out if we are 'ready' to take requests. initialised.set(true); - return getProbeMessage(isLiveProbe) + "Success - "+message; + return getProbeMessage(isLiveProbe) + "Success - " + message; } private void checkMaxTransformTimeAndCount(boolean isLiveProbe) @@ -244,15 +239,15 @@ public class ProbeTransform if (die.get()) { throw new TransformException(TOO_MANY_REQUESTS, - getMessagePrefix(isLiveProbe) + "Transformer requested to die. A transform took " + - "longer than " + (maxTransformTime / 1000) + " seconds"); + getMessagePrefix(isLiveProbe) + "Transformer requested to die. A transform took " + + "longer than " + (maxTransformTime / 1000) + " seconds"); } if (maxTransformCount > 0 && transformCount.get() > maxTransformCount) { throw new TransformException(TOO_MANY_REQUESTS, - getMessagePrefix(isLiveProbe) + "Transformer requested to die. It has performed " + - "more than " + maxTransformCount + " transformations"); + getMessagePrefix(isLiveProbe) + "Transformer requested to die. It has performed " + + "more than " + maxTransformCount + " transformations"); } } @@ -266,7 +261,7 @@ public class ProbeTransform catch (IOException e) { throw new TransformException(INSUFFICIENT_STORAGE, - getMessagePrefix(isLiveProbe) + "Failed to store the source file", e); + getMessagePrefix(isLiveProbe) + "Failed to store the source file", e); } long length = sourceFile.length(); LogEntry.setSource(sourceFile.getName(), length); @@ -301,7 +296,7 @@ public class ProbeTransform maxTime = (normalTime * (livenessPercent + 100)) / 100; if ((!isLiveProbe && !readySent.getAndSet( - true)) || transCount > AVERAGE_OVER_TRANSFORMS) + true)) || transCount > AVERAGE_OVER_TRANSFORMS) { nextTransformTime = System.currentTimeMillis() + livenessTransformPeriod; logger.trace("{} - {}ms+{}%={}ms", message, normalTime, livenessPercent, maxTime); @@ -320,16 +315,16 @@ public class ProbeTransform if (!targetFile.exists() || !targetFile.isFile()) { throw new TransformException(INTERNAL_SERVER_ERROR, - probeMessage + "Target File \"" + targetFile.getAbsolutePath() + "\" did not exist"); + probeMessage + "Target File \"" + targetFile.getAbsolutePath() + "\" did not exist"); } long length = targetFile.length(); targetFile.delete(); if (length < minExpectedLength || length > maxExpectedLength) { throw new TransformException(INTERNAL_SERVER_ERROR, - probeMessage + "Target File \"" + targetFile.getAbsolutePath() + - "\" was the wrong size (" + length + "). Needed to be between " + - minExpectedLength + " and " + maxExpectedLength); + probeMessage + "Target File \"" + targetFile.getAbsolutePath() + + "\" was the wrong size (" + length + "). Needed to be between " + + minExpectedLength + " and " + maxExpectedLength); } } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/registry/AbstractTransformConfigSource.java b/engines/base/src/main/java/org/alfresco/transform/base/registry/AbstractTransformConfigSource.java index c7eb36cc..c3bee891 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/registry/AbstractTransformConfigSource.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/registry/AbstractTransformConfigSource.java @@ -56,4 +56,4 @@ public abstract class AbstractTransformConfigSource implements TransformConfigSo { return baseUrl; } -} \ No newline at end of file +} diff --git a/engines/base/src/main/java/org/alfresco/transform/base/registry/CustomTransformers.java b/engines/base/src/main/java/org/alfresco/transform/base/registry/CustomTransformers.java index d7473018..d0c972d2 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/registry/CustomTransformers.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/registry/CustomTransformers.java @@ -26,18 +26,19 @@ */ package org.alfresco.transform.base.registry; -import org.alfresco.transform.base.CustomTransformer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import jakarta.annotation.PostConstruct; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; +import jakarta.annotation.PostConstruct; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.CustomTransformer; @Component public class CustomTransformers @@ -54,22 +55,21 @@ public class CustomTransformers { if (customTransformerList != null) { - customTransformerList.forEach(customTransformer -> - customTransformersByName.put(customTransformer.getTransformerName(), customTransformer)); + customTransformerList.forEach(customTransformer -> customTransformersByName.put(customTransformer.getTransformerName(), customTransformer)); List nonNullTransformerNames = customTransformerList.stream() - .map(CustomTransformer::getTransformerName) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + .map(CustomTransformer::getTransformerName) + .filter(Objects::nonNull) + .collect(Collectors.toList()); if (!nonNullTransformerNames.isEmpty()) { logger.info("Custom Transformers:"); nonNullTransformerNames - .stream() - .sorted() - .map(name -> " "+name) - .forEach(logger::debug); + .stream() + .sorted() + .map(name -> " " + name) + .forEach(logger::debug); } } } @@ -80,7 +80,7 @@ public class CustomTransformers return customTransformer == null ? customTransformersByName.get(null) : customTransformer; } - public void put(String name, CustomTransformer customTransformer) + public void put(String name, CustomTransformer customTransformer) { customTransformersByName.put(name, customTransformer); } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFiles.java b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFiles.java index eb8d3b68..1a678cdf 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFiles.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFiles.java @@ -26,14 +26,14 @@ */ package org.alfresco.transform.base.registry; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.Resource; - import java.util.HashMap; import java.util.List; import java.util.Map; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; + @Configuration @ConfigurationProperties(prefix = "transform.config") public class TransformConfigFiles diff --git a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFilesHistoric.java b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFilesHistoric.java index 090dc784..f4ff1679 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFilesHistoric.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFilesHistoric.java @@ -26,18 +26,18 @@ */ package org.alfresco.transform.base.registry; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; +import static org.alfresco.transform.base.registry.TransformConfigFromFiles.retrieveResource; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import static org.alfresco.transform.base.registry.TransformConfigFromFiles.retrieveResource; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; /** * Similar to {@link TransformConfigFiles} but uses the names historically used by the t-router. @@ -50,8 +50,9 @@ public class TransformConfigFilesHistoric // environment variables like TRANSFORMER_ROUTES_ADDITIONAL_. private final Map additional = new HashMap<>(); - //Used by ConfigurationProperties annotation - public Map getAdditional() { + // Used by ConfigurationProperties annotation + public Map getAdditional() + { return additional; } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFromFiles.java b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFromFiles.java index 7e5357c6..24957e0c 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFromFiles.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFromFiles.java @@ -26,8 +26,14 @@ */ package org.alfresco.transform.base.registry; -import org.alfresco.transform.config.reader.TransformConfigResourceReader; -import org.alfresco.transform.config.TransformConfig; +import static java.util.stream.Collectors.toList; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import jakarta.annotation.PostConstruct; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; @@ -35,13 +41,8 @@ import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; -import jakarta.annotation.PostConstruct; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -import static java.util.stream.Collectors.toList; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.reader.TransformConfigResourceReader; /** * Makes {@link TransformConfig} from files on the classpath or externally available to the {@link TransformRegistry}. @@ -66,30 +67,29 @@ public class TransformConfigFromFiles final List resources = new ArrayList<>(); resources.addAll(transformConfigFiles.retrieveResources()); resources.addAll(transformConfigFilesHistoric.retrieveResources()); - resources.forEach(resource -> - { + resources.forEach(resource -> { String filename = resource.getFilename(); transformConfigSources.add( - new AbstractTransformConfigSource(filename, filename, isTRouter ? null : "---") - { - @Override public TransformConfig getTransformConfig() - { - return transformConfigResourceReader.read(resource); - } - }); + new AbstractTransformConfigSource(filename, filename, isTRouter ? null : "---") { + @Override + public TransformConfig getTransformConfig() + { + return transformConfigResourceReader.read(resource); + } + }); }); } public static List retrieveResources(Map additional) { return additional - .values() - .stream() - .filter(Objects::nonNull) - .map(String::trim) - .filter(s -> !s.isBlank()) - .map(TransformConfigFromFiles::retrieveResource) - .collect(toList()); + .values() + .stream() + .filter(Objects::nonNull) + .map(String::trim) + .filter(s -> !s.isBlank()) + .map(TransformConfigFromFiles::retrieveResource) + .collect(toList()); } public static Resource retrieveResource(final String filename) diff --git a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFromTransformEngines.java b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFromTransformEngines.java index d0e36dfa..84f0aa8e 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFromTransformEngines.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformConfigFromTransformEngines.java @@ -26,14 +26,15 @@ */ package org.alfresco.transform.base.registry; -import org.alfresco.transform.base.TransformEngine; -import org.alfresco.transform.config.TransformConfig; +import java.util.List; +import jakarta.annotation.PostConstruct; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import jakarta.annotation.PostConstruct; -import java.util.List; +import org.alfresco.transform.base.TransformEngine; +import org.alfresco.transform.config.TransformConfig; /** * Makes {@link TransformConfig} from {@link TransformEngine}s available to the {@link TransformRegistry}. @@ -54,21 +55,21 @@ public class TransformConfigFromTransformEngines if (transformEngines != null) { transformEngines.stream() - .forEach(transformEngine -> { - TransformConfig transformConfig = transformEngine.getTransformConfig(); - if (transformConfig != null) // if not a wrapping TransformEngine like all-in-one - { - String engineName = transformEngine.getTransformEngineName(); - transformConfigSources.add( - new AbstractTransformConfigSource(engineName, engineName, isTRouter ? null : "---") - { - @Override public TransformConfig getTransformConfig() - { - return transformEngine.getTransformConfig(); - } - }); - } - }); + .forEach(transformEngine -> { + TransformConfig transformConfig = transformEngine.getTransformConfig(); + if (transformConfig != null) // if not a wrapping TransformEngine like all-in-one + { + String engineName = transformEngine.getTransformEngineName(); + transformConfigSources.add( + new AbstractTransformConfigSource(engineName, engineName, isTRouter ? null : "---") { + @Override + public TransformConfig getTransformConfig() + { + return transformEngine.getTransformConfig(); + } + }); + } + }); } } } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformRegistry.java b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformRegistry.java index 97560c00..06f58289 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformRegistry.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformRegistry.java @@ -25,17 +25,26 @@ */ package org.alfresco.transform.base.registry; -import org.alfresco.transform.base.CustomTransformer; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.config.TransformOption; -import org.alfresco.transform.config.TransformOptionGroup; -import org.alfresco.transform.config.TransformOptionValue; -import org.alfresco.transform.config.Transformer; -import org.alfresco.transform.registry.AbstractTransformRegistry; -import org.alfresco.transform.registry.CombinedTransformConfig; -import org.alfresco.transform.registry.Origin; -import org.alfresco.transform.registry.TransformCache; -import org.alfresco.transform.registry.TransformerType; +import static java.util.Collections.emptyMap; +import static java.util.Objects.isNull; +import static java.util.stream.Collectors.toUnmodifiableMap; +import static java.util.stream.Collectors.toUnmodifiableSet; + +import static org.springframework.util.CollectionUtils.isEmpty; + +import static org.alfresco.transform.config.CoreVersionDecorator.setCoreVersionOnSingleStepTransformers; +import static org.alfresco.transform.registry.TransformerType.FAILOVER_TRANSFORMER; +import static org.alfresco.transform.registry.TransformerType.PIPELINE_TRANSFORMER; + +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -49,26 +58,20 @@ import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import java.util.*; -import java.util.Map.Entry; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.function.Supplier; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static java.util.Collections.emptyMap; -import static java.util.Objects.isNull; -import static java.util.stream.Collectors.toUnmodifiableMap; -import static java.util.stream.Collectors.toUnmodifiableSet; -import static org.alfresco.transform.config.CoreVersionDecorator.setCoreVersionOnSingleStepTransformers; -import static org.alfresco.transform.registry.TransformerType.FAILOVER_TRANSFORMER; -import static org.alfresco.transform.registry.TransformerType.PIPELINE_TRANSFORMER; -import static org.springframework.util.CollectionUtils.isEmpty; +import org.alfresco.transform.base.CustomTransformer; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.TransformOptionGroup; +import org.alfresco.transform.config.TransformOptionValue; +import org.alfresco.transform.config.Transformer; +import org.alfresco.transform.registry.AbstractTransformRegistry; +import org.alfresco.transform.registry.CombinedTransformConfig; +import org.alfresco.transform.registry.Origin; +import org.alfresco.transform.registry.TransformCache; +import org.alfresco.transform.registry.TransformerType; @Service -@Scope( proxyMode = ScopedProxyMode.TARGET_CLASS ) +@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) public class TransformRegistry extends AbstractTransformRegistry { private static final Logger logger = LoggerFactory.getLogger(TransformRegistry.class); @@ -91,7 +94,7 @@ public class TransformRegistry extends AbstractTransformRegistry { private TransformConfig transformConfig; private TransformConfig uncombinedTransformConfig; - private Map> transformerByNameMap; + private Map> transformerByNameMap; public TransformConfig getTransformConfig() { @@ -143,8 +146,7 @@ public class TransformRegistry extends AbstractTransformRegistry } /** - * Load the registry on application startup. This allows Components in projects that extend the t-engine base - * to use @PostConstruct to add to {@code transformConfigSources}, before the registry is loaded. + * Load the registry on application startup. This allows Components in projects that extend the t-engine base to use @PostConstruct to add to {@code transformConfigSources}, before the registry is loaded. */ @Async void initRegistryOnAppStartup(final ContextRefreshedEvent event) @@ -170,7 +172,7 @@ public class TransformRegistry extends AbstractTransformRegistry fixedDelayString = "#{${transform.engine.config.retry.timeout} * 1000}") public void retrieveEngineConfigsAfterFailure() { - if(isRecoveryModeOn.get()) + if (isRecoveryModeOn.get()) { logger.trace("Recovery mode, attempting to retrieve configs for all registered T-Engines."); retrieveConfig(); @@ -191,8 +193,7 @@ public class TransformRegistry extends AbstractTransformRegistry TransformConfig transformConfig = source.getTransformConfig(); availableTransformers.put( sortOnName, - new LocalTransformConfigSource(transformConfig, sortOnName, source.getReadFrom(), source.getBaseUrl()) - ); + new LocalTransformConfigSource(transformConfig, sortOnName, source.getReadFrom(), source.getBaseUrl())); } catch (IllegalStateException e) { @@ -204,14 +205,13 @@ public class TransformRegistry extends AbstractTransformRegistry { logger.warn( "Failed to retrieved TransformConfig during refreshment. Stops refreshing TransformRegistry. {}", - e.getMessage() - ); + e.getMessage()); return; } } } - if(transformConfigSources.size() == availableTransformers.size() + if (transformConfigSources.size() == availableTransformers.size() && isRecoveryModeOn.compareAndExchange(true, false)) { logger.trace("All TransformConfigSources have been retrieved, turning off recovery mode."); @@ -219,11 +219,11 @@ public class TransformRegistry extends AbstractTransformRegistry logger.debug("Creating CombinedTransformConfig."); availableTransformers.values().forEach(source -> { - TransformConfig transformConfig = source.getTransformConfig(); - setCoreVersionOnSingleStepTransformers(transformConfig, coreVersion); - combinedTransformConfig.addTransformConfig(transformConfig, source.getReadFrom(), source.getBaseUrl(), + TransformConfig transformConfig = source.getTransformConfig(); + setCoreVersionOnSingleStepTransformers(transformConfig, coreVersion); + combinedTransformConfig.addTransformConfig(transformConfig, source.getReadFrom(), source.getBaseUrl(), this); - }); + }); TransformConfig uncombinedTransformConfig = combinedTransformConfig.buildTransformConfig(); combinedTransformConfig.combineTransformerConfig(this); @@ -239,48 +239,48 @@ public class TransformRegistry extends AbstractTransformRegistry if (logger.isInfoEnabled()) { Set customTransformerNames = new HashSet<>(customTransformerList == null - ? Collections.emptySet() - : customTransformerList.stream().map(CustomTransformer::getTransformerName).collect(Collectors.toSet())); - List nonNullTransformerNames = uncombinedTransformConfig.getTransformers().stream() - .map(Transformer::getTransformerName) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + ? Collections.emptySet() + : customTransformerList.stream().map(CustomTransformer::getTransformerName).collect(Collectors.toSet())); + List nonNullTransformerNames = uncombinedTransformConfig.getTransformers().stream() + .map(Transformer::getTransformerName) + .filter(Objects::nonNull) + .collect(Collectors.toList()); ArrayList logMessages = new ArrayList<>(); if (!nonNullTransformerNames.isEmpty()) { - logMessages.add("Transformers (" + nonNullTransformerNames.size() + ") Transforms (" + getData().getTransformCount()+ "):"); + logMessages.add("Transformers (" + nonNullTransformerNames.size() + ") Transforms (" + getData().getTransformCount() + "):"); nonNullTransformerNames - .stream() - .sorted(String.CASE_INSENSITIVE_ORDER) - .map(name -> { - Origin transformerOrigin = transformerByNameMap.get(name); - String message = " " + name + (transformerOrigin == null - ? " -- unavailable: see previous messages" - : isTRouter - ? "" - : TransformerType.valueOf(transformerOrigin.get()) == PIPELINE_TRANSFORMER - ? " -- unavailable: pipeline only available via t-router" - : TransformerType.valueOf(transformerOrigin.get()) == FAILOVER_TRANSFORMER - ? " -- unavailable: failover only available via t-router" - : !customTransformerNames.contains(name) - ? " -- missing: CustomTransformer" - : ""); - customTransformerNames.remove(name); - return message; - }) - .forEach(logMessages::add); + .stream() + .sorted(String.CASE_INSENSITIVE_ORDER) + .map(name -> { + Origin transformerOrigin = transformerByNameMap.get(name); + String message = " " + name + (transformerOrigin == null + ? " -- unavailable: see previous messages" + : isTRouter + ? "" + : TransformerType.valueOf(transformerOrigin.get()) == PIPELINE_TRANSFORMER + ? " -- unavailable: pipeline only available via t-router" + : TransformerType.valueOf(transformerOrigin.get()) == FAILOVER_TRANSFORMER + ? " -- unavailable: failover only available via t-router" + : !customTransformerNames.contains(name) + ? " -- missing: CustomTransformer" + : ""); + customTransformerNames.remove(name); + return message; + }) + .forEach(logMessages::add); List unusedCustomTransformNames = customTransformerNames.stream() - .filter(Objects::nonNull) - .sorted() - .collect(Collectors.toList()); + .filter(Objects::nonNull) + .sorted() + .collect(Collectors.toList()); if (!unusedCustomTransformNames.isEmpty()) { logMessages.add("Unused CustomTransformers (" + unusedCustomTransformNames.size() + ") - name is not in the transform config:"); unusedCustomTransformNames - .stream() - .map(name -> " " + name) - .forEach(logMessages::add); + .stream() + .map(name -> " " + name) + .forEach(logMessages::add); } int logMessageHashCode = logMessages.hashCode(); @@ -301,8 +301,8 @@ public class TransformRegistry extends AbstractTransformRegistry { Data data = getData(); return isTRouter - ? data.getTransformConfig() - : data.getUncombinedTransformConfig(); + ? data.getTransformConfig() + : data.getUncombinedTransformConfig(); } /** @@ -321,15 +321,15 @@ public class TransformRegistry extends AbstractTransformRegistry @Override public Data getData() { - return concurrentRead(() -> data ); + return concurrentRead(() -> data); } /** * Lock for reads while updating, use {@link #concurrentRead} to access locked fields */ private void concurrentUpdate(CombinedTransformConfig combinedTransformConfig, - TransformConfig uncombinedTransformConfig, TransformConfig transformConfig, - Map> transformerByNameMap) + TransformConfig uncombinedTransformConfig, TransformConfig transformConfig, + Map> transformerByNameMap) { configRefreshLock.writeLock().lock(); try @@ -372,10 +372,9 @@ public class TransformRegistry extends AbstractTransformRegistry } public Transformer getTransformer(final String sourceMediaType, final Long fileSizeBytes, - final String targetMediaType, final Map transformOptions) + final String targetMediaType, final Map transformOptions) { - return concurrentRead(() -> - { + return concurrentRead(() -> { long fileSize = fileSizeBytes == null ? 0 : fileSizeBytes; String transformerName = findTransformerName(sourceMediaType, fileSize, targetMediaType, transformOptions, null); return getTransformer(transformerName); @@ -395,15 +394,10 @@ public class TransformRegistry extends AbstractTransformRegistry public boolean checkSourceSize(String transformerName, String sourceMediaType, Long sourceSize, String targetMediaType) { - return Optional.ofNullable(getTransformer(transformerName)). - map(transformer -> transformer.getSupportedSourceAndTargetList().stream(). - filter(supported -> supported.getSourceMediaType().equals(sourceMediaType) && - supported.getTargetMediaType().equals(targetMediaType)). - findFirst(). - map(supported -> supported.getMaxSourceSizeBytes() == -1 || - supported.getMaxSourceSizeBytes() >= sourceSize). - orElse(false)). - orElse(false); + return Optional.ofNullable(getTransformer(transformerName)).map(transformer -> transformer.getSupportedSourceAndTargetList().stream().filter(supported -> supported.getSourceMediaType().equals(sourceMediaType) && + supported.getTargetMediaType().equals(targetMediaType)).findFirst().map(supported -> supported.getMaxSourceSizeBytes() == -1 || + supported.getMaxSourceSizeBytes() >= sourceSize) + .orElse(false)).orElse(false); } public String getEngineName(String transformerName) @@ -447,9 +441,9 @@ public class TransformRegistry extends AbstractTransformRegistry if (option instanceof TransformOptionGroup) { return ((TransformOptionGroup) option) - .getTransformOptions() - .stream() - .flatMap(TransformRegistry::retrieveOptionsStrings); + .getTransformOptions() + .stream() + .flatMap(TransformRegistry::retrieveOptionsStrings); } return Stream.of(((TransformOptionValue) option).getName()); } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/sfs/SharedFileStoreClient.java b/engines/base/src/main/java/org/alfresco/transform/base/sfs/SharedFileStoreClient.java index e1913efb..6a2d2364 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/sfs/SharedFileStoreClient.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/sfs/SharedFileStoreClient.java @@ -33,10 +33,9 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; import java.io.File; +import javax.net.ssl.SSLException; +import jakarta.annotation.PostConstruct; -import org.alfresco.transform.base.WebClientBuilderAdjuster; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transform.base.model.FileRefResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -54,8 +53,9 @@ import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient; -import jakarta.annotation.PostConstruct; -import javax.net.ssl.SSLException; +import org.alfresco.transform.base.WebClientBuilderAdjuster; +import org.alfresco.transform.base.model.FileRefResponse; +import org.alfresco.transform.exceptions.TransformException; /** * Simple Rest client that call Alfresco Shared File Store @@ -77,19 +77,21 @@ public class SharedFileStoreClient private WebClient client; @PostConstruct - public void init() throws SSLException { + public void init() throws SSLException + { final WebClient.Builder clientBuilder = WebClient.builder(); adjuster.adjust(clientBuilder); client = clientBuilder.baseUrl(url.endsWith("/") ? url : url + "/") - .defaultHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) - .defaultHeader(ACCEPT, APPLICATION_JSON_VALUE) - .build(); + .defaultHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) + .defaultHeader(ACCEPT, APPLICATION_JSON_VALUE) + .build(); } /** * Retrieves a file from Shared File Store using given file reference * - * @param fileRef File reference + * @param fileRef + * File reference * @return ResponseEntity */ public ResponseEntity retrieveFile(String fileRef) @@ -97,7 +99,7 @@ public class SharedFileStoreClient try { return restTemplate.getForEntity(url + "/" + fileRef, - org.springframework.core.io.Resource.class); + org.springframework.core.io.Resource.class); } catch (HttpClientErrorException e) { @@ -108,7 +110,8 @@ public class SharedFileStoreClient /** * Stores given file in Shared File Store * - * @param file File to be stored + * @param file + * File to be stored * @return A FileRefResponse containing detail about file's reference */ public FileRefResponse saveFile(File file) @@ -121,9 +124,9 @@ public class SharedFileStoreClient HttpHeaders headers = new HttpHeaders(); headers.setContentType(MULTIPART_FORM_DATA); HttpEntity> requestEntity = new HttpEntity<>(map, - headers); + headers); ResponseEntity responseEntity = restTemplate - .exchange(url, POST, requestEntity, FileRefResponse.class); + .exchange(url, POST, requestEntity, FileRefResponse.class); return responseEntity.getBody(); } catch (HttpClientErrorException e) @@ -140,7 +143,7 @@ public class SharedFileStoreClient logger.debug(" Deleting intermediate file {}", fileReference); client.delete().uri(fileReference) - .exchange().block(); + .exchange().block(); } catch (Exception e) { diff --git a/engines/base/src/main/java/org/alfresco/transform/base/transform/FragmentHandler.java b/engines/base/src/main/java/org/alfresco/transform/base/transform/FragmentHandler.java index dd083622..eeafad77 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/transform/FragmentHandler.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/transform/FragmentHandler.java @@ -26,12 +26,12 @@ */ package org.alfresco.transform.base.transform; -import org.alfresco.transform.exceptions.TransformException; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; import java.io.IOException; import java.io.OutputStream; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import org.alfresco.transform.exceptions.TransformException; /** * Separation of transform fragments logic from the {@link ProcessHandler} logic and {@link StreamHandler}. @@ -42,8 +42,7 @@ public abstract class FragmentHandler extends StreamHandler private boolean noMoreFragments; protected void initTarget() - { - } + {} public OutputStream respondWithFragment(Integer index, boolean finished) throws IOException { @@ -74,8 +73,7 @@ public abstract class FragmentHandler extends StreamHandler } protected void logFragment(Integer index, Long outputLength) - { - } + {} @Override protected void handleSuccessfulTransform() throws IOException diff --git a/engines/base/src/main/java/org/alfresco/transform/base/transform/StreamHandler.java b/engines/base/src/main/java/org/alfresco/transform/base/transform/StreamHandler.java index 29296120..0a712e78 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/transform/StreamHandler.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/transform/StreamHandler.java @@ -26,22 +26,17 @@ */ package org.alfresco.transform.base.transform; -import org.alfresco.transform.base.CustomTransformer; -import org.alfresco.transform.base.TransformManager; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import org.alfresco.transform.base.CustomTransformer; +import org.alfresco.transform.base.TransformManager; + /** - * Separation of InputStream, OutputStream, sourceFile and targetFile from the {@link ProcessHandler} logic. Allows - * {@link CustomTransformer} implementations to call {@link TransformManager#createSourceFile()} and - * {@link TransformManager#createTargetFile()} so that extra Files are not created if there was one already in - * existence. + * Separation of InputStream, OutputStream, sourceFile and targetFile from the {@link ProcessHandler} logic. Allows {@link CustomTransformer} implementations to call {@link TransformManager#createSourceFile()} and {@link TransformManager#createTargetFile()} so that extra Files are not created if there was one already in existence. * - * Subclasses MUST call transformManager.setSourceFile(File) and transformManager.setSourceFile(File) if they start - * with files rather than streams, before calling the {@link #init()} method which calls - * transformManager.setOutputStream(InputStream) and transformManager.setOutputStream(OutputStream). + * Subclasses MUST call transformManager.setSourceFile(File) and transformManager.setSourceFile(File) if they start with files rather than streams, before calling the {@link #init()} method which calls transformManager.setOutputStream(InputStream) and transformManager.setOutputStream(OutputStream). */ public abstract class StreamHandler { @@ -95,8 +90,7 @@ public abstract class StreamHandler } protected void onSuccessfulTransform() - { - } + {} protected void closeOutputStream() throws IOException { @@ -112,8 +106,7 @@ public abstract class StreamHandler inputStream.close(); } catch (IOException ignore) - { - } + {} } } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/transform/TransformHandler.java b/engines/base/src/main/java/org/alfresco/transform/base/transform/TransformHandler.java index 9cb3f330..9cbcce82 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/transform/TransformHandler.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/transform/TransformHandler.java @@ -26,34 +26,18 @@ */ package org.alfresco.transform.base.transform; -import org.alfresco.transform.base.sfs.SharedFileStoreClient; -import org.alfresco.transform.base.messaging.TransformReplySender; -import org.alfresco.transform.base.model.FileRefResponse; -import org.alfresco.transform.base.probes.ProbeTransform; -import org.alfresco.transform.base.registry.CustomTransformers; -import org.alfresco.transform.client.model.InternalContext; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.common.ExtensionService; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transform.common.TransformerDebug; -import org.alfresco.transform.messages.TransformRequestValidator; -import org.alfresco.transform.messages.TransformStack; -import org.alfresco.transform.registry.TransformServiceRegistry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.Resource; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; -import org.springframework.validation.DirectFieldBindingResult; -import org.springframework.validation.Errors; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.multipart.MultipartFile; +import static java.util.stream.Collectors.joining; + +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.CREATED; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +import static org.alfresco.transform.base.fs.FileManager.createAttachment; +import static org.alfresco.transform.base.fs.FileManager.createTargetFile; +import static org.alfresco.transform.base.fs.FileManager.getDirectAccessUrlInputStream; +import static org.alfresco.transform.base.fs.FileManager.getMultipartFileInputStream; +import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; -import jakarta.jms.Destination; -import jakarta.servlet.http.HttpServletRequest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -66,16 +50,35 @@ import java.io.OutputStream; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import jakarta.jms.Destination; +import jakarta.servlet.http.HttpServletRequest; -import static java.util.stream.Collectors.joining; -import static org.alfresco.transform.base.fs.FileManager.createAttachment; -import static org.alfresco.transform.base.fs.FileManager.createTargetFile; -import static org.alfresco.transform.base.fs.FileManager.getDirectAccessUrlInputStream; -import static org.alfresco.transform.base.fs.FileManager.getMultipartFileInputStream; -import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.HttpStatus.CREATED; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.validation.DirectFieldBindingResult; +import org.springframework.validation.Errors; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.multipart.MultipartFile; + +import org.alfresco.transform.base.messaging.TransformReplySender; +import org.alfresco.transform.base.model.FileRefResponse; +import org.alfresco.transform.base.probes.ProbeTransform; +import org.alfresco.transform.base.registry.CustomTransformers; +import org.alfresco.transform.base.sfs.SharedFileStoreClient; +import org.alfresco.transform.client.model.InternalContext; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.common.ExtensionService; +import org.alfresco.transform.common.TransformerDebug; +import org.alfresco.transform.exceptions.TransformException; +import org.alfresco.transform.messages.TransformRequestValidator; +import org.alfresco.transform.messages.TransformStack; +import org.alfresco.transform.registry.TransformServiceRegistry; /** * Handles the transform requests from either http or a message. @@ -109,9 +112,8 @@ public class TransformHandler AtomicReference> responseEntity = new AtomicReference<>(); new ProcessHandler(sourceMimetype, targetMimetype, requestParameters, - "e" + httpRequestCount.getAndIncrement(), transformRegistry, - transformerDebug, probeTransform, customTransformers) - { + "e" + httpRequestCount.getAndIncrement(), transformRegistry, + transformerDebug, probeTransform, customTransformers) { @Override protected void init() throws IOException { @@ -143,7 +145,7 @@ public class TransformHandler protected void sendTransformResponse(TransformManagerImpl transformManager) { String extension = ExtensionService.getExtensionForTargetMimetype(targetMimetype, sourceMimetype); - responseEntity.set(createAttachment("transform."+extension, transformManager.getTargetFile())); + responseEntity.set(createAttachment("transform." + extension, transformManager.getTargetFile())); } }.handleTransformRequest(); @@ -151,12 +153,11 @@ public class TransformHandler } public void handleProbeRequest(String sourceMimetype, String targetMimetype, Map transformOptions, - File sourceFile, File targetFile, ProbeTransform probeTransform) + File sourceFile, File targetFile, ProbeTransform probeTransform) { new ProcessHandler(sourceMimetype, targetMimetype, transformOptions, - "p" + httpRequestCount.getAndIncrement(), transformRegistry, - transformerDebug, probeTransform, customTransformers) - { + "p" + httpRequestCount.getAndIncrement(), transformRegistry, + transformerDebug, probeTransform, customTransformers) { @Override protected void init() throws IOException { @@ -187,13 +188,12 @@ public class TransformHandler } public TransformReply handleMessageRequest(TransformRequest request, Long timeout, Destination replyToQueue, - ProbeTransform probeTransform) + ProbeTransform probeTransform) { TransformReply reply = createBasicTransformReply(request); new ProcessHandler(request.getSourceMediaType(), request.getTargetMediaType(), - request.getTransformRequestOptions(),"unset", transformRegistry, - transformerDebug, probeTransform, customTransformers) - { + request.getTransformRequestOptions(), "unset", transformRegistry, + transformerDebug, probeTransform, customTransformers) { @Override protected void init() throws IOException { @@ -337,12 +337,12 @@ public class TransformHandler } private InputStream getInputStreamForHandleHttpRequest(Map requestParameters, - MultipartFile sourceMultipartFile) + MultipartFile sourceMultipartFile) { final String directUrl = requestParameters.getOrDefault(DIRECT_ACCESS_URL, ""); return new BufferedInputStream(directUrl.isBlank() - ? getMultipartFileInputStream(sourceMultipartFile) - : getDirectAccessUrlInputStream(directUrl)); + ? getMultipartFileInputStream(sourceMultipartFile) + : getDirectAccessUrlInputStream(directUrl)); } private InputStream getInputStreamForHandleProbeRequest(File sourceFile) @@ -419,8 +419,8 @@ public class TransformHandler { e = e.getCause(); sb.append(", cause ") - .append(e.getClass().getSimpleName()).append(": ") - .append(e.getMessage()); + .append(e.getClass().getSimpleName()).append(": ") + .append(e.getMessage()); } return sb.toString(); diff --git a/engines/base/src/main/java/org/alfresco/transform/base/util/CustomTransformerFileAdaptor.java b/engines/base/src/main/java/org/alfresco/transform/base/util/CustomTransformerFileAdaptor.java index be166ca2..6a0d7434 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/util/CustomTransformerFileAdaptor.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/util/CustomTransformerFileAdaptor.java @@ -26,17 +26,16 @@ */ package org.alfresco.transform.base.util; -import org.alfresco.transform.base.CustomTransformer; -import org.alfresco.transform.base.TransformManager; - import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.util.Map; +import org.alfresco.transform.base.CustomTransformer; +import org.alfresco.transform.base.TransformManager; + /** - * Helper interface for older code that uses Files rather than InputStreams and OutputStreams. - * If you can, refactor your code to NOT use Files. + * Helper interface for older code that uses Files rather than InputStreams and OutputStreams. If you can, refactor your code to NOT use Files. */ public interface CustomTransformerFileAdaptor extends CustomTransformer { @@ -52,4 +51,4 @@ public interface CustomTransformerFileAdaptor extends CustomTransformer void transform(String sourceMimetype, String targetMimetype, Map transformOptions, File sourceFile, File targetFile, TransformManager transformManager) throws Exception; -} \ No newline at end of file +} diff --git a/engines/base/src/main/java/org/alfresco/transform/base/util/OutputStreamLengthRecorder.java b/engines/base/src/main/java/org/alfresco/transform/base/util/OutputStreamLengthRecorder.java index 989de7e4..62a0c9f1 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/util/OutputStreamLengthRecorder.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/util/OutputStreamLengthRecorder.java @@ -61,4 +61,4 @@ public class OutputStreamLengthRecorder extends FilterOutputStream { super.write(b, off, len); } -} \ No newline at end of file +} diff --git a/engines/base/src/main/java/org/alfresco/transform/base/util/Util.java b/engines/base/src/main/java/org/alfresco/transform/base/util/Util.java index 3b95c5cd..3dcf30ba 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/util/Util.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/util/Util.java @@ -29,13 +29,13 @@ package org.alfresco.transform.base.util; public class Util { private Util() - { - } + {} /** * Safely converts a {@link String} to an {@link Integer} * - * @param param String to be converted + * @param param + * String to be converted * @return Null if param is null or converted value as {@link Integer} */ public static Integer stringToInteger(final String param) @@ -46,7 +46,8 @@ public class Util /** * Safely converts a {@link String} to a {@link Boolean} * - * @param param String to be converted + * @param param + * String to be converted * @return Null if param is null or converted value as {@link Boolean} */ public static Boolean stringToBoolean(final String param) @@ -57,7 +58,8 @@ public class Util /** * Safely converts a {@link String} to a {@link Long} * - * @param param String to be converted + * @param param + * String to be converted * @return Null if param is null or converted value as {@link Boolean} */ public static Long stringToLong(final String param) diff --git a/engines/base/src/test/java/org/alfresco/transform/base/AbstractBaseTest.java b/engines/base/src/test/java/org/alfresco/transform/base/AbstractBaseTest.java index 88251244..acba6539 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/AbstractBaseTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/AbstractBaseTest.java @@ -26,30 +26,22 @@ */ package org.alfresco.transform.base; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.alfresco.transform.base.sfs.SharedFileStoreClient; -import org.alfresco.transform.base.executors.CommandExecutor; -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.base.probes.ProbeTransform; -import org.alfresco.transform.base.transform.TransformHandler; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.registry.TransformServiceRegistry; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultActions; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + +import static org.junit.jupiter.api.Assertions.assertEquals; +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_TYPE; +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.CREATED; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +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.RequestParamMap.DIRECT_ACCESS_URL; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; import java.io.File; import java.io.FileInputStream; @@ -64,25 +56,36 @@ import java.util.UUID; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; -import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; -import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; -import static org.junit.jupiter.api.Assertions.assertEquals; -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_TYPE; -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.HttpStatus.CREATED; -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import org.alfresco.transform.base.executors.CommandExecutor; +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.base.probes.ProbeTransform; +import org.alfresco.transform.base.sfs.SharedFileStoreClient; +import org.alfresco.transform.base.transform.TransformHandler; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.registry.TransformServiceRegistry; /** * Super class for unit testing. */ -@SpringBootTest(classes={org.alfresco.transform.base.Application.class}) +@SpringBootTest(classes = {org.alfresco.transform.base.Application.class}) @AutoConfigureMockMvc public abstract class AbstractBaseTest { @@ -123,8 +126,7 @@ public abstract class AbstractBaseTest /** * The expected result. Taken resting target quick file's bytes. * - * Note: These checks generally don't work on Windows (Mac and Linux are okay). Possibly to do with byte order - * loading. + * Note: These checks generally don't work on Windows (Mac and Linux are okay). Possibly to do with byte order loading. */ protected byte[] expectedTargetFileBytes; @@ -134,7 +136,7 @@ public abstract class AbstractBaseTest private RuntimeExec origCheckCommand; protected void setMockExternalCommandsOnTransformer(CommandExecutor commandExecutor, RuntimeExec mockTransformCommand, - RuntimeExec mockCheckCommand) + RuntimeExec mockCheckCommand) { this.commandExecutor = commandExecutor; origTransformCommand = (RuntimeExec) ReflectionTestUtils.getField(commandExecutor, "transformCommand"); @@ -150,27 +152,27 @@ public abstract class AbstractBaseTest } protected void mockTransformCommand(String sourceExtension, - String targetExtension, String sourceMimetype, - boolean readTargetFileBytes) throws IOException - { - } + String targetExtension, String sourceMimetype, + boolean readTargetFileBytes) throws IOException + {} protected void updateTransformRequestWithSpecificOptions(TransformRequest transformRequest) - { - } + {} /** - * This method ends up being the core of the mock. - * It copies content from an existing file in the resources folder to the desired location - * in order to simulate a successful transformation. + * This method ends up being the core of the mock. It copies content from an existing file in the resources folder to the desired location in order to simulate a successful transformation. * - * @param actualTargetExtension Requested extension. - * @param testFile The test file (transformed) - basically the result. - * @param targetFile The location where the content from the testFile should be copied - * @throws IOException in case of any errors. + * @param actualTargetExtension + * Requested extension. + * @param testFile + * The test file (transformed) - basically the result. + * @param targetFile + * The location where the content from the testFile should be copied + * @throws IOException + * in case of any errors. */ public void generateTargetFileFromResourceFile(String actualTargetExtension, File testFile, - File targetFile) throws IOException + File targetFile) throws IOException { if (testFile == null) { @@ -179,13 +181,14 @@ public abstract class AbstractBaseTest if (testFile != null) { try (var inputStream = new FileInputStream(testFile); - var outputStream = new FileOutputStream(targetFile)) + var outputStream = new FileOutputStream(targetFile)) { FileChannel source = inputStream.getChannel(); FileChannel target = outputStream.getChannel(); target.transferFrom(source, 0, source.size()); - } catch (Exception e) + } + catch (Exception e) { throw e; } @@ -218,7 +221,7 @@ public abstract class AbstractBaseTest { // Each use of the tempDir should result in a unique directory being used testFile = new File(tempDir, testFilename); - Files.copy(classLoader.getResourceAsStream(testFilename), testFile.toPath(),REPLACE_EXISTING); + Files.copy(classLoader.getResourceAsStream(testFilename), testFile.toPath(), REPLACE_EXISTING); } return testFileUrl == null ? null : testFile; @@ -253,7 +256,7 @@ public abstract class AbstractBaseTest } private MockHttpServletRequestBuilder mockMvcRequestWithMockMultipartFile(String url, MockMultipartFile sourceFile, - String... params) + String... params) { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM).file(sourceFile); @@ -272,18 +275,18 @@ public abstract class AbstractBaseTest protected TransformRequest createTransformRequest(String sourceFileRef, File sourceFile) { return TransformRequest.builder() - .withRequestId("1") - .withSchema(1) - .withClientData("Alfresco Digital Business Platform") - .withTransformRequestOptions(options) - .withSourceReference(sourceFileRef) - .withSourceExtension(sourceExtension) - .withSourceMediaType(sourceMimetype) - .withSourceSize(sourceFile.length()) - .withTargetExtension(targetExtension) - .withTargetMediaType(targetMimetype) - .withInternalContextForTransformEngineTests() - .build(); + .withRequestId("1") + .withSchema(1) + .withClientData("Alfresco Digital Business Platform") + .withTransformRequestOptions(options) + .withSourceReference(sourceFileRef) + .withSourceExtension(sourceExtension) + .withSourceMediaType(sourceMimetype) + .withSourceSize(sourceFile.length()) + .withTargetExtension(targetExtension) + .withTargetMediaType(targetMimetype) + .withInternalContextForTransformEngineTests() + .build(); } public static void resetProbeForTesting(ProbeTransform probe) @@ -294,21 +297,21 @@ public abstract class AbstractBaseTest ReflectionTestUtils.setField(probe, "maxTime", Long.MAX_VALUE); ReflectionTestUtils.setField(probe, "nextTransformTime", 0); - ((AtomicBoolean)ReflectionTestUtils.getField(probe, "initialised")).set(false); - ((AtomicBoolean)ReflectionTestUtils.getField(probe, "readySent")).set(false); - ((AtomicLong)ReflectionTestUtils.getField(probe, "transformCount")).set(0); - ((AtomicBoolean)ReflectionTestUtils.getField(probe, "die")).set(false); + ((AtomicBoolean) ReflectionTestUtils.getField(probe, "initialised")).set(false); + ((AtomicBoolean) ReflectionTestUtils.getField(probe, "readySent")).set(false); + ((AtomicLong) ReflectionTestUtils.getField(probe, "transformCount")).set(0); + ((AtomicBoolean) ReflectionTestUtils.getField(probe, "die")).set(false); } @Test public void simpleTransformTest() throws Exception { mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile)) - .andExpect(status().isOk()) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile)) + .andExpect(status().isOk()) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Test @@ -317,11 +320,11 @@ public abstract class AbstractBaseTest sourceFile = new MockMultipartFile("file", "../quick." + sourceExtension, sourceMimetype, sourceFileBytes); mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile)) - .andExpect(status().isOk()) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile)) + .andExpect(status().isOk()) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Test @@ -330,11 +333,11 @@ public abstract class AbstractBaseTest sourceFile = new MockMultipartFile("file", "../quick", sourceMimetype, sourceFileBytes); mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile)) - .andExpect(status().isOk()) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile)) + .andExpect(status().isOk()) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Test @@ -345,14 +348,14 @@ public abstract class AbstractBaseTest probeTransform.setLivenessPercent(110); long[][] values = new long[][]{ - {5000, 0, Long.MAX_VALUE}, // 1st transform is ignored - {1000, 1000, 2100}, // 1000 + 1000*1.1 - {3000, 2000, 4200}, // 2000 + 2000*1.1 - {2000, 2000, 4200}, - {6000, 3000, 6300}, - {8000, 4000, 8400}, - {4444, 4000, 8400}, // no longer in the first few, so normal and max times don't change - {5555, 4000, 8400} + {5000, 0, Long.MAX_VALUE}, // 1st transform is ignored + {1000, 1000, 2100}, // 1000 + 1000*1.1 + {3000, 2000, 4200}, // 2000 + 2000*1.1 + {2000, 2000, 4200}, + {6000, 3000, 6300}, + {8000, 4000, 8400}, + {4444, 4000, 8400}, // no longer in the first few, so normal and max times don't change + {5555, 4000, 8400} }; for (long[] v : values) @@ -376,16 +379,16 @@ public abstract class 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(BAD_REQUEST.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(BAD_REQUEST.value())) + .andReturn().getResponse().getContentAsString(); TransformReply transformReply = objectMapper.readValue(transformationReplyAsString, - TransformReply.class); + TransformReply.class); // Assert the reply assertEquals(BAD_REQUEST.value(), transformReply.getStatus()); @@ -437,11 +440,11 @@ public abstract class AbstractBaseTest String directUrl = "file://" + dauSourceFile.toPath(); ResultActions resultActions = mockMvc.perform( - mockMvcRequest(ENDPOINT_TRANSFORM, null) - .param(DIRECT_ACCESS_URL, directUrl)) - .andExpect(status().isOk()) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform."+targetExtension)); + mockMvcRequest(ENDPOINT_TRANSFORM, null) + .param(DIRECT_ACCESS_URL, directUrl)) + .andExpect(status().isOk()) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); if (expectedTargetFileBytes != null) { diff --git a/engines/base/src/test/java/org/alfresco/transform/base/LivenessReadinessProbeTest.java b/engines/base/src/test/java/org/alfresco/transform/base/LivenessReadinessProbeTest.java index b1bde0d1..7044c12e 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/LivenessReadinessProbeTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/LivenessReadinessProbeTest.java @@ -1,24 +1,19 @@ package org.alfresco.transform.base; -import org.junit.jupiter.api.Test; - -import org.springframework.core.io.ClassPathResource; - -import org.springframework.http.client.MultipartBodyBuilder; -import org.springframework.test.web.reactive.server.WebTestClient; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.wait.strategy.Wait; - -import java.net.URISyntaxException; - -import java.util.Objects; - import static org.junit.jupiter.api.Assertions.assertEquals; - import static org.springframework.http.HttpStatus.OK; import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; +import java.net.URISyntaxException; +import java.util.Objects; + +import org.junit.jupiter.api.Test; +import org.springframework.core.io.ClassPathResource; +import org.springframework.http.client.MultipartBodyBuilder; +import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.web.reactive.function.client.WebClient; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.Wait; public abstract class LivenessReadinessProbeTest { @@ -34,12 +29,9 @@ public abstract class LivenessReadinessProbeTest env.start(); var url = "http://localhost:" + env.getFirstMappedPort(); - /* - Asserts that /ready probe hasn't died before sending a transformation request. - Each /ready request creates a valid transformation and increases the counter of - used transformations, hence the need to divide MAX_TRANSFORMS - */ - for (int i = 0; i createEnv(String image) throws URISyntaxException { System.out.println(image); - final GenericContainer transformCore = new GenericContainer<>("alfresco/"+image+":latest"); + final GenericContainer transformCore = new GenericContainer<>("alfresco/" + image + ":latest"); return transformCore.withEnv("livenessTransformEnabled", "true") - .withEnv("maxTransforms", MAX_TRANSFORMS.toString()) - .withNetworkAliases(image) - .withExposedPorts(8090) - .waitingFor(Wait.forListeningPort()); + .withEnv("maxTransforms", MAX_TRANSFORMS.toString()) + .withNetworkAliases(image) + .withExposedPorts(8090) + .waitingFor(Wait.forListeningPort()); } protected static class ImagesForTests @@ -106,7 +98,7 @@ public abstract class LivenessReadinessProbeTest private static void assertProbeDied(String url) { - WebTestClient client = WebTestClient.bindToServer().baseUrl(url+"/ready").build(); + WebTestClient client = WebTestClient.bindToServer().baseUrl(url + "/ready").build(); client.get() .exchange() .expectStatus().isEqualTo(TOO_MANY_REQUESTS); @@ -114,9 +106,9 @@ public abstract class LivenessReadinessProbeTest private static void assertProbeIsOk(String url) { - WebTestClient client = WebTestClient.bindToServer().baseUrl(url+"/ready").build(); + WebTestClient client = WebTestClient.bindToServer().baseUrl(url + "/ready").build(); client.get() .exchange() .expectStatus().isEqualTo(OK); } -} \ No newline at end of file +} diff --git a/engines/base/src/test/java/org/alfresco/transform/base/MtlsTestUtils.java b/engines/base/src/test/java/org/alfresco/transform/base/MtlsTestUtils.java index ff4fd220..384bc861 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/MtlsTestUtils.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/MtlsTestUtils.java @@ -26,6 +26,18 @@ */ package org.alfresco.transform.base; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import javax.net.ssl.SSLContext; + import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; @@ -42,19 +54,8 @@ import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; -import javax.net.ssl.SSLContext; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; - -public class MtlsTestUtils { +public class MtlsTestUtils +{ private static final boolean MTLS_ENABLED = Boolean.parseBoolean(System.getProperty("test-mtls-enabled")); private static final boolean HOSTNAME_VERIFICATION_DISABLED = Boolean.parseBoolean(System.getProperty("test-client-disable-hostname-verification")); @@ -96,20 +97,20 @@ public class MtlsTestUtils { return HttpClients.custom().setConnectionManager(buildSslConnectionManager(sslContext)).build(); } - private static HttpClientConnectionManager buildSslConnectionManager(SSLContext sslContext) { - final SSLConnectionSocketFactoryBuilder sslConnectionSocketFactoryBuilder = - SSLConnectionSocketFactoryBuilder.create() - .setSslContext(sslContext) - .setTlsVersions(TLS.V_1_2, TLS.V_1_3); - if (HOSTNAME_VERIFICATION_DISABLED) { + private static HttpClientConnectionManager buildSslConnectionManager(SSLContext sslContext) + { + final SSLConnectionSocketFactoryBuilder sslConnectionSocketFactoryBuilder = SSLConnectionSocketFactoryBuilder.create() + .setSslContext(sslContext) + .setTlsVersions(TLS.V_1_2, TLS.V_1_3); + if (HOSTNAME_VERIFICATION_DISABLED) + { sslConnectionSocketFactoryBuilder.setHostnameVerifier(NoopHostnameVerifier.INSTANCE); } final SSLConnectionSocketFactory sslConnectionSocketFactory = sslConnectionSocketFactoryBuilder.build(); - final Registry sslSocketFactoryRegistry = - RegistryBuilder. create() - .register("https", sslConnectionSocketFactory) - .build(); + final Registry sslSocketFactoryRegistry = RegistryBuilder. create() + .register("https", sslConnectionSocketFactory) + .build(); return new PoolingHttpClientConnectionManager(sslSocketFactoryRegistry); } @@ -117,9 +118,12 @@ public class MtlsTestUtils { public static RestTemplate restTemplateWithMtls() { ClientHttpRequestFactory requestFactory = null; - try { + try + { requestFactory = new HttpComponentsClientHttpRequestFactory(httpClientWithMtls()); - } catch (Exception e) { + } + catch (Exception e) + { e.printStackTrace(); } return new RestTemplate(requestFactory); @@ -130,7 +134,8 @@ public class MtlsTestUtils { return MtlsTestUtils.isMtlsEnabled() ? MtlsTestUtils.restTemplateWithMtls() : new RestTemplate(); } - public static CloseableHttpClient getHttpClient() throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException { + public static CloseableHttpClient getHttpClient() throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException + { return MtlsTestUtils.isMtlsEnabled() ? MtlsTestUtils.httpClientWithMtls() : HttpClients.createDefault(); } } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/clients/HttpClient.java b/engines/base/src/test/java/org/alfresco/transform/base/clients/HttpClient.java index ab51b887..54ad0f6f 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/clients/HttpClient.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/clients/HttpClient.java @@ -8,12 +8,13 @@ package org.alfresco.transform.base.clients; import static java.util.Collections.emptyMap; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; + import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; + import java.util.Map; -import org.alfresco.transform.base.MtlsTestUtils; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; @@ -23,6 +24,8 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import org.alfresco.transform.base.MtlsTestUtils; + /** * @author Cezar Leahu */ @@ -31,16 +34,16 @@ public class HttpClient private static final RestTemplate REST_TEMPLATE = MtlsTestUtils.getRestTemplate(); public static ResponseEntity sendTRequest( - final String engineUrl, final String sourceFile, - final String sourceMimetype, final String targetMimetype, final String targetExtension) + final String engineUrl, final String sourceFile, + final String sourceMimetype, final String targetMimetype, final String targetExtension) { return sendTRequest(engineUrl, sourceFile, sourceMimetype, targetMimetype, targetExtension, emptyMap()); } public static ResponseEntity sendTRequest( - final String engineUrl, final String sourceFile, - final String sourceMimetype, final String targetMimetype, final String targetExtension, - final Map transformOptions) + final String engineUrl, final String sourceFile, + final String sourceMimetype, final String targetMimetype, final String targetExtension, + final Map transformOptions) { final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MULTIPART_FORM_DATA); diff --git a/engines/base/src/test/java/org/alfresco/transform/base/clients/JacksonSerializer.java b/engines/base/src/test/java/org/alfresco/transform/base/clients/JacksonSerializer.java index 9b4fb5fd..2189d41a 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/clients/JacksonSerializer.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/clients/JacksonSerializer.java @@ -34,7 +34,7 @@ public class JacksonSerializer public static byte[] serialize(T value) throws Exception { try (final ByteArrayOutputStream stream = new ByteArrayOutputStream(1024); - final OutputStreamWriter writer = new OutputStreamWriter(stream, UTF_8)) + final OutputStreamWriter writer = new OutputStreamWriter(stream, UTF_8)) { MAPPER.writer().writeValue(writer, value); return stream.toByteArray(); diff --git a/engines/base/src/test/java/org/alfresco/transform/base/clients/JmsClient.java b/engines/base/src/test/java/org/alfresco/transform/base/clients/JmsClient.java index 1c73f200..87d68a3f 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/clients/JmsClient.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/clients/JmsClient.java @@ -18,17 +18,16 @@ import jakarta.jms.MessageProducer; import jakarta.jms.Session; import jakarta.jms.TextMessage; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQQueue; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; + /** * JMSClient * - * Contains the bare minimum logic necessary for sending and receiving T-Request/Reply messages - * through the basic vanilla ActiveMQ client. + * Contains the bare minimum logic necessary for sending and receiving T-Request/Reply messages through the basic vanilla ActiveMQ client. * * Used by Aspose t-engine and t-router, but likely to be useful in other t-engines. * @@ -51,29 +50,28 @@ public class JmsClient } public void sendBytesMessage(final TransformRequest request) - throws Exception + throws Exception { sendBytesMessage(request, request.getRequestId()); } public void sendBytesMessage(final TransformRequest request, final String correlationID) - throws Exception + throws Exception { sendBytesMessage(JacksonSerializer.serialize(request), correlationID); } public void sendBytesMessage(final TransformRequest request, final String correlationID, - final Destination replyTo) throws Exception + final Destination replyTo) throws Exception { sendBytesMessage(JacksonSerializer.serialize(request), correlationID, replyTo); } - public void sendBytesMessage(final byte[] data, final String correlationID) throws - Exception + public void sendBytesMessage(final byte[] data, final String correlationID) throws Exception { try (final Connection connection = factory.createConnection(); - final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - final MessageProducer producer = session.createProducer(queue)) + final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + final MessageProducer producer = session.createProducer(queue)) { producer.setDeliveryMode(DeliveryMode.PERSISTENT); final BytesMessage message = session.createBytesMessage(); @@ -87,11 +85,11 @@ public class JmsClient } public void sendBytesMessage(final byte[] data, final String correlationID, - final Destination replyTo) throws Exception + final Destination replyTo) throws Exception { try (final Connection connection = factory.createConnection(); - final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - final MessageProducer producer = session.createProducer(queue)) + final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + final MessageProducer producer = session.createProducer(queue)) { producer.setDeliveryMode(DeliveryMode.PERSISTENT); final BytesMessage message = session.createBytesMessage(); @@ -109,23 +107,22 @@ public class JmsClient } public void sendTextMessage(final TransformRequest request) - throws Exception + throws Exception { sendTextMessage(request, request.getRequestId()); } public void sendTextMessage(final TransformRequest request, final String correlationID) - throws Exception + throws Exception { sendTextMessage(new String(JacksonSerializer.serialize(request)), correlationID); } - public void sendTextMessage(final String data, final String correlationID) throws - Exception + public void sendTextMessage(final String data, final String correlationID) throws Exception { try (final Connection connection = factory.createConnection(); - final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - final MessageProducer producer = session.createProducer(queue)) + final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + final MessageProducer producer = session.createProducer(queue)) { producer.setDeliveryMode(DeliveryMode.PERSISTENT); final TextMessage message = session.createTextMessage(data); @@ -143,11 +140,11 @@ public class JmsClient } public TransformReply receiveMessage(final long timeout) - throws Exception + throws Exception { try (final Connection connection = factory.createConnection(); - final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - final MessageConsumer consumer = session.createConsumer(queue)) + final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + final MessageConsumer consumer = session.createConsumer(queue)) { connection.start(); @@ -167,11 +164,9 @@ public class JmsClient try { while (receiveMessage(2 * 1000) != null) - { - } + {} } catch (Exception ignore) - { - } + {} } } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/clients/SfsClient.java b/engines/base/src/test/java/org/alfresco/transform/base/clients/SfsClient.java index e2aa6562..7af1ce45 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/clients/SfsClient.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/clients/SfsClient.java @@ -21,7 +21,9 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import org.alfresco.transform.base.MtlsTestUtils; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import com.google.common.collect.ImmutableMap; import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.classic.methods.HttpHead; import org.apache.hc.client5.http.classic.methods.HttpPost; @@ -36,10 +38,7 @@ import org.apache.hc.core5.http.HttpResponse; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.slf4j.LoggerFactory; -import com.google.common.collect.ImmutableMap; - -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; +import org.alfresco.transform.base.MtlsTestUtils; /** * Used by Aspose t-engine and t-router, but likely to be useful in other t-engines. @@ -69,12 +68,12 @@ public class SfsClient final File file = readFile(fileToUploadName); final HttpPost post = new HttpPost( - sfsBaseUrl+"/alfresco/api/-default-/private/sfs/versions/1/file"); + sfsBaseUrl + "/alfresco/api/-default-/private/sfs/versions/1/file"); post.setEntity(MultipartEntityBuilder - .create() - .setMode(HttpMultipartMode.LEGACY) - .addPart("file", new FileBody(file, ContentType.DEFAULT_BINARY)) - .build()); + .create() + .setMode(HttpMultipartMode.LEGACY) + .addPart("file", new FileBody(file, ContentType.DEFAULT_BINARY)) + .build()); try (CloseableHttpClient client = MtlsTestUtils.getHttpClient()) { @@ -83,7 +82,7 @@ public class SfsClient if (status >= 200 && status < 300) { return JacksonSerializer.readStringValue(EntityUtils.toString(((HttpEntityContainer) response).getEntity()), - "entry.fileRef"); + "entry.fileRef"); } else { @@ -114,7 +113,7 @@ public class SfsClient { final String[] array = uri.toString().split("!"); try (final FileSystem fs = FileSystems.newFileSystem(URI.create(array[0]), - ImmutableMap.of("create", "true"))) + ImmutableMap.of("create", "true"))) { File temp = File.createTempFile("temp-", "", new File(System.getProperty("user.dir"))); temp.deleteOnExit(); @@ -124,7 +123,7 @@ public class SfsClient } } - public static boolean checkFile(final String uuid) throws Exception + public static boolean checkFile(final String uuid) throws Exception { return checkFile(uuid, SFS_BASE_URL); } @@ -132,8 +131,8 @@ public class SfsClient public static boolean checkFile(final String uuid, final String sfsBaseUrl) throws Exception { final HttpHead head = new HttpHead(format( - sfsBaseUrl+"/alfresco/api/-default-/private/sfs/versions/1/file/{0}", - uuid)); + sfsBaseUrl + "/alfresco/api/-default-/private/sfs/versions/1/file/{0}", + uuid)); try (CloseableHttpClient client = MtlsTestUtils.getHttpClient()) { @@ -151,8 +150,8 @@ public class SfsClient public static File downloadFile(final String uuid, final String sfsBaseUrl) throws Exception { final HttpGet get = new HttpGet(format( - sfsBaseUrl+"/alfresco/api/-default-/private/sfs/versions/1/file/{0}", - uuid)); + sfsBaseUrl + "/alfresco/api/-default-/private/sfs/versions/1/file/{0}", + uuid)); try (CloseableHttpClient client = MtlsTestUtils.getHttpClient()) { @@ -169,7 +168,7 @@ public class SfsClient } final File file = File.createTempFile(uuid, "_tmp", - new File(System.getProperty("user.dir"))); + new File(System.getProperty("user.dir"))); file.deleteOnExit(); try (OutputStream os = new FileOutputStream(file)) diff --git a/engines/base/src/test/java/org/alfresco/transform/base/clients/SourceTarget.java b/engines/base/src/test/java/org/alfresco/transform/base/clients/SourceTarget.java index da93457e..ccdbf7a6 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/clients/SourceTarget.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/clients/SourceTarget.java @@ -47,11 +47,13 @@ public class SourceTarget @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; SourceTarget that = (SourceTarget) o; return Objects.equals(source, that.source) && - Objects.equals(target, that.target); + Objects.equals(target, that.target); } @Override diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/AbstractFakeTransformEngine.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/AbstractFakeTransformEngine.java index 6e1cbdb3..0ac00a7f 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/AbstractFakeTransformEngine.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/AbstractFakeTransformEngine.java @@ -26,9 +26,10 @@ */ package org.alfresco.transform.base.fakes; +import org.springframework.boot.test.context.TestComponent; + import org.alfresco.transform.base.TransformEngine; import org.alfresco.transform.base.probes.ProbeTransform; -import org.springframework.boot.test.context.TestComponent; /** * Subclass MUST be named FakeTransformEngineWith\ otherwise the engine name will be "undefined". @@ -39,18 +40,20 @@ public abstract class AbstractFakeTransformEngine implements TransformEngine private static final String FAKE_TRANSFORM_ENGINE_WITH = "FakeTransformEngineWith"; - @Override public String getTransformEngineName() + @Override + public String getTransformEngineName() { String simpleClassName = getClass().getSimpleName(); return simpleClassName.startsWith(FAKE_TRANSFORM_ENGINE_WITH) - ? "0000 "+simpleClassName.substring(FAKE_TRANSFORM_ENGINE_WITH.length()) - : "undefined"; + ? "0000 " + simpleClassName.substring(FAKE_TRANSFORM_ENGINE_WITH.length()) + : "undefined"; } - @Override public String getStartupMessage() + @Override + public String getStartupMessage() { - return "Startup "+getTransformEngineName()+ - "\nLine 2 "+getTransformEngineName()+ + return "Startup " + getTransformEngineName() + + "\nLine 2 " + getTransformEngineName() + "\nLine 3"; } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/AbstractFakeTransformer.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/AbstractFakeTransformer.java index cc093e86..0bac2c0f 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/AbstractFakeTransformer.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/AbstractFakeTransformer.java @@ -26,21 +26,21 @@ */ package org.alfresco.transform.base.fakes; -import org.alfresco.transform.base.CustomTransformer; -import org.alfresco.transform.base.TransformManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.test.context.TestComponent; - import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.test.context.TestComponent; + +import org.alfresco.transform.base.CustomTransformer; +import org.alfresco.transform.base.TransformManager; + /** - * Subclass MUST be named FakeTransformer\. Appends the name of the CustomTransformer and any t-options - * to the output. The output is always a String regardless of the stated mimetypes. + * Subclass MUST be named FakeTransformer\. Appends the name of the CustomTransformer and any t-options to the output. The output is always a String regardless of the stated mimetypes. */ @TestComponent public abstract class AbstractFakeTransformer implements CustomTransformer @@ -65,9 +65,9 @@ public abstract class AbstractFakeTransformer implements CustomTransformer .append(getTransformerName()) .append("(") .append(transformOptions.entrySet() - .stream() - .map(e -> e.getKey() + '=' + e.getValue()) - .collect(Collectors.joining(", "))) + .stream() + .map(e -> e.getKey() + '=' + e.getValue()) + .collect(Collectors.joining(", "))) .append(')') .toString(); logger.info(newValue); diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithAllInOne.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithAllInOne.java index cea3577e..60986479 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithAllInOne.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithAllInOne.java @@ -26,23 +26,26 @@ */ package org.alfresco.transform.base.fakes; +import org.springframework.beans.factory.annotation.Autowired; + import org.alfresco.transform.base.probes.ProbeTransform; import org.alfresco.transform.config.TransformConfig; -import org.springframework.beans.factory.annotation.Autowired; public class FakeTransformEngineWithAllInOne extends AbstractFakeTransformEngine { @Autowired private FakeTransformEngineWithTwoCustomTransformers oneOfTheTransformEngines; - @Override public TransformConfig getTransformConfig() + @Override + public TransformConfig getTransformConfig() { // Has no config of its own. The combined config of the others is returned from the t-engine. return null; } - @Override public ProbeTransform getProbeTransform() + @Override + public ProbeTransform getProbeTransform() { return oneOfTheTransformEngines.getProbeTransform(); } -} \ No newline at end of file +} diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithFragments.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithFragments.java index b50dfc19..1460bfaa 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithFragments.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithFragments.java @@ -26,38 +26,41 @@ */ package org.alfresco.transform.base.fakes; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; + +import java.util.Collections; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; + import org.alfresco.transform.base.probes.ProbeTransform; import org.alfresco.transform.config.SupportedSourceAndTarget; import org.alfresco.transform.config.TransformConfig; import org.alfresco.transform.config.Transformer; -import java.util.Collections; - -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; - -public class FakeTransformEngineWithFragments extends AbstractFakeTransformEngine +public class FakeTransformEngineWithFragments extends AbstractFakeTransformEngine { - @Override public TransformConfig getTransformConfig() + @Override + public TransformConfig getTransformConfig() { return TransformConfig.builder() - .withTransformers(ImmutableList.of( - Transformer.builder() - .withTransformerName("Fragments") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType(MIMETYPE_PDF) - .withTargetMediaType(MIMETYPE_IMAGE_JPEG) - .build())) - .build())) - .build(); + .withTransformers(ImmutableList.of( + Transformer.builder() + .withTransformerName("Fragments") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType(MIMETYPE_PDF) + .withTargetMediaType(MIMETYPE_IMAGE_JPEG) + .build())) + .build())) + .build(); } - @Override public ProbeTransform getProbeTransform() + @Override + public ProbeTransform getProbeTransform() { return new ProbeTransform("probe.pdf", MIMETYPE_PDF, MIMETYPE_IMAGE_JPEG, Collections.emptyMap(), - 60, 16, 400, 10240, 60 * 30 + 1, 60 * 15 + 20); + 60, 16, 400, 10240, 60 * 30 + 1, 60 * 15 + 20); } } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithOneCustomTransformer.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithOneCustomTransformer.java index 84b55663..692ec880 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithOneCustomTransformer.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithOneCustomTransformer.java @@ -26,43 +26,46 @@ */ package org.alfresco.transform.base.fakes; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; + import org.alfresco.transform.base.probes.ProbeTransform; import org.alfresco.transform.config.SupportedSourceAndTarget; import org.alfresco.transform.config.TransformConfig; import org.alfresco.transform.config.TransformOptionValue; import org.alfresco.transform.config.Transformer; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; - public class FakeTransformEngineWithOneCustomTransformer extends AbstractFakeTransformEngine { - @Override public TransformConfig getTransformConfig() + @Override + public TransformConfig getTransformConfig() { String imageOptions = "imageOptions"; return TransformConfig.builder() - .withTransformOptions(ImmutableMap.of( - imageOptions, ImmutableSet.of( - new TransformOptionValue(false, "width"), - new TransformOptionValue(false, "height")))) - .withTransformers(ImmutableList.of( - Transformer.builder() - .withTransformerName("Pdf2Jpg") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType(MIMETYPE_PDF) - .withTargetMediaType(MIMETYPE_IMAGE_JPEG) - .build())) - .withTransformOptions(ImmutableSet.of(imageOptions)) - .build())) - .build(); + .withTransformOptions(ImmutableMap.of( + imageOptions, ImmutableSet.of( + new TransformOptionValue(false, "width"), + new TransformOptionValue(false, "height")))) + .withTransformers(ImmutableList.of( + Transformer.builder() + .withTransformerName("Pdf2Jpg") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType(MIMETYPE_PDF) + .withTargetMediaType(MIMETYPE_IMAGE_JPEG) + .build())) + .withTransformOptions(ImmutableSet.of(imageOptions)) + .build())) + .build(); } - @Override public ProbeTransform getProbeTransform() + @Override + public ProbeTransform getProbeTransform() { return null; // Not used in tests } -} \ No newline at end of file +} diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithTwoCustomTransformers.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithTwoCustomTransformers.java index c133ecda..36bc1a45 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithTwoCustomTransformers.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformEngineWithTwoCustomTransformers.java @@ -26,9 +26,18 @@ */ package org.alfresco.transform.base.fakes; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; + +import java.util.List; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; + import org.alfresco.transform.base.probes.ProbeTransform; import org.alfresco.transform.config.SupportedSourceAndTarget; import org.alfresco.transform.config.TransformConfig; @@ -36,14 +45,6 @@ import org.alfresco.transform.config.TransformOptionValue; import org.alfresco.transform.config.TransformStep; import org.alfresco.transform.config.Transformer; -import java.util.List; - -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; -import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; - public class FakeTransformEngineWithTwoCustomTransformers extends AbstractFakeTransformEngine { @Override @@ -52,63 +53,63 @@ public class FakeTransformEngineWithTwoCustomTransformers extends AbstractFakeTr String docOptions = "docOptions"; String imageOptions = "imageOptions"; return TransformConfig.builder() - .withTransformOptions(ImmutableMap.of( - docOptions, ImmutableSet.of( - new TransformOptionValue(false, "page")), - imageOptions, ImmutableSet.of( - new TransformOptionValue(false, "width"), - new TransformOptionValue(false, "height")))) - .withTransformers(ImmutableList.of( - Transformer.builder() - .withTransformerName("TxT2Pdf") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType(MIMETYPE_TEXT_PLAIN) - .withTargetMediaType(MIMETYPE_PDF) - .build())) - .withTransformOptions(ImmutableSet.of(docOptions)) - .build(), - Transformer.builder() - .withTransformerName("Pdf2Png") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType(MIMETYPE_PDF) - .withTargetMediaType(MIMETYPE_IMAGE_PNG) - .build())) - .withTransformOptions(ImmutableSet.of(imageOptions)) - .build(), - Transformer.builder() - .withTransformerName("Txt2PngViaPdf") - .withTransformerPipeline(List.of( - new TransformStep("TxT2Pdf", MIMETYPE_PDF), - new TransformStep("Pdf2Png", null))) - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType(MIMETYPE_TEXT_PLAIN) - .withTargetMediaType(MIMETYPE_IMAGE_PNG) - .build())) - .withTransformOptions(ImmutableSet.of(imageOptions)) - .build(), - Transformer.builder() // Unavailable until Pdf2Jpg is added - .withTransformerName("Txt2JpgViaPdf") - .withTransformerPipeline(List.of( - new TransformStep("TxT2Pdf", MIMETYPE_PDF), - new TransformStep("Pdf2Jpg", null))) - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType(MIMETYPE_TEXT_PLAIN) - .withTargetMediaType(MIMETYPE_IMAGE_JPEG) - .build())) - .withTransformOptions(ImmutableSet.of(imageOptions)) - .build())) - .build(); + .withTransformOptions(ImmutableMap.of( + docOptions, ImmutableSet.of( + new TransformOptionValue(false, "page")), + imageOptions, ImmutableSet.of( + new TransformOptionValue(false, "width"), + new TransformOptionValue(false, "height")))) + .withTransformers(ImmutableList.of( + Transformer.builder() + .withTransformerName("TxT2Pdf") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType(MIMETYPE_TEXT_PLAIN) + .withTargetMediaType(MIMETYPE_PDF) + .build())) + .withTransformOptions(ImmutableSet.of(docOptions)) + .build(), + Transformer.builder() + .withTransformerName("Pdf2Png") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType(MIMETYPE_PDF) + .withTargetMediaType(MIMETYPE_IMAGE_PNG) + .build())) + .withTransformOptions(ImmutableSet.of(imageOptions)) + .build(), + Transformer.builder() + .withTransformerName("Txt2PngViaPdf") + .withTransformerPipeline(List.of( + new TransformStep("TxT2Pdf", MIMETYPE_PDF), + new TransformStep("Pdf2Png", null))) + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType(MIMETYPE_TEXT_PLAIN) + .withTargetMediaType(MIMETYPE_IMAGE_PNG) + .build())) + .withTransformOptions(ImmutableSet.of(imageOptions)) + .build(), + Transformer.builder() // Unavailable until Pdf2Jpg is added + .withTransformerName("Txt2JpgViaPdf") + .withTransformerPipeline(List.of( + new TransformStep("TxT2Pdf", MIMETYPE_PDF), + new TransformStep("Pdf2Jpg", null))) + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType(MIMETYPE_TEXT_PLAIN) + .withTargetMediaType(MIMETYPE_IMAGE_JPEG) + .build())) + .withTransformOptions(ImmutableSet.of(imageOptions)) + .build())) + .build(); } @Override public ProbeTransform getProbeTransform() { return new ProbeTransform("original.txt", MIMETYPE_TEXT_PLAIN, MIMETYPE_PDF, - ImmutableMap.of(SOURCE_ENCODING, "UTF-8"), 46, 0, - 150, 1024, 1, 60 * 2); + ImmutableMap.of(SOURCE_ENCODING, "UTF-8"), 46, 0, + 150, 1024, 1, 60 * 2); } } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerFragments.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerFragments.java index 961ff594..bb1a75bf 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerFragments.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerFragments.java @@ -26,31 +26,23 @@ */ package org.alfresco.transform.base.fakes; -import org.alfresco.transform.base.TransformManager; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.Map; +import org.alfresco.transform.base.TransformManager; + /** - * Returns lines in the supplied input as a sequence of transform result fragments. - * - If the current line is {@code "Null"} no output is made and {@code null} is passed as the {@code index} to - * {@link TransformManager#respondWithFragment(Integer, boolean)}. The {code finished} parameter is unset. - * - If {@code "Finished"}, the text is written and the {code finished} parameter is set. - * - If the current line is {@code "NullFinished"} no output is made and {@code null} is passed as the {@code index} to - * {@code respondWithFragment}. The {code finished} parameter is set. - * - If {@code "Ignored"} it will be written to the output, but the {@code respondWithFragment} method will not be - * called, so should be ignored if the final line. - * If the input is "WithoutFragments", {@code respondWithFragment} is not called. + * Returns lines in the supplied input as a sequence of transform result fragments. - If the current line is {@code "Null"} no output is made and {@code null} is passed as the {@code index} to {@link TransformManager#respondWithFragment(Integer, boolean)}. The {code finished} parameter is unset. - If {@code "Finished"}, the text is written and the {code finished} parameter is set. - If the current line is {@code "NullFinished"} no output is made and {@code null} is passed as the {@code index} to {@code respondWithFragment}. The {code finished} parameter is set. - If {@code "Ignored"} it will be written to the output, but the {@code respondWithFragment} method will not be called, so should be ignored if the final line. If the input is "WithoutFragments", {@code respondWithFragment} is not called. */ public class FakeTransformerFragments extends AbstractFakeTransformer { @Override public void transform(String sourceMimetype, InputStream inputStream, String targetMimetype, - OutputStream outputStream, Map transformOptions, TransformManager transformManager) - throws Exception + OutputStream outputStream, Map transformOptions, TransformManager transformManager) + throws Exception { String input = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); String[] lines = input.split("\n"); @@ -69,7 +61,8 @@ public class FakeTransformerFragments extends AbstractFakeTransformer { write(outputStream, line); } - if (!"Ignored".equals(line)) { + if (!"Ignored".equals(line)) + { outputStream = transformManager.respondWithFragment(index, finished); } } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerPdf2Jpg.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerPdf2Jpg.java index dc3e8dad..c386e1d9 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerPdf2Jpg.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerPdf2Jpg.java @@ -27,5 +27,4 @@ package org.alfresco.transform.base.fakes; public class FakeTransformerPdf2Jpg extends AbstractFakeTransformer -{ -} +{} diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerPdf2Png.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerPdf2Png.java index cf228a82..7760233b 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerPdf2Png.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerPdf2Png.java @@ -27,5 +27,4 @@ package org.alfresco.transform.base.fakes; public class FakeTransformerPdf2Png extends AbstractFakeTransformer -{ -} +{} diff --git a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerTxT2Pdf.java b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerTxT2Pdf.java index d11e357c..48c2cd44 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerTxT2Pdf.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/fakes/FakeTransformerTxT2Pdf.java @@ -27,5 +27,4 @@ package org.alfresco.transform.base.fakes; public class FakeTransformerTxT2Pdf extends AbstractFakeTransformer -{ -} +{} diff --git a/engines/base/src/test/java/org/alfresco/transform/base/http/OptionsHelperTest.java b/engines/base/src/test/java/org/alfresco/transform/base/http/OptionsHelperTest.java index e47e2538..4377e173 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/http/OptionsHelperTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/http/OptionsHelperTest.java @@ -26,21 +26,23 @@ */ package org.alfresco.transform.base.http; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import org.alfresco.transform.config.TransformOption; -import org.alfresco.transform.config.TransformOptionGroup; -import org.alfresco.transform.config.TransformOptionValue; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import static org.alfresco.transform.base.html.OptionsHelper.getOptionNames; import java.util.ArrayList; import java.util.Collections; import java.util.Map; import java.util.Set; -import static org.alfresco.transform.base.html.OptionsHelper.getOptionNames; -import static org.junit.jupiter.api.Assertions.assertEquals; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.Test; + +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.TransformOptionGroup; +import org.alfresco.transform.config.TransformOptionValue; public class OptionsHelperTest { @@ -56,7 +58,7 @@ public class OptionsHelperTest public void singleOptionNameWithSingleValue() { Map> transformOptionsByName = ImmutableMap.of("Dummy", ImmutableSet.of( - new TransformOptionValue(true, "startPage"))); + new TransformOptionValue(true, "startPage"))); assertEquals(ImmutableSet.of("startPage"), getOptionNames(transformOptionsByName)); } @@ -65,7 +67,7 @@ public class OptionsHelperTest public void whenOptionNameEndsInOptions_stripIt() { Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( - new TransformOptionValue(true, "startPage"))); + new TransformOptionValue(true, "startPage"))); assertEquals(ImmutableSet.of("startPage"), getOptionNames(transformOptionsByName)); } @@ -74,7 +76,7 @@ public class OptionsHelperTest public void singleOptionNameWithASingleRequiredValue() { Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( - new TransformOptionValue(true, "startPage"))); + new TransformOptionValue(true, "startPage"))); assertEquals(ImmutableSet.of("startPage"), getOptionNames(transformOptionsByName)); } @@ -83,8 +85,8 @@ public class OptionsHelperTest public void singleOptionNameWithACoupleOfValues() { Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( - new TransformOptionValue(false, "startPage"), - new TransformOptionValue(true, "endPage"))); + new TransformOptionValue(false, "startPage"), + new TransformOptionValue(true, "endPage"))); assertEquals(ImmutableSet.of("startPage", "endPage"), getOptionNames(transformOptionsByName)); } @@ -93,11 +95,11 @@ public class OptionsHelperTest public void sortedValues() { Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( - new TransformOptionValue(false, "a"), - new TransformOptionValue(false, "n"), - new TransformOptionValue(false, "k"), - new TransformOptionValue(false, "f"), - new TransformOptionValue(true, "z"))); + new TransformOptionValue(false, "a"), + new TransformOptionValue(false, "n"), + new TransformOptionValue(false, "k"), + new TransformOptionValue(false, "f"), + new TransformOptionValue(true, "z"))); assertEquals(ImmutableList.of("a", "f", "k", "n", "z"), new ArrayList<>(getOptionNames(transformOptionsByName))); } @@ -108,13 +110,12 @@ public class OptionsHelperTest Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( new TransformOptionValue(false, "startPage"), new TransformOptionValue(true, "endPage")), - "Another", ImmutableSet.of( - new TransformOptionValue(false, "scale")), - "YetAnother", ImmutableSet.of( - new TransformOptionValue(false, "x"), - new TransformOptionValue(false, "y"), - new TransformOptionValue(true, "ratio")) - ); + "Another", ImmutableSet.of( + new TransformOptionValue(false, "scale")), + "YetAnother", ImmutableSet.of( + new TransformOptionValue(false, "x"), + new TransformOptionValue(false, "y"), + new TransformOptionValue(true, "ratio"))); assertEquals(ImmutableSet.of( "startPage", @@ -123,7 +124,7 @@ public class OptionsHelperTest "x", "y", "ratio"), - getOptionNames(transformOptionsByName)); + getOptionNames(transformOptionsByName)); } @Test @@ -132,13 +133,12 @@ public class OptionsHelperTest Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( new TransformOptionValue(false, "startPage"), new TransformOptionValue(true, "endPage")), - "Another", ImmutableSet.of( - new TransformOptionValue(false, "scale")), - "YetAnother", ImmutableSet.of( - new TransformOptionValue(false, "x"), - new TransformOptionValue(false, "y"), - new TransformOptionValue(true, "scale")) - ); + "Another", ImmutableSet.of( + new TransformOptionValue(false, "scale")), + "YetAnother", ImmutableSet.of( + new TransformOptionValue(false, "x"), + new TransformOptionValue(false, "y"), + new TransformOptionValue(true, "scale"))); assertEquals(ImmutableSet.of( "startPage", @@ -146,27 +146,27 @@ public class OptionsHelperTest "scale", "x", "y"), - getOptionNames(transformOptionsByName)); + getOptionNames(transformOptionsByName)); } @Test public void nestedGroups() { Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( - new TransformOptionValue(false, "1"), - new TransformOptionValue(true, "2"), - new TransformOptionGroup(false, ImmutableSet.of( - new TransformOptionValue(false, "3.1"), - new TransformOptionValue(true, "3.2"), - new TransformOptionValue(false, "3.3"))), - new TransformOptionGroup(true, ImmutableSet.of( - new TransformOptionValue(false, "4.1"), + new TransformOptionValue(false, "1"), + new TransformOptionValue(true, "2"), new TransformOptionGroup(false, ImmutableSet.of( - new TransformOptionValue(false, "4.2.1"), - new TransformOptionGroup(true, ImmutableSet.of( - new TransformOptionValue(false, "4.2.2.1"))), - new TransformOptionValue(true, "4.2.3"))), - new TransformOptionValue(false, "4.3"))))); + new TransformOptionValue(false, "3.1"), + new TransformOptionValue(true, "3.2"), + new TransformOptionValue(false, "3.3"))), + new TransformOptionGroup(true, ImmutableSet.of( + new TransformOptionValue(false, "4.1"), + new TransformOptionGroup(false, ImmutableSet.of( + new TransformOptionValue(false, "4.2.1"), + new TransformOptionGroup(true, ImmutableSet.of( + new TransformOptionValue(false, "4.2.2.1"))), + new TransformOptionValue(true, "4.2.3"))), + new TransformOptionValue(false, "4.3"))))); assertEquals(ImmutableSet.of( "1", @@ -179,6 +179,6 @@ public class OptionsHelperTest "4.2.2.1", "4.2.3", "4.3"), - getOptionNames(transformOptionsByName)); + getOptionNames(transformOptionsByName)); } } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/http/RestTest.java b/engines/base/src/test/java/org/alfresco/transform/base/http/RestTest.java index 798afc92..c6fe51b2 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/http/RestTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/http/RestTest.java @@ -26,9 +26,18 @@ */ package org.alfresco.transform.base.http; -import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers; -import org.alfresco.transform.base.fakes.FakeTransformerPdf2Png; -import org.alfresco.transform.base.fakes.FakeTransformerTxT2Pdf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.http.HttpMethod.POST; +import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; + +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; +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 org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -39,33 +48,27 @@ import org.springframework.http.ResponseEntity; import org.springframework.test.context.ContextConfiguration; import org.springframework.util.LinkedMultiValueMap; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; -import static org.alfresco.transform.common.RequestParamMap.DIRECT_ACCESS_URL; -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.assertTrue; -import static org.springframework.http.HttpMethod.POST; -import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; +import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers; +import org.alfresco.transform.base.fakes.FakeTransformerPdf2Png; +import org.alfresco.transform.base.fakes.FakeTransformerTxT2Pdf; /** * Very basic requests to the TransformController using http. */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - classes={org.alfresco.transform.base.Application.class}) + classes = {org.alfresco.transform.base.Application.class}) @ContextConfiguration(classes = { - FakeTransformEngineWithTwoCustomTransformers.class, - FakeTransformerTxT2Pdf.class, - FakeTransformerPdf2Png.class}) + FakeTransformEngineWithTwoCustomTransformers.class, + FakeTransformerTxT2Pdf.class, + FakeTransformerPdf2Png.class}) public class RestTest { @Autowired private TestRestTemplate restTemplate; private static final HttpHeaders HEADERS = new HttpHeaders(); - static { + static + { HEADERS.setContentType(MULTIPART_FORM_DATA); } @@ -92,7 +95,7 @@ public class RestTest parameters.add("file", new org.springframework.core.io.ClassPathResource("original.txt")); ResponseEntity response = restTemplate.exchange(ENDPOINT_TRANSFORM, POST, - new HttpEntity<>(parameters, HEADERS), String.class, ""); + new HttpEntity<>(parameters, HEADERS), String.class, ""); assertTrue(response.getBody().contains("Direct Access Url not found.")); } @@ -106,7 +109,7 @@ public class RestTest parameters.add("file", new org.springframework.core.io.ClassPathResource("original.txt")); ResponseEntity response = restTemplate.exchange(ENDPOINT_TRANSFORM, POST, - new HttpEntity<>(parameters, HEADERS), String.class, ""); + new HttpEntity<>(parameters, HEADERS), String.class, ""); assertEquals("Original Text -> TxT2Pdf()", response.getBody()); } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/messaging/AbstractQueueIT.java b/engines/base/src/test/java/org/alfresco/transform/base/messaging/AbstractQueueIT.java index 51e8d025..9fc1192c 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/messaging/AbstractQueueIT.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/messaging/AbstractQueueIT.java @@ -30,25 +30,23 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import jakarta.jms.Queue; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; import org.apache.activemq.command.ActiveMQQueue; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jms.core.JmsTemplate; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; + /** - * Checks that a t-engine can respond to its message queue. This is really just checking that - * ${queue.engineRequestQueue} has been configured. The transform request can (and does fail - * because the shared file store does not exist). + * Checks that a t-engine can respond to its message queue. This is really just checking that ${queue.engineRequestQueue} has been configured. The transform request can (and does fail because the shared file store does not exist). * - * @author Lucian Tuca - * created on 15/01/2019 + * @author Lucian Tuca created on 15/01/2019 */ -@SpringBootTest(classes={org.alfresco.transform.base.Application.class}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - properties = {"activemq.url=nio://localhost:61616"}) +@SpringBootTest(classes = {org.alfresco.transform.base.Application.class}, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = {"activemq.url=nio://localhost:61616"}) public abstract class AbstractQueueIT { @Autowired diff --git a/engines/base/src/test/java/org/alfresco/transform/base/messaging/QueueTransformServiceTest.java b/engines/base/src/test/java/org/alfresco/transform/base/messaging/QueueTransformServiceTest.java index d8d55a44..33779b2e 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/messaging/QueueTransformServiceTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/messaging/QueueTransformServiceTest.java @@ -27,21 +27,6 @@ package org.alfresco.transform.base.messaging; -import org.alfresco.transform.base.TransformController; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.apache.activemq.command.ActiveMQObjectMessage; -import org.apache.activemq.command.ActiveMQQueue; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.jms.support.converter.MessageConversionException; - -import jakarta.jms.Destination; -import jakarta.jms.JMSException; -import jakarta.jms.Message; - import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; @@ -52,7 +37,23 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.CREATED; import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; -@SpringBootTest(classes={org.alfresco.transform.base.Application.class}) +import jakarta.jms.Destination; +import jakarta.jms.JMSException; +import jakarta.jms.Message; + +import org.apache.activemq.command.ActiveMQObjectMessage; +import org.apache.activemq.command.ActiveMQQueue; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jms.support.converter.MessageConversionException; + +import org.alfresco.transform.base.TransformController; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; + +@SpringBootTest(classes = {org.alfresco.transform.base.Application.class}) public class QueueTransformServiceTest { @Mock @@ -94,12 +95,12 @@ public class QueueTransformServiceTest msg.setJMSReplyTo(destination); TransformReply reply = TransformReply - .builder() - .withStatus(INTERNAL_SERVER_ERROR.value()) - .withErrorDetails( - "JMS exception during T-Request deserialization of message with correlationID " - + msg.getCorrelationId() + ": null") - .build(); + .builder() + .withStatus(INTERNAL_SERVER_ERROR.value()) + .withErrorDetails( + "JMS exception during T-Request deserialization of message with correlationID " + + msg.getCorrelationId() + ": null") + .build(); doReturn(null).when(transformMessageConverter).fromMessage(msg); @@ -113,7 +114,7 @@ public class QueueTransformServiceTest @Test public void testConvertMessageThrowsMessageConversionExceptionThenReplyWithBadRequest() - throws JMSException + throws JMSException { ActiveMQObjectMessage msg = new ActiveMQObjectMessage(); msg.setCorrelationId("1234"); @@ -121,12 +122,12 @@ public class QueueTransformServiceTest msg.setJMSReplyTo(destination); TransformReply reply = TransformReply - .builder() - .withStatus(BAD_REQUEST.value()) - .withErrorDetails( - "Message conversion exception during T-Request deserialization of message with correlationID" - + msg.getCorrelationId() + ": null") - .build(); + .builder() + .withStatus(BAD_REQUEST.value()) + .withErrorDetails( + "Message conversion exception during T-Request deserialization of message with correlationID" + + msg.getCorrelationId() + ": null") + .build(); doThrow(MessageConversionException.class).when(transformMessageConverter).fromMessage(msg); @@ -140,7 +141,7 @@ public class QueueTransformServiceTest @Test public void testConvertMessageThrowsJMSExceptionThenReplyWithInternalServerError() - throws JMSException + throws JMSException { ActiveMQObjectMessage msg = new ActiveMQObjectMessage(); msg.setCorrelationId("1234"); @@ -148,12 +149,12 @@ public class QueueTransformServiceTest msg.setJMSReplyTo(destination); TransformReply reply = TransformReply - .builder() - .withStatus(INTERNAL_SERVER_ERROR.value()) - .withErrorDetails( - "JMSException during T-Request deserialization of message with correlationID " + - msg.getCorrelationId() + ": null") - .build(); + .builder() + .withStatus(INTERNAL_SERVER_ERROR.value()) + .withErrorDetails( + "JMSException during T-Request deserialization of message with correlationID " + + msg.getCorrelationId() + ": null") + .build(); doThrow(JMSException.class).when(transformMessageConverter).fromMessage(msg); @@ -174,13 +175,16 @@ public class QueueTransformServiceTest TransformRequest request = new TransformRequest(); TransformReply reply = TransformReply - .builder() - .withStatus(CREATED.value()) - .build(); + .builder() + .withStatus(CREATED.value()) + .build(); doReturn(request).when(transformMessageConverter).fromMessage(msg); - doAnswer(invocation -> {transformReplySender.send(destination, reply); return null;}) - .when(transformController).transform(request, null, destination); + doAnswer(invocation -> { + transformReplySender.send(destination, reply); + return null; + }) + .when(transformController).transform(request, null, destination); queueTransformService.receive(msg); @@ -205,7 +209,7 @@ public class QueueTransformServiceTest @Test public void testWhenExceptionOnCorrelationIdIsThrownThenContinueFlowWithNullCorrelationId() - throws JMSException + throws JMSException { Message msg = mock(Message.class); Destination destination = mock(Destination.class); @@ -215,13 +219,16 @@ public class QueueTransformServiceTest TransformRequest request = new TransformRequest(); TransformReply reply = TransformReply - .builder() - .withStatus(CREATED.value()) - .build(); + .builder() + .withStatus(CREATED.value()) + .build(); doReturn(request).when(transformMessageConverter).fromMessage(msg); - doAnswer(invocation -> {transformReplySender.send(destination, reply); return null;}) - .when(transformController).transform(request, null, destination); + doAnswer(invocation -> { + transformReplySender.send(destination, reply); + return null; + }) + .when(transformController).transform(request, null, destination); queueTransformService.receive(msg); diff --git a/engines/base/src/test/java/org/alfresco/transform/base/metadata/AbstractMetadataExtractsIT.java b/engines/base/src/test/java/org/alfresco/transform/base/metadata/AbstractMetadataExtractsIT.java index 819cc23c..bd657081 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/metadata/AbstractMetadataExtractsIT.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/metadata/AbstractMetadataExtractsIT.java @@ -27,12 +27,14 @@ package org.alfresco.transform.base.metadata; import static java.text.MessageFormat.format; -import static org.alfresco.transform.base.clients.HttpClient.sendTRequest; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_METADATA_EXTRACT; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.springframework.http.HttpStatus.OK; +import static org.alfresco.transform.base.clients.HttpClient.sendTRequest; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_METADATA_EXTRACT; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -42,20 +44,20 @@ import java.util.Map; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; - -import org.alfresco.transform.base.clients.FileInfo; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; +import org.alfresco.transform.base.clients.FileInfo; + /** * Super class of metadata integration tests. Sub classes should provide the following: *

*

    - *
  • A method providing a - * Stream of test files: {@code public static Stream engineTransformations()};
  • - *
  • Provide expected json files (<sourceFilename>"_metadata.json") as resources on the classpath.
  • - *
  • Override the method {@code testTransformation(FileInfo testFileInfo)} such that it calls - * the super method as a {@code @ParameterizedTest} for example:
+ *
  • A method providing a Stream of test files: {@code public static Stream engineTransformations()};
  • + *
  • Provide expected json files (<sourceFilename>"_metadata.json") as resources on the classpath.
  • + *
  • Override the method {@code testTransformation(FileInfo testFileInfo)} such that it calls the super method as a {@code @ParameterizedTest} for example:
  • + * + * *
      * @ParameterizedTest
      * 
    @@ -68,7 +70,7 @@ import org.springframework.http.ResponseEntity;
      * { 
      *      super.testTransformation(FileInfo testFileInfo)
      * }
    - * 
    + * * * @author adavis * @author dedwards @@ -82,7 +84,6 @@ public abstract class AbstractMetadataExtractsIT private final ObjectMapper jsonObjectMapper = new ObjectMapper(); - public void testTransformation(FileInfo fileInfo) { final String sourceMimetype = fileInfo.getMimeType(); @@ -104,7 +105,7 @@ public abstract class AbstractMetadataExtractsIT Map expectedMetadata = readExpectedMetadata(metadataFilename, actualMetadataFile); assertEquals(expectedMetadata, actualMetadata, - sourceFile+": The metadata did not match the expected value. It has been saved in "+actualMetadataFile.getAbsolutePath()); + sourceFile + ": The metadata did not match the expected value. It has been saved in " + actualMetadataFile.getAbsolutePath()); actualMetadataFile.delete(); } catch (Exception e) @@ -120,8 +121,8 @@ public abstract class AbstractMetadataExtractsIT { if (inputStream == null) { - fail("The expected metadata file "+filename+" did not exist.\n"+ - "The actual metadata has been saved in "+actualMetadataFile.getAbsoluteFile()); + fail("The expected metadata file " + filename + " did not exist.\n" + + "The actual metadata has been saved in " + actualMetadataFile.getAbsoluteFile()); } return readMetadata(inputStream); } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/registry/TransformRegistryRefreshTest.java b/engines/base/src/test/java/org/alfresco/transform/base/registry/TransformRegistryRefreshTest.java index 12094e1e..e528959a 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/registry/TransformRegistryRefreshTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/registry/TransformRegistryRefreshTest.java @@ -7,10 +7,12 @@ */ package org.alfresco.transform.base.registry; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers; -import org.alfresco.transform.base.fakes.FakeTransformerPdf2Png; -import org.alfresco.transform.base.fakes.FakeTransformerTxT2Pdf; import org.awaitility.Awaitility; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -21,19 +23,16 @@ import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.util.ReflectionTestUtils; -import java.time.Duration; -import java.util.concurrent.TimeUnit; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.verify; +import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers; +import org.alfresco.transform.base.fakes.FakeTransformerPdf2Png; +import org.alfresco.transform.base.fakes.FakeTransformerTxT2Pdf; @AutoConfigureMockMvc -@SpringBootTest(classes={org.alfresco.transform.base.Application.class}, properties={"transform.engine.config.cron=*/1 * * * * *"}) +@SpringBootTest(classes = {org.alfresco.transform.base.Application.class}, properties = {"transform.engine.config.cron=*/1 * * * * *"}) @ContextConfiguration(classes = { - FakeTransformEngineWithTwoCustomTransformers.class, - FakeTransformerTxT2Pdf.class, - FakeTransformerPdf2Png.class}) + FakeTransformEngineWithTwoCustomTransformers.class, + FakeTransformerTxT2Pdf.class, + FakeTransformerPdf2Png.class}) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) public class TransformRegistryRefreshTest { @@ -53,11 +52,11 @@ public class TransformRegistryRefreshTest // As we can't change the content of a classpath resource, lets change what is read. ReflectionTestUtils.setField(transformConfigFiles, "files", ImmutableMap.of( - "a", "config/addA2B.json", - "foo", "config/addB2C.json")); + "a", "config/addA2B.json", + "foo", "config/addB2C.json")); transformConfigFromFiles.initFileConfig(); - Awaitility.await().pollDelay(3, TimeUnit.SECONDS).until( () -> { // i.e. Thread.sleep(3_000) - but keeps sona happy + Awaitility.await().pollDelay(3, TimeUnit.SECONDS).until(() -> { // i.e. Thread.sleep(3_000) - but keeps sona happy transformRegistry.retrieveConfig(); assertEquals(6, transformRegistry.getTransformConfig().getTransformers().size()); return true; @@ -67,8 +66,8 @@ public class TransformRegistryRefreshTest private void waitForRegistryReady() throws InterruptedException { Awaitility.await().atMost(1, TimeUnit.SECONDS) - .pollInterval(100, TimeUnit.MILLISECONDS) - .pollDelay(Duration.ZERO) - .until(() -> transformRegistry.isReadyForTransformRequests()); + .pollInterval(100, TimeUnit.MILLISECONDS) + .pollDelay(Duration.ZERO) + .until(() -> transformRegistry.isReadyForTransformRequests()); } } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/registry/TransformRegistryTest.java b/engines/base/src/test/java/org/alfresco/transform/base/registry/TransformRegistryTest.java index 2de11a64..787d1090 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/registry/TransformRegistryTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/registry/TransformRegistryTest.java @@ -26,16 +26,21 @@ */ package org.alfresco.transform.base.registry; +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.alfresco.transform.common.Mimetype.MIMETYPE_EXCEL; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import org.alfresco.transform.base.fakes.AbstractFakeTransformEngine; -import org.alfresco.transform.base.fakes.FakeTransformEngineWithAllInOne; -import org.alfresco.transform.base.fakes.FakeTransformEngineWithOneCustomTransformer; -import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers; -import org.alfresco.transform.config.SupportedSourceAndTarget; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.config.Transformer; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -43,20 +48,16 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.util.ReflectionTestUtils; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -import static org.alfresco.transform.common.Mimetype.MIMETYPE_EXCEL; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; -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.mockito.Mockito.doReturn; +import org.alfresco.transform.base.fakes.AbstractFakeTransformEngine; +import org.alfresco.transform.base.fakes.FakeTransformEngineWithAllInOne; +import org.alfresco.transform.base.fakes.FakeTransformEngineWithOneCustomTransformer; +import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers; +import org.alfresco.transform.config.SupportedSourceAndTarget; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.Transformer; @AutoConfigureMockMvc -@SpringBootTest(classes={org.alfresco.transform.base.Application.class}) +@SpringBootTest(classes = {org.alfresco.transform.base.Application.class}) public class TransformRegistryTest { @Autowired @@ -86,9 +87,9 @@ public class TransformRegistryTest private String getTransformerNames(TransformConfig transformConfig) { return transformConfig.getTransformers().stream() - .map(Transformer::getTransformerName) - .sorted() - .collect(Collectors.joining(", ")); + .map(Transformer::getTransformerName) + .sorted() + .collect(Collectors.joining(", ")); } @Test @@ -101,7 +102,7 @@ public class TransformRegistryTest public void singleTransformEngine() { ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new FakeTransformEngineWithOneCustomTransformer())); + new FakeTransformEngineWithOneCustomTransformer())); transformConfigFromTransformEngines.initTransformEngineConfig(); transformRegistry.retrieveConfig(); @@ -112,34 +113,34 @@ public class TransformRegistryTest public void multipleTransformEngines() { ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new FakeTransformEngineWithAllInOne(), - new FakeTransformEngineWithOneCustomTransformer(), - new FakeTransformEngineWithTwoCustomTransformers())); + new FakeTransformEngineWithAllInOne(), + new FakeTransformEngineWithOneCustomTransformer(), + new FakeTransformEngineWithTwoCustomTransformers())); transformConfigFromTransformEngines.initTransformEngineConfig(); transformRegistry.retrieveConfig(); assertEquals("Pdf2Jpg, Pdf2Png, TxT2Pdf, Txt2JpgViaPdf, Txt2PngViaPdf", - getTransformerNames(transformRegistry.getTransformConfig())); + getTransformerNames(transformRegistry.getTransformConfig())); } @Test public void uncombinedConfigFromEngine() { ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new FakeTransformEngineWithAllInOne(), - new FakeTransformEngineWithTwoCustomTransformers())); + new FakeTransformEngineWithAllInOne(), + new FakeTransformEngineWithTwoCustomTransformers())); transformConfigFromTransformEngines.initTransformEngineConfig(); transformRegistry.retrieveConfig(); assertEquals("Pdf2Png, TxT2Pdf, Txt2JpgViaPdf, Txt2PngViaPdf", - getTransformerNames(transformRegistry.getTransformConfig())); + getTransformerNames(transformRegistry.getTransformConfig())); ReflectionTestUtils.setField(transformRegistry, "isTRouter", true); transformConfigFromTransformEngines.initTransformEngineConfig(); transformRegistry.retrieveConfig(); assertEquals("Pdf2Png, TxT2Pdf, Txt2PngViaPdf", - getTransformerNames(transformRegistry.getTransformConfig())); + getTransformerNames(transformRegistry.getTransformConfig())); } @Test @@ -147,23 +148,23 @@ public class TransformRegistryTest { ReflectionTestUtils.setField(transformRegistry, "isTRouter", true); ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new FakeTransformEngineWithAllInOne(), - new FakeTransformEngineWithTwoCustomTransformers())); + new FakeTransformEngineWithAllInOne(), + new FakeTransformEngineWithTwoCustomTransformers())); transformConfigFromTransformEngines.initTransformEngineConfig(); transformRegistry.retrieveConfig(); assertEquals("Pdf2Png, TxT2Pdf, Txt2PngViaPdf", - getTransformerNames(transformRegistry.getTransformConfig())); + getTransformerNames(transformRegistry.getTransformConfig())); } @Test public void singleTransformEngineWithAdditionalConfig() { ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new FakeTransformEngineWithOneCustomTransformer())); + new FakeTransformEngineWithOneCustomTransformer())); ReflectionTestUtils.setField(transformConfigFiles, "files", ImmutableMap.of( - "a", "config/addA2B.json", - "foo", "config/addB2C.json")); + "a", "config/addA2B.json", + "foo", "config/addB2C.json")); transformConfigFromTransformEngines.initTransformEngineConfig(); transformConfigFromFiles.initFileConfig(); @@ -176,10 +177,10 @@ public class TransformRegistryTest public void singleTransformEngineWithHistoricAdditionalRoutes() { ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new FakeTransformEngineWithOneCustomTransformer())); + new FakeTransformEngineWithOneCustomTransformer())); ReflectionTestUtils.setField(transformConfigFilesHistoric, "additional", ImmutableMap.of( - "a", "config/addA2B.json", - "foo", "config/addB2C.json")); + "a", "config/addA2B.json", + "foo", "config/addB2C.json")); transformConfigFromTransformEngines.initTransformEngineConfig(); transformConfigFromFiles.initFileConfig(); @@ -192,11 +193,11 @@ public class TransformRegistryTest public void singleTransformEngineWithHistoricTransformerRoutesExternalFile() { ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new FakeTransformEngineWithOneCustomTransformer())); + new FakeTransformEngineWithOneCustomTransformer())); ReflectionTestUtils.setField(transformConfigFilesHistoric, "TRANSFORMER_ROUTES_FROM_CLASSPATH", - "config/removePdf2JpgAndAddA2Z.json"); // checking it is ignored + "config/removePdf2JpgAndAddA2Z.json"); // checking it is ignored ReflectionTestUtils.setField(transformConfigFilesHistoric, "transformerRoutesExternalFile", - "config/addA2B.json"); + "config/addA2B.json"); transformConfigFromTransformEngines.initTransformEngineConfig(); transformConfigFromFiles.initFileConfig(); @@ -209,9 +210,9 @@ public class TransformRegistryTest public void singleTransformEngineWithHistoricTransformerRoutesOnClasspath() { ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new FakeTransformEngineWithOneCustomTransformer())); + new FakeTransformEngineWithOneCustomTransformer())); ReflectionTestUtils.setField(transformConfigFilesHistoric, "TRANSFORMER_ROUTES_FROM_CLASSPATH", - "config/removePdf2JpgAndAddA2Z.json"); + "config/removePdf2JpgAndAddA2Z.json"); transformConfigFromTransformEngines.initTransformEngineConfig(); transformConfigFromFiles.initFileConfig(); @@ -228,7 +229,7 @@ public class TransformRegistryTest assertFalse(transformRegistry.isReadyForTransformRequests()); ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new FakeTransformEngineWithOneCustomTransformer())); + new FakeTransformEngineWithOneCustomTransformer())); transformConfigFromTransformEngines.initTransformEngineConfig(); transformRegistry.retrieveConfig(); @@ -239,33 +240,33 @@ public class TransformRegistryTest public void testCheckSourceSize() { ReflectionTestUtils.setField(transformConfigFromTransformEngines, "transformEngines", ImmutableList.of( - new AbstractFakeTransformEngine() - { - @Override public TransformConfig getTransformConfig() - { - return TransformConfig.builder() - .withTransformers(ImmutableList.of( - Transformer.builder() - .withTransformerName("transformerName") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType(MIMETYPE_WORD) - .withTargetMediaType(MIMETYPE_PDF) - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType(MIMETYPE_EXCEL) - .withTargetMediaType(MIMETYPE_PDF) - .withMaxSourceSizeBytes(12345L) - .build())) - .build())) - .build(); - } - })); + new AbstractFakeTransformEngine() { + @Override + public TransformConfig getTransformConfig() + { + return TransformConfig.builder() + .withTransformers(ImmutableList.of( + Transformer.builder() + .withTransformerName("transformerName") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType(MIMETYPE_WORD) + .withTargetMediaType(MIMETYPE_PDF) + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType(MIMETYPE_EXCEL) + .withTargetMediaType(MIMETYPE_PDF) + .withMaxSourceSizeBytes(12345L) + .build())) + .build())) + .build(); + } + })); transformConfigFromTransformEngines.initTransformEngineConfig(); transformRegistry.retrieveConfig(); - assertTrue( transformRegistry.checkSourceSize("transformerName", MIMETYPE_WORD, Long.MAX_VALUE, MIMETYPE_PDF)); - assertTrue( transformRegistry.checkSourceSize("transformerName", MIMETYPE_EXCEL, 12345L, MIMETYPE_PDF)); + assertTrue(transformRegistry.checkSourceSize("transformerName", MIMETYPE_WORD, Long.MAX_VALUE, MIMETYPE_PDF)); + assertTrue(transformRegistry.checkSourceSize("transformerName", MIMETYPE_EXCEL, 12345L, MIMETYPE_PDF)); assertFalse(transformRegistry.checkSourceSize("transformerName", MIMETYPE_EXCEL, 12346L, MIMETYPE_PDF)); assertFalse(transformRegistry.checkSourceSize("transformerName", "doesNotExist", 12345L, MIMETYPE_PDF)); assertFalse(transformRegistry.checkSourceSize("doesNotExist", MIMETYPE_WORD, 12345L, MIMETYPE_PDF)); diff --git a/engines/base/src/test/java/org/alfresco/transform/base/transform/FragmentHandlerTest.java b/engines/base/src/test/java/org/alfresco/transform/base/transform/FragmentHandlerTest.java index 4dc74dd6..16945494 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/transform/FragmentHandlerTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/transform/FragmentHandlerTest.java @@ -26,16 +26,28 @@ */ package org.alfresco.transform.base.transform; +import static org.awaitility.Awaitility.await; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.when; +import static org.springframework.http.HttpStatus.OK; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import static org.alfresco.transform.base.transform.StreamHandlerTest.read; +import static org.alfresco.transform.common.Mimetype.*; +import static org.alfresco.transform.common.RequestParamMap.*; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import jakarta.jms.Destination; + import com.google.common.collect.ImmutableList; -import org.alfresco.transform.base.fakes.FakeTransformEngineWithFragments; -import org.alfresco.transform.base.fakes.FakeTransformerFragments; -import org.alfresco.transform.base.messaging.TransformReplySender; -import org.alfresco.transform.base.model.FileRefEntity; -import org.alfresco.transform.base.model.FileRefResponse; -import org.alfresco.transform.base.probes.ProbeTransform; -import org.alfresco.transform.base.sfs.SharedFileStoreClient; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -51,31 +63,21 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import jakarta.jms.Destination; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.TimeUnit; - -import static org.alfresco.transform.base.transform.StreamHandlerTest.read; -import static org.alfresco.transform.common.Mimetype.*; -import static org.alfresco.transform.common.RequestParamMap.*; -import static org.awaitility.Awaitility.await; -import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.when; -import static org.springframework.http.HttpStatus.OK; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import org.alfresco.transform.base.fakes.FakeTransformEngineWithFragments; +import org.alfresco.transform.base.fakes.FakeTransformerFragments; +import org.alfresco.transform.base.messaging.TransformReplySender; +import org.alfresco.transform.base.model.FileRefEntity; +import org.alfresco.transform.base.model.FileRefResponse; +import org.alfresco.transform.base.probes.ProbeTransform; +import org.alfresco.transform.base.sfs.SharedFileStoreClient; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; @AutoConfigureMockMvc -@SpringBootTest(classes={org.alfresco.transform.base.Application.class}) +@SpringBootTest(classes = {org.alfresco.transform.base.Application.class}) @ContextConfiguration(classes = { - FakeTransformEngineWithFragments.class, - FakeTransformerFragments.class}) + FakeTransformEngineWithFragments.class, + FakeTransformerFragments.class}) public class FragmentHandlerTest { @Autowired @@ -99,34 +101,32 @@ public class FragmentHandlerTest String targetReference = UUID.randomUUID().toString(); when(fakeSfsClient.retrieveFile(any())) - .thenReturn(new ResponseEntity<>(new ByteArrayResource(sourceText.getBytes(StandardCharsets.UTF_8)), - new HttpHeaders(), OK)); + .thenReturn(new ResponseEntity<>(new ByteArrayResource(sourceText.getBytes(StandardCharsets.UTF_8)), + new HttpHeaders(), OK)); when(fakeSfsClient.saveFile(any())) - .thenAnswer(invocation -> - { - lines.add(read(invocation.getArgument(0))); - return new FileRefResponse(new FileRefEntity(targetReference)); - }); + .thenAnswer(invocation -> { + lines.add(read(invocation.getArgument(0))); + return new FileRefResponse(new FileRefEntity(targetReference)); + }); - doAnswer(invocation -> - { + doAnswer(invocation -> { replies.add(Pair.of(invocation.getArgument(0), invocation.getArgument(1))); return null; }).when(transformReplySender).send(any(), any()); TransformRequest request = TransformRequest - .builder() - .withRequestId(UUID.randomUUID().toString()) - .withSourceMediaType(MIMETYPE_PDF) - .withTargetMediaType(MIMETYPE_IMAGE_JPEG) - .withTargetExtension("jpeg") - .withSchema(1) - .withClientData("ACS") - .withSourceReference(sourceReference) - .withSourceSize(32L) - .withInternalContextForTransformEngineTests() - .build(); + .builder() + .withRequestId(UUID.randomUUID().toString()) + .withSourceMediaType(MIMETYPE_PDF) + .withTargetMediaType(MIMETYPE_IMAGE_JPEG) + .withTargetExtension("jpeg") + .withSchema(1) + .withClientData("ACS") + .withSourceReference(sourceReference) + .withSourceSize(32L) + .withInternalContextForTransformEngineTests() + .build(); transformHandler.handleMessageRequest(request, Long.MAX_VALUE, null, probeTransform); TransformReply lastReply = replies.get(replies.size() - 1).getRight(); @@ -139,25 +139,26 @@ public class FragmentHandlerTest } else { - assertEquals("Transform failed - "+expectedError, errorDetails); + assertEquals("Transform failed - " + expectedError, errorDetails); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), status); } assertEquals(expectedLines, lines); } @Test - public void testErrorIfHttp() { + public void testErrorIfHttp() + { String expectedError = "Fragments may only be sent via message queues. This an http request"; await() - .atMost(10, TimeUnit.SECONDS) - .untilAsserted(() -> mockMvc.perform( - MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM) - .file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN, - "Start".getBytes(StandardCharsets.UTF_8))) - .param(SOURCE_MIMETYPE, MIMETYPE_PDF) - .param(TARGET_MIMETYPE, MIMETYPE_IMAGE_JPEG)) - .andExpect(status().isInternalServerError()) - .andExpect(status().reason(containsString(expectedError)))); + .atMost(10, TimeUnit.SECONDS) + .untilAsserted(() -> mockMvc.perform( + MockMvcRequestBuilders.multipart(ENDPOINT_TRANSFORM) + .file(new MockMultipartFile("file", null, MIMETYPE_TEXT_PLAIN, + "Start".getBytes(StandardCharsets.UTF_8))) + .param(SOURCE_MIMETYPE, MIMETYPE_PDF) + .param(TARGET_MIMETYPE, MIMETYPE_IMAGE_JPEG)) + .andExpect(status().isInternalServerError()) + .andExpect(status().reason(containsString(expectedError)))); } @Test @@ -176,21 +177,21 @@ public class FragmentHandlerTest public void testMultipleFragmentCallsWithFinished() { assertFragments("line1\nline2\nFinished", null, - ImmutableList.of("line1", "line2", "Finished")); + ImmutableList.of("line1", "line2", "Finished")); } @Test public void testMultipleFragmentsCallsWithoutFinish() { assertFragments("line1\nline2\nline3", null, - ImmutableList.of("line1", "line2", "line3")); + ImmutableList.of("line1", "line2", "line3")); } @Test public void testMultipleFragmentsCallsWithoutSendingLastFragment() { assertFragments("line1\nline2\nline3\nIgnored", null, - ImmutableList.of("line1", "line2", "line3")); + ImmutableList.of("line1", "line2", "line3")); } @@ -204,14 +205,14 @@ public class FragmentHandlerTest public void testEndTooEarlyUsingFinished() { assertFragments("line1\nFinished\nline3", "Final fragment already sent", - ImmutableList.of("line1", "Finished")); + ImmutableList.of("line1", "Finished")); } @Test public void testEndTooEarlyUsingNull() { assertFragments("line1\nNull\nline3", "Final fragment already sent", - ImmutableList.of("line1")); + ImmutableList.of("line1")); } @Test @@ -232,6 +233,6 @@ public class FragmentHandlerTest public void testNullAndFinished() { assertFragments("line1\nNull\nFinished", "Final fragment already sent", - ImmutableList.of("line1")); + ImmutableList.of("line1")); } } diff --git a/engines/example/src/main/java/org/alfresco/transform/example/HelloTransformEngine.java b/engines/example/src/main/java/org/alfresco/transform/example/HelloTransformEngine.java index aa9c36cc..8c05f03a 100644 --- a/engines/example/src/main/java/org/alfresco/transform/example/HelloTransformEngine.java +++ b/engines/example/src/main/java/org/alfresco/transform/example/HelloTransformEngine.java @@ -27,13 +27,14 @@ package org.alfresco.transform.example; 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 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 HelloTransformEngine implements TransformEngine { @@ -49,7 +50,7 @@ public class HelloTransformEngine implements TransformEngine @Override public String getStartupMessage() { - return "Startup "+getTransformEngineName()+"\nNo 3rd party licenses"; + return "Startup " + getTransformEngineName() + "\nNo 3rd party licenses"; } @Override @@ -62,7 +63,7 @@ public class HelloTransformEngine implements TransformEngine public ProbeTransform getProbeTransform() { return new ProbeTransform("probe.txt", "text/plain", "text/plain", - ImmutableMap.of("sourceEncoding", "UTF-8", "language", "English"), - 11, 10, 150, 1024, 1, 60 * 2); + ImmutableMap.of("sourceEncoding", "UTF-8", "language", "English"), + 11, 10, 150, 1024, 1, 60 * 2); } -} \ No newline at end of file +} diff --git a/engines/example/src/main/java/org/alfresco/transform/example/HelloTransformer.java b/engines/example/src/main/java/org/alfresco/transform/example/HelloTransformer.java index 7bfb7e09..d9f24c31 100644 --- a/engines/example/src/main/java/org/alfresco/transform/example/HelloTransformer.java +++ b/engines/example/src/main/java/org/alfresco/transform/example/HelloTransformer.java @@ -26,14 +26,15 @@ */ package org.alfresco.transform.example; -import org.alfresco.transform.base.CustomTransformer; -import org.alfresco.transform.base.TransformManager; -import org.springframework.stereotype.Component; - import java.io.InputStream; import java.io.OutputStream; import java.util.Map; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.CustomTransformer; +import org.alfresco.transform.base.TransformManager; + @Component public class HelloTransformer implements CustomTransformer { @@ -58,4 +59,4 @@ public class HelloTransformer implements CustomTransformer { return "Hello %s"; } -} \ No newline at end of file +} diff --git a/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/ImageMagickOptionsBuilder.java b/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/ImageMagickOptionsBuilder.java index 526d812f..0c08a3a7 100644 --- a/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/ImageMagickOptionsBuilder.java +++ b/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/ImageMagickOptionsBuilder.java @@ -26,15 +26,17 @@ */ package org.alfresco.transform.imagemagick; -import com.google.common.collect.ImmutableList; -import org.alfresco.transform.exceptions.TransformException; +import static org.springframework.http.HttpStatus.BAD_REQUEST; + +import static org.alfresco.transform.base.util.Util.stringToBoolean; +import static org.alfresco.transform.base.util.Util.stringToInteger; import java.util.List; import java.util.StringJoiner; -import static org.alfresco.transform.base.util.Util.stringToBoolean; -import static org.alfresco.transform.base.util.Util.stringToInteger; -import static org.springframework.http.HttpStatus.BAD_REQUEST; +import com.google.common.collect.ImmutableList; + +import org.alfresco.transform.exceptions.TransformException; /** * ImageMagick options builder. @@ -44,7 +46,7 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST; public final class ImageMagickOptionsBuilder { private static final List GRAVITY_VALUES = ImmutableList.of("North", "NorthEast", - "East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center"); + "East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center"); private Integer startPage; private Integer endPage; @@ -64,7 +66,8 @@ public final class ImageMagickOptionsBuilder private Boolean maintainAspectRatio; private String commandOptions; - private ImageMagickOptionsBuilder() {} + private ImageMagickOptionsBuilder() + {} public ImageMagickOptionsBuilder withStartPage(final String startPage) { @@ -270,7 +273,7 @@ public final class ImageMagickOptionsBuilder } if (cropGravity != null || cropWidth != null || cropHeight != null || cropPercentage != null || - cropXOffset != null || cropYOffset != null) + cropXOffset != null || cropYOffset != null) { if (cropGravity != null) { @@ -349,8 +352,8 @@ public final class ImageMagickOptionsBuilder } return (commandOptions == null || "".equals( - commandOptions.trim()) ? "" : commandOptions + ' ') + - args.toString(); + commandOptions.trim()) ? "" : commandOptions + ' ') + + args.toString(); } public static ImageMagickOptionsBuilder builder() diff --git a/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/ImageMagickTransformEngine.java b/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/ImageMagickTransformEngine.java index 3b4ed436..f091c051 100644 --- a/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/ImageMagickTransformEngine.java +++ b/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/ImageMagickTransformEngine.java @@ -26,19 +26,20 @@ */ package org.alfresco.transform.imagemagick; -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.Collections; - import static org.alfresco.transform.base.logging.StandardMessages.COMMUNITY_LICENCE; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG; +import java.util.Collections; + +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 ImageMagickTransformEngine implements TransformEngine { @@ -69,6 +70,6 @@ public class ImageMagickTransformEngine implements TransformEngine public ProbeTransform getProbeTransform() { return new ProbeTransform("probe.jpg", MIMETYPE_IMAGE_JPEG, MIMETYPE_IMAGE_PNG, Collections.emptyMap(), - 25383, 1024, 150, 1024, 60 * 15 + 1, 60 * 15); + 25383, 1024, 150, 1024, 60 * 15 + 1, 60 * 15); } } diff --git a/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/transformers/ImageMagickTransformer.java b/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/transformers/ImageMagickTransformer.java index ef13614f..9f777b45 100644 --- a/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/transformers/ImageMagickTransformer.java +++ b/engines/imagemagick/src/main/java/org/alfresco/transform/imagemagick/transformers/ImageMagickTransformer.java @@ -52,11 +52,15 @@ import static org.alfresco.transform.common.RequestParamMap.START_PAGE; import static org.alfresco.transform.common.RequestParamMap.THUMBNAIL; import static org.alfresco.transform.common.RequestParamMap.TIMEOUT; -import jakarta.annotation.PostConstruct; import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; +import jakarta.annotation.PostConstruct; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; import org.alfresco.transform.base.TransformManager; import org.alfresco.transform.base.executors.AbstractCommandExecutor; @@ -64,13 +68,9 @@ import org.alfresco.transform.base.executors.RuntimeExec; import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; import org.alfresco.transform.exceptions.TransformException; import org.alfresco.transform.imagemagick.ImageMagickOptionsBuilder; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; /** - * Converts image files into different types of images. Transformer supports multi-page images and allows to specify via parameters `startPage` and `endPage` range of pages that should be converted. - * In case of a one-page target image type (like `jpeg` or `png`) parameters `startPage` and `endPage` will be set to 0 by default - this means that only first page will be converted. + * Converts image files into different types of images. Transformer supports multi-page images and allows to specify via parameters `startPage` and `endPage` range of pages that should be converted. In case of a one-page target image type (like `jpeg` or `png`) parameters `startPage` and `endPage` will be set to 0 by default - this means that only first page will be converted. */ @Component public class ImageMagickTransformer extends AbstractCommandExecutor implements CustomTransformerFileAdaptor @@ -122,7 +122,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C RuntimeExec runtimeExec = new RuntimeExec(); Map commandsAndArguments = new HashMap<>(); commandsAndArguments.put(".*", - new String[]{exe, "${source}", "SPLIT:${options}", "-strip", "-quiet", "${target}"}); + new String[]{exe, "${source}", "SPLIT:${options}", "-strip", "-quiet", "${target}"}); runtimeExec.setCommandsAndArguments(commandsAndArguments); Map processProperties = new HashMap<>(); @@ -130,7 +130,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C processProperties.put("DYLD_FALLBACK_LIBRARY_PATH", dyn); processProperties.put("LD_LIBRARY_PATH", dyn); - //Optional properties (see also https://imagemagick.org/script/resources.php#environment) + // Optional properties (see also https://imagemagick.org/script/resources.php#environment) if (coders != null && !coders.isBlank()) { processProperties.put("MAGICK_CODER_MODULE_PATH", coders); @@ -146,7 +146,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C runtimeExec.setDefaultProperties(defaultProperties); runtimeExec.setErrorCodes( - "1,2,255,400,405,410,415,420,425,430,435,440,450,455,460,465,470,475,480,485,490,495,499,700,705,710,715,720,725,730,735,740,750,755,760,765,770,775,780,785,790,795,799"); + "1,2,255,400,405,410,415,420,425,430,435,440,450,455,460,465,470,475,480,485,490,495,499,700,705,710,715,720,725,730,735,740,750,755,760,765,770,775,780,785,790,795,799"); return runtimeExec; } @@ -163,7 +163,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C @Override public void transform(String sourceMimetype, String targetMimetype, Map transformOptions, - File sourceFile, File targetFile, TransformManager transformManager) throws TransformException + File sourceFile, File targetFile, TransformManager transformManager) throws TransformException { String startPageString = transformOptions.get(START_PAGE); String endPageString = transformOptions.get(END_PAGE); @@ -202,8 +202,7 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C String pageRange = calculatePageRange( stringToInteger(startPageString), - stringToInteger(endPageString) - ); + stringToInteger(endPageString)); Long timeout = stringToLong(transformOptions.get(TIMEOUT)); @@ -214,10 +213,10 @@ public class ImageMagickTransformer extends AbstractCommandExecutor implements C { return startPage == null ? endPage == null - ? "" - : "[" + endPage + ']' + ? "" + : "[" + endPage + ']' : endPage == null || startPage.equals(endPage) - ? "[" + startPage + ']' - : "[" + startPage + '-' + endPage + ']'; + ? "[" + startPage + ']' + : "[" + startPage + '-' + endPage + ']'; } } diff --git a/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickLivenessReadinessProbeIT.java b/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickLivenessReadinessProbeIT.java index e0a8b0f5..05c0b422 100644 --- a/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickLivenessReadinessProbeIT.java +++ b/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickLivenessReadinessProbeIT.java @@ -29,9 +29,11 @@ package org.alfresco.transform.imagemagick; import org.alfresco.transform.base.LivenessReadinessProbeTest; -public class ImageMagickLivenessReadinessProbeIT extends LivenessReadinessProbeTest { +public class ImageMagickLivenessReadinessProbeIT extends LivenessReadinessProbeTest +{ @Override - protected ImagesForTests getImageForTest() { + protected ImagesForTests getImageForTest() + { return new ImagesForTests("alfresco-imagemagick", "image/jpeg", "image/png", "quick.jpg"); } } diff --git a/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickQueueIT.java b/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickQueueIT.java index 5681aa8c..bd0b55c1 100644 --- a/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickQueueIT.java +++ b/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickQueueIT.java @@ -31,12 +31,11 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG; import java.util.UUID; -import org.alfresco.transform.client.model.TransformRequest; import org.alfresco.transform.base.messaging.AbstractQueueIT; +import org.alfresco.transform.client.model.TransformRequest; /** - * @author Lucian Tuca - * created on 15/01/2019 + * @author Lucian Tuca created on 15/01/2019 */ public class ImageMagickQueueIT extends AbstractQueueIT { @@ -44,16 +43,16 @@ public class ImageMagickQueueIT extends AbstractQueueIT protected TransformRequest buildRequest() { return TransformRequest - .builder() - .withRequestId(UUID.randomUUID().toString()) - .withSourceMediaType(MIMETYPE_IMAGE_PNG) - .withTargetMediaType(MIMETYPE_IMAGE_JPEG) - .withTargetExtension("jpeg") - .withSchema(1) - .withClientData("ACS") - .withSourceReference(UUID.randomUUID().toString()) - .withSourceSize(32L) - .withInternalContextForTransformEngineTests() - .build(); + .builder() + .withRequestId(UUID.randomUUID().toString()) + .withSourceMediaType(MIMETYPE_IMAGE_PNG) + .withTargetMediaType(MIMETYPE_IMAGE_JPEG) + .withTargetExtension("jpeg") + .withSchema(1) + .withClientData("ACS") + .withSourceReference(UUID.randomUUID().toString()) + .withSourceSize(32L) + .withInternalContextForTransformEngineTests() + .build(); } } diff --git a/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickTest.java b/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickTest.java index 5a1412cd..8ea2f12b 100644 --- a/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickTest.java +++ b/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickTest.java @@ -26,39 +26,6 @@ */ package org.alfresco.transform.imagemagick; -import org.alfresco.transform.base.AbstractBaseTest; -import org.alfresco.transform.base.executors.RuntimeExec; -import org.alfresco.transform.base.executors.RuntimeExec.ExecutionResult; -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.alfresco.transform.imagemagick.transformers.ImageMagickTransformer; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.Mock; -import org.mockito.stubbing.Answer; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -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.request.MockHttpServletRequestBuilder; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.util.Arrays; -import java.util.Map; -import java.util.UUID; - -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -79,6 +46,41 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.util.StringUtils.getFilenameExtension; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Map; +import java.util.UUID; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.Mock; +import org.mockito.stubbing.Answer; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +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.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.executors.RuntimeExec.ExecutionResult; +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.alfresco.transform.imagemagick.transformers.ImageMagickTransformer; + /** * Test ImageMagick with mocked external command. */ @@ -123,20 +125,20 @@ public class ImageMagickTest extends AbstractBaseTest protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params) { final MockHttpServletRequestBuilder builder = super.mockMvcRequest(url, sourceFile, params) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype); + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype); return builder; } @Override public void mockTransformCommand(String sourceExtension, - String targetExtension, String sourceMimetype, - boolean readTargetFileBytes) throws IOException + String targetExtension, String sourceMimetype, + boolean readTargetFileBytes) throws IOException { this.sourceExtension = sourceExtension; this.targetExtension = targetExtension; this.sourceMimetype = sourceMimetype; - this.targetMimetype = PREFIX_IMAGE + ("jpg".equals(targetExtension) ? "jpeg" : targetExtension); + this.targetMimetype = PREFIX_IMAGE + ("jpg".equals(targetExtension) ? "jpeg" : targetExtension); expectedOptions = null; expectedSourceSuffix = null; @@ -145,55 +147,55 @@ public class ImageMagickTest extends AbstractBaseTest sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, sourceFileBytes); when(mockTransformCommand.execute(any(), anyLong())).thenAnswer( - (Answer) invocation -> { - Map actualProperties = invocation.getArgument(0); - assertEquals(3, actualProperties.size(), "There should be 3 properties"); + (Answer) invocation -> { + Map actualProperties = invocation.getArgument(0); + assertEquals(3, actualProperties.size(), "There should be 3 properties"); - String actualOptions = actualProperties.get("options"); - String actualSource = actualProperties.get("source"); - String actualTarget = actualProperties.get("target"); - String actualTargetExtension = getFilenameExtension(actualTarget); + String actualOptions = actualProperties.get("options"); + String actualSource = actualProperties.get("source"); + String actualTarget = actualProperties.get("target"); + String actualTargetExtension = getFilenameExtension(actualTarget); - assertNotNull(actualSource); - assertNotNull(actualTarget); - if (expectedSourceSuffix != null) - { - assertTrue(actualSource.endsWith(expectedSourceSuffix), - "The source file \"" + actualSource + "\" should have ended in \"" + expectedSourceSuffix + "\""); - actualSource = actualSource.substring(0, actualSource.length() - expectedSourceSuffix.length()); - } + assertNotNull(actualSource); + assertNotNull(actualTarget); + if (expectedSourceSuffix != null) + { + assertTrue(actualSource.endsWith(expectedSourceSuffix), + "The source file \"" + actualSource + "\" should have ended in \"" + expectedSourceSuffix + "\""); + actualSource = actualSource.substring(0, actualSource.length() - expectedSourceSuffix.length()); + } - assertNotNull(actualOptions); - if (expectedOptions != null) - { - assertEquals(expectedOptions, actualOptions,"expectedOptions"); - } + assertNotNull(actualOptions); + if (expectedOptions != null) + { + assertEquals(expectedOptions, actualOptions, "expectedOptions"); + } - Long actualTimeout = invocation.getArgument(1); - assertNotNull(actualTimeout); - if (expectedTimeout != null) - { - assertEquals(expectedTimeout, actualTimeout,"expectedTimeout"); - } + Long actualTimeout = invocation.getArgument(1); + assertNotNull(actualTimeout); + if (expectedTimeout != null) + { + assertEquals(expectedTimeout, actualTimeout, "expectedTimeout"); + } - // Copy a test file into the target file location if it exists - int i = actualTarget.lastIndexOf('_'); - if (i >= 0) - { - String testFilename = actualTarget.substring(i + 1); - File testFile = getTestFile(testFilename, false); - File targetFile = new File(actualTarget); - generateTargetFileFromResourceFile(actualTargetExtension, testFile, - targetFile); - } + // Copy a test file into the target file location if it exists + int i = actualTarget.lastIndexOf('_'); + if (i >= 0) + { + String testFilename = actualTarget.substring(i + 1); + File testFile = getTestFile(testFilename, false); + File targetFile = new File(actualTarget); + generateTargetFileFromResourceFile(actualTargetExtension, testFile, + targetFile); + } - // Check the supplied source file has not been changed. - byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath()); - assertTrue(Arrays.equals(sourceFileBytes, actualSourceFileBytes), - "Source file is not the same"); + // Check the supplied source file has not been changed. + byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath()); + assertTrue(Arrays.equals(sourceFileBytes, actualSourceFileBytes), + "Source file is not the same"); - return mockExecutionResult; - }); + return mockExecutionResult; + }); when(mockExecutionResult.getExitValue()).thenReturn(0); when(mockExecutionResult.getStdErr()).thenReturn("STDERROR"); @@ -206,31 +208,31 @@ public class ImageMagickTest extends AbstractBaseTest { expectedOptions = "-auto-orient " + "-gravity " + value + " +repage"; mockMvc - .perform(MockMvcRequestBuilders - .multipart(ENDPOINT_TRANSFORM) - .file(sourceFile) - .param("targetExtension", targetExtension) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype) - .param("cropGravity", value)) - .andExpect(status().isOk()) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + .perform(MockMvcRequestBuilders + .multipart(ENDPOINT_TRANSFORM) + .file(sourceFile) + .param("targetExtension", targetExtension) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype) + .param("cropGravity", value)) + .andExpect(status().isOk()) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Test public void cropGravityBadTest() throws Exception { mockMvc - .perform(MockMvcRequestBuilders - .multipart(ENDPOINT_TRANSFORM) - .file(sourceFile) - .param("targetExtension", targetExtension) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype) - .param("cropGravity", "badValue")) - .andExpect(status().is(BAD_REQUEST.value())); + .perform(MockMvcRequestBuilders + .multipart(ENDPOINT_TRANSFORM) + .file(sourceFile) + .param("targetExtension", targetExtension) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype) + .param("cropGravity", "badValue")) + .andExpect(status().is(BAD_REQUEST.value())); } @Test @@ -239,36 +241,36 @@ public class ImageMagickTest extends AbstractBaseTest expectedOptions = "-alpha remove -gravity SouthEast -crop 123x456%+90+12 +repage -thumbnail 321x654%!"; expectedSourceSuffix = "[2-3]"; mockMvc - .perform(MockMvcRequestBuilders - .multipart(ENDPOINT_TRANSFORM) - .file(sourceFile) - .param("targetExtension", targetExtension) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype) + .perform(MockMvcRequestBuilders + .multipart(ENDPOINT_TRANSFORM) + .file(sourceFile) + .param("targetExtension", targetExtension) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype) - .param("startPage", "2") - .param("endPage", "3") + .param("startPage", "2") + .param("endPage", "3") - .param("alphaRemove", "true") - .param("autoOrient", "false") + .param("alphaRemove", "true") + .param("autoOrient", "false") - .param("cropGravity", "SouthEast") - .param("cropWidth", "123") - .param("cropHeight", "456") - .param("cropPercentage", "true") - .param("cropXOffset", "90") - .param("cropYOffset", "12") + .param("cropGravity", "SouthEast") + .param("cropWidth", "123") + .param("cropHeight", "456") + .param("cropPercentage", "true") + .param("cropXOffset", "90") + .param("cropYOffset", "12") - .param("thumbnail", "true") - .param("resizeWidth", "321") - .param("resizeHeight", "654") - .param("resizePercentage", "true") - .param("allowEnlargement", "true") - .param("maintainAspectRatio", "false")) - .andExpect(status().isOk()) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + .param("thumbnail", "true") + .param("resizeWidth", "321") + .param("resizeHeight", "654") + .param("resizePercentage", "true") + .param("allowEnlargement", "true") + .param("maintainAspectRatio", "false")) + .andExpect(status().isOk()) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Test @@ -277,36 +279,36 @@ public class ImageMagickTest extends AbstractBaseTest expectedOptions = "-auto-orient -gravity SouthEast -crop 123x456+90+12 +repage -resize 321x654>"; expectedSourceSuffix = "[2-3]"; mockMvc - .perform(MockMvcRequestBuilders - .multipart(ENDPOINT_TRANSFORM) - .file(sourceFile) - .param("targetExtension", targetExtension) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype) - - .param("startPage", "2") - .param("endPage", "3") + .perform(MockMvcRequestBuilders + .multipart(ENDPOINT_TRANSFORM) + .file(sourceFile) + .param("targetExtension", targetExtension) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype) - .param("alphaRemove", "false") - .param("autoOrient", "true") + .param("startPage", "2") + .param("endPage", "3") - .param("cropGravity", "SouthEast") - .param("cropWidth", "123") - .param("cropHeight", "456") - .param("cropPercentage", "false") - .param("cropXOffset", "90") - .param("cropYOffset", "12") + .param("alphaRemove", "false") + .param("autoOrient", "true") - .param("thumbnail", "false") - .param("resizeWidth", "321") - .param("resizeHeight", "654") - .param("resizePercentage", "false") - .param("allowEnlargement", "false") - .param("maintainAspectRatio", "true")) - .andExpect(status().isOk()) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + .param("cropGravity", "SouthEast") + .param("cropWidth", "123") + .param("cropHeight", "456") + .param("cropPercentage", "false") + .param("cropXOffset", "90") + .param("cropYOffset", "12") + + .param("thumbnail", "false") + .param("resizeWidth", "321") + .param("resizeHeight", "654") + .param("resizePercentage", "false") + .param("allowEnlargement", "false") + .param("maintainAspectRatio", "true")) + .andExpect(status().isOk()) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Test @@ -315,20 +317,20 @@ public class ImageMagickTest extends AbstractBaseTest // Example of why the commandOptions parameter is a bad idea. expectedOptions = "( horrible command / ); -auto-orient -resize 321x654"; mockMvc - .perform(MockMvcRequestBuilders - .multipart(ENDPOINT_TRANSFORM) - .file(sourceFile) - .param("targetExtension", targetExtension) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype) - .param("thumbnail", "false") - .param("resizeWidth", "321") - .param("resizeHeight", "654") - .param("commandOptions", "( horrible command / );")) - .andExpect(status().isOk()) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + .perform(MockMvcRequestBuilders + .multipart(ENDPOINT_TRANSFORM) + .file(sourceFile) + .param("targetExtension", targetExtension) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype) + .param("thumbnail", "false") + .param("resizeWidth", "321") + .param("resizeHeight", "654") + .param("commandOptions", "( horrible command / );")) + .andExpect(status().isOk()) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Override @@ -346,9 +348,9 @@ public class ImageMagickTest extends AbstractBaseTest when(mockExecutionResult.getExitValue()).thenReturn(1); mockMvc.perform(mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", "xxx")) - .andExpect(status().is(BAD_REQUEST.value())) - .andExpect(status() - .reason(containsString("Transformer exit code was not 0: \nSTDERR"))); + .andExpect(status().is(BAD_REQUEST.value())) + .andExpect(status() + .reason(containsString("Transformer exit code was not 0: \nSTDERR"))); } @Test @@ -365,11 +367,11 @@ public class ImageMagickTest 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 @@ -378,16 +380,16 @@ public class ImageMagickTest 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()); @@ -398,7 +400,7 @@ public class ImageMagickTest extends AbstractBaseTest @Test public void testOverridingExecutorPaths() { - //System test property values can me modified in the pom.xml + // System test property values can me modified in the pom.xml assertEquals(EXE, System.getProperty("IMAGEMAGICK_EXE")); assertEquals(DYN, System.getProperty("IMAGEMAGICK_DYN")); assertEquals(ROOT, System.getProperty("IMAGEMAGICK_ROOT")); diff --git a/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickTransformationIT.java b/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickTransformationIT.java index 2aacaa86..66766c40 100644 --- a/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickTransformationIT.java +++ b/engines/imagemagick/src/test/java/org/alfresco/transform/imagemagick/ImageMagickTransformationIT.java @@ -30,8 +30,13 @@ import static java.text.MessageFormat.format; import static java.util.Collections.emptyMap; import static java.util.function.Function.identity; import static java.util.stream.Collectors.toMap; -import static org.alfresco.transform.base.clients.HttpClient.sendTRequest; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.springframework.http.HttpStatus.OK; + import static org.alfresco.transform.base.clients.FileInfo.testFile; +import static org.alfresco.transform.base.clients.HttpClient.sendTRequest; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_BMP; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_CGM; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_GIF; @@ -64,18 +69,12 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_TIFF; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_XBM; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_XPM; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_XWD; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; -import static org.springframework.http.HttpStatus.OK; import java.util.List; import java.util.Map; import java.util.stream.Stream; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; - -import org.alfresco.transform.base.clients.FileInfo; import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -84,10 +83,13 @@ import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; +import org.alfresco.transform.base.clients.FileInfo; + /** * @author Cezar Leahu */ -public class ImageMagickTransformationIT { +public class ImageMagickTransformationIT +{ private static final Logger logger = LoggerFactory.getLogger(ImageMagickTransformationIT.class); private static final String ENGINE_URL = "http://localhost:8090"; private static final List> targetExtensions = new ImmutableList.Builder>() @@ -152,38 +154,38 @@ public class ImageMagickTransformationIT { .build(); private static final Map TEST_FILES = Stream.of( - testFile(MIMETYPE_IMAGE_BMP, "bmp", "quick.bmp"), + testFile(MIMETYPE_IMAGE_BMP, "bmp", "quick.bmp"), testFile(MIMETYPE_IMAGE_GIF, "gif", "quick.gif"), testFile(MIMETYPE_IMAGE_JPEG, "jpg", "quick.jpg"), - testFile(MIMETYPE_IMAGE_PBM, "pbm", "quick.pbm"), + testFile(MIMETYPE_IMAGE_PBM, "pbm", "quick.pbm"), testFile(MIMETYPE_IMAGE_PGM, "pgm", "quick.pgm"), - testFile(MIMETYPE_IMAGE_PNG, "png", "quick.png"), + testFile(MIMETYPE_IMAGE_PNG, "png", "quick.png"), testFile(MIMETYPE_IMAGE_PNM, "pnm", "quick.pnm"), - testFile(MIMETYPE_IMAGE_PPM, "ppm", "quick.ppm"), + testFile(MIMETYPE_IMAGE_PPM, "ppm", "quick.ppm"), testFile(MIMETYPE_IMAGE_XBM, "xbm", "quick.xbm"), - testFile(MIMETYPE_IMAGE_XPM, "xpm", "quick.xpm"), + testFile(MIMETYPE_IMAGE_XPM, "xpm", "quick.xpm"), testFile(MIMETYPE_IMAGE_PSD, "psd", "quick.psd"), - testFile(MIMETYPE_IMAGE_TIFF, "tiff", "quick.tiff"), - testFile(MIMETYPE_IMAGE_XWD, "xwd", "quick.xwd") - ).collect(toMap(FileInfo::getPath, identity())); + testFile(MIMETYPE_IMAGE_TIFF, "tiff", "quick.tiff"), + testFile(MIMETYPE_IMAGE_XWD, "xwd", "quick.xwd")).collect(toMap(FileInfo::getPath, identity())); - public static Stream>> engineTransformations() { + public static Stream>> engineTransformations() + { return Stream - .of( - allTargets("quick.bmp", targetExtensions), - allTargets("quick.gif", targetExtensions), - allTargets("quick.jpg", targetExtensions), - allTargets("quick.pbm", targetExtensions), - allTargets("quick.pgm", targetExtensions), - allTargets("quick.png", targetExtensions), - allTargets("quick.pnm", targetExtensions), - allTargets("quick.ppm", targetExtensions), - allTargets("quick.psd", targetExtensionsForPSD), - allTargets("quick.tiff", targetExtensions), - allTargets("quick.xbm", targetExtensions), - allTargets("quick.xpm", targetExtensions), - allTargets("quick.xwd", targetExtensions) - ).flatMap(identity()); + .of( + allTargets("quick.bmp", targetExtensions), + allTargets("quick.gif", targetExtensions), + allTargets("quick.jpg", targetExtensions), + allTargets("quick.pbm", targetExtensions), + allTargets("quick.pgm", targetExtensions), + allTargets("quick.png", targetExtensions), + allTargets("quick.pnm", targetExtensions), + allTargets("quick.ppm", targetExtensions), + allTargets("quick.psd", targetExtensionsForPSD), + allTargets("quick.tiff", targetExtensions), + allTargets("quick.xbm", targetExtensions), + allTargets("quick.xpm", targetExtensions), + allTargets("quick.xwd", targetExtensions)) + .flatMap(identity()); } @ParameterizedTest @@ -194,13 +196,13 @@ public class ImageMagickTransformationIT { String targetExtension = entry.getRight().getLeft(); String sourceMimetype = entry.getLeft().getMimeType(); String targetMimetype = entry.getRight().getRight(); - + final String descriptor = format("Transform ({0}, {1} -> {2}, {3})", - sourceFile, sourceMimetype, targetMimetype, targetExtension); + sourceFile, sourceMimetype, targetMimetype, targetExtension); try { final ResponseEntity response = sendTRequest(ENGINE_URL, sourceFile, sourceMimetype, - targetMimetype, targetExtension, emptyMap()); + targetMimetype, targetExtension, emptyMap()); assertEquals(OK, response.getStatusCode(), descriptor); } catch (Exception e) @@ -209,10 +211,10 @@ public class ImageMagickTransformationIT { } } - private static Stream>> allTargets(final String sourceFile, List> targetExtensionsList) + private static Stream>> allTargets(final String sourceFile, List> targetExtensionsList) { return targetExtensionsList - .stream() - .map(k -> Pair.of(TEST_FILES.get(sourceFile), k)); + .stream() + .map(k -> Pair.of(TEST_FILES.get(sourceFile), k)); } } diff --git a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/LibreOfficeTransformEngine.java b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/LibreOfficeTransformEngine.java index 3eb04a21..a16b65df 100644 --- a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/LibreOfficeTransformEngine.java +++ b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/LibreOfficeTransformEngine.java @@ -26,19 +26,20 @@ */ package org.alfresco.transform.libreoffice; -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.Collections; - import static org.alfresco.transform.base.logging.StandardMessages.COMMUNITY_LICENCE; import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; +import java.util.Collections; + +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 LibreOfficeTransformEngine implements TransformEngine { @@ -70,6 +71,6 @@ public class LibreOfficeTransformEngine implements TransformEngine public ProbeTransform getProbeTransform() { return new ProbeTransform("probe.doc", MIMETYPE_WORD, MIMETYPE_PDF, Collections.emptyMap(), - 9728, 1024, 150, 10240, 60 * 30 + 1, 60 * 15 + 20); + 9728, 1024, 150, 10240, 60 * 30 + 1, 60 * 15 + 20); } } diff --git a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/JodConverterSharedInstance.java b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/JodConverterSharedInstance.java index 854fc1f5..f9fff41f 100644 --- a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/JodConverterSharedInstance.java +++ b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/JodConverterSharedInstance.java @@ -33,7 +33,6 @@ import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; - import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; @@ -46,8 +45,7 @@ import org.slf4j.LoggerFactory; ///////// THIS FILE WAS A COPY OF THE CODE IN alfresco-repository ///////////// /** - * Makes use of the JodConverter library and an installed - * OpenOffice application to perform OpenOffice-driven conversions. + * Makes use of the JodConverter library and an installed OpenOffice application to perform OpenOffice-driven conversions. * * @author Neil McErlean */ @@ -68,7 +66,7 @@ public class JodConverterSharedInstance implements JodConverter // // Numeric parameters have to be handled as Strings, as that is what Spring gives us for missing values // e.g. if jodconverter.maxTasksPerProcess is not specified in the properties file, the value - // "${jodconverter.maxTasksPerProcess}" will be injected. + // "${jodconverter.maxTasksPerProcess}" will be injected. private Integer maxTasksPerProcess; private String url; @@ -163,7 +161,7 @@ public class JodConverterSharedInstance implements JodConverter if (!tmp.isDirectory()) { throw new RuntimeException( - "OpenOffice template profile directory " + templateProfileDir + " does not exist."); + "OpenOffice template profile directory " + templateProfileDir + " does not exist."); } this.templateProfileDir = tmp; } @@ -216,10 +214,10 @@ public class JodConverterSharedInstance implements JodConverter // value in it. // jodconverter.officeHome=/opt/libreoffice5.4/ - // ooo.exe=/opt/libreoffice5.4/program/soffice.bin + // ooo.exe=/opt/libreoffice5.4/program/soffice.bin // jodconverter.officeHome=C:/noscan/installs/521~1.1/LIBREO~1/App/libreoffice - // ooo.exe=C:/noscan/installs/COMMUN~1.0-E/LIBREO~1/App/libreoffice/program/soffice.exe + // ooo.exe=C:/noscan/installs/COMMUN~1.0-E/LIBREO~1/App/libreoffice/program/soffice.exe File oooExe = new File(deprecatedOooExe); File parent = oooExe.getParentFile(); @@ -238,11 +236,11 @@ public class JodConverterSharedInstance implements JodConverter // So that Community systems <= Alfresco 6.0.1-ea keep working on upgrade, we may need to use the deprecated // ooo.enabled setting if true rather than the jodconverter.enabled setting as oooDirect was replaced by // jodconverter after this release. - // If ooo.enabled is true the JodConverter will be enabled. - // If ooo.enabled is false or unset the jodconverter.enabled value is used. - // Community set properties via alfresco-global.properties. - // Enterprise may do the same but may also reset jodconverter.enabled them via the Admin console. - // In the case of Enterprise it is very unlikely that ooo.enabled will be set to true. + // If ooo.enabled is true the JodConverter will be enabled. + // If ooo.enabled is false or unset the jodconverter.enabled value is used. + // Community set properties via alfresco-global.properties. + // Enterprise may do the same but may also reset jodconverter.enabled them via the Admin console. + // In the case of Enterprise it is very unlikely that ooo.enabled will be set to true. private boolean isEnabled() { return (deprecatedOooEnabled != null && deprecatedOooEnabled) || (enabled != null && enabled); @@ -254,8 +252,8 @@ public class JodConverterSharedInstance implements JodConverter private int[] getPortNumbers() { return (enabled == null || !enabled) && deprecatedOooEnabled != null && deprecatedOooEnabled - ? deprecatedOooPortNumbers - : portNumbers; + ? deprecatedOooPortNumbers + : portNumbers; } private Long parseStringForLong(String string) @@ -271,19 +269,17 @@ public class JodConverterSharedInstance implements JodConverter return null; } - /* - * (non-Javadoc) - * @see org.alfresco.repo.content.JodConverter#isAvailable() - */ + /* (non-Javadoc) + * + * @see org.alfresco.repo.content.JodConverter#isAvailable() */ public boolean isAvailable() { return isAvailable && (officeManager != null || (url != null && !url.isEmpty())); } - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ + /* (non-Javadoc) + * + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ @PostConstruct public void afterPropertiesSet() { @@ -295,7 +291,7 @@ public class JodConverterSharedInstance implements JodConverter if (logger.isDebugEnabled()) { logger.debug( - "JodConverter settings (null settings will be replaced by jodconverter defaults):"); + "JodConverter settings (null settings will be replaced by jodconverter defaults):"); logger.debug(" officeHome = {}", officeHome); logger.debug(" enabled = {}", isEnabled()); logger.debug(" portNumbers = {}", getString(portNumbers)); @@ -361,22 +357,23 @@ public class JodConverterSharedInstance implements JodConverter catch (IllegalStateException e) { logger.error("Unable to pre-initialise JodConverter library. " + - "The following error is shown for informational purposes only.", e); + "The following error is shown for informational purposes only.", e); return; } catch (OfficeException e) { logger.error("Unable to start JodConverter library. " + - "The following error is shown for informational purposes only.", e); + "The following error is shown for informational purposes only.", e); // We need to let it continue (comment-out return statement) even if an error occurs. See MNT-13706 and associated issues. - //return; + // return; } catch (Exception e) { logger.error( - "Unexpected error in configuring or starting the JodConverter library." + - "The following error is shown for informational purposes only.", e); + "Unexpected error in configuring or starting the JodConverter library." + + "The following error is shown for informational purposes only.", + e); return; } } @@ -424,13 +421,13 @@ public class JodConverterSharedInstance implements JodConverter } private List findSofficePrograms(File searchRoot, List results, - int maxRecursionDepth) + int maxRecursionDepth) { return this.findSofficePrograms(searchRoot, results, 0, maxRecursionDepth); } private List findSofficePrograms(File searchRoot, List results, - int currentRecursionDepth, int maxRecursionDepth) + int currentRecursionDepth, int maxRecursionDepth) { if (currentRecursionDepth >= maxRecursionDepth) { @@ -461,7 +458,8 @@ public class JodConverterSharedInstance implements JodConverter /** * Logs some information on the specified file, including name and r/w/x permissions. * - * @param f the file to log. + * @param f + * the file to log. */ private void logFileInfo(File f) { @@ -475,11 +473,11 @@ public class JodConverterSharedInstance implements JodConverter if (f.exists() && f.canRead()) { msg.append("(") - .append(f.isDirectory() ? "d" : "-") - .append(f.canRead() ? "r" : "-") - .append(f.canWrite() ? "w" : "-") - .append(f.canExecute() ? "x" : "-") - .append(")"); + .append(f.isDirectory() ? "d" : "-") + .append(f.canRead() ? "r" : "-") + .append(f.canWrite() ? "w" : "-") + .append(f.canExecute() ? "x" : "-") + .append(")"); } else { @@ -488,10 +486,9 @@ public class JodConverterSharedInstance implements JodConverter logger.debug(msg.toString()); } - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ + /* (non-Javadoc) + * + * @see org.springframework.beans.factory.DisposableBean#destroy() */ @PreDestroy public void destroy() { @@ -510,8 +507,8 @@ public class JodConverterSharedInstance implements JodConverter } /* (non-Javadoc) - * @see org.alfresco.repo.content.JodConverterWorker#getOfficeManager() - */ + * + * @see org.alfresco.repo.content.JodConverterWorker#getOfficeManager() */ @Override public OfficeManager getOfficeManager() { diff --git a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeExtractMetadataTask.java b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeExtractMetadataTask.java index 3871d571..aacdd98e 100644 --- a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeExtractMetadataTask.java +++ b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeExtractMetadataTask.java @@ -26,6 +26,15 @@ */ package org.alfresco.transform.libreoffice.transformers; +import static org.artofsolving.jodconverter.office.OfficeUtils.SERVICE_DESKTOP; +import static org.artofsolving.jodconverter.office.OfficeUtils.cast; +import static org.artofsolving.jodconverter.office.OfficeUtils.toUrl; + +import java.io.File; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + import com.sun.star.beans.PropertyValue; import com.sun.star.beans.UnknownPropertyException; import com.sun.star.beans.XPropertySet; @@ -43,24 +52,16 @@ import org.artofsolving.jodconverter.office.OfficeContext; import org.artofsolving.jodconverter.office.OfficeException; import org.artofsolving.jodconverter.office.OfficeTask; -import java.io.File; -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; - -import static org.artofsolving.jodconverter.office.OfficeUtils.SERVICE_DESKTOP; -import static org.artofsolving.jodconverter.office.OfficeUtils.cast; -import static org.artofsolving.jodconverter.office.OfficeUtils.toUrl; - /** * @deprecated The JodConverterMetadataExtracter has not been in use since 6.0.1 * - * Extracts values from Open Office documents into the following: - *
    + *             Extracts values from Open Office documents into the following:
    + * 
    + *             
      *   author:                 --      cm:author
      *   title:                  --      cm:title
      *   description:            --      cm:description
    - * 
    + *
    * * @author Neil McErlean * @author adavis @@ -68,10 +69,7 @@ import static org.artofsolving.jodconverter.office.OfficeUtils.toUrl; @Deprecated public class LibreOfficeExtractMetadataTask implements OfficeTask { - /* - * These keys are used by Alfresco to map properties into a content model and do need to - * have lower-case initial letters. - */ + /* These keys are used by Alfresco to map properties into a content model and do need to have lower-case initial letters. */ private static final String KEY_AUTHOR = "author"; private static final String KEY_TITLE = "title"; private static final String KEY_DESCRIPTION = "description"; @@ -192,7 +190,8 @@ public class LibreOfficeExtractMetadataTask implements OfficeTask * OOo throws exceptions if we ask for properties that aren't there, so we'll tread carefully. * * @param propSet - * @param propertyName property name as used by the OOo API. + * @param propertyName + * property name as used by the OOo API. * @throws UnknownPropertyException * @throws WrappedTargetException */ diff --git a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeTransformer.java b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeTransformer.java index 37931c26..702dba65 100644 --- a/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeTransformer.java +++ b/engines/libreoffice/src/main/java/org/alfresco/transform/libreoffice/transformers/LibreOfficeTransformer.java @@ -26,12 +26,17 @@ */ package org.alfresco.transform.libreoffice.transformers; +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +import java.io.File; +import java.io.IOException; +import java.io.Serializable; +import java.util.Map; +import jakarta.annotation.PostConstruct; + import com.fasterxml.jackson.databind.ObjectMapper; import com.sun.star.task.ErrorCodeIOException; -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.base.executors.JavaExecutor; -import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; -import org.alfresco.transform.exceptions.TransformException; import org.apache.commons.lang3.StringUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; @@ -44,18 +49,13 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import jakarta.annotation.PostConstruct; -import java.io.File; -import java.io.IOException; -import java.io.Serializable; -import java.util.Map; - -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.executors.JavaExecutor; +import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; +import org.alfresco.transform.exceptions.TransformException; /** - * JavaExecutor implementation for running LibreOffice transformations. It loads the - * transformation logic in the same JVM (check the {@link JodConverter} implementation). + * JavaExecutor implementation for running LibreOffice transformations. It loads the transformation logic in the same JVM (check the {@link JodConverter} implementation). */ @Component public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFileAdaptor @@ -105,7 +105,7 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi { throw new IllegalArgumentException("LibreOfficeTransformer LIBREOFFICE_TEMPLATE_PROFILE_DIR variable cannot be null"); } - if (isEnabled == null || isEnabled.isEmpty() || !(isEnabled.equalsIgnoreCase("true")|| isEnabled.equalsIgnoreCase("false"))) + if (isEnabled == null || isEnabled.isEmpty() || !(isEnabled.equalsIgnoreCase("true") || isEnabled.equalsIgnoreCase("false"))) { throw new IllegalArgumentException("LibreOfficeTransformer LIBREOFFICE_IS_ENABLED variable must be set to true/false"); } @@ -131,7 +131,7 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi @Override public void transform(String sourceMimetype, String targetMimetype, Map transformOptions, - File sourceFile, File targetFile, TransformManager transformManager) + File sourceFile, File targetFile, TransformManager transformManager) { call(sourceFile, targetFile); } @@ -146,20 +146,21 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi catch (OfficeException e) { throw new TransformException(BAD_REQUEST, - "LibreOffice server conversion failed: \n" + - " from file: " + sourceFile + "\n" + - " to file: " + targetFile, e); + "LibreOffice server conversion failed: \n" + + " from file: " + sourceFile + "\n" + + " to file: " + targetFile, + e); } catch (Throwable throwable) { // Because of the known bug with empty Spreadsheets in JodConverter try to catch exception and produce empty pdf file if (throwable.getCause() instanceof ErrorCodeIOException && - ((ErrorCodeIOException) throwable.getCause()).ErrCode == JODCONVERTER_TRANSFORMATION_ERROR_CODE) + ((ErrorCodeIOException) throwable.getCause()).ErrCode == JODCONVERTER_TRANSFORMATION_ERROR_CODE) { logger.warn("Transformation failed: \n" + - "from file: " + sourceFile + "\n" + - "to file: " + targetFile + - "Source file " + sourceFile + " has no content"); + "from file: " + sourceFile + "\n" + + "to file: " + targetFile + + "Source file " + sourceFile + " has no content"); produceEmptyPdfFile(targetFile); } else @@ -182,8 +183,7 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi } /** - * This method produces an empty PDF file at the specified File location. - * Apache's PDFBox is used to create the PDF file. + * This method produces an empty PDF file at the specified File location. Apache's PDFBox is used to create the PDF file. */ private static void produceEmptyPdfFile(File targetFile) { @@ -193,7 +193,7 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi PDPage pdfPage = new PDPage(); try (PDDocument pdfDoc = new PDDocument(); - PDPageContentStream ignore = new PDPageContentStream(pdfDoc, pdfPage)) + PDPageContentStream ignore = new PDPageContentStream(pdfDoc, pdfPage)) { // Even though, we want an empty PDF, some libs (e.g. PDFRenderer) object to PDFs // that have literally nothing in them. So we'll put a content stream in it. @@ -209,13 +209,11 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi } /** - * @deprecated The JodConverterMetadataExtracter has not been in use since 6.0.1. - * This code exists in case there are custom implementations, that need to be converted to T-Engines. - * It is simply a copy and paste from the content repository and has received limited testing. + * @deprecated The JodConverterMetadataExtracter has not been in use since 6.0.1. This code exists in case there are custom implementations, that need to be converted to T-Engines. It is simply a copy and paste from the content repository and has received limited testing. */ public void extractMetadata(String transformName, String sourceMimetype, String targetMimetype, - Map transformOptions, - File sourceFile, File targetFile) + Map transformOptions, + File sourceFile, File targetFile) { OfficeManager officeManager = jodconverter.getOfficeManager(); LibreOfficeExtractMetadataTask extractMetadataTask = new LibreOfficeExtractMetadataTask(sourceFile); @@ -227,13 +225,14 @@ public class LibreOfficeTransformer implements JavaExecutor, CustomTransformerFi { throw new TransformException(BAD_REQUEST, "LibreOffice metadata extract failed: \n" + - " from file: " + sourceFile, e); + " from file: " + sourceFile, + e); } Map metadata = extractMetadataTask.getMetadata(); if (logger.isDebugEnabled()) { - metadata.forEach((k,v) -> logger.debug(k+"="+v)); + metadata.forEach((k, v) -> logger.debug(k + "=" + v)); } writeMetadataIntoTargetFile(targetFile, metadata); diff --git a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeLivenessReadinessProbeIT.java b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeLivenessReadinessProbeIT.java index 1fe37792..8fffb74c 100644 --- a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeLivenessReadinessProbeIT.java +++ b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeLivenessReadinessProbeIT.java @@ -29,9 +29,11 @@ package org.alfresco.transform.libreoffice; import org.alfresco.transform.base.LivenessReadinessProbeTest; -public class LibreOfficeLivenessReadinessProbeIT extends LivenessReadinessProbeTest { +public class LibreOfficeLivenessReadinessProbeIT extends LivenessReadinessProbeTest +{ @Override - protected ImagesForTests getImageForTest() { + protected ImagesForTests getImageForTest() + { return new ImagesForTests("alfresco-libreoffice", "text/plain", "application/pdf", "original.txt"); } } diff --git a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeQueueIT.java b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeQueueIT.java index b8d89fac..5018ccd7 100644 --- a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeQueueIT.java +++ b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeQueueIT.java @@ -30,12 +30,11 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORDPROCES import java.util.UUID; -import org.alfresco.transform.client.model.TransformRequest; import org.alfresco.transform.base.messaging.AbstractQueueIT; +import org.alfresco.transform.client.model.TransformRequest; /** - * @author Lucian Tuca - * created on 15/01/2019 + * @author Lucian Tuca created on 15/01/2019 */ public class LibreOfficeQueueIT extends AbstractQueueIT { @@ -43,16 +42,16 @@ public class LibreOfficeQueueIT extends AbstractQueueIT protected TransformRequest buildRequest() { return TransformRequest - .builder() - .withRequestId(UUID.randomUUID().toString()) - .withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING) - .withTargetMediaType(MIMETYPE_OPENXML_WORDPROCESSING) - .withTargetExtension("doc") - .withSchema(1) - .withClientData("ACS") - .withSourceReference(UUID.randomUUID().toString()) - .withSourceSize(32L) - .withInternalContextForTransformEngineTests() - .build(); + .builder() + .withRequestId(UUID.randomUUID().toString()) + .withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING) + .withTargetMediaType(MIMETYPE_OPENXML_WORDPROCESSING) + .withTargetExtension("doc") + .withSchema(1) + .withClientData("ACS") + .withSourceReference(UUID.randomUUID().toString()) + .withSourceSize(32L) + .withInternalContextForTransformEngineTests() + .build(); } } diff --git a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTest.java b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTest.java index fcea3eb0..0e753350 100644 --- a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTest.java +++ b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTest.java @@ -26,11 +26,6 @@ */ package org.alfresco.transform.libreoffice; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; -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.alfresco.transform.common.RequestParamMap.TARGET_EXTENSION; import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -48,20 +43,18 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.util.StringUtils.getFilenameExtension; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; +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_EXTENSION; +import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE; + import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.Arrays; import java.util.UUID; -import org.alfresco.transform.base.registry.CustomTransformers; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.libreoffice.transformers.LibreOfficeTransformer; -import org.alfresco.transform.base.AbstractBaseTest; -import org.alfresco.transform.base.executors.RuntimeExec.ExecutionResult; -import org.alfresco.transform.base.model.FileRefEntity; -import org.alfresco.transform.base.model.FileRefResponse; import org.artofsolving.jodconverter.office.OfficeException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -79,6 +72,15 @@ import org.springframework.test.util.ReflectionTestUtils; 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.ExecutionResult; +import org.alfresco.transform.base.model.FileRefEntity; +import org.alfresco.transform.base.model.FileRefResponse; +import org.alfresco.transform.base.registry.CustomTransformers; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.libreoffice.transformers.LibreOfficeTransformer; + /** * Test LibreOffice with mocked external command. */ @@ -122,13 +124,12 @@ public class LibreOfficeTest extends AbstractBaseTest // This is because LibreOffice used JodConverter rather than a RuntimeExec sourceFileBytes = Files.readAllBytes( - getTestFile("quick." + sourceExtension, true).toPath()); + getTestFile("quick." + sourceExtension, true).toPath()); expectedTargetFileBytes = Files.readAllBytes( - getTestFile("quick." + targetExtension, true).toPath()); + getTestFile("quick." + targetExtension, true).toPath()); sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, sourceFileBytes); - doAnswer(invocation -> - { + doAnswer(invocation -> { File sourceFile = invocation.getArgument(0); File targetFile = invocation.getArgument(1); String actualTargetExtension = getFilenameExtension(targetFile.getAbsolutePath()); @@ -164,8 +165,8 @@ public class LibreOfficeTest extends AbstractBaseTest protected MockHttpServletRequestBuilder mockMvcRequest(String url, MockMultipartFile sourceFile, String... params) { final MockHttpServletRequestBuilder builder = super.mockMvcRequest(url, sourceFile, params) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype); + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype); return builder; } @@ -175,15 +176,15 @@ public class LibreOfficeTest extends AbstractBaseTest doThrow(OfficeException.class).when(spyLibreOfficeTransformer).convert(any(), any()); mockMvc - .perform(MockMvcRequestBuilders - .multipart(ENDPOINT_TRANSFORM) - .file(sourceFile) - .param(TARGET_EXTENSION, "xxx") - .param(SOURCE_MIMETYPE,sourceMimetype) - .param(TARGET_MIMETYPE,targetMimetype)) - .andExpect(status().is(400)) - .andExpect(status().reason( - containsString("LibreOffice server conversion failed:"))); + .perform(MockMvcRequestBuilders + .multipart(ENDPOINT_TRANSFORM) + .file(sourceFile) + .param(TARGET_EXTENSION, "xxx") + .param(SOURCE_MIMETYPE, sourceMimetype) + .param(TARGET_MIMETYPE, targetMimetype)) + .andExpect(status().is(400)) + .andExpect(status().reason( + containsString("LibreOffice server conversion failed:"))); } @Override @@ -209,11 +210,11 @@ public class LibreOfficeTest 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 @@ -222,16 +223,16 @@ public class LibreOfficeTest 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()); @@ -242,42 +243,42 @@ public class LibreOfficeTest extends AbstractBaseTest @Test public void testOverridingExecutorPaths() { - //System test property value can be modified in the pom.xml + // System test property value can be modified in the pom.xml assertEquals(execPath, System.getProperty("LIBREOFFICE_HOME")); } @Test public void testOverridingExecutorMaxTasksPerProcess() { - //System test property value can be modified in the pom.xml + // System test property value can be modified in the pom.xml assertEquals(maxTasksPerProcess, System.getProperty("LIBREOFFICE_MAX_TASKS_PER_PROCESS")); } @Test public void testOverridingExecutorTimeout() { - //System test property value can be modified in the pom.xml + // System test property value can be modified in the pom.xml assertEquals(timeout, System.getProperty("LIBREOFFICE_TIMEOUT")); } @Test public void testOverridingExecutorPortNumbers() { - //System test property value can be modified in the pom.xml + // System test property value can be modified in the pom.xml assertEquals(portNumbers, System.getProperty("LIBREOFFICE_PORT_NUMBERS")); } @Test public void testOverridingExecutorTemplateProfileDir() { - //System test property value can be modified in the pom.xml + // System test property value can be modified in the pom.xml assertEquals(templateProfileDir, System.getProperty("LIBREOFFICE_TEMPLATE_PROFILE_DIR")); } @Test public void testOverridingExecutorIsEnabled() { - //System test property value can be modified in the pom.xml + // System test property value can be modified in the pom.xml assertEquals(isEnabled, System.getProperty("LIBREOFFICE_IS_ENABLED")); } @@ -285,33 +286,33 @@ public class LibreOfficeTest extends AbstractBaseTest public void testInvalidExecutorMaxTasksPerProcess() { testInvalidValue("maxTasksPerProcess", "INVALID", - "LibreOfficeTransformer LIBREOFFICE_MAX_TASKS_PER_PROCESS must have a numeric value"); + "LibreOfficeTransformer LIBREOFFICE_MAX_TASKS_PER_PROCESS must have a numeric value"); } @Test public void testInvalidExecutorTimeout() { testInvalidValue("timeout", "INVALID", - "LibreOfficeTransformer LIBREOFFICE_TIMEOUT must have a numeric value"); + "LibreOfficeTransformer LIBREOFFICE_TIMEOUT must have a numeric value"); } @Test public void testInvalidExecutorPortNumbers() { testInvalidValue("portNumbers", null, - "LibreOfficeTransformer LIBREOFFICE_PORT_NUMBERS variable cannot be null or empty"); + "LibreOfficeTransformer LIBREOFFICE_PORT_NUMBERS variable cannot be null or empty"); } @Test public void testInvalidExecutorIsEnabled() { testInvalidValue("isEnabled", "INVALID", - "LibreOfficeTransformer LIBREOFFICE_IS_ENABLED variable must be set to true/false"); + "LibreOfficeTransformer LIBREOFFICE_IS_ENABLED variable must be set to true/false"); } private void testInvalidValue(String fieldName, String invalidValue, String expectedErrorMessage) { - String validValue = (String)ReflectionTestUtils.getField(libreOfficeTransformer, fieldName); + String validValue = (String) ReflectionTestUtils.getField(libreOfficeTransformer, fieldName); String errorMessage = ""; try { diff --git a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTransformationIT.java b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTransformationIT.java index 3915a4a7..ec4e32e9 100644 --- a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTransformationIT.java +++ b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTransformationIT.java @@ -29,8 +29,14 @@ package org.alfresco.transform.libreoffice; import static java.text.MessageFormat.format; import static java.util.function.Function.identity; import static java.util.stream.Collectors.toMap; -import static org.alfresco.transform.base.clients.HttpClient.sendTRequest; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.springframework.http.HttpStatus.OK; + import static org.alfresco.transform.base.clients.FileInfo.testFile; +import static org.alfresco.transform.base.clients.HttpClient.sendTRequest; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_DITA; import static org.alfresco.transform.common.Mimetype.MIMETYPE_EXCEL; import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_SVG; @@ -39,39 +45,33 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_PRESE import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_SPREADSHEET; import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_TEXT; import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW_MACRO; import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO; 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_PPT; import static org.alfresco.transform.common.Mimetype.MIMETYPE_RTF; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_STC; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_STI; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_STW; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_SXC; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_SXI; import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_CSV; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; import static org.alfresco.transform.common.Mimetype.MIMETYPE_TSV; import static org.alfresco.transform.common.Mimetype.MIMETYPE_VISIO; import static org.alfresco.transform.common.Mimetype.MIMETYPE_VISIO_2013; import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORDPERFECT; import static org.alfresco.transform.common.Mimetype.MIMETYPE_XML; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_DITA; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_SXI; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_SXC; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_STW; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_STI; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_STC; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; -import static org.springframework.http.HttpStatus.OK; import java.util.Map; import java.util.Set; import java.util.stream.Stream; import com.google.common.collect.ImmutableSet; - -import org.alfresco.transform.base.clients.FileInfo; import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -80,6 +80,8 @@ import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; +import org.alfresco.transform.base.clients.FileInfo; + /** * @author Cezar Leahu */ @@ -88,112 +90,105 @@ public class LibreOfficeTransformationIT private static final Logger logger = LoggerFactory.getLogger(LibreOfficeTransformationIT.class); private static final String ENGINE_URL = "http://localhost:8090"; private static final Set spreadsheetTargets = ImmutableSet.of( - testFile(MIMETYPE_TEXT_CSV, "csv",null), - testFile(MIMETYPE_HTML,"html",null), - testFile(MIMETYPE_OPENDOCUMENT_SPREADSHEET,"ods",null), - testFile(MIMETYPE_PDF,"pdf",null), - testFile(MIMETYPE_TSV,"tsv",null), - testFile(MIMETYPE_EXCEL,"xls",null) - ); + testFile(MIMETYPE_TEXT_CSV, "csv", null), + testFile(MIMETYPE_HTML, "html", null), + testFile(MIMETYPE_OPENDOCUMENT_SPREADSHEET, "ods", null), + testFile(MIMETYPE_PDF, "pdf", null), + testFile(MIMETYPE_TSV, "tsv", null), + testFile(MIMETYPE_EXCEL, "xls", null)); private static final Set documentsTargets = ImmutableSet.of( - testFile(MIMETYPE_WORD,"doc",null), - testFile(MIMETYPE_HTML,"html",null), - testFile(MIMETYPE_OPENDOCUMENT_TEXT,"odt",null), - testFile(MIMETYPE_PDF,"pdf",null), - testFile(MIMETYPE_RTF,"rtf",null) - ); + testFile(MIMETYPE_WORD, "doc", null), + testFile(MIMETYPE_HTML, "html", null), + testFile(MIMETYPE_OPENDOCUMENT_TEXT, "odt", null), + testFile(MIMETYPE_PDF, "pdf", null), + testFile(MIMETYPE_RTF, "rtf", null)); private static final Set graphicTargets = ImmutableSet.of( - testFile(MIMETYPE_PDF,"pdf",null), - testFile(MIMETYPE_IMAGE_SVG,"svg",null) - ); + testFile(MIMETYPE_PDF, "pdf", null), + testFile(MIMETYPE_IMAGE_SVG, "svg", null)); private static final Set presentationTargets = ImmutableSet.of( - testFile(MIMETYPE_HTML,"html",null), - testFile(MIMETYPE_OPENDOCUMENT_PRESENTATION,"odp",null), - testFile(MIMETYPE_PPT,"ppt",null), - testFile(MIMETYPE_PDF,"pdf",null) - ); + testFile(MIMETYPE_HTML, "html", null), + testFile(MIMETYPE_OPENDOCUMENT_PRESENTATION, "odp", null), + testFile(MIMETYPE_PPT, "ppt", null), + testFile(MIMETYPE_PDF, "pdf", null)); private static final Set pdfTarget = ImmutableSet.of( - testFile(MIMETYPE_PDF,"pdf",null) - ); + testFile(MIMETYPE_PDF, "pdf", null)); private static final Set txtTarget = ImmutableSet.of( - testFile(MIMETYPE_TEXT_PLAIN,"txt",null) - ); + testFile(MIMETYPE_TEXT_PLAIN, "txt", null)); private static final Map TEST_FILES = Stream.of( - testFile(MIMETYPE_WORD ,"doc" ,"quick.doc"), - testFile(MIMETYPE_OPENXML_WORDPROCESSING ,"docx" ,"quick.docx"), - testFile(MIMETYPE_OPENDOCUMENT_GRAPHICS ,"odg" ,"quick.odg"), - testFile(MIMETYPE_OPENDOCUMENT_PRESENTATION ,"odp" ,"quick.odp"), - testFile(MIMETYPE_OPENDOCUMENT_SPREADSHEET ,"ods" ,"quick.ods"), - testFile(MIMETYPE_OPENDOCUMENT_TEXT ,"odt" ,"quick.odt"), - testFile(MIMETYPE_PPT ,"ppt" ,"quick.ppt"), - testFile(MIMETYPE_OPENXML_PRESENTATION ,"pptx" ,"quick.pptx"), - testFile(MIMETYPE_VISIO ,"vdx" ,"quick.vdx"), - testFile(MIMETYPE_VISIO_2013 ,"vsd" ,"quick.vsd"), - testFile(MIMETYPE_WORDPERFECT ,"wpd" ,"quick.wpd"), - testFile(MIMETYPE_EXCEL ,"xls" ,"quick.xls" ), - testFile(MIMETYPE_OPENXML_SPREADSHEET ,"xlsx" ,"quick.xlsx"), - testFile(MIMETYPE_TEXT_CSV ,"csv" ,"people.csv"), - testFile(MIMETYPE_RTF ,"rtf" ,"sample.rtf"), - testFile(MIMETYPE_HTML ,"html" ,"quick.html"), - testFile(MIMETYPE_XML ,"xml" ,"quick.xml"), - testFile(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO ,"xltm" ,"quick.xltm"), - testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW ,"ppsx" ,"quick.ppsx"), - testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW_MACRO ,"ppsm" ,"quick.ppsm"), - testFile(MIMETYPE_DITA ,"dita" ,"quick.dita"), - testFile(MIMETYPE_TEXT_PLAIN ,"txt" ,"quick.txt"), - testFile(MIMETYPE_STC ,"stc" ,"quick.stc"), - testFile(MIMETYPE_STI ,"sti" ,"quick.sti"), - testFile(MIMETYPE_STW ,"stw" ,"quick.stw"), - testFile(MIMETYPE_SXC ,"sxc" ,"quick.sxc"), - testFile(MIMETYPE_SXI ,"sxi" ,"quick.sxi"), - testFile(MIMETYPE_TSV ,"tsv" ,"sample.tsv") - ).collect(toMap(FileInfo::getPath, identity())); + testFile(MIMETYPE_WORD, "doc", "quick.doc"), + testFile(MIMETYPE_OPENXML_WORDPROCESSING, "docx", "quick.docx"), + testFile(MIMETYPE_OPENDOCUMENT_GRAPHICS, "odg", "quick.odg"), + testFile(MIMETYPE_OPENDOCUMENT_PRESENTATION, "odp", "quick.odp"), + testFile(MIMETYPE_OPENDOCUMENT_SPREADSHEET, "ods", "quick.ods"), + testFile(MIMETYPE_OPENDOCUMENT_TEXT, "odt", "quick.odt"), + testFile(MIMETYPE_PPT, "ppt", "quick.ppt"), + testFile(MIMETYPE_OPENXML_PRESENTATION, "pptx", "quick.pptx"), + testFile(MIMETYPE_VISIO, "vdx", "quick.vdx"), + testFile(MIMETYPE_VISIO_2013, "vsd", "quick.vsd"), + testFile(MIMETYPE_WORDPERFECT, "wpd", "quick.wpd"), + testFile(MIMETYPE_EXCEL, "xls", "quick.xls"), + testFile(MIMETYPE_OPENXML_SPREADSHEET, "xlsx", "quick.xlsx"), + testFile(MIMETYPE_TEXT_CSV, "csv", "people.csv"), + testFile(MIMETYPE_RTF, "rtf", "sample.rtf"), + testFile(MIMETYPE_HTML, "html", "quick.html"), + testFile(MIMETYPE_XML, "xml", "quick.xml"), + testFile(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO, "xltm", "quick.xltm"), + testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW, "ppsx", "quick.ppsx"), + testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW_MACRO, "ppsm", "quick.ppsm"), + testFile(MIMETYPE_DITA, "dita", "quick.dita"), + testFile(MIMETYPE_TEXT_PLAIN, "txt", "quick.txt"), + testFile(MIMETYPE_STC, "stc", "quick.stc"), + testFile(MIMETYPE_STI, "sti", "quick.sti"), + testFile(MIMETYPE_STW, "stw", "quick.stw"), + testFile(MIMETYPE_SXC, "sxc", "quick.sxc"), + testFile(MIMETYPE_SXI, "sxi", "quick.sxi"), + testFile(MIMETYPE_TSV, "tsv", "sample.tsv")).collect(toMap(FileInfo::getPath, identity())); public static Stream> engineTransformations() { return Stream - .of( - allTargets("quick.doc", documentsTargets), - allTargets("quick.docx", documentsTargets), - allTargets("quick.html", documentsTargets), - allTargets("quick.odt", documentsTargets), - allTargets("quick.wpd", documentsTargets), - allTargets("quick.txt", documentsTargets), - allTargets("sample.rtf", documentsTargets), + .of( + allTargets("quick.doc", documentsTargets), + allTargets("quick.docx", documentsTargets), + allTargets("quick.html", documentsTargets), + allTargets("quick.odt", documentsTargets), + allTargets("quick.wpd", documentsTargets), + allTargets("quick.txt", documentsTargets), + allTargets("sample.rtf", documentsTargets), - allTargets("quick.odp", presentationTargets), - allTargets("quick.ppt", presentationTargets), - allTargets("quick.pptx", presentationTargets), + allTargets("quick.odp", presentationTargets), + allTargets("quick.ppt", presentationTargets), + allTargets("quick.pptx", presentationTargets), - allTargets("quick.odg", graphicTargets), - allTargets("quick.vdx", graphicTargets), - allTargets("quick.vsd", graphicTargets), + allTargets("quick.odg", graphicTargets), + allTargets("quick.vdx", graphicTargets), + allTargets("quick.vsd", graphicTargets), - allTargets("quick.ods", spreadsheetTargets), - allTargets("quick.xls", spreadsheetTargets), - allTargets("quick.xlsx", spreadsheetTargets), - allTargets("people.csv", spreadsheetTargets), - allTargets("sample.tsv", spreadsheetTargets), + allTargets("quick.ods", spreadsheetTargets), + allTargets("quick.xls", spreadsheetTargets), + allTargets("quick.xlsx", spreadsheetTargets), + allTargets("people.csv", spreadsheetTargets), + allTargets("sample.tsv", spreadsheetTargets), - allTargets("quick.xml", pdfTarget), - allTargets("quick.xltm", pdfTarget), - allTargets("quick.dita", pdfTarget), - allTargets("quick.ppsm", pdfTarget), - allTargets("quick.ppsx", pdfTarget), - allTargets("quick.stc", pdfTarget), - allTargets("quick.sti", pdfTarget), - allTargets("quick.stw", pdfTarget), - allTargets("quick.sxc", pdfTarget), - allTargets("quick.sxi", pdfTarget) + allTargets("quick.xml", pdfTarget), + allTargets("quick.xltm", pdfTarget), + allTargets("quick.dita", pdfTarget), + allTargets("quick.ppsm", pdfTarget), + allTargets("quick.ppsx", pdfTarget), + allTargets("quick.stc", pdfTarget), + allTargets("quick.sti", pdfTarget), + allTargets("quick.stw", pdfTarget), + allTargets("quick.sxc", pdfTarget), + allTargets("quick.sxi", pdfTarget) ) - .flatMap(identity()); + .flatMap(identity()); } @ParameterizedTest @@ -205,11 +200,11 @@ public class LibreOfficeTransformationIT final String sourceMimetype = entry.getLeft().getMimeType(); final String targetMimetype = entry.getRight().getMimeType(); final String descriptor = format("Transform ({0}, {1} -> {2}, {3})", - sourceFile, sourceMimetype, targetMimetype, targetExtension); + sourceFile, sourceMimetype, targetMimetype, targetExtension); try { final ResponseEntity response = sendTRequest(ENGINE_URL, sourceFile, sourceMimetype, - targetMimetype, targetExtension); + targetMimetype, targetExtension); assertEquals(OK, response.getStatusCode(), descriptor); } catch (Exception e) @@ -219,14 +214,14 @@ public class LibreOfficeTransformationIT } private static Stream> allTargets(final String sourceFile, - final Set mimetypes) + final Set mimetypes) { return mimetypes - .stream() - //Filter out duplicate mimetypes. eg. We do not want "Transform (quick.doc, application/msword -> application/msword, doc)" as these are not contained in the engine_config - .filter(type -> !type.getMimeType().equals(TEST_FILES.get(sourceFile).getMimeType())) - // Edge case: Transform (quick.ods, application/vnd.oasis.opendocument.spreadsheet -> text/csv, csv) not in engine_config - .filter(type -> !(TEST_FILES.get(sourceFile).getMimeType().equals(MIMETYPE_OPENDOCUMENT_SPREADSHEET) && type.getMimeType().equals(MIMETYPE_TEXT_CSV))) - .map(k -> Pair.of(TEST_FILES.get(sourceFile), k)); + .stream() + // Filter out duplicate mimetypes. eg. We do not want "Transform (quick.doc, application/msword -> application/msword, doc)" as these are not contained in the engine_config + .filter(type -> !type.getMimeType().equals(TEST_FILES.get(sourceFile).getMimeType())) + // Edge case: Transform (quick.ods, application/vnd.oasis.opendocument.spreadsheet -> text/csv, csv) not in engine_config + .filter(type -> !(TEST_FILES.get(sourceFile).getMimeType().equals(MIMETYPE_OPENDOCUMENT_SPREADSHEET) && type.getMimeType().equals(MIMETYPE_TEXT_CSV))) + .map(k -> Pair.of(TEST_FILES.get(sourceFile), k)); } } diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/metadataExtractors/HtmlMetadataExtractor.java b/engines/misc/src/main/java/org/alfresco/transform/misc/metadataExtractors/HtmlMetadataExtractor.java index ff17040d..8f460c9a 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/metadataExtractors/HtmlMetadataExtractor.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/metadataExtractors/HtmlMetadataExtractor.java @@ -26,17 +26,8 @@ */ package org.alfresco.transform.misc.metadataExtractors; -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR; -import javax.swing.text.ChangedCharSetException; -import javax.swing.text.MutableAttributeSet; -import javax.swing.text.html.HTML; -import javax.swing.text.html.HTMLEditorKit; -import javax.swing.text.html.parser.ParserDelegator; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; @@ -46,13 +37,23 @@ import java.io.Reader; import java.io.Serializable; import java.util.HashMap; import java.util.Map; +import javax.swing.text.ChangedCharSetException; +import javax.swing.text.MutableAttributeSet; +import javax.swing.text.html.HTML; +import javax.swing.text.html.HTMLEditorKit; +import javax.swing.text.html.parser.ParserDelegator; -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder; /** * Metadata extractor for HTML and XHTML. * - * Configuration: (see HtmlMetadataExtractor_metadata_extract.properties and misc_engine_config.json) + * Configuration: (see HtmlMetadataExtractor_metadata_extract.properties and misc_engine_config.json) * *
      *   author:                 --      cm:author
    @@ -73,7 +74,7 @@ public class HtmlMetadataExtractor extends AbstractMetadataExtractorEmbedder
     
         private static final String KEY_AUTHOR = "author";
         private static final String KEY_TITLE = "title";
    -    private static final String KEY_DESCRIPTION= "description";
    +    private static final String KEY_DESCRIPTION = "description";
     
         public HtmlMetadataExtractor()
         {
    @@ -105,8 +106,7 @@ public class HtmlMetadataExtractor extends AbstractMetadataExtractorEmbedder
             // so cannot use the input stream provided, as it will get closed.
             final File sourceFile = transformManager.createSourceFile();
     
    -        HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback()
    -        {
    +        HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback() {
                 StringBuffer title = null;
                 boolean inHead = false;
     
    @@ -177,8 +177,7 @@ public class HtmlMetadataExtractor extends AbstractMetadataExtractorEmbedder
                 }
     
                 public void handleError(String errorMsg, int pos)
    -            {
    -            }
    +            {}
             };
     
             String charsetGuess = "UTF-8";
    diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/metadataExtractors/RFC822MetadataExtractor.java b/engines/misc/src/main/java/org/alfresco/transform/misc/metadataExtractors/RFC822MetadataExtractor.java
    index fc8460aa..c265d71e 100644
    --- a/engines/misc/src/main/java/org/alfresco/transform/misc/metadataExtractors/RFC822MetadataExtractor.java
    +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/metadataExtractors/RFC822MetadataExtractor.java
    @@ -26,17 +26,8 @@
      */
     package org.alfresco.transform.misc.metadataExtractors;
     
    -import org.alfresco.transform.base.TransformManager;
    -import org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder;
    -import org.slf4j.Logger;
    -import org.slf4j.LoggerFactory;
    -import org.springframework.stereotype.Component;
    +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
     
    -import jakarta.mail.Header;
    -import jakarta.mail.internet.InternetAddress;
    -import jakarta.mail.internet.MimeMessage;
    -import jakarta.mail.internet.MimeMessage.RecipientType;
    -import jakarta.mail.internet.MimeUtility;
     import java.io.InputStream;
     import java.io.OutputStream;
     import java.io.Serializable;
    @@ -46,13 +37,23 @@ import java.util.Enumeration;
     import java.util.HashMap;
     import java.util.Map;
     import java.util.Set;
    +import jakarta.mail.Header;
    +import jakarta.mail.internet.InternetAddress;
    +import jakarta.mail.internet.MimeMessage;
    +import jakarta.mail.internet.MimeMessage.RecipientType;
    +import jakarta.mail.internet.MimeUtility;
     
    -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.springframework.stereotype.Component;
    +
    +import org.alfresco.transform.base.TransformManager;
    +import org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder;
     
     /**
      * Metadata extractor for RFC822 mime emails.
      *
    - * Configuration:   (see HtmlMetadataExtractor_metadata_extract.properties and misc_engine_config.json)
    + * Configuration: (see HtmlMetadataExtractor_metadata_extract.properties and misc_engine_config.json)
      *
      * 
      *   messageFrom:              --      imap:messageFrom, cm:originator
    @@ -106,8 +107,7 @@ public class RFC822MetadataExtractor extends AbstractMetadataExtractorEmbedder
             if (mimeMessage != null)
             {
                 /**
    -             * Extract RFC822 values that doesn't match to headers and need to be encoded.
    -             * Or those special fields that require some code to extract data
    +             * Extract RFC822 values that doesn't match to headers and need to be encoded. Or those special fields that require some code to extract data
                  */
                 String tmp = InternetAddress.toString(mimeMessage.getFrom());
                 tmp = tmp != null ? MimeUtility.decodeText(tmp) : null;
    @@ -126,18 +126,11 @@ public class RFC822MetadataExtractor extends AbstractMetadataExtractorEmbedder
                 /**
                  * Received field from RFC 822
                  *
    -             * "Received"    ":"        ; one per relay
    -             *   ["from" domain]        ; sending host
    -             *   ["by"   domain]        ; receiving host
    -             *   ["via"  atom]          ; physical path
    -             *  ("with" atom)           ; link/mail protocol
    -             *   ["id"   msg-id]        ; receiver msg id
    -             *   ["for"  addr-spec]     ; initial form
    -             * ";"    date-time         ; time received
    +             * "Received" ":" ; one per relay ["from" domain] ; sending host ["by" domain] ; receiving host ["via" atom] ; physical path ("with" atom) ; link/mail protocol ["id" msg-id] ; receiver msg id ["for" addr-spec] ; initial form ";" date-time ; time received
                  */
                 Date rxDate = mimeMessage.getReceivedDate();
     
    -            if(rxDate != null)
    +            if (rxDate != null)
                 {
                     // The email implementation extracted the received date for us.
                     putRawValue(KEY_MESSAGE_RECEIVED, rxDate, rawProperties);
    @@ -146,12 +139,12 @@ public class RFC822MetadataExtractor extends AbstractMetadataExtractorEmbedder
                 {
                     // the email implementation did not parse the received date for us.
                     String[] rx = mimeMessage.getHeader("received");
    -                if(rx != null && rx.length > 0)
    +                if (rx != null && rx.length > 0)
                     {
                         String lastReceived = rx[0];
                         lastReceived = MimeUtility.unfold(lastReceived);
                         int x = lastReceived.lastIndexOf(';');
    -                    if(x > 0)
    +                    if (x > 0)
                         {
                             String dateStr = lastReceived.substring(x + 1).trim();
                             putRawValue(KEY_MESSAGE_RECEIVED, dateStr, rawProperties);
    @@ -174,9 +167,7 @@ public class RFC822MetadataExtractor extends AbstractMetadataExtractorEmbedder
                     putRawValue(KEY_MESSAGE_SUBJECT, decodedSubject, rawProperties);
                 }
     
    -            /*
    -             * Extract values from all header fields, including extension fields "X-"
    -             */
    +            /* Extract values from all header fields, including extension fields "X-" */
                 Set keys = getExtractMapping().keySet();
                 Enumeration
    headers = mimeMessage.getAllHeaders(); while (headers.hasMoreElements()) diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/AppleIWorksContentTransformer.java b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/AppleIWorksContentTransformer.java index d6b9de4f..b0233e83 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/AppleIWorksContentTransformer.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/AppleIWorksContentTransformer.java @@ -1,125 +1,122 @@ -/* - * #%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 com.google.common.collect.ImmutableList; -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; -import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; -import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; -import java.util.List; -import java.util.Map; - -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; - -/** - * Converts Apple iWorks files to JPEGs for thumbnailing and previewing. - * The transformer will only work for iWorks 2013/14 files. Support for iWorks 2008/9 has been dropped as we cannot - * support both, because the newer format does not contain a PDF. If we say this transformer supports PDF, Share will - * assume incorrectly that we can convert to PDF and we would only get a preview for the older format and never the - * newer one. Both formats have the same mimetype. - * - *

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

    - * - * @author Neil Mc Erlean - * @author eknizat - * @since 4.0 - */ -@Component -public class AppleIWorksContentTransformer implements CustomTransformerFileAdaptor -{ - private static final Logger logger = LoggerFactory.getLogger( - AppleIWorksContentTransformer.class); - - // Apple's zip entry names for previews in iWorks have changed over time. - private static final List PDF_PATHS = ImmutableList.of( - "QuickLook/Preview.pdf"); // iWorks 2008/9 - private static final List JPG_PATHS = ImmutableList.of( - "QuickLook/Thumbnail.jpg", // iWorks 2008/9 - "preview.jpg"); // iWorks 2013/14 (720 x 552) We use the best quality image. Others are: - // (225 x 173) preview-web.jpg - // (53 x 41) preview-micro.jpg - - @Override - public String getTransformerName() - { - return "appleIWorks"; - } - - @Override - public void transform(String sourceMimetype, String targetMimetype, Map transformOptions, - File sourceFile, File targetFile, TransformManager transformManager) - { - logger.debug("Performing IWorks to jpeg transform with sourceMimetype={} targetMimetype={}", - sourceMimetype, targetMimetype); - - // iWorks files are zip (or package) files. - // If it's not a zip file, the resultant ZipException will be caught as an IOException below. - try (ZipArchiveInputStream iWorksZip = new ZipArchiveInputStream( - new BufferedInputStream(new FileInputStream(sourceFile)))) - { - // Look through the zip file entries for the preview/thumbnail. - List paths = MIMETYPE_IMAGE_JPEG.equals(targetMimetype) ? JPG_PATHS : PDF_PATHS; - ZipArchiveEntry entry; - boolean found = false; - while ((entry = iWorksZip.getNextZipEntry()) != null) - { - String name = entry.getName(); - if (paths.contains(name)) - { - Files.copy(iWorksZip, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - found = true; - break; - } - } - - if (!found) - { - throw new RuntimeException( - "The source " + sourceMimetype + " file did not contain a " + targetMimetype + " preview"); - } - } - catch (IOException e) - { - throw new RuntimeException( - "Unable to transform " + sourceMimetype + " file. It should have been a zip format file.", - e); - } - } -} +/* + * #%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 static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.List; +import java.util.Map; + +import com.google.common.collect.ImmutableList; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; + +/** + * Converts Apple iWorks files to JPEGs for thumbnailing and previewing. The transformer will only work for iWorks 2013/14 files. Support for iWorks 2008/9 has been dropped as we cannot support both, because the newer format does not contain a PDF. If we say this transformer supports PDF, Share will assume incorrectly that we can convert to PDF and we would only get a preview for the older format and never the newer one. Both formats have the same mimetype. + * + *

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

    + * + * @author Neil Mc Erlean + * @author eknizat + * @since 4.0 + */ +@Component +public class AppleIWorksContentTransformer implements CustomTransformerFileAdaptor +{ + private static final Logger logger = LoggerFactory.getLogger( + AppleIWorksContentTransformer.class); + + // Apple's zip entry names for previews in iWorks have changed over time. + private static final List PDF_PATHS = ImmutableList.of( + "QuickLook/Preview.pdf"); // iWorks 2008/9 + private static final List JPG_PATHS = ImmutableList.of( + "QuickLook/Thumbnail.jpg", // iWorks 2008/9 + "preview.jpg"); // iWorks 2013/14 (720 x 552) We use the best quality image. Others are: + // (225 x 173) preview-web.jpg + // (53 x 41) preview-micro.jpg + + @Override + public String getTransformerName() + { + return "appleIWorks"; + } + + @Override + public void transform(String sourceMimetype, String targetMimetype, Map transformOptions, + File sourceFile, File targetFile, TransformManager transformManager) + { + logger.debug("Performing IWorks to jpeg transform with sourceMimetype={} targetMimetype={}", + sourceMimetype, targetMimetype); + + // iWorks files are zip (or package) files. + // If it's not a zip file, the resultant ZipException will be caught as an IOException below. + try (ZipArchiveInputStream iWorksZip = new ZipArchiveInputStream( + new BufferedInputStream(new FileInputStream(sourceFile)))) + { + // Look through the zip file entries for the preview/thumbnail. + List paths = MIMETYPE_IMAGE_JPEG.equals(targetMimetype) ? JPG_PATHS : PDF_PATHS; + ZipArchiveEntry entry; + boolean found = false; + while ((entry = iWorksZip.getNextZipEntry()) != null) + { + String name = entry.getName(); + if (paths.contains(name)) + { + Files.copy(iWorksZip, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + found = true; + break; + } + } + + if (!found) + { + throw new RuntimeException( + "The source " + sourceMimetype + " file did not contain a " + targetMimetype + " preview"); + } + } + catch (IOException e) + { + throw new RuntimeException( + "Unable to transform " + sourceMimetype + " file. It should have been a zip format file.", + e); + } + } +} diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/EMLTransformer.java b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/EMLTransformer.java index ea62be45..794198da 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/EMLTransformer.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/EMLTransformer.java @@ -1,241 +1,237 @@ -/* - * #%L - * Alfresco Transform Core - * %% - * Copyright (C) 2005 - 2023 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.fs.FileManager; -import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import jakarta.mail.MessagingException; -import jakarta.mail.Multipart; -import jakarta.mail.Part; -import jakarta.mail.Session; -import jakarta.mail.internet.MimeMessage; -import java.io.BufferedInputStream; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.util.Map; -import java.util.Properties; - -import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_MULTIPART_ALTERNATIVE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; - -/** - * Uses javax.mail.MimeMessage to generate plain text versions of RFC822 email - * messages. Searches for all text content parts, and returns them. Any - * attachments are ignored. TIKA Note - could be replaced with the Tika email - * parser. Would require a recursing parser to be specified, but not the full - * Auto one (we don't want attachments), just one containing text and html - * related parsers. - * - *

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

    - */ -@Component -public class EMLTransformer implements CustomTransformerFileAdaptor -{ - private static final Logger logger = LoggerFactory.getLogger(EMLTransformer.class); - - private static final String CHARSET = "charset"; - private static final String DEFAULT_ENCODING = "UTF-8"; - - @Override - public String getTransformerName() - { - return "rfc822"; - } - - @Override - public void transform(String sourceMimetype, String targetMimetype, Map transformOptions, - File sourceFile, File targetFile, TransformManager transformManager) throws Exception - { - logger.debug("Performing RFC822 to text transform."); - // Use try with resource - try (InputStream contentInputStream = new BufferedInputStream( - new FileInputStream(sourceFile)); - Writer bufferedFileWriter = new BufferedWriter(new FileWriter(targetFile))) - { - MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()), - contentInputStream); - - final StringBuilder sb = new StringBuilder(); - Object content = mimeMessage.getContent(); - if (content instanceof Multipart) - { - processMultiPart((Multipart) content, sb); - } - else - { - sb.append(content.toString()); - } - bufferedFileWriter.write(sb.toString()); - } - } - - /** - * Find "text" parts of message recursively and appends it to sb StringBuilder - * - * @param multipart Multipart to process - * @param sb StringBuilder - * @throws MessagingException - * @throws IOException - */ - private void processMultiPart(Multipart multipart, StringBuilder sb) throws MessagingException, - IOException - { - boolean isAlternativeMultipart = multipart.getContentType().contains( - MIMETYPE_MULTIPART_ALTERNATIVE); - if (isAlternativeMultipart) - { - processAlternativeMultipart(multipart, sb); - } - else - { - for (int i = 0, n = multipart.getCount(); i < n; i++) - { - Part part = multipart.getBodyPart(i); - if (part.getContent() instanceof Multipart) - { - processMultiPart((Multipart) part.getContent(), sb); - } - else - { - processPart(part, sb); - } - } - } - } - - /** - * Finds the suitable part from an multipart/alternative and appends it's text content to StringBuilder sb - * - * @param multipart - * @param sb - * @throws IOException - * @throws MessagingException - */ - private void processAlternativeMultipart(Multipart multipart, StringBuilder sb) throws - IOException, MessagingException - { - Part partToUse = null; - for (int i = 0, n = multipart.getCount(); i < n; i++) - { - Part part = multipart.getBodyPart(i); - if (part.getContentType().contains(MIMETYPE_TEXT_PLAIN)) - { - partToUse = part; - break; - } - else if (part.getContentType().contains(MIMETYPE_HTML)) - { - partToUse = part; - } - else if (part.getContentType().contains(MIMETYPE_MULTIPART_ALTERNATIVE)) - { - if (part.getContent() instanceof Multipart) - { - processAlternativeMultipart((Multipart) part.getContent(), sb); - } - } - } - if (partToUse != null) - { - processPart(partToUse, sb); - } - } - - /** - * Finds text on a given mail part. Accepted parts types are text/html and text/plain. - * Attachments are ignored - * - * @param part - * @param sb - * @throws IOException - * @throws MessagingException - */ - private void processPart(Part part, StringBuilder sb) throws IOException, MessagingException - { - boolean isAttachment = Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()); - if (isAttachment) - { - return; - } - if (part.getContentType().contains(MIMETYPE_TEXT_PLAIN)) - { - sb.append(part.getContent().toString()); - } - else if (part.getContentType().contains(MIMETYPE_HTML)) - { - String mailPartContent = part.getContent().toString(); - - //create a temporary html file with same mail part content and encoding - File tempHtmlFile = FileManager.TempFileProvider.createTempFile("EMLTransformer_", - ".html"); - String encoding = getMailPartContentEncoding(part); - try (OutputStreamWriter osWriter = new OutputStreamWriter( - new FileOutputStream(tempHtmlFile), encoding)) - { - osWriter.write(mailPartContent); - } - - //transform html file's content to plain text - HtmlParserContentTransformer.EncodingAwareStringBean extractor = new HtmlParserContentTransformer.EncodingAwareStringBean(); - extractor.setCollapse(false); - extractor.setLinks(false); - extractor.setReplaceNonBreakingSpaces(false); - extractor.setURL(tempHtmlFile, encoding); - sb.append(extractor.getStrings()); - - tempHtmlFile.delete(); - } - } - - private String getMailPartContentEncoding(Part part) throws MessagingException - { - String encoding = DEFAULT_ENCODING; - String contentType = part.getContentType(); - int startIndex = contentType.indexOf(CHARSET); - if (startIndex > 0) - { - encoding = contentType.substring(startIndex + CHARSET.length() + 1) - .replaceAll("\"", ""); - } - return encoding; - } -} \ No newline at end of file +/* + * #%L + * Alfresco Transform Core + * %% + * Copyright (C) 2005 - 2023 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.Mimetype.MIMETYPE_HTML; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_MULTIPART_ALTERNATIVE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; + +import java.io.BufferedInputStream; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Map; +import java.util.Properties; +import jakarta.mail.MessagingException; +import jakarta.mail.Multipart; +import jakarta.mail.Part; +import jakarta.mail.Session; +import jakarta.mail.internet.MimeMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.fs.FileManager; +import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; + +/** + * Uses javax.mail.MimeMessage to generate plain text versions of RFC822 email messages. Searches for all text content parts, and returns them. Any attachments are ignored. TIKA Note - could be replaced with the Tika email parser. Would require a recursing parser to be specified, but not the full Auto one (we don't want attachments), just one containing text and html related parsers. + * + *

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

    + */ +@Component +public class EMLTransformer implements CustomTransformerFileAdaptor +{ + private static final Logger logger = LoggerFactory.getLogger(EMLTransformer.class); + + private static final String CHARSET = "charset"; + private static final String DEFAULT_ENCODING = "UTF-8"; + + @Override + public String getTransformerName() + { + return "rfc822"; + } + + @Override + public void transform(String sourceMimetype, String targetMimetype, Map transformOptions, + File sourceFile, File targetFile, TransformManager transformManager) throws Exception + { + logger.debug("Performing RFC822 to text transform."); + // Use try with resource + try (InputStream contentInputStream = new BufferedInputStream( + new FileInputStream(sourceFile)); + Writer bufferedFileWriter = new BufferedWriter(new FileWriter(targetFile))) + { + MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()), + contentInputStream); + + final StringBuilder sb = new StringBuilder(); + Object content = mimeMessage.getContent(); + if (content instanceof Multipart) + { + processMultiPart((Multipart) content, sb); + } + else + { + sb.append(content.toString()); + } + bufferedFileWriter.write(sb.toString()); + } + } + + /** + * Find "text" parts of message recursively and appends it to sb StringBuilder + * + * @param multipart + * Multipart to process + * @param sb + * StringBuilder + * @throws MessagingException + * @throws IOException + */ + private void processMultiPart(Multipart multipart, StringBuilder sb) throws MessagingException, + IOException + { + boolean isAlternativeMultipart = multipart.getContentType().contains( + MIMETYPE_MULTIPART_ALTERNATIVE); + if (isAlternativeMultipart) + { + processAlternativeMultipart(multipart, sb); + } + else + { + for (int i = 0, n = multipart.getCount(); i < n; i++) + { + Part part = multipart.getBodyPart(i); + if (part.getContent() instanceof Multipart) + { + processMultiPart((Multipart) part.getContent(), sb); + } + else + { + processPart(part, sb); + } + } + } + } + + /** + * Finds the suitable part from an multipart/alternative and appends it's text content to StringBuilder sb + * + * @param multipart + * @param sb + * @throws IOException + * @throws MessagingException + */ + private void processAlternativeMultipart(Multipart multipart, StringBuilder sb) throws IOException, MessagingException + { + Part partToUse = null; + for (int i = 0, n = multipart.getCount(); i < n; i++) + { + Part part = multipart.getBodyPart(i); + if (part.getContentType().contains(MIMETYPE_TEXT_PLAIN)) + { + partToUse = part; + break; + } + else if (part.getContentType().contains(MIMETYPE_HTML)) + { + partToUse = part; + } + else if (part.getContentType().contains(MIMETYPE_MULTIPART_ALTERNATIVE)) + { + if (part.getContent() instanceof Multipart) + { + processAlternativeMultipart((Multipart) part.getContent(), sb); + } + } + } + if (partToUse != null) + { + processPart(partToUse, sb); + } + } + + /** + * Finds text on a given mail part. Accepted parts types are text/html and text/plain. Attachments are ignored + * + * @param part + * @param sb + * @throws IOException + * @throws MessagingException + */ + private void processPart(Part part, StringBuilder sb) throws IOException, MessagingException + { + boolean isAttachment = Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()); + if (isAttachment) + { + return; + } + if (part.getContentType().contains(MIMETYPE_TEXT_PLAIN)) + { + sb.append(part.getContent().toString()); + } + else if (part.getContentType().contains(MIMETYPE_HTML)) + { + String mailPartContent = part.getContent().toString(); + + // create a temporary html file with same mail part content and encoding + File tempHtmlFile = FileManager.TempFileProvider.createTempFile("EMLTransformer_", + ".html"); + String encoding = getMailPartContentEncoding(part); + try (OutputStreamWriter osWriter = new OutputStreamWriter( + new FileOutputStream(tempHtmlFile), encoding)) + { + osWriter.write(mailPartContent); + } + + // transform html file's content to plain text + HtmlParserContentTransformer.EncodingAwareStringBean extractor = new HtmlParserContentTransformer.EncodingAwareStringBean(); + extractor.setCollapse(false); + extractor.setLinks(false); + extractor.setReplaceNonBreakingSpaces(false); + extractor.setURL(tempHtmlFile, encoding); + sb.append(extractor.getStrings()); + + tempHtmlFile.delete(); + } + } + + private String getMailPartContentEncoding(Part part) throws MessagingException + { + String encoding = DEFAULT_ENCODING; + String contentType = part.getContentType(); + int startIndex = contentType.indexOf(CHARSET); + if (startIndex > 0) + { + encoding = contentType.substring(startIndex + CHARSET.length() + 1) + .replaceAll("\"", ""); + } + return encoding; + } +} diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/ImageToPdfTransformer.java b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/ImageToPdfTransformer.java index 64515757..c751e431 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/ImageToPdfTransformer.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/ImageToPdfTransformer.java @@ -26,16 +26,14 @@ */ package org.alfresco.transform.misc.transformers; +import static org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants.TIFF_TAG_XRESOLUTION; +import static org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants.TIFF_TAG_YRESOLUTION; + import static org.alfresco.transform.common.RequestParamMap.END_PAGE; import static org.alfresco.transform.common.RequestParamMap.PDF_FORMAT; import static org.alfresco.transform.common.RequestParamMap.PDF_ORIENTATION; import static org.alfresco.transform.common.RequestParamMap.START_PAGE; -import static org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants.TIFF_TAG_XRESOLUTION; -import static org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants.TIFF_TAG_YRESOLUTION; -import javax.imageio.ImageIO; -import javax.imageio.ImageReader; -import javax.imageio.stream.ImageInputStream; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -44,9 +42,10 @@ import java.util.Iterator; import java.util.Map; import java.util.Optional; import java.util.stream.Stream; +import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.stream.ImageInputStream; -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; import org.apache.commons.imaging.Imaging; import org.apache.commons.imaging.ImagingException; import org.apache.commons.imaging.common.ImageMetadata; @@ -63,15 +62,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; + /** - * Converts image files into PDF files. Transformer uses PDF Box to perform conversions. - * During conversion image might be scaled down (keeping proportions) to match width or height of the PDF document. - * If the image is smaller than PDF page size, the image will be placed in the top left-hand side of the PDF document page. - * Transformer accepts bellow optional transform parameters: - * - startPage - page number of image (for multi-page images) from which transformer should start conversion. Default: first page of the image. - * - endPage - page number of image (for multi-page images) up to which transformation should be performed. Default: last page of the image. - * - pdfFormat - output PDF file format. Available formats: DEFAULT, A0, A1, A2, A3, A4, A5, A6, LETTER, LEGAL. Default: original image size. - * - pdfOrientation - output PDF file orientation. Available options: DEFAULT, PORTRAIT, LANDSCAPE. Default: original image orientation. + * Converts image files into PDF files. Transformer uses PDF Box to perform conversions. During conversion image might be scaled down (keeping proportions) to match width or height of the PDF document. If the image is smaller than PDF page size, the image will be placed in the top left-hand side of the PDF document page. Transformer accepts bellow optional transform parameters: - startPage - page number of image (for multi-page images) from which transformer should start conversion. Default: first page of the image. - endPage - page number of image (for multi-page images) up to which transformation should be performed. Default: last page of the image. - pdfFormat - output PDF file format. Available formats: DEFAULT, A0, A1, A2, A3, A4, A5, A6, LETTER, LEGAL. Default: original image size. - pdfOrientation - output PDF file orientation. Available options: DEFAULT, PORTRAIT, LANDSCAPE. Default: original image orientation. */ @Component public class ImageToPdfTransformer implements CustomTransformerFileAdaptor @@ -95,13 +90,13 @@ public class ImageToPdfTransformer implements CustomTransformerFileAdaptor @Override public void transform( - String sourceMimetype, String targetMimetype, Map transformOptions, - File imageFile, File pdfFile, TransformManager transformManager - ) throws Exception { + String sourceMimetype, String targetMimetype, Map transformOptions, + File imageFile, File pdfFile, TransformManager transformManager) throws Exception + { try ( - ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageFile); - PDDocument pdfDocument = new PDDocument() - ) { + ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageFile); + PDDocument pdfDocument = new PDDocument()) + { final Integer startPage = parseOptionIfPresent(transformOptions, START_PAGE, Integer.class).orElse(null); final Integer endPage = parseOptionIfPresent(transformOptions, END_PAGE, Integer.class).orElse(null); final String pdfFormat = parseOptionIfPresent(transformOptions, PDF_FORMAT, String.class).orElse(DEFAULT_PDF_FORMAT_STRING); @@ -142,7 +137,7 @@ public class ImageToPdfTransformer implements CustomTransformerFileAdaptor } private void scaleAndDrawImage(final PDDocument pdfDocument, final BufferedImage bufferedImage, final String pdfFormat, final String pdfOrientation, final Map resolution) - throws IOException + throws IOException { final PDImageXObject image = LosslessFactory.createFromImage(pdfDocument, bufferedImage); @@ -150,10 +145,10 @@ public class ImageToPdfTransformer implements CustomTransformerFileAdaptor int imageHeight = image.getHeight(); // if the image has a resolution which differs from pdfbox then adjust size in pixels according to pdfbox ppi if (resolution.get("X") > 0 && resolution.get("X") != PDFBOX_POINTS_PER_INCH && - resolution.get("Y") > 0 && resolution.get("Y") != PDFBOX_POINTS_PER_INCH) + resolution.get("Y") > 0 && resolution.get("Y") != PDFBOX_POINTS_PER_INCH) { - imageWidth = (int)(((float)imageWidth / resolution.get("X")) * PDFBOX_POINTS_PER_INCH); - imageHeight = (int)(((float)imageHeight / resolution.get("Y")) * PDFBOX_POINTS_PER_INCH); + imageWidth = (int) (((float) imageWidth / resolution.get("X")) * PDFBOX_POINTS_PER_INCH); + imageHeight = (int) (((float) imageHeight / resolution.get("Y")) * PDFBOX_POINTS_PER_INCH); } final PDPage pdfPage = new PDPage(resolvePdfFormat(pdfFormat, pdfOrientation, imageWidth, imageHeight)); diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/OOXMLThumbnailContentTransformer.java b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/OOXMLThumbnailContentTransformer.java index 779a6296..5620a214 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/OOXMLThumbnailContentTransformer.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/OOXMLThumbnailContentTransformer.java @@ -1,139 +1,112 @@ -/* - * #%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.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.openxml4j.opc.PackagePart; -import org.apache.poi.openxml4j.opc.PackageRelationship; -import org.apache.poi.openxml4j.opc.PackageRelationshipCollection; -import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; -import java.util.Map; - -/** - * Extracts out Thumbnail JPEGs from OOXML files for thumbnailing and previewing. - * This transformer will only work for OOXML files where thumbnailing was enabled, - * which isn't on by default on Windows, but is more common on Mac. - * - *

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

    - * - * @author Nick Burch - * @author eknizat - */ -@Component -public class OOXMLThumbnailContentTransformer implements CustomTransformerFileAdaptor -{ - private static final Logger logger = LoggerFactory.getLogger( - OOXMLThumbnailContentTransformer.class); - - public String getTransformerName() - { - return "ooXmlThumbnail"; - } - - @Override - public void transform(final String sourceMimetype, final String targetMimetype, final Map parameters, - final File sourceFile, final File targetFile, TransformManager transformManager) throws Exception - { - if (logger.isDebugEnabled()) - { - logger.debug("Performing OOXML to jpeg transform with sourceMimetype=" + sourceMimetype - + " targetMimetype=" + targetMimetype); - } - - try (OPCPackage pkg = OPCPackage.open(sourceFile.getPath())) - { - - // Does it have a thumbnail? - PackageRelationshipCollection rels = pkg.getRelationshipsByType( - PackageRelationshipTypes.THUMBNAIL); - if (rels.size() > 0) - { - // Get the thumbnail part - PackageRelationship tRel = rels.getRelationship(0); - PackagePart tPart = pkg.getPart(tRel); - - // Write it to the target - InputStream tStream = tPart.getInputStream(); - Files.copy(tStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - tStream.close(); - } - else - { - logger.debug("No thumbnail present in file."); - throw new Exception( - "No thumbnail present in file, unable to generate " + targetMimetype); - } - } - catch (IOException e) - { - throw new RuntimeException("Unable to transform file.", e); - } - } - - /* - // TODO Add this back to engine_config.json when the transformer is fixed for java 11 - { - "transformerName": "ooxmlThumbnail", - "supportedSourceAndTargetList": [ - {"sourceMediaType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-word.document.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.openxmlformats-officedocument.wordprocessingml.template", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-word.template.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.openxmlformats-officedocument.presentationml.presentation", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-powerpoint.presentation.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.openxmlformats-officedocument.presentationml.slideshow", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-powerpoint.slideshow.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.openxmlformats-officedocument.presentationml.template", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-powerpoint.template.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-powerpoint.addin.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.openxmlformats-officedocument.presentationml.slide", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-powerpoint.slide.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.openxmlformats-officedocument.spreadsheetml.template", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-excel.sheet.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-excel.template.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-excel.addin.macroenabled.12", "targetMediaType": "image/jpeg"}, - {"sourceMediaType": "application/vnd.ms-excel.sheet.binary.macroenabled.12", "targetMediaType": "image/jpeg"} - ], - "transformOptions": [ - ] - } - */ -} +/* + * #%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 java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.Map; + +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.opc.PackagePart; +import org.apache.poi.openxml4j.opc.PackageRelationship; +import org.apache.poi.openxml4j.opc.PackageRelationshipCollection; +import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; + +/** + * Extracts out Thumbnail JPEGs from OOXML files for thumbnailing and previewing. This transformer will only work for OOXML files where thumbnailing was enabled, which isn't on by default on Windows, but is more common on Mac. + * + *

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

    + * + * @author Nick Burch + * @author eknizat + */ +@Component +public class OOXMLThumbnailContentTransformer implements CustomTransformerFileAdaptor +{ + private static final Logger logger = LoggerFactory.getLogger( + OOXMLThumbnailContentTransformer.class); + + public String getTransformerName() + { + return "ooXmlThumbnail"; + } + + @Override + public void transform(final String sourceMimetype, final String targetMimetype, final Map parameters, + final File sourceFile, final File targetFile, TransformManager transformManager) throws Exception + { + if (logger.isDebugEnabled()) + { + logger.debug("Performing OOXML to jpeg transform with sourceMimetype=" + sourceMimetype + + " targetMimetype=" + targetMimetype); + } + + try (OPCPackage pkg = OPCPackage.open(sourceFile.getPath())) + { + + // Does it have a thumbnail? + PackageRelationshipCollection rels = pkg.getRelationshipsByType( + PackageRelationshipTypes.THUMBNAIL); + if (rels.size() > 0) + { + // Get the thumbnail part + PackageRelationship tRel = rels.getRelationship(0); + PackagePart tPart = pkg.getPart(tRel); + + // Write it to the target + InputStream tStream = tPart.getInputStream(); + Files.copy(tStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + tStream.close(); + } + else + { + logger.debug("No thumbnail present in file."); + throw new Exception( + "No thumbnail present in file, unable to generate " + targetMimetype); + } + } + catch (IOException e) + { + throw new RuntimeException("Unable to transform file.", e); + } + } + + /* // TODO Add this back to engine_config.json when the transformer is fixed for java 11 { "transformerName": "ooxmlThumbnail", "supportedSourceAndTargetList": [ {"sourceMediaType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-word.document.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.openxmlformats-officedocument.wordprocessingml.template", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-word.template.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.openxmlformats-officedocument.presentationml.presentation", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-powerpoint.presentation.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.openxmlformats-officedocument.presentationml.slideshow", "targetMediaType": + * "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-powerpoint.slideshow.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.openxmlformats-officedocument.presentationml.template", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-powerpoint.template.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-powerpoint.addin.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.openxmlformats-officedocument.presentationml.slide", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-powerpoint.slide.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.openxmlformats-officedocument.spreadsheetml.template", "targetMediaType": "image/jpeg"}, {"sourceMediaType": + * "application/vnd.ms-excel.sheet.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-excel.template.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-excel.addin.macroenabled.12", "targetMediaType": "image/jpeg"}, {"sourceMediaType": "application/vnd.ms-excel.sheet.binary.macroenabled.12", "targetMediaType": "image/jpeg"} ], "transformOptions": [ ] } */ +} diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/StringExtractingContentTransformer.java b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/StringExtractingContentTransformer.java index 65147efe..0355ef9c 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/StringExtractingContentTransformer.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/StringExtractingContentTransformer.java @@ -1,166 +1,178 @@ -/* - * #%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.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Reader; -import java.io.Writer; -import java.nio.charset.Charset; -import java.nio.charset.IllegalCharsetNameException; -import java.util.Map; - -import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; -import static org.alfresco.transform.common.RequestParamMap.TARGET_ENCODING; - -/** - * Converts any textual format to plain text. - *

    - * The transformation is sensitive to the source and target string encodings. - * - * - *

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

    - * - * @author Derek Hulley - * @author eknizat - */ -@Component -public class StringExtractingContentTransformer implements CustomTransformerFileAdaptor -{ - private static final Logger logger = LoggerFactory.getLogger(StringExtractingContentTransformer.class); - - public String getTransformerName() - { - return "string"; - } - - /** - * Text to text conversions are done directly using the content reader and writer string - * manipulation methods. - *

    - * Extraction of text from binary content attempts to take the possible character - * encoding into account. The text produced from this will, if the encoding was correct, - * be unformatted but valid. - */ - @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); - String targetEncoding = transformOptions.get(TARGET_ENCODING); - - if (logger.isDebugEnabled()) - { - logger.debug("Performing text to text transform with sourceEncoding=" + sourceEncoding - + " targetEncoding=" + targetEncoding); - } - - Reader charReader = null; - Writer charWriter = null; - try - { - // Build reader - if (sourceEncoding == null) - { - charReader = new BufferedReader( - new InputStreamReader(new FileInputStream(sourceFile))); - } - else - { - checkEncodingParameter(sourceEncoding, SOURCE_ENCODING); - charReader = new BufferedReader( - new InputStreamReader(new FileInputStream(sourceFile), sourceEncoding)); - } - - // Build writer - if (targetEncoding == null) - { - charWriter = new BufferedWriter( - new OutputStreamWriter(new FileOutputStream(targetFile))); - } - else - { - checkEncodingParameter(targetEncoding, TARGET_ENCODING); - charWriter = new BufferedWriter( - new OutputStreamWriter(new FileOutputStream(targetFile), targetEncoding)); - } - - // copy from the one to the other - char[] buffer = new char[8192]; - int readCount = 0; - while (readCount > -1) - { - // write the last read count number of bytes - charWriter.write(buffer, 0, readCount); - // fill the buffer again - readCount = charReader.read(buffer); - } - } - finally - { - if (charReader != null) - { - try { charReader.close(); } catch (Throwable e) { logger.error("Failed to close charReader", e); } - } - if (charWriter != null) - { - try { charWriter.close(); } catch (Throwable e) { logger.error("Failed to close charWriter", e); } - } - } - // done - } - - private void checkEncodingParameter(String encoding, String paramterName) - { - try - { - if (!Charset.isSupported(encoding)) - { - throw new IllegalArgumentException( - paramterName + "=" + encoding + " is not supported by the JVM."); - } - } - catch (IllegalCharsetNameException e) - { - throw new IllegalArgumentException( - paramterName + "=" + encoding + " is not a valid encoding."); - } - } -} +/* + * #%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 static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; +import static org.alfresco.transform.common.RequestParamMap.TARGET_ENCODING; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.charset.IllegalCharsetNameException; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; + +/** + * Converts any textual format to plain text. + *

    + * The transformation is sensitive to the source and target string encodings. + * + * + *

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

    + * + * @author Derek Hulley + * @author eknizat + */ +@Component +public class StringExtractingContentTransformer implements CustomTransformerFileAdaptor +{ + private static final Logger logger = LoggerFactory.getLogger(StringExtractingContentTransformer.class); + + public String getTransformerName() + { + return "string"; + } + + /** + * Text to text conversions are done directly using the content reader and writer string manipulation methods. + *

    + * Extraction of text from binary content attempts to take the possible character encoding into account. The text produced from this will, if the encoding was correct, be unformatted but valid. + */ + @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); + String targetEncoding = transformOptions.get(TARGET_ENCODING); + + if (logger.isDebugEnabled()) + { + logger.debug("Performing text to text transform with sourceEncoding=" + sourceEncoding + + " targetEncoding=" + targetEncoding); + } + + Reader charReader = null; + Writer charWriter = null; + try + { + // Build reader + if (sourceEncoding == null) + { + charReader = new BufferedReader( + new InputStreamReader(new FileInputStream(sourceFile))); + } + else + { + checkEncodingParameter(sourceEncoding, SOURCE_ENCODING); + charReader = new BufferedReader( + new InputStreamReader(new FileInputStream(sourceFile), sourceEncoding)); + } + + // Build writer + if (targetEncoding == null) + { + charWriter = new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(targetFile))); + } + else + { + checkEncodingParameter(targetEncoding, TARGET_ENCODING); + charWriter = new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(targetFile), targetEncoding)); + } + + // copy from the one to the other + char[] buffer = new char[8192]; + int readCount = 0; + while (readCount > -1) + { + // write the last read count number of bytes + charWriter.write(buffer, 0, readCount); + // fill the buffer again + readCount = charReader.read(buffer); + } + } + finally + { + if (charReader != null) + { + try + { + charReader.close(); + } + catch (Throwable e) + { + logger.error("Failed to close charReader", e); + } + } + if (charWriter != null) + { + try + { + charWriter.close(); + } + catch (Throwable e) + { + logger.error("Failed to close charWriter", e); + } + } + } + // done + } + + private void checkEncodingParameter(String encoding, String paramterName) + { + try + { + if (!Charset.isSupported(encoding)) + { + throw new IllegalArgumentException( + paramterName + "=" + encoding + " is not supported by the JVM."); + } + } + catch (IllegalCharsetNameException e) + { + throw new IllegalArgumentException( + paramterName + "=" + encoding + " is not a valid encoding."); + } + } +} diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformer.java b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformer.java index c8643d0d..483f98b7 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformer.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformer.java @@ -1,786 +1,782 @@ -/* - * #%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 static org.alfresco.transform.common.RequestParamMap.PAGE_LIMIT; -import static org.alfresco.transform.common.RequestParamMap.PDF_FONT; -import static org.alfresco.transform.common.RequestParamMap.PDF_FONT_SIZE; -import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; - -import java.io.BufferedOutputStream; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.PushbackInputStream; -import java.io.Reader; -import java.net.URI; -import java.nio.charset.Charset; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; -import org.apache.fontbox.ttf.TrueTypeFont; -import org.apache.fontbox.util.autodetect.FontFileFinder; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.pdmodel.PDPage; -import org.apache.pdfbox.pdmodel.PDPageContentStream; -import org.apache.pdfbox.pdmodel.font.FontMappers; -import org.apache.pdfbox.pdmodel.font.FontMapping; -import org.apache.pdfbox.pdmodel.font.PDFont; -import org.apache.pdfbox.pdmodel.font.PDType0Font; -import org.apache.pdfbox.pdmodel.font.PDType1Font; -import org.apache.pdfbox.tools.TextToPDF; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -import jakarta.annotation.PostConstruct; - -/** - *

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

    - * - * Makes use of the PDFBox library's TextToPDF utility. - * - * @author Derek Hulley - * @author eknizat - */ -@Component -public class TextToPdfContentTransformer implements CustomTransformerFileAdaptor -{ - private static final Logger logger = LoggerFactory.getLogger(TextToPdfContentTransformer.class); - - private static final int UTF16_READ_AHEAD_BYTES = 16; // 8 characters including BOM if it exists - private static final byte FE = (byte) 0xFE; - private static final byte FF = (byte) 0xFF; - private static final int UTF8_READ_AHEAD_BYTES = 3; - private static final byte EF = (byte) 0xEF; - private static final byte BB = (byte) 0xBB; - private static final byte BF = (byte) 0xBF; - private static final String DEFAULT_FONT = "NotoSans-Regular"; - private static final int DEFAULT_FONT_SIZE = 10; - - private final PagedTextToPDF transformer; - - @Value("${transform.core.misc.pdfBox.defaultFont:NotoSans-Regular}") - private String pdfBoxDefaultFont; - - public TextToPdfContentTransformer() - { - transformer = new PagedTextToPDF(); - } - - @PostConstruct - public void init() - { - transformer.setDefaultFont(pdfBoxDefaultFont); - } - - public void setStandardFont(String fontName) - { - try - { - transformer.setFont(fontName); - } - catch (Throwable e) - { - throw new RuntimeException( - "Unable to set Standard Font for PDF generation: " + fontName, e); - } - } - - public void setFontSize(int fontSize) - { - try - { - transformer.setFontSize(fontSize); - } - catch (Throwable e) - { - throw new RuntimeException( - "Unable to set Font Size for PDF generation: " + fontSize); - } - } - - public String getUsedFont() - { - return transformer.getFontName(); - } - - @Override - public String getTransformerName() - { - return "textToPdf"; - } - - @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); - String stringPageLimit = transformOptions.get(PAGE_LIMIT); - int pageLimit = -1; - if (stringPageLimit != null) - { - pageLimit = parseInt(stringPageLimit, PAGE_LIMIT); - } - String pdfFont = transformOptions.get(PDF_FONT); - if (pdfFont == null || pdfFont.isBlank()) - { - pdfFont = pdfBoxDefaultFont; - } - String pdfFontSize = transformOptions.get(PDF_FONT_SIZE); - Integer fontSize = null; - if (pdfFontSize != null && !pdfFontSize.isBlank()) - { - try - { - fontSize = parseInt(pdfFontSize, PDF_FONT_SIZE); - } - catch (Exception e) - { - fontSize = DEFAULT_FONT_SIZE; - logger.error("Error parsing font size {}, going to set it as {}", pdfFontSize, fontSize, e); - } - } - - PDDocument pdf = null; - try (InputStream is = new FileInputStream(sourceFile); - Reader ir = new BufferedReader(buildReader(is, sourceEncoding)); - OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile))) - { - //TransformationOptionLimits limits = getLimits(reader, writer, options); - //TransformationOptionPair pageLimits = limits.getPagesPair(); - pdf = transformer.createPDFFromText(ir, pageLimit, pdfFont, fontSize); - pdf.save(os); - } - finally - { - if (pdf != null) - { - try { pdf.close(); } catch (Throwable e) {e.printStackTrace(); } - } - } - } - - protected InputStreamReader buildReader(InputStream is, String encoding) - { - // If they gave an encoding, try to use it - if (encoding != null) - { - Charset charset = null; - try - { - charset = Charset.forName(encoding); - } - catch (Exception e) - { - logger.warn("JVM doesn't understand encoding '" + encoding + - "' when transforming text to pdf"); - } - if (charset != null) - { - // Handles the situation where there is a BOM even though the encoding indicates that normally - // there should not be one for UTF-16BE and UTF-16LE. For extra flexibility includes UTF-16 too - // which optionally has the BOM. Rather than look at the BOM we look at the number of zero bytes - // in the first few character. XML files even when not in European languages tend to have more - // even zero bytes when big-endian encoded and more odd zero bytes when little-endian. - // Think of: The normal Java decoder does not have this flexibility but - // other transformers do. - String name = charset.displayName(); - if ("UTF-16".equals(name) || "UTF-16BE".equals(name) || "UTF-16LE".equals(name)) - { - logger.debug("Handle big and little endian UTF-16 text. Using UTF-16 rather than encoding " + name); - charset = Charset.forName("UTF-16"); - is = handleUTF16BOM(is); - } - else if ("UTF-8".equals(name)) - { - logger.debug("Using UTF-8"); - charset = Charset.forName("UTF-8"); - is = handleUTF8BOM(is); - } - logger.debug("Processing plain text in encoding " + name); - return new InputStreamReader(is, charset); - } - } - - // Fall back on the system default - logger.debug("Processing plain text using system default encoding"); - return new InputStreamReader(is); - } - - private static class PagedTextToPDF extends TextToPDF - { - // REPO-1066: duplicating the following lines from org.apache.pdfbox.tools.TextToPDF because they made them private - // before the upgrade to pdfbox 2.0.8, in pdfbox 1.8, this piece of code was public in org.apache.pdfbox.pdmodel.font.PDType1Font - static PDType1Font getStandardFont(String name) - { - return STANDARD_14.get(name); - } - - private static final Map STANDARD_14 = new HashMap<>(); - - static - { - STANDARD_14.put(PDType1Font.TIMES_ROMAN.getBaseFont(), PDType1Font.TIMES_ROMAN); - STANDARD_14.put(PDType1Font.TIMES_BOLD.getBaseFont(), PDType1Font.TIMES_BOLD); - STANDARD_14.put(PDType1Font.TIMES_ITALIC.getBaseFont(), PDType1Font.TIMES_ITALIC); - STANDARD_14.put(PDType1Font.TIMES_BOLD_ITALIC.getBaseFont(), - PDType1Font.TIMES_BOLD_ITALIC); - STANDARD_14.put(PDType1Font.HELVETICA.getBaseFont(), PDType1Font.HELVETICA); - STANDARD_14.put(PDType1Font.HELVETICA_BOLD.getBaseFont(), PDType1Font.HELVETICA_BOLD); - STANDARD_14.put(PDType1Font.HELVETICA_OBLIQUE.getBaseFont(), - PDType1Font.HELVETICA_OBLIQUE); - STANDARD_14.put(PDType1Font.HELVETICA_BOLD_OBLIQUE.getBaseFont(), - PDType1Font.HELVETICA_BOLD_OBLIQUE); - STANDARD_14.put(PDType1Font.COURIER.getBaseFont(), PDType1Font.COURIER); - STANDARD_14.put(PDType1Font.COURIER_BOLD.getBaseFont(), PDType1Font.COURIER_BOLD); - STANDARD_14.put(PDType1Font.COURIER_OBLIQUE.getBaseFont(), PDType1Font.COURIER_OBLIQUE); - STANDARD_14.put(PDType1Font.COURIER_BOLD_OBLIQUE.getBaseFont(), - PDType1Font.COURIER_BOLD_OBLIQUE); - STANDARD_14.put(PDType1Font.SYMBOL.getBaseFont(), PDType1Font.SYMBOL); - STANDARD_14.put(PDType1Font.ZAPF_DINGBATS.getBaseFont(), PDType1Font.ZAPF_DINGBATS); - } - //duplicating until here - - private String fontName = null; - private String defaultFont = null; - - // The following code is based on the code in TextToPDF with the addition of - // checks for page limits. - // The calling code must close the PDDocument once finished with it. - public PDDocument createPDFFromText(Reader text, int pageLimit, String pdfFontName, Integer pdfFontSize) - throws IOException - { - PDDocument doc = null; - int pageCount = 0; - try - { - doc = new PDDocument(); - - final PDFont font = getFont(doc, pdfFontName); - final int fontSize = pdfFontSize != null ? pdfFontSize : getFontSize(); - - fontName = font.getName(); - - logger.debug("Going to use font {} with size {}", fontName, fontSize); - - final int margin = 40; - float height = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000; - - //calculate font height and increase by 5 percent. - height = height * fontSize * 1.05f; - - BufferedReader data = (text instanceof BufferedReader) ? (BufferedReader) text : new BufferedReader(text); - String nextLine; - PDPage page = new PDPage(); - PDPageContentStream contentStream = null; - float y = -1; - float maxStringLength = page.getMediaBox().getWidth() - 2 * margin; - - // There is a special case of creating a PDF document from an empty string. - boolean textIsEmpty = true; - - outer: - while ((nextLine = data.readLine()) != null) - { - // The input text is nonEmpty. New pages will be created and added - // to the PDF document as they are needed, depending on the length of - // the text. - textIsEmpty = false; - - String[] lineWords = nextLine.trim().split(" "); - int lineIndex = 0; - while (lineIndex < lineWords.length) - { - final StringBuilder nextLineToDraw = new StringBuilder(); - float lengthIfUsingNextWord = 0; - do - { - nextLineToDraw.append(lineWords[lineIndex]); - nextLineToDraw.append(" "); - lineIndex++; - if (lineIndex < lineWords.length) - { - String lineWithNextWord = nextLineToDraw.toString() + lineWords[lineIndex]; - lengthIfUsingNextWord = - (font.getStringWidth( - lineWithNextWord) / 1000) * fontSize; - } - } - while (lineIndex < lineWords.length && - lengthIfUsingNextWord < maxStringLength); - if (y < margin) - { - int test = pageCount + 1; - if (pageLimit > 0 && (pageCount++ >= pageLimit)) - { - break outer; - } - - // We have crossed the end-of-page boundary and need to extend the - // document by another page. - page = new PDPage(); - doc.addPage(page); - if (contentStream != null) - { - contentStream.endText(); - contentStream.close(); - } - contentStream = new PDPageContentStream(doc, page); - contentStream.setFont(font, fontSize); - contentStream.beginText(); - y = page.getMediaBox().getHeight() - margin + height; - contentStream.moveTextPositionByAmount(margin, y); - } - - if (contentStream == null) - { - throw new IOException("Error:Expected non-null content stream."); - } - contentStream.moveTextPositionByAmount(0, -height); - y -= height; - contentStream.drawString(nextLineToDraw.toString()); - } - } - - // If the input text was the empty string, then the above while loop will have short-circuited - // and we will not have added any PDPages to the document. - // So in order to make the resultant PDF document readable by Adobe Reader etc, we'll add an empty page. - if (textIsEmpty) - { - doc.addPage(page); - } - - if (contentStream != null) - { - contentStream.endText(); - contentStream.close(); - } - } - catch (IOException io) - { - if (doc != null) - { - doc.close(); - } - throw io; - } - return doc; - } - - public void setFont(String aFontName) - { - PDType1Font font = PagedTextToPDF.getStandardFont(aFontName); - - if (font != null) - { - super.setFont(font); - this.fontName = aFontName; - } - } - - /** - * Gets the font that will be used in document transformation using the following approaches: - *
      - *
    1. Standard font map - *
    2. Font Mappers - *
    3. File system fonts - *
    4. Transformer default font - *
    5. PdfBox default font - *
    - * - * @param doc - * the document that will be transformed - * @param fontName - * the font name that will be used in transformation - * - * @return the font that was found - */ - private PDFont getFont(PDDocument doc, String fontName) - { - if (fontName == null) - { - fontName = fontName != null ? fontName : getDefaultFont(); - } - - // First, it tries to get the font from PdfBox STANDARD_14 map - PDFont font = getFromStandardFonts(fontName); - - // If not found, tries to get the font from FontMappers - if (font == null) - { - font = getFromFontMapper(fontName, doc); - - // If still not found, tries to get the font from file system - if (font == null) - { - font = getFromFileSystem(fontName); - - // If font is still null: - // - it will recursively get the transformer default font - // - Otherwise, it will use the PdfBox default font (Helvetica) - if (font == null) - { - if (defaultFont != null && !fontName.equals(defaultFont)) - { - font = getFont(doc, defaultFont); - } - else - { - font = getFont(); - } - } - } - - } - - return font; - } - - /** - * Gets the font from PdfBox standard fonts map - * - * @param fontName - * the font name to obtain - * - * @return the font object that has been found, otherwise null - */ - private PDFont getFromStandardFonts(String fontName) - { - return PagedTextToPDF.getStandardFont(fontName); - } - - /** - * Gets the font from {@link FontMappers} instance - * - * @param fontName - * the font name to obtain - * @param doc - * the PDF document - * - * @return the font object that has been found, otherwise null - */ - private PDFont getFromFontMapper(String fontName, PDDocument doc) - { - PDFont font = null; - FontMapping mapping = FontMappers.instance().getTrueTypeFont(fontName, null); - - if (mapping != null && mapping.getFont() != null && !mapping.isFallback()) - { - try - { - font = PDType0Font.load(doc, mapping.getFont().getOriginalData()); - } - catch (Exception e) - { - logger.error("Error loading font mapping {}", fontName, e); - } - } - - return font; - } - - /** - * Gets the font from existing file system fonts - * - * @param fontName - * the font name to obtain - * @return the font object that has been found, otherwise null - */ - private PDFont getFromFileSystem(String fontName) - { - PDFont font = null; - String nameWithExtension = fontName + ".ttf"; - - FontFileFinder fontFileFinder = new FontFileFinder(); - List uris = fontFileFinder.find(); - - for (URI uri : uris) - { - if (uri.getPath().contains(nameWithExtension)) - { - InputStream fontIS = null; - try - { - fontIS = new FileInputStream(new File(uri)); - if (null != fontIS) - { - PDDocument documentMock = new PDDocument(); - font = PDType0Font.load(documentMock, fontIS); - break; - } - } - catch (IOException ioe) - { - logger.error("Error loading font {} from filesystem", fontName, ioe); - } - finally - { - if (fontIS != null) - { - try - { - fontIS.close(); - } - catch (Exception e) - { - logger.error("Error closing font inputstream", e); - } - } - } - } - } - - return font; - } - - public String getFontName() - { - return this.fontName; - } - - public String getDefaultFont() - { - if (defaultFont == null || defaultFont.isBlank()) - { - return TextToPdfContentTransformer.DEFAULT_FONT; - } - - return defaultFont; - } - - public void setDefaultFont(String name) - { - if (name == null || name.isBlank()) - { - defaultFont = TextToPdfContentTransformer.DEFAULT_FONT; - } - else - { - this.defaultFont = name; - } - } - } - - private int parseInt(String s, String paramName) - { - try - { - return Integer.valueOf(s); - } - catch (NumberFormatException e) - { - throw new IllegalArgumentException(paramName + " parameter must be an integer."); - } - } - - /** - * Skips the BOM character for UTF-8 encoding - */ - private InputStream handleUTF8BOM(InputStream is) - { - return new PushbackInputStream(is, UTF8_READ_AHEAD_BYTES) - { - boolean bomRead; - - @Override - public int read(byte[] bytes, int off, int len) throws IOException - { - int i = 0; - int b = 0; - for (; i < len; i++) - { - b = read(); - if (b == -1) - { - break; - } - bytes[off + i] = (byte) b; - } - return i == 0 && b == -1 ? -1 : i; - } - - @Override - public int read() throws IOException - { - if (!bomRead) - { - bomRead = true; - byte[] bytes = new byte[UTF8_READ_AHEAD_BYTES]; - int end = in.read(bytes, 0, UTF8_READ_AHEAD_BYTES); - - if (bytes[0] == EF && bytes[1] == BB && bytes[2] == BF) - { - logger.warn("UTF-8 BOM detected, it will be skipped"); - } - else - { - for (int i = end - 1; i >= 0; i--) - { - unread(bytes[i]); - } - } - } - - return super.read(); - } - }; - } - - /** - * Handles the situation where there is a BOM even though the encoding indicates that normally there should not be - * one for UTF-16BE and UTF-16LE. For extra flexibility includes UTF-16 too which optionally has the BOM. Rather - * than look at the BOM we look at the number of zero bytes in the first few character. XML files even when not in - * European languages tend to have more even zero bytes when big-endian encoded and more odd zero bytes when - * little-endian. Think of: The normal Java decoder does not have this flexibility but other - * transformers do. - */ - private InputStream handleUTF16BOM(InputStream is) - { - return new PushbackInputStream(is, UTF16_READ_AHEAD_BYTES) - { - boolean bomRead; - boolean switchByteOrder; - boolean evenByte = true; - - @Override - public int read(byte[] bytes, int off, int len) throws IOException - { - int i = 0; - int b = 0; - for (; i < len; i++) - { - b = read(); - if (b == -1) - { - break; - } - bytes[off + i] = (byte) b; - } - return i == 0 && b == -1 ? -1 : i; - } - - @Override - public int read() throws IOException - { - if (!bomRead) - { - bomRead = true; - boolean switchBom = false; - byte[] bytes = new byte[UTF16_READ_AHEAD_BYTES]; - int end = in.read(bytes, 0, UTF16_READ_AHEAD_BYTES); - int evenZeros = countZeros(bytes, 0); - int oddZeros = countZeros(bytes, 1); - if (evenZeros > oddZeros) - { - if (bytes[0] == FF && bytes[1] == FE) - { - switchByteOrder = true; - switchBom = true; - logger.warn("Little-endian BOM FFFE read, but characters are big-endian"); - } - else - { - logger.debug("More even zero bytes, so normal read for big-endian"); - } - } - else - { - if (bytes[0] == FE && bytes[1] == FF) - { - switchBom = true; - logger.debug("Big-endian BOM FEFF read, but characters are little-endian"); - } - else - { - switchByteOrder = true; - logger.debug("More odd zero bytes, so switch bytes from little-endian"); - } - } - - if (switchBom) - { - byte b = bytes[0]; - bytes[0] = bytes[1]; - bytes[1] = b; - } - - for (int i = end - 1; i >= 0; i--) - { - unread(bytes[i]); - } - } - - if (switchByteOrder) - { - if (evenByte) - { - int b1 = super.read(); - int b2 = super.read(); - if (b1 != -1) - { - unread(b1); - } - if (b2 != -1) - { - unread(b2); - } - } - evenByte = !evenByte; - } - - return super.read(); - } - - // Counts the number of even or odd 00 bytes - private int countZeros(byte[] b, int offset) - { - int count = 0; - for (int i = offset; i < UTF16_READ_AHEAD_BYTES; i += 2) - { - if (b[i] == 0) - { - count++; - } - } - return count; - } - }; - } -} +/* + * #%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 static org.alfresco.transform.common.RequestParamMap.PAGE_LIMIT; +import static org.alfresco.transform.common.RequestParamMap.PDF_FONT; +import static org.alfresco.transform.common.RequestParamMap.PDF_FONT_SIZE; +import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PushbackInputStream; +import java.io.Reader; +import java.net.URI; +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import jakarta.annotation.PostConstruct; + +import org.apache.fontbox.ttf.TrueTypeFont; +import org.apache.fontbox.util.autodetect.FontFileFinder; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PDPageContentStream; +import org.apache.pdfbox.pdmodel.font.FontMappers; +import org.apache.pdfbox.pdmodel.font.FontMapping; +import org.apache.pdfbox.pdmodel.font.PDFont; +import org.apache.pdfbox.pdmodel.font.PDType0Font; +import org.apache.pdfbox.pdmodel.font.PDType1Font; +import org.apache.pdfbox.tools.TextToPDF; +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; + +/** + *

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

    + * + * Makes use of the PDFBox library's TextToPDF utility. + * + * @author Derek Hulley + * @author eknizat + */ +@Component +public class TextToPdfContentTransformer implements CustomTransformerFileAdaptor +{ + private static final Logger logger = LoggerFactory.getLogger(TextToPdfContentTransformer.class); + + private static final int UTF16_READ_AHEAD_BYTES = 16; // 8 characters including BOM if it exists + private static final byte FE = (byte) 0xFE; + private static final byte FF = (byte) 0xFF; + private static final int UTF8_READ_AHEAD_BYTES = 3; + private static final byte EF = (byte) 0xEF; + private static final byte BB = (byte) 0xBB; + private static final byte BF = (byte) 0xBF; + private static final String DEFAULT_FONT = "NotoSans-Regular"; + private static final int DEFAULT_FONT_SIZE = 10; + + private final PagedTextToPDF transformer; + + @Value("${transform.core.misc.pdfBox.defaultFont:NotoSans-Regular}") + private String pdfBoxDefaultFont; + + public TextToPdfContentTransformer() + { + transformer = new PagedTextToPDF(); + } + + @PostConstruct + public void init() + { + transformer.setDefaultFont(pdfBoxDefaultFont); + } + + public void setStandardFont(String fontName) + { + try + { + transformer.setFont(fontName); + } + catch (Throwable e) + { + throw new RuntimeException( + "Unable to set Standard Font for PDF generation: " + fontName, e); + } + } + + public void setFontSize(int fontSize) + { + try + { + transformer.setFontSize(fontSize); + } + catch (Throwable e) + { + throw new RuntimeException( + "Unable to set Font Size for PDF generation: " + fontSize); + } + } + + public String getUsedFont() + { + return transformer.getFontName(); + } + + @Override + public String getTransformerName() + { + return "textToPdf"; + } + + @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); + String stringPageLimit = transformOptions.get(PAGE_LIMIT); + int pageLimit = -1; + if (stringPageLimit != null) + { + pageLimit = parseInt(stringPageLimit, PAGE_LIMIT); + } + String pdfFont = transformOptions.get(PDF_FONT); + if (pdfFont == null || pdfFont.isBlank()) + { + pdfFont = pdfBoxDefaultFont; + } + String pdfFontSize = transformOptions.get(PDF_FONT_SIZE); + Integer fontSize = null; + if (pdfFontSize != null && !pdfFontSize.isBlank()) + { + try + { + fontSize = parseInt(pdfFontSize, PDF_FONT_SIZE); + } + catch (Exception e) + { + fontSize = DEFAULT_FONT_SIZE; + logger.error("Error parsing font size {}, going to set it as {}", pdfFontSize, fontSize, e); + } + } + + PDDocument pdf = null; + try (InputStream is = new FileInputStream(sourceFile); + Reader ir = new BufferedReader(buildReader(is, sourceEncoding)); + OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile))) + { + // TransformationOptionLimits limits = getLimits(reader, writer, options); + // TransformationOptionPair pageLimits = limits.getPagesPair(); + pdf = transformer.createPDFFromText(ir, pageLimit, pdfFont, fontSize); + pdf.save(os); + } + finally + { + if (pdf != null) + { + try + { + pdf.close(); + } + catch (Throwable e) + { + e.printStackTrace(); + } + } + } + } + + protected InputStreamReader buildReader(InputStream is, String encoding) + { + // If they gave an encoding, try to use it + if (encoding != null) + { + Charset charset = null; + try + { + charset = Charset.forName(encoding); + } + catch (Exception e) + { + logger.warn("JVM doesn't understand encoding '" + encoding + + "' when transforming text to pdf"); + } + if (charset != null) + { + // Handles the situation where there is a BOM even though the encoding indicates that normally + // there should not be one for UTF-16BE and UTF-16LE. For extra flexibility includes UTF-16 too + // which optionally has the BOM. Rather than look at the BOM we look at the number of zero bytes + // in the first few character. XML files even when not in European languages tend to have more + // even zero bytes when big-endian encoded and more odd zero bytes when little-endian. + // Think of: The normal Java decoder does not have this flexibility but + // other transformers do. + String name = charset.displayName(); + if ("UTF-16".equals(name) || "UTF-16BE".equals(name) || "UTF-16LE".equals(name)) + { + logger.debug("Handle big and little endian UTF-16 text. Using UTF-16 rather than encoding " + name); + charset = Charset.forName("UTF-16"); + is = handleUTF16BOM(is); + } + else if ("UTF-8".equals(name)) + { + logger.debug("Using UTF-8"); + charset = Charset.forName("UTF-8"); + is = handleUTF8BOM(is); + } + logger.debug("Processing plain text in encoding " + name); + return new InputStreamReader(is, charset); + } + } + + // Fall back on the system default + logger.debug("Processing plain text using system default encoding"); + return new InputStreamReader(is); + } + + private static class PagedTextToPDF extends TextToPDF + { + // REPO-1066: duplicating the following lines from org.apache.pdfbox.tools.TextToPDF because they made them private + // before the upgrade to pdfbox 2.0.8, in pdfbox 1.8, this piece of code was public in org.apache.pdfbox.pdmodel.font.PDType1Font + static PDType1Font getStandardFont(String name) + { + return STANDARD_14.get(name); + } + + private static final Map STANDARD_14 = new HashMap<>(); + + static + { + STANDARD_14.put(PDType1Font.TIMES_ROMAN.getBaseFont(), PDType1Font.TIMES_ROMAN); + STANDARD_14.put(PDType1Font.TIMES_BOLD.getBaseFont(), PDType1Font.TIMES_BOLD); + STANDARD_14.put(PDType1Font.TIMES_ITALIC.getBaseFont(), PDType1Font.TIMES_ITALIC); + STANDARD_14.put(PDType1Font.TIMES_BOLD_ITALIC.getBaseFont(), + PDType1Font.TIMES_BOLD_ITALIC); + STANDARD_14.put(PDType1Font.HELVETICA.getBaseFont(), PDType1Font.HELVETICA); + STANDARD_14.put(PDType1Font.HELVETICA_BOLD.getBaseFont(), PDType1Font.HELVETICA_BOLD); + STANDARD_14.put(PDType1Font.HELVETICA_OBLIQUE.getBaseFont(), + PDType1Font.HELVETICA_OBLIQUE); + STANDARD_14.put(PDType1Font.HELVETICA_BOLD_OBLIQUE.getBaseFont(), + PDType1Font.HELVETICA_BOLD_OBLIQUE); + STANDARD_14.put(PDType1Font.COURIER.getBaseFont(), PDType1Font.COURIER); + STANDARD_14.put(PDType1Font.COURIER_BOLD.getBaseFont(), PDType1Font.COURIER_BOLD); + STANDARD_14.put(PDType1Font.COURIER_OBLIQUE.getBaseFont(), PDType1Font.COURIER_OBLIQUE); + STANDARD_14.put(PDType1Font.COURIER_BOLD_OBLIQUE.getBaseFont(), + PDType1Font.COURIER_BOLD_OBLIQUE); + STANDARD_14.put(PDType1Font.SYMBOL.getBaseFont(), PDType1Font.SYMBOL); + STANDARD_14.put(PDType1Font.ZAPF_DINGBATS.getBaseFont(), PDType1Font.ZAPF_DINGBATS); + } + // duplicating until here + + private String fontName = null; + private String defaultFont = null; + + // The following code is based on the code in TextToPDF with the addition of + // checks for page limits. + // The calling code must close the PDDocument once finished with it. + public PDDocument createPDFFromText(Reader text, int pageLimit, String pdfFontName, Integer pdfFontSize) + throws IOException + { + PDDocument doc = null; + int pageCount = 0; + try + { + doc = new PDDocument(); + + final PDFont font = getFont(doc, pdfFontName); + final int fontSize = pdfFontSize != null ? pdfFontSize : getFontSize(); + + fontName = font.getName(); + + logger.debug("Going to use font {} with size {}", fontName, fontSize); + + final int margin = 40; + float height = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000; + + // calculate font height and increase by 5 percent. + height = height * fontSize * 1.05f; + + BufferedReader data = (text instanceof BufferedReader) ? (BufferedReader) text : new BufferedReader(text); + String nextLine; + PDPage page = new PDPage(); + PDPageContentStream contentStream = null; + float y = -1; + float maxStringLength = page.getMediaBox().getWidth() - 2 * margin; + + // There is a special case of creating a PDF document from an empty string. + boolean textIsEmpty = true; + + outer: while ((nextLine = data.readLine()) != null) + { + // The input text is nonEmpty. New pages will be created and added + // to the PDF document as they are needed, depending on the length of + // the text. + textIsEmpty = false; + + String[] lineWords = nextLine.trim().split(" "); + int lineIndex = 0; + while (lineIndex < lineWords.length) + { + final StringBuilder nextLineToDraw = new StringBuilder(); + float lengthIfUsingNextWord = 0; + do + { + nextLineToDraw.append(lineWords[lineIndex]); + nextLineToDraw.append(" "); + lineIndex++; + if (lineIndex < lineWords.length) + { + String lineWithNextWord = nextLineToDraw.toString() + lineWords[lineIndex]; + lengthIfUsingNextWord = (font.getStringWidth( + lineWithNextWord) / 1000) * fontSize; + } + } while (lineIndex < lineWords.length && + lengthIfUsingNextWord < maxStringLength); + if (y < margin) + { + int test = pageCount + 1; + if (pageLimit > 0 && (pageCount++ >= pageLimit)) + { + break outer; + } + + // We have crossed the end-of-page boundary and need to extend the + // document by another page. + page = new PDPage(); + doc.addPage(page); + if (contentStream != null) + { + contentStream.endText(); + contentStream.close(); + } + contentStream = new PDPageContentStream(doc, page); + contentStream.setFont(font, fontSize); + contentStream.beginText(); + y = page.getMediaBox().getHeight() - margin + height; + contentStream.moveTextPositionByAmount(margin, y); + } + + if (contentStream == null) + { + throw new IOException("Error:Expected non-null content stream."); + } + contentStream.moveTextPositionByAmount(0, -height); + y -= height; + contentStream.drawString(nextLineToDraw.toString()); + } + } + + // If the input text was the empty string, then the above while loop will have short-circuited + // and we will not have added any PDPages to the document. + // So in order to make the resultant PDF document readable by Adobe Reader etc, we'll add an empty page. + if (textIsEmpty) + { + doc.addPage(page); + } + + if (contentStream != null) + { + contentStream.endText(); + contentStream.close(); + } + } + catch (IOException io) + { + if (doc != null) + { + doc.close(); + } + throw io; + } + return doc; + } + + public void setFont(String aFontName) + { + PDType1Font font = PagedTextToPDF.getStandardFont(aFontName); + + if (font != null) + { + super.setFont(font); + this.fontName = aFontName; + } + } + + /** + * Gets the font that will be used in document transformation using the following approaches: + *
      + *
    1. Standard font map + *
    2. Font Mappers + *
    3. File system fonts + *
    4. Transformer default font + *
    5. PdfBox default font + *
    + * + * @param doc + * the document that will be transformed + * @param fontName + * the font name that will be used in transformation + * + * @return the font that was found + */ + private PDFont getFont(PDDocument doc, String fontName) + { + if (fontName == null) + { + fontName = fontName != null ? fontName : getDefaultFont(); + } + + // First, it tries to get the font from PdfBox STANDARD_14 map + PDFont font = getFromStandardFonts(fontName); + + // If not found, tries to get the font from FontMappers + if (font == null) + { + font = getFromFontMapper(fontName, doc); + + // If still not found, tries to get the font from file system + if (font == null) + { + font = getFromFileSystem(fontName); + + // If font is still null: + // - it will recursively get the transformer default font + // - Otherwise, it will use the PdfBox default font (Helvetica) + if (font == null) + { + if (defaultFont != null && !fontName.equals(defaultFont)) + { + font = getFont(doc, defaultFont); + } + else + { + font = getFont(); + } + } + } + + } + + return font; + } + + /** + * Gets the font from PdfBox standard fonts map + * + * @param fontName + * the font name to obtain + * + * @return the font object that has been found, otherwise null + */ + private PDFont getFromStandardFonts(String fontName) + { + return PagedTextToPDF.getStandardFont(fontName); + } + + /** + * Gets the font from {@link FontMappers} instance + * + * @param fontName + * the font name to obtain + * @param doc + * the PDF document + * + * @return the font object that has been found, otherwise null + */ + private PDFont getFromFontMapper(String fontName, PDDocument doc) + { + PDFont font = null; + FontMapping mapping = FontMappers.instance().getTrueTypeFont(fontName, null); + + if (mapping != null && mapping.getFont() != null && !mapping.isFallback()) + { + try + { + font = PDType0Font.load(doc, mapping.getFont().getOriginalData()); + } + catch (Exception e) + { + logger.error("Error loading font mapping {}", fontName, e); + } + } + + return font; + } + + /** + * Gets the font from existing file system fonts + * + * @param fontName + * the font name to obtain + * @return the font object that has been found, otherwise null + */ + private PDFont getFromFileSystem(String fontName) + { + PDFont font = null; + String nameWithExtension = fontName + ".ttf"; + + FontFileFinder fontFileFinder = new FontFileFinder(); + List uris = fontFileFinder.find(); + + for (URI uri : uris) + { + if (uri.getPath().contains(nameWithExtension)) + { + InputStream fontIS = null; + try + { + fontIS = new FileInputStream(new File(uri)); + if (null != fontIS) + { + PDDocument documentMock = new PDDocument(); + font = PDType0Font.load(documentMock, fontIS); + break; + } + } + catch (IOException ioe) + { + logger.error("Error loading font {} from filesystem", fontName, ioe); + } + finally + { + if (fontIS != null) + { + try + { + fontIS.close(); + } + catch (Exception e) + { + logger.error("Error closing font inputstream", e); + } + } + } + } + } + + return font; + } + + public String getFontName() + { + return this.fontName; + } + + public String getDefaultFont() + { + if (defaultFont == null || defaultFont.isBlank()) + { + return TextToPdfContentTransformer.DEFAULT_FONT; + } + + return defaultFont; + } + + public void setDefaultFont(String name) + { + if (name == null || name.isBlank()) + { + defaultFont = TextToPdfContentTransformer.DEFAULT_FONT; + } + else + { + this.defaultFont = name; + } + } + } + + private int parseInt(String s, String paramName) + { + try + { + return Integer.valueOf(s); + } + catch (NumberFormatException e) + { + throw new IllegalArgumentException(paramName + " parameter must be an integer."); + } + } + + /** + * Skips the BOM character for UTF-8 encoding + */ + private InputStream handleUTF8BOM(InputStream is) + { + return new PushbackInputStream(is, UTF8_READ_AHEAD_BYTES) { + boolean bomRead; + + @Override + public int read(byte[] bytes, int off, int len) throws IOException + { + int i = 0; + int b = 0; + for (; i < len; i++) + { + b = read(); + if (b == -1) + { + break; + } + bytes[off + i] = (byte) b; + } + return i == 0 && b == -1 ? -1 : i; + } + + @Override + public int read() throws IOException + { + if (!bomRead) + { + bomRead = true; + byte[] bytes = new byte[UTF8_READ_AHEAD_BYTES]; + int end = in.read(bytes, 0, UTF8_READ_AHEAD_BYTES); + + if (bytes[0] == EF && bytes[1] == BB && bytes[2] == BF) + { + logger.warn("UTF-8 BOM detected, it will be skipped"); + } + else + { + for (int i = end - 1; i >= 0; i--) + { + unread(bytes[i]); + } + } + } + + return super.read(); + } + }; + } + + /** + * Handles the situation where there is a BOM even though the encoding indicates that normally there should not be one for UTF-16BE and UTF-16LE. For extra flexibility includes UTF-16 too which optionally has the BOM. Rather than look at the BOM we look at the number of zero bytes in the first few character. XML files even when not in European languages tend to have more even zero bytes when big-endian encoded and more odd zero bytes when little-endian. Think of: The normal Java decoder does not have this flexibility but other transformers do. + */ + private InputStream handleUTF16BOM(InputStream is) + { + return new PushbackInputStream(is, UTF16_READ_AHEAD_BYTES) { + boolean bomRead; + boolean switchByteOrder; + boolean evenByte = true; + + @Override + public int read(byte[] bytes, int off, int len) throws IOException + { + int i = 0; + int b = 0; + for (; i < len; i++) + { + b = read(); + if (b == -1) + { + break; + } + bytes[off + i] = (byte) b; + } + return i == 0 && b == -1 ? -1 : i; + } + + @Override + public int read() throws IOException + { + if (!bomRead) + { + bomRead = true; + boolean switchBom = false; + byte[] bytes = new byte[UTF16_READ_AHEAD_BYTES]; + int end = in.read(bytes, 0, UTF16_READ_AHEAD_BYTES); + int evenZeros = countZeros(bytes, 0); + int oddZeros = countZeros(bytes, 1); + if (evenZeros > oddZeros) + { + if (bytes[0] == FF && bytes[1] == FE) + { + switchByteOrder = true; + switchBom = true; + logger.warn("Little-endian BOM FFFE read, but characters are big-endian"); + } + else + { + logger.debug("More even zero bytes, so normal read for big-endian"); + } + } + else + { + if (bytes[0] == FE && bytes[1] == FF) + { + switchBom = true; + logger.debug("Big-endian BOM FEFF read, but characters are little-endian"); + } + else + { + switchByteOrder = true; + logger.debug("More odd zero bytes, so switch bytes from little-endian"); + } + } + + if (switchBom) + { + byte b = bytes[0]; + bytes[0] = bytes[1]; + bytes[1] = b; + } + + for (int i = end - 1; i >= 0; i--) + { + unread(bytes[i]); + } + } + + if (switchByteOrder) + { + if (evenByte) + { + int b1 = super.read(); + int b2 = super.read(); + if (b1 != -1) + { + unread(b1); + } + if (b2 != -1) + { + unread(b2); + } + } + evenByte = !evenByte; + } + + return super.read(); + } + + // Counts the number of even or odd 00 bytes + private int countZeros(byte[] b, int offset) + { + int count = 0; + for (int i = offset; i < UTF16_READ_AHEAD_BYTES; i += 2) + { + if (b[i] == 0) + { + count++; + } + } + return count; + } + }; + } +} diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscLivenessReadinessProbeIT.java b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscLivenessReadinessProbeIT.java index 1d772a37..5e494e51 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscLivenessReadinessProbeIT.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscLivenessReadinessProbeIT.java @@ -29,9 +29,11 @@ package org.alfresco.transform.misc; import org.alfresco.transform.base.LivenessReadinessProbeTest; -public class MiscLivenessReadinessProbeIT extends LivenessReadinessProbeTest { +public class MiscLivenessReadinessProbeIT extends LivenessReadinessProbeTest +{ @Override - protected ImagesForTests getImageForTest() { + protected ImagesForTests getImageForTest() + { return new ImagesForTests("alfresco-transform-misc", "text/plain", "text/plain", "original.txt"); } } diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscMetadataExtractsIT.java b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscMetadataExtractsIT.java index d18dacf1..2c678484 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscMetadataExtractsIT.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscMetadataExtractsIT.java @@ -26,18 +26,19 @@ */ package org.alfresco.transform.misc; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; import static org.alfresco.transform.base.clients.FileInfo.testFile; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; import static org.alfresco.transform.common.Mimetype.MIMETYPE_RFC822; import static org.alfresco.transform.common.Mimetype.MIMETYPE_XHTML; import java.util.stream.Stream; -import org.alfresco.transform.base.metadata.AbstractMetadataExtractsIT; -import org.alfresco.transform.base.clients.FileInfo; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.alfresco.transform.base.clients.FileInfo; +import org.alfresco.transform.base.metadata.AbstractMetadataExtractsIT; + /** * Metadata integration tests in the Misc T-Engine. * @@ -53,7 +54,7 @@ public class MiscMetadataExtractsIT extends AbstractMetadataExtractsIT { super.testTransformation(fileInfo); } - + private static Stream engineTransformations() { return Stream.of( @@ -66,7 +67,6 @@ public class MiscMetadataExtractsIT extends AbstractMetadataExtractsIT // Special test cases from the repo tests // ====================================== testFile(MIMETYPE_RFC822, "eml", "quick.spanish.eml"), - testFile(MIMETYPE_HTML, "html", "quick.japanese.html") - ); + testFile(MIMETYPE_HTML, "html", "quick.japanese.html")); } } diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscQueueIT.java b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscQueueIT.java index ebcbf983..c2bf8ed5 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscQueueIT.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscQueueIT.java @@ -1,55 +1,55 @@ -/* - * #%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 static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; - -import java.util.UUID; - -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.base.messaging.AbstractQueueIT; - -public class MiscQueueIT extends AbstractQueueIT -{ - @Override - protected TransformRequest buildRequest() - { - return TransformRequest - .builder() - .withRequestId(UUID.randomUUID().toString()) - .withSourceMediaType(MIMETYPE_HTML) - .withTargetMediaType(MIMETYPE_TEXT_PLAIN) - .withTargetExtension("txt") - .withSchema(1) - .withClientData("ACS") - .withSourceReference(UUID.randomUUID().toString()) - .withSourceSize(32L) - .withInternalContextForTransformEngineTests() - .build(); - } -} +/* + * #%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 static org.alfresco.transform.common.Mimetype.MIMETYPE_HTML; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; + +import java.util.UUID; + +import org.alfresco.transform.base.messaging.AbstractQueueIT; +import org.alfresco.transform.client.model.TransformRequest; + +public class MiscQueueIT extends AbstractQueueIT +{ + @Override + protected TransformRequest buildRequest() + { + return TransformRequest + .builder() + .withRequestId(UUID.randomUUID().toString()) + .withSourceMediaType(MIMETYPE_HTML) + .withTargetMediaType(MIMETYPE_TEXT_PLAIN) + .withTargetExtension("txt") + .withSchema(1) + .withClientData("ACS") + .withSourceReference(UUID.randomUUID().toString()) + .withSourceSize(32L) + .withInternalContextForTransformEngineTests() + .build(); + } +} diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTransformsIT.java b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTransformsIT.java index d8625041..1f7f166f 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTransformsIT.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTransformsIT.java @@ -30,6 +30,11 @@ import static java.text.MessageFormat.format; import static java.util.function.Function.identity; import static java.util.stream.Collectors.toMap; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; +import static org.springframework.http.HttpStatus.OK; + import static org.alfresco.transform.base.clients.FileInfo.testFile; import static org.alfresco.transform.base.clients.HttpClient.sendTRequest; import static org.alfresco.transform.common.Mimetype.MIMETYPE_DITA; @@ -57,23 +62,20 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_MEDIAWIKI; import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; import static org.alfresco.transform.common.Mimetype.MIMETYPE_XML; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; -import static org.springframework.http.HttpStatus.OK; import java.util.Map; import java.util.Objects; import java.util.stream.Stream; -import org.alfresco.transform.base.clients.FileInfo; -import org.alfresco.transform.base.clients.SourceTarget; import org.apache.pdfbox.pdmodel.PDDocument; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; +import org.alfresco.transform.base.clients.FileInfo; +import org.alfresco.transform.base.clients.SourceTarget; + /** * @author Cezar Leahu */ @@ -82,78 +84,76 @@ public class MiscTransformsIT private static final String ENGINE_URL = "http://localhost:8090"; private static final Map TEST_FILES = Stream.of( - testFile(MIMETYPE_IMAGE_GIF, "gif", "sample.gif"), - testFile(MIMETYPE_IMAGE_JPEG, "jpg", "sample.jpg"), - testFile(MIMETYPE_IMAGE_PNG, "png", "sample.png"), - testFile(MIMETYPE_IMAGE_TIFF, "tiff", "sample.tiff"), - testFile(MIMETYPE_WORD, "doc", "quick.doc"), - testFile(MIMETYPE_OPENXML_WORDPROCESSING, "docx", "quick.docx"), - testFile(MIMETYPE_EXCEL, "xls", "quick.xls"), - testFile(MIMETYPE_OPENXML_SPREADSHEET, "xlsx", "quick.xlsx"), - testFile(MIMETYPE_PPT, "ppt", "quick.ppt"), - testFile(MIMETYPE_OPENXML_PRESENTATION, "pptx", "quick.pptx"), - testFile(MIMETYPE_OUTLOOK_MSG, "msg", "quick.msg"), - testFile(MIMETYPE_PDF, "pdf", "quick.pdf"), - testFile(MIMETYPE_TEXT_PLAIN, "txt", "quick2.txt"), + testFile(MIMETYPE_IMAGE_GIF, "gif", "sample.gif"), + testFile(MIMETYPE_IMAGE_JPEG, "jpg", "sample.jpg"), + testFile(MIMETYPE_IMAGE_PNG, "png", "sample.png"), + testFile(MIMETYPE_IMAGE_TIFF, "tiff", "sample.tiff"), + testFile(MIMETYPE_WORD, "doc", "quick.doc"), + testFile(MIMETYPE_OPENXML_WORDPROCESSING, "docx", "quick.docx"), + testFile(MIMETYPE_EXCEL, "xls", "quick.xls"), + testFile(MIMETYPE_OPENXML_SPREADSHEET, "xlsx", "quick.xlsx"), + testFile(MIMETYPE_PPT, "ppt", "quick.ppt"), + testFile(MIMETYPE_OPENXML_PRESENTATION, "pptx", "quick.pptx"), + testFile(MIMETYPE_OUTLOOK_MSG, "msg", "quick.msg"), + testFile(MIMETYPE_PDF, "pdf", "quick.pdf"), + testFile(MIMETYPE_TEXT_PLAIN, "txt", "quick2.txt"), - testFile("text/richtext", "rtf", "sample.rtf"), - testFile("text/sgml", "sgml", "sample.sgml"), - testFile("text/tab-separated-values", "tsv", "sample.tsv"), - testFile("text/x-setext", "etx", "sample.etx"), - testFile("text/x-java-source", "java", "Sample.java.txt"), - testFile("text/x-jsp", "jsp", "sample.jsp.txt"), - testFile("text/x-markdown", "md", "sample.md"), - testFile("text/calendar", "ics", "sample.ics"), + testFile("text/richtext", "rtf", "sample.rtf"), + testFile("text/sgml", "sgml", "sample.sgml"), + testFile("text/tab-separated-values", "tsv", "sample.tsv"), + testFile("text/x-setext", "etx", "sample.etx"), + testFile("text/x-java-source", "java", "Sample.java.txt"), + testFile("text/x-jsp", "jsp", "sample.jsp.txt"), + testFile("text/x-markdown", "md", "sample.md"), + testFile("text/calendar", "ics", "sample.ics"), + + testFile(MIMETYPE_TEXT_MEDIAWIKI, "mw", "sample.mw"), + testFile(MIMETYPE_TEXT_CSS, "css", "style.css"), + testFile(MIMETYPE_TEXT_CSV, "csv", "people.csv"), + testFile(MIMETYPE_TEXT_JAVASCRIPT, "js", "script.js"), + testFile(MIMETYPE_XML, "xml", "quick.xml"), + testFile(MIMETYPE_HTML, "html", "quick.html"), + testFile(MIMETYPE_JAVASCRIPT, "js", "script.js"), + testFile(MIMETYPE_DITA, "dita", "quickConcept.dita"), + testFile(MIMETYPE_IWORK_KEYNOTE, "key", "quick.key"), + testFile(MIMETYPE_IWORK_NUMBERS, "number", "quick.numbers"), + testFile(MIMETYPE_IWORK_PAGES, "pages", "quick.pages"), + testFile(MIMETYPE_RFC822, "eml", "quick.eml")).collect(toMap(FileInfo::getMimeType, identity())); - testFile(MIMETYPE_TEXT_MEDIAWIKI, "mw", "sample.mw"), - testFile(MIMETYPE_TEXT_CSS, "css", "style.css"), - testFile(MIMETYPE_TEXT_CSV, "csv", "people.csv"), - testFile(MIMETYPE_TEXT_JAVASCRIPT, "js", "script.js"), - testFile(MIMETYPE_XML, "xml", "quick.xml"), - testFile(MIMETYPE_HTML, "html", "quick.html"), - testFile(MIMETYPE_JAVASCRIPT, "js", "script.js"), - testFile(MIMETYPE_DITA, "dita", "quickConcept.dita"), - testFile(MIMETYPE_IWORK_KEYNOTE, "key", "quick.key"), - testFile(MIMETYPE_IWORK_NUMBERS, "number", "quick.numbers"), - testFile(MIMETYPE_IWORK_PAGES, "pages", "quick.pages"), - testFile(MIMETYPE_RFC822, "eml", "quick.eml") - ).collect(toMap(FileInfo::getMimeType, identity())); - public static Stream engineTransformations() { return Stream.of( - SourceTarget.of("text/html", "text/plain"), //duplicate + SourceTarget.of("text/html", "text/plain"), // duplicate - SourceTarget.of("text/plain", "text/plain"), - SourceTarget.of("text/mediawiki", "text/plain"), - SourceTarget.of("text/css", "text/plain"), - SourceTarget.of("text/csv", "text/plain"), - SourceTarget.of("text/xml", "text/plain"), - SourceTarget.of("text/html", "text/plain"), - SourceTarget.of("text/richtext", "text/plain"), - SourceTarget.of("text/sgml", "text/plain"), - SourceTarget.of("text/tab-separated-values", "text/plain"), - SourceTarget.of("text/x-setext", "text/plain"), - SourceTarget.of("text/x-java-source", "text/plain"), - SourceTarget.of("text/x-jsp", "text/plain"), - SourceTarget.of("text/x-markdown", "text/plain"), - SourceTarget.of("text/calendar", "text/plain"), - SourceTarget.of("application/x-javascript", "text/plain"), - SourceTarget.of("application/dita+xml", "text/plain"), + SourceTarget.of("text/plain", "text/plain"), + SourceTarget.of("text/mediawiki", "text/plain"), + SourceTarget.of("text/css", "text/plain"), + SourceTarget.of("text/csv", "text/plain"), + SourceTarget.of("text/xml", "text/plain"), + SourceTarget.of("text/html", "text/plain"), + SourceTarget.of("text/richtext", "text/plain"), + SourceTarget.of("text/sgml", "text/plain"), + SourceTarget.of("text/tab-separated-values", "text/plain"), + SourceTarget.of("text/x-setext", "text/plain"), + SourceTarget.of("text/x-java-source", "text/plain"), + SourceTarget.of("text/x-jsp", "text/plain"), + SourceTarget.of("text/x-markdown", "text/plain"), + SourceTarget.of("text/calendar", "text/plain"), + SourceTarget.of("application/x-javascript", "text/plain"), + SourceTarget.of("application/dita+xml", "text/plain"), - SourceTarget.of("application/vnd.apple.keynote", "image/jpeg"), - SourceTarget.of("application/vnd.apple.numbers", "image/jpeg"), - SourceTarget.of("application/vnd.apple.pages", "image/jpeg"), + SourceTarget.of("application/vnd.apple.keynote", "image/jpeg"), + SourceTarget.of("application/vnd.apple.numbers", "image/jpeg"), + SourceTarget.of("application/vnd.apple.pages", "image/jpeg"), - SourceTarget.of("text/plain", "application/pdf"), - SourceTarget.of("text/csv", "application/pdf"), - SourceTarget.of("application/dita+xml", "application/pdf"), - SourceTarget.of("text/xml", "application/pdf"), + SourceTarget.of("text/plain", "application/pdf"), + SourceTarget.of("text/csv", "application/pdf"), + SourceTarget.of("application/dita+xml", "application/pdf"), + SourceTarget.of("text/xml", "application/pdf"), - SourceTarget.of(MIMETYPE_IMAGE_TIFF, MIMETYPE_PDF), + SourceTarget.of(MIMETYPE_IMAGE_TIFF, MIMETYPE_PDF), - SourceTarget.of("message/rfc822", "text/plain") - ); + SourceTarget.of("message/rfc822", "text/plain")); } @ParameterizedTest @@ -166,13 +166,13 @@ public class MiscTransformsIT final String targetExtension = TEST_FILES.get(targetMimetype).getExtension(); final String descriptor = format("Transform ({0}, {1} -> {2}, {3})", - sourceFile, sourceMimetype, targetMimetype, targetExtension); + sourceFile, sourceMimetype, targetMimetype, targetExtension); try { // when final ResponseEntity response = sendTRequest(ENGINE_URL, sourceFile, - sourceMimetype, targetMimetype, targetExtension); + sourceMimetype, targetMimetype, targetExtension); assertEquals(OK, response.getStatusCode(), descriptor); if (MIMETYPE_PDF.equals(targetMimetype)) diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/ImageToPdfTransformerTest.java b/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/ImageToPdfTransformerTest.java index 269ec388..e8fd320c 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/ImageToPdfTransformerTest.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/ImageToPdfTransformerTest.java @@ -26,6 +26,11 @@ */ package org.alfresco.transform.misc.transformers; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.BDDMockito.then; + import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_GIF; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG; @@ -35,12 +40,7 @@ import static org.alfresco.transform.common.RequestParamMap.END_PAGE; import static org.alfresco.transform.common.RequestParamMap.PDF_FORMAT; import static org.alfresco.transform.common.RequestParamMap.PDF_ORIENTATION; import static org.alfresco.transform.common.RequestParamMap.START_PAGE; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.BDDMockito.then; -import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -52,9 +52,8 @@ import java.util.function.BiFunction; import java.util.function.BiPredicate; import java.util.function.Function; import java.util.stream.Stream; +import javax.imageio.ImageIO; -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.misc.util.ArgumentsCartesianProduct; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.junit.jupiter.api.BeforeEach; @@ -66,6 +65,9 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.misc.util.ArgumentsCartesianProduct; + class ImageToPdfTransformerTest { private static final File sourceFile = loadFile("sample.gif"); @@ -91,32 +93,30 @@ class ImageToPdfTransformerTest static Stream imageFiles() { return Stream.of( - ImageFile.of("sample.jpg", MIMETYPE_IMAGE_JPEG), - ImageFile.of("sample.gif", MIMETYPE_IMAGE_GIF), - ImageFile.of("sample.png", MIMETYPE_IMAGE_PNG) - ); + ImageFile.of("sample.jpg", MIMETYPE_IMAGE_JPEG), + ImageFile.of("sample.gif", MIMETYPE_IMAGE_GIF), + ImageFile.of("sample.png", MIMETYPE_IMAGE_PNG)); } static Stream defaultTransformOptions() { return Stream.of( - TransformOptions.none(), - TransformOptions.of(0, null), - TransformOptions.of(0, 0) - ); + TransformOptions.none(), + TransformOptions.of(0, null), + TransformOptions.of(0, 0)); } static Stream tiffTransformOptions() { return Stream.of( - TransformOptions.of(0, 0), // (startPage, endPage) - TransformOptions.of(0, 1), - TransformOptions.of(1, 1), - TransformOptions.of(null, 0), // expected 1 page in target file - TransformOptions.of(null, 1), // expected 2 pages in target file - TransformOptions.of(0, null), // expected all pages in target file - TransformOptions.of(1, null), // expected all except first page in target file - TransformOptions.none() // expected all pages in target file + TransformOptions.of(0, 0), // (startPage, endPage) + TransformOptions.of(0, 1), + TransformOptions.of(1, 1), + TransformOptions.of(null, 0), // expected 1 page in target file + TransformOptions.of(null, 1), // expected 2 pages in target file + TransformOptions.of(0, null), // expected all pages in target file + TransformOptions.of(1, null), // expected all except first page in target file + TransformOptions.none() // expected all pages in target file ); } @@ -124,9 +124,8 @@ class ImageToPdfTransformerTest { ImageFile tiffImage = ImageFile.of("sample.tiff", MIMETYPE_IMAGE_TIFF, 6); return Stream.of( - ArgumentsCartesianProduct.of(imageFiles(), defaultTransformOptions()), - ArgumentsCartesianProduct.of(tiffImage, tiffTransformOptions()) - ).flatMap(Function.identity()); + ArgumentsCartesianProduct.of(imageFiles(), defaultTransformOptions()), + ArgumentsCartesianProduct.of(tiffImage, tiffTransformOptions())).flatMap(Function.identity()); } @ParameterizedTest @@ -157,10 +156,9 @@ class ImageToPdfTransformerTest static Stream improperTransformOptions() { return Stream.of( - TransformOptions.of(1, 0), - TransformOptions.of(-1, 0), - TransformOptions.of(0, -1) - ); + TransformOptions.of(1, 0), + TransformOptions.of(-1, 0), + TransformOptions.of(0, -1)); } @ParameterizedTest @@ -168,8 +166,7 @@ class ImageToPdfTransformerTest void testTransformTiffToPdf_withImproperOptions(TransformOptions transformOptions) { // when - assertThrows(IllegalArgumentException.class, () -> - transformer.transform(MIMETYPE_IMAGE_TIFF, MIMETYPE_PDF, transformOptions.toMap(), sourceFile, targetFile, transformManager)); + assertThrows(IllegalArgumentException.class, () -> transformer.transform(MIMETYPE_IMAGE_TIFF, MIMETYPE_PDF, transformOptions.toMap(), sourceFile, targetFile, transformManager)); } @Test @@ -179,8 +176,7 @@ class ImageToPdfTransformerTest transformOptions.put(START_PAGE, "a"); // when - assertThrows(IllegalArgumentException.class, () -> - transformer.transform(MIMETYPE_IMAGE_TIFF, MIMETYPE_PDF, transformOptions, sourceFile, targetFile, transformManager)); + assertThrows(IllegalArgumentException.class, () -> transformer.transform(MIMETYPE_IMAGE_TIFF, MIMETYPE_PDF, transformOptions, sourceFile, targetFile, transformManager)); } @Test @@ -190,57 +186,53 @@ class ImageToPdfTransformerTest transformOptions.put(END_PAGE, "z"); // when - assertThrows(IllegalArgumentException.class, () -> - transformer.transform(MIMETYPE_IMAGE_TIFF, MIMETYPE_PDF, transformOptions, sourceFile, targetFile, transformManager)); + assertThrows(IllegalArgumentException.class, () -> transformer.transform(MIMETYPE_IMAGE_TIFF, MIMETYPE_PDF, transformOptions, sourceFile, targetFile, transformManager)); } /** Option and expected dimensions. */ static Stream validPdfFormats() { return Stream.of( - Arguments.of("DEFAULT", new PDRectangle(sourceFileWidth, sourceFileHeight)), - Arguments.of("default", new PDRectangle(sourceFileWidth, sourceFileHeight)), - Arguments.of("A0", PDRectangle.A0), - Arguments.of("a0", PDRectangle.A0), - Arguments.of("A1", PDRectangle.A1), - Arguments.of("A2", PDRectangle.A2), - Arguments.of("A3", PDRectangle.A3), - Arguments.of("A4", PDRectangle.A4), - Arguments.of("A5", PDRectangle.A5), - Arguments.of("A6", PDRectangle.A6), - Arguments.of("A6", PDRectangle.A6), - Arguments.of("LETTER", PDRectangle.LETTER), - Arguments.of("letter", PDRectangle.LETTER), - Arguments.of("LEGAL", PDRectangle.LEGAL), - Arguments.of("legal", PDRectangle.LEGAL) - ); + Arguments.of("DEFAULT", new PDRectangle(sourceFileWidth, sourceFileHeight)), + Arguments.of("default", new PDRectangle(sourceFileWidth, sourceFileHeight)), + Arguments.of("A0", PDRectangle.A0), + Arguments.of("a0", PDRectangle.A0), + Arguments.of("A1", PDRectangle.A1), + Arguments.of("A2", PDRectangle.A2), + Arguments.of("A3", PDRectangle.A3), + Arguments.of("A4", PDRectangle.A4), + Arguments.of("A5", PDRectangle.A5), + Arguments.of("A6", PDRectangle.A6), + Arguments.of("A6", PDRectangle.A6), + Arguments.of("LETTER", PDRectangle.LETTER), + Arguments.of("letter", PDRectangle.LETTER), + Arguments.of("LEGAL", PDRectangle.LEGAL), + Arguments.of("legal", PDRectangle.LEGAL)); } /** Option and expected orientation. */ static Stream validPdfOrientations() { return Stream.of( - Arguments.of("DEFAULT", unchangedRectangle()), - Arguments.of("default", unchangedRectangle()), - Arguments.of("PORTRAIT", rectangleRotatedIf((width, height) -> width > height)), - Arguments.of("portrait", rectangleRotatedIf((width, height) -> width > height)), - Arguments.of("LANDSCAPE", rectangleRotatedIf((width, height) -> height > width)), - Arguments.of("landscape", rectangleRotatedIf((width, height) -> height > width)) - ); + Arguments.of("DEFAULT", unchangedRectangle()), + Arguments.of("default", unchangedRectangle()), + Arguments.of("PORTRAIT", rectangleRotatedIf((width, height) -> width > height)), + Arguments.of("portrait", rectangleRotatedIf((width, height) -> width > height)), + Arguments.of("LANDSCAPE", rectangleRotatedIf((width, height) -> height > width)), + Arguments.of("landscape", rectangleRotatedIf((width, height) -> height > width))); } static Stream validPdfFormatsAndOrientations() { return ArgumentsCartesianProduct.ofArguments( - validPdfFormats(), - validPdfOrientations() - ); + validPdfFormats(), + validPdfOrientations()); } @ParameterizedTest @MethodSource("validPdfFormatsAndOrientations") void testTransformImageToPDF_withVariousPdfFormatsAndOrientations(String pdfFormat, PDRectangle expectedPdfFormat, - String pdfOrientation, BiFunction expectedPdfFormatRotator) throws Exception + String pdfOrientation, BiFunction expectedPdfFormatRotator) throws Exception { TransformOptions transformOptions = TransformOptions.of(pdfFormat, pdfOrientation); @@ -302,8 +294,7 @@ class ImageToPdfTransformerTest Arguments.of(ImageFile.of("612x792-300.tif", MIMETYPE_IMAGE_TIFF), 146.0f, 190.0f), Arguments.of(ImageFile.of("765x990-50.tif", MIMETYPE_IMAGE_TIFF), 1101.0f, 1425.0f), Arguments.of(ImageFile.of("765x990-72.tif", MIMETYPE_IMAGE_TIFF), 765.0f, 990.0f), - Arguments.of(ImageFile.of("765x990-300.tif", MIMETYPE_IMAGE_TIFF), 183.0f, 237.0f) - ); + Arguments.of(ImageFile.of("765x990-300.tif", MIMETYPE_IMAGE_TIFF), 183.0f, 237.0f)); } @ParameterizedTest @@ -320,12 +311,12 @@ class ImageToPdfTransformerTest try (PDDocument actualPdfDocument = PDDocument.load(targetFile)) { assertNotNull(actualPdfDocument); - assertEquals(expectedWidth, actualPdfDocument.getPage(0).getMediaBox().getWidth(),"Pdf width"); - assertEquals(expectedHeight,actualPdfDocument.getPage(0).getMediaBox().getHeight(),"Pdf height"); + assertEquals(expectedWidth, actualPdfDocument.getPage(0).getMediaBox().getWidth(), "Pdf width"); + assertEquals(expectedHeight, actualPdfDocument.getPage(0).getMediaBox().getHeight(), "Pdf height"); } } - //----------------------------------------------- Helper methods and classes ----------------------------------------------- + // ----------------------------------------------- Helper methods and classes ----------------------------------------------- private static BiFunction unchangedRectangle() { @@ -339,7 +330,7 @@ class ImageToPdfTransformerTest return PDRectangle::new; } - return (width, height) -> predicate.test(width, height)? new PDRectangle(height, width) : new PDRectangle(width, height); + return (width, height) -> predicate.test(width, height) ? new PDRectangle(height, width) : new PDRectangle(width, height); } private static File loadFile(String fileName) @@ -451,7 +442,8 @@ class ImageToPdfTransformerTest } } - static { + static + { try { BufferedImage image = ImageIO.read(sourceFile); @@ -463,4 +455,4 @@ class ImageToPdfTransformerTest throw new RuntimeException(e); } } -} \ No newline at end of file +} diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformerTest.java b/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformerTest.java index a177a945..e9f9afea 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformerTest.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformerTest.java @@ -26,15 +26,16 @@ */ package org.alfresco.transform.misc.transformers; -import static org.alfresco.transform.common.RequestParamMap.PAGE_LIMIT; -import static org.alfresco.transform.common.RequestParamMap.PDF_FONT; -import static org.alfresco.transform.common.RequestParamMap.PDF_FONT_SIZE; -import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; 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.junit.jupiter.api.Assertions.assertTrue; +import static org.alfresco.transform.common.RequestParamMap.PAGE_LIMIT; +import static org.alfresco.transform.common.RequestParamMap.PDF_FONT; +import static org.alfresco.transform.common.RequestParamMap.PDF_FONT_SIZE; +import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; + import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -51,7 +52,7 @@ import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.text.PDFTextStripper; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test;; +import org.junit.jupiter.api.Test; public class TextToPdfContentTransformerTest { @@ -95,7 +96,7 @@ public class TextToPdfContentTransformerTest public void test1UTF16BigEndianBomBigEndianChars() throws Exception { // 1. BOM indicates BE (fe then ff) + chars appear to be BE (as first byte read tends to be a zero) - // Expected with UTF-16. Some systems use BE and other like Windows and Mac used LE + // Expected with UTF-16. Some systems use BE and other like Windows and Mac used LE String expectedByteOrder = "fe ff 00 31 00 20 00 49"; transformTextAndCheck("UTF-16", true, true, expectedByteOrder); transformTextAndCheck("UTF-16", true, true, expectedByteOrder); @@ -107,7 +108,7 @@ public class TextToPdfContentTransformerTest public void test2UTF16LittleEndianBomLittleEndianChars() throws Exception { // 2. BOM indicates LE (ff then fe) + chars appear to be LE (as second byte read tends to be a zero) - // Expected with UTF-16. Some systems use BE and other like Windows and Mac used LE + // Expected with UTF-16. Some systems use BE and other like Windows and Mac used LE transformTextAndCheck("UTF-16", false, true, "ff fe 31 00 20 00 49 00"); } @@ -115,7 +116,7 @@ public class TextToPdfContentTransformerTest public void test3UTF16NoBomBigEndianChars() throws Exception { // 3. No BOM + chars appear to be BE (as first byte read tends to be a zero) - // Expected with UTF-16BE + // Expected with UTF-16BE transformTextAndCheck("UTF-16", true, null, "00 31 00 20 00 49"); } @@ -123,7 +124,7 @@ public class TextToPdfContentTransformerTest public void test4UTF16NoBomLittleEndianChars() throws Exception { // 4. No BOM + chars appear to be LE (as second byte read tends to be a zero) - // Expected with UTF-16LE + // Expected with UTF-16LE transformTextAndCheck("UTF-16", false, null, "31 00 20 00 49 00"); } @@ -131,7 +132,7 @@ public class TextToPdfContentTransformerTest public void test5UTF16BigEndianBomLittleEndianChars() throws Exception { // 5. BOM indicates BE (fe then ff) + chars appear to be LE (as second byte read tends to be a zero) - // SOMETHING IS WRONG, BUT USE LE!!!! + // SOMETHING IS WRONG, BUT USE LE!!!! transformTextAndCheck("UTF-16", false, false, "fe ff 31 00 20 00 49 00"); } @@ -139,7 +140,7 @@ public class TextToPdfContentTransformerTest public void test6UTF16LittleEndianBomBigEndianChars() throws Exception { // 6. BOM indicates LE (ff then fe) + chars appear to be BE (as first byte read tends to be a zero) - // SOMETHING IS WRONG, BUT USE BE!!!! + // SOMETHING IS WRONG, BUT USE BE!!!! transformTextAndCheck("UTF-16", true, false, "ff fe 00 31 00 20 00 49"); } @@ -204,8 +205,7 @@ public class TextToPdfContentTransformerTest } /** - * Test if a different font can be chosen to perform the transformation with breve character. This test - * transformation should fail as Times-Bold font doesn't handle the breve character + * Test if a different font can be chosen to perform the transformation with breve character. This test transformation should fail as Times-Bold font doesn't handle the breve character * * @throws Exception */ @@ -229,16 +229,17 @@ public class TextToPdfContentTransformerTest } /** - * @param encoding to be used to read the source file - * @param bigEndian indicates that the file should contain big endian characters, so typically the first byte of - * each char is a zero when using English. - * @param validBom if not null, the BOM is included. If true it is the one matching bigEndian. If false it is the - * opposite byte order, which really is an error, but we try to recover from it. - * @param expectedByteOrder The first few bytes of the source file so we can check the test data has been - * correctly created. + * @param encoding + * to be used to read the source file + * @param bigEndian + * indicates that the file should contain big endian characters, so typically the first byte of each char is a zero when using English. + * @param validBom + * if not null, the BOM is included. If true it is the one matching bigEndian. If false it is the opposite byte order, which really is an error, but we try to recover from it. + * @param expectedByteOrder + * The first few bytes of the source file so we can check the test data has been correctly created. */ protected TransformCheckResult transformTextAndCheck(String encoding, Boolean bigEndian, Boolean validBom, - String expectedByteOrder) throws Exception + String expectedByteOrder) throws Exception { return transformTextAndCheckImpl(-1, encoding, bigEndian, validBom, expectedByteOrder); } @@ -249,7 +250,7 @@ public class TextToPdfContentTransformerTest } private TransformCheckResult transformTextAndCheckImpl(int pageLimit, String encoding, Boolean bigEndian, Boolean validBom, - String expectedByteOrder) throws Exception + String expectedByteOrder) throws Exception { StringBuilder sb = new StringBuilder(); String checkText = createTestText(pageLimit, sb); @@ -286,13 +287,13 @@ public class TextToPdfContentTransformerTest } private TransformCheckResult transformTextAndCheck(File sourceFile, String encoding, String checkText, - String pageLimit) throws Exception + String pageLimit) throws Exception { return transformTextAndCheck(sourceFile, encoding, checkText, pageLimit, true, null, false); } private TransformCheckResult transformTextAndCheck(File sourceFile, String encoding, String checkText, - String pageLimit, boolean clean, Map extraParameters, boolean shouldFail) throws Exception + String pageLimit, boolean clean, Map extraParameters, boolean shouldFail) throws Exception { TransformCheckResult result = new TransformCheckResult(); @@ -384,7 +385,7 @@ public class TextToPdfContentTransformerTest boolean firstRead = true; byte[] bytes = new byte[8192]; try (InputStream is = new BufferedInputStream(new FileInputStream(file)); - OutputStream os = new BufferedOutputStream(new FileOutputStream(originalFile))) + OutputStream os = new BufferedOutputStream(new FileOutputStream(originalFile))) { int l; int off; @@ -398,7 +399,7 @@ public class TextToPdfContentTransformerTest if (firstRead) { firstRead = false; - boolean actualEndianBytes = bytes[0] == (byte)0xfe; // if true [1] would also be 0xff + boolean actualEndianBytes = bytes[0] == (byte) 0xfe; // if true [1] would also be 0xff switchBytes = actualEndianBytes != bigEndian; if (validBom == null) { @@ -419,14 +420,14 @@ public class TextToPdfContentTransformerTest if (switchBytes) { // Reverse the byte order of characters including the BOM. - for (int i=0; i 0) { diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/util/ArgumentsCartesianProduct.java b/engines/misc/src/test/java/org/alfresco/transform/misc/util/ArgumentsCartesianProduct.java index f3097ccf..7502d7ae 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/util/ArgumentsCartesianProduct.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/util/ArgumentsCartesianProduct.java @@ -43,8 +43,7 @@ import org.junit.jupiter.params.provider.Arguments; public class ArgumentsCartesianProduct { /** - * Creates arguments cartesian product of fixed object and a stream of objects. - * Example: a ✕ {x,y,z} = {a,x}, {a,y}, {a,z} + * Creates arguments cartesian product of fixed object and a stream of objects. Example: a ✕ {x,y,z} = {a,x}, {a,y}, {a,z} */ public static Stream of(final Object fixedFirstArgument, final Stream secondArguments) { @@ -52,8 +51,7 @@ public class ArgumentsCartesianProduct } /** - * Creates arguments cartesian product of a stream of objects and fixed object. - * Example: {a,b,c} ✕ y ✕ z = {a,y,z}, {b,y,z}, {c,y,z} + * Creates arguments cartesian product of a stream of objects and fixed object. Example: {a,b,c} ✕ y ✕ z = {a,y,z}, {b,y,z}, {c,y,z} */ public static Stream of(final Stream firstArguments, final Object... otherFixedArguments) { @@ -61,8 +59,7 @@ public class ArgumentsCartesianProduct } /** - * Creates arguments cartesian product of two streams of objects. - * Example: {a,b} ✕ {y,z} = {a,y}, {a,z}, {b,y}, {b,z} + * Creates arguments cartesian product of two streams of objects. Example: {a,b} ✕ {y,z} = {a,y}, {a,z}, {b,y}, {b,z} */ public static Stream of(final Stream firstArguments, final Stream secondArguments) { @@ -70,8 +67,7 @@ public class ArgumentsCartesianProduct } /** - * Creates arguments cartesian product of multiple streams of objects. - * Example: {a,b} ✕ {k,l,m} ✕ ... ✕ {y,z} = {a,k,...,y}, {a,k,...,z}, {a,l,...,y}, ..., {b,m,...,z} + * Creates arguments cartesian product of multiple streams of objects. Example: {a,b} ✕ {k,l,m} ✕ ... ✕ {y,z} = {a,k,...,y}, {a,k,...,z}, {a,l,...,y}, ..., {b,m,...,z} */ public static Stream of(final Stream... argumentsStreams) { @@ -79,18 +75,17 @@ public class ArgumentsCartesianProduct } /** - * Creates arguments cartesian product of multiple streams of arguments. - * Example: {a,b} ✕ {k,l,m} ✕ ... ✕ {y,z} = {a,k,...,y}, {a,k,...,z}, {a,l,...,y}, ..., {b,m,...,z} + * Creates arguments cartesian product of multiple streams of arguments. Example: {a,b} ✕ {k,l,m} ✕ ... ✕ {y,z} = {a,k,...,y}, {a,k,...,z}, {a,l,...,y}, ..., {b,m,...,z} */ @SafeVarargs @SuppressWarnings("unchecked") public static Stream ofArguments(final Stream... argumentsStreams) { return cartesianProductOf(argumentsStreams) - .map(argumentsStream -> (Stream) argumentsStream) - .map(argumentsStream -> Arguments.of(argumentsStream - .flatMap(arguments -> Arrays.stream(arguments.get())) - .toArray())); + .map(argumentsStream -> (Stream) argumentsStream) + .map(argumentsStream -> Arguments.of(argumentsStream + .flatMap(arguments -> Arrays.stream(arguments.get())) + .toArray())); } private static Stream> cartesianProductOf(final Stream... streams) @@ -101,17 +96,17 @@ public class ArgumentsCartesianProduct } return Stream.of(streams) - .filter(Objects::nonNull) - .map(stream -> stream.map(Collections::singletonList)) - .reduce((result, nextElements) -> { - final List> nextElementsCopy = nextElements.collect(Collectors.toList()); - return result.flatMap(resultPortion -> nextElementsCopy.stream().map(nextElementsPortion -> { - final List extendedResultPortion = new ArrayList<>(); - extendedResultPortion.addAll(resultPortion); - extendedResultPortion.addAll(nextElementsPortion); - return extendedResultPortion; - })); - }).orElse(Stream.empty()) - .map(Collection::stream); + .filter(Objects::nonNull) + .map(stream -> stream.map(Collections:: singletonList)) + .reduce((result, nextElements) -> { + final List> nextElementsCopy = nextElements.collect(Collectors.toList()); + return result.flatMap(resultPortion -> nextElementsCopy.stream().map(nextElementsPortion -> { + final List extendedResultPortion = new ArrayList<>(); + extendedResultPortion.addAll(resultPortion); + extendedResultPortion.addAll(nextElementsPortion); + return extendedResultPortion; + })); + }).orElse(Stream.empty()) + .map(Collection::stream); } } diff --git a/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/PdfRendererOptionsBuilder.java b/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/PdfRendererOptionsBuilder.java index e3631d6e..b1cb8d05 100644 --- a/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/PdfRendererOptionsBuilder.java +++ b/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/PdfRendererOptionsBuilder.java @@ -44,7 +44,8 @@ public final class PdfRendererOptionsBuilder private Boolean allowPdfEnlargement; private Boolean maintainPdfAspectRatio; - private PdfRendererOptionsBuilder() {} + private PdfRendererOptionsBuilder() + {} public PdfRendererOptionsBuilder withPage(final String page) { diff --git a/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/PdfRendererTransformEngine.java b/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/PdfRendererTransformEngine.java index c848ed80..b3792968 100644 --- a/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/PdfRendererTransformEngine.java +++ b/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/PdfRendererTransformEngine.java @@ -26,19 +26,20 @@ */ package org.alfresco.transform.pdfrenderer; -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.Collections; - import static org.alfresco.transform.base.logging.StandardMessages.COMMUNITY_LICENCE; import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG; import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; +import java.util.Collections; + +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 PdfRendererTransformEngine implements TransformEngine { @@ -52,9 +53,10 @@ public class PdfRendererTransformEngine implements TransformEngine } @Override - public String getStartupMessage() { + public String getStartupMessage() + { return COMMUNITY_LICENCE + - "This transformer uses alfresco-pdf-renderer which uses the PDFium library from Google Inc. "+ + "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"; } @@ -70,4 +72,4 @@ public class PdfRendererTransformEngine implements TransformEngine return new ProbeTransform("probe.pdf", MIMETYPE_PDF, MIMETYPE_IMAGE_PNG, Collections.emptyMap(), 7455, 1024, 150, 10240, 60 * 20 + 1, 60 * 15 - 15); } -} \ No newline at end of file +} diff --git a/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/transformers/PdfRendererTransformer.java b/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/transformers/PdfRendererTransformer.java index aee978bb..ba9290f6 100644 --- a/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/transformers/PdfRendererTransformer.java +++ b/engines/pdfrenderer/src/main/java/org/alfresco/transform/pdfrenderer/transformers/PdfRendererTransformer.java @@ -26,20 +26,6 @@ */ package org.alfresco.transform.pdfrenderer.transformers; -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.base.executors.AbstractCommandExecutor; -import org.alfresco.transform.base.executors.RuntimeExec; -import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transform.pdfrenderer.PdfRendererOptionsBuilder; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -import jakarta.annotation.PostConstruct; -import java.io.File; -import java.util.HashMap; -import java.util.Map; - import static org.alfresco.transform.base.util.Util.stringToLong; import static org.alfresco.transform.common.RequestParamMap.ALLOW_PDF_ENLARGEMENT; import static org.alfresco.transform.common.RequestParamMap.HEIGHT_REQUEST_PARAM; @@ -48,9 +34,23 @@ import static org.alfresco.transform.common.RequestParamMap.PAGE_REQUEST_PARAM; import static org.alfresco.transform.common.RequestParamMap.TIMEOUT; import static org.alfresco.transform.common.RequestParamMap.WIDTH_REQUEST_PARAM; +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import jakarta.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.executors.AbstractCommandExecutor; +import org.alfresco.transform.base.executors.RuntimeExec; +import org.alfresco.transform.base.util.CustomTransformerFileAdaptor; +import org.alfresco.transform.exceptions.TransformException; +import org.alfresco.transform.pdfrenderer.PdfRendererOptionsBuilder; + /** - * CommandExecutor implementation for running PDF Renderer transformations. It runs the - * transformation logic as a separate Shell process. + * CommandExecutor implementation for running PDF Renderer transformations. It runs the transformation logic as a separate Shell process. */ @Component public class PdfRendererTransformer extends AbstractCommandExecutor implements CustomTransformerFileAdaptor @@ -69,7 +69,8 @@ public class PdfRendererTransformer extends AbstractCommandExecutor implements C super.checkCommand = createCheckCommand(); } - @Override public String getTransformerName() + @Override + public String getTransformerName() { return "pdfrenderer"; } @@ -103,7 +104,7 @@ public class PdfRendererTransformer extends AbstractCommandExecutor implements C @Override public void transform(String sourceMimetype, String targetMimetype, Map transformOptions, - File sourceFile, File targetFile, TransformManager transformManager) throws TransformException + File sourceFile, File targetFile, TransformManager transformManager) throws TransformException { final String options = PdfRendererOptionsBuilder .builder() diff --git a/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererLivenessReadinessProbeIT.java b/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererLivenessReadinessProbeIT.java index 5a16b455..d6e70933 100644 --- a/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererLivenessReadinessProbeIT.java +++ b/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererLivenessReadinessProbeIT.java @@ -29,9 +29,11 @@ package org.alfresco.transform.pdfrenderer; import org.alfresco.transform.base.LivenessReadinessProbeTest; -public class PdfRendererLivenessReadinessProbeIT extends LivenessReadinessProbeTest { +public class PdfRendererLivenessReadinessProbeIT extends LivenessReadinessProbeTest +{ @Override - protected ImagesForTests getImageForTest() { + protected ImagesForTests getImageForTest() + { return new ImagesForTests("alfresco-pdf-renderer", "application/pdf", "image/png", "quick.pdf"); } } diff --git a/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererQueueIT.java b/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererQueueIT.java index 5658d35e..6ee5f097 100644 --- a/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererQueueIT.java +++ b/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererQueueIT.java @@ -31,12 +31,11 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; import java.util.UUID; -import org.alfresco.transform.client.model.TransformRequest; import org.alfresco.transform.base.messaging.AbstractQueueIT; +import org.alfresco.transform.client.model.TransformRequest; /** - * @author Lucian Tuca - * created on 15/01/2019 + * @author Lucian Tuca created on 15/01/2019 */ public class PdfRendererQueueIT extends AbstractQueueIT { @@ -44,16 +43,16 @@ public class PdfRendererQueueIT extends AbstractQueueIT protected TransformRequest buildRequest() { return TransformRequest - .builder() - .withRequestId(UUID.randomUUID().toString()) - .withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING) - .withTargetMediaType(MIMETYPE_PDF) - .withTargetExtension("pdf") - .withSchema(1) - .withClientData("ACS") - .withSourceReference(UUID.randomUUID().toString()) - .withSourceSize(32L) - .withInternalContextForTransformEngineTests() - .build(); + .builder() + .withRequestId(UUID.randomUUID().toString()) + .withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING) + .withTargetMediaType(MIMETYPE_PDF) + .withTargetExtension("pdf") + .withSchema(1) + .withClientData("ACS") + .withSourceReference(UUID.randomUUID().toString()) + .withSourceSize(32L) + .withInternalContextForTransformEngineTests() + .build(); } } diff --git a/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererTest.java b/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererTest.java index 570662f5..5bce24d3 100644 --- a/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererTest.java +++ b/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererTest.java @@ -26,37 +26,6 @@ */ package org.alfresco.transform.pdfrenderer; -import org.alfresco.transform.base.AbstractBaseTest; -import org.alfresco.transform.base.executors.RuntimeExec; -import org.alfresco.transform.base.executors.RuntimeExec.ExecutionResult; -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.alfresco.transform.pdfrenderer.transformers.PdfRendererTransformer; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.stubbing.Answer; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -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.request.MockHttpServletRequestBuilder; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.util.Arrays; -import java.util.Map; -import java.util.UUID; - -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -78,6 +47,39 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.util.StringUtils.getFilenameExtension; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Map; +import java.util.UUID; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.stubbing.Answer; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +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.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.executors.RuntimeExec.ExecutionResult; +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.alfresco.transform.pdfrenderer.transformers.PdfRendererTransformer; + /** * Test PdfRenderer with mocked external command. */ @@ -118,8 +120,8 @@ public class PdfRendererTest extends AbstractBaseTest @Override public void mockTransformCommand(String sourceExtension, - String targetExtension, String sourceMimetype, - boolean readTargetFileBytes) throws IOException + String targetExtension, String sourceMimetype, + boolean readTargetFileBytes) throws IOException { this.sourceExtension = sourceExtension; this.targetExtension = targetExtension; @@ -133,57 +135,57 @@ public class PdfRendererTest extends AbstractBaseTest sourceFile = new MockMultipartFile("file", "quick." + sourceExtension, sourceMimetype, sourceFileBytes); when(mockTransformCommand.execute(any(), anyLong())).thenAnswer( - (Answer) invocation -> { - Map actualProperties = invocation.getArgument(0); - assertEquals(3, actualProperties.size(), "There should be 3 properties"); + (Answer) invocation -> { + Map actualProperties = invocation.getArgument(0); + assertEquals(3, actualProperties.size(), "There should be 3 properties"); - String actualOptions = actualProperties.get("options"); - String actualSource = actualProperties.get("source"); - String actualTarget = actualProperties.get("target"); - String actualTargetExtension = getFilenameExtension(actualTarget); + String actualOptions = actualProperties.get("options"); + String actualSource = actualProperties.get("source"); + String actualTarget = actualProperties.get("target"); + String actualTargetExtension = getFilenameExtension(actualTarget); - assertNotNull(actualSource); - assertNotNull(actualTarget); - if (expectedSourceSuffix != null) - { - assertTrue(actualSource.endsWith(expectedSourceSuffix), - "The source file \"" + actualSource + - "\" should have ended in \"" + expectedSourceSuffix + "\""); - actualSource = actualSource.substring(0, - actualSource.length() - expectedSourceSuffix.length()); - } + assertNotNull(actualSource); + assertNotNull(actualTarget); + if (expectedSourceSuffix != null) + { + assertTrue(actualSource.endsWith(expectedSourceSuffix), + "The source file \"" + actualSource + + "\" should have ended in \"" + expectedSourceSuffix + "\""); + actualSource = actualSource.substring(0, + actualSource.length() - expectedSourceSuffix.length()); + } - assertNotNull(actualOptions); - if (expectedOptions != null) - { - assertEquals(expectedOptions, actualOptions,"expectedOptions"); - } + assertNotNull(actualOptions); + if (expectedOptions != null) + { + assertEquals(expectedOptions, actualOptions, "expectedOptions"); + } - Long actualTimeout = invocation.getArgument(1); - assertNotNull(actualTimeout); - if (expectedTimeout != null) - { - assertEquals(expectedTimeout, actualTimeout,"expectedTimeout"); - } + Long actualTimeout = invocation.getArgument(1); + assertNotNull(actualTimeout); + if (expectedTimeout != null) + { + assertEquals(expectedTimeout, actualTimeout, "expectedTimeout"); + } - // Copy a test file into the target file location if it exists - int i = actualTarget.lastIndexOf('_'); - if (i >= 0) - { - String testFilename = actualTarget.substring(i + 1); - File testFile = getTestFile(testFilename, false); - File targetFile = new File(actualTarget); - generateTargetFileFromResourceFile(actualTargetExtension, testFile, - targetFile); - } + // Copy a test file into the target file location if it exists + int i = actualTarget.lastIndexOf('_'); + if (i >= 0) + { + String testFilename = actualTarget.substring(i + 1); + File testFile = getTestFile(testFilename, false); + File targetFile = new File(actualTarget); + generateTargetFileFromResourceFile(actualTargetExtension, testFile, + targetFile); + } - // Check the supplied source file has not been changed. - byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath()); - assertTrue(Arrays.equals(sourceFileBytes, actualSourceFileBytes), - "Source file is not the same"); + // Check the supplied source file has not been changed. + byte[] actualSourceFileBytes = Files.readAllBytes(new File(actualSource).toPath()); + assertTrue(Arrays.equals(sourceFileBytes, actualSourceFileBytes), + "Source file is not the same"); - return mockExecutionResult; - }); + return mockExecutionResult; + }); when(mockExecutionResult.getExitValue()).thenReturn(0); when(mockExecutionResult.getStdErr()).thenReturn("STDERROR"); @@ -195,23 +197,23 @@ public class PdfRendererTest extends AbstractBaseTest { expectedOptions = "--width=321 --height=654 --allow-enlargement --maintain-aspect-ratio --page=2"; mockMvc - .perform(MockMvcRequestBuilders - .multipart(ENDPOINT_TRANSFORM) - .file(sourceFile) - .param("targetExtension", targetExtension) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype) + .perform(MockMvcRequestBuilders + .multipart(ENDPOINT_TRANSFORM) + .file(sourceFile) + .param("targetExtension", targetExtension) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype) - .param("page", "2") + .param("page", "2") - .param("width", "321") - .param("height", "654") - .param("allowPdfEnlargement", "true") - .param("maintainPdfAspectRatio", "true")) - .andExpect(status().isOk()) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + .param("width", "321") + .param("height", "654") + .param("allowPdfEnlargement", "true") + .param("maintainPdfAspectRatio", "true")) + .andExpect(status().isOk()) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Test @@ -219,23 +221,23 @@ public class PdfRendererTest extends AbstractBaseTest { expectedOptions = "--width=321 --height=654 --page=2"; mockMvc - .perform(MockMvcRequestBuilders - .multipart(ENDPOINT_TRANSFORM) - .file(sourceFile) - .param("targetExtension", targetExtension) - .param("targetMimetype", targetMimetype) - .param("sourceMimetype", sourceMimetype) + .perform(MockMvcRequestBuilders + .multipart(ENDPOINT_TRANSFORM) + .file(sourceFile) + .param("targetExtension", targetExtension) + .param("targetMimetype", targetMimetype) + .param("sourceMimetype", sourceMimetype) - .param("page", "2") + .param("page", "2") - .param("width", "321") - .param("height", "654") - .param("allowPdfEnlargement", "false") - .param("maintainPdfAspectRatio", "false")) - .andExpect(status().isOk()) - .andExpect(content().bytes(expectedTargetFileBytes)) - .andExpect(header().string("Content-Disposition", - "attachment; filename*=UTF-8''transform." + targetExtension)); + .param("width", "321") + .param("height", "654") + .param("allowPdfEnlargement", "false") + .param("maintainPdfAspectRatio", "false")) + .andExpect(status().isOk()) + .andExpect(content().bytes(expectedTargetFileBytes)) + .andExpect(header().string("Content-Disposition", + "attachment; filename*=UTF-8''transform." + targetExtension)); } @Override @@ -253,9 +255,9 @@ public class PdfRendererTest extends AbstractBaseTest when(mockExecutionResult.getExitValue()).thenReturn(1); mockMvc.perform(mockMvcRequest(ENDPOINT_TRANSFORM, sourceFile, "targetExtension", "xxx")) - .andExpect(status().is(BAD_REQUEST.value())) - .andExpect(status() - .reason(containsString("Transformer exit code was not 0: \nSTDERR"))); + .andExpect(status().is(BAD_REQUEST.value())) + .andExpect(status() + .reason(containsString("Transformer exit code was not 0: \nSTDERR"))); } @Test @@ -272,11 +274,11 @@ public class PdfRendererTest 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 @@ -285,16 +287,16 @@ public class PdfRendererTest 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()); @@ -305,7 +307,7 @@ public class PdfRendererTest extends AbstractBaseTest @Test public void testOverridingExecutorPaths() { - //System test property value can me modified in the pom.xml + // System test property value can me modified in the pom.xml assertEquals(execPath, System.getProperty("PDF_RENDERER_EXE")); } } diff --git a/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererTransformationIT.java b/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererTransformationIT.java index 27b5770e..a6fffb05 100644 --- a/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererTransformationIT.java +++ b/engines/pdfrenderer/src/test/java/org/alfresco/transform/pdfrenderer/PdfRendererTransformationIT.java @@ -29,16 +29,17 @@ package org.alfresco.transform.pdfrenderer; import static java.text.MessageFormat.format; import static java.util.function.Function.identity; import static java.util.stream.Collectors.toMap; -import static org.alfresco.transform.base.clients.HttpClient.sendTRequest; -import static org.alfresco.transform.base.clients.FileInfo.testFile; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.springframework.http.HttpStatus.OK; +import static org.alfresco.transform.base.clients.FileInfo.testFile; +import static org.alfresco.transform.base.clients.HttpClient.sendTRequest; + import java.util.Map; import java.util.stream.Stream; -import org.alfresco.transform.base.clients.FileInfo; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.slf4j.Logger; @@ -46,6 +47,8 @@ import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; +import org.alfresco.transform.base.clients.FileInfo; + /** * @author Cezar Leahu */ @@ -55,18 +58,16 @@ public class PdfRendererTransformationIT private static final String ENGINE_URL = "http://localhost:8090"; private static final Map TEST_FILES = Stream.of( - testFile("application/pdf","pdf","quick.pdf"), - testFile("application/illustrator","ai","quickCS3.ai") , - testFile("application/illustrator","ai","quickCS5.ai") - ).collect(toMap(FileInfo::getPath, identity())); + testFile("application/pdf", "pdf", "quick.pdf"), + testFile("application/illustrator", "ai", "quickCS3.ai"), + testFile("application/illustrator", "ai", "quickCS5.ai")).collect(toMap(FileInfo::getPath, identity())); public static Stream engineTransformations() { return Stream.of( - "quick.pdf", - "quickCS3.ai", - "quickCS5.ai" - ); + "quick.pdf", + "quickCS3.ai", + "quickCS5.ai"); } @ParameterizedTest @@ -76,13 +77,13 @@ public class PdfRendererTransformationIT final String sourceMimetype = TEST_FILES.get(sourceFile).getMimeType(); final String descriptor = format("Transform ({0}, {1} -> {2}, {3})", - sourceFile, sourceMimetype, "image/png", "png"); + sourceFile, sourceMimetype, "image/png", "png"); try { final ResponseEntity response = sendTRequest(ENGINE_URL, sourceFile, sourceMimetype, - "image/png", "png"); - assertEquals(OK, response.getStatusCode(),descriptor); + "image/png", "png"); + assertEquals(OK, response.getStatusCode(), descriptor); } catch (Exception e) { diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/TikaTransformEngine.java b/engines/tika/src/main/java/org/alfresco/transform/tika/TikaTransformEngine.java index 123f42b1..6ec08eaf 100644 --- a/engines/tika/src/main/java/org/alfresco/transform/tika/TikaTransformEngine.java +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/TikaTransformEngine.java @@ -26,19 +26,20 @@ */ package org.alfresco.transform.tika; -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.Collections; - import static org.alfresco.transform.base.logging.StandardMessages.COMMUNITY_LICENCE; import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +import java.util.Collections; + +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 TikaTransformEngine implements TransformEngine { diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/AbstractTikaMetadataExtractorEmbeddor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/AbstractTikaMetadataExtractorEmbeddor.java index 5e65e233..fc4d2a49 100644 --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/AbstractTikaMetadataExtractorEmbeddor.java +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/AbstractTikaMetadataExtractorEmbeddor.java @@ -26,8 +26,20 @@ */ package org.alfresco.transform.tika.metadata; -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Serializable; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.apache.tika.embedder.Embedder; import org.apache.tika.extractor.DocumentSelector; import org.apache.tika.metadata.DublinCore; @@ -51,24 +63,12 @@ import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Serializable; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.Locale; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder; /** - * The parent of all Metadata Extractors which use Apache Tika under the hood. This handles all the - * common parts of processing the files, and the common mappings. - + * The parent of all Metadata Extractors which use Apache Tika under the hood. This handles all the common parts of processing the files, and the common mappings. + * *
      *   author:                 --      cm:author
      *   title:                  --      cm:title
    @@ -117,8 +117,7 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
         }
     
         /**
    -     * Version which also tries the ISO-8601 formats (in order..),
    -     *  and similar formats, which Tika makes use of
    +     * Version which also tries the ISO-8601 formats (in order..), and similar formats, which Tika makes use of
          */
         protected Serializable makeDate(String dateStr)
         {
    @@ -127,25 +126,29 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
             {
                 return this.tikaUTCDateFormater.parseDateTime(dateStr).toDate();
             }
    -        catch (IllegalArgumentException ignore) {}
    +        catch (IllegalArgumentException ignore)
    +        {}
     
             try
             {
                 return this.tikaUTCDateFormater.withLocale(Locale.US).parseDateTime(dateStr).toDate();
             }
    -        catch (IllegalArgumentException ignore) {}
    +        catch (IllegalArgumentException ignore)
    +        {}
     
             try
             {
                 return this.tikaDateFormater.parseDateTime(dateStr).toDate();
             }
    -        catch (IllegalArgumentException ignore) {}
    +        catch (IllegalArgumentException ignore)
    +        {}
     
             try
             {
                 return this.tikaDateFormater.withLocale(Locale.US).parseDateTime(dateStr).toDate();
             }
    -        catch (IllegalArgumentException ignore) {}
    +        catch (IllegalArgumentException ignore)
    +        {}
     
             // Fall back to the normal ones: We just return the String as AbstractMappingMetadataExtracter
             // convertSystemPropertyValues in the repo will do the conversion that was previously done here.
    @@ -155,8 +158,7 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
         protected abstract Parser getParser();
     
         /**
    -     * Returns the Tika Embedder to modify
    -     * the document.
    +     * Returns the Tika Embedder to modify the document.
          *
          * @return the Tika embedder
          */
    @@ -166,8 +168,7 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
         }
     
         /**
    -     * Do we care about the contents of the
    -     *  extracted header, or nothing at all?
    +     * Do we care about the contents of the extracted header, or nothing at all?
          */
         protected boolean needHeaderContents()
         {
    @@ -178,14 +179,13 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
          * Allows implementation specific mappings to be done.
          */
         protected Map extractSpecific(Metadata metadata,
    -                                                        Map properties, Map headers)
    +            Map properties, Map headers)
         {
             return properties;
         }
     
         /**
    -     * Gets the document selector, used for determining whether to parse embedded resources,
    -     * null by default so parse all.
    +     * Gets the document selector, used for determining whether to parse embedded resources, null by default so parse all.
          */
         protected DocumentSelector getDocumentSelector(Metadata metadata, String targetMimeType)
         {
    @@ -221,11 +221,10 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
             ParseContext context = buildParseContext(metadata, sourceMimetype);
     
             ContentHandler handler;
    -        Map headers = null;
    +        Map headers = null;
             if (needHeaderContents())
             {
    -            MapCaptureContentHandler headerCapture =
    -                    new MapCaptureContentHandler();
    +            MapCaptureContentHandler headerCapture = new MapCaptureContentHandler();
                 headers = headerCapture.tags;
                 handler = new HeadContentHandler(headerCapture);
             }
    @@ -238,7 +237,7 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
     
             // First up, copy all the Tika metadata over
             // This allows people to map any of the Tika
    -        //  keys onto their own content model
    +        // keys onto their own content model
             for (String tikaKey : metadata.names())
             {
                 // TODO review this change (part of MNT-15267) - should we really force string concatenation here !?
    @@ -246,9 +245,9 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
             }
     
             // Now, map the common Tika metadata keys onto
    -        //  the common Alfresco metadata keys. This allows
    -        //  existing mapping properties files to continue
    -        //  to work without needing any changes
    +        // the common Alfresco metadata keys. This allows
    +        // existing mapping properties files to continue
    +        // to work without needing any changes
     
             // The simple ones
             putRawValue(KEY_AUTHOR, getMetadataValue(metadata, TikaCoreProperties.CREATOR), rawProperties);
    @@ -259,7 +258,7 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
             putRawValue(KEY_TAGS, getMetadataValues(metadata, KEY_TAGS), rawProperties);
     
             // Get the subject and description, despite things not
    -        //  being nearly as consistent as one might hope
    +        // being nearly as consistent as one might hope
             String subject = getMetadataValue(metadata, TikaCoreProperties.SUBJECT);
             String description = getMetadataValue(metadata, TikaCoreProperties.DESCRIPTION);
             if (subject != null && description != null)
    @@ -289,19 +288,17 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
             }
     
             // If people created a specific instance
    -        //  (eg OfficeMetadataExtractor), then allow that
    -        //  instance to map the Tika keys onto its
    -        //  existing namespace so that older properties
    -        //  files continue to map correctly
    +        // (eg OfficeMetadataExtractor), then allow that
    +        // instance to map the Tika keys onto its
    +        // existing namespace so that older properties
    +        // files continue to map correctly
             rawProperties = extractSpecific(metadata, rawProperties, headers);
     
             return rawProperties;
         }
     
         /**
    -     * @deprecated The content repository's TikaPoweredMetadataExtracter provides no non test implementations.
    -     *             This code exists in case there are custom implementations, that need to be converted to T-Engines.
    -     *             It is simply a copy and paste from the content repository and has received limited testing.
    +     * @deprecated The content repository's TikaPoweredMetadataExtracter provides no non test implementations. This code exists in case there are custom implementations, that need to be converted to T-Engines. It is simply a copy and paste from the content repository and has received limited testing.
          */
         @Override
         public void embedMetadata(String sourceMimetype, InputStream inputStream,
    @@ -335,7 +332,7 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
                     {
                         try
                         {
    -                        metadataToEmbed.add(metadataKey, (String)singleValue);
    +                        metadataToEmbed.add(metadataKey, (String) singleValue);
                         }
                         catch (ClassCastException e)
                         {
    @@ -347,7 +344,7 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
                 {
                     try
                     {
    -                    metadataToEmbed.add(metadataKey, (String)value);
    +                    metadataToEmbed.add(metadataKey, (String) value);
                     }
                     catch (ClassCastException e)
                     {
    @@ -395,47 +392,41 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
         protected static Stream distinct(final String[] strings)
         {
             return Stream.of(strings)
    -                     .filter(Objects::nonNull)
    -                     .map(String::strip)
    -                     .filter(s -> !s.isEmpty())
    -                     .distinct();
    +                .filter(Objects::nonNull)
    +                .map(String::strip)
    +                .filter(s -> !s.isEmpty())
    +                .distinct();
         }
     
         /**
    -     * This content handler will capture entries from within
    -     *  the header of the Tika content XHTML, but ignore the
    -     *  rest.
    +     * This content handler will capture entries from within the header of the Tika content XHTML, but ignore the rest.
          */
         protected static class HeadContentHandler extends ContentHandlerDecorator
         {
             /**
              * XHTML XPath parser.
              */
    -        private static final XPathParser PARSER =
    -                new XPathParser("xhtml", XHTMLContentHandler.XHTML);
    +        private static final XPathParser PARSER = new XPathParser("xhtml", XHTMLContentHandler.XHTML);
     
             /**
              * The XPath matcher used to select the XHTML body contents.
              */
    -        private static final Matcher MATCHER =
    -                PARSER.parse("/xhtml:html/xhtml:head/descendant:node()");
    +        private static final Matcher MATCHER = PARSER.parse("/xhtml:html/xhtml:head/descendant:node()");
     
             /**
    -         * Creates a content handler that passes all XHTML body events to the
    -         * given underlying content handler.
    +         * Creates a content handler that passes all XHTML body events to the given underlying content handler.
              *
    -         * @param handler content handler
    +         * @param handler
    +         *            content handler
              */
             protected HeadContentHandler(ContentHandler handler)
             {
                 super(new MatchingContentHandler(handler, MATCHER));
             }
         }
    +
         /**
    -     * This content handler will grab all tags and attributes,
    -     *  and record the textual content of the last seen one
    -     *  of them.
    -     * Normally only used with {@link HeadContentHandler}
    +     * This content handler will grab all tags and attributes, and record the textual content of the last seen one of them. Normally only used with {@link HeadContentHandler}
          */
         protected static class MapCaptureContentHandler implements ContentHandler
         {
    @@ -461,41 +452,75 @@ public abstract class AbstractTikaMetadataExtractorEmbeddor extends AbstractMeta
     
             public void startElement(String namespace, String localname, String qname, Attributes attrs)
             {
    -            for(int i=0; i
      *   title:           --      cm:title
    @@ -72,7 +73,7 @@ public class DWGMetadataExtractor extends AbstractTikaMetadataExtractorEmbeddor
     
         @Override
         protected Map extractSpecific(Metadata metadata,
    -                                                        Map properties, Map headers)
    +            Map properties, Map headers)
         {
             putRawValue(KEY_KEYWORD, metadata.get(TikaCoreProperties.SUBJECT), properties);
             putRawValue(KEY_LAST_AUTHOR, metadata.get(TikaCoreProperties.MODIFIED), properties);
    diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/IPTCMetadataExtractor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/IPTCMetadataExtractor.java
    index 6d1f12eb..32411112 100644
    --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/IPTCMetadataExtractor.java
    +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/IPTCMetadataExtractor.java
    @@ -1,165 +1,174 @@
    -/*
    - * #%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.tika.metadata.extractors;
    -
    -import org.alfresco.transform.base.executors.RuntimeExec;
    -import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor;
    -import org.alfresco.transform.tika.parsers.ExifToolParser;
    -import org.apache.commons.lang3.StringUtils;
    -import org.apache.tika.metadata.Metadata;
    -import org.apache.tika.parser.Parser;
    -import org.slf4j.Logger;
    -import org.slf4j.LoggerFactory;
    -import org.springframework.stereotype.Component;
    -
    -import java.io.Serializable;
    -import java.util.Arrays;
    -import java.util.Map;
    -import java.util.Set;
    -import java.util.regex.Matcher;
    -import java.util.regex.Pattern;
    -
    -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    -
    -@Component
    -public class IPTCMetadataExtractor extends AbstractTikaMetadataExtractorEmbeddor
    -{
    -
    -    private static final Logger logger = LoggerFactory.getLogger(IPTCMetadataExtractor.class);
    -    
    -    private static Set IPTC_DATE_KEYS = Set.of("XMP-photoshop:DateCreated", "XMP-iptcExt:ArtworkDateCreated");
    -
    -    private static final Pattern YEAR_IPTC = Pattern.compile("(\\d{4}[:|-]\\d{2}[:|-]\\d{2})");
    -
    -    private ExifToolParser parser;
    -    private RuntimeExec exifRuntimeExec;
    -
    -    public IPTCMetadataExtractor(RuntimeExec exifRuntimeExec) {
    -        super(EXTRACTOR, logger);
    -        this.exifRuntimeExec = exifRuntimeExec;
    -    }
    -
    -    @Override
    -    protected Parser getParser() 
    -    {
    -        if (this.parser == null) {
    -            this.parser = new ExifToolParser(exifRuntimeExec);
    -        }
    -        return this.parser;  
    -    }
    -
    -    /**
    -     * Because some of the mimetypes that IPTCMetadataExtractor now parse, were previously handled 
    -     * by TikaAutoMetadataExtractor we call the TikaAutoMetadataExtractor.extractSpecific method to 
    -     * ensure that the returned properties contains the expected entries.
    -     */
    -    @Override
    -    protected Map extractSpecific(Metadata metadata, Map properties,
    -            Map headers) 
    -    {
    -        properties = new TikaAutoMetadataExtractor().extractSpecific(metadata, properties, headers);
    -        ExifToolParser etParser = (ExifToolParser)this.getParser();
    -        if (etParser.getSeparator()!=null)
    -        {
    -            for (String key : properties.keySet())
    -            {
    -                if (properties.get(key) instanceof String)
    -                {
    -                    String value = (String) properties.get(key);
    -                    String separator = etParser.getSeparator();
    -                    if (value.contains(separator))
    -                    {
    -                        if (value.contains(String.format("\"%s\"",separator)))
    -                        {
    -                            separator = String.format("\"%s\"",separator);
    -                        }
    -                        String [] values = StringUtils.splitByWholeSeparator(value, separator);
    -                        // Change dateTime format. MM converted ':' to '-'
    -                        if (IPTC_DATE_KEYS.contains(key)){
    -                            values =  iptcToIso8601DateStrings(values);
    -                        }
    -                        putRawValue(key, (Serializable) Arrays.asList(values), properties);
    -                    }
    -                    else if (IPTC_DATE_KEYS.contains(key)) {
    -                        // Handle property with a single date string
    -                        putRawValue(key, (Serializable) iptcToIso8601DateString(value), properties);
    -                    }
    -                }
    -            }
    -        }
    -        return properties;
    -    }
    -
    -    /**
    -     * Converts a date or date time strings into Iso8601 format 

    - * - * @param dateStrings - * @return dateStrings in Iso8601 format - * @see #iptcToIso8601DateString - */ - public String[] iptcToIso8601DateStrings(String[] dateStrings) - { - for (int i = 0; i < dateStrings.length; i++) - { - dateStrings[i] = iptcToIso8601DateString(dateStrings[i]); - } - return dateStrings; - } - - /** - * Converts a date or date time string into Iso8601 format

    - * Converts any ':' in the year portion of a date string characters to '-'.

    - * Expects the year in the format YYYY:MM:DD or YYYY-MM-DD

    - * Will add the correct delimiter, 'T', to any dateTime strings, where | can be any char other than ,'T': - * YYYY:MM:DD|HH:mm:ss.... or YYYY-MM-DD|HH:mm:ss.... - *

    - * Examples:

      - *
    • "1919:10:16" will convert to "1919-10-16"
    • - *
    • "1901:02:01 00:00:00.000Z" will convert to "1901-02-01T00:00:00.000Z"
    • - *
    • "2001:02:01 16:15+00:00" will convert to "2001-02-01T16:15+00:00"
    • - *
    • "2021-06-11 05:36-01:00" will convert to "2021-06-11T05:36-01:00"
    • - *
    - * @param dateStr - * @return dateStr in Iso8601 format - */ - protected String iptcToIso8601DateString(String dateStr) - { - char timeSeparator = 'T'; - Matcher yearMatcher = YEAR_IPTC.matcher(dateStr); - if (yearMatcher.find()) - { - String year = yearMatcher.group(1); - dateStr = yearMatcher.replaceFirst(year.replaceAll(":", "-")); - if (dateStr.length()>year.length() && dateStr.charAt(year.length())!=timeSeparator) - { - dateStr = dateStr.replace(dateStr.charAt(year.length()), timeSeparator); - } - } - return dateStr; - } - -} +/* + * #%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.tika.metadata.extractors; + +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.apache.tika.metadata.Metadata; +import org.apache.tika.parser.Parser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import org.alfresco.transform.base.executors.RuntimeExec; +import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor; +import org.alfresco.transform.tika.parsers.ExifToolParser; + +@Component +public class IPTCMetadataExtractor extends AbstractTikaMetadataExtractorEmbeddor +{ + + private static final Logger logger = LoggerFactory.getLogger(IPTCMetadataExtractor.class); + + private static Set IPTC_DATE_KEYS = Set.of("XMP-photoshop:DateCreated", "XMP-iptcExt:ArtworkDateCreated"); + + private static final Pattern YEAR_IPTC = Pattern.compile("(\\d{4}[:|-]\\d{2}[:|-]\\d{2})"); + + private ExifToolParser parser; + private RuntimeExec exifRuntimeExec; + + public IPTCMetadataExtractor(RuntimeExec exifRuntimeExec) + { + super(EXTRACTOR, logger); + this.exifRuntimeExec = exifRuntimeExec; + } + + @Override + protected Parser getParser() + { + if (this.parser == null) + { + this.parser = new ExifToolParser(exifRuntimeExec); + } + return this.parser; + } + + /** + * Because some of the mimetypes that IPTCMetadataExtractor now parse, were previously handled by TikaAutoMetadataExtractor we call the TikaAutoMetadataExtractor.extractSpecific method to ensure that the returned properties contains the expected entries. + */ + @Override + protected Map extractSpecific(Metadata metadata, Map properties, + Map headers) + { + properties = new TikaAutoMetadataExtractor().extractSpecific(metadata, properties, headers); + ExifToolParser etParser = (ExifToolParser) this.getParser(); + if (etParser.getSeparator() != null) + { + for (String key : properties.keySet()) + { + if (properties.get(key) instanceof String) + { + String value = (String) properties.get(key); + String separator = etParser.getSeparator(); + if (value.contains(separator)) + { + if (value.contains(String.format("\"%s\"", separator))) + { + separator = String.format("\"%s\"", separator); + } + String[] values = StringUtils.splitByWholeSeparator(value, separator); + // Change dateTime format. MM converted ':' to '-' + if (IPTC_DATE_KEYS.contains(key)) + { + values = iptcToIso8601DateStrings(values); + } + putRawValue(key, (Serializable) Arrays.asList(values), properties); + } + else if (IPTC_DATE_KEYS.contains(key)) + { + // Handle property with a single date string + putRawValue(key, (Serializable) iptcToIso8601DateString(value), properties); + } + } + } + } + return properties; + } + + /** + * Converts a date or date time strings into Iso8601 format + *

    + * + * @param dateStrings + * @return dateStrings in Iso8601 format + * @see #iptcToIso8601DateString + */ + public String[] iptcToIso8601DateStrings(String[] dateStrings) + { + for (int i = 0; i < dateStrings.length; i++) + { + dateStrings[i] = iptcToIso8601DateString(dateStrings[i]); + } + return dateStrings; + } + + /** + * Converts a date or date time string into Iso8601 format + *

    + * Converts any ':' in the year portion of a date string characters to '-'. + *

    + * Expects the year in the format YYYY:MM:DD or YYYY-MM-DD + *

    + * Will add the correct delimiter, 'T', to any dateTime strings, where | can be any char other than ,'T': YYYY:MM:DD|HH:mm:ss.... or YYYY-MM-DD|HH:mm:ss.... + *

    + * Examples: + *

    + *

      + *
    • "1919:10:16" will convert to "1919-10-16"
    • + *
    • "1901:02:01 00:00:00.000Z" will convert to "1901-02-01T00:00:00.000Z"
    • + *
    • "2001:02:01 16:15+00:00" will convert to "2001-02-01T16:15+00:00"
    • + *
    • "2021-06-11 05:36-01:00" will convert to "2021-06-11T05:36-01:00"
    • + *
    + * + * @param dateStr + * @return dateStr in Iso8601 format + */ + protected String iptcToIso8601DateString(String dateStr) + { + char timeSeparator = 'T'; + Matcher yearMatcher = YEAR_IPTC.matcher(dateStr); + if (yearMatcher.find()) + { + String year = yearMatcher.group(1); + dateStr = yearMatcher.replaceFirst(year.replaceAll(":", "-")); + if (dateStr.length() > year.length() && dateStr.charAt(year.length()) != timeSeparator) + { + dateStr = dateStr.replace(dateStr.charAt(year.length()), timeSeparator); + } + } + return dateStr; + } + +} diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/MP3MetadataExtractor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/MP3MetadataExtractor.java index b22dc0db..576493e4 100644 --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/MP3MetadataExtractor.java +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/MP3MetadataExtractor.java @@ -26,6 +26,9 @@ */ package org.alfresco.transform.tika.metadata.extractors; +import java.io.Serializable; +import java.util.Map; + import org.apache.tika.metadata.Metadata; import org.apache.tika.metadata.TikaCoreProperties; import org.apache.tika.metadata.XMPDM; @@ -35,13 +38,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.io.Serializable; -import java.util.Map; - /** * MP3 file metadata extractor. * - * Configuration: (see MP3MetadataExtractor_metadata_extract.properties and tika_engine_config.json) + * Configuration: (see MP3MetadataExtractor_metadata_extract.properties and tika_engine_config.json) * *
      *   songTitle:              --      cm:title
    @@ -56,8 +56,7 @@ import java.util.Map;
      *   lyrics:                 --
      * 
    * - * Note - XMPDM metadata keys are also emitted, in common with - * the other Tika powered extracters + * Note - XMPDM metadata keys are also emitted, in common with the other Tika powered extracters * * Uses Apache Tika * @@ -91,14 +90,14 @@ public class MP3MetadataExtractor extends TikaAudioMetadataExtractor @Override protected Map extractSpecific(Metadata metadata, - Map properties, Map headers) + Map properties, Map headers) { // Do the normal Audio mappings super.extractSpecific(metadata, properties, headers); // Now do the compatibility ones // We only need these for people who had pre-existing mapping - // properties from before the proper audio model was added + // properties from before the proper audio model was added putRawValue(KEY_ALBUM_TITLE, metadata.get(XMPDM.ALBUM), properties); putRawValue(KEY_SONG_TITLE, metadata.get(TikaCoreProperties.TITLE), properties); putRawValue(KEY_ARTIST, metadata.get(XMPDM.ARTIST), properties); diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/MailMetadataExtractor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/MailMetadataExtractor.java index a7b580a2..76c3374f 100644 --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/MailMetadataExtractor.java +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/MailMetadataExtractor.java @@ -26,7 +26,11 @@ */ package org.alfresco.transform.tika.metadata.extractors; -import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor; +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR; + +import java.io.Serializable; +import java.util.Map; + import org.apache.tika.metadata.Message; import org.apache.tika.metadata.Metadata; import org.apache.tika.metadata.TikaCoreProperties; @@ -36,15 +40,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.io.Serializable; -import java.util.Map; - -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR; +import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor; /** * Outlook MAPI format email metadata extractor. * - * Configuration: (see MailMetadataExtractor_metadata_extract.properties and tika_engine_config.json) + * Configuration: (see MailMetadataExtractor_metadata_extract.properties and tika_engine_config.json) * *
      *   sentDate:               --      cm:sentdate
    @@ -57,8 +58,7 @@ import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbe
      *   bccNames:               --
      * 
    * - * TIKA note - to/cc/bcc go into the html part, not the metadata. - * Also, email addresses not included as yet. + * TIKA note - to/cc/bcc go into the html part, not the metadata. Also, email addresses not included as yet. * * @author Kevin Roast * @author adavis @@ -91,7 +91,7 @@ public class MailMetadataExtractor extends AbstractTikaMetadataExtractorEmbeddor @Override protected Map extractSpecific(Metadata metadata, - Map properties, Map headers) + Map properties, Map headers) { putRawValue(KEY_ORIGINATOR, metadata.get(TikaCoreProperties.CREATOR), properties); putRawValue(KEY_SUBJECT, metadata.get(TikaCoreProperties.TITLE), properties); diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/OfficeMetadataExtractor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/OfficeMetadataExtractor.java index 5088ff4d..ef782008 100644 --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/OfficeMetadataExtractor.java +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/OfficeMetadataExtractor.java @@ -26,7 +26,11 @@ */ package org.alfresco.transform.tika.metadata.extractors; -import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor; +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR; + +import java.io.Serializable; +import java.util.Map; + import org.apache.tika.metadata.Metadata; import org.apache.tika.metadata.Office; import org.apache.tika.metadata.TikaCoreProperties; @@ -36,17 +40,15 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.io.Serializable; -import java.util.Map; - -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR; +import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor; /** * Office file format metadata extractor. * - * Configuration: (see OfficeMetadataExtractor_metadata_extract.properties and tika_engine_config.json) + * Configuration: (see OfficeMetadataExtractor_metadata_extract.properties and tika_engine_config.json) * * This extractor uses the POI library to extract the following: + * *
      *   author:             --      cm:author
      *   title:              --      cm:title
    @@ -100,7 +102,7 @@ public class OfficeMetadataExtractor extends AbstractTikaMetadataExtractorEmbedd
     
         @Override
         protected Map extractSpecific(Metadata metadata,
    -                                                        Map properties, Map headers)
    +            Map properties, Map headers)
         {
             putRawValue(KEY_CREATE_DATETIME, metadata.get(TikaCoreProperties.CREATED), properties);
             putRawValue(KEY_LAST_SAVE_DATETIME, metadata.get(TikaCoreProperties.MODIFIED), properties);
    diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/OpenDocumentMetadataExtractor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/OpenDocumentMetadataExtractor.java
    index c42daa34..060523d4 100644
    --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/OpenDocumentMetadataExtractor.java
    +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/OpenDocumentMetadataExtractor.java
    @@ -26,7 +26,17 @@
      */
     package org.alfresco.transform.tika.metadata.extractors;
     
    -import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor;
    +import static org.apache.tika.metadata.DublinCore.NAMESPACE_URI_DC;
    +
    +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    +
    +import java.io.Serializable;
    +import java.util.Date;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.stream.Collectors;
    +
     import org.apache.tika.metadata.Metadata;
     import org.apache.tika.metadata.TikaCoreProperties;
     import org.apache.tika.parser.ParseContext;
    @@ -42,20 +52,12 @@ import org.slf4j.LoggerFactory;
     import org.springframework.stereotype.Component;
     import org.xml.sax.ContentHandler;
     
    -import java.io.Serializable;
    -import java.util.Date;
    -import java.util.List;
    -import java.util.Map;
    -import java.util.Set;
    -import java.util.stream.Collectors;
    -
    -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    -import static org.apache.tika.metadata.DublinCore.NAMESPACE_URI_DC;
    +import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor;
     
     /**
      * {@code "application/vnd.oasis.opendocument..."} and {@code "applicationvnd.oasis.opendocument..."} metadata extractor.
      *
    - * Configuration:   (see OpenDocumentMetadataExtractor_metadata_extract.properties and tika_engine_config.json)
    + * Configuration: (see OpenDocumentMetadataExtractor_metadata_extract.properties and tika_engine_config.json)
      *
      * 
      *   creationDate:           --      cm:created
    @@ -120,7 +122,7 @@ public class OpenDocumentMetadataExtractor extends AbstractTikaMetadataExtractor
     
         @Override
         protected Map extractSpecific(Metadata metadata,
    -                                                        Map properties, Map headers)
    +            Map properties, Map headers)
         {
             putRawValue(KEY_CREATION_DATE, getDateOrNull(metadata.get(TikaCoreProperties.CREATED)), properties);
             final String creator = getCreator(metadata);
    @@ -167,8 +169,7 @@ public class OpenDocumentMetadataExtractor extends AbstractTikaMetadataExtractor
                     return dateFormatter.parseDateTime(dateString).toDate();
                 }
                 catch (IllegalArgumentException ignore)
    -            {
    -            }
    +            {}
             }
             return null;
         }
    diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/PdfBoxMetadataExtractor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/PdfBoxMetadataExtractor.java
    index 648d7c30..28841137 100644
    --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/PdfBoxMetadataExtractor.java
    +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/PdfBoxMetadataExtractor.java
    @@ -26,8 +26,8 @@
      */
     package org.alfresco.transform.tika.metadata.extractors;
     
    -import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor;
    -import org.alfresco.transform.tika.transformers.Tika;
    +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    +
     import org.apache.tika.extractor.DocumentSelector;
     import org.apache.tika.metadata.Metadata;
     import org.apache.tika.parser.Parser;
    @@ -36,12 +36,13 @@ import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     import org.springframework.stereotype.Component;
     
    -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    +import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor;
    +import org.alfresco.transform.tika.transformers.Tika;
     
     /**
      * Metadata extractor for the PDF documents.
      *
    - * Configuration:   (see PdfBoxMetadataExtractor_metadata_extract.properties and tika_engine_config.json)
    + * Configuration: (see PdfBoxMetadataExtractor_metadata_extract.properties and tika_engine_config.json)
      *
      * 
      *   author:                 --      cm:author
    diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/PoiMetadataExtractor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/PoiMetadataExtractor.java
    index edc99fcd..3a1155b8 100644
    --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/PoiMetadataExtractor.java
    +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/PoiMetadataExtractor.java
    @@ -26,19 +26,20 @@
      */
     package org.alfresco.transform.tika.metadata.extractors;
     
    -import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor;
    +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    +
     import org.apache.tika.parser.Parser;
     import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     import org.springframework.stereotype.Component;
     
    -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    +import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor;
     
     /**
      * POI-based metadata extractor for Office 07 documents. See http://poi.apache.org/ for information on POI.
      *
    - * Configuration:   (see PoiMetadataExtractor_metadata_extract.properties and tika_engine_config.json)
    + * Configuration: (see PoiMetadataExtractor_metadata_extract.properties and tika_engine_config.json)
      *
      * 
      *   author:                 --      cm:author
    @@ -47,7 +48,7 @@ import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbe
      *   created:                --      cm:created
      *   Any custom property:    --      [not mapped]
      * 
    - + * * @author Nick Burch * @author Neil McErlean * @author Dmitry Velichkevich diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/TikaAudioMetadataExtractor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/TikaAudioMetadataExtractor.java index f1e31f36..b15a348c 100644 --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/TikaAudioMetadataExtractor.java +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/TikaAudioMetadataExtractor.java @@ -26,7 +26,13 @@ */ package org.alfresco.transform.tika.metadata.extractors; -import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor; +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR; +import static org.alfresco.transform.tika.transformers.Tika.readTikaConfig; + +import java.io.Serializable; +import java.util.Calendar; +import java.util.Map; + import org.apache.tika.config.TikaConfig; import org.apache.tika.metadata.Metadata; import org.apache.tika.metadata.TikaCoreProperties; @@ -40,19 +46,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.io.Serializable; -import java.util.Calendar; -import java.util.Map; - -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR; -import static org.alfresco.transform.tika.transformers.Tika.readTikaConfig; +import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor; /** - * A Metadata Extractor which makes use of the Apache Tika Audio Parsers to extract metadata from media files. - * For backwards compatibility reasons, this doesn't handle the MP3 format, which has its own dedicated extractor - * in {@link MP3MetadataExtractor} + * A Metadata Extractor which makes use of the Apache Tika Audio Parsers to extract metadata from media files. For backwards compatibility reasons, this doesn't handle the MP3 format, which has its own dedicated extractor in {@link MP3MetadataExtractor} * - * Configuration: (see TikaAudioMetadataExtractor_metadata_extract.properties and tika_engine_config.json) + * Configuration: (see TikaAudioMetadataExtractor_metadata_extract.properties and tika_engine_config.json) * *
      *   author:                 --      cm:author
    @@ -75,7 +74,7 @@ public class TikaAudioMetadataExtractor extends AbstractTikaMetadataExtractorEmb
         private static final Logger logger = LoggerFactory.getLogger(TikaAudioMetadataExtractor.class);
     
         // The Audio related parsers we use
    -    private static final Parser[] parsers = new Parser[] {
    +    private static final Parser[] parsers = new Parser[]{
                 new VorbisParser(),
                 new FlacParser(),
                 new MP4Parser()
    @@ -102,7 +101,7 @@ public class TikaAudioMetadataExtractor extends AbstractTikaMetadataExtractorEmb
     
         @Override
         protected Map extractSpecific(Metadata metadata,
    -                                                        Map properties, Map headers)
    +            Map properties, Map headers)
         {
             // Most things can go with the default Tika -> Alfresco Mapping
             // Handle the few special cases here
    @@ -124,21 +123,20 @@ public class TikaAudioMetadataExtractor extends AbstractTikaMetadataExtractorEmb
         private Serializable generateReleaseDate(Metadata metadata)
         {
             String date = metadata.get(XMPDM.RELEASE_DATE);
    -        if(date == null || date.length() == 0)
    +        if (date == null || date.length() == 0)
             {
                 return null;
             }
     
             // Is it just a year?
    -        if(date.matches("\\d\\d\\d\\d"))
    +        if (date.matches("\\d\\d\\d\\d"))
             {
                 // Just a year, we need a full date
                 // Go for the 1st of the 1st
                 Calendar c = Calendar.getInstance();
                 c.set(
                         Integer.parseInt(date), Calendar.JANUARY, 1,
    -                    0, 0, 0
    -            );
    +                    0, 0, 0);
                 c.set(Calendar.MILLISECOND, 0);
                 return c.getTime();
             }
    @@ -150,8 +148,9 @@ public class TikaAudioMetadataExtractor extends AbstractTikaMetadataExtractorEmb
         /**
          * Generate the description
          *
    -     * @param metadata     the metadata extracted from the file
    -     * @return          the description
    +     * @param metadata
    +     *            the metadata extracted from the file
    +     * @return the description
          */
         private String generateDescription(Metadata metadata)
         {
    diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/TikaAutoMetadataExtractor.java b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/TikaAutoMetadataExtractor.java
    index 2baebebf..6a25bc20 100644
    --- a/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/TikaAutoMetadataExtractor.java
    +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/metadata/extractors/TikaAutoMetadataExtractor.java
    @@ -26,7 +26,13 @@
      */
     package org.alfresco.transform.tika.metadata.extractors;
     
    -import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor;
    +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG;
    +import static org.alfresco.transform.tika.transformers.Tika.readTikaConfig;
    +
    +import java.io.Serializable;
    +import java.util.Map;
    +
     import org.apache.tika.config.TikaConfig;
     import org.apache.tika.metadata.Metadata;
     import org.apache.tika.metadata.TIFF;
    @@ -36,19 +42,12 @@ import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     import org.springframework.stereotype.Component;
     
    -import java.io.Serializable;
    -import java.util.Map;
    -
    -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EXTRACTOR;
    -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG;
    -import static org.alfresco.transform.tika.transformers.Tika.readTikaConfig;
    +import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor;
     
     /**
    - * A Metadata Extractor which makes use of the Apache Tika auto-detection to select the best parser to extract the
    - * metadata from a document. This will be used for all files which Tika can handle, but where no other more explicit
    - * extractor is defined.
    + * A Metadata Extractor which makes use of the Apache Tika auto-detection to select the best parser to extract the metadata from a document. This will be used for all files which Tika can handle, but where no other more explicit extractor is defined.
      *
    - * Configuration:   (see TikaAutoMetadataExtractor_metadata_extract.properties and tika_engine_config.json)
    + * Configuration: (see TikaAutoMetadataExtractor_metadata_extract.properties and tika_engine_config.json)
      *
      * 
      *   author:                 --      cm:author
    @@ -92,27 +91,21 @@ public class TikaAutoMetadataExtractor extends AbstractTikaMetadataExtractorEmbe
         }
     
         /**
    -     * Because some editors use JPEG_IMAGE_HEIGHT_TAG when
    -     * saving JPEG images , a more reliable source for
    -     * image size are the values provided by Tika
    -     * and not the exif/tiff metadata read from the file
    -     * This will override the tiff:Image size
    -     * which gets embedded into the alfresco node properties
    -     * for jpeg files that contain such exif information
    +     * Because some editors use JPEG_IMAGE_HEIGHT_TAG when saving JPEG images , a more reliable source for image size are the values provided by Tika and not the exif/tiff metadata read from the file This will override the tiff:Image size which gets embedded into the alfresco node properties for jpeg files that contain such exif information
          */
         @Override
         protected Map extractSpecific(Metadata metadata,
    -                                                        Map properties, Map headers)
    +            Map properties, Map headers)
         {
             if (MIMETYPE_IMAGE_JPEG.equals(metadata.get(Metadata.CONTENT_TYPE)))
             {
    -            //check if the image has exif information
    +            // check if the image has exif information
                 if (metadata.get(EXIF_IMAGE_WIDTH_TAG) != null
    -                && metadata.get(EXIF_IMAGE_HEIGHT_TAG) != null
    -                && metadata.get(COMPRESSION_TAG) != null)
    +                    && metadata.get(EXIF_IMAGE_HEIGHT_TAG) != null
    +                    && metadata.get(COMPRESSION_TAG) != null)
                 {
    -                //replace the exif size properties that will be embedded in the node with
    -                //the guessed dimensions from Tika
    +                // replace the exif size properties that will be embedded in the node with
    +                // the guessed dimensions from Tika
                     putRawValue(TIFF.IMAGE_LENGTH.getName(), extractSize(metadata.get(EXIF_IMAGE_HEIGHT_TAG)), properties);
                     putRawValue(TIFF.IMAGE_WIDTH.getName(), extractSize(metadata.get(EXIF_IMAGE_WIDTH_TAG)), properties);
                     putRawValue(JPEG_IMAGE_HEIGHT_TAG, metadata.get(EXIF_IMAGE_HEIGHT_TAG), properties);
    @@ -123,18 +116,18 @@ public class TikaAutoMetadataExtractor extends AbstractTikaMetadataExtractorEmbe
         }
     
         /**
    -     * Exif metadata for size also returns the string "pixels"
    -     * after the number value , this function will
    -     * stop at the first non digit character found in the text
    -     * @param sizeText string text
    +     * Exif metadata for size also returns the string "pixels" after the number value , this function will stop at the first non digit character found in the text
    +     * 
    +     * @param sizeText
    +     *            string text
          * @return the size value
          */
         private String extractSize(String sizeText)
         {
             StringBuilder sizeValue = new StringBuilder();
    -        for(char c : sizeText.toCharArray())
    +        for (char c : sizeText.toCharArray())
             {
    -            if(Character.isDigit(c))
    +            if (Character.isDigit(c))
                 {
                     sizeValue.append(c);
                 }
    diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/parsers/ExifToolParser.java b/engines/tika/src/main/java/org/alfresco/transform/tika/parsers/ExifToolParser.java
    index 1a6bb9b8..65890c92 100644
    --- a/engines/tika/src/main/java/org/alfresco/transform/tika/parsers/ExifToolParser.java
    +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/parsers/ExifToolParser.java
    @@ -1,397 +1,494 @@
    -/*
    - * #%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.tika.parsers;
    -
    -import static java.nio.charset.StandardCharsets.UTF_8;
    -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG;
    -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_TIFF;
    -
    -import java.io.BufferedReader;
    -import java.io.File;
    -import java.io.FileInputStream;
    -import java.io.IOException;
    -import java.io.InputStream;
    -import java.io.InputStreamReader;
    -import java.io.OutputStream;
    -import java.io.Reader;
    -import java.net.URL;
    -import java.util.List;
    -import java.util.Map;
    -import java.util.regex.Matcher;
    -import java.util.regex.Pattern;
    -
    -import org.alfresco.transform.base.executors.RuntimeExec;
    -import org.apache.commons.io.IOUtils;
    -import org.apache.commons.io.output.NullOutputStream;
    -import org.apache.tika.exception.TikaException;
    -import org.apache.tika.io.TemporaryResources;
    -import org.apache.tika.io.TikaInputStream;
    -import org.apache.tika.metadata.Metadata;
    -import org.apache.tika.mime.MediaType;
    -import org.apache.tika.parser.ParseContext;
    -import org.apache.tika.parser.Parser;
    -import org.apache.tika.parser.external.ExternalParser;
    -import org.apache.tika.parser.external.ExternalParsersFactory;
    -import org.apache.tika.parser.image.ImageParser;
    -import org.apache.tika.parser.image.TiffParser;
    -import org.apache.tika.parser.image.JpegParser;
    -import org.apache.tika.sax.XHTMLContentHandler;
    -import org.slf4j.Logger;
    -import org.slf4j.LoggerFactory;
    -import org.xml.sax.ContentHandler;
    -import org.xml.sax.SAXException;
    -
    -public class ExifToolParser extends ExternalParser {
    -    
    -    private static final Logger logger = LoggerFactory.getLogger(ExifToolParser.class);
    -
    -    private static final String EXIFTOOL_PARSER_CONFIG = "parsers/external/config/exiftool-parser.xml";
    -
    -    protected static final String DEFAULT_SEPARATOR = ", ";
    -    protected static final String SEPARATOR_SETTING = "-sep";
    -
    -    private String separator;
    -
    -    public ExifToolParser() {
    -        this(null);
    -    }
    -
    -    public ExifToolParser(RuntimeExec exifRuntimeExec) {
    -        super();
    -        try {
    -            List eParsers = ExternalParsersFactory.create(getExternalParserConfigURL());
    -            // if ExifTool is not installed then no parsers are returned
    -            if (eParsers.size() > 0) {
    -                ExternalParser eParser = eParsers.get(0);
    -
    -                String[] commandToBeExecuted;
    -                if (exifRuntimeExec==null) {
    -                    logger.debug("Command to be executed determined from Tika ExternalParser");
    -                    commandToBeExecuted = eParser.getCommand();
    -                } else {
    -                    logger.debug("Command to be executed determined from RuntimeExec");
    -                    commandToBeExecuted = exifRuntimeExec.getCommand();
    -                }
    -                if (commandToBeExecuted==null || commandToBeExecuted.length==0) {
    -                    commandToBeExecuted = eParser.getCommand();
    -                }
    -
    -                String commandToBeExecutedAsString = String.join( " ", commandToBeExecuted);
    -                logger.debug("Command to be executed: " + commandToBeExecutedAsString );
    -
    -                this.setCommand(commandToBeExecutedAsString);
    -                this.setIgnoredLineConsumer(eParser.getIgnoredLineConsumer());
    -                this.setMetadataExtractionPatterns(eParser.getMetadataExtractionPatterns());
    -                this.setSupportedTypes(eParser.getSupportedTypes());
    -            } else {
    -                logger.error(
    -                        "Error creating ExifToolParser from config, ExifToolExtractions not enabled. Please check ExifTool is installed correctly.");
    -            }
    -        } catch (IOException | TikaException e) {
    -            logger.error("Error creating ExifToolParser from config, ExifToolExtractions not enabled: ", e);
    -        }
    -    }
    -
    -    private URL getExternalParserConfigURL(){
    -        ClassLoader classLoader = ExifToolParser.class.getClassLoader();
    -        return classLoader.getResource(EXIFTOOL_PARSER_CONFIG);
    -    }
    -
    -    public void setSeparator(String sep) {
    -        this.separator = sep;
    -    }
    -
    -    public String getSeparator() {
    -        return this.separator;
    -    }
    -
    -    @Override
    -    public void setCommand(String... command){
    -        super.setCommand(command);
    -        if (command.length==1) {
    -            setSeparator(findSeparator(command[0]));
    -        }
    -        else {
    -            setSeparator(DEFAULT_SEPARATOR);
    -        }
    -    }
    -
    -    protected String findSeparator(String command) {
    -        if (command.contains(SEPARATOR_SETTING)) {
    -            int start = command.indexOf(SEPARATOR_SETTING)+SEPARATOR_SETTING.length()+1;
    -            String separator = DEFAULT_SEPARATOR;
    -            if (command.charAt(start)=='\"') {
    -                //get all chars up to the next \"
    -                int end = command.indexOf("\"", start+1);
    -                separator = command.substring(start+1, end);
    -            }
    -            else {
    -                int end = command.indexOf(" ", start);
    -                separator = command.substring(start, end);
    -            }
    -            return separator;
    -        }
    -        return DEFAULT_SEPARATOR;
    -    }
    -
    -    /**
    -     * Adapted from {@link org.apache.tika.parser.external.ExternalParser} 
    -     * due to errors attempting to {@link #extractMetadata} from the errorStream in original implementation.  

    - * Executes the configured external command and passes the given document - * stream as a simple XHTML document to the given SAX content handler. - * Metadata is only extracted if {@link #setMetadataExtractionPatterns(Map)} - * has been called to set patterns. - */ - public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) - throws IOException, SAXException, TikaException { - XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); - - MediaType mediaType = MediaType.parse(metadata.get(Metadata.CONTENT_TYPE)); - TemporaryResources tmp = new TemporaryResources(); - try { - TikaInputStream tis = TikaInputStream.get(stream, tmp); - - if (this.getSupportedTypes().contains(mediaType)) { - parse(tis, xhtml, metadata, tmp); - } - - switch (mediaType.getType()+"/"+mediaType.getSubtype()) { - case MIMETYPE_IMAGE_JPEG: - parseAdditional(new JpegParser(), tis, handler, metadata, context, mediaType); - break; - case MIMETYPE_IMAGE_TIFF: - parseAdditional(new TiffParser(), tis, handler, metadata, context, mediaType); - break; - default: - parseAdditional(new ImageParser(), tis, handler, metadata, context, mediaType); - } - } finally { - tmp.dispose(); - } - } - - private void parseAdditional(Parser parser, TikaInputStream tis, ContentHandler handler, Metadata metadata, ParseContext context, - MediaType mediaType) throws IOException, SAXException, TikaException { - if (parser.getSupportedTypes(context).contains(mediaType)) { - parser.parse(tis, handler, metadata, context); - } - } - - private void parse(TikaInputStream stream, XHTMLContentHandler xhtml, Metadata metadata, TemporaryResources tmp) - throws IOException, SAXException, TikaException { - boolean inputToStdIn = true; - boolean outputFromStdOut = true; - boolean hasPatterns = (getMetadataExtractionPatterns() != null && !getMetadataExtractionPatterns().isEmpty()); - - File output = null; - - // Build our getCommand() - String[] cmd; - if (getCommand().length == 1) { - cmd = getCommand()[0].split(" "); - } else { - cmd = new String[getCommand().length]; - System.arraycopy(getCommand(), 0, cmd, 0, getCommand().length); - } - for (int i = 0; i < cmd.length; i++) { - if (cmd[i].indexOf(INPUT_FILE_TOKEN) != -1) { - cmd[i] = cmd[i].replace(INPUT_FILE_TOKEN, stream.getFile().getPath()); - inputToStdIn = false; - } - if (cmd[i].indexOf(OUTPUT_FILE_TOKEN) != -1) { - output = tmp.createTemporaryFile(); - outputFromStdOut = false; - cmd[i] = cmd[i].replace(OUTPUT_FILE_TOKEN, output.getPath()); - } - } - - // Execute - Process process = null; - try { - if (cmd.length == 1) { - process = Runtime.getRuntime().exec(cmd[0]); - } else { - process = Runtime.getRuntime().exec(cmd); - } - } catch (Exception e) { - e.printStackTrace(); - } - - try { - if (inputToStdIn) { - sendInput(process, stream); - } else { - process.getOutputStream().close(); - } - - InputStream out = process.getInputStream(); - InputStream err = process.getErrorStream(); - - if (hasPatterns) { - - if (outputFromStdOut) { - extractOutput(out, xhtml); - } else { - extractMetadata(out, metadata); - } - } else { - ignoreStream(err); - - if (outputFromStdOut) { - extractOutput(out, xhtml); - } else { - ignoreStream(out); - } - } - } finally { - try { - process.waitFor(); - } catch (InterruptedException ignore) { - } - } - - // Grab the output if we haven't already - if (!outputFromStdOut) { - extractOutput(new FileInputStream(output), xhtml); - } - } - - /** - * Adapted from {@link org.apache.tika.parser.external.ExternalParser}

    - * Starts a thread that extracts the contents of the standard output - * stream of the given process to the given XHTML content handler. - * The standard output stream is closed once fully processed. - * - * @param stream stream - * @param xhtml XHTML content handler - * @throws SAXException if the XHTML SAX events could not be handled - * @throws IOException if an input error occurred - */ - private void extractOutput(InputStream stream, XHTMLContentHandler xhtml) throws SAXException, IOException { - try (Reader reader = new InputStreamReader(stream, UTF_8)) { - xhtml.startDocument(); - xhtml.startElement("p"); - char[] buffer = new char[1024]; - for (int n = reader.read(buffer); n != -1; n = reader.read(buffer)) { - xhtml.characters(buffer, 0, n); - } - xhtml.endElement("p"); - xhtml.endDocument(); - } - } - - /** - * Adapted from {@link org.apache.tika.parser.external.ExternalParser}

    - * Starts a thread that sends the contents of the given input stream - * to the standard input stream of the given process. Potential - * exceptions are ignored, and the standard input stream is closed - * once fully processed. Note that the given input stream is not - * closed by this method. - * - * @param process process - * @param stream input stream - */ - private void sendInput(final Process process, final InputStream stream) { - Thread t = new Thread() { - public void run() { - OutputStream stdin = process.getOutputStream(); - try { - IOUtils.copy(stream, stdin); - } catch (IOException e) { - logger.error( e.getMessage()); - } - } - }; - t.start(); - try { - t.join(); - } catch (InterruptedException ignore) { - logger.error(ignore.getMessage()); - } - } - - /** - * Adapted from {@link org.apache.tika.parser.external.ExternalParser}

    - * Starts a thread that reads and discards the contents of the - * standard stream of the given process. Potential exceptions - * are ignored, and the stream is closed once fully processed. - * - * @param stream stream - */ - private void ignoreStream(final InputStream stream) { - Thread t = new Thread() { - public void run() { - try { - IOUtils.copy(stream, NullOutputStream.NULL_OUTPUT_STREAM); - } catch (IOException e) { - } finally { - IOUtils.closeQuietly(stream); - } - } - }; - t.start(); - try { - t.join(); - } catch (InterruptedException ignore) { - } - } - - private void extractMetadata(final InputStream stream, final Metadata metadata) { - Thread t = new Thread() { - public void run() { - BufferedReader reader; - reader = new BufferedReader(new InputStreamReader(stream, UTF_8)); - try { - String line; - while ((line = reader.readLine()) != null) { - for (Pattern p : getMetadataExtractionPatterns().keySet()) { - Matcher m = p.matcher(line); - if (m.find()) { - if (getMetadataExtractionPatterns().get(p) != null - && !getMetadataExtractionPatterns().get(p).equals("")) { - metadata.add(getMetadataExtractionPatterns().get(p), m.group(1)); - } else { - metadata.add(m.group(1), m.group(2)); - } - } - } - } - } catch (IOException e) { - // Ignore - } finally { - IOUtils.closeQuietly(reader); - IOUtils.closeQuietly(stream); - } - } - }; - t.start(); - try { - t.join(); - } catch (InterruptedException ignore) { - } - } -} +/* + * #%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.tika.parsers; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_JPEG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_TIFF; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; +import java.net.URL; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.output.NullOutputStream; +import org.apache.tika.exception.TikaException; +import org.apache.tika.io.TemporaryResources; +import org.apache.tika.io.TikaInputStream; +import org.apache.tika.metadata.Metadata; +import org.apache.tika.mime.MediaType; +import org.apache.tika.parser.ParseContext; +import org.apache.tika.parser.Parser; +import org.apache.tika.parser.external.ExternalParser; +import org.apache.tika.parser.external.ExternalParsersFactory; +import org.apache.tika.parser.image.ImageParser; +import org.apache.tika.parser.image.JpegParser; +import org.apache.tika.parser.image.TiffParser; +import org.apache.tika.sax.XHTMLContentHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; + +import org.alfresco.transform.base.executors.RuntimeExec; + +public class ExifToolParser extends ExternalParser +{ + + private static final Logger logger = LoggerFactory.getLogger(ExifToolParser.class); + + private static final String EXIFTOOL_PARSER_CONFIG = "parsers/external/config/exiftool-parser.xml"; + + protected static final String DEFAULT_SEPARATOR = ", "; + protected static final String SEPARATOR_SETTING = "-sep"; + + private String separator; + + public ExifToolParser() + { + this(null); + } + + public ExifToolParser(RuntimeExec exifRuntimeExec) + { + super(); + try + { + List eParsers = ExternalParsersFactory.create(getExternalParserConfigURL()); + // if ExifTool is not installed then no parsers are returned + if (eParsers.size() > 0) + { + ExternalParser eParser = eParsers.get(0); + + String[] commandToBeExecuted; + if (exifRuntimeExec == null) + { + logger.debug("Command to be executed determined from Tika ExternalParser"); + commandToBeExecuted = eParser.getCommand(); + } + else + { + logger.debug("Command to be executed determined from RuntimeExec"); + commandToBeExecuted = exifRuntimeExec.getCommand(); + } + if (commandToBeExecuted == null || commandToBeExecuted.length == 0) + { + commandToBeExecuted = eParser.getCommand(); + } + + String commandToBeExecutedAsString = String.join(" ", commandToBeExecuted); + logger.debug("Command to be executed: " + commandToBeExecutedAsString); + + this.setCommand(commandToBeExecutedAsString); + this.setIgnoredLineConsumer(eParser.getIgnoredLineConsumer()); + this.setMetadataExtractionPatterns(eParser.getMetadataExtractionPatterns()); + this.setSupportedTypes(eParser.getSupportedTypes()); + } + else + { + logger.error( + "Error creating ExifToolParser from config, ExifToolExtractions not enabled. Please check ExifTool is installed correctly."); + } + } + catch (IOException | TikaException e) + { + logger.error("Error creating ExifToolParser from config, ExifToolExtractions not enabled: ", e); + } + } + + private URL getExternalParserConfigURL() + { + ClassLoader classLoader = ExifToolParser.class.getClassLoader(); + return classLoader.getResource(EXIFTOOL_PARSER_CONFIG); + } + + public void setSeparator(String sep) + { + this.separator = sep; + } + + public String getSeparator() + { + return this.separator; + } + + @Override + public void setCommand(String... command) + { + super.setCommand(command); + if (command.length == 1) + { + setSeparator(findSeparator(command[0])); + } + else + { + setSeparator(DEFAULT_SEPARATOR); + } + } + + protected String findSeparator(String command) + { + if (command.contains(SEPARATOR_SETTING)) + { + int start = command.indexOf(SEPARATOR_SETTING) + SEPARATOR_SETTING.length() + 1; + String separator = DEFAULT_SEPARATOR; + if (command.charAt(start) == '\"') + { + // get all chars up to the next \" + int end = command.indexOf("\"", start + 1); + separator = command.substring(start + 1, end); + } + else + { + int end = command.indexOf(" ", start); + separator = command.substring(start, end); + } + return separator; + } + return DEFAULT_SEPARATOR; + } + + /** + * Adapted from {@link org.apache.tika.parser.external.ExternalParser} due to errors attempting to {@link #extractMetadata} from the errorStream in original implementation. + *

    + * Executes the configured external command and passes the given document stream as a simple XHTML document to the given SAX content handler. Metadata is only extracted if {@link #setMetadataExtractionPatterns(Map)} has been called to set patterns. + */ + public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) + throws IOException, SAXException, TikaException + { + XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); + + MediaType mediaType = MediaType.parse(metadata.get(Metadata.CONTENT_TYPE)); + TemporaryResources tmp = new TemporaryResources(); + try + { + TikaInputStream tis = TikaInputStream.get(stream, tmp); + + if (this.getSupportedTypes().contains(mediaType)) + { + parse(tis, xhtml, metadata, tmp); + } + + switch (mediaType.getType() + "/" + mediaType.getSubtype()) + { + case MIMETYPE_IMAGE_JPEG: + parseAdditional(new JpegParser(), tis, handler, metadata, context, mediaType); + break; + case MIMETYPE_IMAGE_TIFF: + parseAdditional(new TiffParser(), tis, handler, metadata, context, mediaType); + break; + default: + parseAdditional(new ImageParser(), tis, handler, metadata, context, mediaType); + } + } + finally + { + tmp.dispose(); + } + } + + private void parseAdditional(Parser parser, TikaInputStream tis, ContentHandler handler, Metadata metadata, ParseContext context, + MediaType mediaType) throws IOException, SAXException, TikaException + { + if (parser.getSupportedTypes(context).contains(mediaType)) + { + parser.parse(tis, handler, metadata, context); + } + } + + private void parse(TikaInputStream stream, XHTMLContentHandler xhtml, Metadata metadata, TemporaryResources tmp) + throws IOException, SAXException, TikaException + { + boolean inputToStdIn = true; + boolean outputFromStdOut = true; + boolean hasPatterns = (getMetadataExtractionPatterns() != null && !getMetadataExtractionPatterns().isEmpty()); + + File output = null; + + // Build our getCommand() + String[] cmd; + if (getCommand().length == 1) + { + cmd = getCommand()[0].split(" "); + } + else + { + cmd = new String[getCommand().length]; + System.arraycopy(getCommand(), 0, cmd, 0, getCommand().length); + } + for (int i = 0; i < cmd.length; i++) + { + if (cmd[i].indexOf(INPUT_FILE_TOKEN) != -1) + { + cmd[i] = cmd[i].replace(INPUT_FILE_TOKEN, stream.getFile().getPath()); + inputToStdIn = false; + } + if (cmd[i].indexOf(OUTPUT_FILE_TOKEN) != -1) + { + output = tmp.createTemporaryFile(); + outputFromStdOut = false; + cmd[i] = cmd[i].replace(OUTPUT_FILE_TOKEN, output.getPath()); + } + } + + // Execute + Process process = null; + try + { + if (cmd.length == 1) + { + process = Runtime.getRuntime().exec(cmd[0]); + } + else + { + process = Runtime.getRuntime().exec(cmd); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + + try + { + if (inputToStdIn) + { + sendInput(process, stream); + } + else + { + process.getOutputStream().close(); + } + + InputStream out = process.getInputStream(); + InputStream err = process.getErrorStream(); + + if (hasPatterns) + { + + if (outputFromStdOut) + { + extractOutput(out, xhtml); + } + else + { + extractMetadata(out, metadata); + } + } + else + { + ignoreStream(err); + + if (outputFromStdOut) + { + extractOutput(out, xhtml); + } + else + { + ignoreStream(out); + } + } + } + finally + { + try + { + process.waitFor(); + } + catch (InterruptedException ignore) + {} + } + + // Grab the output if we haven't already + if (!outputFromStdOut) + { + extractOutput(new FileInputStream(output), xhtml); + } + } + + /** + * Adapted from {@link org.apache.tika.parser.external.ExternalParser} + *

    + * Starts a thread that extracts the contents of the standard output stream of the given process to the given XHTML content handler. The standard output stream is closed once fully processed. + * + * @param stream + * stream + * @param xhtml + * XHTML content handler + * @throws SAXException + * if the XHTML SAX events could not be handled + * @throws IOException + * if an input error occurred + */ + private void extractOutput(InputStream stream, XHTMLContentHandler xhtml) throws SAXException, IOException + { + try (Reader reader = new InputStreamReader(stream, UTF_8)) + { + xhtml.startDocument(); + xhtml.startElement("p"); + char[] buffer = new char[1024]; + for (int n = reader.read(buffer); n != -1; n = reader.read(buffer)) + { + xhtml.characters(buffer, 0, n); + } + xhtml.endElement("p"); + xhtml.endDocument(); + } + } + + /** + * Adapted from {@link org.apache.tika.parser.external.ExternalParser} + *

    + * Starts a thread that sends the contents of the given input stream to the standard input stream of the given process. Potential exceptions are ignored, and the standard input stream is closed once fully processed. Note that the given input stream is not closed by this method. + * + * @param process + * process + * @param stream + * input stream + */ + private void sendInput(final Process process, final InputStream stream) + { + Thread t = new Thread() { + public void run() + { + OutputStream stdin = process.getOutputStream(); + try + { + IOUtils.copy(stream, stdin); + } + catch (IOException e) + { + logger.error(e.getMessage()); + } + } + }; + t.start(); + try + { + t.join(); + } + catch (InterruptedException ignore) + { + logger.error(ignore.getMessage()); + } + } + + /** + * Adapted from {@link org.apache.tika.parser.external.ExternalParser} + *

    + * Starts a thread that reads and discards the contents of the standard stream of the given process. Potential exceptions are ignored, and the stream is closed once fully processed. + * + * @param stream + * stream + */ + private void ignoreStream(final InputStream stream) + { + Thread t = new Thread() { + public void run() + { + try + { + IOUtils.copy(stream, NullOutputStream.NULL_OUTPUT_STREAM); + } + catch (IOException e) + {} + finally + { + IOUtils.closeQuietly(stream); + } + } + }; + t.start(); + try + { + t.join(); + } + catch (InterruptedException ignore) + {} + } + + private void extractMetadata(final InputStream stream, final Metadata metadata) + { + Thread t = new Thread() { + public void run() + { + BufferedReader reader; + reader = new BufferedReader(new InputStreamReader(stream, UTF_8)); + try + { + String line; + while ((line = reader.readLine()) != null) + { + for (Pattern p : getMetadataExtractionPatterns().keySet()) + { + Matcher m = p.matcher(line); + if (m.find()) + { + if (getMetadataExtractionPatterns().get(p) != null + && !getMetadataExtractionPatterns().get(p).equals("")) + { + metadata.add(getMetadataExtractionPatterns().get(p), m.group(1)); + } + else + { + metadata.add(m.group(1), m.group(2)); + } + } + } + } + } + catch (IOException e) + { + // Ignore + } + finally + { + IOUtils.closeQuietly(reader); + IOUtils.closeQuietly(stream); + } + } + }; + t.start(); + try + { + t.join(); + } + catch (InterruptedException ignore) + {} + } +} diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/parsers/TikaOfficeDetectParser.java b/engines/tika/src/main/java/org/alfresco/transform/tika/parsers/TikaOfficeDetectParser.java index 8d0409a9..0a47b449 100644 --- a/engines/tika/src/main/java/org/alfresco/transform/tika/parsers/TikaOfficeDetectParser.java +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/parsers/TikaOfficeDetectParser.java @@ -47,14 +47,7 @@ import org.xml.sax.SAXException; ///////// THIS FILE WAS A COPY OF THE CODE IN alfresco-repository ///////////// /** - * Apache Tika assumes that - * you either know exactly what your content is, or that - * you'll leave it to auto-detection. - * Within Alfresco, we usually do know. However, from time - * to time, we don't know if we have one of the old or one - * of the new office files (eg .xls and .xlsx). - * This class allows automatically selects the appropriate - * old (OLE2) or new (OOXML) Tika parser as required. + * Apache Tika assumes that you either know exactly what your content is, or that you'll leave it to auto-detection. Within Alfresco, we usually do know. However, from time to time, we don't know if we have one of the old or one of the new office files (eg .xls and .xlsx). This class allows automatically selects the appropriate old (OLE2) or new (OOXML) Tika parser as required. * * @author Nick Burch */ @@ -72,9 +65,9 @@ public class TikaOfficeDetectParser implements Parser } public void parse(InputStream stream, - ContentHandler handler, Metadata metadata, - ParseContext parseContext) throws IOException, SAXException, - TikaException + ContentHandler handler, Metadata metadata, + ParseContext parseContext) throws IOException, SAXException, + TikaException { byte[] initial4 = new byte[4]; InputStream wrapped; @@ -109,8 +102,8 @@ public class TikaOfficeDetectParser implements Parser * @deprecated This method will be removed in Apache Tika 1.0. */ public void parse(InputStream stream, - ContentHandler handler, Metadata metadata) - throws IOException, SAXException, TikaException + ContentHandler handler, Metadata metadata) + throws IOException, SAXException, TikaException { parse(stream, handler, metadata, new ParseContext()); } diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/transformers/AbstractTikaTransformer.java b/engines/tika/src/main/java/org/alfresco/transform/tika/transformers/AbstractTikaTransformer.java index cdb6ad25..6f9ad9e3 100644 --- a/engines/tika/src/main/java/org/alfresco/transform/tika/transformers/AbstractTikaTransformer.java +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/transformers/AbstractTikaTransformer.java @@ -26,18 +26,7 @@ */ package org.alfresco.transform.tika.transformers; -import org.alfresco.transform.base.CustomTransformer; -import org.alfresco.transform.base.TransformManager; -import org.alfresco.transform.base.executors.RuntimeExec; -import org.alfresco.transform.base.logging.LogEntry; -import org.alfresco.transform.common.RequestParamMap; -import org.apache.tika.extractor.DocumentSelector; -import org.apache.tika.parser.Parser; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; +import static java.lang.Boolean.parseBoolean; import java.io.InputStream; import java.io.OutputStream; @@ -47,7 +36,19 @@ import java.util.Map; import java.util.Objects; import java.util.StringJoiner; -import static java.lang.Boolean.parseBoolean; +import org.apache.tika.extractor.DocumentSelector; +import org.apache.tika.parser.Parser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; + +import org.alfresco.transform.base.CustomTransformer; +import org.alfresco.transform.base.TransformManager; +import org.alfresco.transform.base.executors.RuntimeExec; +import org.alfresco.transform.base.logging.LogEntry; +import org.alfresco.transform.common.RequestParamMap; public abstract class AbstractTikaTransformer implements CustomTransformer { @@ -85,7 +86,7 @@ public abstract class AbstractTikaTransformer implements CustomTransformer public String getTransformerName() { String simpleClassName = getClass().getSimpleName(); - return simpleClassName.substring(0, simpleClassName.length()-"Transformer".length()); + return simpleClassName.substring(0, simpleClassName.length() - "Transformer".length()); } @Override diff --git a/engines/tika/src/main/java/org/alfresco/transform/tika/transformers/Tika.java b/engines/tika/src/main/java/org/alfresco/transform/tika/transformers/Tika.java index cf8f06ad..7b3fb01e 100644 --- a/engines/tika/src/main/java/org/alfresco/transform/tika/transformers/Tika.java +++ b/engines/tika/src/main/java/org/alfresco/transform/tika/transformers/Tika.java @@ -26,8 +26,33 @@ */ package org.alfresco.transform.tika.transformers; +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_IMAGE_PNG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_TIFF; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_CSV; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_XHTML; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_XML; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.net.URL; +import java.util.List; +import java.util.Objects; +import java.util.regex.Pattern; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.stream.StreamResult; + import com.google.common.collect.ImmutableList; -import org.alfresco.transform.tika.parsers.TikaOfficeDetectParser; import org.apache.tika.config.TikaConfig; import org.apache.tika.exception.TikaException; import org.apache.tika.extractor.DocumentSelector; @@ -49,31 +74,7 @@ import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.sax.SAXTransformerFactory; -import javax.xml.transform.sax.TransformerHandler; -import javax.xml.transform.stream.StreamResult; -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.UnsupportedEncodingException; -import java.io.Writer; -import java.net.URL; -import java.util.List; -import java.util.Objects; -import java.util.regex.Pattern; - -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_IMAGE_PNG; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_TIFF; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_CSV; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_XHTML; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_XML; +import org.alfresco.transform.tika.parsers.TikaOfficeDetectParser; @Component public class Tika @@ -113,10 +114,9 @@ public class Tika public static final Parser tikaOfficeDetectParser = new TikaOfficeDetectParser(); public final PDFParserConfig pdfParserConfig = new PDFParserConfig(); - public static final DocumentSelector pdfBoxEmbededDocumentSelector = new DocumentSelector() - { + public static final DocumentSelector pdfBoxEmbededDocumentSelector = new DocumentSelector() { private final List disabledMediaTypes = ImmutableList.of(MIMETYPE_IMAGE_JPEG, - MIMETYPE_IMAGE_TIFF, MIMETYPE_IMAGE_PNG); + MIMETYPE_IMAGE_TIFF, MIMETYPE_IMAGE_PNG); @Override public boolean select(Metadata metadata) @@ -198,7 +198,7 @@ public class Tika notExtractBookmarksText = notExtractBookmarksText == null ? false : notExtractBookmarksText; transform(parser, documentSelector, includeContents, notExtractBookmarksText, inputStream, - outputStream, targetMimetype, targetEncoding); + outputStream, targetMimetype, targetEncoding); } private String getValue(String arg, boolean valueExpected, Object value, String optionName) @@ -220,23 +220,23 @@ public class Tika } private void transform(Parser parser, DocumentSelector documentSelector, - Boolean includeContents, - Boolean notExtractBookmarksText, - InputStream inputStream, - OutputStream outputStream, String targetMimetype, String targetEncoding) + Boolean includeContents, + Boolean notExtractBookmarksText, + InputStream inputStream, + OutputStream outputStream, String targetMimetype, String targetEncoding) { try (Writer ow = new BufferedWriter(new OutputStreamWriter(outputStream, targetEncoding))) { Metadata metadata = new Metadata(); ParseContext context = buildParseContext(documentSelector, includeContents, - notExtractBookmarksText); + notExtractBookmarksText); ContentHandler handler = getContentHandler(targetMimetype, ow); parser.parse(inputStream, handler, metadata, context); } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("Unsupported encoding "+e.getMessage(), e); + throw new IllegalArgumentException("Unsupported encoding " + e.getMessage(), e); } catch (SAXException | TikaException | IOException e) { @@ -268,7 +268,7 @@ public class Tika return new ExpandedTitleContentHandler(transformerHandler); } else if (MIMETYPE_XHTML.equals(targetMimetype) || - MIMETYPE_XML.equals(targetMimetype)) + MIMETYPE_XML.equals(targetMimetype)) { transformerHandler.getTransformer().setOutputProperty(OutputKeys.METHOD, XML); } @@ -307,7 +307,7 @@ public class Tika @Override public void ignorableWhitespace(char[] ch, int start, int length) - throws SAXException + throws SAXException { if (length == 1 && ch[0] == '\t') { @@ -321,7 +321,7 @@ public class Tika @Override public void characters(char[] ch, int start, int length) - throws SAXException + throws SAXException { if (inCell) { @@ -357,7 +357,7 @@ public class Tika @Override public void startElement(String uri, String localName, String name, - Attributes atts) throws SAXException + Attributes atts) throws SAXException { if (localName.equals("td")) { @@ -376,7 +376,7 @@ public class Tika @Override public void endElement(String uri, String localName, String name) - throws SAXException + throws SAXException { if (localName.equals("td")) { @@ -395,7 +395,7 @@ public class Tika } private ParseContext buildParseContext(DocumentSelector documentSelector, - Boolean includeContents, Boolean notExtractBookmarksText) + Boolean includeContents, Boolean notExtractBookmarksText) { ParseContext context = new ParseContext(); diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaLivenessReadinessProbeIT.java b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaLivenessReadinessProbeIT.java index dddb9e1e..579b182b 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaLivenessReadinessProbeIT.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaLivenessReadinessProbeIT.java @@ -29,10 +29,11 @@ package org.alfresco.transform.tika; import org.alfresco.transform.base.LivenessReadinessProbeTest; - -public class TikaLivenessReadinessProbeIT extends LivenessReadinessProbeTest { +public class TikaLivenessReadinessProbeIT extends LivenessReadinessProbeTest +{ @Override - protected ImagesForTests getImageForTest() { + protected ImagesForTests getImageForTest() + { return new ImagesForTests("alfresco-tika", "text/plain", "text/plain", "original.txt"); } } diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaMetadataExtractsIT.java b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaMetadataExtractsIT.java index 4a018810..368a501f 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaMetadataExtractsIT.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaMetadataExtractsIT.java @@ -26,13 +26,6 @@ */ package org.alfresco.transform.tika; -import org.alfresco.transform.base.clients.FileInfo; -import org.alfresco.transform.base.metadata.AbstractMetadataExtractsIT; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; - -import java.util.stream.Stream; - import static org.alfresco.transform.base.clients.FileInfo.testFile; import static org.alfresco.transform.common.Mimetype.MIMETYPE_APP_DWG; import static org.alfresco.transform.common.Mimetype.MIMETYPE_AUDIO_MP4; @@ -79,6 +72,14 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; import static org.alfresco.transform.common.Mimetype.MIMETYPE_XML; import static org.alfresco.transform.common.Mimetype.MIMETYPE_ZIP; +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import org.alfresco.transform.base.clients.FileInfo; +import org.alfresco.transform.base.metadata.AbstractMetadataExtractsIT; + /** * Metadata integration tests in the Tika T-Engine. * @@ -105,7 +106,7 @@ public class TikaMetadataExtractsIT extends AbstractMetadataExtractsIT // either no quick file or the target extension has not been registered. return Stream.of( - //IPTCMetadataExtractor + // IPTCMetadataExtractor testFile(MIMETYPE_IMAGE_JPEG, "jpg", "quick.jpg"), testFile(MIMETYPE_IMAGE_JPEG, "jpg", "quickIPTC-EXT.jpg"), testFile(MIMETYPE_IMAGE_JPEG, "jpg", "quickIPTC-multi-creator.jpg"), @@ -117,7 +118,7 @@ public class TikaMetadataExtractsIT extends AbstractMetadataExtractsIT testFile(MIMETYPE_IMAGE_RAW_RW2, "rw2", "20140629_145035_Flower.RW2"), testFile(MIMETYPE_IMAGE_RAW_NEF, "nef", "20150408_074941_Bush.NEF"), testFile(MIMETYPE_IMAGE_RAW_RAF, "raf", "20160502_190928_London_Underground.RAF"), - + // DWGMetadataExtractor testFile(MIMETYPE_APP_DWG, "dwg", "quick2010CustomProps.dwg"), @@ -129,97 +130,97 @@ public class TikaMetadataExtractsIT extends AbstractMetadataExtractsIT // OfficeMetadataExtractor testFile(MIMETYPE_WORD, "doc", "quick.doc"), - //testFile("application/x-tika-msoffice-embedded; format=ole10_native", "", ""), + // testFile("application/x-tika-msoffice-embedded; format=ole10_native", "", ""), testFile(MIMETYPE_VISIO, "vsd", "quick.vsd"), - //testFile("application/vnd.ms-project", "mpp", ""), - //testFile("application/x-tika-msworks-spreadsheet", "", ""), - //testFile("application/x-mspublisher", "", ""), + // testFile("application/vnd.ms-project", "mpp", ""), + // testFile("application/x-tika-msworks-spreadsheet", "", ""), + // testFile("application/x-mspublisher", "", ""), testFile(MIMETYPE_PPT, "ppt", "quick.ppt"), - //testFile("application/x-tika-msoffice", "", ""), - //testFile(MIMETYPE_VISIO_2013, "vsdx", ""), - //testFile("application/sldworks", "", ""), - //testFile(MIMETYPE_ENCRYPTED_OFFICE, "", ""), + // testFile("application/x-tika-msoffice", "", ""), + // testFile(MIMETYPE_VISIO_2013, "vsdx", ""), + // testFile("application/sldworks", "", ""), + // testFile(MIMETYPE_ENCRYPTED_OFFICE, "", ""), testFile(MIMETYPE_EXCEL, "xls", "quick.xls"), // OpenDocumentMetadataExtractor - //testFile("application/x-vnd.oasis.opendocument.presentation", "", ""), - //testFile(MIMETYPE_OPENDOCUMENT_CHART, "odc", ""), - //testFile(MIMETYPE_OPENDOCUMENT_IMAGE_TEMPLATE, "", ""), - //testFile("application/x-vnd.oasis.opendocument.text-web", "", ""), - //testFile("application/x-vnd.oasis.opendocument.image", "", ""), + // testFile("application/x-vnd.oasis.opendocument.presentation", "", ""), + // testFile(MIMETYPE_OPENDOCUMENT_CHART, "odc", ""), + // testFile(MIMETYPE_OPENDOCUMENT_IMAGE_TEMPLATE, "", ""), + // testFile("application/x-vnd.oasis.opendocument.text-web", "", ""), + // testFile("application/x-vnd.oasis.opendocument.image", "", ""), testFile(MIMETYPE_OPENDOCUMENT_GRAPHICS_TEMPLATE, "otg", "quick.otg"), - //testFile(MIMETYPE_OPENDOCUMENT_TEXT_WEB, "oth", ""), - //testFile("application/x-vnd.oasis.opendocument.spreadsheet-template", "", ""), + // testFile(MIMETYPE_OPENDOCUMENT_TEXT_WEB, "oth", ""), + // testFile("application/x-vnd.oasis.opendocument.spreadsheet-template", "", ""), testFile(MIMETYPE_OPENDOCUMENT_SPREADSHEET_TEMPLATE, "ots", "quick.ots"), testFile(MIMETYPE_OPENOFFICE1_WRITER, "sxw", "quick.sxw"), - //testFile("application/x-vnd.oasis.opendocument.graphics-template", "", ""), + // testFile("application/x-vnd.oasis.opendocument.graphics-template", "", ""), testFile(MIMETYPE_OPENDOCUMENT_GRAPHICS, "odg", "quick.odg"), testFile(MIMETYPE_OPENDOCUMENT_SPREADSHEET, "ods", "quick.ods"), - //testFile("application/x-vnd.oasis.opendocument.chart", "", ""), - //testFile("application/x-vnd.oasis.opendocument.spreadsheet", "", ""), - //testFile(MIMETYPE_OPENDOCUMENT_IMAGE, "odi", ""), - //testFile("application/x-vnd.oasis.opendocument.text", "", ""), - //testFile("application/x-vnd.oasis.opendocument.text-template", "", ""), - //testFile("application/vnd.oasis.opendocument.formula-template", "", ""), - //testFile("application/x-vnd.oasis.opendocument.formula", "", ""), - //testFile("application/vnd.oasis.opendocument.image-template", "", ""), - //testFile("application/x-vnd.oasis.opendocument.image-template", "", ""), - //testFile("application/x-vnd.oasis.opendocument.presentation-template", "", ""), + // testFile("application/x-vnd.oasis.opendocument.chart", "", ""), + // testFile("application/x-vnd.oasis.opendocument.spreadsheet", "", ""), + // testFile(MIMETYPE_OPENDOCUMENT_IMAGE, "odi", ""), + // testFile("application/x-vnd.oasis.opendocument.text", "", ""), + // testFile("application/x-vnd.oasis.opendocument.text-template", "", ""), + // testFile("application/vnd.oasis.opendocument.formula-template", "", ""), + // testFile("application/x-vnd.oasis.opendocument.formula", "", ""), + // testFile("application/vnd.oasis.opendocument.image-template", "", ""), + // testFile("application/x-vnd.oasis.opendocument.image-template", "", ""), + // testFile("application/x-vnd.oasis.opendocument.presentation-template", "", ""), testFile(MIMETYPE_OPENDOCUMENT_PRESENTATION_TEMPLATE, "otp", "quick.otp"), testFile(MIMETYPE_OPENDOCUMENT_TEXT, "odt", "quick.odt"), - //testFile(MIMETYPE_OPENDOCUMENT_FORMULA_TEMPLATE, "", ""), + // testFile(MIMETYPE_OPENDOCUMENT_FORMULA_TEMPLATE, "", ""), testFile(MIMETYPE_OPENDOCUMENT_TEXT_TEMPLATE, "ott", "quick.ott"), - //testFile("application/vnd.oasis.opendocument.chart-template", "", ""), - //testFile("application/x-vnd.oasis.opendocument.chart-template", "", ""), - //testFile("application/x-vnd.oasis.opendocument.formula-template", "", ""), - //testFile(MIMETYPE_OPENDOCUMENT_DATABASE, "odb", ""), - //testFile("application/x-vnd.oasis.opendocument.text-master", "", ""), + // testFile("application/vnd.oasis.opendocument.chart-template", "", ""), + // testFile("application/x-vnd.oasis.opendocument.chart-template", "", ""), + // testFile("application/x-vnd.oasis.opendocument.formula-template", "", ""), + // testFile(MIMETYPE_OPENDOCUMENT_DATABASE, "odb", ""), + // testFile("application/x-vnd.oasis.opendocument.text-master", "", ""), testFile(MIMETYPE_OPENDOCUMENT_PRESENTATION, "odp", "quick.odp"), - //testFile(MIMETYPE_OPENDOCUMENT_CHART_TEMPLATE, "", ""), - //testFile("application/x-vnd.oasis.opendocument.graphics", "", ""), + // testFile(MIMETYPE_OPENDOCUMENT_CHART_TEMPLATE, "", ""), + // testFile("application/x-vnd.oasis.opendocument.graphics", "", ""), testFile(MIMETYPE_OPENDOCUMENT_FORMULA, "odf", "quick.odf"), - //testFile(MIMETYPE_OPENDOCUMENT_TEXT_MASTER, "odm", ""), + // testFile(MIMETYPE_OPENDOCUMENT_TEXT_MASTER, "odm", ""), // PdfBoxMetadataExtractor testFile(MIMETYPE_PDF, "pdf", "quick.pdf"), - //testFile(MIMETYPE_APPLICATION_ILLUSTRATOR, "ai", ""), + // testFile(MIMETYPE_APPLICATION_ILLUSTRATOR, "ai", ""), // PoiMetadataExtractor - //testFile(MIMETYPE_OPENXML_PRESENTATION_TEMPLATE_MACRO, "potm", ""), - //testFile(MIMETYPE_OPENXML_SPREADSHEET_ADDIN_MACRO, "xlam", ""), - //testFile(MIMETYPE_OPENXML_WORD_TEMPLATE, "dotx", ""), - //testFile(MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO, "xlsb", ""), + // testFile(MIMETYPE_OPENXML_PRESENTATION_TEMPLATE_MACRO, "potm", ""), + // testFile(MIMETYPE_OPENXML_SPREADSHEET_ADDIN_MACRO, "xlam", ""), + // testFile(MIMETYPE_OPENXML_WORD_TEMPLATE, "dotx", ""), + // testFile(MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO, "xlsb", ""), testFile(MIMETYPE_OPENXML_WORDPROCESSING, "docx", "quick.docx"), - //testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDE_MACRO, "sldm", ""), - //testFile("application/vnd.ms-visio.drawing", "", ""), - //testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW_MACRO, "ppsm", ""), - //testFile(MIMETYPE_OPENXML_PRESENTATION_MACRO, "pptm", ""), - //testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDE, "sldx", ""), - //testFile(MIMETYPE_OPENXML_SPREADSHEET_MACRO, "xlsm", ""), - //testFile(MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO, "dotm", ""), - //testFile(MIMETYPE_OPENXML_WORDPROCESSING_MACRO, "docm", ""), - //testFile(MIMETYPE_OPENXML_PRESENTATION_ADDIN, "ppam", ""), - //testFile(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE, "xltx", ""), - //testFile("application/vnd.ms-xpsdocument", "", ""), - //testFile("application/vnd.ms-visio.drawing.macroenabled.12", "", ""), - //testFile("application/vnd.ms-visio.template.macroenabled.12", "", ""), - //testFile("model/vnd.dwfx+xps", "", ""), - //testFile(MIMETYPE_OPENXML_PRESENTATION_TEMPLATE, "potx", ""), + // testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDE_MACRO, "sldm", ""), + // testFile("application/vnd.ms-visio.drawing", "", ""), + // testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW_MACRO, "ppsm", ""), + // testFile(MIMETYPE_OPENXML_PRESENTATION_MACRO, "pptm", ""), + // testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDE, "sldx", ""), + // testFile(MIMETYPE_OPENXML_SPREADSHEET_MACRO, "xlsm", ""), + // testFile(MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO, "dotm", ""), + // testFile(MIMETYPE_OPENXML_WORDPROCESSING_MACRO, "docm", ""), + // testFile(MIMETYPE_OPENXML_PRESENTATION_ADDIN, "ppam", ""), + // testFile(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE, "xltx", ""), + // testFile("application/vnd.ms-xpsdocument", "", ""), + // testFile("application/vnd.ms-visio.drawing.macroenabled.12", "", ""), + // testFile("application/vnd.ms-visio.template.macroenabled.12", "", ""), + // testFile("model/vnd.dwfx+xps", "", ""), + // testFile(MIMETYPE_OPENXML_PRESENTATION_TEMPLATE, "potx", ""), testFile(MIMETYPE_OPENXML_PRESENTATION, "pptx", "quick.pptx"), testFile(MIMETYPE_OPENXML_SPREADSHEET, "xlsx", "quick.xlsx"), - //testFile("application/vnd.ms-visio.stencil", "", ""), - //testFile("application/vnd.ms-visio.template", "", ""), - //testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW, "ppsx", ""), - //testFile("application/vnd.ms-visio.stencil.macroenabled.12", "", ""), - //testFile(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO, "xltm", ""), + // testFile("application/vnd.ms-visio.stencil", "", ""), + // testFile("application/vnd.ms-visio.template", "", ""), + // testFile(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW, "ppsx", ""), + // testFile("application/vnd.ms-visio.stencil.macroenabled.12", "", ""), + // testFile(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO, "xltm", ""), // TikaAudioMetadataExtractor testFile("video/x-m4v", "m4v", "quick.m4v"), - //testFile("audio/x-oggflac", "", ""), - //testFile("application/mp4", "", ""), + // testFile("audio/x-oggflac", "", ""), + // testFile("application/mp4", "", ""), testFile(MIMETYPE_VORBIS, "ogg", "quick.ogg"), testFile(MIMETYPE_VIDEO_3GP, "3gp", "quick.3gp"), - //testFile(MIMETYPE_FLAC, "flac", ""), + // testFile(MIMETYPE_FLAC, "flac", ""), testFile(MIMETYPE_VIDEO_3GP2, "3g2", "quick.3g2"), testFile(MIMETYPE_VIDEO_QUICKTIME, "mov", "quick.mov"), testFile(MIMETYPE_AUDIO_MP4, "m4a", "quick.m4a"), @@ -238,304 +239,304 @@ public class TikaMetadataExtractsIT extends AbstractMetadataExtractsIT // contain one or more values, but also include nulls. Again this may be correct, a bug or just the // example quick file rather than a problem with the extractor. - //testFile("application/vnd.ms-htmlhelp", "", ""), - //testFile(MIMETYPE_ATOM, "", ""), - //testFile("audio/midi", "", ""), - //testFile("application/aaigrid", "", ""), - //testFile("application/x-bag", "", ""), + // testFile("application/vnd.ms-htmlhelp", "", ""), + // testFile(MIMETYPE_ATOM, "", ""), + // testFile("audio/midi", "", ""), + // testFile("application/aaigrid", "", ""), + // testFile("application/x-bag", "", ""), testFile(MIMETYPE_IWORK_KEYNOTE, "key", "quick.key"), - //testFile("application/x-quattro-pro; version=9", "", ""), - //testFile("application/x-ibooks+zip", "", ""), - //testFile("audio/wave", "", ""), - //testFile("application/x-midi", "", ""), + // testFile("application/x-quattro-pro; version=9", "", ""), + // testFile("application/x-ibooks+zip", "", ""), + // testFile("audio/wave", "", ""), + // testFile("application/x-midi", "", ""), testFile(MIMETYPE_XML, "xml", "quick.xml"), - //testFile(MIMETYPE_RSS, "rss", ""), - //testFile("application/x-netcdf", "cdf", ""), - //testFile("video/x-daala", "", ""), - //testFile("application/matlab-mat", "", ""), - //testFile("audio/aiff", "", ""), - //testFile("application/jaxa-pal-sar", "", ""), - //testFile("image/x-pcraster", "", ""), - //testFile("image/arg", "", ""), - //testFile("application/x-kro", "", ""), - //testFile("image/x-hdf5-image", "", ""), - //testFile("audio/speex", "", ""), - //testFile("image/big-gif", "", ""), - //testFile("application/zlib", "", ""), - //testFile("application/x-cosar", "", ""), - //testFile("application/x-ntv2", "", ""), - //testFile("application/x-archive", "", ""), - //testFile("application/java-archive", "jar", ""), - //testFile("application/x-vnd.sun.xml.writer", "", ""), - //testFile("application/x-gmt", "", ""), - //testFile("application/x-xml", "", ""), - //testFile("application/gzip-compressed", "", ""), - //testFile("image/ida", "", ""), - //testFile("text/x-groovy", "", ""), - //testFile("image/x-emf", "", ""), - //testFile("application/x-rar", "", ""), - //testFile("image/sar-ceos", "", ""), - //testFile("application/acad", "", ""), + // testFile(MIMETYPE_RSS, "rss", ""), + // testFile("application/x-netcdf", "cdf", ""), + // testFile("video/x-daala", "", ""), + // testFile("application/matlab-mat", "", ""), + // testFile("audio/aiff", "", ""), + // testFile("application/jaxa-pal-sar", "", ""), + // testFile("image/x-pcraster", "", ""), + // testFile("image/arg", "", ""), + // testFile("application/x-kro", "", ""), + // testFile("image/x-hdf5-image", "", ""), + // testFile("audio/speex", "", ""), + // testFile("image/big-gif", "", ""), + // testFile("application/zlib", "", ""), + // testFile("application/x-cosar", "", ""), + // testFile("application/x-ntv2", "", ""), + // testFile("application/x-archive", "", ""), + // testFile("application/java-archive", "jar", ""), + // testFile("application/x-vnd.sun.xml.writer", "", ""), + // testFile("application/x-gmt", "", ""), + // testFile("application/x-xml", "", ""), + // testFile("application/gzip-compressed", "", ""), + // testFile("image/ida", "", ""), + // testFile("text/x-groovy", "", ""), + // testFile("image/x-emf", "", ""), + // testFile("application/x-rar", "", ""), + // testFile("image/sar-ceos", "", ""), + // testFile("application/acad", "", ""), testFile(MIMETYPE_ZIP, "zip", "quick.zip"), - //testFile(MIMETYPE_IMAGE_PSD, "psd", ""), - //testFile("application/x-sharedlib", "", ""), - //testFile("audio/x-m4a", "", ""), - //testFile("image/webp", "", ""), - //testFile("application/vnd.wap.xhtml+xml", "", ""), - //testFile("audio/x-aiff", "aiff", ""), - //testFile("application/vnd.ms-spreadsheetml", "", ""), - //testFile("image/x-airsar", "", ""), - //testFile("application/x-pcidsk", "", ""), - //testFile("application/x-java-pack200", "", ""), - //testFile("image/x-fujibas", "", ""), - //testFile("application/x-zmap", "", ""), - //testFile("image/x-bmp", "", ""), - //testFile("image/bpg", "", ""), - //testFile(MIMETYPE_RTF, "rtf", ""), - //testFile("application/x-xz", "", ""), - //testFile("application/x-speex", "", ""), - //testFile("audio/ogg; codecs=speex", "", ""), - //testFile("application/x-l1b", "", ""), - //testFile("application/x-gsbg", "", ""), - //testFile("application/x-sdat", "", ""), - //testFile("application/vnd.ms-visio", "", ""), - //testFile("application/x-coredump", "", ""), - //testFile("application/x-msaccess", "", ""), - //testFile("application/x-dods", "", ""), + // testFile(MIMETYPE_IMAGE_PSD, "psd", ""), + // testFile("application/x-sharedlib", "", ""), + // testFile("audio/x-m4a", "", ""), + // testFile("image/webp", "", ""), + // testFile("application/vnd.wap.xhtml+xml", "", ""), + // testFile("audio/x-aiff", "aiff", ""), + // testFile("application/vnd.ms-spreadsheetml", "", ""), + // testFile("image/x-airsar", "", ""), + // testFile("application/x-pcidsk", "", ""), + // testFile("application/x-java-pack200", "", ""), + // testFile("image/x-fujibas", "", ""), + // testFile("application/x-zmap", "", ""), + // testFile("image/x-bmp", "", ""), + // testFile("image/bpg", "", ""), + // testFile(MIMETYPE_RTF, "rtf", ""), + // testFile("application/x-xz", "", ""), + // testFile("application/x-speex", "", ""), + // testFile("audio/ogg; codecs=speex", "", ""), + // testFile("application/x-l1b", "", ""), + // testFile("application/x-gsbg", "", ""), + // testFile("application/x-sdat", "", ""), + // testFile("application/vnd.ms-visio", "", ""), + // testFile("application/x-coredump", "", ""), + // testFile("application/x-msaccess", "", ""), + // testFile("application/x-dods", "", ""), testFile(MIMETYPE_IMAGE_PNG, "png", "quick.png"), - //testFile("application/vnd.ms-outlook-pst", "", ""), - //testFile("image/bsb", "", ""), - //testFile("application/x-cpio", "cpio", ""), - //testFile("audio/ogg", "oga", ""), + // testFile("application/vnd.ms-outlook-pst", "", ""), + // testFile("image/bsb", "", ""), + // testFile("application/x-cpio", "cpio", ""), + // testFile("audio/ogg", "oga", ""), testFile("application/x-tar", "tar", "quick.tar"), - //testFile("application/x-dbf", "", ""), - //testFile("video/x-ogm", "", ""), - //testFile("application/x-los-las", "", ""), - //testFile("application/autocad_dwg", "", ""), - //testFile("application/vnd.ms-excel.workspace.3", "", ""), - //testFile("application/vnd.ms-excel.workspace.4", "", ""), - //testFile("image/x-bpg", "", ""), - //testFile("gzip/document", "", ""), - //testFile("text/x-java", "", ""), - //testFile("application/x-brotli", "", ""), - //testFile("application/elas", "", ""), - //testFile("image/x-jb2", "", ""), - //testFile("application/x-cappi", "", ""), - //testFile("application/epub+zip", "", ""), - //testFile("application/x-ace2", "", ""), - //testFile("application/x-sas-data", "", ""), - //testFile("application/x-hdf", "hdf", ""), - //testFile("image/x-mff", "", ""), - //testFile("image/x-srp", "", ""), + // testFile("application/x-dbf", "", ""), + // testFile("video/x-ogm", "", ""), + // testFile("application/x-los-las", "", ""), + // testFile("application/autocad_dwg", "", ""), + // testFile("application/vnd.ms-excel.workspace.3", "", ""), + // testFile("application/vnd.ms-excel.workspace.4", "", ""), + // testFile("image/x-bpg", "", ""), + // testFile("gzip/document", "", ""), + // testFile("text/x-java", "", ""), + // testFile("application/x-brotli", "", ""), + // testFile("application/elas", "", ""), + // testFile("image/x-jb2", "", ""), + // testFile("application/x-cappi", "", ""), + // testFile("application/epub+zip", "", ""), + // testFile("application/x-ace2", "", ""), + // testFile("application/x-sas-data", "", ""), + // testFile("application/x-hdf", "hdf", ""), + // testFile("image/x-mff", "", ""), + // testFile("image/x-srp", "", ""), testFile(MIMETYPE_IMAGE_BMP, "bmp", "quick.bmp"), - //testFile("video/x-ogguvs", "", ""), - //testFile("drawing/dwg", "", ""), - //testFile("application/x-doq2", "", ""), - //testFile("application/x-acad", "", ""), - //testFile("application/x-kml", "", ""), - //testFile("application/x-autocad", "", ""), - //testFile("image/x-mff2", "", ""), - //testFile("application/x-snodas", "", ""), - //testFile("application/terragen", "", ""), - //testFile("application/x-wcs", "", ""), - //testFile("text/x-c++src", "", ""), - //testFile("application/timestamped-data", "", ""), + // testFile("video/x-ogguvs", "", ""), + // testFile("drawing/dwg", "", ""), + // testFile("application/x-doq2", "", ""), + // testFile("application/x-acad", "", ""), + // testFile("application/x-kml", "", ""), + // testFile("application/x-autocad", "", ""), + // testFile("image/x-mff2", "", ""), + // testFile("application/x-snodas", "", ""), + // testFile("application/terragen", "", ""), + // testFile("application/x-wcs", "", ""), + // testFile("text/x-c++src", "", ""), + // testFile("application/timestamped-data", "", ""), testFile(MIMETYPE_IMAGE_TIFF, "tiff", "quick.tiff"), - //testFile("application/msexcel", "", ""), - //testFile("application/x-asp", "", ""), - //testFile("application/x-rar-compressed", "rar", ""), - //testFile("application/x-envi-hdr", "", ""), - //testFile("text/iso19139+xml", "", ""), - //testFile("application/vnd.ms-tnef", "", ""), - //testFile("application/x-ecrg-toc", "", ""), - //testFile("application/aig", "", ""), - //testFile("audio/x-wav", "wav", ""), - //testFile("image/emf", "", ""), - //testFile("application/x-bzip", "", ""), - //testFile("application/jdem", "", ""), - //testFile("application/x-webp", "", ""), - //testFile("application/x-arj", "", ""), - //testFile("application/x-lzma", "", ""), - //testFile("application/x-java-vm", "", ""), - //testFile("image/envisat", "", ""), - //testFile("application/x-doq1", "", ""), - //testFile("audio/vnd.wave", "", ""), - //testFile("application/x-ppi", "", ""), - //testFile("image/ilwis", "", ""), - //testFile("application/x-gunzip", "", ""), - //testFile("image/x-icon", "", ""), - //testFile("application/ogg", "ogx", ""), - //testFile(MIMETYPE_IMAGE_SVG, "svg", ""), - //testFile("application/x-ms-owner", "", ""), - //testFile("application/x-grib", "", ""), - //testFile("application/ms-tnef", "", ""), - //testFile("image/fits", "", ""), - //testFile("audio/x-mpeg", "", ""), - //testFile("application/x-bzip2", "", ""), - //testFile("text/tsv", "", ""), - //testFile("application/x-fictionbook+xml", "", ""), - //testFile("application/x-p-aux", "", ""), - //testFile("application/x-font-ttf", "", ""), - //testFile("image/x-xcf", "", ""), - //testFile("image/x-ms-bmp", "", ""), - //testFile("image/wmf", "", ""), - //testFile("image/eir", "", ""), - //testFile("application/x-matlab-data", "", ""), - //testFile("application/deflate64", "", ""), - //testFile("audio/wav", "", ""), - //testFile("application/x-rs2", "", ""), - //testFile("application/vnd.ms-word", "", ""), - //testFile("application/x-tsx", "", ""), - //testFile("application/x-lcp", "", ""), - //testFile("application/x-mbtiles", "", ""), - //testFile("audio/x-oggpcm", "", ""), - //testFile("application/x-epsilon", "", ""), - //testFile("application/x-msgn", "", ""), - //testFile(MIMETYPE_TEXT_CSV, "csv", ""), - //testFile("image/x-dimap", "", ""), - //testFile("image/vnd.microsoft.icon", "", ""), - //testFile("application/x-envi", "", ""), - //testFile("application/x-dwg", "", ""), + // testFile("application/msexcel", "", ""), + // testFile("application/x-asp", "", ""), + // testFile("application/x-rar-compressed", "rar", ""), + // testFile("application/x-envi-hdr", "", ""), + // testFile("text/iso19139+xml", "", ""), + // testFile("application/vnd.ms-tnef", "", ""), + // testFile("application/x-ecrg-toc", "", ""), + // testFile("application/aig", "", ""), + // testFile("audio/x-wav", "wav", ""), + // testFile("image/emf", "", ""), + // testFile("application/x-bzip", "", ""), + // testFile("application/jdem", "", ""), + // testFile("application/x-webp", "", ""), + // testFile("application/x-arj", "", ""), + // testFile("application/x-lzma", "", ""), + // testFile("application/x-java-vm", "", ""), + // testFile("image/envisat", "", ""), + // testFile("application/x-doq1", "", ""), + // testFile("audio/vnd.wave", "", ""), + // testFile("application/x-ppi", "", ""), + // testFile("image/ilwis", "", ""), + // testFile("application/x-gunzip", "", ""), + // testFile("image/x-icon", "", ""), + // testFile("application/ogg", "ogx", ""), + // testFile(MIMETYPE_IMAGE_SVG, "svg", ""), + // testFile("application/x-ms-owner", "", ""), + // testFile("application/x-grib", "", ""), + // testFile("application/ms-tnef", "", ""), + // testFile("image/fits", "", ""), + // testFile("audio/x-mpeg", "", ""), + // testFile("application/x-bzip2", "", ""), + // testFile("text/tsv", "", ""), + // testFile("application/x-fictionbook+xml", "", ""), + // testFile("application/x-p-aux", "", ""), + // testFile("application/x-font-ttf", "", ""), + // testFile("image/x-xcf", "", ""), + // testFile("image/x-ms-bmp", "", ""), + // testFile("image/wmf", "", ""), + // testFile("image/eir", "", ""), + // testFile("application/x-matlab-data", "", ""), + // testFile("application/deflate64", "", ""), + // testFile("audio/wav", "", ""), + // testFile("application/x-rs2", "", ""), + // testFile("application/vnd.ms-word", "", ""), + // testFile("application/x-tsx", "", ""), + // testFile("application/x-lcp", "", ""), + // testFile("application/x-mbtiles", "", ""), + // testFile("audio/x-oggpcm", "", ""), + // testFile("application/x-epsilon", "", ""), + // testFile("application/x-msgn", "", ""), + // testFile(MIMETYPE_TEXT_CSV, "csv", ""), + // testFile("image/x-dimap", "", ""), + // testFile("image/vnd.microsoft.icon", "", ""), + // testFile("application/x-envi", "", ""), + // testFile("application/x-dwg", "", ""), testFile(MIMETYPE_IWORK_NUMBERS, "numbers", "quick.numbers"), - //testFile("application/vnd.ms-word2006ml", "", ""), - //testFile("application/x-bt", "", ""), - //testFile("application/x-font-adobe-metric", "", ""), - //testFile("application/x-rst", "", ""), - //testFile("application/vrt", "", ""), - //testFile("application/x-ctg", "", ""), - //testFile("application/x-e00-grid", "", ""), - //testFile("audio/x-ogg-flac", "", ""), - //testFile("application/x-compress", "z", ""), - //testFile("image/x-psd", "", ""), - //testFile("text/rss", "", ""), - //testFile("application/sdts-raster", "", ""), - //testFile("application/oxps", "", ""), - //testFile("application/leveller", "", ""), - //testFile("application/x-ingr", "", ""), - //testFile("image/sgi", "", ""), - //testFile("application/x-pnm", "", ""), - //testFile("image/raster", "", ""), - //testFile("audio/x-ogg-pcm", "", ""), - //testFile("audio/ogg; codecs=opus", "", ""), - //testFile("application/fits", "", ""), - //testFile("application/x-r", "", ""), + // testFile("application/vnd.ms-word2006ml", "", ""), + // testFile("application/x-bt", "", ""), + // testFile("application/x-font-adobe-metric", "", ""), + // testFile("application/x-rst", "", ""), + // testFile("application/vrt", "", ""), + // testFile("application/x-ctg", "", ""), + // testFile("application/x-e00-grid", "", ""), + // testFile("audio/x-ogg-flac", "", ""), + // testFile("application/x-compress", "z", ""), + // testFile("image/x-psd", "", ""), + // testFile("text/rss", "", ""), + // testFile("application/sdts-raster", "", ""), + // testFile("application/oxps", "", ""), + // testFile("application/leveller", "", ""), + // testFile("application/x-ingr", "", ""), + // testFile("image/sgi", "", ""), + // testFile("application/x-pnm", "", ""), + // testFile("image/raster", "", ""), + // testFile("audio/x-ogg-pcm", "", ""), + // testFile("audio/ogg; codecs=opus", "", ""), + // testFile("application/fits", "", ""), + // testFile("application/x-r", "", ""), testFile(MIMETYPE_IMAGE_GIF, "gif", "quick.gif"), - //testFile("application/java-vm", "", ""), - //testFile("application/mspowerpoint", "", ""), - //testFile("application/x-http", "", ""), - //testFile("application/x-rmf", "", ""), - //testFile("application/x-ogg", "", ""), - //testFile("video/ogg", "ogv", "quick.ogv"), - //testFile(MIMETYPE_APPLEFILE, "", ""), - //testFile("text/rtf", "", ""), - //testFile("image/adrg", "", ""), - //testFile("video/x-ogg-rgb", "", ""), - //testFile("application/x-ngs-geoid", "", ""), - //testFile("application/x-map", "", ""), - //testFile("image/ceos", "", ""), - //testFile("application/xpm", "", ""), - //testFile("application/x-ers", "", ""), - //testFile("video/x-ogg-yuv", "", ""), - //testFile("application/x-isis2", "", ""), - //testFile("application/x-nwt-grd", "", ""), - //testFile("application/x-isis3", "", ""), - //testFile("application/x-nwt-grc", "", ""), - //testFile("video/daala", "", ""), - //testFile("application/x-blx", "", ""), - //testFile("application/x-tnef", "", ""), - //testFile("video/x-dirac", "", ""), - //testFile("application/x-ndf", "", ""), - //testFile("image/vnd.wap.wbmp", "", ""), - //testFile("video/theora", "", ""), - //testFile("application/kate", "", ""), - //testFile("application/pkcs7-mime", "", ""), - //testFile("image/fit", "", ""), - //testFile("application/x-ctable2", "", ""), - //testFile("application/x-executable", "", ""), - //testFile("application/x-isatab", "", ""), - //testFile("application/grass-ascii-grid", "", ""), + // testFile("application/java-vm", "", ""), + // testFile("application/mspowerpoint", "", ""), + // testFile("application/x-http", "", ""), + // testFile("application/x-rmf", "", ""), + // testFile("application/x-ogg", "", ""), + // testFile("video/ogg", "ogv", "quick.ogv"), + // testFile(MIMETYPE_APPLEFILE, "", ""), + // testFile("text/rtf", "", ""), + // testFile("image/adrg", "", ""), + // testFile("video/x-ogg-rgb", "", ""), + // testFile("application/x-ngs-geoid", "", ""), + // testFile("application/x-map", "", ""), + // testFile("image/ceos", "", ""), + // testFile("application/xpm", "", ""), + // testFile("application/x-ers", "", ""), + // testFile("video/x-ogg-yuv", "", ""), + // testFile("application/x-isis2", "", ""), + // testFile("application/x-nwt-grd", "", ""), + // testFile("application/x-isis3", "", ""), + // testFile("application/x-nwt-grc", "", ""), + // testFile("video/daala", "", ""), + // testFile("application/x-blx", "", ""), + // testFile("application/x-tnef", "", ""), + // testFile("video/x-dirac", "", ""), + // testFile("application/x-ndf", "", ""), + // testFile("image/vnd.wap.wbmp", "", ""), + // testFile("video/theora", "", ""), + // testFile("application/kate", "", ""), + // testFile("application/pkcs7-mime", "", ""), + // testFile("image/fit", "", ""), + // testFile("application/x-ctable2", "", ""), + // testFile("application/x-executable", "", ""), + // testFile("application/x-isatab", "", ""), + // testFile("application/grass-ascii-grid", "", ""), testFile(MIMETYPE_TEXT_PLAIN, "txt", "quick.txt"), - //testFile("application/gzipped", "", ""), - //testFile("application/x-gxf", "", ""), - //testFile("application/x-cpg", "", ""), - //testFile("application/x-lan", "", ""), - //testFile("application/x-xyz", "", ""), + // testFile("application/gzipped", "", ""), + // testFile("application/x-gxf", "", ""), + // testFile("application/x-cpg", "", ""), + // testFile("application/x-lan", "", ""), + // testFile("application/x-xyz", "", ""), testFile(MIMETYPE_IWORK_PAGES, "pages", "quick.pages"), - //testFile("image/x-jbig2", "", ""), - //testFile("image/nitf", "", ""), - //testFile("application/mbox", "", ""), - //testFile("application/chm", "", ""), - //testFile("application/x-fast", "", ""), - //testFile("application/x-gsc", "", ""), - //testFile("application/x-deflate", "", ""), - //testFile("application/x-grib2", "", ""), - //testFile("image/x-ozi", "", ""), - //testFile("application/x-pds", "", ""), - //testFile("application/vnd.apple.iwork", "", ""), - //testFile("application/x-usgs-dem", "", ""), - //testFile("application/vnd.ms-excel.sheet.2", "", ""), - //testFile("application/vnd.ms-excel.sheet.3", "", ""), - //testFile("application/dif+xml", "", ""), - //testFile("application/vnd.ms-excel.sheet.4", "", ""), - //testFile("application/x-java", "", ""), - //testFile("image/geotiff", "", ""), - //testFile("application/x-gsag", "", ""), - //testFile("application/x-snappy", "", ""), - //testFile("video/x-theora", "", ""), - //testFile("image/ntf", "", ""), - //testFile("application/x-pdf", "", ""), - //testFile("application/xml", "", ""), - //testFile("application/vnd.wordperfect; version=6.x", "", ""), - //testFile("application/pkcs7-signature", "", ""), - //testFile("application/vnd.wordperfect; version=5.1", "", ""), - //testFile("application/vnd.wordperfect; version=5.0", "", ""), - //testFile("application/x-arj-compressed", "", ""), - //testFile("application/geotopic", "", ""), - //testFile("text/x-java-source", "java", ""), - //testFile("audio/basic", "au", ""), - //testFile("application/pcisdk", "", ""), - //testFile("application/x-rik", "", ""), - //testFile("audio/opus", "", ""), - //testFile(MIMETYPE_IMAGE_JP2, "jp2", ""), - //testFile("application/x-gtx", "", ""), - //testFile("application/x-object", "", ""), - //testFile("application/vnd.ms-wordml", "", ""), - //testFile("image/x-wmf", "", ""), - //testFile("application/x-rpf-toc", "", ""), - //testFile("application/x-srtmhgt", "", ""), - //testFile("application/x-generic-bin", "", ""), - //testFile("text/vnd.iptc.anpa", "", ""), - //testFile("application/x-msmetafile", "", ""), - //testFile("application/x-wms", "", ""), - //testFile("video/x-oggrgb", "", ""), - //testFile("image/xcf", "", ""), - //testFile("application/photoshop", "", ""), - //testFile("application/x-lz4", "", ""), - //testFile("application/x-7z-compressed", "", ""), - //testFile("application/gff", "", ""), - //testFile("video/x-oggyuv", "", ""), - //testFile("application/x-msdownload", "", ""), - //testFile("image/icns", "", ""), - //testFile("application/x-emf", "", ""), - //testFile("application/x-geo-pdf", "", ""), - //testFile("video/x-ogg-uvs", "", ""), + // testFile("image/x-jbig2", "", ""), + // testFile("image/nitf", "", ""), + // testFile("application/mbox", "", ""), + // testFile("application/chm", "", ""), + // testFile("application/x-fast", "", ""), + // testFile("application/x-gsc", "", ""), + // testFile("application/x-deflate", "", ""), + // testFile("application/x-grib2", "", ""), + // testFile("image/x-ozi", "", ""), + // testFile("application/x-pds", "", ""), + // testFile("application/vnd.apple.iwork", "", ""), + // testFile("application/x-usgs-dem", "", ""), + // testFile("application/vnd.ms-excel.sheet.2", "", ""), + // testFile("application/vnd.ms-excel.sheet.3", "", ""), + // testFile("application/dif+xml", "", ""), + // testFile("application/vnd.ms-excel.sheet.4", "", ""), + // testFile("application/x-java", "", ""), + // testFile("image/geotiff", "", ""), + // testFile("application/x-gsag", "", ""), + // testFile("application/x-snappy", "", ""), + // testFile("video/x-theora", "", ""), + // testFile("image/ntf", "", ""), + // testFile("application/x-pdf", "", ""), + // testFile("application/xml", "", ""), + // testFile("application/vnd.wordperfect; version=6.x", "", ""), + // testFile("application/pkcs7-signature", "", ""), + // testFile("application/vnd.wordperfect; version=5.1", "", ""), + // testFile("application/vnd.wordperfect; version=5.0", "", ""), + // testFile("application/x-arj-compressed", "", ""), + // testFile("application/geotopic", "", ""), + // testFile("text/x-java-source", "java", ""), + // testFile("audio/basic", "au", ""), + // testFile("application/pcisdk", "", ""), + // testFile("application/x-rik", "", ""), + // testFile("audio/opus", "", ""), + // testFile(MIMETYPE_IMAGE_JP2, "jp2", ""), + // testFile("application/x-gtx", "", ""), + // testFile("application/x-object", "", ""), + // testFile("application/vnd.ms-wordml", "", ""), + // testFile("image/x-wmf", "", ""), + // testFile("application/x-rpf-toc", "", ""), + // testFile("application/x-srtmhgt", "", ""), + // testFile("application/x-generic-bin", "", ""), + // testFile("text/vnd.iptc.anpa", "", ""), + // testFile("application/x-msmetafile", "", ""), + // testFile("application/x-wms", "", ""), + // testFile("video/x-oggrgb", "", ""), + // testFile("image/xcf", "", ""), + // testFile("application/photoshop", "", ""), + // testFile("application/x-lz4", "", ""), + // testFile("application/x-7z-compressed", "", ""), + // testFile("application/gff", "", ""), + // testFile("video/x-oggyuv", "", ""), + // testFile("application/x-msdownload", "", ""), + // testFile("image/icns", "", ""), + // testFile("application/x-emf", "", ""), + // testFile("application/x-geo-pdf", "", ""), + // testFile("video/x-ogg-uvs", "", ""), testFile(MIMETYPE_VIDEO_FLV, "flv", "quick.flv"), - //testFile("application/x-zip-compressed", "", ""), - //testFile("application/gzip", "", ""), - //testFile("application/x-tika-unix-dump", "", ""), - //testFile("application/x-coasp", "", ""), - //testFile("application/x-dipex", "", ""), - //testFile("application/x-til", "", ""), - //testFile("application/x-gzip", "gzip", ""), - //testFile("application/x-gs7bg", "", ""), - //testFile("application/x-unix-archive", "", ""), - //testFile("application/x-elf", "", ""), - //testFile("application/dted", "", ""), - //testFile("application/x-rasterlite", "", ""), - //testFile("audio/x-mp4a", "", ""), - //testFile("application/x-gzip-compressed", "", ""), - //testFile("application/x-chm", "", ""), - //testFile("image/hfa", "", ""), + // testFile("application/x-zip-compressed", "", ""), + // testFile("application/gzip", "", ""), + // testFile("application/x-tika-unix-dump", "", ""), + // testFile("application/x-coasp", "", ""), + // testFile("application/x-dipex", "", ""), + // testFile("application/x-til", "", ""), + // testFile("application/x-gzip", "gzip", ""), + // testFile("application/x-gs7bg", "", ""), + // testFile("application/x-unix-archive", "", ""), + // testFile("application/x-elf", "", ""), + // testFile("application/dted", "", ""), + // testFile("application/x-rasterlite", "", ""), + // testFile("audio/x-mp4a", "", ""), + // testFile("application/x-gzip-compressed", "", ""), + // testFile("application/x-chm", "", ""), + // testFile("image/hfa", "", ""), // Special test cases from the repo tests // ====================================== @@ -571,7 +572,6 @@ public class TikaMetadataExtractsIT extends AbstractMetadataExtractsIT testFile(MIMETYPE_OPENDOCUMENT_TEXT, "odt", "quick.odt"), testFile(MIMETYPE_OPENDOCUMENT_TEXT_TEMPLATE, "ott", "quick.ott"), testFile(MIMETYPE_OPENDOCUMENT_FORMULA, "odf", "quick.odf"), - testFile(MIMETYPE_PDF, "pdf", "quick.pdf") - ); + testFile(MIMETYPE_PDF, "pdf", "quick.pdf")); } } diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaQueueIT.java b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaQueueIT.java index 34887f63..b5af046d 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaQueueIT.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaQueueIT.java @@ -31,12 +31,11 @@ import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; import java.util.UUID; -import org.alfresco.transform.client.model.TransformRequest; import org.alfresco.transform.base.messaging.AbstractQueueIT; +import org.alfresco.transform.client.model.TransformRequest; /** - * @author Lucian Tuca - * created on 15/01/2019 + * @author Lucian Tuca created on 15/01/2019 */ public class TikaQueueIT extends AbstractQueueIT { @@ -44,16 +43,16 @@ public class TikaQueueIT extends AbstractQueueIT protected TransformRequest buildRequest() { return TransformRequest - .builder() - .withRequestId(UUID.randomUUID().toString()) - .withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING) - .withTargetMediaType(MIMETYPE_TEXT_PLAIN) - .withTargetExtension("txt") - .withSchema(1) - .withClientData("ACS") - .withSourceReference(UUID.randomUUID().toString()) - .withSourceSize(32L) - .withInternalContextForTransformEngineTests() - .build(); + .builder() + .withRequestId(UUID.randomUUID().toString()) + .withSourceMediaType(MIMETYPE_OPENXML_WORDPROCESSING) + .withTargetMediaType(MIMETYPE_TEXT_PLAIN) + .withTargetExtension("txt") + .withSchema(1) + .withClientData("ACS") + .withSourceReference(UUID.randomUUID().toString()) + .withSourceSize(32L) + .withInternalContextForTransformEngineTests() + .build(); } } diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTransformationIT.java b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTransformationIT.java index 7b4b7ba9..a3d1fd04 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTransformationIT.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTransformationIT.java @@ -26,22 +26,24 @@ */ package org.alfresco.transform.tika; +import static java.text.MessageFormat.format; +import static java.util.function.Function.identity; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.springframework.http.HttpStatus.OK; + +import java.util.Map; +import java.util.stream.Stream; + import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.base.clients.HttpClient; import org.apache.commons.lang3.tuple.Triple; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; -import java.util.Map; -import java.util.stream.Stream; - -import static java.text.MessageFormat.format; -import static java.util.function.Function.identity; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; -import static org.springframework.http.HttpStatus.OK; +import org.alfresco.transform.base.clients.HttpClient; /** * @author Cezar Leahu @@ -50,10 +52,10 @@ public class TikaTransformationIT { private static final String ENGINE_URL = "http://localhost:8090"; private static final Map extensionMimetype = ImmutableMap.of( - "html", "text/html", - "txt", "text/plain", - "xhtml", "application/xhtml+xml", - "xml", "text/xml"); + "html", "text/html", + "txt", "text/plain", + "xhtml", "application/xhtml+xml", + "xml", "text/xml"); @ParameterizedTest @MethodSource("engineTransformations") @@ -63,7 +65,7 @@ public class TikaTransformationIT final String sourceMimetype = entry.getRight(); final String targetExtension = entry.getMiddle(); String targetMimetype; - //Single test to cover pdf-->csv + // Single test to cover pdf-->csv if (sourceFile.contains("pdf") && targetExtension.contains("csv")) { targetMimetype = "text/csv"; @@ -74,13 +76,13 @@ public class TikaTransformationIT } final String descriptor = format("Transform ({0}, {1} -> {2}, {3})", - sourceFile, sourceMimetype, targetMimetype, targetExtension); + sourceFile, sourceMimetype, targetMimetype, targetExtension); try { final ResponseEntity response = HttpClient.sendTRequest(ENGINE_URL, sourceFile, null, - targetMimetype, targetExtension, ImmutableMap.of( - "targetEncoding", "UTF-8", - "sourceMimetype", sourceMimetype)); + targetMimetype, targetExtension, ImmutableMap.of( + "targetEncoding", "UTF-8", + "sourceMimetype", sourceMimetype)); assertEquals(OK, response.getStatusCode(), descriptor); } catch (Exception e) @@ -90,80 +92,76 @@ public class TikaTransformationIT } private static Stream> allTargets(final String sourceFile, - final String sourceMimetype) + final String sourceMimetype) { return extensionMimetype - .keySet() - .stream() - .map(k -> Triple.of(sourceFile, k, sourceMimetype)); + .keySet() + .stream() + .map(k -> Triple.of(sourceFile, k, sourceMimetype)); } // TODO unit tests for the following file types (for which is difficult to find file samples): - // *.ogx (application/ogg) - // *.cpio (application/x-cpio) - // *.cdf (application/x-netcdf) - // *.hdf (application/x-hdf) + // *.ogx (application/ogg) + // *.cpio (application/x-cpio) + // *.cdf (application/x-netcdf) + // *.hdf (application/x-hdf) public static Stream> engineTransformations() { return Stream - .of( - allTargets("quick.doc", "application/msword"), - allTargets("quick.docx", - "application/vnd.openxmlformats-officedocument.wordprocessingml.document"), - allTargets("quick.html", "text/html"), - allTargets("quick.jar", "application/java-archive"), - allTargets("quick.java", "text/x-java-source"), - Stream.of( - Triple.of("quick.key", "html", "application/vnd.apple.keynote"), - // Does not work, alfresco-docker-sourceMimetype-misc can handle this target mimetype, removed from engine_config.json - Triple.of("quick.key", "txt", "application/vnd.apple.keynote"), - Triple.of("quick.key", "xhtml", "application/vnd.apple.keynote"), - Triple.of("quick.key", "xml", "application/vnd.apple.keynote") - ), - allTargets("quick.msg", "application/vnd.ms-outlook"), - Stream.of( - Triple.of("quick.numbers", "html", "application/vnd.apple.numbers"), - // Does not work, alfresco-docker-sourceMimetype-misc can handle this target mimetype, removed from engine_config.json - // Triple.of("quick.numbers", "txt", "TikaAuto"), - Triple.of("quick.numbers", "xhtml", "application/vnd.apple.numbers"), - Triple.of("quick.numbers", "xml", "application/vnd.apple.numbers") - ), - Stream.of( - Triple.of("quick.pdf", "csv", "application/pdf") - ), - allTargets("quick.odp", "application/vnd.oasis.opendocument.presentation"), - allTargets("quick.ods", "application/vnd.oasis.opendocument.spreadsheet"), - allTargets("quick.odt", "application/vnd.oasis.opendocument.text"), - allTargets("quick.otp", "application/vnd.oasis.opendocument.presentation-template"), - allTargets("quick.ots", "application/vnd.oasis.opendocument.spreadsheet-template"), - allTargets("quick.ott", "application/vnd.oasis.opendocument.text-template"), - Stream.of( - Triple.of("quick.pages", "html", "application/vnd.apple.pages"), - // Does not work, alfresco-docker-sourceMimetype-misc can handle this target mimetype, removed from engine_config.json - // Triple.of("quick.pages", "txt", "TikaAuto"), - Triple.of("quick.pages", "xhtml", "application/vnd.apple.pages"), - Triple.of("quick.pages", "xml", "application/vnd.apple.pages") - ), - allTargets("quick.pdf", "application/pdf"), - allTargets("quick.ppt", "application/vnd.ms-powerpoint"), - allTargets("quick.pptx", - "application/vnd.openxmlformats-officedocument.presentationml.presentation"), - allTargets("quick.sxw", "application/vnd.sun.xml.writer"), - allTargets("quick.txt", "text/plain"), - allTargets("quick.vsd", "application/vnd.visio"), - allTargets("quick.xls", "application/vnd.ms-excel"), - allTargets("quick.xlsx", - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), - allTargets("quick.zip", "application/zip"), - allTargets("quick.tar", "application/x-tar"), - allTargets("sample.rtf", "application/rtf"), - allTargets("quick.xml", "text/xml"), - allTargets("sample.xhtml.txt", "application/xhtml+xml"), - allTargets("sample.rss", "application/rss+xml"), - //allTargets("quick.rar", "application/x-rar-compressed"), - allTargets("quick.z", "application/x-compress"), - allTargets("quick.csv", "text/csv"), - allTargets("quick.tar.gz", "application/x-gzip")) - .flatMap(identity()); + .of( + allTargets("quick.doc", "application/msword"), + allTargets("quick.docx", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document"), + allTargets("quick.html", "text/html"), + allTargets("quick.jar", "application/java-archive"), + allTargets("quick.java", "text/x-java-source"), + Stream.of( + Triple.of("quick.key", "html", "application/vnd.apple.keynote"), + // Does not work, alfresco-docker-sourceMimetype-misc can handle this target mimetype, removed from engine_config.json + Triple.of("quick.key", "txt", "application/vnd.apple.keynote"), + Triple.of("quick.key", "xhtml", "application/vnd.apple.keynote"), + Triple.of("quick.key", "xml", "application/vnd.apple.keynote")), + allTargets("quick.msg", "application/vnd.ms-outlook"), + Stream.of( + Triple.of("quick.numbers", "html", "application/vnd.apple.numbers"), + // Does not work, alfresco-docker-sourceMimetype-misc can handle this target mimetype, removed from engine_config.json + // Triple.of("quick.numbers", "txt", "TikaAuto"), + Triple.of("quick.numbers", "xhtml", "application/vnd.apple.numbers"), + Triple.of("quick.numbers", "xml", "application/vnd.apple.numbers")), + Stream.of( + Triple.of("quick.pdf", "csv", "application/pdf")), + allTargets("quick.odp", "application/vnd.oasis.opendocument.presentation"), + allTargets("quick.ods", "application/vnd.oasis.opendocument.spreadsheet"), + allTargets("quick.odt", "application/vnd.oasis.opendocument.text"), + allTargets("quick.otp", "application/vnd.oasis.opendocument.presentation-template"), + allTargets("quick.ots", "application/vnd.oasis.opendocument.spreadsheet-template"), + allTargets("quick.ott", "application/vnd.oasis.opendocument.text-template"), + Stream.of( + Triple.of("quick.pages", "html", "application/vnd.apple.pages"), + // Does not work, alfresco-docker-sourceMimetype-misc can handle this target mimetype, removed from engine_config.json + // Triple.of("quick.pages", "txt", "TikaAuto"), + Triple.of("quick.pages", "xhtml", "application/vnd.apple.pages"), + Triple.of("quick.pages", "xml", "application/vnd.apple.pages")), + allTargets("quick.pdf", "application/pdf"), + allTargets("quick.ppt", "application/vnd.ms-powerpoint"), + allTargets("quick.pptx", + "application/vnd.openxmlformats-officedocument.presentationml.presentation"), + allTargets("quick.sxw", "application/vnd.sun.xml.writer"), + allTargets("quick.txt", "text/plain"), + allTargets("quick.vsd", "application/vnd.visio"), + allTargets("quick.xls", "application/vnd.ms-excel"), + allTargets("quick.xlsx", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), + allTargets("quick.zip", "application/zip"), + allTargets("quick.tar", "application/x-tar"), + allTargets("sample.rtf", "application/rtf"), + allTargets("quick.xml", "text/xml"), + allTargets("sample.xhtml.txt", "application/xhtml+xml"), + allTargets("sample.rss", "application/rss+xml"), + // allTargets("quick.rar", "application/x-rar-compressed"), + allTargets("quick.z", "application/x-compress"), + allTargets("quick.csv", "text/csv"), + allTargets("quick.tar.gz", "application/x-gzip")) + .flatMap(identity()); } } diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/metadata/embedders/PoiMetadataEmbedder.java b/engines/tika/src/test/java/org/alfresco/transform/tika/metadata/embedders/PoiMetadataEmbedder.java index 7d4e0d35..51b423ac 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/metadata/embedders/PoiMetadataEmbedder.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/metadata/embedders/PoiMetadataEmbedder.java @@ -26,7 +26,15 @@ */ package org.alfresco.transform.tika.metadata.embedders; -import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor; +import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EMBEDDER; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Collections; +import java.util.Set; +import java.util.StringJoiner; + import org.apache.poi.ooxml.POIXMLProperties; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.tika.embedder.Embedder; @@ -39,18 +47,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Collections; -import java.util.Set; -import java.util.StringJoiner; - -import static org.alfresco.transform.base.metadata.AbstractMetadataExtractorEmbedder.Type.EMBEDDER; +import org.alfresco.transform.tika.metadata.AbstractTikaMetadataExtractorEmbeddor; /** - * Sample POI metadata embedder to demonstrate it is possible to add custom T-Engines that will add - * metadata. This is not production code, so no supported mimetypes exist in the {@code tika_engine_config.json}. + * Sample POI metadata embedder to demonstrate it is possible to add custom T-Engines that will add metadata. This is not production code, so no supported mimetypes exist in the {@code tika_engine_config.json}. */ @Component public class PoiMetadataEmbedder extends AbstractTikaMetadataExtractorEmbeddor @@ -76,8 +76,7 @@ public class PoiMetadataEmbedder extends AbstractTikaMetadataExtractorEmbeddor private static class SamplePoiEmbedder implements Embedder { - private static final Set SUPPORTED_EMBED_TYPES = - Collections.singleton(MediaType.application("vnd.openxmlformats-officedocument.spreadsheetml.sheet")); + private static final Set SUPPORTED_EMBED_TYPES = Collections.singleton(MediaType.application("vnd.openxmlformats-officedocument.spreadsheetml.sheet")); @Override public Set getSupportedEmbedTypes(ParseContext parseContext) @@ -115,19 +114,19 @@ public class PoiMetadataEmbedder extends AbstractTikaMetadataExtractorEmbeddor } switch (name) { - case "author": - coreProp.setCreator(value); - break; - case "title": - coreProp.setTitle(value); - break; - case "description": - coreProp.setDescription(value); - break; - // There are other core values but this is sample code, so we will assume it is a custom value. - default: - custProp.addProperty(name, value); - break; + case "author": + coreProp.setCreator(value); + break; + case "title": + coreProp.setTitle(value); + break; + case "description": + coreProp.setDescription(value); + break; + // There are other core values but this is sample code, so we will assume it is a custom value. + default: + custProp.addProperty(name, value); + break; } } workbook.write(outputStream); diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/metadata/extractors/IPTCMetadataExtractorTest.java b/engines/tika/src/test/java/org/alfresco/transform/tika/metadata/extractors/IPTCMetadataExtractorTest.java index b587aa59..a3c46742 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/metadata/extractors/IPTCMetadataExtractorTest.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/metadata/extractors/IPTCMetadataExtractorTest.java @@ -26,20 +26,21 @@ */ package org.alfresco.transform.tika.metadata.extractors; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import org.junit.jupiter.api.Test; + public class IPTCMetadataExtractorTest { IPTCMetadataExtractor extractor = new IPTCMetadataExtractor(null); @Test - public void testIptcToIso8601DateStrings() { - String[] testStrings = { "1890:01:01", "1901:02:01 00:00:00.000Z", "1901-02-01 00:00:00.000Z", - "1901-02-01T00:00:00.000Z", "1901:02:01T00:00+00:00", "1901:02:01 00:00+00:00" }; - String[] expected = { "1890-01-01", "1901-02-01T00:00:00.000Z", "1901-02-01T00:00:00.000Z", - "1901-02-01T00:00:00.000Z", "1901-02-01T00:00+00:00", "1901-02-01T00:00+00:00" }; + public void testIptcToIso8601DateStrings() + { + String[] testStrings = {"1890:01:01", "1901:02:01 00:00:00.000Z", "1901-02-01 00:00:00.000Z", + "1901-02-01T00:00:00.000Z", "1901:02:01T00:00+00:00", "1901:02:01 00:00+00:00"}; + String[] expected = {"1890-01-01", "1901-02-01T00:00:00.000Z", "1901-02-01T00:00:00.000Z", + "1901-02-01T00:00:00.000Z", "1901-02-01T00:00+00:00", "1901-02-01T00:00+00:00"}; assertArrayEquals(expected, extractor.iptcToIso8601DateStrings(testStrings)); } diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/parsers/ExifToolParserTest.java b/engines/tika/src/test/java/org/alfresco/transform/tika/parsers/ExifToolParserTest.java index 8b822ecd..53884321 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/parsers/ExifToolParserTest.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/parsers/ExifToolParserTest.java @@ -1,59 +1,61 @@ -/* - * #%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.tika.parsers; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -public class ExifToolParserTest { - - ExifToolParser exifToolParser = new ExifToolParser(); - - @Test - public void testFindSeparator() { - - String testCommand = "env FOO=${OUTPUT} exiftool -args -G1 " + ExifToolParser.SEPARATOR_SETTING - + " \"|||\" ${INPUT}"; - String expected = "|||"; - String actual = exifToolParser.findSeparator(testCommand); - assertEquals(expected, actual); - - expected = "TESTWITHOUTQUOTES"; - testCommand = "nothing matters until the " + ExifToolParser.SEPARATOR_SETTING + " " + expected - + " now all this extra should be ignored"; - actual = exifToolParser.findSeparator(testCommand); - assertEquals(expected, actual); - - expected = "Test something bonkers 112!£$%^£$^"; - testCommand = ExifToolParser.SEPARATOR_SETTING + " \""+expected+"\""; - actual = exifToolParser.findSeparator(testCommand); - assertEquals(expected, actual); - - } - -} +/* + * #%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.tika.parsers; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class ExifToolParserTest +{ + + ExifToolParser exifToolParser = new ExifToolParser(); + + @Test + public void testFindSeparator() + { + + String testCommand = "env FOO=${OUTPUT} exiftool -args -G1 " + ExifToolParser.SEPARATOR_SETTING + + " \"|||\" ${INPUT}"; + String expected = "|||"; + String actual = exifToolParser.findSeparator(testCommand); + assertEquals(expected, actual); + + expected = "TESTWITHOUTQUOTES"; + testCommand = "nothing matters until the " + ExifToolParser.SEPARATOR_SETTING + " " + expected + + " now all this extra should be ignored"; + actual = exifToolParser.findSeparator(testCommand); + assertEquals(expected, actual); + + expected = "Test something bonkers 112!£$%^£$^"; + testCommand = ExifToolParser.SEPARATOR_SETTING + " \"" + expected + "\""; + actual = exifToolParser.findSeparator(testCommand); + assertEquals(expected, actual); + + } + +} diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/transformers/TikaExtractBookmarksTest.java b/engines/tika/src/test/java/org/alfresco/transform/tika/transformers/TikaExtractBookmarksTest.java index a7f03e8c..c37fb3cb 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/transformers/TikaExtractBookmarksTest.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/transformers/TikaExtractBookmarksTest.java @@ -1,138 +1,140 @@ -/* - * #%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.tika.transformers; - -import static org.alfresco.transform.tika.transformers.Tika.NOT_EXTRACT_BOOKMARKS_TEXT; -import static org.alfresco.transform.tika.transformers.Tika.TARGET_ENCODING; -import static org.alfresco.transform.tika.transformers.Tika.TARGET_MIMETYPE; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.clearInvocations; -import static org.mockito.Mockito.lenient; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import java.io.InputStream; -import java.io.OutputStream; -import java.util.HashMap; -import java.util.Map; - -import org.alfresco.transform.base.TransformManager; -import org.apache.tika.parser.Parser; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -public class TikaExtractBookmarksTest -{ - private static class TikaTestTransformer extends AbstractTikaTransformer - { - @Override - protected Parser getParser() - { - return null; - } - - TikaTestTransformer(boolean notExtractBookmarksTextDefault) - { - this.notExtractBookmarksTextDefault = notExtractBookmarksTextDefault; - } - } - - @Test - public void testNotExtractBookmarkTextDefault() throws Exception - { - AbstractTikaTransformer executorSpyDefaultTrue = spy(new TikaTestTransformer(true)); - AbstractTikaTransformer executorSpyDefaultFalse = spy(new TikaTestTransformer(false)); - - InputStream mockInputStream = mock(InputStream.class); - OutputStream mockOutputStream = mock(OutputStream.class); - TransformManager mockTransformManager = mock(TransformManager.class); - String sourceMimetype = "sourceMimetype"; - String targetMimetype = "targetMimetype"; - String defaultEncoding = "UTF-8"; - - // no need to continue execution passed here or check values as we're checking the correct params passed to this method later. - lenient().doNothing().when(executorSpyDefaultTrue).call(any(), any(), any(), any(), any(), any()); - lenient().doNothing().when(executorSpyDefaultFalse).call(any(), any(), any(), any(), any(), any()); - - Map transformOptions = new HashMap<>(); - - // use empty transformOptions to test defaults - executorSpyDefaultTrue.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); - executorSpyDefaultFalse.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); - - // when default set to true, with no options passed we should get a call method with NOT_EXTRACT_BOOKMARKS_TEXT - verify(executorSpyDefaultTrue, times(1)).call(mockInputStream, mockOutputStream, null, - NOT_EXTRACT_BOOKMARKS_TEXT, TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); - - // when default set to false, with no options passed we should get a call method without NOT_EXTRACT_BOOKMARKS_TEXT - verify(executorSpyDefaultFalse, times(1)).call(mockInputStream, mockOutputStream, null, null, - TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); - - // use transforms with notExtractBookmarksText set to true - clearInvocations(executorSpyDefaultTrue, executorSpyDefaultFalse); - transformOptions.put("notExtractBookmarksText", "true"); - executorSpyDefaultTrue.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); - executorSpyDefaultFalse.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); - - // both call methods should have NOT_EXTRACT_BOOKMARKS_TEXT - verify(executorSpyDefaultTrue, times(1)).call(mockInputStream, mockOutputStream, null, - NOT_EXTRACT_BOOKMARKS_TEXT, TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); - - verify(executorSpyDefaultFalse, times(1)).call(mockInputStream, mockOutputStream, null, - NOT_EXTRACT_BOOKMARKS_TEXT, TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); - - // use transforms with notExtractBookmarksText set to false - clearInvocations(executorSpyDefaultTrue, executorSpyDefaultFalse); - transformOptions.replace("notExtractBookmarksText", "true", "false"); - executorSpyDefaultTrue.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); - executorSpyDefaultFalse.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); - - // both call methods should have NOT_EXTRACT_BOOKMARKS_TEXT - verify(executorSpyDefaultTrue, times(1)).call(mockInputStream, mockOutputStream, null, null, - TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); - - verify(executorSpyDefaultFalse, times(1)).call(mockInputStream, mockOutputStream, null, null, - TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); - - // useful set of pdfbox transformOptions just to be safe - clearInvocations(executorSpyDefaultTrue, executorSpyDefaultFalse); - transformOptions.put("targetEncoding", "anyEncoding"); - executorSpyDefaultTrue.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); - executorSpyDefaultFalse.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); - - // both call methods should have NOT_EXTRACT_BOOKMARKS_TEXT but the encoding will change - verify(executorSpyDefaultTrue, times(1)).call(mockInputStream, mockOutputStream, null, null, - TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + "anyEncoding"); - - verify(executorSpyDefaultFalse, times(1)).call(mockInputStream, mockOutputStream, null, null, - TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + "anyEncoding"); - } -} +/* + * #%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.tika.transformers; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.clearInvocations; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import static org.alfresco.transform.tika.transformers.Tika.NOT_EXTRACT_BOOKMARKS_TEXT; +import static org.alfresco.transform.tika.transformers.Tika.TARGET_ENCODING; +import static org.alfresco.transform.tika.transformers.Tika.TARGET_MIMETYPE; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tika.parser.Parser; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import org.alfresco.transform.base.TransformManager; + +@ExtendWith(MockitoExtension.class) +public class TikaExtractBookmarksTest +{ + private static class TikaTestTransformer extends AbstractTikaTransformer + { + @Override + protected Parser getParser() + { + return null; + } + + TikaTestTransformer(boolean notExtractBookmarksTextDefault) + { + this.notExtractBookmarksTextDefault = notExtractBookmarksTextDefault; + } + } + + @Test + public void testNotExtractBookmarkTextDefault() throws Exception + { + AbstractTikaTransformer executorSpyDefaultTrue = spy(new TikaTestTransformer(true)); + AbstractTikaTransformer executorSpyDefaultFalse = spy(new TikaTestTransformer(false)); + + InputStream mockInputStream = mock(InputStream.class); + OutputStream mockOutputStream = mock(OutputStream.class); + TransformManager mockTransformManager = mock(TransformManager.class); + String sourceMimetype = "sourceMimetype"; + String targetMimetype = "targetMimetype"; + String defaultEncoding = "UTF-8"; + + // no need to continue execution passed here or check values as we're checking the correct params passed to this method later. + lenient().doNothing().when(executorSpyDefaultTrue).call(any(), any(), any(), any(), any(), any()); + lenient().doNothing().when(executorSpyDefaultFalse).call(any(), any(), any(), any(), any(), any()); + + Map transformOptions = new HashMap<>(); + + // use empty transformOptions to test defaults + executorSpyDefaultTrue.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); + executorSpyDefaultFalse.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); + + // when default set to true, with no options passed we should get a call method with NOT_EXTRACT_BOOKMARKS_TEXT + verify(executorSpyDefaultTrue, times(1)).call(mockInputStream, mockOutputStream, null, + NOT_EXTRACT_BOOKMARKS_TEXT, TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); + + // when default set to false, with no options passed we should get a call method without NOT_EXTRACT_BOOKMARKS_TEXT + verify(executorSpyDefaultFalse, times(1)).call(mockInputStream, mockOutputStream, null, null, + TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); + + // use transforms with notExtractBookmarksText set to true + clearInvocations(executorSpyDefaultTrue, executorSpyDefaultFalse); + transformOptions.put("notExtractBookmarksText", "true"); + executorSpyDefaultTrue.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); + executorSpyDefaultFalse.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); + + // both call methods should have NOT_EXTRACT_BOOKMARKS_TEXT + verify(executorSpyDefaultTrue, times(1)).call(mockInputStream, mockOutputStream, null, + NOT_EXTRACT_BOOKMARKS_TEXT, TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); + + verify(executorSpyDefaultFalse, times(1)).call(mockInputStream, mockOutputStream, null, + NOT_EXTRACT_BOOKMARKS_TEXT, TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); + + // use transforms with notExtractBookmarksText set to false + clearInvocations(executorSpyDefaultTrue, executorSpyDefaultFalse); + transformOptions.replace("notExtractBookmarksText", "true", "false"); + executorSpyDefaultTrue.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); + executorSpyDefaultFalse.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); + + // both call methods should have NOT_EXTRACT_BOOKMARKS_TEXT + verify(executorSpyDefaultTrue, times(1)).call(mockInputStream, mockOutputStream, null, null, + TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); + + verify(executorSpyDefaultFalse, times(1)).call(mockInputStream, mockOutputStream, null, null, + TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + defaultEncoding); + + // useful set of pdfbox transformOptions just to be safe + clearInvocations(executorSpyDefaultTrue, executorSpyDefaultFalse); + transformOptions.put("targetEncoding", "anyEncoding"); + executorSpyDefaultTrue.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); + executorSpyDefaultFalse.transform(sourceMimetype, mockInputStream, targetMimetype, mockOutputStream, transformOptions, mockTransformManager); + + // both call methods should have NOT_EXTRACT_BOOKMARKS_TEXT but the encoding will change + verify(executorSpyDefaultTrue, times(1)).call(mockInputStream, mockOutputStream, null, null, + TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + "anyEncoding"); + + verify(executorSpyDefaultFalse, times(1)).call(mockInputStream, mockOutputStream, null, null, + TARGET_MIMETYPE + targetMimetype, TARGET_ENCODING + "anyEncoding"); + } +} diff --git a/model/src/main/java/org/alfresco/transform/client/model/InternalContext.java b/model/src/main/java/org/alfresco/transform/client/model/InternalContext.java index 558616c8..84786a3d 100644 --- a/model/src/main/java/org/alfresco/transform/client/model/InternalContext.java +++ b/model/src/main/java/org/alfresco/transform/client/model/InternalContext.java @@ -21,18 +21,17 @@ */ package org.alfresco.transform.client.model; -import org.alfresco.transform.messages.TransformStack; - import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import org.alfresco.transform.messages.TransformStack; + /** * Holds required contextual information. * - * @author Denis Ungureanu - * created on 10/01/2019 + * @author Denis Ungureanu created on 10/01/2019 */ // This class is in the package org.alfresco.transform.messages in HxP because that is more readable, but in // org.alfresco.transform.client.model in Alfresco for backward compatibility. @@ -97,10 +96,10 @@ public class InternalContext implements Serializable } /** - * Sets the reply to destination name. - * Note: replyToDestination is populated from jmsMessage replyTo field sent by T-Client + * Sets the reply to destination name. Note: replyToDestination is populated from jmsMessage replyTo field sent by T-Client * - * @param replyToDestination reply to destination name + * @param replyToDestination + * reply to destination name */ public void setReplyToDestination(String replyToDestination) { @@ -123,22 +122,23 @@ public class InternalContext implements Serializable } public void setTransformRequestOptions( - Map transformRequestOptions) + Map transformRequestOptions) { this.transformRequestOptions = transformRequestOptions; } - @Override public String toString() + @Override + public String toString() { return "InternalContext{" + - "multiStep=" + multiStep + - ", attemptedRetries=" + attemptedRetries + - ", currentSourceMediaType='" + currentSourceMediaType + '\'' + - ", currentTargetMediaType='" + currentTargetMediaType + '\'' + - ", replyToDestination='" + replyToDestination + '\'' + - ", currentSourceSize=" + currentSourceSize + - ", transformRequestOptions=" + transformRequestOptions + - '}'; + "multiStep=" + multiStep + + ", attemptedRetries=" + attemptedRetries + + ", currentSourceMediaType='" + currentSourceMediaType + '\'' + + ", currentTargetMediaType='" + currentTargetMediaType + '\'' + + ", replyToDestination='" + replyToDestination + '\'' + + ", currentSourceSize=" + currentSourceSize + + ", transformRequestOptions=" + transformRequestOptions + + '}'; } // To avoid NPE checks, initialise expected bits of the structure @@ -153,7 +153,7 @@ public class InternalContext implements Serializable internalContext.setMultiStep(new MultiStep()); } if (internalContext.getMultiStep().getTransformsToBeDone() == null || - internalContext.getMultiStep().getTransformsToBeDone().isEmpty()) // might be immutable + internalContext.getMultiStep().getTransformsToBeDone().isEmpty()) // might be immutable { internalContext.getMultiStep().setTransformsToBeDone(new ArrayList<>()); } @@ -166,11 +166,11 @@ public class InternalContext implements Serializable return internalContext == null ? type + " InternalContext was null" : internalContext.getMultiStep() == null - ? type + " InternalContext did not have the MultiStep set" - : internalContext.getMultiStep().getTransformsToBeDone() == null - ? type + " InternalContext did not have the TransformsToBeDone set" - : internalContext.getMultiStep().getInitialRequestId() == null - ? type + " InternalContext did not have the InitialRequestId set" - : TransformStack.checkStructure(internalContext, type); + ? type + " InternalContext did not have the MultiStep set" + : internalContext.getMultiStep().getTransformsToBeDone() == null + ? type + " InternalContext did not have the TransformsToBeDone set" + : internalContext.getMultiStep().getInitialRequestId() == null + ? type + " InternalContext did not have the InitialRequestId set" + : TransformStack.checkStructure(internalContext, type); } } diff --git a/model/src/main/java/org/alfresco/transform/client/model/Mimetype.java b/model/src/main/java/org/alfresco/transform/client/model/Mimetype.java index 994d5ca5..4a91eb05 100644 --- a/model/src/main/java/org/alfresco/transform/client/model/Mimetype.java +++ b/model/src/main/java/org/alfresco/transform/client/model/Mimetype.java @@ -26,5 +26,4 @@ package org.alfresco.transform.client.model; */ @Deprecated public interface Mimetype extends org.alfresco.transform.common.Mimetype -{ -} +{} diff --git a/model/src/main/java/org/alfresco/transform/client/model/MultiStep.java b/model/src/main/java/org/alfresco/transform/client/model/MultiStep.java index a10556c7..af2a6ed8 100644 --- a/model/src/main/java/org/alfresco/transform/client/model/MultiStep.java +++ b/model/src/main/java/org/alfresco/transform/client/model/MultiStep.java @@ -28,8 +28,7 @@ import java.util.List; /** * Holds required contextual information for a multi-step transform. * - * @author Lucian Tuca - * created on 19/12/2018 + * @author Lucian Tuca created on 19/12/2018 */ // This class is in the package org.alfresco.transform.messages in HxP because that is more readable, but in // org.alfresco.transform.client.model in Alfresco for backward compatibility. @@ -71,14 +70,15 @@ public class MultiStep implements Serializable this.transformsToBeDone = transformsToBeDone; } - //endregion + // endregion - @Override public String toString() + @Override + public String toString() { return "MultiStep{" + - "initialRequestId='" + initialRequestId + '\'' + - ", initialSourceMediaType='" + initialSourceMediaType + '\'' + - ", transformsToBeDone=" + transformsToBeDone + - '}'; + "initialRequestId='" + initialRequestId + '\'' + + ", initialSourceMediaType='" + initialSourceMediaType + '\'' + + ", transformsToBeDone=" + transformsToBeDone + + '}'; } } diff --git a/model/src/main/java/org/alfresco/transform/client/model/TransformReply.java b/model/src/main/java/org/alfresco/transform/client/model/TransformReply.java index 511d04d3..4c7172fa 100644 --- a/model/src/main/java/org/alfresco/transform/client/model/TransformReply.java +++ b/model/src/main/java/org/alfresco/transform/client/model/TransformReply.java @@ -37,7 +37,7 @@ public class TransformReply implements Serializable private int schema; private InternalContext internalContext; - //region [Accessors] + // region [Accessors] public String getRequestId() { return requestId; @@ -118,7 +118,7 @@ public class TransformReply implements Serializable this.internalContext = internalContext; } - //endregion + // endregion @Override public boolean equals(Object o) @@ -137,18 +137,19 @@ public class TransformReply implements Serializable return Objects.hash(requestId); } - @Override public String toString() + @Override + public String toString() { return "TransformReply{" + - "requestId='" + requestId + '\'' + - ", status=" + status + - ", errorDetails='" + errorDetails + '\'' + - ", sourceReference='" + sourceReference + '\'' + - ", targetReference='" + targetReference + '\'' + - ", clientData='" + clientData + '\'' + - ", schema=" + schema + - ", internalContext=" + internalContext + - '}'; + "requestId='" + requestId + '\'' + + ", status=" + status + + ", errorDetails='" + errorDetails + '\'' + + ", sourceReference='" + sourceReference + '\'' + + ", targetReference='" + targetReference + '\'' + + ", clientData='" + clientData + '\'' + + ", schema=" + schema + + ", internalContext=" + internalContext + + '}'; } public static Builder builder() @@ -160,7 +161,8 @@ public class TransformReply implements Serializable { private final TransformReply reply = new TransformReply(); - private Builder() {} + private Builder() + {} public Builder withRequestId(final String requestId) { diff --git a/model/src/main/java/org/alfresco/transform/client/model/TransformRequest.java b/model/src/main/java/org/alfresco/transform/client/model/TransformRequest.java index 89ad0a01..02ab1073 100644 --- a/model/src/main/java/org/alfresco/transform/client/model/TransformRequest.java +++ b/model/src/main/java/org/alfresco/transform/client/model/TransformRequest.java @@ -21,17 +21,17 @@ */ package org.alfresco.transform.client.model; -import org.alfresco.transform.common.ExtensionService; -import org.alfresco.transform.messages.TransformStack; +import static org.alfresco.transform.messages.TransformStack.PIPELINE_FLAG; +import static org.alfresco.transform.messages.TransformStack.levelBuilder; +import static org.alfresco.transform.messages.TransformStack.setInitialTransformRequestOptions; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import java.util.Objects; -import static org.alfresco.transform.messages.TransformStack.PIPELINE_FLAG; -import static org.alfresco.transform.messages.TransformStack.levelBuilder; -import static org.alfresco.transform.messages.TransformStack.setInitialTransformRequestOptions; +import org.alfresco.transform.common.ExtensionService; +import org.alfresco.transform.messages.TransformStack; // This class is in the package org.alfresco.transform.messages in HxP because that is more readable, but in // org.alfresco.transform.client.model in Alfresco for backward compatibility. @@ -160,7 +160,7 @@ public class TransformRequest implements Serializable this.internalContext = internalContext; } - //endregion + // endregion @Override public boolean equals(Object o) @@ -179,34 +179,32 @@ public class TransformRequest implements Serializable return Objects.hash(requestId); } - @Override public String toString() + @Override + public String toString() { return "TransformRequest{" + - "requestId='" + requestId + '\'' + - ", sourceReference='" + sourceReference + '\'' + - ", sourceMediaType='" + sourceMediaType + '\'' + - ", sourceSize=" + sourceSize + - ", sourceExtension='" + sourceExtension + '\'' + - ", targetMediaType='" + targetMediaType + '\'' + - ", targetExtension='" + targetExtension + '\'' + - ", clientData='" + clientData + '\'' + - ", schema=" + schema + - ", transformRequestOptions=" + transformRequestOptions + - ", internalContext=" + internalContext + - '}'; + "requestId='" + requestId + '\'' + + ", sourceReference='" + sourceReference + '\'' + + ", sourceMediaType='" + sourceMediaType + '\'' + + ", sourceSize=" + sourceSize + + ", sourceExtension='" + sourceExtension + '\'' + + ", targetMediaType='" + targetMediaType + '\'' + + ", targetExtension='" + targetExtension + '\'' + + ", clientData='" + clientData + '\'' + + ", schema=" + schema + + ", transformRequestOptions=" + transformRequestOptions + + ", internalContext=" + internalContext + + '}'; } /** - * Sets up the internal context structure when a client request is initially received by the router, - * so that we don't have to keep checking if bits of it are initialised. Prior to making this call, - * the id, sourceMimetypes, targetMimetype, transformRequestOptions and sourceReference should have - * been set, if they are to be set. + * Sets up the internal context structure when a client request is initially received by the router, so that we don't have to keep checking if bits of it are initialised. Prior to making this call, the id, sourceMimetypes, targetMimetype, transformRequestOptions and sourceReference should have been set, if they are to be set. */ public TransformRequest initialiseContextWhenReceivedByRouter() { setInternalContext(InternalContext.initialise(getInternalContext())); setTargetExtension(ExtensionService.getExtensionForTargetMimetype(getTargetMediaType(), - getSourceMediaType())); + getSourceMediaType())); getInternalContext().getMultiStep().setInitialRequestId(getRequestId()); getInternalContext().getMultiStep().setInitialSourceMediaType(getSourceMediaType()); getInternalContext().setTransformRequestOptions(getTransformRequestOptions()); @@ -224,7 +222,8 @@ public class TransformRequest implements Serializable { private final TransformRequest request = new TransformRequest(); - private Builder() {} + private Builder() + {} public Builder withRequestId(final String requestId) { @@ -281,7 +280,7 @@ public class TransformRequest implements Serializable } public Builder withTransformRequestOptions( - final Map transformRequestOptions) + final Map transformRequestOptions) { request.transformRequestOptions = transformRequestOptions; return this; @@ -297,7 +296,7 @@ public class TransformRequest implements Serializable { request.initialiseContextWhenReceivedByRouter(); TransformStack.addTransformLevel(request.internalContext, levelBuilder(PIPELINE_FLAG) - .withStep("dummyTransformerName", request.sourceMediaType, request.targetMediaType)); + .withStep("dummyTransformerName", request.sourceMediaType, request.targetMediaType)); return this; } diff --git a/model/src/main/java/org/alfresco/transform/client/util/RequestParamMap.java b/model/src/main/java/org/alfresco/transform/client/util/RequestParamMap.java index 7757594a..ad439161 100644 --- a/model/src/main/java/org/alfresco/transform/client/util/RequestParamMap.java +++ b/model/src/main/java/org/alfresco/transform/client/util/RequestParamMap.java @@ -25,6 +25,5 @@ package org.alfresco.transform.client.util; * @deprecated will be removed in a future release when the deprecated alfresco-transform-model is removed. */ @Deprecated -public interface RequestParamMap extends org.alfresco.transform.common.RequestParamMap -{ -} +public interface RequestParamMap extends org.alfresco.transform.common.RequestParamMap +{} diff --git a/model/src/main/java/org/alfresco/transform/common/ExtensionService.java b/model/src/main/java/org/alfresco/transform/common/ExtensionService.java index b91dd557..580b22fa 100644 --- a/model/src/main/java/org/alfresco/transform/common/ExtensionService.java +++ b/model/src/main/java/org/alfresco/transform/common/ExtensionService.java @@ -1,163 +1,159 @@ -/* - * #%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 static org.alfresco.transform.common.Mimetype.MIMETYPE_DITA; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_EXCEL; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_SVG; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_XWD; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_GRAPHICS; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_PRESENTATION; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_PRESENTATION_TEMPLATE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_SPREADSHEET; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_SPREADSHEET_TEMPLATE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_TEXT; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_TEXT_TEMPLATE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENOFFICE1_CALC; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENOFFICE1_IMPRESS; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENOFFICE1_WRITER; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_ADDIN; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDE_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_TEMPLATE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_TEMPLATE_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORDPROCESSING; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORDPROCESSING_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_OUTLOOK_MSG; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PPT; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_VISIO; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_VISIO_2013; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORDPERFECT; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_XHTML; -import static org.alfresco.transform.common.TransformerDebug.MIMETYPE_METADATA_EMBED; -import static org.alfresco.transform.common.TransformerDebug.MIMETYPE_METADATA_EXTRACT; - -import java.util.Map; - -/** - * Provides mapping between mimtypes and file extensions, static and not configurable. - * The correct extension is required for a small subset of transforms in pipelines which go through the - * libreoffice transformer - */ -public class ExtensionService -{ - private final static String MIMETYPE_TAB_SEPARATED_VALUES="text/tab-separated-values"; - private final static String MIMETYPE_CALC_TEMPLATE="application/vnd.sun.xml.calc.template"; - private final static String MIMETYPE_IMPRESS_TEMPLATE="application/vnd.sun.xml.impress.template"; - private final static String MIMETYPE_WRITER_TEMPLATE="application/vnd.sun.xml.writer.template"; - - private static final Map mimetpeExtensions = Map.ofEntries( - Map.entry(MIMETYPE_WORD, "doc"), - Map.entry(MIMETYPE_OPENXML_WORDPROCESSING_MACRO, "docm"), - Map.entry(MIMETYPE_OPENXML_WORDPROCESSING, "docx"), - Map.entry(MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO, "dotm"), - Map.entry(MIMETYPE_OPENXML_WORD_TEMPLATE, "dotx"), - Map.entry(MIMETYPE_OPENDOCUMENT_GRAPHICS, "odg"), - Map.entry(MIMETYPE_OPENDOCUMENT_PRESENTATION, "odp"), - Map.entry(MIMETYPE_OPENDOCUMENT_PRESENTATION_TEMPLATE, "otp"), - Map.entry(MIMETYPE_OPENDOCUMENT_SPREADSHEET, "ods"), - Map.entry(MIMETYPE_OPENDOCUMENT_SPREADSHEET_TEMPLATE, "ots"), - Map.entry(MIMETYPE_OPENDOCUMENT_TEXT, "odt"), - Map.entry(MIMETYPE_OPENDOCUMENT_TEXT_TEMPLATE, "ott"), - Map.entry(MIMETYPE_OPENXML_PRESENTATION_TEMPLATE_MACRO, "potm"), - Map.entry(MIMETYPE_OPENXML_PRESENTATION_TEMPLATE, "potx"), - Map.entry(MIMETYPE_OPENXML_PRESENTATION_ADDIN, "ppam"), - Map.entry(MIMETYPE_PPT, "ppt"), - Map.entry(MIMETYPE_OPENXML_PRESENTATION_MACRO, "pptm"), - Map.entry(MIMETYPE_OPENXML_PRESENTATION, "pptx"), - Map.entry(MIMETYPE_OPENXML_PRESENTATION_SLIDE_MACRO, "sldm"), - Map.entry(MIMETYPE_OPENXML_PRESENTATION_SLIDE, "sldx"), - Map.entry(MIMETYPE_CALC_TEMPLATE, "stc"), - Map.entry(MIMETYPE_IMPRESS_TEMPLATE, "sti"), - Map.entry(MIMETYPE_WRITER_TEMPLATE, "stw"), - Map.entry(MIMETYPE_TAB_SEPARATED_VALUES, "tsv"), - Map.entry(MIMETYPE_OPENOFFICE1_CALC, "sxc"), - Map.entry(MIMETYPE_OPENOFFICE1_IMPRESS, "sxi"), - Map.entry(MIMETYPE_OPENOFFICE1_WRITER, "sxw"), - Map.entry(MIMETYPE_VISIO, "vsd"), - Map.entry(MIMETYPE_VISIO_2013, "vsdx"), - Map.entry(MIMETYPE_WORDPERFECT, "wp"), - Map.entry(MIMETYPE_EXCEL, "xls"), - Map.entry(MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO, "xlsb"), - Map.entry(MIMETYPE_OPENXML_SPREADSHEET_MACRO, "xlsm"), - Map.entry(MIMETYPE_OPENXML_SPREADSHEET, "xlsx"), - Map.entry(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO, "xltm"), - Map.entry(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW, "ppsx"), - Map.entry(MIMETYPE_OUTLOOK_MSG, "msg"), - Map.entry(MIMETYPE_DITA, "dita"), - Map.entry(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE, "xltx"), - Map.entry(MIMETYPE_IMAGE_SVG, "svg"), - Map.entry(MIMETYPE_TEXT_PLAIN, "txt"), - Map.entry(MIMETYPE_XHTML, "xhtml"), - Map.entry(MIMETYPE_IMAGE_XWD, "xwd") - ); - - protected ExtensionService() - { - } - - public static String getExtensionForTargetMimetype(String targetMimetype, String sourceMimetype) - { - if (targetMimetype == null) - { - return null; - } - return getExtensionForMimetype(MIMETYPE_METADATA_EMBED.equals(targetMimetype) ? sourceMimetype : targetMimetype); - } - - public static String getExtensionForMimetype(String mimetype) - { - if (mimetype == null) - { - return null; - } - if (mimetpeExtensions.containsKey(mimetype)) - { - return mimetpeExtensions.get(mimetype); - } - if (MIMETYPE_METADATA_EXTRACT.equals(mimetype)) - { - return "json"; - } - // else fall back to the original implementation - return splitMimetype(mimetype); - } - - // Moved from Dispatcher. This fails to work in many cases, but we need to be backward compatible. - private static String splitMimetype(final String mimetype) - { - final String[] parts = mimetype.split("[.\\-_|/]"); - return parts[parts.length - 1]; - } -} +/* + * #%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 static org.alfresco.transform.common.Mimetype.MIMETYPE_DITA; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_EXCEL; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_SVG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_XWD; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_GRAPHICS; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_PRESENTATION; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_PRESENTATION_TEMPLATE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_SPREADSHEET; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_SPREADSHEET_TEMPLATE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_TEXT; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENDOCUMENT_TEXT_TEMPLATE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENOFFICE1_CALC; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENOFFICE1_IMPRESS; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENOFFICE1_WRITER; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_ADDIN; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_MACRO; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_SLIDE_MACRO; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_TEMPLATE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_PRESENTATION_TEMPLATE_MACRO; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_MACRO; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORDPROCESSING; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORDPROCESSING_MACRO; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_OUTLOOK_MSG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PPT; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_VISIO; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_VISIO_2013; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORDPERFECT; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_XHTML; +import static org.alfresco.transform.common.TransformerDebug.MIMETYPE_METADATA_EMBED; +import static org.alfresco.transform.common.TransformerDebug.MIMETYPE_METADATA_EXTRACT; + +import java.util.Map; + +/** + * Provides mapping between mimtypes and file extensions, static and not configurable. The correct extension is required for a small subset of transforms in pipelines which go through the libreoffice transformer + */ +public class ExtensionService +{ + private final static String MIMETYPE_TAB_SEPARATED_VALUES = "text/tab-separated-values"; + private final static String MIMETYPE_CALC_TEMPLATE = "application/vnd.sun.xml.calc.template"; + private final static String MIMETYPE_IMPRESS_TEMPLATE = "application/vnd.sun.xml.impress.template"; + private final static String MIMETYPE_WRITER_TEMPLATE = "application/vnd.sun.xml.writer.template"; + + private static final Map mimetpeExtensions = Map.ofEntries( + Map.entry(MIMETYPE_WORD, "doc"), + Map.entry(MIMETYPE_OPENXML_WORDPROCESSING_MACRO, "docm"), + Map.entry(MIMETYPE_OPENXML_WORDPROCESSING, "docx"), + Map.entry(MIMETYPE_OPENXML_WORD_TEMPLATE_MACRO, "dotm"), + Map.entry(MIMETYPE_OPENXML_WORD_TEMPLATE, "dotx"), + Map.entry(MIMETYPE_OPENDOCUMENT_GRAPHICS, "odg"), + Map.entry(MIMETYPE_OPENDOCUMENT_PRESENTATION, "odp"), + Map.entry(MIMETYPE_OPENDOCUMENT_PRESENTATION_TEMPLATE, "otp"), + Map.entry(MIMETYPE_OPENDOCUMENT_SPREADSHEET, "ods"), + Map.entry(MIMETYPE_OPENDOCUMENT_SPREADSHEET_TEMPLATE, "ots"), + Map.entry(MIMETYPE_OPENDOCUMENT_TEXT, "odt"), + Map.entry(MIMETYPE_OPENDOCUMENT_TEXT_TEMPLATE, "ott"), + Map.entry(MIMETYPE_OPENXML_PRESENTATION_TEMPLATE_MACRO, "potm"), + Map.entry(MIMETYPE_OPENXML_PRESENTATION_TEMPLATE, "potx"), + Map.entry(MIMETYPE_OPENXML_PRESENTATION_ADDIN, "ppam"), + Map.entry(MIMETYPE_PPT, "ppt"), + Map.entry(MIMETYPE_OPENXML_PRESENTATION_MACRO, "pptm"), + Map.entry(MIMETYPE_OPENXML_PRESENTATION, "pptx"), + Map.entry(MIMETYPE_OPENXML_PRESENTATION_SLIDE_MACRO, "sldm"), + Map.entry(MIMETYPE_OPENXML_PRESENTATION_SLIDE, "sldx"), + Map.entry(MIMETYPE_CALC_TEMPLATE, "stc"), + Map.entry(MIMETYPE_IMPRESS_TEMPLATE, "sti"), + Map.entry(MIMETYPE_WRITER_TEMPLATE, "stw"), + Map.entry(MIMETYPE_TAB_SEPARATED_VALUES, "tsv"), + Map.entry(MIMETYPE_OPENOFFICE1_CALC, "sxc"), + Map.entry(MIMETYPE_OPENOFFICE1_IMPRESS, "sxi"), + Map.entry(MIMETYPE_OPENOFFICE1_WRITER, "sxw"), + Map.entry(MIMETYPE_VISIO, "vsd"), + Map.entry(MIMETYPE_VISIO_2013, "vsdx"), + Map.entry(MIMETYPE_WORDPERFECT, "wp"), + Map.entry(MIMETYPE_EXCEL, "xls"), + Map.entry(MIMETYPE_OPENXML_SPREADSHEET_BINARY_MACRO, "xlsb"), + Map.entry(MIMETYPE_OPENXML_SPREADSHEET_MACRO, "xlsm"), + Map.entry(MIMETYPE_OPENXML_SPREADSHEET, "xlsx"), + Map.entry(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE_MACRO, "xltm"), + Map.entry(MIMETYPE_OPENXML_PRESENTATION_SLIDESHOW, "ppsx"), + Map.entry(MIMETYPE_OUTLOOK_MSG, "msg"), + Map.entry(MIMETYPE_DITA, "dita"), + Map.entry(MIMETYPE_OPENXML_SPREADSHEET_TEMPLATE, "xltx"), + Map.entry(MIMETYPE_IMAGE_SVG, "svg"), + Map.entry(MIMETYPE_TEXT_PLAIN, "txt"), + Map.entry(MIMETYPE_XHTML, "xhtml"), + Map.entry(MIMETYPE_IMAGE_XWD, "xwd")); + + protected ExtensionService() + {} + + public static String getExtensionForTargetMimetype(String targetMimetype, String sourceMimetype) + { + if (targetMimetype == null) + { + return null; + } + return getExtensionForMimetype(MIMETYPE_METADATA_EMBED.equals(targetMimetype) ? sourceMimetype : targetMimetype); + } + + public static String getExtensionForMimetype(String mimetype) + { + if (mimetype == null) + { + return null; + } + if (mimetpeExtensions.containsKey(mimetype)) + { + return mimetpeExtensions.get(mimetype); + } + if (MIMETYPE_METADATA_EXTRACT.equals(mimetype)) + { + return "json"; + } + // else fall back to the original implementation + return splitMimetype(mimetype); + } + + // Moved from Dispatcher. This fails to work in many cases, but we need to be backward compatible. + private static String splitMimetype(final String mimetype) + { + final String[] parts = mimetype.split("[.\\-_|/]"); + return parts[parts.length - 1]; + } +} diff --git a/model/src/main/java/org/alfresco/transform/common/RepositoryClientData.java b/model/src/main/java/org/alfresco/transform/common/RepositoryClientData.java index b6a70370..066478d9 100644 --- a/model/src/main/java/org/alfresco/transform/common/RepositoryClientData.java +++ b/model/src/main/java/org/alfresco/transform/common/RepositoryClientData.java @@ -25,10 +25,10 @@ import java.util.StringJoiner; import java.util.stream.Stream; /** - * The client data supplied and echoed back to content repository (the client). May be modified to include - * TransformerDebug. + * The client data supplied and echoed back to content repository (the client). May be modified to include TransformerDebug. */ -public class RepositoryClientData { +public class RepositoryClientData +{ public static final String CLIENT_DATA_SEPARATOR = "\u23D0"; public static final String DEBUG_SEPARATOR = "\u23D1"; public static final String REPO_ID = "Repo"; @@ -66,23 +66,29 @@ public class RepositoryClientData { return isRepositoryClientData() ? split[REQUEST_ID_INDEX] : ""; } - public String getRenditionName() { + public String getRenditionName() + { return isRepositoryClientData() ? split[RENDITION_INDEX] : ""; } - public void appendDebug(String message) { + public void appendDebug(String message) + { if (isDebugRequested()) { split[DEBUG_INDEX] += DEBUG_SEPARATOR + message; } } - public boolean isDebugRequested() { + public boolean isDebugRequested() + { return isRepositoryClientData() && split[9].startsWith(DEBUG); } - @Override public String toString() { - if (split == null) { + @Override + public String toString() + { + if (split == null) + { return origClientData; } StringJoiner sj = new StringJoiner(CLIENT_DATA_SEPARATOR); @@ -90,7 +96,8 @@ public class RepositoryClientData { return sj.toString(); } - public static Builder builder() { + public static Builder builder() + { return new Builder(); } @@ -99,14 +106,13 @@ public class RepositoryClientData { private final RepositoryClientData clientData = new RepositoryClientData(emptyClientData()); private Builder() - { - } + {} private static String emptyClientData() { StringJoiner sj = new StringJoiner(CLIENT_DATA_SEPARATOR); - sj.add(REPO_ID+"ACS1234"); - for (int i=0; i options) { if (logger.isDebugEnabled() && !isTRouter && options != null && !options.isEmpty()) @@ -186,8 +187,8 @@ public class TransformerDebug int len = value.length(); if (len > MAX_OPTION_VALUE) { - value = value.substring(0, MAX_OPTION_VALUE-MAX_OPTION_DOTS.length()-MAX_OPTION_END_CHARS) + - MAX_OPTION_DOTS +value.substring(len-MAX_OPTION_END_CHARS); + value = value.substring(0, MAX_OPTION_VALUE - MAX_OPTION_DOTS.length() - MAX_OPTION_END_CHARS) + + MAX_OPTION_DOTS + value.substring(len - MAX_OPTION_END_CHARS); } return getPaddedReference(reference) + " " + key + "=\"" + value.replaceAll("\"", "\\\"") + "\""; @@ -277,7 +278,7 @@ public class TransformerDebug else { sb.append(mimetypeExt); - sb.append(spaces(4 - mimetypeExt.length())); // Pad to normal max ext (4) + sb.append(spaces(4 - mimetypeExt.length())); // Pad to normal max ext (4) } sb.append(' '); return sb.toString(); @@ -286,18 +287,18 @@ public class TransformerDebug private String getRenditionName(String renditionName) { return !renditionName.isEmpty() - ? "-- "+ replaceWithMetadataRenditionNameIfEmbedOrExtract(renditionName)+" -- " + ? "-- " + replaceWithMetadataRenditionNameIfEmbedOrExtract(renditionName) + " -- " : ""; } private static String replaceWithMetadataRenditionNameIfEmbedOrExtract(String renditionName) { String transformName = getTransformName(renditionName); - return transformName.startsWith(MIMETYPE_METADATA_EXTRACT) + return transformName.startsWith(MIMETYPE_METADATA_EXTRACT) ? "metadataExtract" : transformName.startsWith(MIMETYPE_METADATA_EMBED) - ? "metadataEmbed" - : renditionName; + ? "metadataEmbed" + : renditionName; } private static String getTransformName(String renditionName) @@ -332,9 +333,9 @@ public class TransformerDebug { return "1 byte"; } - final String[] units = new String[] { "bytes", "KB", "MB", "GB", "TB" }; + final String[] units = new String[]{"bytes", "KB", "MB", "GB", "TB"}; long divider = 1; - for(int i = 0; i < units.length-1; i++) + for (int i = 0; i < units.length - 1; i++) { long nextDivider = divider * 1024; if (size < nextDivider) @@ -343,7 +344,7 @@ public class TransformerDebug } divider = nextDivider; } - return fileSizeFormat(size, divider, units[units.length-1]); + return fileSizeFormat(size, divider, units[units.length - 1]); } private static String fileSizeFormat(long size, long divider, String unit) @@ -352,7 +353,7 @@ public class TransformerDebug int decimalPoint = (int) size % 10; StringBuilder sb = new StringBuilder(); - sb.append(size/10); + sb.append(size / 10); if (decimalPoint != 0) { sb.append("."); diff --git a/model/src/main/java/org/alfresco/transform/config/AbstractTransformOption.java b/model/src/main/java/org/alfresco/transform/config/AbstractTransformOption.java index de0b4851..abcd955e 100644 --- a/model/src/main/java/org/alfresco/transform/config/AbstractTransformOption.java +++ b/model/src/main/java/org/alfresco/transform/config/AbstractTransformOption.java @@ -31,8 +31,7 @@ public abstract class AbstractTransformOption implements TransformOption private boolean required; public AbstractTransformOption() - { - } + {} public AbstractTransformOption(boolean required) { @@ -54,8 +53,10 @@ public abstract class AbstractTransformOption implements TransformOption @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; AbstractTransformOption that = (AbstractTransformOption) o; return required == that.required; } @@ -70,7 +71,7 @@ public abstract class AbstractTransformOption implements TransformOption public String toString() { return "AbstractTransformOption{" + - "required=" + required + - '}'; + "required=" + required + + '}'; } } diff --git a/model/src/main/java/org/alfresco/transform/config/AddSupported.java b/model/src/main/java/org/alfresco/transform/config/AddSupported.java index 58fd267d..609e5356 100644 --- a/model/src/main/java/org/alfresco/transform/config/AddSupported.java +++ b/model/src/main/java/org/alfresco/transform/config/AddSupported.java @@ -22,7 +22,9 @@ package org.alfresco.transform.config; /** - * Holds information to add new {@link SupportedSourceAndTarget} objects to an existing {@link Transformer}.


    + * Holds information to add new {@link SupportedSourceAndTarget} objects to an existing {@link Transformer}. + *

    + *
    * *

      *   "addSupported": [
    diff --git a/model/src/main/java/org/alfresco/transform/config/OverrideSupported.java b/model/src/main/java/org/alfresco/transform/config/OverrideSupported.java
    index 80d02f28..301279a7 100644
    --- a/model/src/main/java/org/alfresco/transform/config/OverrideSupported.java
    +++ b/model/src/main/java/org/alfresco/transform/config/OverrideSupported.java
    @@ -22,8 +22,9 @@
     package org.alfresco.transform.config;
     
     /**
    - * Holds information to overriding existing {@link SupportedSourceAndTarget} objects with new {@code priority} and
    - * {@code maxSourceSizeBytes} values.


    + * Holds information to overriding existing {@link SupportedSourceAndTarget} objects with new {@code priority} and {@code maxSourceSizeBytes} values. + *

    + *
    * *

      *   "overrideSupported" : [
    diff --git a/model/src/main/java/org/alfresco/transform/config/RemoveSupported.java b/model/src/main/java/org/alfresco/transform/config/RemoveSupported.java
    index 1c0ce805..40de95cb 100644
    --- a/model/src/main/java/org/alfresco/transform/config/RemoveSupported.java
    +++ b/model/src/main/java/org/alfresco/transform/config/RemoveSupported.java
    @@ -22,7 +22,9 @@
     package org.alfresco.transform.config;
     
     /**
    - * Holds information about existing {@link SupportedSourceAndTarget} objects that should be removed.


    + * Holds information about existing {@link SupportedSourceAndTarget} objects that should be removed. + *

    + *
    * *

      *   "removeSupported" : [
    @@ -39,8 +41,9 @@ public class RemoveSupported extends TransformerAndTypes
         @Override
         public String toString()
         {
    -        return "{"+super.toString()+"}";
    +        return "{" + super.toString() + "}";
         }
    +
         public static Builder builder()
         {
             return new Builder();
    diff --git a/model/src/main/java/org/alfresco/transform/config/SupportedDefaults.java b/model/src/main/java/org/alfresco/transform/config/SupportedDefaults.java
    index 07c3b20c..e6075dfe 100644
    --- a/model/src/main/java/org/alfresco/transform/config/SupportedDefaults.java
    +++ b/model/src/main/java/org/alfresco/transform/config/SupportedDefaults.java
    @@ -25,19 +25,20 @@ import java.util.Objects;
     import java.util.StringJoiner;
     
     /**
    - * Holds information to provide default {@code priority} and / or {@code maxSourceSizeBytes} defaults. In precedence
    - * order from most specific to most general these are defined by combinations of {@code transformerName} and
    - * {@code sourceMediaType}:


    + * Holds information to provide default {@code priority} and / or {@code maxSourceSizeBytes} defaults. In precedence order from most specific to most general these are defined by combinations of {@code transformerName} and {@code sourceMediaType}: + *

    + *
    *

      - *
    • transformer and source media type default {@code transformerName} + {@code sourceMediaType}
    • - *
    • transformer default {@code transformerName}
    • - *
    • source media type default {@code sourceMediaType}
    • - *
    • system wide default none
    • - *

    + *
  • transformer and source media type default {@code transformerName} + {@code sourceMediaType}
  • + *
  • transformer default {@code transformerName}
  • + *
  • source media type default {@code sourceMediaType}
  • + *
  • system wide default none
  • + * + *
    * - * Both {@code maxSourceSizeBytes} and {@code priority} may be specified in a {@code "supportedDefaults"} element, but - * if only one is specified it is only that value that is being defaulted at the level specified by the combination of - * {@code transformerName} and {@code sourceMediaType}.


    + * Both {@code maxSourceSizeBytes} and {@code priority} may be specified in a {@code "supportedDefaults"} element, but if only one is specified it is only that value that is being defaulted at the level specified by the combination of {@code transformerName} and {@code sourceMediaType}. + *

    + *
    * *

      *   "supportedDefaults" : [
    @@ -110,9 +111,11 @@ public class SupportedDefaults
         @Override
         public boolean equals(Object o)
         {
    -        if (this == o) return true;
    -        if (o == null || getClass() != o.getClass()) return false;
    -        SupportedDefaults that = (SupportedDefaults)o;
    +        if (this == o)
    +            return true;
    +        if (o == null || getClass() != o.getClass())
    +            return false;
    +        SupportedDefaults that = (SupportedDefaults) o;
             return Objects.equals(transformerName, that.transformerName) &&
                     Objects.equals(sourceMediaType, that.sourceMediaType) &&
                     Objects.equals(maxSourceSizeBytes, that.maxSourceSizeBytes) &&
    @@ -129,10 +132,14 @@ public class SupportedDefaults
         public String toString()
         {
             StringJoiner sj = new StringJoiner(", ");
    -        if (transformerName != null) sj.add("\"transformerName\": \""+transformerName+'"');
    -        if (sourceMediaType != null) sj.add("\"sourceMediaType\": \""+sourceMediaType+'"');
    -        if (maxSourceSizeBytes != null) sj.add("\"maxSourceSizeBytes\": \""+maxSourceSizeBytes+'"');
    -        if (priority != null) sj.add("\"priority\": \""+priority+'"');
    +        if (transformerName != null)
    +            sj.add("\"transformerName\": \"" + transformerName + '"');
    +        if (sourceMediaType != null)
    +            sj.add("\"sourceMediaType\": \"" + sourceMediaType + '"');
    +        if (maxSourceSizeBytes != null)
    +            sj.add("\"maxSourceSizeBytes\": \"" + maxSourceSizeBytes + '"');
    +        if (priority != null)
    +            sj.add("\"priority\": \"" + priority + '"');
             return "{" + sj.toString() + "}";
         }
     
    diff --git a/model/src/main/java/org/alfresco/transform/config/SupportedSourceAndTarget.java b/model/src/main/java/org/alfresco/transform/config/SupportedSourceAndTarget.java
    index 01bcbc27..86a049ba 100644
    --- a/model/src/main/java/org/alfresco/transform/config/SupportedSourceAndTarget.java
    +++ b/model/src/main/java/org/alfresco/transform/config/SupportedSourceAndTarget.java
    @@ -25,8 +25,7 @@ import java.util.Objects;
     import java.util.StringJoiner;
     
     /**
    - * Represents a single source and target combination supported by a transformer. Each combination has an optional
    - * maximum size and priority.
    + * Represents a single source and target combination supported by a transformer. Each combination has an optional maximum size and priority.
      */
     public class SupportedSourceAndTarget extends Types
     {
    @@ -34,8 +33,7 @@ public class SupportedSourceAndTarget extends Types
         Integer priority = null;
     
         public SupportedSourceAndTarget()
    -    {
    -    }
    +    {}
     
         public Long getMaxSourceSizeBytes()
         {
    @@ -60,12 +58,15 @@ public class SupportedSourceAndTarget extends Types
         @Override
         public boolean equals(Object o)
         {
    -        if (this == o) return true;
    -        if (o == null || getClass() != o.getClass()) return false;
    -        if (!super.equals(o)) return false;
    -        SupportedSourceAndTarget that = (SupportedSourceAndTarget)o;
    +        if (this == o)
    +            return true;
    +        if (o == null || getClass() != o.getClass())
    +            return false;
    +        if (!super.equals(o))
    +            return false;
    +        SupportedSourceAndTarget that = (SupportedSourceAndTarget) o;
             return Objects.equals(maxSourceSizeBytes, that.maxSourceSizeBytes) &&
    -               Objects.equals(priority, that.priority);
    +                Objects.equals(priority, that.priority);
         }
     
         @Override
    @@ -79,9 +80,12 @@ public class SupportedSourceAndTarget extends Types
         {
             StringJoiner sj = new StringJoiner(", ");
             String superToString = super.toString();
    -        if (superToString != null) sj.add(superToString);
    -        if (maxSourceSizeBytes != null) sj.add("\"maxSourceSizeBytes\": \""+maxSourceSizeBytes+'"');
    -        if (priority != null) sj.add("\"priority\": \""+priority+'"');
    +        if (superToString != null)
    +            sj.add(superToString);
    +        if (maxSourceSizeBytes != null)
    +            sj.add("\"maxSourceSizeBytes\": \"" + maxSourceSizeBytes + '"');
    +        if (priority != null)
    +            sj.add("\"priority\": \"" + priority + '"');
             return "{" + sj.toString() + "}";
         }
     
    diff --git a/model/src/main/java/org/alfresco/transform/config/TransformConfig.java b/model/src/main/java/org/alfresco/transform/config/TransformConfig.java
    index d1b31e8e..97813943 100644
    --- a/model/src/main/java/org/alfresco/transform/config/TransformConfig.java
    +++ b/model/src/main/java/org/alfresco/transform/config/TransformConfig.java
    @@ -30,8 +30,7 @@ import java.util.Objects;
     import java.util.Set;
     
     /**
    - * Transform Configuration with multiple {@link Transformer}s and {@link TransformOption}s.
    - * It can be used for one or more Transformers.
    + * Transform Configuration with multiple {@link Transformer}s and {@link TransformOption}s. It can be used for one or more Transformers.
      */
     public class TransformConfig
     {
    @@ -122,10 +121,12 @@ public class TransformConfig
         @Override
         public boolean equals(Object o)
         {
    -        if (this == o) return true;
    -        if (o == null || getClass() != o.getClass()) return false;
    +        if (this == o)
    +            return true;
    +        if (o == null || getClass() != o.getClass())
    +            return false;
             TransformConfig that = (TransformConfig) o;
    -        return  transformOptions.equals(that.transformOptions) &&
    +        return transformOptions.equals(that.transformOptions) &&
                     transformers.equals(that.transformers) &&
                     removeTransformers.equals(that.removeTransformers) &&
                     addSupported.equals(that.addSupported) &&
    @@ -145,9 +146,9 @@ public class TransformConfig
         public String toString()
         {
             return "TransformConfig{" +
    -               "transformOptions=" + transformOptions +
    -               ", transformers=" + transformers +
    -               '}';
    +                "transformOptions=" + transformOptions +
    +                ", transformers=" + transformers +
    +                '}';
         }
     
         public static Builder builder()
    @@ -159,7 +160,8 @@ public class TransformConfig
         {
             private final TransformConfig transformConfig = new TransformConfig();
     
    -        private Builder() {}
    +        private Builder()
    +        {}
     
             public TransformConfig build()
             {
    diff --git a/model/src/main/java/org/alfresco/transform/config/TransformOption.java b/model/src/main/java/org/alfresco/transform/config/TransformOption.java
    index 61597b76..7c964db4 100644
    --- a/model/src/main/java/org/alfresco/transform/config/TransformOption.java
    +++ b/model/src/main/java/org/alfresco/transform/config/TransformOption.java
    @@ -28,9 +28,9 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
      * Represents an individual transformer option or group of options that are required or optional (default).
      */
     @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
    -              include = JsonTypeInfo.As.WRAPPER_OBJECT)
    +        include = JsonTypeInfo.As.WRAPPER_OBJECT)
     @JsonSubTypes({@JsonSubTypes.Type(value = TransformOptionValue.class, name = "value"),
    -               @JsonSubTypes.Type(value = TransformOptionGroup.class, name = "group")})
    +        @JsonSubTypes.Type(value = TransformOptionGroup.class, name = "group")})
     public interface TransformOption
     {
         boolean isRequired();
    diff --git a/model/src/main/java/org/alfresco/transform/config/TransformOptionGroup.java b/model/src/main/java/org/alfresco/transform/config/TransformOptionGroup.java
    index 456dc26f..aea3e1c4 100644
    --- a/model/src/main/java/org/alfresco/transform/config/TransformOptionGroup.java
    +++ b/model/src/main/java/org/alfresco/transform/config/TransformOptionGroup.java
    @@ -26,9 +26,7 @@ import java.util.Objects;
     import java.util.Set;
     
     /**
    - * Represents a group of one or more options. If the group is optional, child options that are marked as required are
    - * only required if any child in the group is supplied by the client. If the group is required, child options are
    - * optional or required based on their own setting alone.
    + * Represents a group of one or more options. If the group is optional, child options that are marked as required are only required if any child in the group is supplied by the client. If the group is required, child options are optional or required based on their own setting alone.
      *
      * In a pipeline transformation, a group of options
      */
    @@ -37,8 +35,7 @@ public class TransformOptionGroup extends AbstractTransformOption
         private Set transformOptions = new HashSet<>();
     
         public TransformOptionGroup()
    -    {
    -    }
    +    {}
     
         public TransformOptionGroup(boolean required, Set transformOptions)
         {
    @@ -59,9 +56,12 @@ public class TransformOptionGroup extends AbstractTransformOption
         @Override
         public boolean equals(Object o)
         {
    -        if (this == o) return true;
    -        if (o == null || getClass() != o.getClass()) return false;
    -        if (!super.equals(o)) return false;
    +        if (this == o)
    +            return true;
    +        if (o == null || getClass() != o.getClass())
    +            return false;
    +        if (!super.equals(o))
    +            return false;
             TransformOptionGroup that = (TransformOptionGroup) o;
             return Objects.equals(transformOptions, that.transformOptions);
         }
    @@ -76,7 +76,7 @@ public class TransformOptionGroup extends AbstractTransformOption
         public String toString()
         {
             return "TransformOptionGroup{" +
    -               "transformOptions=" + transformOptions +
    -               '}';
    +                "transformOptions=" + transformOptions +
    +                '}';
         }
    -}
    \ No newline at end of file
    +}
    diff --git a/model/src/main/java/org/alfresco/transform/config/TransformOptionValue.java b/model/src/main/java/org/alfresco/transform/config/TransformOptionValue.java
    index 823ae095..1fdc525d 100644
    --- a/model/src/main/java/org/alfresco/transform/config/TransformOptionValue.java
    +++ b/model/src/main/java/org/alfresco/transform/config/TransformOptionValue.java
    @@ -31,8 +31,7 @@ public class TransformOptionValue extends AbstractTransformOption
         private String name;
     
         public TransformOptionValue()
    -    {
    -    }
    +    {}
     
         public TransformOptionValue(boolean required, String name)
         {
    @@ -53,9 +52,12 @@ public class TransformOptionValue extends AbstractTransformOption
         @Override
         public boolean equals(Object o)
         {
    -        if (this == o) return true;
    -        if (o == null || getClass() != o.getClass()) return false;
    -        if (!super.equals(o)) return false;
    +        if (this == o)
    +            return true;
    +        if (o == null || getClass() != o.getClass())
    +            return false;
    +        if (!super.equals(o))
    +            return false;
             TransformOptionValue that = (TransformOptionValue) o;
             return Objects.equals(name, that.name);
         }
    @@ -70,7 +72,7 @@ public class TransformOptionValue extends AbstractTransformOption
         public String toString()
         {
             return "TransformOptionValue{" +
    -               "name='" + name + '\'' +
    -               '}';
    +                "name='" + name + '\'' +
    +                '}';
         }
     }
    diff --git a/model/src/main/java/org/alfresco/transform/config/TransformStep.java b/model/src/main/java/org/alfresco/transform/config/TransformStep.java
    index a91526b6..881dc318 100644
    --- a/model/src/main/java/org/alfresco/transform/config/TransformStep.java
    +++ b/model/src/main/java/org/alfresco/transform/config/TransformStep.java
    @@ -24,8 +24,7 @@ package org.alfresco.transform.config;
     import java.util.Objects;
     
     /**
    - * Represents a single transform step in a transform pipeline. The last step in the pipeline does not specify the
    - * target type as that is based on the supported types and what has been requested.
    + * Represents a single transform step in a transform pipeline. The last step in the pipeline does not specify the target type as that is based on the supported types and what has been requested.
      */
     public class TransformStep
     {
    @@ -33,8 +32,7 @@ public class TransformStep
         private String targetMediaType;
     
         public TransformStep()
    -    {
    -    }
    +    {}
     
         public TransformStep(String transformerName, String targetMediaType)
         {
    @@ -65,11 +63,13 @@ public class TransformStep
         @Override
         public boolean equals(Object o)
         {
    -        if (this == o) return true;
    -        if (o == null || getClass() != o.getClass()) return false;
    +        if (this == o)
    +            return true;
    +        if (o == null || getClass() != o.getClass())
    +            return false;
             TransformStep that = (TransformStep) o;
             return Objects.equals(transformerName, that.transformerName) &&
    -               Objects.equals(targetMediaType, that.targetMediaType);
    +                Objects.equals(targetMediaType, that.targetMediaType);
         }
     
         @Override
    @@ -82,8 +82,8 @@ public class TransformStep
         public String toString()
         {
             return "TransformStep{" +
    -               "transformerName='" + transformerName + '\'' +
    -               ", targetMediaType='" + targetMediaType + '\'' +
    -               '}';
    +                "transformerName='" + transformerName + '\'' +
    +                ", targetMediaType='" + targetMediaType + '\'' +
    +                '}';
         }
     }
    diff --git a/model/src/main/java/org/alfresco/transform/config/Transformer.java b/model/src/main/java/org/alfresco/transform/config/Transformer.java
    index 6404264c..4ced7141 100644
    --- a/model/src/main/java/org/alfresco/transform/config/Transformer.java
    +++ b/model/src/main/java/org/alfresco/transform/config/Transformer.java
    @@ -21,37 +21,24 @@
      */
     package org.alfresco.transform.config;
     
    -import org.alfresco.transform.registry.TransformServiceRegistry;
    -
     import java.util.ArrayList;
     import java.util.HashSet;
     import java.util.List;
     import java.util.Objects;
     import java.util.Set;
     
    +import org.alfresco.transform.registry.TransformServiceRegistry;
    +
     /**
    - * Represents a set of transformations supported by the Transform Service or Local Transform Service Registry that
    - * share the same transform options. Each may be an actual transformer, a pipeline of multiple transformers or a list
    - * of failover transforms. It is possible that more than one transformer may able to perform a transformation from one
    - * mimetype to another. The actual selection of transformer is up to the Transform Service or Local Transform Service
    - * Registry to decide. Clients may use
    - * {@link TransformServiceRegistry#isSupported(String, long, String, java.util.Map, String)} to decide
    - * if they should send a request to the service. As a result clients have a simple generic view of transformations which
    - * allows new transformations to be added without the need to change client data structures other than to define new
    - * name value pairs. For this to work the Transform Service defines unique names for each option.
    + * Represents a set of transformations supported by the Transform Service or Local Transform Service Registry that share the same transform options. Each may be an actual transformer, a pipeline of multiple transformers or a list of failover transforms. It is possible that more than one transformer may able to perform a transformation from one mimetype to another. The actual selection of transformer is up to the Transform Service or Local Transform Service Registry to decide. Clients may use {@link TransformServiceRegistry#isSupported(String, long, String, java.util.Map, String)} to decide if they should send a request to the service. As a result clients have a simple generic view of transformations which allows new transformations to be added without the need to change client data structures other than to define new name value pairs. For this to work the Transform Service defines unique names for each option.
      * 
      - *
    • transformerName - is optional but if supplied should be unique. The client should infer nothing from the name - * as it is simply a label, but the Local Transform Service Registry will use the name in pipelines.
    • - *
    • transformOptions - a grouping of individual transformer transformOptions. The group may be optional and may - * contain nested transformOptions.
    • + *
    • transformerName - is optional but if supplied should be unique. The client should infer nothing from the name as it is simply a label, but the Local Transform Service Registry will use the name in pipelines.
    • + *
    • transformOptions - a grouping of individual transformer transformOptions. The group may be optional and may contain nested transformOptions.
    • *
    * For local transforms, this structure is extended when defining a pipeline transform and failover transform. *
      - *
    • transformerPipeline - an array of pairs of transformer name and target extension for each transformer in the - * pipeline. The last one should not have an extension as that is defined by the request and should be in the - * supported list.
    • - *
    • transformerFailover - an array of failover definitions used in case of a fail transformation to pass a document - * to a sequence of transforms until one succeeds.
    • + *
    • transformerPipeline - an array of pairs of transformer name and target extension for each transformer in the pipeline. The last one should not have an extension as that is defined by the request and should be in the supported list.
    • + *
    • transformerFailover - an array of failover definitions used in case of a fail transformation to pass a document to a sequence of transforms until one succeeds.
    • *
    • coreVersion - indicates the version of the T-Engine's base. See {@link CoreVersionDecorator} for more detail.
    • *
    */ @@ -65,11 +52,10 @@ public class Transformer private List transformerFailover = new ArrayList<>(); public Transformer() - { - } + {} public Transformer(String transformerName, Set transformOptions, - Set supportedSourceAndTargetList) + Set supportedSourceAndTargetList) { this.transformerName = transformerName; this.transformOptions = transformOptions; @@ -77,16 +63,16 @@ public class Transformer } public Transformer(String transformerName, Set transformOptions, - Set supportedSourceAndTargetList, - List transformerPipeline) + Set supportedSourceAndTargetList, + List transformerPipeline) { this(transformerName, transformOptions, supportedSourceAndTargetList); this.transformerPipeline = transformerPipeline; } public Transformer(String transformerName, Set transformOptions, - Set supportedSourceAndTargetList, - List transformerPipeline, List transformerFailover) + Set supportedSourceAndTargetList, + List transformerPipeline, List transformerFailover) { this(transformerName, transformOptions, supportedSourceAndTargetList, transformerPipeline); this.transformerFailover = transformerFailover; @@ -148,7 +134,7 @@ public class Transformer } public void setSupportedSourceAndTargetList( - Set supportedSourceAndTargetList) + Set supportedSourceAndTargetList) { this.supportedSourceAndTargetList = supportedSourceAndTargetList == null ? new HashSet<>() : supportedSourceAndTargetList; } @@ -156,36 +142,38 @@ public class Transformer @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; Transformer that = (Transformer) o; return Objects.equals(transformerName, that.transformerName) && - Objects.equals(coreVersion, that.coreVersion) && - Objects.equals(transformerPipeline, that.transformerPipeline) && - Objects.equals(transformerFailover, that.transformerFailover) && - Objects.equals(transformOptions, that.transformOptions) && - Objects.equals(supportedSourceAndTargetList, - that.supportedSourceAndTargetList); + Objects.equals(coreVersion, that.coreVersion) && + Objects.equals(transformerPipeline, that.transformerPipeline) && + Objects.equals(transformerFailover, that.transformerFailover) && + Objects.equals(transformOptions, that.transformOptions) && + Objects.equals(supportedSourceAndTargetList, + that.supportedSourceAndTargetList); } @Override public int hashCode() { return Objects.hash(transformerName, coreVersion, transformerPipeline, transformerFailover, transformOptions, - supportedSourceAndTargetList); + supportedSourceAndTargetList); } @Override public String toString() { return "Transformer{" + - "transformerName='" + transformerName + '\'' + - ", coreVersion=" + coreVersion + - ", transformerPipeline=" + transformerPipeline + - ", transformerFailover=" + transformerFailover + - ", transformOptions=" + transformOptions + - ", supportedSourceAndTargetList=" + supportedSourceAndTargetList + - '}'; + "transformerName='" + transformerName + '\'' + + ", coreVersion=" + coreVersion + + ", transformerPipeline=" + transformerPipeline + + ", transformerFailover=" + transformerFailover + + ", transformOptions=" + transformOptions + + ", supportedSourceAndTargetList=" + supportedSourceAndTargetList + + '}'; } public static Builder builder() @@ -197,7 +185,8 @@ public class Transformer { private final Transformer transformer = new Transformer(); - private Builder() {} + private Builder() + {} public Transformer build() { @@ -235,7 +224,7 @@ public class Transformer } public Builder withSupportedSourceAndTargetList( - final Set supportedSourceAndTargetList) + final Set supportedSourceAndTargetList) { transformer.supportedSourceAndTargetList = supportedSourceAndTargetList; return this; diff --git a/model/src/main/java/org/alfresco/transform/config/TransformerAndTypes.java b/model/src/main/java/org/alfresco/transform/config/TransformerAndTypes.java index 978f4cfe..92572b4f 100644 --- a/model/src/main/java/org/alfresco/transform/config/TransformerAndTypes.java +++ b/model/src/main/java/org/alfresco/transform/config/TransformerAndTypes.java @@ -25,14 +25,14 @@ import java.util.Objects; import java.util.StringJoiner; /** - * Base object with {@code transformerName}, {@code sourceMediaType}and {@code targetMediaType}. - * Used to identify supported transforms. + * Base object with {@code transformerName}, {@code sourceMediaType}and {@code targetMediaType}. Used to identify supported transforms. */ public abstract class TransformerAndTypes extends Types { String transformerName; - protected TransformerAndTypes() {} + protected TransformerAndTypes() + {} public String getTransformerName() { @@ -47,11 +47,14 @@ public abstract class TransformerAndTypes extends Types @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - if (!super.equals(o)) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + if (!super.equals(o)) + return false; TransformerAndTypes that = (TransformerAndTypes) o; - return Objects.equals(transformerName, that.transformerName); + return Objects.equals(transformerName, that.transformerName); } @Override @@ -65,8 +68,10 @@ public abstract class TransformerAndTypes extends Types { StringJoiner sj = new StringJoiner(", "); String superToString = super.toString(); - if (transformerName != null) sj.add("\"transformerName\": \""+transformerName+'"'); - if (superToString != null) sj.add(superToString); + if (transformerName != null) + sj.add("\"transformerName\": \"" + transformerName + '"'); + if (superToString != null) + sj.add(superToString); return sj.toString(); } @@ -84,7 +89,7 @@ public abstract class TransformerAndTypes extends Types public B withTransformerName(final String transformerName) { t.transformerName = transformerName; - return (B)this; + return (B) this; } } } diff --git a/model/src/main/java/org/alfresco/transform/config/TransformerTypesSizeAndPriority.java b/model/src/main/java/org/alfresco/transform/config/TransformerTypesSizeAndPriority.java index 808c8f74..45a7bbf4 100644 --- a/model/src/main/java/org/alfresco/transform/config/TransformerTypesSizeAndPriority.java +++ b/model/src/main/java/org/alfresco/transform/config/TransformerTypesSizeAndPriority.java @@ -25,15 +25,15 @@ import java.util.Objects; import java.util.StringJoiner; /** - * Base object with {@code transformerName}, {@code sourceMediaType}, {@code targetMediaType}, - * {@code maxSourceSizeBytes} and {@code priority}. + * Base object with {@code transformerName}, {@code sourceMediaType}, {@code targetMediaType}, {@code maxSourceSizeBytes} and {@code priority}. */ public abstract class TransformerTypesSizeAndPriority extends TransformerAndTypes { Long maxSourceSizeBytes = null; Integer priority = null; - protected TransformerTypesSizeAndPriority() {} + protected TransformerTypesSizeAndPriority() + {} public Long getMaxSourceSizeBytes() { @@ -58,11 +58,14 @@ public abstract class TransformerTypesSizeAndPriority extends TransformerAndType @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - if (!super.equals(o)) return false; - TransformerTypesSizeAndPriority that = (TransformerTypesSizeAndPriority)o; - return getMaxSourceSizeBytes().equals(that.getMaxSourceSizeBytes()) && + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + if (!super.equals(o)) + return false; + TransformerTypesSizeAndPriority that = (TransformerTypesSizeAndPriority) o; + return getMaxSourceSizeBytes().equals(that.getMaxSourceSizeBytes()) && getPriority() == that.getPriority(); } @@ -77,10 +80,13 @@ public abstract class TransformerTypesSizeAndPriority extends TransformerAndType { StringJoiner sj = new StringJoiner(", "); String superToString = super.toString(); - if (superToString != null) sj.add(superToString); - if (maxSourceSizeBytes != null) sj.add("\"maxSourceSizeBytes\": \""+maxSourceSizeBytes+'"'); - if (priority != null) sj.add("\"priority\": \""+priority+'"'); - return "{"+sj.toString()+"}"; + if (superToString != null) + sj.add(superToString); + if (maxSourceSizeBytes != null) + sj.add("\"maxSourceSizeBytes\": \"" + maxSourceSizeBytes + '"'); + if (priority != null) + sj.add("\"priority\": \"" + priority + '"'); + return "{" + sj.toString() + "}"; } public static class Builder @@ -97,13 +103,13 @@ public abstract class TransformerTypesSizeAndPriority extends TransformerAndType public B withMaxSourceSizeBytes(final long maxSourceSizeBytes) { t.setMaxSourceSizeBytes(maxSourceSizeBytes); - return (B)this; + return (B) this; } public B withPriority(final int priority) { t.setPriority(priority); - return (B)this; + return (B) this; } } } diff --git a/model/src/main/java/org/alfresco/transform/config/Types.java b/model/src/main/java/org/alfresco/transform/config/Types.java index fbbcbde2..1c3c1444 100644 --- a/model/src/main/java/org/alfresco/transform/config/Types.java +++ b/model/src/main/java/org/alfresco/transform/config/Types.java @@ -25,15 +25,15 @@ import java.util.Objects; import java.util.StringJoiner; /** - * Base object with {@code sourceMediaType} and {@code targetMediaType}. - * Used to identify supported transforms. + * Base object with {@code sourceMediaType} and {@code targetMediaType}. Used to identify supported transforms. */ public class Types { String sourceMediaType; String targetMediaType; - protected Types() {} + protected Types() + {} public String getSourceMediaType() { @@ -58,10 +58,12 @@ public class Types @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; Types that = (Types) o; - return Objects.equals(sourceMediaType, that.sourceMediaType) && + return Objects.equals(sourceMediaType, that.sourceMediaType) && Objects.equals(targetMediaType, that.targetMediaType); } @@ -75,8 +77,10 @@ public class Types public String toString() { StringJoiner sj = new StringJoiner(", "); - if (sourceMediaType != null) sj.add("\"sourceMediaType\": \""+sourceMediaType+'"'); - if (targetMediaType != null) sj.add("\"targetMediaType\": \""+targetMediaType+'"'); + if (sourceMediaType != null) + sj.add("\"sourceMediaType\": \"" + sourceMediaType + '"'); + if (targetMediaType != null) + sj.add("\"targetMediaType\": \"" + targetMediaType + '"'); return sj.toString(); } @@ -91,19 +95,19 @@ public class Types public T build() { - return (T)t; + return (T) t; } public B withSourceMediaType(final String sourceMediaType) { t.sourceMediaType = sourceMediaType; - return (B)this; + return (B) this; } public B withTargetMediaType(final String targetMediaType) { t.targetMediaType = targetMediaType; - return (B)this; + return (B) this; } } } diff --git a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReader.java b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReader.java index ee52385a..d42c6fc0 100644 --- a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReader.java +++ b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReader.java @@ -21,10 +21,10 @@ */ package org.alfresco.transform.config.reader; -import org.alfresco.transform.config.TransformConfig; - import java.io.IOException; +import org.alfresco.transform.config.TransformConfig; + public interface TransformConfigReader { TransformConfig load() throws IOException; diff --git a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderFactory.java b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderFactory.java index b7cd31fb..f24b080c 100644 --- a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderFactory.java +++ b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderFactory.java @@ -28,8 +28,7 @@ import org.springframework.core.io.Resource; public class TransformConfigReaderFactory { private TransformConfigReaderFactory() - { - } + {} public static TransformConfigReader create(final Resource resource) { @@ -44,7 +43,7 @@ public class TransformConfigReaderFactory { case "properties": throw new UnsupportedOperationException(".properties configuration files are no longer " + - "supported: " + fileName); + "supported: " + fileName); case "yaml": case "yml": return new TransformConfigReaderYaml(resource); diff --git a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderJson.java b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderJson.java index ad7daed0..4f7d7763 100644 --- a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderJson.java +++ b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderJson.java @@ -1,47 +1,47 @@ -/* - * #%L - * Alfresco Transform Model - * %% - * Copyright (C) 2015 - 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.config.reader; - -import java.io.IOException; - -import org.alfresco.transform.config.TransformConfig; -import org.springframework.core.io.Resource; - -import com.fasterxml.jackson.databind.ObjectMapper; - -public class TransformConfigReaderJson implements TransformConfigReader -{ - private static final ObjectMapper MAPPER= new ObjectMapper(); - - private final Resource resource; - - TransformConfigReaderJson(final Resource resource) - { - this.resource = resource; - } - - @Override - public TransformConfig load() throws IOException - { - return MAPPER.readValue(resource.getInputStream(), TransformConfig.class); - } -} \ No newline at end of file +/* + * #%L + * Alfresco Transform Model + * %% + * Copyright (C) 2015 - 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.config.reader; + +import java.io.IOException; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.core.io.Resource; + +import org.alfresco.transform.config.TransformConfig; + +public class TransformConfigReaderJson implements TransformConfigReader +{ + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private final Resource resource; + + TransformConfigReaderJson(final Resource resource) + { + this.resource = resource; + } + + @Override + public TransformConfig load() throws IOException + { + return MAPPER.readValue(resource.getInputStream(), TransformConfig.class); + } +} diff --git a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderYaml.java b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderYaml.java index 093e91c5..80ed69af 100644 --- a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderYaml.java +++ b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigReaderYaml.java @@ -23,11 +23,11 @@ package org.alfresco.transform.config.reader; import java.io.IOException; -import org.alfresco.transform.config.TransformConfig; -import org.springframework.core.io.Resource; - import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import org.springframework.core.io.Resource; + +import org.alfresco.transform.config.TransformConfig; public class TransformConfigReaderYaml implements TransformConfigReader { diff --git a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigResourceReader.java b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigResourceReader.java index f103e9b9..0fb34383 100644 --- a/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigResourceReader.java +++ b/model/src/main/java/org/alfresco/transform/config/reader/TransformConfigResourceReader.java @@ -26,27 +26,30 @@ */ package org.alfresco.transform.config.reader; -import org.alfresco.transform.exceptions.TransformException; -import org.alfresco.transform.config.TransformConfig; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +import java.io.IOException; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; -import java.io.IOException; - -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.exceptions.TransformException; /** * Reads {@link TransformConfig} from json or yaml files. Typically used by {@code TransformEngine.getTransformConfig()}. + * *
    - *     transformConfigResourceReader.read("classpath:pdfrenderer_engine_config.json");
    + * transformConfigResourceReader.read("classpath:pdfrenderer_engine_config.json");
      * 
    */ @Component public class TransformConfigResourceReader { - @Autowired ResourceLoader resourceLoader; + @Autowired + ResourceLoader resourceLoader; public TransformConfig read(String resourcePath) { diff --git a/model/src/main/java/org/alfresco/transform/messages/TransformRequestValidator.java b/model/src/main/java/org/alfresco/transform/messages/TransformRequestValidator.java index f0bbf048..fa87d97d 100644 --- a/model/src/main/java/org/alfresco/transform/messages/TransformRequestValidator.java +++ b/model/src/main/java/org/alfresco/transform/messages/TransformRequestValidator.java @@ -21,10 +21,11 @@ */ package org.alfresco.transform.messages; -import org.alfresco.transform.client.model.TransformRequest; import org.springframework.validation.Errors; import org.springframework.validation.Validator; +import org.alfresco.transform.client.model.TransformRequest; + /** * TransformRequestValidator *

    @@ -58,36 +59,36 @@ public class TransformRequestValidator implements Validator if (sourceSize == null || sourceSize <= 0) { errors.rejectValue("sourceSize", null, - "sourceSize cannot be null or have its value smaller than 0"); + "sourceSize cannot be null or have its value smaller than 0"); } String sourceMediaType = request.getSourceMediaType(); if (sourceMediaType == null || sourceMediaType.isEmpty()) { errors.rejectValue("sourceMediaType", null, - "sourceMediaType cannot be null or empty"); + "sourceMediaType cannot be null or empty"); } String targetMediaType = request.getTargetMediaType(); if (targetMediaType == null || targetMediaType.isEmpty()) { errors.rejectValue("targetMediaType", null, - "targetMediaType cannot be null or empty"); + "targetMediaType cannot be null or empty"); } String targetExtension = request.getTargetExtension(); if (targetExtension == null || targetExtension.isEmpty()) { errors.rejectValue("targetExtension", null, - "targetExtension cannot be null or empty"); + "targetExtension cannot be null or empty"); } String clientData = request.getClientData(); if (clientData == null || clientData.isEmpty()) { errors.rejectValue("clientData", String.valueOf(request.getSchema()), - "clientData cannot be null or empty"); + "clientData cannot be null or empty"); } if (request.getSchema() < 0) { errors.rejectValue("schema", String.valueOf(request.getSchema()), - "schema cannot be less than 0"); + "schema cannot be less than 0"); } } } diff --git a/model/src/main/java/org/alfresco/transform/messages/TransformStack.java b/model/src/main/java/org/alfresco/transform/messages/TransformStack.java index eff05cff..b2b40b0e 100644 --- a/model/src/main/java/org/alfresco/transform/messages/TransformStack.java +++ b/model/src/main/java/org/alfresco/transform/messages/TransformStack.java @@ -21,50 +21,40 @@ */ package org.alfresco.transform.messages; -import org.alfresco.transform.client.model.InternalContext; -import org.alfresco.transform.client.model.MultiStep; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.common.TransformerDebug; -import org.apache.commons.lang3.StringUtils; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringJoiner; +import org.apache.commons.lang3.StringUtils; + +import org.alfresco.transform.client.model.InternalContext; +import org.alfresco.transform.client.model.MultiStep; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.common.TransformerDebug; + /** - * Represents the current state of a top level transform request in terms of its current nested call stack, which is - * the current transform step being performed and what other transform steps are still to be executed in the current - * level. This information is encoded in the {@link MultiStep} structure of the - * internal context passed between T-Router and T-Engines. Ideally we would have changed the structure, - * but for backward compatibility we are using the existing structure, which allows T-Engines that were developed - * previously to be used unchanged.


    + * Represents the current state of a top level transform request in terms of its current nested call stack, which is the current transform step being performed and what other transform steps are still to be executed in the current level. This information is encoded in the {@link MultiStep} structure of the internal context passed between T-Router and T-Engines. Ideally we would have changed the structure, but for backward compatibility we are using the existing structure, which allows T-Engines that were developed previously to be used unchanged. + *

    + *
    * - * Originally the T-Router only allowed pipeline and single step transforms, so it was possible to represent them as a - * flat list. However the original design was of a nested structure. Pipelines are just one example, with failover - * transforms being the other. To bring the T-Router up to the same level of functionality as the Repository, it too - * needs to support nested transforms.


    + * Originally the T-Router only allowed pipeline and single step transforms, so it was possible to represent them as a flat list. However the original design was of a nested structure. Pipelines are just one example, with failover transforms being the other. To bring the T-Router up to the same level of functionality as the Repository, it too needs to support nested transforms. + *

    + *
    * - *

  • @{code transformsToBeDone[0]} holds the original Transformer Request Options as a list of key values pairs. - * Needed so that we don't lose values as we walk down the individual transform steps
  • - *
  • @{code transformsToBeDone[1]} holds the original source reference so that we don't delete the original source - * until the whole transform is known to be successful, so that a queue entry may be retried on failure.
  • + *
  • @{code transformsToBeDone[0]} holds the original Transformer Request Options as a list of key values pairs. Needed so that we don't lose values as we walk down the individual transform steps
  • + *
  • @{code transformsToBeDone[1]} holds the original source reference so that we don't delete the original source until the whole transform is known to be successful, so that a queue entry may be retried on failure.
  • *
  • @{code transformsToBeDone[2]} holds information about the top level transform
  • *
  • @{code transformsToBeDone[size()-1]} holds information about the most nested transform being processed
  • - *
  • Each level contains a list of step transforms. Just one for a singe step transform or a list for pipeline and - * failover transforms
  • - *
  • When a step is processed it will result in the creation of another level if it is a pipeline or failover - * transform
  • + *
  • Each level contains a list of step transforms. Just one for a singe step transform or a list for pipeline and failover transforms
  • + *
  • When a step is processed it will result in the creation of another level if it is a pipeline or failover transform
  • *
  • As steps are completed, they are removed
  • *
  • When there are no steps left in a level the level is removed
  • * - * Each level is represented by a String with a pipeline or failover flag @{code 'P'|'F'} followed by a step counter - * and start time used in debug, a retry count and a sequence of transform steps. Each step is made up of three parts: - * @{code|| . All fields are separated by a @code{'\u23D0'} character. - * The last step in the sequence is the current transform being performed. The top level transform is a pipeline of - * one step. Although the source and target mimetypes are always the same for failover transforms, they use the same - * structure. + * Each level is represented by a String with a pipeline or failover flag @{code 'P'|'F'} followed by a step counter and start time used in debug, a retry count and a sequence of transform steps. Each step is made up of three parts: + * + * @{code|| . All fields are separated by a @code{'\u23D0'} character. The last step in the sequence is the current transform being performed. The top level transform is a pipeline of one step. Although the source and target mimetypes are always the same for failover transforms, they use the same structure. */ public class TransformStack { @@ -84,7 +74,7 @@ public class TransformStack private static final int RETRY_INDEX = 3; private static final int FIELDS_IN_HEADER = 4; // flag | counter | retry - private static final int FIELDS_PER_STEP = 3; // name | source | target + private static final int FIELDS_PER_STEP = 3; // name | source | target public static LevelBuilder levelBuilder(String flag) { @@ -116,7 +106,7 @@ public class TransformStack stringJoiner.add("1"); stringJoiner.add("0"); stringJoiner.add("0"); - for (int i=reverseOrderStepElements.size()-1; i>=0; i--) + for (int i = reverseOrderStepElements.size() - 1; i >= 0; i--) { stringJoiner.add(reverseOrderStepElements.get(i)); } @@ -150,11 +140,11 @@ public class TransformStack } public static void setInitialTransformRequestOptions(InternalContext internalContext, - Map transformRequestOptions) + Map transformRequestOptions) { init(internalContext); StringJoiner sj = new StringJoiner(SEPARATOR); - transformRequestOptions.forEach((key,value)-> sj.add(key).add(value)); + transformRequestOptions.forEach((key, value) -> sj.add(key).add(value)); levels(internalContext).set(OPTIONS_LEVEL, sj.toString()); } @@ -170,13 +160,13 @@ public class TransformStack // To avoid the case where split() discards the last value, when it is a zero length string, we add and remove // a space. None of the keys or value may be null. - String[] split = (level(internalContext, OPTIONS_LEVEL)+' ').split(SEPARATOR_REGEX); + String[] split = (level(internalContext, OPTIONS_LEVEL) + ' ').split(SEPARATOR_REGEX); String lastValue = split[split.length - 1]; - split[split.length-1] = lastValue.substring(0, lastValue.length()-1); + split[split.length - 1] = lastValue.substring(0, lastValue.length() - 1); - for (int i = split.length-2; i >= 0; i-=2) + for (int i = split.length - 2; i >= 0; i -= 2) { - transformRequestOptions.put(split[i], split[i+1]); + transformRequestOptions.put(split[i], split[i + 1]); } return transformRequestOptions; } @@ -198,7 +188,7 @@ public class TransformStack private static void init(InternalContext internalContext) { - while(levels(internalContext).size() < TOP_STACK_LEVEL) + while (levels(internalContext).size() < TOP_STACK_LEVEL) { levels(internalContext).add(null); } @@ -220,9 +210,9 @@ public class TransformStack { int levelsLeft = levels(internalContext).size() - TOP_STACK_LEVEL; - return levelsLeft <= 0 || // there has been an error, so we have lost the stack + return levelsLeft <= 0 || // there has been an error, so we have lost the stack levelsLeft == 1 && // on top level wrapper level - isTransformLevelFinished(internalContext); // the one step has been processed (removed) + isTransformLevelFinished(internalContext); // the one step has been processed (removed) } @@ -238,7 +228,7 @@ public class TransformStack public static void incrementReference(InternalContext internalContext) { - setHeaderField(internalContext, REFERENCE_INDEX, Integer.toString(getReferenceCounter(internalContext)+1)); + setHeaderField(internalContext, REFERENCE_INDEX, Integer.toString(getReferenceCounter(internalContext) + 1)); } public static void resetAttemptedRetries(InternalContext internalContext) @@ -253,7 +243,7 @@ public class TransformStack public static void incrementAttemptedRetries(InternalContext internalContext) { - setHeaderField(internalContext, RETRY_INDEX,getAttemptedRetries(internalContext)+1); + setHeaderField(internalContext, RETRY_INDEX, getAttemptedRetries(internalContext) + 1); } private static void setHeaderField(InternalContext internalContext, int index, long value) @@ -265,17 +255,17 @@ public class TransformStack { List levels = levels(internalContext); int size = levels.size(); - String level = levels.get(size-1); + String level = levels.get(size - 1); int j = indexOfField(level, index); - int k = level.indexOf(SEPARATOR, j+1); - levels.set(size-1, level.substring(0, j) + value + level.substring(k)); + int k = level.indexOf(SEPARATOR, j + 1); + levels.set(size - 1, level.substring(0, j) + value + level.substring(k)); } public static String getReference(InternalContext internalContext) { StringJoiner ref = new StringJoiner("."); List levels = levels(internalContext); - for (int i=TOP_STACK_LEVEL; i levels = levels(internalContext); - levels.remove(levels.size()-1); + levels.remove(levels.size() - 1); } public static void removeRemainingTransformLevels(TransformReply reply, TransformerDebug transformerDebug) @@ -370,7 +360,7 @@ public class TransformStack private static int getStepCount(InternalContext internalContext) { - return (StringUtils.countMatches(currentLevel(internalContext), SEPARATOR)+1-FIELDS_IN_HEADER)/FIELDS_PER_STEP; + return (StringUtils.countMatches(currentLevel(internalContext), SEPARATOR) + 1 - FIELDS_IN_HEADER) / FIELDS_PER_STEP; } public static void removeSuccessfulStep(TransformReply reply, TransformerDebug transformerDebug) @@ -384,7 +374,7 @@ public class TransformStack } private static void removeFinishedSteps(TransformReply reply, boolean successful, - TransformerDebug transformerDebug) + TransformerDebug transformerDebug) { TransformStack.removeStep(reply, successful, transformerDebug); @@ -414,8 +404,8 @@ public class TransformStack // remove one step as it was a successful pipeline step or an unsuccessful failover step List levels = levels(internalContext); int size = levels.size(); - String level = levels.get(size-1); - levels.set(size-1, level.substring(0, + String level = levels.get(size - 1); + levels.set(size - 1, level.substring(0, (successfulFailoverStep || unsuccessfulPipelineStep ? indexOfLastStep(level) : indexOfNextStep(level)) - 1)); if (!isTransformLevelFinished(internalContext)) @@ -426,12 +416,12 @@ public class TransformStack private static int indexOfNextStep(String level) { - int j = level.length()-1; + int j = level.length() - 1; for (int i = FIELDS_PER_STEP; i >= 1 && j > 0; i--) { - j = level.lastIndexOf(SEPARATOR, j-1); + j = level.lastIndexOf(SEPARATOR, j - 1); } - return j+1; + return j + 1; } private static int indexOfLastStep(String level) @@ -444,23 +434,23 @@ public class TransformStack int j = 0; for (int i = n; i >= 1; i--) { - j = level.indexOf(SEPARATOR, j+1); + j = level.indexOf(SEPARATOR, j + 1); } - return j+1; + return j + 1; } public static String checkStructure(InternalContext internalContext, String type) { // A null value will have been replaced with an empty array, so no need to check for that. String errorMessage = levels(internalContext).size() < (TOP_STACK_LEVEL + 1) - ? type+" InternalContext did not have the Stack set" + ? type + " InternalContext did not have the Stack set" : !validTransformOptions(internalContext) - ? type+" InternalContext did not have the TransformOptions set correctly" - : levels(internalContext).size() == 1 - ? type+" InternalContext levels were not set" - : !validLevels(levels(internalContext)) - ? type+" InternalContext did not have levels set correctly" - : null; + ? type + " InternalContext did not have the TransformOptions set correctly" + : levels(internalContext).size() == 1 + ? type + " InternalContext levels were not set" + : !validLevels(levels(internalContext)) + ? type + " InternalContext did not have levels set correctly" + : null; return errorMessage; } @@ -476,11 +466,11 @@ public class TransformStack return true; } String[] split = keysAndValues.split(SEPARATOR_REGEX); - if (split.length%2 != 0) + if (split.length % 2 != 0) { return false; } - for (int i = split.length-2; i >= 0; i-=2) + for (int i = split.length - 2; i >= 0; i -= 2) { if (split[i].isEmpty()) { @@ -492,7 +482,7 @@ public class TransformStack private static boolean validLevels(List levels) { - for (int i=levels.size()-1; i >=TOP_STACK_LEVEL; i--) + for (int i = levels.size() - 1; i >= TOP_STACK_LEVEL; i--) { String level = levels.get(i); if (!validLevel(level)) @@ -510,18 +500,19 @@ public class TransformStack return false; } String[] split = level.split(SEPARATOR_REGEX); - if (split.length < FIELDS_IN_HEADER+FIELDS_PER_STEP || // must be at least 1 step - (split.length-FIELDS_IN_HEADER)%FIELDS_PER_STEP != 0 || - (!PIPELINE_FLAG.equals(split[FLAG_INDEX]) && - !FAILOVER_FLAG.equals(split[FLAG_INDEX])) || - !aValidReference(split[REFERENCE_INDEX]) || - !aPositiveLong(split[START_INDEX]) || - !aPositiveInt(split[RETRY_INDEX])) + if (split.length < FIELDS_IN_HEADER + FIELDS_PER_STEP || // must be at least 1 step + (split.length - FIELDS_IN_HEADER) % FIELDS_PER_STEP != 0 || + (!PIPELINE_FLAG.equals(split[FLAG_INDEX]) && + !FAILOVER_FLAG.equals(split[FLAG_INDEX])) + || + !aValidReference(split[REFERENCE_INDEX]) || + !aPositiveLong(split[START_INDEX]) || + !aPositiveInt(split[RETRY_INDEX])) { return false; } - for (int i=split.length-1; i>=FIELDS_IN_HEADER; i--) + for (int i = split.length - 1; i >= FIELDS_IN_HEADER; i--) { if (split[i].isBlank()) { diff --git a/model/src/main/java/org/alfresco/transform/registry/AbstractTransformRegistry.java b/model/src/main/java/org/alfresco/transform/registry/AbstractTransformRegistry.java index 7a60d94b..a17ec015 100644 --- a/model/src/main/java/org/alfresco/transform/registry/AbstractTransformRegistry.java +++ b/model/src/main/java/org/alfresco/transform/registry/AbstractTransformRegistry.java @@ -21,35 +21,35 @@ */ package org.alfresco.transform.registry; -import org.alfresco.transform.config.CoreFunction; -import org.alfresco.transform.config.TransformOption; -import org.alfresco.transform.config.Transformer; +import static org.alfresco.transform.registry.TransformRegistryHelper.lookupTransformOptions; +import static org.alfresco.transform.registry.TransformRegistryHelper.retrieveTransformListBySize; import java.util.List; import java.util.Map; import java.util.Set; -import static org.alfresco.transform.registry.TransformRegistryHelper.lookupTransformOptions; -import static org.alfresco.transform.registry.TransformRegistryHelper.retrieveTransformListBySize; +import org.alfresco.transform.config.CoreFunction; +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.Transformer; /** - * Used to work out if a transformation is supported. Sub classes should implement {@link #getData()} to return an - * instance of the {@link TransformCache} class. This allows sub classes to periodically replace the registry's data with newer - * values. They may also extend the Data class to include extra fields and methods. + * Used to work out if a transformation is supported. Sub classes should implement {@link #getData()} to return an instance of the {@link TransformCache} class. This allows sub classes to periodically replace the registry's data with newer values. They may also extend the Data class to include extra fields and methods. */ public abstract class AbstractTransformRegistry implements TransformServiceRegistry { /** * Logs an error message if there is an error in the configuration. * - * @param msg to be logged. + * @param msg + * to be logged. */ protected abstract void logError(String msg); /** * Logs a warning message if there is a problem in the configuration. * - * @param msg to be logged. + * @param msg + * to be logged. */ protected void logWarn(String msg) { @@ -64,70 +64,71 @@ public abstract class AbstractTransformRegistry implements TransformServiceRegis public abstract TransformCache getData(); /** - * Registers a single transformer. This is an internal method called by - * {@link CombinedTransformConfig#registerCombinedTransformers(AbstractTransformRegistry)}. + * Registers a single transformer. This is an internal method called by {@link CombinedTransformConfig#registerCombinedTransformers(AbstractTransformRegistry)}. * - * @param transformer to be registered - * @param transformOptions all the transform options - * @param baseUrl where the config was be read from. Only needed when it is remote. Is null when local. - * Does not need to be a URL. May just be a name. - * @param readFrom debug message for log messages, indicating what type of config was read. + * @param transformer + * to be registered + * @param transformOptions + * all the transform options + * @param baseUrl + * where the config was be read from. Only needed when it is remote. Is null when local. Does not need to be a URL. May just be a name. + * @param readFrom + * debug message for log messages, indicating what type of config was read. */ protected void register(final Transformer transformer, - final Map> transformOptions, final String baseUrl, - final String readFrom) + final Map> transformOptions, final String baseUrl, + final String readFrom) { getData().incrementTransformerCount(); transformer - .getSupportedSourceAndTargetList() - .forEach(e -> getData().appendTransform(e.getSourceMediaType(), e.getTargetMediaType(), - new SupportedTransform( - transformer.getTransformerName(), - lookupTransformOptions(transformer.getTransformOptions(), transformOptions, - readFrom, this::logError), - e.getMaxSourceSizeBytes(), - e.getPriority()), - transformer.getTransformerName(), - transformer.getCoreVersion())); + .getSupportedSourceAndTargetList() + .forEach(e -> getData().appendTransform(e.getSourceMediaType(), e.getTargetMediaType(), + new SupportedTransform( + transformer.getTransformerName(), + lookupTransformOptions(transformer.getTransformOptions(), transformOptions, + readFrom, this::logError), + e.getMaxSourceSizeBytes(), + e.getPriority()), + transformer.getTransformerName(), + transformer.getCoreVersion())); } /** - * Works out the name of the transformer (might not map to an actual transformer) that will be used to transform - * content of a given source mimetype and size into a target mimetype given a list of actual transform option names - * and values (Strings) plus the data contained in the Transform objects registered with this class. + * Works out the name of the transformer (might not map to an actual transformer) that will be used to transform content of a given source mimetype and size into a target mimetype given a list of actual transform option names and values (Strings) plus the data contained in the Transform objects registered with this class. * - * @param sourceMimetype the mimetype of the source content - * @param sourceSizeInBytes the size in bytes of the source content. Ignored if negative. - * @param targetMimetype the mimetype of the target - * @param actualOptions the actual name value pairs available that could be passed to the Transform Service. - * @param renditionName (optional) name for the set of options and target mimetype. If supplied is used to cache - * results to avoid having to work out if a given transformation is supported a second time. - * The sourceMimetype and sourceSizeInBytes may still change. In the case of ACS this is the - * rendition name. + * @param sourceMimetype + * the mimetype of the source content + * @param sourceSizeInBytes + * the size in bytes of the source content. Ignored if negative. + * @param targetMimetype + * the mimetype of the target + * @param actualOptions + * the actual name value pairs available that could be passed to the Transform Service. + * @param renditionName + * (optional) name for the set of options and target mimetype. If supplied is used to cache results to avoid having to work out if a given transformation is supported a second time. The sourceMimetype and sourceSizeInBytes may still change. In the case of ACS this is the rendition name. */ @Override public String findTransformerName(final String sourceMimetype, final long sourceSizeInBytes, - final String targetMimetype, final Map actualOptions, - final String renditionName) + final String targetMimetype, final Map actualOptions, + final String renditionName) { return retrieveTransformListBySize(getData(), sourceMimetype, targetMimetype, actualOptions, - renditionName) - .stream() - .filter(t -> t.getMaxSourceSizeBytes() == -1 || - t.getMaxSourceSizeBytes() >= sourceSizeInBytes) - .findFirst() - .map(SupportedTransform::getName) - .orElse(null); + renditionName) + .stream() + .filter(t -> t.getMaxSourceSizeBytes() == -1 || + t.getMaxSourceSizeBytes() >= sourceSizeInBytes) + .findFirst() + .map(SupportedTransform::getName) + .orElse(null); } @Override public long findMaxSize(final String sourceMimetype, final String targetMimetype, - final Map actualOptions, final String renditionName) + final Map actualOptions, final String renditionName) { final List supportedTransforms = retrieveTransformListBySize(getData(), - sourceMimetype, targetMimetype, actualOptions, renditionName); - return supportedTransforms.isEmpty() ? 0 : - supportedTransforms.get(supportedTransforms.size() - 1).getMaxSourceSizeBytes(); + sourceMimetype, targetMimetype, actualOptions, renditionName); + return supportedTransforms.isEmpty() ? 0 : supportedTransforms.get(supportedTransforms.size() - 1).getMaxSourceSizeBytes(); } @Override diff --git a/model/src/main/java/org/alfresco/transform/registry/CombinedTransformConfig.java b/model/src/main/java/org/alfresco/transform/registry/CombinedTransformConfig.java index 858a948c..56128bcb 100644 --- a/model/src/main/java/org/alfresco/transform/registry/CombinedTransformConfig.java +++ b/model/src/main/java/org/alfresco/transform/registry/CombinedTransformConfig.java @@ -21,6 +21,20 @@ */ package org.alfresco.transform.registry; +import static java.util.stream.Collectors.toSet; + +import static org.alfresco.transform.config.CoreVersionDecorator.setCoreVersionOnMultiStepTransformers; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringJoiner; +import java.util.function.Function; +import java.util.stream.Collectors; + import org.alfresco.transform.config.AddSupported; import org.alfresco.transform.config.OverrideSupported; import org.alfresco.transform.config.RemoveSupported; @@ -33,30 +47,16 @@ import org.alfresco.transform.config.Transformer; import org.alfresco.transform.config.TransformerAndTypes; import org.alfresco.transform.config.Types; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.StringJoiner; -import java.util.TreeSet; -import java.util.function.Function; -import java.util.stream.Collectors; - -import static java.util.stream.Collectors.toSet; -import static org.alfresco.transform.config.CoreVersionDecorator.setCoreVersionOnMultiStepTransformers; - /** - * This class combines one or more T-Engine config and local files and registers them as if they were all in one file. - * Transform options are shared between all sources.


    + * This class combines one or more T-Engine config and local files and registers them as if they were all in one file. Transform options are shared between all sources. + *

    + *
    * - * The caller should make calls to {@link #addTransformConfig(TransformConfig, String, String, AbstractTransformRegistry)} - * followed by calls to {@link #combineTransformerConfig(AbstractTransformRegistry)} and then - * {@link #registerCombinedTransformers(AbstractTransformRegistry)}.


    + * The caller should make calls to {@link #addTransformConfig(TransformConfig, String, String, AbstractTransformRegistry)} followed by calls to {@link #combineTransformerConfig(AbstractTransformRegistry)} and then {@link #registerCombinedTransformers(AbstractTransformRegistry)}. + *

    + *
    * - * The helper method {@link #combineAndRegister(TransformConfig, String, String, AbstractTransformRegistry)} may be used - * when there is only one config. + * The helper method {@link #combineAndRegister(TransformConfig, String, String, AbstractTransformRegistry)} may be used when there is only one config. * * @author adavis */ @@ -73,7 +73,7 @@ public class CombinedTransformConfig private final Defaults defaults = new Defaults(); public static void combineAndRegister(TransformConfig transformConfig, String readFrom, String baseUrl, - AbstractTransformRegistry registry) + AbstractTransformRegistry registry) { CombinedTransformConfig combinedTransformConfig = new CombinedTransformConfig(); combinedTransformConfig.addTransformConfig(transformConfig, readFrom, baseUrl, registry); @@ -94,7 +94,7 @@ public class CombinedTransformConfig } public void addTransformConfig(TransformConfig transformConfig, String readFrom, String baseUrl, - AbstractTransformRegistry registry) + AbstractTransformRegistry registry) { removeTransformers(transformConfig.getRemoveTransformers(), readFrom, registry); supportedDefaults(transformConfig.getSupportedDefaults(), readFrom, registry); @@ -113,8 +113,7 @@ public class CombinedTransformConfig if (!removeTransformersSet.isEmpty()) { Set leftOver = new HashSet<>(removeTransformersSet); - combinedTransformers.removeIf(combinedTransformer -> - { + combinedTransformers.removeIf(combinedTransformer -> { String transformerName = combinedTransformer.get().getTransformerName(); if (removeTransformersSet.contains(transformerName)) { @@ -124,7 +123,7 @@ public class CombinedTransformConfig return false; }); - Set quotedLeftOver = leftOver.stream().map(transformerName -> "\""+transformerName+'"').collect(toSet()); + Set quotedLeftOver = leftOver.stream().map(transformerName -> "\"" + transformerName + '"').collect(toSet()); logWarn(quotedLeftOver, readFrom, registry, "removeTransformers"); } } @@ -136,9 +135,8 @@ public class CombinedTransformConfig Set leftOver = new HashSet<>(supportedDefaults); supportedDefaults.stream() .filter(supportedDefault -> supportedDefault.getMaxSourceSizeBytes() != null || - supportedDefault.getPriority() != null) - .forEach(supportedDefault -> - { + supportedDefault.getPriority() != null) + .forEach(supportedDefault -> { defaults.add(supportedDefault); leftOver.remove(supportedDefault); }); @@ -156,8 +154,8 @@ public class CombinedTransformConfig Set leftOver = new HashSet<>(tSet); tSet.stream() .filter(t -> t.getTransformerName() != null && - t.getSourceMediaType() != null && - t.getTargetMediaType() != null) + t.getSourceMediaType() != null && + t.getTargetMediaType() != null) .forEach(t -> process.process(leftOver, t)); logWarn(leftOver, readFrom, registry, elementName); @@ -183,14 +181,11 @@ public class CombinedTransformConfig private void removeSupported(Set removeSupportedSet, String readFrom, AbstractTransformRegistry registry) { processSupported(removeSupportedSet, readFrom, registry, "removeSupported", - (leftOver, removeSupported) -> - combinedTransformers.stream() + (leftOver, removeSupported) -> combinedTransformers.stream() .map(Origin::get) - .forEach(transformer -> - { + .forEach(transformer -> { if (transformer.getTransformerName().equals(removeSupported.getTransformerName()) && - transformer.getSupportedSourceAndTargetList().removeIf(supported -> - supported.getSourceMediaType().equals(removeSupported.getSourceMediaType()) && + transformer.getSupportedSourceAndTargetList().removeIf(supported -> supported.getSourceMediaType().equals(removeSupported.getSourceMediaType()) && supported.getTargetMediaType().equals(removeSupported.getTargetMediaType()))) { leftOver.remove(removeSupported); @@ -201,61 +196,52 @@ public class CombinedTransformConfig private void addSupported(Set addSupportedSet, String readFrom, AbstractTransformRegistry registry) { processSupported(addSupportedSet, readFrom, registry, "addSupported", - (leftOver, addSupported) -> - combinedTransformers.stream() - .map(Origin::get) - .filter(transformer -> transformer.getTransformerName().equals(addSupported.getTransformerName())) - .forEach(transformerWithName -> - { - Set supportedSourceAndTargetList = - transformerWithName.getSupportedSourceAndTargetList(); - SupportedSourceAndTarget existingSupported = getExistingSupported( - supportedSourceAndTargetList, - addSupported.getSourceMediaType(), addSupported.getTargetMediaType()); - if (existingSupported == null) - { - SupportedSourceAndTarget newSupportedSourceAndTarget = SupportedSourceAndTarget.builder() - .withSourceMediaType(addSupported.getSourceMediaType()) - .withTargetMediaType(addSupported.getTargetMediaType()) - .withMaxSourceSizeBytes(addSupported.getMaxSourceSizeBytes()) - .withPriority(addSupported.getPriority()) - .build(); - supportedSourceAndTargetList.add(newSupportedSourceAndTarget); - leftOver.remove(addSupported); - }})); + (leftOver, addSupported) -> combinedTransformers.stream() + .map(Origin::get) + .filter(transformer -> transformer.getTransformerName().equals(addSupported.getTransformerName())) + .forEach(transformerWithName -> { + Set supportedSourceAndTargetList = transformerWithName.getSupportedSourceAndTargetList(); + SupportedSourceAndTarget existingSupported = getExistingSupported( + supportedSourceAndTargetList, + addSupported.getSourceMediaType(), addSupported.getTargetMediaType()); + if (existingSupported == null) + { + SupportedSourceAndTarget newSupportedSourceAndTarget = SupportedSourceAndTarget.builder() + .withSourceMediaType(addSupported.getSourceMediaType()) + .withTargetMediaType(addSupported.getTargetMediaType()) + .withMaxSourceSizeBytes(addSupported.getMaxSourceSizeBytes()) + .withPriority(addSupported.getPriority()) + .build(); + supportedSourceAndTargetList.add(newSupportedSourceAndTarget); + leftOver.remove(addSupported); + } + })); } private void overrideSupported(Set overrideSupportedSet, String readFrom, AbstractTransformRegistry registry) { processSupported(overrideSupportedSet, readFrom, registry, "overrideSupported", - (leftOver, overrideSupported) -> - combinedTransformers.stream(). - map(Origin::get). - filter(transformer -> transformer.getTransformerName().equals(overrideSupported.getTransformerName())). - forEach(transformerWithName -> - { - Set supportedSourceAndTargetList = - transformerWithName.getSupportedSourceAndTargetList(); - SupportedSourceAndTarget existingSupported = getExistingSupported( - supportedSourceAndTargetList, - overrideSupported.getSourceMediaType(), overrideSupported.getTargetMediaType()); - if (existingSupported != null) - { - supportedSourceAndTargetList.remove(existingSupported); - existingSupported.setMaxSourceSizeBytes(overrideSupported.getMaxSourceSizeBytes()); - existingSupported.setPriority(overrideSupported.getPriority()); - supportedSourceAndTargetList.add(existingSupported); - leftOver.remove(overrideSupported); - } - })); + (leftOver, overrideSupported) -> combinedTransformers.stream().map(Origin::get).filter(transformer -> transformer.getTransformerName().equals(overrideSupported.getTransformerName())).forEach(transformerWithName -> { + Set supportedSourceAndTargetList = transformerWithName.getSupportedSourceAndTargetList(); + SupportedSourceAndTarget existingSupported = getExistingSupported( + supportedSourceAndTargetList, + overrideSupported.getSourceMediaType(), overrideSupported.getTargetMediaType()); + if (existingSupported != null) + { + supportedSourceAndTargetList.remove(existingSupported); + existingSupported.setMaxSourceSizeBytes(overrideSupported.getMaxSourceSizeBytes()); + existingSupported.setPriority(overrideSupported.getPriority()); + supportedSourceAndTargetList.add(existingSupported); + leftOver.remove(overrideSupported); + } + })); } private SupportedSourceAndTarget getExistingSupported(Set supportedSourceAndTargetList, - String sourceMediaType, String targetMediaType) + String sourceMediaType, String targetMediaType) { - return supportedSourceAndTargetList.stream().filter(supported -> - supported.getSourceMediaType().equals(sourceMediaType) && - supported.getTargetMediaType().equals(targetMediaType)) + return supportedSourceAndTargetList.stream().filter(supported -> supported.getSourceMediaType().equals(sourceMediaType) && + supported.getTargetMediaType().equals(targetMediaType)) .findFirst() .orElse(null); } @@ -273,7 +259,7 @@ public class CombinedTransformConfig public TransformConfig buildTransformConfig() { List transformers = new ArrayList<>(); - combinedTransformers.forEach(ct->transformers.add(ct.get())); + combinedTransformers.forEach(ct -> transformers.add(ct.get())); Set supportedDefaults = defaults.getSupportedDefaults(); return TransformConfig .builder() @@ -285,21 +271,18 @@ public class CombinedTransformConfig public void registerCombinedTransformers(AbstractTransformRegistry registry) { - combinedTransformers.forEach(ct -> - registry.register(ct.get(), combinedTransformOptions, ct.getBaseUrl(), ct.getReadFrom())); + combinedTransformers.forEach(ct -> registry.register(ct.get(), combinedTransformOptions, ct.getBaseUrl(), ct.getReadFrom())); } /** - * Discards transformers that are invalid (e.g. transformers that have both pipeline and failover sections). Calls - * {@link #removeInvalidTransformer(int, List, AbstractTransformRegistry, Origin, Transformer, String, String, - * boolean, boolean)} for each transform, so that individual invalid transforms or overridden - * transforms may be discarded. + * Discards transformers that are invalid (e.g. transformers that have both pipeline and failover sections). Calls {@link #removeInvalidTransformer(int, List, AbstractTransformRegistry, Origin, Transformer, String, String, boolean, boolean)} for each transform, so that individual invalid transforms or overridden transforms may be discarded. * - * @param registry that will hold the transforms. + * @param registry + * that will hold the transforms. */ private void removeInvalidTransformers(AbstractTransformRegistry registry) { - for (int i=0; i> combinedTransformers, - AbstractTransformRegistry registry, - Origin transformAndItsOrigin, Transformer transformer, - String name, String readFrom, boolean isPipeline, boolean isFailover) + AbstractTransformRegistry registry, + Origin transformAndItsOrigin, Transformer transformer, + String name, String readFrom, boolean isPipeline, boolean isFailover) { int indexToRemove = -1; @@ -459,8 +440,7 @@ public class CombinedTransformConfig // so we can talk to its T-Engine String overriddenBaseUrl = overriddenTransformAndItsOrigin.getBaseUrl(); Transformer overriddenTransformTransform = transformAndItsOrigin.get(); - Origin overridingTransform = - new Origin<>(overriddenTransformTransform, overriddenBaseUrl, readFrom); + Origin overridingTransform = new Origin<>(overriddenTransformTransform, overriddenBaseUrl, readFrom); combinedTransformers.set(i, overridingTransform); } indexToRemove = j; @@ -484,7 +464,7 @@ public class CombinedTransformConfig { // Lists are short (< 100) entries and this is not a frequent or time critical step, so walking the list // should be okay. - for (int j = toIndex-1; j >=0; j--) + for (int j = toIndex - 1; j >= 0; j--) { Origin transformAndItsOrigin = combinedTransformers.get(j); Transformer transformer = transformAndItsOrigin.get(); @@ -509,10 +489,11 @@ public class CombinedTransformConfig } /** - * Sort transformers so there are no forward references, if that is possible. - * Logs warning message for those that have missing step transformers and removes them. - * @param registry used to log messages - */ + * Sort transformers so there are no forward references, if that is possible. Logs warning message for those that have missing step transformers and removes them. + * + * @param registry + * used to log messages + */ private void sortTransformers(AbstractTransformRegistry registry) { List> transformers = new ArrayList<>(combinedTransformers.size()); @@ -552,8 +533,7 @@ public class CombinedTransformConfig combinedTransformers.clear(); combinedTransformers.addAll(todo); todo.clear(); - } - while (added && !combinedTransformers.isEmpty()); + } while (added && !combinedTransformers.isEmpty()); transformers.addAll(todo); @@ -604,18 +584,15 @@ public class CombinedTransformConfig } /** - * Applies priority and size defaults. Must be called before {@link #addWildcardSupportedSourceAndTarget(AbstractTransformRegistry)} - * as it uses the priority value. + * Applies priority and size defaults. Must be called before {@link #addWildcardSupportedSourceAndTarget(AbstractTransformRegistry)} as it uses the priority value. */ private void applyDefaults() { combinedTransformers.stream() .map(Origin::get) - .forEach(transformer -> - { + .forEach(transformer -> { transformer.setSupportedSourceAndTargetList( - transformer.getSupportedSourceAndTargetList().stream().map(supportedSourceAndTarget -> - { + transformer.getSupportedSourceAndTargetList().stream().map(supportedSourceAndTarget -> { Integer priority = supportedSourceAndTarget.getPriority(); Long maxSourceSizeBytes = supportedSourceAndTarget.getMaxSourceSizeBytes(); if (defaults.valuesUnset(priority, maxSourceSizeBytes)) @@ -633,26 +610,19 @@ public class CombinedTransformConfig } /** - * When no supported source and target mimetypes have been defined in a failover or pipeline transformer - * this method adds all possible values that make sense. - * - *

  • Failover - all the supported values from the step transformers
  • - *
  • Pipeline - builds up supported source and target values. The list of source types and max sizes will come - * from the initial step transformer that have a target mimetype that matches the first - * intermediate mimetype. We then step through all intermediate transformers checking the next - * intermediate type is supported. When we get to the last step transformer, it provides all the - * target mimetypes based on the previous intermediate mimetype. Any combinations supported by - * the first transformer are excluded.
  • - * - * @param registry used to log messages + * When no supported source and target mimetypes have been defined in a failover or pipeline transformer this method adds all possible values that make sense. + *
  • Failover - all the supported values from the step transformers
  • + *
  • Pipeline - builds up supported source and target values. The list of source types and max sizes will come from the initial step transformer that have a target mimetype that matches the first intermediate mimetype. We then step through all intermediate transformers checking the next intermediate type is supported. When we get to the last step transformer, it provides all the target mimetypes based on the previous intermediate mimetype. Any combinations supported by the first transformer are excluded.
  • + * + * @param registry + * used to log messages */ private void addWildcardSupportedSourceAndTarget(AbstractTransformRegistry registry) { Map transformers = new HashMap<>(); combinedTransformers.forEach(ct -> transformers.put(ct.get().getTransformerName(), ct.get())); - combinedTransformers.forEach(transformAndItsOrigin -> - { + combinedTransformers.forEach(transformAndItsOrigin -> { Transformer transformer = transformAndItsOrigin.get(); // If there are no SupportedSourceAndTarget, then work out all the wildcard combinations. @@ -666,13 +636,12 @@ public class CombinedTransformConfig if (isFailover) { Set supportedSourceAndTargets = failover.stream().flatMap( - name -> transformers.get(name).getSupportedSourceAndTargetList().stream()). - collect(toSet()); + name -> transformers.get(name).getSupportedSourceAndTargetList().stream()).collect(toSet()); // The failover transform might not be picked if the priority is the same as the step transforms // so reduce it here by 1. In future we might want to specify the priority in the json, but for now // it should be okay. - supportedSourceAndTargets.forEach(s->s.setPriority(s.getPriority()-1)); + supportedSourceAndTargets.forEach(s -> s.setPriority(s.getPriority() - 1)); transformer.setSupportedSourceAndTargetList(supportedSourceAndTargets); errorReason = "the step transforms don't support any"; // only used if there are none } @@ -683,7 +652,7 @@ public class CombinedTransformConfig Set firstTransformOptions = null; String firstTransformStepName = null; int numberOfSteps = pipeline.size(); - for (int stepIndex=0; stepIndex stepTrg.equals(s.getTargetMediaType())). - collect(toSet()); + sourceMediaTypesAndMaxSizes = stepTransformer.getSupportedSourceAndTargetList().stream().filter(s -> stepTrg.equals(s.getTargetMediaType())).collect(toSet()); sourceMediaType = stepTrg; firstTransformOptions = stepTransformer.getTransformOptions(); firstTransformStepName = name; @@ -713,7 +680,7 @@ public class CombinedTransformConfig else { final String src = sourceMediaType; - if (stepIndex+1 == numberOfSteps) // if final step + if (stepIndex + 1 == numberOfSteps) // if final step { if (stepTrg != null) { @@ -723,22 +690,12 @@ public class CombinedTransformConfig // Create a cartesian product of sourceMediaType,MaxSourceSize and TargetMediaType where // the source matches the last intermediate. - Set supportedSourceAndTargets = sourceMediaTypesAndMaxSizes.stream(). - flatMap(s -> stepTransformer.getSupportedSourceAndTargetList().stream(). - filter(st -> - { - String targetMimetype = st.getTargetMediaType(); - return st.getSourceMediaType().equals(src) && - !(MIMETYPE_METADATA_EXTRACT.equals(targetMimetype) || - MIMETYPE_METADATA_EMBED.equals(targetMimetype)); - }). - map(Types::getTargetMediaType). - map(trg -> SupportedSourceAndTarget.builder(). - withSourceMediaType(s.getSourceMediaType()). - withMaxSourceSizeBytes(s.getMaxSourceSizeBytes()). - withPriority(s.getPriority()). - withTargetMediaType(trg).build())). - collect(toSet()); + Set supportedSourceAndTargets = sourceMediaTypesAndMaxSizes.stream().flatMap(s -> stepTransformer.getSupportedSourceAndTargetList().stream().filter(st -> { + String targetMimetype = st.getTargetMediaType(); + return st.getSourceMediaType().equals(src) && + !(MIMETYPE_METADATA_EXTRACT.equals(targetMimetype) || + MIMETYPE_METADATA_EMBED.equals(targetMimetype)); + }).map(Types::getTargetMediaType).map(trg -> SupportedSourceAndTarget.builder().withSourceMediaType(s.getSourceMediaType()).withMaxSourceSizeBytes(s.getMaxSourceSizeBytes()).withPriority(s.getPriority()).withTargetMediaType(trg).build())).collect(toSet()); if (supportedSourceAndTargets.isEmpty()) { @@ -773,9 +730,8 @@ public class CombinedTransformConfig } // Check source to target is supported (it normally is) - if (stepTransformer.getSupportedSourceAndTargetList().stream(). - noneMatch(st -> st.getSourceMediaType().equals(src) && - st.getTargetMediaType().equals(stepTrg))) + if (stepTransformer.getSupportedSourceAndTargetList().stream().noneMatch(st -> st.getSourceMediaType().equals(src) && + st.getTargetMediaType().equals(stepTrg))) { errorReason = "the step transformer " + transformerName(stepTransformer.getTransformerName()) + " does not support \"" + @@ -815,22 +771,23 @@ public class CombinedTransformConfig private Set getTransformOptions(Set transformOptionNames) { Set transformOptions = new HashSet<>(); - transformOptionNames.forEach(name->transformOptions.addAll(combinedTransformOptions.get(name))); + transformOptionNames.forEach(name -> transformOptions.addAll(combinedTransformOptions.get(name))); return transformOptions; } /** - * Removes pipeline transformers if the step transformers cannot be chained together via the intermediary types - * to produce the set of claimed source and target mimetypes. - * @param registry used to log messages + * Removes pipeline transformers if the step transformers cannot be chained together via the intermediary types to produce the set of claimed source and target mimetypes. + * + * @param registry + * used to log messages */ private void removePipelinesWithUnsupportedTransforms(AbstractTransformRegistry registry) { Map transformersByName = combinedTransformers.stream() - .map(Origin::get) - .collect(Collectors.toMap(Transformer::getTransformerName, Function.identity())); + .map(Origin::get) + .collect(Collectors.toMap(Transformer::getTransformerName, Function.identity())); - for (int i=0; i unsupported = new ArrayList<>(); transformer.getSupportedSourceAndTargetList() - .forEach(supportedSourceAndTarget -> { - String nextSource = supportedSourceAndTarget.getSourceMediaType(); - for (TransformStep step : pipeline) - { - String source = nextSource; - String target = step.getTargetMediaType() == null - ? supportedSourceAndTarget.getTargetMediaType() - : step.getTargetMediaType(); - - if (transformersByName.get(step.getTransformerName()).getSupportedSourceAndTargetList().stream() - .noneMatch(stepsSupportedSourceAndTarget -> source.equals(stepsSupportedSourceAndTarget.getSourceMediaType()) - && target.equals(stepsSupportedSourceAndTarget.getTargetMediaType()))) + .forEach(supportedSourceAndTarget -> { + String nextSource = supportedSourceAndTarget.getSourceMediaType(); + for (TransformStep step : pipeline) { - unsupported.add(supportedSourceAndTarget.getSourceMediaType()+"->"+supportedSourceAndTarget.getTargetMediaType()); - break; + String source = nextSource; + String target = step.getTargetMediaType() == null + ? supportedSourceAndTarget.getTargetMediaType() + : step.getTargetMediaType(); + + if (transformersByName.get(step.getTransformerName()).getSupportedSourceAndTargetList().stream() + .noneMatch(stepsSupportedSourceAndTarget -> source.equals(stepsSupportedSourceAndTarget.getSourceMediaType()) + && target.equals(stepsSupportedSourceAndTarget.getTargetMediaType()))) + { + unsupported.add(supportedSourceAndTarget.getSourceMediaType() + "->" + supportedSourceAndTarget.getTargetMediaType()); + break; + } + nextSource = target; } - nextSource = target; - } - }); + }); if (!unsupported.isEmpty()) { throw new IllegalStateException("Pipeline transformer " + transformerName(name) + - " steps do not support ("+ + " steps do not support (" + unsupported.stream() - .sorted() - .collect(Collectors.joining(", "))+ + .sorted() + .collect(Collectors.joining(", ")) + + ") so will be ignored"); } } @@ -903,7 +861,7 @@ public class CombinedTransformConfig return combinedTransformers.size(); } - public Map> getTransformerByNameMap() + public Map> getTransformerByNameMap() { return combinedTransformers.stream().collect(Collectors.toMap(origin -> origin.get().getTransformerName(), origin -> origin)); } diff --git a/model/src/main/java/org/alfresco/transform/registry/Defaults.java b/model/src/main/java/org/alfresco/transform/registry/Defaults.java index 4802bf79..1889ba76 100644 --- a/model/src/main/java/org/alfresco/transform/registry/Defaults.java +++ b/model/src/main/java/org/alfresco/transform/registry/Defaults.java @@ -21,19 +21,17 @@ */ package org.alfresco.transform.registry; -import org.alfresco.transform.config.SupportedDefaults; +import static java.util.stream.Collectors.toSet; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.stream.Stream; -import static java.util.stream.Collectors.toSet; +import org.alfresco.transform.config.SupportedDefaults; /** - * Maintains a list of defaults of {@code maxSourceSizeBytes} and {@code priority} keyed on - * {@code transformerName} and {@code sourceMediaType} so that it can provide a lookup when we - * join everything together in {@link CombinedTransformConfig#combineTransformerConfig(AbstractTransformRegistry)}. + * Maintains a list of defaults of {@code maxSourceSizeBytes} and {@code priority} keyed on {@code transformerName} and {@code sourceMediaType} so that it can provide a lookup when we join everything together in {@link CombinedTransformConfig#combineTransformerConfig(AbstractTransformRegistry)}. * * @see SupportedDefaults */ @@ -49,8 +47,7 @@ class Defaults public void add(SupportedDefaults supportedDefault) { - TransformerAndSourceType key = - new TransformerAndSourceType(supportedDefault.getTransformerName(), supportedDefault.getSourceMediaType()); + TransformerAndSourceType key = new TransformerAndSourceType(supportedDefault.getTransformerName(), supportedDefault.getSourceMediaType()); Long maxSourceSizeBytes = supportedDefault.getMaxSourceSizeBytes(); if (maxSourceSizeBytes != null) { @@ -84,7 +81,7 @@ class Defaults } private T getDefault(String transformerName, String sourceMediaType, T supportedSourceAndTargetValue, - Map map) + Map map) { if (supportedSourceAndTargetValue != null) { @@ -96,7 +93,7 @@ class Defaults // 2: source media type default // 3: system wide default TransformerAndSourceType key = new TransformerAndSourceType(transformerName, sourceMediaType); - for (int i=0; ; i++) + for (int i = 0;; i++) { T value = map.get(key); if (value != null) @@ -106,10 +103,16 @@ class Defaults switch (i) { - case 0: - case 2: key.setSourceMediaType(null); break; - case 1: key.setSourceMediaType(sourceMediaType); key.setTransformerName(null); break; - default: throw new IllegalStateException("Should have found an entry with a null, null lookup"); + case 0: + case 2: + key.setSourceMediaType(null); + break; + case 1: + key.setSourceMediaType(sourceMediaType); + key.setTransformerName(null); + break; + default: + throw new IllegalStateException("Should have found an entry with a null, null lookup"); } } } @@ -117,15 +120,13 @@ class Defaults public Set getSupportedDefaults() { return Stream.concat(maxSourceSizeBytesDefaults.keySet().stream(), priorityDefaults.keySet().stream()) - .filter(key -> - { + .filter(key -> { // Discard the entry added by clear() return !SYSTEM_WIDE_KEY.equals(key) || - !DEFAULT_MAX_SOURCE_SIZE_BYTES.equals(maxSourceSizeBytesDefaults.get(key)) || - !DEFAULT_PRIORITY.equals(priorityDefaults.get(key)); + !DEFAULT_MAX_SOURCE_SIZE_BYTES.equals(maxSourceSizeBytesDefaults.get(key)) || + !DEFAULT_PRIORITY.equals(priorityDefaults.get(key)); }) - .map(key -> - { + .map(key -> { Long maxSourceSizeBytes = maxSourceSizeBytesDefaults.get(key); Integer priority = priorityDefaults.get(key); return SupportedDefaults.builder() diff --git a/model/src/main/java/org/alfresco/transform/registry/Origin.java b/model/src/main/java/org/alfresco/transform/registry/Origin.java index 3cf8cc95..b29b2e20 100644 --- a/model/src/main/java/org/alfresco/transform/registry/Origin.java +++ b/model/src/main/java/org/alfresco/transform/registry/Origin.java @@ -22,9 +22,8 @@ package org.alfresco.transform.registry; /** - * Wraps an object so that we know where it was read from. The equals() and hashcode() are that of the wrapped object - * so it is still possible do set operations. - */ + * Wraps an object so that we know where it was read from. The equals() and hashcode() are that of the wrapped object so it is still possible do set operations. + */ public class Origin { final T t; @@ -42,6 +41,7 @@ public class Origin { return t; } + public String getBaseUrl() { return baseUrl; @@ -52,4 +52,3 @@ public class Origin return readFrom; } } - diff --git a/model/src/main/java/org/alfresco/transform/registry/SupportedTransform.java b/model/src/main/java/org/alfresco/transform/registry/SupportedTransform.java index 9cae1f75..4ca399bb 100644 --- a/model/src/main/java/org/alfresco/transform/registry/SupportedTransform.java +++ b/model/src/main/java/org/alfresco/transform/registry/SupportedTransform.java @@ -35,7 +35,7 @@ public class SupportedTransform private final int priority; SupportedTransform(String name, Set transformOptions, - long maxSourceSizeBytes, int priority) + long maxSourceSizeBytes, int priority) { // Logically the top level TransformOptionGroup is required, so that child options are optional or required // based on their own setting. @@ -68,13 +68,15 @@ public class SupportedTransform @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; SupportedTransform that = (SupportedTransform) o; return maxSourceSizeBytes == that.maxSourceSizeBytes && - priority == that.priority && - Objects.equals(transformOptions, that.transformOptions) && - Objects.equals(name, that.name); + priority == that.priority && + Objects.equals(transformOptions, that.transformOptions) && + Objects.equals(name, that.name); } @Override @@ -89,4 +91,3 @@ public class SupportedTransform return name + ':' + maxSourceSizeBytes + ':' + priority; } } - diff --git a/model/src/main/java/org/alfresco/transform/registry/TransformCache.java b/model/src/main/java/org/alfresco/transform/registry/TransformCache.java index 8de73912..7487268c 100644 --- a/model/src/main/java/org/alfresco/transform/registry/TransformCache.java +++ b/model/src/main/java/org/alfresco/transform/registry/TransformCache.java @@ -31,8 +31,7 @@ import java.util.concurrent.ConcurrentHashMap; public class TransformCache { // Looks up supported transform routes given source to target media types. - private final Map>> transforms = - new ConcurrentHashMap<>(); + private final Map>> transforms = new ConcurrentHashMap<>(); // Looks up coreVersion given the transformer name private final Map coreVersions = new ConcurrentHashMap<>(); @@ -41,8 +40,7 @@ public class TransformCache // Looks up a sorted list of transform routes, for a rendition (if the name is supplied) and the source // media type. Unlike a lookup on the transforms map above, processing of the transform options and priorities // will have already been done if cached. - private final Map>> cachedSupportedTransformList = - new ConcurrentHashMap<>(); + private final Map>> cachedSupportedTransformList = new ConcurrentHashMap<>(); protected int transformerCount = 0; protected int transformCount = 0; @@ -53,13 +51,13 @@ public class TransformCache } public void appendTransform(final String sourceMimetype, - final String targetMimetype, final SupportedTransform transform, - String transformerName, String coreVersion) + final String targetMimetype, final SupportedTransform transform, + String transformerName, String coreVersion) { transforms - .computeIfAbsent(sourceMimetype, k -> new ConcurrentHashMap<>()) - .computeIfAbsent(targetMimetype, k -> new ArrayList<>()) - .add(transform); + .computeIfAbsent(sourceMimetype, k -> new ConcurrentHashMap<>()) + .computeIfAbsent(targetMimetype, k -> new ArrayList<>()) + .add(transform); coreVersions.put(transformerName, coreVersion == null ? "" : coreVersion); transformCount++; } @@ -75,27 +73,27 @@ public class TransformCache } public void cache(final String renditionName, final String sourceMimetype, - final List transformListBySize) + final List transformListBySize) { cachedSupportedTransformList - .get(renditionName) - .put(sourceMimetype, transformListBySize); + .get(renditionName) + .put(sourceMimetype, transformListBySize); } public List retrieveCached(final String renditionName, - final String sourceMimetype) + final String sourceMimetype) { return cachedSupportedTransformList - .computeIfAbsent(renditionName, k -> new ConcurrentHashMap<>()) - .get(sourceMimetype); + .computeIfAbsent(renditionName, k -> new ConcurrentHashMap<>()) + .get(sourceMimetype); } @Override public String toString() { return transformerCount == 0 && transformCount == 0 - ? "" - : "(transformers: " + transformerCount + " transforms: " + transformCount + ")"; + ? "" + : "(transformers: " + transformerCount + " transforms: " + transformCount + ")"; } public String getCoreVersion(String transformerName) diff --git a/model/src/main/java/org/alfresco/transform/registry/TransformServiceRegistry.java b/model/src/main/java/org/alfresco/transform/registry/TransformServiceRegistry.java index 9232b157..d0187286 100644 --- a/model/src/main/java/org/alfresco/transform/registry/TransformServiceRegistry.java +++ b/model/src/main/java/org/alfresco/transform/registry/TransformServiceRegistry.java @@ -21,33 +21,33 @@ */ package org.alfresco.transform.registry; -import org.alfresco.transform.config.CoreFunction; - import java.util.Map; +import org.alfresco.transform.config.CoreFunction; + /** * Used by clients work out if a transformation is supported by a Transform Service. */ public interface TransformServiceRegistry { /** - * Works out if the Transform Server should be able to transform content of a given source mimetype and size into a - * target mimetype given a list of actual transform option names and values (Strings) plus the data contained in the - * Transformer objects registered with this class. + * Works out if the Transform Server should be able to transform content of a given source mimetype and size into a target mimetype given a list of actual transform option names and values (Strings) plus the data contained in the Transformer objects registered with this class. * - * @param sourceMimetype the mimetype of the source content - * @param sourceSizeInBytes the size in bytes of the source content. Ignored if negative. - * @param targetMimetype the mimetype of the target - * @param actualOptions the actual name value pairs available that could be passed to the Transform Service. - * @param transformName (optional) name for the set of options and target mimetype. If supplied is used to cache - * results to avoid having to work out if a given transformation is supported a second time. - * The sourceMimetype and sourceSizeInBytes may still change. In the case of ACS this is the - * rendition name. + * @param sourceMimetype + * the mimetype of the source content + * @param sourceSizeInBytes + * the size in bytes of the source content. Ignored if negative. + * @param targetMimetype + * the mimetype of the target + * @param actualOptions + * the actual name value pairs available that could be passed to the Transform Service. + * @param transformName + * (optional) name for the set of options and target mimetype. If supplied is used to cache results to avoid having to work out if a given transformation is supported a second time. The sourceMimetype and sourceSizeInBytes may still change. In the case of ACS this is the rendition name. * @return {@code}true{@code} if it is supported. */ default boolean isSupported(final String sourceMimetype, final long sourceSizeInBytes, - final String targetMimetype, final Map actualOptions, - final String transformName) + final String targetMimetype, final Map actualOptions, + final String transformName) { long maxSize = findMaxSize(sourceMimetype, targetMimetype, actualOptions, transformName); return maxSize != 0 && (maxSize == -1L || maxSize >= sourceSizeInBytes); @@ -56,44 +56,44 @@ public interface TransformServiceRegistry /** * Returns the maximun size (in bytes) of the source content that can be transformed. * - * @param sourceMimetype the mimetype of the source content - * @param targetMimetype the mimetype of the target - * @param actualOptions the actual name value pairs available that could be passed to the Transform Service. - * @param transformName (optional) name for the set of options and target mimetype. If supplied is used to cache - * results to avoid having to work out if a given transformation is supported a second time. - * The sourceMimetype and sourceSizeInBytes may still change. In the case of ACS this is the - * rendition name. - * @return the maximum size (in bytes) of the source content that can be transformed. If {@code -1} there is no - * limit, but if {@code 0} the transform is not supported. + * @param sourceMimetype + * the mimetype of the source content + * @param targetMimetype + * the mimetype of the target + * @param actualOptions + * the actual name value pairs available that could be passed to the Transform Service. + * @param transformName + * (optional) name for the set of options and target mimetype. If supplied is used to cache results to avoid having to work out if a given transformation is supported a second time. The sourceMimetype and sourceSizeInBytes may still change. In the case of ACS this is the rendition name. + * @return the maximum size (in bytes) of the source content that can be transformed. If {@code -1} there is no limit, but if {@code 0} the transform is not supported. */ long findMaxSize(String sourceMimetype, String targetMimetype, Map actualOptions, - String transformName); + String transformName); /** - * Works out the name of the transformer (might not map to an actual transformer) that will be used to transform - * content of a given source mimetype and size into a target mimetype given a list of actual transform option names - * and values (Strings) plus the data contained in the Transformer objects registered with this class. + * Works out the name of the transformer (might not map to an actual transformer) that will be used to transform content of a given source mimetype and size into a target mimetype given a list of actual transform option names and values (Strings) plus the data contained in the Transformer objects registered with this class. * - * @param sourceMimetype the mimetype of the source content - * @param sourceSizeInBytes the size in bytes of the source content. Ignored if negative. - * @param targetMimetype the mimetype of the target - * @param actualOptions the actual name value pairs available that could be passed to the Transform Service. - * @param renditionName (optional) name for the set of options and target mimetype. If supplied is used to cache - * results to avoid having to work out if a given transformation is supported a second time. - * The sourceMimetype and sourceSizeInBytes may still change. In the case of ACS this is the - * rendition name. + * @param sourceMimetype + * the mimetype of the source content + * @param sourceSizeInBytes + * the size in bytes of the source content. Ignored if negative. + * @param targetMimetype + * the mimetype of the target + * @param actualOptions + * the actual name value pairs available that could be passed to the Transform Service. + * @param renditionName + * (optional) name for the set of options and target mimetype. If supplied is used to cache results to avoid having to work out if a given transformation is supported a second time. The sourceMimetype and sourceSizeInBytes may still change. In the case of ACS this is the rendition name. * @return the name of the transformer or {@code}null{@code} if not set or there is no supported transformer. */ String findTransformerName(String sourceMimetype, long sourceSizeInBytes, String targetMimetype, - Map actualOptions, String renditionName); + Map actualOptions, String renditionName); /** - * Returns {@code true} if the {@code function} is supported by the named transformer. Not all transformers are - * able to support all functionality, as newer features may have been introduced into the core t-engine code since - * it was released. Normally used in conjunction with {@link #findTransformerName(String, long, String, Map, String)} - * rather than {@link #isSupported(String, long, String, Map, String)}. - * @param function to be checked. - * @param transformerName name of the transformer. + * Returns {@code true} if the {@code function} is supported by the named transformer. Not all transformers are able to support all functionality, as newer features may have been introduced into the core t-engine code since it was released. Normally used in conjunction with {@link #findTransformerName(String, long, String, Map, String)} rather than {@link #isSupported(String, long, String, Map, String)}. + * + * @param function + * to be checked. + * @param transformerName + * name of the transformer. * @return {@code true} is supported, {@code false} otherwise. */ default boolean isSupported(CoreFunction function, String transformerName) diff --git a/model/src/main/java/org/alfresco/transform/registry/TransformerAndSourceType.java b/model/src/main/java/org/alfresco/transform/registry/TransformerAndSourceType.java index 3c520075..6e261fdc 100644 --- a/model/src/main/java/org/alfresco/transform/registry/TransformerAndSourceType.java +++ b/model/src/main/java/org/alfresco/transform/registry/TransformerAndSourceType.java @@ -21,13 +21,12 @@ */ package org.alfresco.transform.registry; -import org.alfresco.transform.config.OverrideSupported; - import java.util.Objects; +import org.alfresco.transform.config.OverrideSupported; + /** - * Key based using {@code transformerName} and {@code sourceMediaType} used as a key to lookup default values held in - * {@link OverrideSupported} objects. + * Key based using {@code transformerName} and {@code sourceMediaType} used as a key to lookup default values held in {@link OverrideSupported} objects. */ class TransformerAndSourceType { @@ -71,9 +70,9 @@ class TransformerAndSourceType { return false; } - TransformerAndSourceType that = (TransformerAndSourceType)o; + TransformerAndSourceType that = (TransformerAndSourceType) o; return Objects.equals(transformerName, that.transformerName) && - Objects.equals(sourceMediaType, that.sourceMediaType); + Objects.equals(sourceMediaType, that.sourceMediaType); } @Override diff --git a/model/src/main/java/org/alfresco/transform/registry/TransformerType.java b/model/src/main/java/org/alfresco/transform/registry/TransformerType.java index b7b8c06b..22359bcf 100644 --- a/model/src/main/java/org/alfresco/transform/registry/TransformerType.java +++ b/model/src/main/java/org/alfresco/transform/registry/TransformerType.java @@ -25,10 +25,7 @@ import org.alfresco.transform.config.Transformer; public enum TransformerType { - ENGINE_TRANSFORMER, - PIPELINE_TRANSFORMER, - FAILOVER_TRANSFORMER, - UNSUPPORTED_TRANSFORMER; + ENGINE_TRANSFORMER, PIPELINE_TRANSFORMER, FAILOVER_TRANSFORMER, UNSUPPORTED_TRANSFORMER; public static TransformerType valueOf(Transformer transformer) { @@ -37,7 +34,7 @@ public enum TransformerType return null; } if ((transformer.getTransformerFailover() == null || transformer.getTransformerFailover().isEmpty()) && - (transformer.getTransformerPipeline() == null || transformer.getTransformerPipeline().isEmpty())) + (transformer.getTransformerPipeline() == null || transformer.getTransformerPipeline().isEmpty())) { return ENGINE_TRANSFORMER; } @@ -52,4 +49,4 @@ public enum TransformerType return UNSUPPORTED_TRANSFORMER; } -} \ No newline at end of file +} diff --git a/model/src/main/java/org/alfresco/transform/router/ExtensionService.java b/model/src/main/java/org/alfresco/transform/router/ExtensionService.java index d9b94594..67ba3e8b 100644 --- a/model/src/main/java/org/alfresco/transform/router/ExtensionService.java +++ b/model/src/main/java/org/alfresco/transform/router/ExtensionService.java @@ -28,6 +28,5 @@ package org.alfresco.transform.router; public class ExtensionService extends org.alfresco.transform.common.ExtensionService { private ExtensionService() - { - } + {} } diff --git a/model/src/test/java/org/alfresco/transform/common/RepositoryClientDataTest.java b/model/src/test/java/org/alfresco/transform/common/RepositoryClientDataTest.java index a4289a1a..fae1f03f 100644 --- a/model/src/test/java/org/alfresco/transform/common/RepositoryClientDataTest.java +++ b/model/src/test/java/org/alfresco/transform/common/RepositoryClientDataTest.java @@ -21,17 +21,18 @@ */ package org.alfresco.transform.common; -import org.junit.jupiter.api.Test; - -import java.util.StringJoiner; +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.alfresco.transform.common.RepositoryClientData.CLIENT_DATA_SEPARATOR; import static org.alfresco.transform.common.RepositoryClientData.DEBUG; import static org.alfresco.transform.common.RepositoryClientData.DEBUG_SEPARATOR; import static org.alfresco.transform.common.RepositoryClientData.REPO_ID; -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 java.util.StringJoiner; + +import org.junit.jupiter.api.Test; public class RepositoryClientDataTest { @@ -41,11 +42,11 @@ public class RepositoryClientDataTest void AcsClientDataWithDebugTest() { repositoryClientData = RepositoryClientData.builder() - .withRepoId("ACS1234") - .withRenditionName("renditionName") - .withRequestId("e123") - .withDebug() - .build(); + .withRepoId("ACS1234") + .withRenditionName("renditionName") + .withRequestId("e123") + .withDebug() + .build(); String clientData = repositoryClientData.toString(); assertEquals("ACS1234", repositoryClientData.getAcsVersion()); @@ -59,17 +60,17 @@ public class RepositoryClientDataTest void AcsClientDataWithoutDebugTest() { String clientData = new StringJoiner(CLIENT_DATA_SEPARATOR) - .add(REPO_ID + "ACS1234") - .add("1") - .add("renditionName") - .add("3") - .add("4") - .add("5") - .add("54321") - .add("7") - .add("8") - .add("9") - .toString(); + .add(REPO_ID + "ACS1234") + .add("1") + .add("renditionName") + .add("3") + .add("4") + .add("5") + .add("54321") + .add("7") + .add("8") + .add("9") + .toString(); repositoryClientData = new RepositoryClientData(clientData); assertEquals("ACS1234", repositoryClientData.getAcsVersion()); @@ -83,17 +84,17 @@ public class RepositoryClientDataTest void noLeadingRepoTest() { String clientData = new StringJoiner(CLIENT_DATA_SEPARATOR) - .add("ACS1234") - .add("1") - .add("renditionName") - .add("3") - .add("4") - .add("5") - .add("54321") - .add("7") - .add("8") - .add("9") - .toString(); + .add("ACS1234") + .add("1") + .add("renditionName") + .add("3") + .add("4") + .add("5") + .add("54321") + .add("7") + .add("8") + .add("9") + .toString(); repositoryClientData = new RepositoryClientData(clientData); assertEquals("", repositoryClientData.getAcsVersion()); @@ -107,16 +108,16 @@ public class RepositoryClientDataTest void tooFewElementsTest() { String clientData = new StringJoiner(CLIENT_DATA_SEPARATOR) - .add(REPO_ID + "ACS1234") - .add("1") - .add("renditionName") - .add("3") - .add("4") - .add("5") - .add("54321") - .add("7") - .add("8") - .toString(); + .add(REPO_ID + "ACS1234") + .add("1") + .add("renditionName") + .add("3") + .add("4") + .add("5") + .add("54321") + .add("7") + .add("8") + .toString(); repositoryClientData = new RepositoryClientData(clientData); assertEquals("", repositoryClientData.getAcsVersion()); @@ -130,18 +131,18 @@ public class RepositoryClientDataTest void tooManyElementsTest() { String clientData = new StringJoiner(CLIENT_DATA_SEPARATOR) - .add(REPO_ID + "ACS1234") - .add("1") - .add("renditionName") - .add("3") - .add("4") - .add("5") - .add("54321") - .add("7") - .add("8") - .add(DEBUG) - .add("10") - .toString(); + .add(REPO_ID + "ACS1234") + .add("1") + .add("renditionName") + .add("3") + .add("4") + .add("5") + .add("54321") + .add("7") + .add("8") + .add(DEBUG) + .add("10") + .toString(); repositoryClientData = new RepositoryClientData(clientData); assertEquals("", repositoryClientData.getAcsVersion()); assertEquals("", repositoryClientData.getRenditionName()); @@ -169,27 +170,27 @@ public class RepositoryClientDataTest void debugTest() { String clientData = new StringJoiner(CLIENT_DATA_SEPARATOR) - .add(REPO_ID + "ACS1234") - .add("1") - .add("2") - .add("3") - .add("4") - .add("5") - .add("6") - .add("7") - .add("8") - .add(DEBUG) - .toString(); + .add(REPO_ID + "ACS1234") + .add("1") + .add("2") + .add("3") + .add("4") + .add("5") + .add("6") + .add("7") + .add("8") + .add(DEBUG) + .toString(); repositoryClientData = new RepositoryClientData(clientData); assertEquals(clientData, repositoryClientData.toString()); repositoryClientData.appendDebug("Some debug"); - assertEquals(clientData+DEBUG_SEPARATOR+"Some debug", - repositoryClientData.toString()); + assertEquals(clientData + DEBUG_SEPARATOR + "Some debug", + repositoryClientData.toString()); repositoryClientData.appendDebug("Some other debug"); - assertEquals(clientData+DEBUG_SEPARATOR+"Some debug"+DEBUG_SEPARATOR+"Some other debug", - repositoryClientData.toString()); + assertEquals(clientData + DEBUG_SEPARATOR + "Some debug" + DEBUG_SEPARATOR + "Some other debug", + repositoryClientData.toString()); } } diff --git a/model/src/test/java/org/alfresco/transform/common/TransformerDebugTest.java b/model/src/test/java/org/alfresco/transform/common/TransformerDebugTest.java index 4f34fd55..fba4bfd3 100644 --- a/model/src/test/java/org/alfresco/transform/common/TransformerDebugTest.java +++ b/model/src/test/java/org/alfresco/transform/common/TransformerDebugTest.java @@ -21,30 +21,31 @@ */ package org.alfresco.transform.common; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; +import static org.alfresco.transform.common.RepositoryClientData.DEBUG_SEPARATOR; + +import java.util.StringJoiner; + import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.AppenderBase; import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.client.model.InternalContext; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.messages.TransformStack; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.LoggerFactory; -import java.util.StringJoiner; - -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_WORD; -import static org.alfresco.transform.common.RepositoryClientData.DEBUG_SEPARATOR; -import static org.junit.jupiter.api.Assertions.assertEquals; +import org.alfresco.transform.client.model.InternalContext; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.messages.TransformStack; /** - * Tests TransformerDebug. AbstractRouterTest in the t-router contains more complete end to end tests. The tests in this - * class are a smoke test at the moment. + * Tests TransformerDebug. AbstractRouterTest in the t-router contains more complete end to end tests. The tests in this class are a smoke test at the moment. */ class TransformerDebugTest { @@ -61,7 +62,7 @@ class TransformerDebugTest } private void twoStepTransform(boolean isTRouter, boolean fail, Level logLevel, String renditionName, - long sourceSize) + long sourceSize) { transformerDebug.setIsTRouter(isTRouter); monitorLogs(logLevel); @@ -90,7 +91,7 @@ class TransformerDebugTest transformerDebug.logOptions(request); TransformStack.removeSuccessfulStep(reply, transformerDebug); - request.setTransformRequestOptions(ImmutableMap.of("k1", "v1", "k2","v2")); + request.setTransformRequestOptions(ImmutableMap.of("k1", "v1", "k2", "v2")); transformerDebug.pushTransform(request); transformerDebug.logOptions(request); if (fail) @@ -107,16 +108,15 @@ class TransformerDebugTest private void monitorLogs(Level logLevel) { - Logger logger = (Logger)LoggerFactory.getLogger(TransformerDebug.class); - AppenderBase logAppender = new AppenderBase<>() - { + Logger logger = (Logger) LoggerFactory.getLogger(TransformerDebug.class); + AppenderBase logAppender = new AppenderBase<>() { @Override protected void append(ILoggingEvent iLoggingEvent) { transformerDebugOutput.add(iLoggingEvent.getMessage()); } }; - logAppender.setContext((LoggerContext)LoggerFactory.getILoggerFactory()); + logAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory()); logger.setLevel(logLevel); logger.addAppender(logAppender); logAppender.start(); @@ -128,12 +128,12 @@ class TransformerDebugTest twoStepTransform(true, false, Level.DEBUG, "", 1234L); Assertions.assertEquals("" + - "1 txt pdf 1.2 KB wrapper\n" + - "1.1 txt doc transformer1\n" + - "1.2 doc pdf transformer2\n" + - "1.2 k1=\"v1\"\n" + - "1.2 k2=\"v2\"\n" + - "1 Finished in -- ms", + "1 txt pdf 1.2 KB wrapper\n" + + "1.1 txt doc transformer1\n" + + "1.2 doc pdf transformer2\n" + + "1.2 k1=\"v1\"\n" + + "1.2 k2=\"v2\"\n" + + "1 Finished in -- ms", getTransformerDebugOutput()); } @@ -145,14 +145,14 @@ class TransformerDebugTest // With trace there are "Finished" lines for nested transforms, like a T-Engine's debug but still without // the size and rendition name Assertions.assertEquals("" + - "1 txt pdf 1.2 KB wrapper\n" + - "1.1 txt doc transformer1\n" + - "1.1 Finished in -- ms\n" + - "1.2 doc pdf transformer2\n" + - "1.2 k1=\"v1\"\n" + - "1.2 k2=\"v2\"\n" + - "1.2 Finished in -- ms\n" + - "1 Finished in -- ms", + "1 txt pdf 1.2 KB wrapper\n" + + "1.1 txt doc transformer1\n" + + "1.1 Finished in -- ms\n" + + "1.2 doc pdf transformer2\n" + + "1.2 k1=\"v1\"\n" + + "1.2 k2=\"v2\"\n" + + "1.2 Finished in -- ms\n" + + "1 Finished in -- ms", getTransformerDebugOutput()); } @@ -164,14 +164,14 @@ class TransformerDebugTest // Note the first and last lines would only ever be logged on the router, but the expected data includes // the extra "Finished" lines, sizes and renditions (if set in client data). Assertions.assertEquals("" + - "1 txt pdf 1.2 KB wrapper\n" + - "1.1 txt doc 1.2 KB transformer1\n" + - "1.1 Finished in -- ms\n" + - "1.2 doc pdf 1.2 KB transformer2\n" + - "1.2 k1=\"v1\"\n" + - "1.2 k2=\"v2\"\n" + - "1.2 Finished in -- ms\n" + - "1 Finished in -- ms", + "1 txt pdf 1.2 KB wrapper\n" + + "1.1 txt doc 1.2 KB transformer1\n" + + "1.1 Finished in -- ms\n" + + "1.2 doc pdf 1.2 KB transformer2\n" + + "1.2 k1=\"v1\"\n" + + "1.2 k2=\"v2\"\n" + + "1.2 Finished in -- ms\n" + + "1 Finished in -- ms", getTransformerDebugOutput()); } @@ -181,13 +181,13 @@ class TransformerDebugTest twoStepTransform(true, true, Level.DEBUG, "", 1234L); Assertions.assertEquals("" + - "1 txt pdf 1.2 KB wrapper\n" + - "1.1 txt doc transformer1\n" + - "1.2 doc pdf transformer2\n" + - "1.2 k1=\"v1\"\n" + - "1.2 k2=\"v2\"\n" + - "1.2 Dummy error", - getTransformerDebugOutput()); + "1 txt pdf 1.2 KB wrapper\n" + + "1.1 txt doc transformer1\n" + + "1.2 doc pdf transformer2\n" + + "1.2 k1=\"v1\"\n" + + "1.2 k2=\"v2\"\n" + + "1.2 Dummy error", + getTransformerDebugOutput()); } @Test @@ -196,13 +196,13 @@ class TransformerDebugTest twoStepTransform(true, false, Level.DEBUG, "renditionName", 1234L); Assertions.assertEquals("" + - "1 txt pdf 1.2 KB -- renditionName -- wrapper\n" + - "1.1 txt doc transformer1\n" + - "1.2 doc pdf transformer2\n" + - "1.2 k1=\"v1\"\n" + - "1.2 k2=\"v2\"\n" + - "1 Finished in -- ms", - getTransformerDebugOutput()); + "1 txt pdf 1.2 KB -- renditionName -- wrapper\n" + + "1.1 txt doc transformer1\n" + + "1.2 doc pdf transformer2\n" + + "1.2 k1=\"v1\"\n" + + "1.2 k2=\"v2\"\n" + + "1 Finished in -- ms", + getTransformerDebugOutput()); } @Test @@ -211,13 +211,13 @@ class TransformerDebugTest twoStepTransform(true, false, Level.DEBUG, "transform:alfresco-metadata-extract", 1234L); Assertions.assertEquals("" + - "1 txt pdf 1.2 KB -- metadataExtract -- wrapper\n" + - "1.1 txt doc transformer1\n" + - "1.2 doc pdf transformer2\n" + - "1.2 k1=\"v1\"\n" + - "1.2 k2=\"v2\"\n" + - "1 Finished in -- ms", - getTransformerDebugOutput()); + "1 txt pdf 1.2 KB -- metadataExtract -- wrapper\n" + + "1.1 txt doc transformer1\n" + + "1.2 doc pdf transformer2\n" + + "1.2 k1=\"v1\"\n" + + "1.2 k2=\"v2\"\n" + + "1 Finished in -- ms", + getTransformerDebugOutput()); } @Test @@ -226,13 +226,13 @@ class TransformerDebugTest twoStepTransform(true, false, Level.DEBUG, "transform:alfresco-metadata-embed", 1234L); Assertions.assertEquals("" + - "1 txt pdf 1.2 KB -- metadataEmbed -- wrapper\n" + - "1.1 txt doc transformer1\n" + - "1.2 doc pdf transformer2\n" + - "1.2 k1=\"v1\"\n" + - "1.2 k2=\"v2\"\n" + - "1 Finished in -- ms", - getTransformerDebugOutput()); + "1 txt pdf 1.2 KB -- metadataEmbed -- wrapper\n" + + "1.1 txt doc transformer1\n" + + "1.2 doc pdf transformer2\n" + + "1.2 k1=\"v1\"\n" + + "1.2 k2=\"v2\"\n" + + "1 Finished in -- ms", + getTransformerDebugOutput()); } @Test @@ -241,28 +241,28 @@ class TransformerDebugTest twoStepTransform(true, false, Level.DEBUG, "", 1); Assertions.assertEquals("" + - "1 txt pdf 1 byte wrapper\n" + - "1.1 txt doc transformer1\n" + - "1.2 doc pdf transformer2\n" + - "1.2 k1=\"v1\"\n" + - "1.2 k2=\"v2\"\n" + - "1 Finished in -- ms", - getTransformerDebugOutput()); + "1 txt pdf 1 byte wrapper\n" + + "1.1 txt doc transformer1\n" + + "1.2 doc pdf transformer2\n" + + "1.2 k1=\"v1\"\n" + + "1.2 k2=\"v2\"\n" + + "1 Finished in -- ms", + getTransformerDebugOutput()); } @Test void testSourceSize23TB() { - twoStepTransform(true, false, Level.DEBUG, "", 23L*1024*1024*1024*1024); + twoStepTransform(true, false, Level.DEBUG, "", 23L * 1024 * 1024 * 1024 * 1024); Assertions.assertEquals("" + - "1 txt pdf 23 TB wrapper\n" + - "1.1 txt doc transformer1\n" + - "1.2 doc pdf transformer2\n" + - "1.2 k1=\"v1\"\n" + - "1.2 k2=\"v2\"\n" + - "1 Finished in -- ms", - getTransformerDebugOutput()); + "1 txt pdf 23 TB wrapper\n" + + "1.1 txt doc transformer1\n" + + "1.2 doc pdf transformer2\n" + + "1.2 k1=\"v1\"\n" + + "1.2 k2=\"v2\"\n" + + "1 Finished in -- ms", + getTransformerDebugOutput()); } @Test @@ -301,7 +301,7 @@ class TransformerDebugTest transformerDebug.logFailure(reply); String expectedDebug = " T-Request was null - a major error"; - String expectedClientData = origClientData+DEBUG_SEPARATOR+expectedDebug; + String expectedClientData = origClientData + DEBUG_SEPARATOR + expectedDebug; Assertions.assertEquals(expectedDebug, getTransformerDebugOutput()); assertEquals(expectedClientData, reply.getClientData()); } @@ -309,15 +309,15 @@ class TransformerDebugTest @Test void tesGetOptionAndValue() { - String sixtyChars = "12345678 10 345678 20 345678 30 345678 40 345678 50 abcdefgh"; + String sixtyChars = "12345678 10 345678 20 345678 30 345678 40 345678 50 abcdefgh"; String sixtyOneChars = "12345678 10 345678 20 345678 30 345678 40 345678 50 abcd12345"; - String expected = "12345678 10 345678 20 345678 30 345678 40 345678 50 ...12345"; + String expected = "12345678 10 345678 20 345678 30 345678 40 345678 50 ...12345"; assertEquals("ref key=\"value\"", transformerDebug.getOptionAndValue("ref", "key", "value")); - assertEquals("ref key=\""+sixtyChars+"\"", + assertEquals("ref key=\"" + sixtyChars + "\"", transformerDebug.getOptionAndValue("ref", "key", sixtyChars)); - assertEquals("ref key=\""+expected+"\"", + assertEquals("ref key=\"" + expected + "\"", transformerDebug.getOptionAndValue("ref", "key", sixtyOneChars)); } -} \ No newline at end of file +} diff --git a/model/src/test/java/org/alfresco/transform/config/CoreFunctionTest.java b/model/src/test/java/org/alfresco/transform/config/CoreFunctionTest.java index df95d3cc..97829083 100644 --- a/model/src/test/java/org/alfresco/transform/config/CoreFunctionTest.java +++ b/model/src/test/java/org/alfresco/transform/config/CoreFunctionTest.java @@ -21,10 +21,10 @@ */ package org.alfresco.transform.config; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + class CoreFunctionTest { @Test @@ -47,4 +47,4 @@ class CoreFunctionTest assertTrue(CoreFunction.DIRECT_ACCESS_URL.isSupported("2.6")); assertTrue(CoreFunction.DIRECT_ACCESS_URL.isSupported("999999")); } -} \ No newline at end of file +} diff --git a/model/src/test/java/org/alfresco/transform/config/reader/TransformConfigReaderJsonTest.java b/model/src/test/java/org/alfresco/transform/config/reader/TransformConfigReaderJsonTest.java index 1c824946..acbd43e8 100644 --- a/model/src/test/java/org/alfresco/transform/config/reader/TransformConfigReaderJsonTest.java +++ b/model/src/test/java/org/alfresco/transform/config/reader/TransformConfigReaderJsonTest.java @@ -1,300 +1,283 @@ -/* - * #%L - * Alfresco Transform Model - * %% - * Copyright (C) 2015 - 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.config.reader; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.config.TransformOption; -import org.alfresco.transform.config.TransformOptionGroup; -import org.alfresco.transform.config.TransformOptionValue; -import org.alfresco.transform.config.Transformer; -import org.alfresco.transform.config.SupportedSourceAndTarget; -import org.alfresco.transform.config.TransformStep; -import org.junit.jupiter.api.Test; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; - -public class TransformConfigReaderJsonTest -{ - @Test - public void testEmptyRoutesFile() throws Exception - { - final Resource resource = new ClassPathResource("config/sample1.json"); - final TransformConfigReader loader = TransformConfigReaderFactory.create(resource); - TransformConfig transformConfig = loader.load(); - final List transformers = transformConfig.getTransformers(); - - assertNotNull(transformers); - assertEquals(Collections.emptyList(), transformers); - } - - @Test - public void testMixedRoutesFile() throws Exception - { - final List expected = prepareSample2(); - - final Resource resource = new ClassPathResource("config/sample2.json"); - final TransformConfigReader loader = TransformConfigReaderFactory.create(resource); - - TransformConfig transformConfig = loader.load(); - final List transformers = transformConfig.getTransformers(); - - assertNotNull(transformers); - assertEquals(expected.size(), transformers.size()); - assertTrue(expected.containsAll(transformers)); - } - - private List prepareSample2() - { - return List.of( - Transformer.builder() - .withTransformerName("CORE_AIO") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("image/gif") - .withTargetMediaType("image/gif") - .build() - )) - .withTransformOptions(ImmutableSet.of("imageMagickOptions")) - .build(), - Transformer.builder() - .withTransformerName("IMAGEMAGICK") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("image/gif") - .withTargetMediaType("image/gif") - .build() - )) - .withTransformOptions(ImmutableSet.of("imageMagickOptions")) - .build(), - Transformer.builder() - .withTransformerName("CORE_AIO") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("application/msword") - .withTargetMediaType("application/pdf") - .withMaxSourceSizeBytes(18874368L) - .build() - )) - .build(), - Transformer.builder() - .withTransformerName("PDF_RENDERER") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("application/vnd.ms-powerpoint") - .withTargetMediaType("application/pdf") - .withPriority(55) - .withMaxSourceSizeBytes(50331648L) - .build() - )) - .build(), - Transformer.builder() - .withTransformerName("CORE_AIO") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("text/plain") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/mediawiki") - .withTargetMediaType("text/plain") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/css") - .withTargetMediaType("text/plain") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("text/plain") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("text/plain") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/html") - .withTargetMediaType("text/plain") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("application/x-javascript") - .withTargetMediaType("text/plain") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("application/dita+xml") - .withTargetMediaType("text/plain") - .build() - )) - .withTransformOptions(ImmutableSet.of("stringOptions")) - .build(), - Transformer.builder() - .withTransformerName("officeToImageViaPdf") - .withTransformerPipeline(ImmutableList.of( - new TransformStep("libreoffice", "application/pdf"), - new TransformStep("pdfToImageViaPng", null) - )) - .withTransformOptions(ImmutableSet.of( - "pdfRendererOptions", - "imageMagickOptions" - )) - .build(), - Transformer.builder() - .withTransformerName("textToImageViaPdf") - .withTransformerPipeline(ImmutableList.of( - new TransformStep("libreoffice", "application/pdf"), - new TransformStep("pdfToImageViaPng", null) - )) - .withTransformOptions(ImmutableSet.of( - "pdfRendererOptions", - "imageMagickOptions" - )) - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("image/gif") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("image/jpeg") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("image/tiff") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("image/png") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("image/gif") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("image/jpeg") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("image/tiff") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("image/png") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("image/gif") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("image/jpeg") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("image/tiff") - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("image/png") - .build() - )) - .withTransformOptions(ImmutableSet.of( - "pdfRendererOptions", - "imageMagickOptions" - )) - .build() - ); - } - - @Test - public void testRouteFileWithTransformOptions() throws Exception - { - final List expected = prepareSample5Transformers(); - Map> expectedOptions = prepareSample5Options(); - - final Resource resource = new ClassPathResource("config/sample5.json"); - final TransformConfigReader loader = TransformConfigReaderFactory.create(resource); - - TransformConfig transformConfig = loader.load(); - final List transformers = transformConfig.getTransformers(); - Map> transformOptions = transformConfig.getTransformOptions(); - - assertNotNull(transformers); - assertEquals(expected.size(), transformers.size()); - assertTrue(expected.containsAll(transformers)); - assertEquals(expectedOptions, transformOptions); - } - - private List prepareSample5Transformers() - { - return List.of( - Transformer.builder() - .withTransformerName("CORE_AIO") - .withSupportedSourceAndTargetList(ImmutableSet.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("image/gif") - .withTargetMediaType("image/gif") - .build() - )) - .withTransformOptions(ImmutableSet.of("imageMagickOptions")) - .build() - ); - } - - public static Map> prepareSample5Options() - { - return ImmutableMap.of( - "imageMagickOptions", ImmutableSet.of( - new TransformOptionValue(false, "alphaRemove"), - new TransformOptionValue(false, "autoOrient"), - new TransformOptionValue(false, "startPage"), - new TransformOptionValue(false, "endPage"), - new TransformOptionGroup(false, ImmutableSet.of( - new TransformOptionValue(false, "cropGravity"), - new TransformOptionValue(false, "cropWidth"), - new TransformOptionValue(false, "cropHeight"), - new TransformOptionValue(false, "cropPercentage"), - new TransformOptionValue(false, "cropXOffset"), - new TransformOptionValue(false, "cropYOffset") - )), - new TransformOptionGroup(false, ImmutableSet.of( - new TransformOptionValue(false, "thumbnail"), - new TransformOptionValue(false, "resizeHeight"), - new TransformOptionValue(false, "resizeWidth"), - new TransformOptionValue(false, "resizePercentage"), - new TransformOptionValue(false, "allowEnlargement"), - new TransformOptionValue(false, "maintainAspectRatio") - ))) - ); - } -} \ No newline at end of file +/* + * #%L + * Alfresco Transform Model + * %% + * Copyright (C) 2015 - 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.config.reader; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.Test; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + +import org.alfresco.transform.config.SupportedSourceAndTarget; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.TransformOptionGroup; +import org.alfresco.transform.config.TransformOptionValue; +import org.alfresco.transform.config.TransformStep; +import org.alfresco.transform.config.Transformer; + +public class TransformConfigReaderJsonTest +{ + @Test + public void testEmptyRoutesFile() throws Exception + { + final Resource resource = new ClassPathResource("config/sample1.json"); + final TransformConfigReader loader = TransformConfigReaderFactory.create(resource); + TransformConfig transformConfig = loader.load(); + final List transformers = transformConfig.getTransformers(); + + assertNotNull(transformers); + assertEquals(Collections.emptyList(), transformers); + } + + @Test + public void testMixedRoutesFile() throws Exception + { + final List expected = prepareSample2(); + + final Resource resource = new ClassPathResource("config/sample2.json"); + final TransformConfigReader loader = TransformConfigReaderFactory.create(resource); + + TransformConfig transformConfig = loader.load(); + final List transformers = transformConfig.getTransformers(); + + assertNotNull(transformers); + assertEquals(expected.size(), transformers.size()); + assertTrue(expected.containsAll(transformers)); + } + + private List prepareSample2() + { + return List.of( + Transformer.builder() + .withTransformerName("CORE_AIO") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("image/gif") + .withTargetMediaType("image/gif") + .build())) + .withTransformOptions(ImmutableSet.of("imageMagickOptions")) + .build(), + Transformer.builder() + .withTransformerName("IMAGEMAGICK") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("image/gif") + .withTargetMediaType("image/gif") + .build())) + .withTransformOptions(ImmutableSet.of("imageMagickOptions")) + .build(), + Transformer.builder() + .withTransformerName("CORE_AIO") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("application/msword") + .withTargetMediaType("application/pdf") + .withMaxSourceSizeBytes(18874368L) + .build())) + .build(), + Transformer.builder() + .withTransformerName("PDF_RENDERER") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("application/vnd.ms-powerpoint") + .withTargetMediaType("application/pdf") + .withPriority(55) + .withMaxSourceSizeBytes(50331648L) + .build())) + .build(), + Transformer.builder() + .withTransformerName("CORE_AIO") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/plain") + .withTargetMediaType("text/plain") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/mediawiki") + .withTargetMediaType("text/plain") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/css") + .withTargetMediaType("text/plain") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/csv") + .withTargetMediaType("text/plain") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/xml") + .withTargetMediaType("text/plain") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/html") + .withTargetMediaType("text/plain") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("application/x-javascript") + .withTargetMediaType("text/plain") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("application/dita+xml") + .withTargetMediaType("text/plain") + .build())) + .withTransformOptions(ImmutableSet.of("stringOptions")) + .build(), + Transformer.builder() + .withTransformerName("officeToImageViaPdf") + .withTransformerPipeline(ImmutableList.of( + new TransformStep("libreoffice", "application/pdf"), + new TransformStep("pdfToImageViaPng", null))) + .withTransformOptions(ImmutableSet.of( + "pdfRendererOptions", + "imageMagickOptions")) + .build(), + Transformer.builder() + .withTransformerName("textToImageViaPdf") + .withTransformerPipeline(ImmutableList.of( + new TransformStep("libreoffice", "application/pdf"), + new TransformStep("pdfToImageViaPng", null))) + .withTransformOptions(ImmutableSet.of( + "pdfRendererOptions", + "imageMagickOptions")) + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/plain") + .withTargetMediaType("image/gif") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/plain") + .withTargetMediaType("image/jpeg") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/plain") + .withTargetMediaType("image/tiff") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/plain") + .withTargetMediaType("image/png") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/csv") + .withTargetMediaType("image/gif") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/csv") + .withTargetMediaType("image/jpeg") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/csv") + .withTargetMediaType("image/tiff") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/csv") + .withTargetMediaType("image/png") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/xml") + .withTargetMediaType("image/gif") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/xml") + .withTargetMediaType("image/jpeg") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/xml") + .withTargetMediaType("image/tiff") + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType("text/xml") + .withTargetMediaType("image/png") + .build())) + .withTransformOptions(ImmutableSet.of( + "pdfRendererOptions", + "imageMagickOptions")) + .build()); + } + + @Test + public void testRouteFileWithTransformOptions() throws Exception + { + final List expected = prepareSample5Transformers(); + Map> expectedOptions = prepareSample5Options(); + + final Resource resource = new ClassPathResource("config/sample5.json"); + final TransformConfigReader loader = TransformConfigReaderFactory.create(resource); + + TransformConfig transformConfig = loader.load(); + final List transformers = transformConfig.getTransformers(); + Map> transformOptions = transformConfig.getTransformOptions(); + + assertNotNull(transformers); + assertEquals(expected.size(), transformers.size()); + assertTrue(expected.containsAll(transformers)); + assertEquals(expectedOptions, transformOptions); + } + + private List prepareSample5Transformers() + { + return List.of( + Transformer.builder() + .withTransformerName("CORE_AIO") + .withSupportedSourceAndTargetList(ImmutableSet.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("image/gif") + .withTargetMediaType("image/gif") + .build())) + .withTransformOptions(ImmutableSet.of("imageMagickOptions")) + .build()); + } + + public static Map> prepareSample5Options() + { + return ImmutableMap.of( + "imageMagickOptions", ImmutableSet.of( + new TransformOptionValue(false, "alphaRemove"), + new TransformOptionValue(false, "autoOrient"), + new TransformOptionValue(false, "startPage"), + new TransformOptionValue(false, "endPage"), + new TransformOptionGroup(false, ImmutableSet.of( + new TransformOptionValue(false, "cropGravity"), + new TransformOptionValue(false, "cropWidth"), + new TransformOptionValue(false, "cropHeight"), + new TransformOptionValue(false, "cropPercentage"), + new TransformOptionValue(false, "cropXOffset"), + new TransformOptionValue(false, "cropYOffset"))), + new TransformOptionGroup(false, ImmutableSet.of( + new TransformOptionValue(false, "thumbnail"), + new TransformOptionValue(false, "resizeHeight"), + new TransformOptionValue(false, "resizeWidth"), + new TransformOptionValue(false, "resizePercentage"), + new TransformOptionValue(false, "allowEnlargement"), + new TransformOptionValue(false, "maintainAspectRatio"))))); + } +} diff --git a/model/src/test/java/org/alfresco/transform/config/reader/TransformConfigReaderYamlTest.java b/model/src/test/java/org/alfresco/transform/config/reader/TransformConfigReaderYamlTest.java index 1449d139..c9892773 100644 --- a/model/src/test/java/org/alfresco/transform/config/reader/TransformConfigReaderYamlTest.java +++ b/model/src/test/java/org/alfresco/transform/config/reader/TransformConfigReaderYamlTest.java @@ -31,17 +31,17 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.config.TransformOption; -import org.alfresco.transform.config.Transformer; -import org.alfresco.transform.config.SupportedSourceAndTarget; -import org.alfresco.transform.config.TransformStep; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.junit.jupiter.api.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; +import org.alfresco.transform.config.SupportedSourceAndTarget; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.TransformStep; +import org.alfresco.transform.config.Transformer; public class TransformConfigReaderYamlTest { @@ -55,8 +55,8 @@ public class TransformConfigReaderYamlTest assertNotNull(transformers); assertEquals(Collections.emptyList(), transformers); } - - @Test + + @Test public void testMixedRoutesFile() throws Exception { final List expected = prepareSample4(); @@ -79,167 +79,156 @@ public class TransformConfigReaderYamlTest { var result = new ArrayList(); result.add(Transformer.builder() - .withTransformerName("CORE_AIO") - .withSupportedSourceAndTargetList(ImmutableSet.of( + .withTransformerName("CORE_AIO") + .withSupportedSourceAndTargetList(ImmutableSet.of( SupportedSourceAndTarget.builder() - .withSourceMediaType("image/gif") - .withTargetMediaType("image/gif") - .build() - )) - .withTransformOptions(ImmutableSet.of("imageMagickOptions")) - .build()); + .withSourceMediaType("image/gif") + .withTargetMediaType("image/gif") + .build())) + .withTransformOptions(ImmutableSet.of("imageMagickOptions")) + .build()); result.add(Transformer.builder() - .withTransformerName("IMAGEMAGICK") - .withSupportedSourceAndTargetList(ImmutableSet.of( + .withTransformerName("IMAGEMAGICK") + .withSupportedSourceAndTargetList(ImmutableSet.of( SupportedSourceAndTarget.builder() - .withSourceMediaType("image/gif") - .withTargetMediaType("image/gif") - .build() - )) - .withTransformOptions(ImmutableSet.of("imageMagickOptions")) - .build()); + .withSourceMediaType("image/gif") + .withTargetMediaType("image/gif") + .build())) + .withTransformOptions(ImmutableSet.of("imageMagickOptions")) + .build()); result.add(Transformer.builder() - .withTransformerName("CORE_AIO") - .withSupportedSourceAndTargetList(ImmutableSet.of( + .withTransformerName("CORE_AIO") + .withSupportedSourceAndTargetList(ImmutableSet.of( SupportedSourceAndTarget.builder() - .withSourceMediaType("application/msword") - .withTargetMediaType("application/pdf") - .withMaxSourceSizeBytes(18874368L) - .build() - )) - .build()); + .withSourceMediaType("application/msword") + .withTargetMediaType("application/pdf") + .withMaxSourceSizeBytes(18874368L) + .build())) + .build()); result.add(Transformer.builder() - .withTransformerName("PDF_RENDERER") - .withSupportedSourceAndTargetList(ImmutableSet.of( + .withTransformerName("PDF_RENDERER") + .withSupportedSourceAndTargetList(ImmutableSet.of( SupportedSourceAndTarget.builder() - .withSourceMediaType("application/vnd.ms-powerpoint") - .withTargetMediaType("application/pdf") - .withPriority(55) - .withMaxSourceSizeBytes(50331648L) - .build() - )) - .build()); + .withSourceMediaType("application/vnd.ms-powerpoint") + .withTargetMediaType("application/pdf") + .withPriority(55) + .withMaxSourceSizeBytes(50331648L) + .build())) + .build()); result.add(Transformer.builder() - .withTransformerName("CORE_AIO") - .withSupportedSourceAndTargetList(ImmutableSet.of( + .withTransformerName("CORE_AIO") + .withSupportedSourceAndTargetList(ImmutableSet.of( SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("text/plain") - .build(), + .withSourceMediaType("text/plain") + .withTargetMediaType("text/plain") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/mediawiki") - .withTargetMediaType("text/plain") - .build(), + .withSourceMediaType("text/mediawiki") + .withTargetMediaType("text/plain") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/css") - .withTargetMediaType("text/plain") - .build(), + .withSourceMediaType("text/css") + .withTargetMediaType("text/plain") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("text/plain") - .build(), + .withSourceMediaType("text/csv") + .withTargetMediaType("text/plain") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("text/plain") - .build(), + .withSourceMediaType("text/xml") + .withTargetMediaType("text/plain") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/html") - .withTargetMediaType("text/plain") - .build(), + .withSourceMediaType("text/html") + .withTargetMediaType("text/plain") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("application/x-javascript") - .withTargetMediaType("text/plain") - .build(), + .withSourceMediaType("application/x-javascript") + .withTargetMediaType("text/plain") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("application/dita+xml") - .withTargetMediaType("text/plain") - .build() - )) - .withTransformOptions(ImmutableSet.of("stringOptions")) - .build()); + .withSourceMediaType("application/dita+xml") + .withTargetMediaType("text/plain") + .build())) + .withTransformOptions(ImmutableSet.of("stringOptions")) + .build()); result.add(Transformer.builder() - .withTransformerName("officeToImageViaPdf") - .withTransformerPipeline(ImmutableList.of( + .withTransformerName("officeToImageViaPdf") + .withTransformerPipeline(ImmutableList.of( new TransformStep("libreoffice", "application/pdf"), - new TransformStep("pdfToImageViaPng", null) - )) - .withTransformOptions(ImmutableSet.of( + new TransformStep("pdfToImageViaPng", null))) + .withTransformOptions(ImmutableSet.of( "pdfRendererOptions", - "imageMagickOptions" - )) - .build()); - + "imageMagickOptions")) + .build()); + result.add(Transformer.builder() - .withTransformerName("textToImageViaPdf") - .withTransformerPipeline(ImmutableList.of( + .withTransformerName("textToImageViaPdf") + .withTransformerPipeline(ImmutableList.of( new TransformStep("libreoffice", "application/pdf"), - new TransformStep("pdfToImageViaPng", null) - )) - .withTransformOptions(ImmutableSet.of( + new TransformStep("pdfToImageViaPng", null))) + .withTransformOptions(ImmutableSet.of( "pdfRendererOptions", - "imageMagickOptions" - )) - .withSupportedSourceAndTargetList(ImmutableSet.of( + "imageMagickOptions")) + .withSupportedSourceAndTargetList(ImmutableSet.of( SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("image/gif") - .build(), + .withSourceMediaType("text/plain") + .withTargetMediaType("image/gif") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("image/jpeg") - .build(), + .withSourceMediaType("text/plain") + .withTargetMediaType("image/jpeg") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("image/tiff") - .build(), + .withSourceMediaType("text/plain") + .withTargetMediaType("image/tiff") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/plain") - .withTargetMediaType("image/png") - .build(), + .withSourceMediaType("text/plain") + .withTargetMediaType("image/png") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("image/gif") - .build(), + .withSourceMediaType("text/csv") + .withTargetMediaType("image/gif") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("image/jpeg") - .build(), + .withSourceMediaType("text/csv") + .withTargetMediaType("image/jpeg") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("image/tiff") - .build(), + .withSourceMediaType("text/csv") + .withTargetMediaType("image/tiff") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/csv") - .withTargetMediaType("image/png") - .build(), + .withSourceMediaType("text/csv") + .withTargetMediaType("image/png") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("image/gif") - .build(), + .withSourceMediaType("text/xml") + .withTargetMediaType("image/gif") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("image/jpeg") - .build(), + .withSourceMediaType("text/xml") + .withTargetMediaType("image/jpeg") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("image/tiff") - .build(), + .withSourceMediaType("text/xml") + .withTargetMediaType("image/tiff") + .build(), SupportedSourceAndTarget.builder() - .withSourceMediaType("text/xml") - .withTargetMediaType("image/png") - .build() - )) - .withTransformOptions(ImmutableSet.of( + .withSourceMediaType("text/xml") + .withTargetMediaType("image/png") + .build())) + .withTransformOptions(ImmutableSet.of( "pdfRendererOptions", - "imageMagickOptions" - )) - .build()); - + "imageMagickOptions")) + .build()); + return result; } } diff --git a/model/src/test/java/org/alfresco/transform/messages/TransformRequestValidatorTest.java b/model/src/test/java/org/alfresco/transform/messages/TransformRequestValidatorTest.java index 12a50763..9b17c511 100644 --- a/model/src/test/java/org/alfresco/transform/messages/TransformRequestValidatorTest.java +++ b/model/src/test/java/org/alfresco/transform/messages/TransformRequestValidatorTest.java @@ -21,19 +21,21 @@ */ package org.alfresco.transform.messages; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; 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.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_PNG; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; + import java.util.UUID; -import org.alfresco.transform.client.model.TransformRequest; import org.junit.jupiter.api.Test; import org.springframework.validation.DirectFieldBindingResult; import org.springframework.validation.Errors; +import org.alfresco.transform.client.model.TransformRequest; + /** * TransformRequestValidatorTest *

    @@ -57,7 +59,7 @@ public class TransformRequestValidatorTest assertEquals(1, errors.getAllErrors().size()); assertEquals("request cannot be null", - errors.getAllErrors().iterator().next().getDefaultMessage()); + errors.getAllErrors().iterator().next().getDefaultMessage()); } @Test @@ -70,7 +72,7 @@ public class TransformRequestValidatorTest assertFalse(errors.getAllErrors().isEmpty()); assertEquals("requestId cannot be null or empty", - errors.getAllErrors().iterator().next().getDefaultMessage()); + errors.getAllErrors().iterator().next().getDefaultMessage()); } @Test @@ -85,7 +87,7 @@ public class TransformRequestValidatorTest assertFalse(errors.getAllErrors().isEmpty()); assertEquals("sourceSize cannot be null or have its value smaller than 0", - errors.getAllErrors().iterator().next().getDefaultMessage()); + errors.getAllErrors().iterator().next().getDefaultMessage()); } @Test @@ -101,7 +103,7 @@ public class TransformRequestValidatorTest assertFalse(errors.getAllErrors().isEmpty()); assertEquals("sourceMediaType cannot be null or empty", - errors.getAllErrors().iterator().next().getDefaultMessage()); + errors.getAllErrors().iterator().next().getDefaultMessage()); } @Test @@ -118,7 +120,7 @@ public class TransformRequestValidatorTest assertFalse(errors.getAllErrors().isEmpty()); assertEquals("targetMediaType cannot be null or empty", - errors.getAllErrors().iterator().next().getDefaultMessage()); + errors.getAllErrors().iterator().next().getDefaultMessage()); } @Test @@ -136,7 +138,7 @@ public class TransformRequestValidatorTest assertFalse(errors.getAllErrors().isEmpty()); assertEquals("targetExtension cannot be null or empty", - errors.getAllErrors().iterator().next().getDefaultMessage()); + errors.getAllErrors().iterator().next().getDefaultMessage()); } @Test @@ -155,7 +157,7 @@ public class TransformRequestValidatorTest assertFalse(errors.getAllErrors().isEmpty()); assertEquals("clientData cannot be null or empty", - errors.getAllErrors().iterator().next().getDefaultMessage()); + errors.getAllErrors().iterator().next().getDefaultMessage()); } @Test @@ -176,7 +178,7 @@ public class TransformRequestValidatorTest assertFalse(errors.getAllErrors().isEmpty()); assertEquals("schema cannot be less than 0", - errors.getAllErrors().iterator().next().getDefaultMessage()); + errors.getAllErrors().iterator().next().getDefaultMessage()); } @Test diff --git a/model/src/test/java/org/alfresco/transform/messages/TransformStackTest.java b/model/src/test/java/org/alfresco/transform/messages/TransformStackTest.java index d0eeca20..69d287f3 100644 --- a/model/src/test/java/org/alfresco/transform/messages/TransformStackTest.java +++ b/model/src/test/java/org/alfresco/transform/messages/TransformStackTest.java @@ -21,16 +21,15 @@ */ package org.alfresco.transform.messages; -import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.client.model.InternalContext; -import org.alfresco.transform.client.model.MultiStep; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.common.TransformerDebug; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +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.mockito.Mockito.doReturn; + +import static org.alfresco.transform.messages.TransformStack.OPTIONS_LEVEL; +import static org.alfresco.transform.messages.TransformStack.SEPARATOR; +import static org.alfresco.transform.messages.TransformStack.TOP_STACK_LEVEL; +import static org.alfresco.transform.messages.TransformStack.getInitialSourceReference; import java.math.BigInteger; import java.util.ArrayList; @@ -38,14 +37,17 @@ import java.util.Arrays; import java.util.Map; import java.util.UUID; -import static org.alfresco.transform.messages.TransformStack.OPTIONS_LEVEL; -import static org.alfresco.transform.messages.TransformStack.SEPARATOR; -import static org.alfresco.transform.messages.TransformStack.TOP_STACK_LEVEL; -import static org.alfresco.transform.messages.TransformStack.getInitialSourceReference; -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.mockito.Mockito.doReturn; +import com.google.common.collect.ImmutableMap; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import org.alfresco.transform.client.model.InternalContext; +import org.alfresco.transform.client.model.MultiStep; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.common.TransformerDebug; class TransformStackTest { @@ -401,7 +403,7 @@ class TransformStackTest TransformStack.Step step = TransformStack.currentStep(internalContext); String transformerName = step.getTransformerName(); int depth = internalContext.getMultiStep().getTransformsToBeDone().size() - 2; - System.out.println(transformStepCount + " ".substring(0, depth*2+1) + transformerName); + System.out.println(transformStepCount + " ".substring(0, depth * 2 + 1) + transformerName); TransformStack.LevelBuilder nextLevel = TEST_LEVELS.get(transformerName); if (nextLevel == null) { @@ -491,15 +493,16 @@ class TransformStackTest TransformStack.addTransformLevel(internalContext, TEST_LEVELS.get("top")); for (String value : Arrays.asList( - "P" + SEPARATOR + "20" + SEPARATOR + START + SEPARATOR + "1" + STEP, - "P" + SEPARATOR + "4" + SEPARATOR + "123" + SEPARATOR + "12" + STEP + STEP)) + "P" + SEPARATOR + "20" + SEPARATOR + START + SEPARATOR + "1" + STEP, + "P" + SEPARATOR + "4" + SEPARATOR + "123" + SEPARATOR + "12" + STEP + STEP)) { System.out.println("TransformLevel value: " + value); internalContext.getMultiStep().getTransformsToBeDone().set(TOP_STACK_LEVEL, value); Assertions.assertNull(TransformStack.checkStructure(internalContext, "T-Reply")); // call a getter just in case we have missed something TransformStack.currentStep(internalContext); - }; + } + ; } @Test @@ -508,12 +511,13 @@ class TransformStackTest TransformStack.addTransformLevel(internalContext, TEST_LEVELS.get("top")); for (String value : Arrays.asList( - "P" + SEPARATOR + "e20" + SEPARATOR + START + SEPARATOR + "1" + STEP)) + "P" + SEPARATOR + "e20" + SEPARATOR + START + SEPARATOR + "1" + STEP)) { System.out.println("TransformLevel value: " + value); internalContext.getMultiStep().getTransformsToBeDone().set(TOP_STACK_LEVEL, value); Assertions.assertNull(TransformStack.checkStructure(internalContext, "T-Reply")); - }; + } + ; } @Test @@ -522,13 +526,14 @@ class TransformStackTest TransformStack.addTransformLevel(internalContext, TEST_LEVELS.get("top")); for (String value : Arrays.asList( - "P" + SEPARATOR + "x20" + SEPARATOR + START + SEPARATOR + "1" + STEP)) + "P" + SEPARATOR + "x20" + SEPARATOR + START + SEPARATOR + "1" + STEP)) { System.out.println("TransformLevel value: " + value); internalContext.getMultiStep().getTransformsToBeDone().set(TOP_STACK_LEVEL, value); assertEquals("T-Reply InternalContext did not have levels set correctly", - TransformStack.checkStructure(internalContext, "T-Reply")); - }; + TransformStack.checkStructure(internalContext, "T-Reply")); + } + ; } @Test @@ -542,25 +547,25 @@ class TransformStackTest for (String value : Arrays.asList( null, "", - "F" + SEPARATOR + "12" + SEPARATOR + START + SEPARATOR + "2", - "F" + SEPARATOR + "-1" + SEPARATOR + START + SEPARATOR + "2" + STEP, - "F" + SEPARATOR + "1" + SEPARATOR + "-3" + SEPARATOR + "2" + STEP, - "F" + SEPARATOR + "1" + SEPARATOR + START + SEPARATOR + "-2" + STEP, - "F" + SEPARATOR + "a" + SEPARATOR + START + SEPARATOR + "-2" + STEP, - "F" + SEPARATOR + "1" + SEPARATOR + START + SEPARATOR + "b" + STEP, - "P" + SEPARATOR + "0" + SEPARATOR + START + SEPARATOR + "12" + SEPARATOR + "name", - "P" + SEPARATOR + "0" + SEPARATOR + START + SEPARATOR + "12" + SEPARATOR + "name" + SEPARATOR + "source", - "P" + SEPARATOR + "0" + SEPARATOR + START + SEPARATOR + "12" + SEPARATOR + "name" + SEPARATOR + "source" + SEPARATOR + "", - "P" + SEPARATOR + "0" + SEPARATOR + START + SEPARATOR + "12" + SEPARATOR + "name" + SEPARATOR + "" + SEPARATOR + "target", - "F" + SEPARATOR + MAX_INT_PLUS_1 + SEPARATOR + START + SEPARATOR + "2" + STEP, - "F" + SEPARATOR + "1" + SEPARATOR + MAX_LONG_PLUS_1 + SEPARATOR + "2" + STEP, - "F" + SEPARATOR + "1" + SEPARATOR + START + SEPARATOR + MAX_INT_PLUS_1 + STEP - )) + "F" + SEPARATOR + "12" + SEPARATOR + START + SEPARATOR + "2", + "F" + SEPARATOR + "-1" + SEPARATOR + START + SEPARATOR + "2" + STEP, + "F" + SEPARATOR + "1" + SEPARATOR + "-3" + SEPARATOR + "2" + STEP, + "F" + SEPARATOR + "1" + SEPARATOR + START + SEPARATOR + "-2" + STEP, + "F" + SEPARATOR + "a" + SEPARATOR + START + SEPARATOR + "-2" + STEP, + "F" + SEPARATOR + "1" + SEPARATOR + START + SEPARATOR + "b" + STEP, + "P" + SEPARATOR + "0" + SEPARATOR + START + SEPARATOR + "12" + SEPARATOR + "name", + "P" + SEPARATOR + "0" + SEPARATOR + START + SEPARATOR + "12" + SEPARATOR + "name" + SEPARATOR + "source", + "P" + SEPARATOR + "0" + SEPARATOR + START + SEPARATOR + "12" + SEPARATOR + "name" + SEPARATOR + "source" + SEPARATOR + "", + "P" + SEPARATOR + "0" + SEPARATOR + START + SEPARATOR + "12" + SEPARATOR + "name" + SEPARATOR + "" + SEPARATOR + "target", + "F" + SEPARATOR + MAX_INT_PLUS_1 + SEPARATOR + START + SEPARATOR + "2" + STEP, + "F" + SEPARATOR + "1" + SEPARATOR + MAX_LONG_PLUS_1 + SEPARATOR + "2" + STEP, + "F" + SEPARATOR + "1" + SEPARATOR + START + SEPARATOR + MAX_INT_PLUS_1 + STEP)) { System.out.println("TransformLevel value: " + value); internalContext.getMultiStep().getTransformsToBeDone().set(TOP_STACK_LEVEL, value); assertEquals("T-Reply InternalContext did not have levels set correctly", TransformStack.checkStructure(internalContext, "T-Reply")); - }; + } + ; } -} \ No newline at end of file +} diff --git a/model/src/test/java/org/alfresco/transform/registry/CombinedTransformConfigTest.java b/model/src/test/java/org/alfresco/transform/registry/CombinedTransformConfigTest.java index 746a2685..2f6b019c 100644 --- a/model/src/test/java/org/alfresco/transform/registry/CombinedTransformConfigTest.java +++ b/model/src/test/java/org/alfresco/transform/registry/CombinedTransformConfigTest.java @@ -21,26 +21,28 @@ */ package org.alfresco.transform.registry; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import org.alfresco.transform.config.SupportedSourceAndTarget; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.config.TransformStep; -import org.alfresco.transform.config.Transformer; -import org.junit.jupiter.api.Test; +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.emptySet; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import java.io.IOException; import java.util.List; import java.util.Set; -import static java.util.Collections.emptyList; -import static java.util.Collections.emptyMap; -import static java.util.Collections.emptySet; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.Test; + +import org.alfresco.transform.config.SupportedSourceAndTarget; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.TransformStep; +import org.alfresco.transform.config.Transformer; /** * Test the CombinedTransformConfig, extended by both T-Router and ACS repository. @@ -194,8 +196,7 @@ public class CombinedTransformConfigTest private static final String ROUTER_CONFIG_HAS_NO_BASE_URL = null; private static final String BASE_URL_B = "baseUrlB"; - private final CombinedTransformConfig config = new CombinedTransformConfig() - { + private final CombinedTransformConfig config = new CombinedTransformConfig() { @Override protected boolean isPassThroughTransformName(String name) { @@ -210,8 +211,8 @@ public class CombinedTransformConfigTest private TransformConfig readTransformConfigFromResource(String filename) throws IOException { return objectMapper.readValue( - getClass().getClassLoader().getResourceAsStream(filename), - TransformConfig.class); + getClass().getClassLoader().getResourceAsStream(filename), + TransformConfig.class); } private String expectedWildcardError(String errorReason) @@ -225,7 +226,7 @@ public class CombinedTransformConfigTest // tEngineTransformers). The override is expected to good unless an error message is provided. // A check is made at the end that A2D is possible and that A2B is not possible. private void assertOverride(List tEngineTransformers, - List tRouterTransformers, String expectedError) + List tRouterTransformers, String expectedError) { Transformer expectedTransformer = tRouterTransformers.isEmpty() ? tEngineTransformers.get(tEngineTransformers.size() - 1) @@ -658,7 +659,7 @@ public class CombinedTransformConfigTest assertEquals("No supported source and target mimetypes could be added to the transformer \"5\" as " + "intermediate steps should have a target mimetype. Read from readFromB", registry.errorMessages.get(0)); assertEquals("Transformer \"5\" has no supported source and target mimetypes, so will be ignored. " - + "Read from readFromB", registry.errorMessages.get(1)); + + "Read from readFromB", registry.errorMessages.get(1)); } @Test @@ -668,7 +669,7 @@ public class CombinedTransformConfigTest .withTransformerPipeline(List.of( new TransformStep("1", "mimetype/b"), new TransformStep("2", "mimetype/c"), - new TransformStep("3", "mimetype/d"))) // the last step's mimetype should be null + new TransformStep("3", "mimetype/d"))) // the last step's mimetype should be null .build(); final TransformConfig transformConfig = TransformConfig.builder() .withTransformers(ImmutableList.of( @@ -683,9 +684,9 @@ public class CombinedTransformConfigTest assertEquals(2, registry.errorMessages.size()); assertEquals("No supported source and target mimetypes could be added to the transformer \"5\" as " + - "the final step should not have a target mimetype. Read from readFromB", registry.errorMessages.get(0)); + "the final step should not have a target mimetype. Read from readFromB", registry.errorMessages.get(0)); assertEquals("Transformer \"5\" has no supported source and target mimetypes, " - + "so will be ignored. Read from readFromB", registry.errorMessages.get(1)); + + "so will be ignored. Read from readFromB", registry.errorMessages.get(1)); } @Test @@ -747,10 +748,10 @@ public class CombinedTransformConfigTest .withTransformerPipeline(List.of( new TransformStep("2", "mimetype/c"), new TransformStep("3", null))) - .withSupportedSourceAndTargetList(Set.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("mimetype/a") - .withTargetMediaType("mimetype/d") + .withSupportedSourceAndTargetList(Set.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("mimetype/a") + .withTargetMediaType("mimetype/d") .build())) .build(); final TransformConfig transformConfig = TransformConfig.builder() @@ -767,11 +768,11 @@ public class CombinedTransformConfigTest assertEquals(0, registry.errorMessages.size()); assertEquals(3, config.buildTransformConfig().getTransformers().size()); assertEquals("1", registry.findTransformerName("mimetype/a", -1, - "mimetype/d", emptyMap(), null)); + "mimetype/d", emptyMap(), null)); assertEquals("2", registry.findTransformerName("mimetype/a", -1, - "mimetype/c", emptyMap(), null)); + "mimetype/c", emptyMap(), null)); assertEquals("3", registry.findTransformerName("mimetype/c", -1, - "mimetype/d", emptyMap(), null)); + "mimetype/d", emptyMap(), null)); } @Test @@ -798,7 +799,7 @@ public class CombinedTransformConfigTest assertEquals(0, registry.errorMessages.size()); assertEquals(3, config.buildTransformConfig().getTransformers().size()); assertEquals("1", registry.findTransformerName("mimetype/a", -1, - "mimetype/d", emptyMap(), null)); + "mimetype/d", emptyMap(), null)); } @Test @@ -810,16 +811,15 @@ public class CombinedTransformConfigTest .withTransformerPipeline(List.of( new TransformStep("2", "mimetype/c"), new TransformStep("2", null))) - .withSupportedSourceAndTargetList(Set.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("mimetype/a") - .withTargetMediaType("mimetype/d") - .build() - )) + .withSupportedSourceAndTargetList(Set.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("mimetype/a") + .withTargetMediaType("mimetype/d") + .build())) .build(); final TransformConfig transformConfig = TransformConfig.builder() .withTransformers(ImmutableList.of( - TRANSFORMER2_B2C, // Does NOT support a->c so steps are invalid for a->d + TRANSFORMER2_B2C, // Does NOT support a->c so steps are invalid for a->d TRANSFORMER3_C2D, pipeline)) .build(); @@ -833,9 +833,9 @@ public class CombinedTransformConfigTest assertEquals(expected, registry.errorMessages.get(0)); assertEquals(2, config.buildTransformConfig().getTransformers().size()); assertEquals("2", registry.findTransformerName("mimetype/b", -1, - "mimetype/c", emptyMap(), null)); + "mimetype/c", emptyMap(), null)); assertEquals("3", registry.findTransformerName("mimetype/c", -1, - "mimetype/d", emptyMap(), null)); + "mimetype/d", emptyMap(), null)); } @Test @@ -847,20 +847,19 @@ public class CombinedTransformConfigTest .withTransformerPipeline(List.of( new TransformStep("2", "mimetype/c"), new TransformStep("2", null))) - .withSupportedSourceAndTargetList(Set.of( - SupportedSourceAndTarget.builder() - .withSourceMediaType("mimetype/a") - .withTargetMediaType("mimetype/d") + .withSupportedSourceAndTargetList(Set.of( + SupportedSourceAndTarget.builder() + .withSourceMediaType("mimetype/a") + .withTargetMediaType("mimetype/d") .build(), - SupportedSourceAndTarget.builder() // Always going to be invalid with these steps - .withSourceMediaType("mimetype/x") - .withTargetMediaType("mimetype/y") - .build() - )) + SupportedSourceAndTarget.builder() // Always going to be invalid with these steps + .withSourceMediaType("mimetype/x") + .withTargetMediaType("mimetype/y") + .build())) .build(); final TransformConfig transformConfig = TransformConfig.builder() .withTransformers(ImmutableList.of( - TRANSFORMER2_B2C, // Does NOT support a->c so steps are invalid for a->d + TRANSFORMER2_B2C, // Does NOT support a->c so steps are invalid for a->d TRANSFORMER3_C2D, pipeline)) .build(); @@ -886,7 +885,7 @@ public class CombinedTransformConfigTest .build(); final TransformConfig transformConfig = TransformConfig.builder() .withTransformers(ImmutableList.of( - TRANSFORMER2_B2X, // Does NOT support ->c so steps are invalid + TRANSFORMER2_B2X, // Does NOT support ->c so steps are invalid TRANSFORMER3_C2D, pipeline)) .build(); @@ -897,11 +896,11 @@ public class CombinedTransformConfigTest assertEquals(2, registry.errorMessages.size()); assertEquals("No supported source and target mimetypes could be added to the transformer \"1\" as " - + "the first step transformer \"2\" does not support to \"mimetype/c\". Read from readFromB", - registry.errorMessages.get(0)); + + "the first step transformer \"2\" does not support to \"mimetype/c\". Read from readFromB", + registry.errorMessages.get(0)); assertEquals("Transformer \"1\" has no supported source and target mimetypes, so will be ignored. " - + "Read from readFromB", - registry.errorMessages.get(1)); + + "Read from readFromB", + registry.errorMessages.get(1)); assertEquals(2, config.buildTransformConfig().getTransformers().size()); } @@ -1028,9 +1027,9 @@ public class CombinedTransformConfigTest assertEquals(2, registry.errorMessages.size()); assertEquals(expectedWildcardError("the step transformer \"2\" does not support \"mimetype/b\" " - + "to \"mimetype/c\""), registry.errorMessages.get(0)); + + "to \"mimetype/c\""), registry.errorMessages.get(0)); assertEquals("Transformer \"4\" has no supported source and target mimetypes, so will be ignored. " - + "Read from readFromB", registry.errorMessages.get(1)); + + "Read from readFromB", registry.errorMessages.get(1)); // The pipeline is removed, so 3 are expected assertEquals(3, config.buildTransformConfig().getTransformers().size()); diff --git a/model/src/test/java/org/alfresco/transform/registry/FakeTransformRegistry.java b/model/src/test/java/org/alfresco/transform/registry/FakeTransformRegistry.java index 2714ed08..4da50a0b 100644 --- a/model/src/test/java/org/alfresco/transform/registry/FakeTransformRegistry.java +++ b/model/src/test/java/org/alfresco/transform/registry/FakeTransformRegistry.java @@ -21,15 +21,15 @@ */ package org.alfresco.transform.registry; -import org.alfresco.transform.config.TransformOption; -import org.alfresco.transform.config.Transformer; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.Transformer; + /** * Helper class for testing an {@link AbstractTransformRegistry}. */ @@ -69,8 +69,8 @@ public class FakeTransformRegistry extends AbstractTransformRegistry @Override protected void register(final Transformer transformer, - final Map> transformOptions, final String baseUrl, - final String readFrom) + final Map> transformOptions, final String baseUrl, + final String readFrom) { super.register(transformer, transformOptions, baseUrl, readFrom); diff --git a/model/src/test/java/org/alfresco/transform/registry/OverrideTransformConfigTests.java b/model/src/test/java/org/alfresco/transform/registry/OverrideTransformConfigTests.java index 640c8013..14050189 100644 --- a/model/src/test/java/org/alfresco/transform/registry/OverrideTransformConfigTests.java +++ b/model/src/test/java/org/alfresco/transform/registry/OverrideTransformConfigTests.java @@ -21,26 +21,26 @@ */ package org.alfresco.transform.registry; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import org.alfresco.transform.config.AddSupported; -import org.alfresco.transform.config.SupportedDefaults; -import org.alfresco.transform.config.OverrideSupported; -import org.alfresco.transform.config.RemoveSupported; -import org.alfresco.transform.config.SupportedSourceAndTarget; -import org.alfresco.transform.config.TransformConfig; -import org.alfresco.transform.config.Transformer; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.HashSet; import java.util.Set; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.Test; + +import org.alfresco.transform.config.AddSupported; +import org.alfresco.transform.config.OverrideSupported; +import org.alfresco.transform.config.RemoveSupported; +import org.alfresco.transform.config.SupportedDefaults; +import org.alfresco.transform.config.SupportedSourceAndTarget; +import org.alfresco.transform.config.TransformConfig; +import org.alfresco.transform.config.Transformer; /** - * Tests the json elements: {@code removeTransformers}, {@code addSupported}, {@code removeSupported}, - * {@code overrideSupported} and {@code supportedDefaults}. + * Tests the json elements: {@code removeTransformers}, {@code addSupported}, {@code removeSupported}, {@code overrideSupported} and {@code supportedDefaults}. */ public class OverrideTransformConfigTests { @@ -200,20 +200,20 @@ public class OverrideTransformConfigTests final TransformConfig thirdConfig = TransformConfig.builder() .withSupportedDefaults(ImmutableSet.of( - default_1A_200, // 0: transformer and source media type default - default_2A__45, // 0: transformer and source media type default - default_3_400, // 1: transformer default - default_B_400, // 2: source media type default - default_B_500, // 2: source media type default - overrides the previous value 400 defined in the same config + default_1A_200, // 0: transformer and source media type default + default_2A__45, // 0: transformer and source media type default + default_3_400, // 1: transformer default + default_B_400, // 2: source media type default + default_B_500, // 2: source media type default - overrides the previous value 400 defined in the same config default__500_50, // 3: system wide default - totally overridden by the next lines. - default__600, // 3: system wide default - default___50, // 3: system wide default (combined with the other system default) - default___45)) // 3: system wide default - overrides the value 45 defined in the same config + default__600, // 3: system wide default + default___50, // 3: system wide default (combined with the other system default) + default___45)) // 3: system wide default - overrides the value 45 defined in the same config .build(); final TransformConfig fourthConfig = TransformConfig.builder() .withSupportedDefaults(ImmutableSet.of( - SupportedDefaults.builder() // 3: system wide default + SupportedDefaults.builder() // 3: system wide default .withMaxSourceSizeBytes(-1L) .withPriority(45) .build())) @@ -221,16 +221,15 @@ public class OverrideTransformConfigTests final TransformConfig fifthConfig = TransformConfig.builder() .withSupportedDefaults(ImmutableSet.of( - SupportedDefaults.builder() // 3: system wide default (reset to the default, so removed) + SupportedDefaults.builder() // 3: system wide default (reset to the default, so removed) .withPriority(50) .build(), - SupportedDefaults.builder() // Invalid as neither priority nor maxSourceSizeBytes are set + SupportedDefaults.builder() // Invalid as neither priority nor maxSourceSizeBytes are set .withTransformerName("9") .withSourceMediaType("mimetype/z") .build())) .build(); - config.addTransformConfig(firstConfig, READ_FROM_A, BASE_URL_A, registry); TransformConfig resultConfig = config.buildTransformConfig(); assertEquals(0, resultConfig.getSupportedDefaults().size()); @@ -279,7 +278,7 @@ public class OverrideTransformConfigTests final TransformConfig secondConfig = TransformConfig.builder() .withRemoveSupported(ImmutableSet.of( RemoveSupported.builder() - .withTransformerName("1") // c -> d does not exist + .withTransformerName("1") // c -> d does not exist .withSourceMediaType("mimetype/c") .withTargetMediaType("mimetype/d") .build(), @@ -288,20 +287,20 @@ public class OverrideTransformConfigTests .withSourceMediaType("mimetype/a") .withTargetMediaType("mimetype/b") .build(), - RemoveSupported.builder() // transformer does not exist + RemoveSupported.builder() // transformer does not exist .withTransformerName("bad") .withSourceMediaType("mimetype/a") .withTargetMediaType("mimetype/d") .build(), - RemoveSupported.builder() // transform name not set + RemoveSupported.builder() // transform name not set .withSourceMediaType("mimetype/a") .withTargetMediaType("mimetype/d") .build(), - RemoveSupported.builder() // source type not set + RemoveSupported.builder() // source type not set .withTransformerName("1") .withTargetMediaType("mimetype/d") .build(), - RemoveSupported.builder() // target type not set + RemoveSupported.builder() // target type not set .withTransformerName("1") .withSourceMediaType("mimetype/a") .build())) @@ -334,7 +333,7 @@ public class OverrideTransformConfigTests .withSourceMediaType("mimetype/c") .withTargetMediaType("mimetype/d") .build(), - AddSupported.builder() // duplicates original + AddSupported.builder() // duplicates original .withTransformerName("1") .withSourceMediaType("mimetype/a") .withTargetMediaType("mimetype/b") @@ -347,20 +346,20 @@ public class OverrideTransformConfigTests .withPriority(44) .withMaxSourceSizeBytes(1234) .build(), - AddSupported.builder() // transformer does not exist + AddSupported.builder() // transformer does not exist .withTransformerName("bad") .withSourceMediaType("mimetype/a") .withTargetMediaType("mimetype/d") .build(), - AddSupported.builder() // transform name not set + AddSupported.builder() // transform name not set .withSourceMediaType("mimetype/a") .withTargetMediaType("mimetype/d") .build(), - AddSupported.builder() // source type not set + AddSupported.builder() // source type not set .withTransformerName("1") .withTargetMediaType("mimetype/d") .build(), - AddSupported.builder() // target type not set + AddSupported.builder() // target type not set .withTransformerName("1") .withSourceMediaType("mimetype/a") .build())) @@ -395,29 +394,29 @@ public class OverrideTransformConfigTests final TransformConfig secondConfig = TransformConfig.builder() .withOverrideSupported(ImmutableSet.of( - OverrideSupported.builder() // does not exist + OverrideSupported.builder() // does not exist .withTransformerName("1") .withSourceMediaType("mimetype/c") .withTargetMediaType("mimetype/d") .build(), - OverrideSupported.builder() // size default -> 200 and priority default -> 100 + OverrideSupported.builder() // size default -> 200 and priority default -> 100 .withTransformerName("1") .withSourceMediaType("mimetype/a") .withTargetMediaType("mimetype/b") .withPriority(40) .build(), - OverrideSupported.builder() // size 100 -> 200 and change priority to default + OverrideSupported.builder() // size 100 -> 200 and change priority to default .withTransformerName("1") .withSourceMediaType("mimetype/x") .withTargetMediaType("mimetype/y") .withMaxSourceSizeBytes(200) .build(), - OverrideSupported.builder() // transformer does not exist + OverrideSupported.builder() // transformer does not exist .withTransformerName("bad") .withSourceMediaType("mimetype/a") .withTargetMediaType("mimetype/d") .build())) - // OverrideSupported values with missing fields are defaults, so no test values here + // OverrideSupported values with missing fields are defaults, so no test values here .build(); String expectedWarnMessage = "Unable to process \"overrideSupported\": [" + @@ -444,7 +443,7 @@ public class OverrideTransformConfigTests } private void addTransformConfig(TransformConfig secondConfig, String expectedWarnMessage, - Set expectedSupported, String expectedToString) + Set expectedSupported, String expectedToString) { config.addTransformConfig(secondConfig, READ_FROM_B, BASE_URL_B, registry); TransformConfig resultConfig = config.buildTransformConfig(); diff --git a/model/src/test/java/org/alfresco/transform/registry/TransformRegistryHelperTest.java b/model/src/test/java/org/alfresco/transform/registry/TransformRegistryHelperTest.java index 49a16f2c..465ca775 100644 --- a/model/src/test/java/org/alfresco/transform/registry/TransformRegistryHelperTest.java +++ b/model/src/test/java/org/alfresco/transform/registry/TransformRegistryHelperTest.java @@ -26,21 +26,24 @@ */ package org.alfresco.transform.registry; -import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.exceptions.TransformException; -import org.junit.jupiter.api.Test; +import static java.util.Arrays.asList; +import static java.util.Collections.emptySet; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; +import static org.alfresco.transform.common.RequestParamMap.TIMEOUT; +import static org.alfresco.transform.registry.TransformRegistryHelper.retrieveTransformListBySize; import java.util.HashMap; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import static java.util.Arrays.asList; -import static java.util.Collections.emptySet; -import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; -import static org.alfresco.transform.common.RequestParamMap.TIMEOUT; -import static org.alfresco.transform.registry.TransformRegistryHelper.retrieveTransformListBySize; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import com.google.common.collect.ImmutableMap; +import org.junit.jupiter.api.Test; + +import org.alfresco.transform.exceptions.TransformException; public class TransformRegistryHelperTest { @@ -50,37 +53,37 @@ public class TransformRegistryHelperTest // This test was inspired by a failure to pick libreoffice over textToPdf despite the fact libreoffice has a // higher priority. SupportedTransform libreoffice = new SupportedTransform("libreoffice", emptySet(), -1, 50); - SupportedTransform textToPdf = new SupportedTransform("textToPdf", emptySet(), 100,55); + SupportedTransform textToPdf = new SupportedTransform("textToPdf", emptySet(), 100, 55); assertOrder(asList(libreoffice, textToPdf), asList(libreoffice)); assertOrder(asList(textToPdf, libreoffice), asList(libreoffice)); // * If multiple transforms with the same priority can support the same size, the one with the highest size - // limit (or no limit) is used. + // limit (or no limit) is used. // * Transforms with a higher priority (lower numerically) are used up to their size limit in preference to - // lower priority transforms. These lower priority transforms will be used above that limit. + // lower priority transforms. These lower priority transforms will be used above that limit. // * If there are multiple transforms with the same priority and size limit, the last one defined is used to - // allow extensions to override standard transforms. + // allow extensions to override standard transforms. // * In each of the above cases, it is possible for supplied transforms not to be returned from - // retrieveTransformListBySize as they will never be used. However this method is currently only used - // by (1) AbstractTransformRegistry.findTransformerName which filters out transformers that cannot support a - // given size and then uses the lowest element and (2) AbstractTransformRegistry.findMaxSize and gets the last - // element without filtering and returns its size limit. So there are opportunities to change the code so that - // it does not actually have to remove transformers that will not be used. + // retrieveTransformListBySize as they will never be used. However this method is currently only used + // by (1) AbstractTransformRegistry.findTransformerName which filters out transformers that cannot support a + // given size and then uses the lowest element and (2) AbstractTransformRegistry.findMaxSize and gets the last + // element without filtering and returns its size limit. So there are opportunities to change the code so that + // it does not actually have to remove transformers that will not be used. // Test transforms - SupportedTransform p45 = new SupportedTransform( "p45", emptySet(), -1, 45); - SupportedTransform p50 = new SupportedTransform( "p50", emptySet(), -1, 50); - SupportedTransform p55 = new SupportedTransform( "p55", emptySet(), -1, 55); - SupportedTransform s100p45 = new SupportedTransform("s100p45", emptySet(), 100, 45); - SupportedTransform s100p50 = new SupportedTransform("s100p50", emptySet(), 100, 50); - SupportedTransform s100p55 = new SupportedTransform("s100p55", emptySet(), 100, 55); - SupportedTransform s200p50 = new SupportedTransform("s200p50", emptySet(), 200, 50); + SupportedTransform p45 = new SupportedTransform("p45", emptySet(), -1, 45); + SupportedTransform p50 = new SupportedTransform("p50", emptySet(), -1, 50); + SupportedTransform p55 = new SupportedTransform("p55", emptySet(), -1, 55); + SupportedTransform s100p45 = new SupportedTransform("s100p45", emptySet(), 100, 45); + SupportedTransform s100p50 = new SupportedTransform("s100p50", emptySet(), 100, 50); + SupportedTransform s100p55 = new SupportedTransform("s100p55", emptySet(), 100, 55); + SupportedTransform s200p50 = new SupportedTransform("s200p50", emptySet(), 200, 50); SupportedTransform s200p50b = new SupportedTransform("s200p50b", emptySet(), 200, 50); - SupportedTransform s200p55 = new SupportedTransform("s200p55", emptySet(), 200, 55); - SupportedTransform s300p45 = new SupportedTransform("s300p45", emptySet(), 300, 45); - SupportedTransform s300p50 = new SupportedTransform("s300p50", emptySet(), 300, 50); - SupportedTransform s300p55 = new SupportedTransform("s300p55", emptySet(), 300, 55); + SupportedTransform s200p55 = new SupportedTransform("s200p55", emptySet(), 200, 55); + SupportedTransform s300p45 = new SupportedTransform("s300p45", emptySet(), 300, 45); + SupportedTransform s300p50 = new SupportedTransform("s300p50", emptySet(), 300, 50); + SupportedTransform s300p55 = new SupportedTransform("s300p55", emptySet(), 300, 55); // Just considers the priority assertOrder(asList(p50), asList(p50)); @@ -125,8 +128,8 @@ public class TransformRegistryHelperTest { AtomicInteger transformerCount = new AtomicInteger(0); TransformCache data = new TransformCache(); - transformsInLoadOrder.forEach(t->data.appendTransform("text/plain", "application/pdf", t, - "transformer"+transformerCount.getAndIncrement(), null)); + transformsInLoadOrder.forEach(t -> data.appendTransform("text/plain", "application/pdf", t, + "transformer" + transformerCount.getAndIncrement(), null)); List supportedTransforms = retrieveTransformListBySize(data, "text/plain", "application/pdf", null, null); @@ -160,8 +163,7 @@ public class TransformRegistryHelperTest // Similar to the method in AbstractTransformRegistry private long findMaxSize(List supportedTransforms) { - return supportedTransforms.isEmpty() ? 0 : - supportedTransforms.get(supportedTransforms.size() - 1).getMaxSourceSizeBytes(); + return supportedTransforms.isEmpty() ? 0 : supportedTransforms.get(supportedTransforms.size() - 1).getMaxSourceSizeBytes(); } @Test @@ -169,8 +171,7 @@ public class TransformRegistryHelperTest { TransformCache data = new TransformCache(); - assertThrows(TransformException.class, () -> - { + assertThrows(TransformException.class, () -> { retrieveTransformListBySize(data, null, "application/pdf", null, null); }); } @@ -180,8 +181,7 @@ public class TransformRegistryHelperTest { TransformCache data = new TransformCache(); - assertThrows(TransformException.class, () -> - { + assertThrows(TransformException.class, () -> { retrieveTransformListBySize(data, "text/plain", null, null, null); }); } @@ -192,10 +192,9 @@ public class TransformRegistryHelperTest // Almost identical to buildTransformListTargetMimeTypeNullErrorTest TransformCache data = new TransformCache(); - assertThrows(TransformException.class, () -> - { + assertThrows(TransformException.class, () -> { retrieveTransformListBySize(data, "text/plain", null, - new HashMap<>(ImmutableMap.of(TIMEOUT, "1234")), null); + new HashMap<>(ImmutableMap.of(TIMEOUT, "1234")), null); }); } @@ -205,10 +204,9 @@ public class TransformRegistryHelperTest // Almost identical to buildTransformListTargetMimeTypeNullErrorTest TransformCache data = new TransformCache(); - assertThrows(TransformException.class, () -> - { + assertThrows(TransformException.class, () -> { retrieveTransformListBySize(data, "text/plain", null, - new HashMap<>(ImmutableMap.of(SOURCE_ENCODING, "UTF-8")), null); + new HashMap<>(ImmutableMap.of(SOURCE_ENCODING, "UTF-8")), null); }); } } diff --git a/model/src/test/java/org/alfresco/transform/registry/TransformRegistryModelTest.java b/model/src/test/java/org/alfresco/transform/registry/TransformRegistryModelTest.java index 65947313..654bb979 100644 --- a/model/src/test/java/org/alfresco/transform/registry/TransformRegistryModelTest.java +++ b/model/src/test/java/org/alfresco/transform/registry/TransformRegistryModelTest.java @@ -28,13 +28,15 @@ import static java.util.Collections.singletonList; import static java.util.Collections.singletonMap; import static java.util.function.Function.identity; import static java.util.stream.Collectors.toMap; -import static org.alfresco.transform.registry.TransformRegistryHelper.addToPossibleTransformOptions; -import static org.alfresco.transform.registry.TransformRegistryHelper.optionsMatch; + import static org.apache.commons.lang3.StringUtils.isBlank; 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.alfresco.transform.registry.TransformRegistryHelper.addToPossibleTransformOptions; +import static org.alfresco.transform.registry.TransformRegistryHelper.optionsMatch; + import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -43,6 +45,10 @@ import java.util.Map; import java.util.Set; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import org.alfresco.transform.config.CoreFunction; import org.alfresco.transform.config.SupportedSourceAndTarget; import org.alfresco.transform.config.TransformConfig; @@ -50,14 +56,9 @@ import org.alfresco.transform.config.TransformOption; import org.alfresco.transform.config.TransformOptionGroup; import org.alfresco.transform.config.TransformOptionValue; import org.alfresco.transform.config.Transformer; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import com.google.common.collect.ImmutableSet; /** - * Test the AbstractTransformRegistry, extended by both T-Engines and ACS repository, which need to - * read JSON config to understand what is supported. + * Test the AbstractTransformRegistry, extended by both T-Engines and ACS repository, which need to read JSON config to understand what is supported. * * @author adavis */ @@ -84,8 +85,7 @@ public class TransformRegistryModelTest protected AbstractTransformRegistry buildTransformServiceRegistryImpl() throws Exception { - return new AbstractTransformRegistry() - { + return new AbstractTransformRegistry() { private TransformCache data = new TransformCache(); @Override @@ -109,13 +109,13 @@ public class TransformRegistryModelTest } private void assertAddToPossibleOptions(final TransformOptionGroup transformOptionGroup, - final Set actualOptionNames, final Set expectedNameSet, - final Set expectedRequiredSet) + final Set actualOptionNames, final Set expectedNameSet, + final Set expectedRequiredSet) { final Map possibleTransformOptions = new HashMap<>(); addToPossibleTransformOptions(possibleTransformOptions, transformOptionGroup, true, - buildActualOptions(actualOptionNames)); + buildActualOptions(actualOptionNames)); assertEquals(expectedNameSet, possibleTransformOptions.keySet()); @@ -133,11 +133,11 @@ public class TransformRegistryModelTest // transformOptionNames are upper case if required. private void assertOptionsMatch(final Set actualOptionNames, - final Set transformOptionNames, final String unsupportedMsg) + final Set transformOptionNames, final String unsupportedMsg) { final Map transformOptions = transformOptionNames - .stream() - .collect(toMap(identity(), name -> name.toUpperCase().equals(name))); + .stream() + .collect(toMap(identity(), name -> name.toUpperCase().equals(name))); boolean supported = optionsMatch(transformOptions, buildActualOptions(actualOptionNames)); if (isBlank(unsupportedMsg)) @@ -153,20 +153,20 @@ public class TransformRegistryModelTest private void assertTransformOptions(Set setOfTransformOptions) throws Exception { final Transformer transformer = new Transformer("name", singleton("testOptions"), set( - SupportedSourceAndTarget.builder() - .withSourceMediaType(DOC) - .withTargetMediaType(TXT) - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType(XLS) - .withTargetMediaType(TXT) - .withMaxSourceSizeBytes(1024000L) - .build())); + SupportedSourceAndTarget.builder() + .withSourceMediaType(DOC) + .withTargetMediaType(TXT) + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType(XLS) + .withTargetMediaType(TXT) + .withMaxSourceSizeBytes(1024000L) + .build())); final TransformConfig transformConfig = TransformConfig - .builder() - .withTransformers(singletonList(transformer)) - .withTransformOptions(singletonMap("testOptions", setOfTransformOptions)) - .build(); + .builder() + .withTransformers(singletonList(transformer)) + .withTransformOptions(singletonMap("testOptions", setOfTransformOptions)) + .build(); registry = buildTransformServiceRegistryImpl(); CombinedTransformConfig.combineAndRegister(transformConfig, getClass().getName(), getBaseUrl(transformer), registry); @@ -183,30 +183,30 @@ public class TransformRegistryModelTest } private void assertTransformerName(String sourceMimetype, long sourceSizeInBytes, - String targetMimetype, Map actualOptions, String expectedTransformerName, - Transformer... transformers) throws Exception + String targetMimetype, Map actualOptions, String expectedTransformerName, + Transformer... transformers) throws Exception { buildAndPopulateRegistry(transformers); String transformerName = registry.findTransformerName(sourceMimetype, sourceSizeInBytes, - targetMimetype, actualOptions, null); + targetMimetype, actualOptions, null); assertEquals(expectedTransformerName, transformerName); } private void assertSupported(final Transformer transformer, final String sourceMimetype, - final long sourceSizeInBytes, final String targetMimetype, - final Map actualOptions, final String unsupportedMsg) throws Exception + final long sourceSizeInBytes, final String targetMimetype, + final Map actualOptions, final String unsupportedMsg) throws Exception { assertSupported(sourceMimetype, sourceSizeInBytes, targetMimetype, actualOptions, - unsupportedMsg, transformer); + unsupportedMsg, transformer); } private void assertSupported(String sourceMimetype, long sourceSizeInBytes, - String targetMimetype, Map actualOptions, String unsupportedMsg, - Transformer... transformers) throws Exception + String targetMimetype, Map actualOptions, String unsupportedMsg, + Transformer... transformers) throws Exception { buildAndPopulateRegistry(transformers); assertSupported(sourceMimetype, sourceSizeInBytes, targetMimetype, actualOptions, null, - unsupportedMsg); + unsupportedMsg); } private void buildAndPopulateRegistry(Transformer[] transformers) throws Exception @@ -220,11 +220,11 @@ public class TransformRegistryModelTest } protected void assertSupported(String sourceMimetype, long sourceSizeInBytes, - String targetMimetype, Map actualOptions, String renditionName, - String unsupportedMsg) + String targetMimetype, Map actualOptions, String renditionName, + String unsupportedMsg) { boolean supported = registry.isSupported(sourceMimetype, sourceSizeInBytes, targetMimetype, - actualOptions, renditionName); + actualOptions, renditionName); if (unsupportedMsg == null || unsupportedMsg.isEmpty()) { assertTrue(supported); @@ -238,151 +238,147 @@ public class TransformRegistryModelTest private static Map buildActualOptions(final Set optionNames) { return optionNames - .stream() - .collect(toMap(identity(), name -> "value for " + name)); + .stream() + .collect(toMap(identity(), name -> "value for " + name)); } @Test public void testOptionalGroups() { - final TransformOptionGroup transformOptionGroup = - new TransformOptionGroup(true, set( + final TransformOptionGroup transformOptionGroup = new TransformOptionGroup(true, set( new TransformOptionValue(false, "1"), new TransformOptionValue(true, "2"), new TransformOptionGroup(false, set( - new TransformOptionValue(false, "3.1"), - new TransformOptionValue(false, "3.2"), - new TransformOptionValue(false, "3.3"))), + new TransformOptionValue(false, "3.1"), + new TransformOptionValue(false, "3.2"), + new TransformOptionValue(false, "3.3"))), new TransformOptionGroup(false, set( // OPTIONAL - new TransformOptionValue(false, "4.1"), - new TransformOptionValue(true, "4.2"), - new TransformOptionValue(false, "4.3"))))); + new TransformOptionValue(false, "4.1"), + new TransformOptionValue(true, "4.2"), + new TransformOptionValue(false, "4.3"))))); assertAddToPossibleOptions(transformOptionGroup, emptySet(), - set("1", "2"), set("2")); + set("1", "2"), set("2")); assertAddToPossibleOptions(transformOptionGroup, set("1"), - set("1", "2"), set("2")); + set("1", "2"), set("2")); assertAddToPossibleOptions(transformOptionGroup, set("2"), - set("1", "2"), set("2")); + set("1", "2"), set("2")); assertAddToPossibleOptions(transformOptionGroup, set("2", "3.2"), - set("1", "2", "3.1", "3.2", "3.3"), set("2")); + set("1", "2", "3.1", "3.2", "3.3"), set("2")); assertAddToPossibleOptions(transformOptionGroup, set("2", "4.1"), - set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); + set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); assertAddToPossibleOptions(transformOptionGroup, set("2", "4.2"), - set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); + set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); } @Test public void testRequiredGroup() { - TransformOptionGroup transformOptionGroup = - new TransformOptionGroup(true, set( + TransformOptionGroup transformOptionGroup = new TransformOptionGroup(true, set( new TransformOptionValue(false, "1"), new TransformOptionValue(true, "2"), new TransformOptionGroup(false, set( - new TransformOptionValue(false, "3.1"), - new TransformOptionValue(false, "3.2"), - new TransformOptionValue(false, "3.3"))), + new TransformOptionValue(false, "3.1"), + new TransformOptionValue(false, "3.2"), + new TransformOptionValue(false, "3.3"))), new TransformOptionGroup(true, set( - new TransformOptionValue(false, "4.1"), - new TransformOptionValue(true, "4.2"), - new TransformOptionValue(false, "4.3"))))); + new TransformOptionValue(false, "4.1"), + new TransformOptionValue(true, "4.2"), + new TransformOptionValue(false, "4.3"))))); assertAddToPossibleOptions(transformOptionGroup, emptySet(), - set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); + set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); assertAddToPossibleOptions(transformOptionGroup, set("1"), - set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); + set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); assertAddToPossibleOptions(transformOptionGroup, set("2", "3.2"), - set("1", "2", "3.1", "3.2", "3.3", "4.1", "4.2", "4.3"), set("2", "4.2")); + set("1", "2", "3.1", "3.2", "3.3", "4.1", "4.2", "4.3"), set("2", "4.2")); assertAddToPossibleOptions(transformOptionGroup, set("2", "4.1"), - set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); + set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); assertAddToPossibleOptions(transformOptionGroup, set("2", "4.2"), - set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); + set("1", "2", "4.1", "4.2", "4.3"), set("2", "4.2")); } @Test public void testNestedGroups() { - TransformOptionGroup transformOptionGroup = - new TransformOptionGroup(false, set( + TransformOptionGroup transformOptionGroup = new TransformOptionGroup(false, set( new TransformOptionGroup(false, set( - new TransformOptionValue(false, "1"), - new TransformOptionGroup(false, set( - new TransformOptionValue(false, "1.2"), + new TransformOptionValue(false, "1"), new TransformOptionGroup(false, set( - new TransformOptionValue(false, "1.2.3"))))))), + new TransformOptionValue(false, "1.2"), + new TransformOptionGroup(false, set( + new TransformOptionValue(false, "1.2.3"))))))), new TransformOptionGroup(false, set( - new TransformOptionValue(false, "2"), - new TransformOptionGroup(false, set( - new TransformOptionValue(false, "2.2"), + new TransformOptionValue(false, "2"), new TransformOptionGroup(false, set( - new TransformOptionGroup(false, set( - new TransformOptionValue(false, "2.2.1.2"))))))))), + new TransformOptionValue(false, "2.2"), + new TransformOptionGroup(false, set( + new TransformOptionGroup(false, set( + new TransformOptionValue(false, "2.2.1.2"))))))))), new TransformOptionGroup(false, set( - new TransformOptionValue(true, "3"), - new TransformOptionGroup(false, set( + new TransformOptionValue(true, "3"), new TransformOptionGroup(false, set( - new TransformOptionGroup(false, set( - new TransformOptionValue(false, "3.1.1.2"))))))))), + new TransformOptionGroup(false, set( + new TransformOptionGroup(false, set( + new TransformOptionValue(false, "3.1.1.2"))))))))), new TransformOptionGroup(false, set( - new TransformOptionValue(false, "4"), - new TransformOptionGroup(true, set( - new TransformOptionGroup(false, set( - new TransformOptionGroup(false, set( - new TransformOptionValue(false, "4.1.1.2"))))))))), - new TransformOptionGroup(false, set( - new TransformOptionValue(false, "5"), - new TransformOptionGroup(false, set( + new TransformOptionValue(false, "4"), new TransformOptionGroup(true, set( - new TransformOptionGroup(false, set( - new TransformOptionValue(false, "5.1.1.2"))))))))), + new TransformOptionGroup(false, set( + new TransformOptionGroup(false, set( + new TransformOptionValue(false, "4.1.1.2"))))))))), new TransformOptionGroup(false, set( - new TransformOptionValue(false, "6"), - new TransformOptionGroup(false, set( + new TransformOptionValue(false, "5"), new TransformOptionGroup(false, set( - new TransformOptionGroup(true, set( - new TransformOptionValue(false, "6.1.1.2"))))))))), + new TransformOptionGroup(true, set( + new TransformOptionGroup(false, set( + new TransformOptionValue(false, "5.1.1.2"))))))))), new TransformOptionGroup(false, set( - new TransformOptionValue(false, "7"), - new TransformOptionGroup(false, set( + new TransformOptionValue(false, "6"), new TransformOptionGroup(false, set( - new TransformOptionGroup(false, set( - new TransformOptionValue(true, "7.1.1.2"))))))))) - )); + new TransformOptionGroup(false, set( + new TransformOptionGroup(true, set( + new TransformOptionValue(false, "6.1.1.2"))))))))), + new TransformOptionGroup(false, set( + new TransformOptionValue(false, "7"), + new TransformOptionGroup(false, set( + new TransformOptionGroup(false, set( + new TransformOptionGroup(false, set( + new TransformOptionValue(true, "7.1.1.2"))))))))))); assertAddToPossibleOptions(transformOptionGroup, emptySet(), - emptySet(), emptySet()); + emptySet(), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("1"), - set("1"), emptySet()); + set("1"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("1", "7"), - set("1", "7"), emptySet()); + set("1", "7"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("1", "7.1.1.2"), - set("1", "7", "7.1.1.2"), set("7.1.1.2")); + set("1", "7", "7.1.1.2"), set("7.1.1.2")); assertAddToPossibleOptions(transformOptionGroup, set("1", "6"), - set("1", "6"), emptySet()); + set("1", "6"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("1", "6.1.1.2"), - set("1", "6", "6.1.1.2"), emptySet()); + set("1", "6", "6.1.1.2"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("1", "5"), - set("1", "5"), emptySet()); + set("1", "5"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("1", "5.1.1.2"), - set("1", "5", "5.1.1.2"), emptySet()); + set("1", "5", "5.1.1.2"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("1", "4"), - set("1", "4"), emptySet()); + set("1", "4"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("1", "4.1.1.2"), - set("1", "4", "4.1.1.2"), emptySet()); + set("1", "4", "4.1.1.2"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("1", "3"), - set("1", "3"), set("3")); + set("1", "3"), set("3")); assertAddToPossibleOptions(transformOptionGroup, set("1", "3.1.1.2"), - set("1", "3", "3.1.1.2"), set("3")); + set("1", "3", "3.1.1.2"), set("3")); assertAddToPossibleOptions(transformOptionGroup, set("2"), - set("2"), emptySet()); + set("2"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("2", "2.2"), - set("2", "2.2"), emptySet()); + set("2", "2.2"), emptySet()); assertAddToPossibleOptions(transformOptionGroup, set("3"), - set("3"), set("3")); + set("3"), set("3")); assertAddToPossibleOptions(transformOptionGroup, set("3.1.1.2"), - set("3", "3.1.1.2"), set("3")); + set("3", "3.1.1.2"), set("3")); } @Test @@ -397,7 +393,7 @@ public class TransformRegistryModelTest assertOptionsMatch(set("B", "d"), set("a", "B", "c"), "there is an extra option d"); assertOptionsMatch(set("B", "c", "d"), set("a", "B", "c"), "there is an extra option d"); assertOptionsMatch(set("d"), set("a", "B", "c"), - "required option B is missing and there is an extra option d"); + "required option B is missing and there is an extra option d"); assertOptionsMatch(set("a"), set("a", "b", "c"), null); assertOptionsMatch(emptySet(), set("a", "b", "c"), null); @@ -408,8 +404,8 @@ public class TransformRegistryModelTest public void testNoActualOptions() throws Exception { assertTransformOptions(set( - new TransformOptionValue(false, "option1"), - new TransformOptionValue(false, "option2"))); + new TransformOptionValue(false, "option1"), + new TransformOptionValue(false, "option2"))); } @Test @@ -423,38 +419,38 @@ public class TransformRegistryModelTest public void testSupported() throws Exception { mapOfTransformOptions.put("options1", set( - new TransformOptionValue(false, "page"), - new TransformOptionValue(false, "width"), - new TransformOptionValue(false, "height"))); + new TransformOptionValue(false, "page"), + new TransformOptionValue(false, "width"), + new TransformOptionValue(false, "height"))); final Transformer transformer = new Transformer("name", singleton("options1"), set( - SupportedSourceAndTarget.builder() - .withSourceMediaType(DOC) - .withTargetMediaType(GIF) - .withMaxSourceSizeBytes(102400L) - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType(DOC) - .withTargetMediaType(JPEG) - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType(MSG) - .withTargetMediaType(GIF) - .build())); + SupportedSourceAndTarget.builder() + .withSourceMediaType(DOC) + .withTargetMediaType(GIF) + .withMaxSourceSizeBytes(102400L) + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType(DOC) + .withTargetMediaType(JPEG) + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType(MSG) + .withTargetMediaType(GIF) + .build())); assertSupported(transformer, DOC, 1024, GIF, emptyMap(), null); assertSupported(transformer, DOC, 102400, GIF, emptyMap(), null); assertSupported(transformer, DOC, 102401, GIF, emptyMap(), "source is too large"); assertSupported(transformer, DOC, 1024, JPEG, emptyMap(), null); assertSupported(transformer, GIF, 1024, DOC, emptyMap(), - GIF + " is not a source of this transformer"); + GIF + " is not a source of this transformer"); assertSupported(transformer, MSG, 1024, GIF, emptyMap(), null); assertSupported(transformer, MSG, 1024, JPEG, emptyMap(), - MSG + " to " + JPEG + " is not supported by this transformer"); + MSG + " to " + JPEG + " is not supported by this transformer"); assertSupported(transformer, DOC, 1024, GIF, buildActualOptions(set("page", "width")), - null); + null); assertSupported(transformer, DOC, 1024, GIF, - buildActualOptions(set("page", "width", "startPage")), "startPage is not an option"); + buildActualOptions(set("page", "width", "startPage")), "startPage is not an option"); } @Test @@ -463,20 +459,20 @@ public class TransformRegistryModelTest public void testCache() { mapOfTransformOptions.put("options1", set( - new TransformOptionValue(false, "page"), - new TransformOptionValue(false, "width"), - new TransformOptionValue(false, "height"))); + new TransformOptionValue(false, "page"), + new TransformOptionValue(false, "width"), + new TransformOptionValue(false, "height"))); final Transformer transformer = new Transformer("name", singleton("options1"), set( - SupportedSourceAndTarget.builder() - .withSourceMediaType(DOC) - .withTargetMediaType(GIF) - .withMaxSourceSizeBytes(102400L) - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType(MSG) - .withTargetMediaType(GIF) - .build())); + SupportedSourceAndTarget.builder() + .withSourceMediaType(DOC) + .withTargetMediaType(GIF) + .withMaxSourceSizeBytes(102400L) + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType(MSG) + .withTargetMediaType(GIF) + .build())); TransformConfig transformConfig = TransformConfig.builder() .withTransformers(Collections.singletonList(transformer)) @@ -492,7 +488,7 @@ public class TransformRegistryModelTest // check we are now using the cached value. final SupportedTransform cachedSupportedTransform = new SupportedTransform("name1", - emptySet(), 999999L, 0); + emptySet(), 999999L, 0); registry.getData() .retrieveCached("doclib", DOC) @@ -504,15 +500,15 @@ public class TransformRegistryModelTest public void testTransformCacheGetTransforms() // Used in the Alfresco Repo tests { TransformConfig transformConfig = TransformConfig.builder() - .withTransformers(ImmutableList.of(new Transformer("transformer1", emptySet(), set( - SupportedSourceAndTarget.builder() - .withSourceMediaType(GIF) - .withTargetMediaType(PDF) - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType(GIF) - .withTargetMediaType(JPEG) - .build())))) + .withTransformers(ImmutableList.of(new Transformer("transformer1", emptySet(), set( + SupportedSourceAndTarget.builder() + .withSourceMediaType(GIF) + .withTargetMediaType(PDF) + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType(GIF) + .withTargetMediaType(JPEG) + .build())))) .build(); assertEquals(0, registry.getData().getTransforms().size()); @@ -539,8 +535,8 @@ public class TransformRegistryModelTest assertTransformerName(MSG, 150, GIF, emptyMap(), "transformer2", t1, t2); assertTransformerName(MSG, 250, GIF, emptyMap(), null, t1, t2); // Select on priority - t1, t2 and t4 are discarded. - // t3 is a higher priority and has a larger size than t1 and t2. - // Similar story fo t4 with t5. + // t3 is a higher priority and has a larger size than t1 and t2. + // Similar story fo t4 with t5. assertTransformerName(MSG, 100, GIF, emptyMap(), "transformer3", t1, t2, t3, t4, t5); assertTransformerName(MSG, 200, GIF, emptyMap(), "transformer3", t1, t2, t3, t4, t5); // Select on size and priority, t1 and t2 discarded @@ -550,7 +546,7 @@ public class TransformRegistryModelTest } private Transformer newTransformer(String transformerName, String sourceMediaType, String targetMediaType, - long maxSourceSizeBytes, int priority) + long maxSourceSizeBytes, int priority) { return Transformer.builder().withTransformerName(transformerName) .withSupportedSourceAndTargetList(ImmutableSet.of( @@ -567,68 +563,68 @@ public class TransformRegistryModelTest public void testMultipleTransformers() throws Exception { mapOfTransformOptions.put("options1", set( - new TransformOptionValue(false, "page"), - new TransformOptionValue(false, "width"), - new TransformOptionValue(false, "height"))); + new TransformOptionValue(false, "page"), + new TransformOptionValue(false, "width"), + new TransformOptionValue(false, "height"))); mapOfTransformOptions.put("options2", set( - new TransformOptionValue(false, "opt1"), - new TransformOptionValue(false, "opt2"))); + new TransformOptionValue(false, "opt1"), + new TransformOptionValue(false, "opt2"))); mapOfTransformOptions.put("options3", new HashSet<>(singletonList( - new TransformOptionValue(false, "opt1")))); + new TransformOptionValue(false, "opt1")))); Transformer transformer1 = new Transformer("transformer1", singleton("options1"), set( - SupportedSourceAndTarget.builder() - .withSourceMediaType(DOC) - .withTargetMediaType(GIF) - .withMaxSourceSizeBytes(102400L) - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType(DOC) - .withTargetMediaType(JPEG) - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType(MSG) - .withTargetMediaType(GIF) - .build())); + SupportedSourceAndTarget.builder() + .withSourceMediaType(DOC) + .withTargetMediaType(GIF) + .withMaxSourceSizeBytes(102400L) + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType(DOC) + .withTargetMediaType(JPEG) + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType(MSG) + .withTargetMediaType(GIF) + .build())); Transformer transformer2 = new Transformer("transformer2", singleton("options2"), set( - SupportedSourceAndTarget.builder() - .withSourceMediaType(PDF) - .withTargetMediaType(GIF) - .build(), - SupportedSourceAndTarget.builder() - .withSourceMediaType(PPT) - .withTargetMediaType(JPEG) - .build())); + SupportedSourceAndTarget.builder() + .withSourceMediaType(PDF) + .withTargetMediaType(GIF) + .build(), + SupportedSourceAndTarget.builder() + .withSourceMediaType(PPT) + .withTargetMediaType(JPEG) + .build())); Transformer transformer3 = new Transformer("transformer3", singleton("options3"), - new HashSet(singletonList(SupportedSourceAndTarget.builder() - .withSourceMediaType(DOC) - .withTargetMediaType(GIF) - .build()))); + new HashSet(singletonList(SupportedSourceAndTarget.builder() + .withSourceMediaType(DOC) + .withTargetMediaType(GIF) + .build()))); assertSupported(DOC, 1024, GIF, emptyMap(), null, transformer1); assertSupported(DOC, 1024, GIF, emptyMap(), null, transformer1, transformer2); assertSupported(DOC, 1024, GIF, emptyMap(), null, transformer1, transformer2, - transformer3); + transformer3); assertSupported(DOC, 102401, GIF, emptyMap(), "source is too large", transformer1); assertSupported(DOC, 102401, GIF, emptyMap(), null, transformer1, transformer3); assertSupported(PDF, 1024, GIF, emptyMap(), "Only transformer2 supports these mimetypes", - transformer1); + transformer1); assertSupported(PDF, 1024, GIF, emptyMap(), null, transformer1, transformer2); assertSupported(PDF, 1024, GIF, emptyMap(), null, transformer1, transformer2, - transformer3); + transformer3); final Map actualOptions = buildActualOptions(set("opt1")); assertSupported(PDF, 1024, GIF, actualOptions, "Only transformer2/4 supports these options", - transformer1); + transformer1); assertSupported(PDF, 1024, GIF, actualOptions, null, transformer1, transformer2); assertSupported(PDF, 1024, GIF, actualOptions, null, transformer1, transformer2, - transformer3); + transformer3); assertSupported(PDF, 1024, GIF, actualOptions, - "transformer4 supports opt1 but not the source mimetype ", transformer1, transformer3); + "transformer4 supports opt1 but not the source mimetype ", transformer1, transformer3); } @SafeVarargs @@ -650,7 +646,7 @@ public class TransformRegistryModelTest t2.setCoreVersion("1.0"); t3.setCoreVersion("2.5.7"); - buildAndPopulateRegistry(new Transformer[] {t1, t2, t3}); + buildAndPopulateRegistry(new Transformer[]{t1, t2, t3}); assertTrue(registry.isSupported(CoreFunction.HTTP, "transformer1")); assertTrue(registry.isSupported(CoreFunction.HTTP, "transformer2")); diff --git a/pom.xml b/pom.xml index febb34e9..109b1609 100644 --- a/pom.xml +++ b/pom.xml @@ -34,8 +34,6 @@ 1.79 2.45.0 - - NO_AUTOMATED_FORMATTING false @@ -257,10 +255,6 @@ ${parent.core.deploy.skip} - - com.diffplug.spotless - spotless-maven-plugin - @@ -378,11 +372,8 @@ ${spotless-plugin.version} - - ${spotless-include-list} - - alfresco-formatter.xml + https://raw.githubusercontent.com/Alfresco/alfresco-community-repo/refs/heads/master/alfresco-formatter.xml \#java|\#javax|\#jakarta,\#,\#org.alfresco,java|javax|jakarta,,org.alfresco diff --git a/scripts/check-format-and-headers.sh b/scripts/check-format-and-headers.sh deleted file mode 100755 index 7cf66260..00000000 --- a/scripts/check-format-and-headers.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash - -if [[ -z ${GITHUB_MODIFIED_FILES} ]] -then - modified_files=$(git diff --cached --name-only --diff-filter=ACMR) -else - modified_files=${GITHUB_MODIFIED_FILES} -fi - -if [[ -z ${modified_files} ]] -then - echo "No modified files, exiting." - exit 0 -fi - -include_list="" -for file in ${modified_files} -do - include_list="${include_list},${file}" -done -include_list=${include_list:1} - -mvn spotless:apply validate -DlicenseUpdateHeaders=true -Dspotless-include-list="${include_list}" > /dev/null || true diff --git a/scripts/hooks/check-format-and-headers.sh b/scripts/hooks/check-format-and-headers.sh new file mode 100755 index 00000000..2b047d38 --- /dev/null +++ b/scripts/hooks/check-format-and-headers.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set +x + +mvn spotless:apply validate -DlicenseUpdateHeaders=true > /dev/null || true + +set -x