Compare commits
88
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d86afba440 | ||
|
|
ebab5a4456 | ||
|
|
436c8cbed2 | ||
|
|
81dc90cd6d | ||
|
|
917edc43b3 | ||
|
|
3c48740c5e | ||
|
|
2c940569d1 | ||
|
|
7bbb04b594 | ||
|
|
fca40e5544 | ||
|
|
c9a70f3754 | ||
|
|
0adcbeedf1 | ||
|
|
de9625942b | ||
|
|
da9327ab1c | ||
|
|
61280f758a | ||
|
|
801cc8a5f4 | ||
|
|
74153b6deb | ||
|
|
ecd1d3cad3 | ||
|
|
0a098cf7b7 | ||
|
|
cfa8d1e5d7 | ||
|
|
5ffa808c0f | ||
|
|
212f12a81f | ||
|
|
c31e4253dd | ||
|
|
a96b95e198 | ||
|
|
a06b6a4bac | ||
|
|
cdc288e78d | ||
|
|
82a3b8c770 | ||
|
|
1e97a32d4b | ||
|
|
4a6b426651 | ||
|
|
0e29640766 | ||
|
|
05b4255751 | ||
|
|
1ab07a9027 | ||
|
|
75421b4223 | ||
|
|
a7fe4e9a76 | ||
|
|
10ab2872f6 | ||
|
|
2fdc9c112f | ||
|
|
dd44de349c | ||
|
|
e10c5f6283 | ||
|
|
9500acd69f | ||
|
|
bb43e9dcdf | ||
|
|
28613caf8a | ||
|
|
47cad0a131 | ||
|
|
4858608162 | ||
|
|
a1f03c844b | ||
|
|
8bbfbd63d7 | ||
|
|
7b3985e34a | ||
|
|
f03f0d4adb | ||
|
|
c3fc200c5d | ||
|
|
c3530024c4 | ||
|
|
3ea11352e3 | ||
|
|
1276e5675e | ||
|
|
81c4718954 | ||
|
|
1806b5d3be | ||
|
|
81c0187bf1 | ||
|
|
9d51414fbb | ||
|
|
2e2b55e87d | ||
|
|
081b1ec49e | ||
|
|
c46156f37f | ||
|
|
41945543e0 | ||
|
|
e5f6180dbe | ||
|
|
57c810ab9a | ||
|
|
b012f18a40 | ||
|
|
9e8606cab4 | ||
|
|
55bcb92810 | ||
|
|
a7f2abcb22 | ||
|
|
3376a87f15 | ||
|
|
2b9f03237a | ||
|
|
214dc20c2e | ||
|
|
b656e1e2d1 | ||
|
|
7f9bbebe5b | ||
|
|
dbff05814f | ||
|
|
1722733802 | ||
|
|
85d5bb5dc2 | ||
|
|
2e64d7cca6 | ||
|
|
3908e258c8 | ||
|
|
9b5714277a | ||
|
|
9df4692648 | ||
|
|
c58a6092ec | ||
|
|
cddc8e6df0 | ||
|
|
971321fb19 | ||
|
|
f384e765fb | ||
|
|
400ee16e83 | ||
|
|
f777efdd1c | ||
|
|
1d62f7ec23 | ||
|
|
c5b202f2a1 | ||
|
|
a2b0d1122c | ||
|
|
34c629dcb4 | ||
|
|
4726f42030 | ||
|
|
c9d693f1eb |
@@ -8,6 +8,9 @@ build/
|
|||||||
**/build/
|
**/build/
|
||||||
out/
|
out/
|
||||||
target/
|
target/
|
||||||
|
**/target/
|
||||||
|
bin/
|
||||||
|
version_builds/
|
||||||
|
|
||||||
# Gradle caches (local, not what's in the container)
|
# Gradle caches (local, not what's in the container)
|
||||||
.gradle/
|
.gradle/
|
||||||
@@ -16,9 +19,15 @@ target/
|
|||||||
# Node / frontend
|
# Node / frontend
|
||||||
node_modules/
|
node_modules/
|
||||||
**/node_modules/
|
**/node_modules/
|
||||||
|
frontend/node_modules/
|
||||||
|
frontend/dist/
|
||||||
.npm/
|
.npm/
|
||||||
.yarn/
|
.yarn/
|
||||||
|
|
||||||
|
# Tauri/desktop builds
|
||||||
|
src-tauri/target/
|
||||||
|
src-tauri/dist/
|
||||||
|
|
||||||
# IDE and editor
|
# IDE and editor
|
||||||
.idea/
|
.idea/
|
||||||
.vscode/
|
.vscode/
|
||||||
@@ -46,7 +55,21 @@ Dockerfile*
|
|||||||
**/test-results/
|
**/test-results/
|
||||||
**/jacoco/
|
**/jacoco/
|
||||||
|
|
||||||
|
# Testing and documentation (not needed in build)
|
||||||
|
testing/
|
||||||
|
docs/
|
||||||
|
*.md
|
||||||
|
README*
|
||||||
|
|
||||||
# Local env
|
# Local env
|
||||||
.env
|
.env
|
||||||
.env.*
|
.env.*
|
||||||
!.env.example
|
!.env.example
|
||||||
|
|
||||||
|
# Misc
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*~
|
||||||
|
.DS_Store
|
||||||
|
.cache/
|
||||||
|
.pytest_cache/
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@ indent_size = 4
|
|||||||
max_line_length = 100
|
max_line_length = 100
|
||||||
|
|
||||||
[*.py]
|
[*.py]
|
||||||
indent_size = 2
|
indent_size = 4
|
||||||
|
|
||||||
[*.gradle]
|
[*.gradle]
|
||||||
indent_size = 4
|
indent_size = 4
|
||||||
|
|||||||
@@ -6,14 +6,20 @@ openapi: &openapi
|
|||||||
- *build
|
- *build
|
||||||
- app/(common|core|proprietary)/src/main/java/**
|
- app/(common|core|proprietary)/src/main/java/**
|
||||||
|
|
||||||
|
docker-base: &docker-base
|
||||||
|
- docker/base/Dockerfile
|
||||||
|
- ".github/workflows/push-docker-base.yml"
|
||||||
|
|
||||||
docker: &docker
|
docker: &docker
|
||||||
- Dockerfile
|
- docker/embedded/Dockerfile
|
||||||
- Dockerfile.fat
|
- docker/embedded/Dockerfile.fat
|
||||||
- Dockerfile.ultra-lite
|
- docker/embedded/Dockerfile.ultra-lite
|
||||||
- ".github/workflows/build.yml"
|
- ".github/workflows/build.yml"
|
||||||
|
- ".github/workflows/push-docker.yml"
|
||||||
- scripts/init.sh
|
- scripts/init.sh
|
||||||
- scripts/init-without-ocr.sh
|
- scripts/init-without-ocr.sh
|
||||||
- exampleYmlFiles/**
|
- exampleYmlFiles/**
|
||||||
|
- *docker-base
|
||||||
|
|
||||||
project: &project
|
project: &project
|
||||||
- app/(common|core|proprietary)/src/(main|test)/java/**
|
- app/(common|core|proprietary)/src/(main|test)/java/**
|
||||||
@@ -24,6 +30,7 @@ project: &project
|
|||||||
- libs/**
|
- libs/**
|
||||||
- "testing/**/!(requirements*.txt|requirements*.in)*"
|
- "testing/**/!(requirements*.txt|requirements*.in)*"
|
||||||
- *docker
|
- *docker
|
||||||
|
- *docker-base
|
||||||
- gradle.properties
|
- gradle.properties
|
||||||
- gradlew
|
- gradlew
|
||||||
- gradlew.bat
|
- gradlew.bat
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ jobs:
|
|||||||
pr_ref: ${{ steps.resolve.outputs.ref }}
|
pr_ref: ${{ steps.resolve.outputs.ref }}
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -359,7 +359,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ jobs:
|
|||||||
enable_enterprise: ${{ steps.check-pro-flag.outputs.enable_enterprise }}
|
enable_enterprise: ${{ steps.check-pro-flag.outputs.enable_enterprise }}
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -128,7 +128,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Run Gradle Command
|
- name: Run Gradle Command
|
||||||
run: |
|
run: |
|
||||||
@@ -372,7 +372,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
name: AI Engine CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
engine:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
pull-requests: write
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: engine
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install uv
|
||||||
|
uses: astral-sh/setup-uv@v4
|
||||||
|
with:
|
||||||
|
enable-cache: true
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: make install
|
||||||
|
|
||||||
|
- name: Run fixers
|
||||||
|
# Ignore errors here because we're going to add comments for them in the following steps before actually failing
|
||||||
|
run: make fix || true
|
||||||
|
|
||||||
|
- name: Check for fixer changes
|
||||||
|
id: fixer_changes
|
||||||
|
run: |
|
||||||
|
if git diff --quiet; then
|
||||||
|
echo "changed=false" >> "$GITHUB_OUTPUT"
|
||||||
|
else
|
||||||
|
echo "changed=true" >> "$GITHUB_OUTPUT"
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Post fixer suggestions
|
||||||
|
if: steps.fixer_changes.outputs.changed == 'true' && github.event_name == 'pull_request'
|
||||||
|
uses: reviewdog/action-suggester@v1
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
tool_name: engine-make-fix
|
||||||
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
filter_mode: file
|
||||||
|
fail_level: any
|
||||||
|
level: info
|
||||||
|
|
||||||
|
- name: Comment on fixer suggestions
|
||||||
|
if: steps.fixer_changes.outputs.changed == 'true' && github.event_name == 'pull_request'
|
||||||
|
uses: actions/github-script@v7
|
||||||
|
with:
|
||||||
|
script: |
|
||||||
|
await github.rest.issues.createComment({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
issue_number: context.issue.number,
|
||||||
|
body: "The Python code in your PR has formatting/linting issues. Consider running `make fix` locally or setting up your editor's Ruff integration to auto-format and lint your files as you go, or commit the suggested changes on this PR.",
|
||||||
|
});
|
||||||
|
|
||||||
|
- name: Verify fixer changes are committed
|
||||||
|
if: steps.fixer_changes.outputs.changed == 'true'
|
||||||
|
run: |
|
||||||
|
if ! git diff --exit-code; then
|
||||||
|
echo "Fixes are out of date."
|
||||||
|
echo "Apply the reviewdog suggestions or run 'make fix' from engine/ and commit the updated files."
|
||||||
|
git --no-pager diff --stat
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Run linting
|
||||||
|
run: make lint
|
||||||
|
|
||||||
|
- name: Run type checking
|
||||||
|
run: make typecheck
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: make test
|
||||||
@@ -19,7 +19,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ jobs:
|
|||||||
pull-requests: write
|
pull-requests: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ jobs:
|
|||||||
app-id: ${{ secrets.GH_APP_ID }}
|
app-id: ${{ secrets.GH_APP_ID }}
|
||||||
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
|
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
|
||||||
|
|
||||||
- uses: srvaroa/labeler@0a20eccb8c94a1ee0bed5f16859aece1c45c3e55 # v1.13.0
|
- uses: srvaroa/labeler@bf262763a8a8e191f5847873aecc0f29df84f957 # v1.14.0
|
||||||
with:
|
with:
|
||||||
config_path: .github/labeler-config-srvaroa.yml
|
config_path: .github/labeler-config-srvaroa.yml
|
||||||
use_local_config: false
|
use_local_config: false
|
||||||
|
|||||||
+101
-28
@@ -30,16 +30,17 @@ jobs:
|
|||||||
project: ${{ steps.changes.outputs.project }}
|
project: ${{ steps.changes.outputs.project }}
|
||||||
openapi: ${{ steps.changes.outputs.openapi }}
|
openapi: ${{ steps.changes.outputs.openapi }}
|
||||||
frontend: ${{ steps.changes.outputs.frontend }}
|
frontend: ${{ steps.changes.outputs.frontend }}
|
||||||
|
docker-base: ${{ steps.changes.outputs.docker-base }}
|
||||||
steps:
|
steps:
|
||||||
- name: Harden the runner (Audit all outbound calls)
|
- name: Harden the runner (Audit all outbound calls)
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
|
||||||
- name: Check for file changes
|
- name: Check for file changes
|
||||||
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
|
uses: dorny/paths-filter@fbd0ab8f3e69293af611ebaee6363fc25e6d187d # v4.0.1
|
||||||
id: changes
|
id: changes
|
||||||
with:
|
with:
|
||||||
filters: .github/config/.files.yaml
|
filters: .github/config/.files.yaml
|
||||||
@@ -56,7 +57,7 @@ jobs:
|
|||||||
spring-security: [true, false]
|
spring-security: [true, false]
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
@@ -69,7 +70,7 @@ jobs:
|
|||||||
distribution: "temurin"
|
distribution: "temurin"
|
||||||
|
|
||||||
- name: Cache Gradle dependency artifacts
|
- name: Cache Gradle dependency artifacts
|
||||||
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
|
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/wrapper
|
~/.gradle/wrapper
|
||||||
@@ -80,7 +81,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
cache-disabled: true
|
cache-disabled: true
|
||||||
|
|
||||||
- name: Build with Gradle and spring security ${{ matrix.spring-security }}
|
- name: Build with Gradle and spring security ${{ matrix.spring-security }}
|
||||||
@@ -111,7 +112,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Upload Test Reports
|
- name: Upload Test Reports
|
||||||
if: always()
|
if: always()
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: test-reports-jdk-${{ matrix.jdk-version }}-spring-security-${{ matrix.spring-security }}
|
name: test-reports-jdk-${{ matrix.jdk-version }}-spring-security-${{ matrix.spring-security }}
|
||||||
path: |
|
path: |
|
||||||
@@ -140,7 +141,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -154,7 +155,7 @@ jobs:
|
|||||||
distribution: "temurin"
|
distribution: "temurin"
|
||||||
|
|
||||||
- name: Cache Gradle dependency artifacts
|
- name: Cache Gradle dependency artifacts
|
||||||
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
|
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/wrapper
|
~/.gradle/wrapper
|
||||||
@@ -165,7 +166,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
cache-disabled: true
|
cache-disabled: true
|
||||||
|
|
||||||
- name: Generate OpenAPI documentation
|
- name: Generate OpenAPI documentation
|
||||||
@@ -177,7 +178,7 @@ jobs:
|
|||||||
DISABLE_ADDITIONAL_FEATURES: true
|
DISABLE_ADDITIONAL_FEATURES: true
|
||||||
|
|
||||||
- name: Upload OpenAPI Documentation
|
- name: Upload OpenAPI Documentation
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: openapi-docs
|
name: openapi-docs
|
||||||
path: ./SwaggerDoc.json
|
path: ./SwaggerDoc.json
|
||||||
@@ -188,7 +189,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
@@ -210,19 +211,52 @@ jobs:
|
|||||||
- name: Run frontend tests
|
- name: Run frontend tests
|
||||||
run: cd frontend && npm run test -- --run
|
run: cd frontend && npm run test -- --run
|
||||||
- name: Upload frontend build artifacts
|
- name: Upload frontend build artifacts
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: frontend-build
|
name: frontend-build
|
||||||
path: frontend/dist/
|
path: frontend/dist/
|
||||||
retention-days: 3
|
retention-days: 3
|
||||||
|
|
||||||
|
playwright-e2e:
|
||||||
|
if: needs.files-changed.outputs.frontend == 'true'
|
||||||
|
needs: files-changed
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Harden Runner
|
||||||
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
|
with:
|
||||||
|
egress-policy: audit
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
- name: Set up Node.js
|
||||||
|
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
|
||||||
|
with:
|
||||||
|
node-version: "22"
|
||||||
|
cache: "npm"
|
||||||
|
cache-dependency-path: frontend/package-lock.json
|
||||||
|
- name: Install frontend dependencies
|
||||||
|
run: cd frontend && npm ci
|
||||||
|
- name: Generate icons
|
||||||
|
run: cd frontend && node scripts/generate-icons.js
|
||||||
|
- name: Install Playwright (chromium only)
|
||||||
|
run: cd frontend && npx playwright install chromium --with-deps
|
||||||
|
- name: Run E2E tests (chromium)
|
||||||
|
run: cd frontend && npx playwright test --project=chromium
|
||||||
|
- name: Upload Playwright report
|
||||||
|
if: always()
|
||||||
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
|
with:
|
||||||
|
name: playwright-report-pr-${{ github.run_id }}
|
||||||
|
path: frontend/playwright-report/
|
||||||
|
retention-days: 7
|
||||||
|
|
||||||
check-licence:
|
check-licence:
|
||||||
if: needs.files-changed.outputs.build == 'true'
|
if: needs.files-changed.outputs.build == 'true'
|
||||||
needs: [files-changed, build]
|
needs: [files-changed, build]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -236,7 +270,7 @@ jobs:
|
|||||||
distribution: "temurin"
|
distribution: "temurin"
|
||||||
|
|
||||||
- name: Cache Gradle dependency artifacts
|
- name: Cache Gradle dependency artifacts
|
||||||
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
|
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/wrapper
|
~/.gradle/wrapper
|
||||||
@@ -247,7 +281,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
cache-disabled: true
|
cache-disabled: true
|
||||||
|
|
||||||
- name: check the licenses for compatibility
|
- name: check the licenses for compatibility
|
||||||
@@ -264,7 +298,7 @@ jobs:
|
|||||||
|
|
||||||
- name: FAILED - check the licenses for compatibility
|
- name: FAILED - check the licenses for compatibility
|
||||||
if: failure()
|
if: failure()
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: dependencies-without-allowed-license.json
|
name: dependencies-without-allowed-license.json
|
||||||
path: build/reports/dependency-license/dependencies-without-allowed-license.json
|
path: build/reports/dependency-license/dependencies-without-allowed-license.json
|
||||||
@@ -295,7 +329,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -309,7 +343,7 @@ jobs:
|
|||||||
distribution: "temurin"
|
distribution: "temurin"
|
||||||
|
|
||||||
- name: Cache Gradle dependency artifacts
|
- name: Cache Gradle dependency artifacts
|
||||||
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
|
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/wrapper
|
~/.gradle/wrapper
|
||||||
@@ -320,7 +354,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
cache-disabled: true
|
cache-disabled: true
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
@@ -328,7 +362,7 @@ jobs:
|
|||||||
|
|
||||||
# Expose ACTIONS_RUNTIME_TOKEN / ACTIONS_RESULTS_URL for docker buildx type=gha cache backend.
|
# Expose ACTIONS_RUNTIME_TOKEN / ACTIONS_RESULTS_URL for docker buildx type=gha cache backend.
|
||||||
- name: Expose GitHub runtime for Buildx cache
|
- name: Expose GitHub runtime for Buildx cache
|
||||||
uses: crazy-max/ghaction-github-runtime@3cb05d89e1f492524af3d41a1c98c83bc3025124 # v3.1.0
|
uses: crazy-max/ghaction-github-runtime@04d248b84655b509d8c44dc1d6f990c879747487 # v4.0.0
|
||||||
|
|
||||||
- name: Install Docker Compose
|
- name: Install Docker Compose
|
||||||
run: |
|
run: |
|
||||||
@@ -336,7 +370,7 @@ jobs:
|
|||||||
sudo chmod +x /usr/local/bin/docker-compose
|
sudo chmod +x /usr/local/bin/docker-compose
|
||||||
|
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
|
||||||
with:
|
with:
|
||||||
python-version: "3.12"
|
python-version: "3.12"
|
||||||
cache: "pip" # caching pip dependencies
|
cache: "pip" # caching pip dependencies
|
||||||
@@ -357,16 +391,26 @@ jobs:
|
|||||||
MAVEN_USER: ${{ secrets.MAVEN_USER }}
|
MAVEN_USER: ${{ secrets.MAVEN_USER }}
|
||||||
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
|
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
|
||||||
MAVEN_PUBLIC_URL: ${{ secrets.MAVEN_PUBLIC_URL }}
|
MAVEN_PUBLIC_URL: ${{ secrets.MAVEN_PUBLIC_URL }}
|
||||||
|
DOCKER_BASE_CHANGED: ${{ needs.files-changed.outputs.docker-base }}
|
||||||
|
|
||||||
- name: Upload Cucumber Report
|
- name: Upload Cucumber Report
|
||||||
if: always()
|
if: always()
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: cucumber-report
|
name: cucumber-report
|
||||||
path: testing/cucumber/report.html
|
path: testing/cucumber/report.html
|
||||||
retention-days: 7
|
retention-days: 7
|
||||||
if-no-files-found: warn
|
if-no-files-found: warn
|
||||||
|
|
||||||
|
- name: Upload Test Reports
|
||||||
|
if: always()
|
||||||
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
|
with:
|
||||||
|
name: docker-compose-test-reports
|
||||||
|
path: testing/reports/
|
||||||
|
retention-days: 7
|
||||||
|
if-no-files-found: warn
|
||||||
|
|
||||||
- name: Cucumber Test Report
|
- name: Cucumber Test Report
|
||||||
if: always()
|
if: always()
|
||||||
uses: dorny/test-reporter@b082adf0eced0765477756c2a610396589b8c637 # v2.5.0
|
uses: dorny/test-reporter@b082adf0eced0765477756c2a610396589b8c637 # v2.5.0
|
||||||
@@ -395,13 +439,24 @@ jobs:
|
|||||||
cache-scope: stirling-pdf-fat
|
cache-scope: stirling-pdf-fat
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
- name: Checkout Repository
|
- name: Checkout Repository
|
||||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
|
||||||
|
- name: Login to GitHub Container Registry
|
||||||
|
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ github.token }}
|
||||||
|
|
||||||
|
- name: Convert repository owner to lowercase
|
||||||
|
id: repoowner
|
||||||
|
run: echo "lowercase=$(echo ${{ github.repository_owner }} | awk '{print tolower($0)}')" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Free disk space on runner
|
- name: Free disk space on runner
|
||||||
run: |
|
run: |
|
||||||
echo "Disk space before cleanup:" && df -h
|
echo "Disk space before cleanup:" && df -h
|
||||||
@@ -416,7 +471,7 @@ jobs:
|
|||||||
distribution: "temurin"
|
distribution: "temurin"
|
||||||
|
|
||||||
- name: Cache Gradle dependency artifacts
|
- name: Cache Gradle dependency artifacts
|
||||||
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
|
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/wrapper
|
~/.gradle/wrapper
|
||||||
@@ -427,7 +482,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
cache-disabled: true
|
cache-disabled: true
|
||||||
|
|
||||||
- name: Build application
|
- name: Build application
|
||||||
@@ -440,12 +495,28 @@ jobs:
|
|||||||
STIRLING_PDF_DESKTOP_UI: false
|
STIRLING_PDF_DESKTOP_UI: false
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
|
uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
id: buildx
|
id: buildx
|
||||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||||
|
|
||||||
|
- name: Build base image locally (PR base change only)
|
||||||
|
if: github.event_name == 'pull_request' && needs.files-changed.outputs.docker-base == 'true'
|
||||||
|
run: |
|
||||||
|
docker build -t stirling-pdf-base:pr-test -f docker/base/Dockerfile docker/base
|
||||||
|
|
||||||
|
- name: Set base image and platform for this build
|
||||||
|
id: build-params
|
||||||
|
run: |
|
||||||
|
if [ "${{ github.event_name }}" == "pull_request" ] && [ "${{ needs.files-changed.outputs.docker-base }}" == "true" ]; then
|
||||||
|
echo "base_image=stirling-pdf-base:pr-test" >> $GITHUB_OUTPUT
|
||||||
|
echo "platforms=linux/amd64" >> $GITHUB_OUTPUT
|
||||||
|
else
|
||||||
|
echo "base_image=stirlingtools/stirling-pdf-base:latest" >> $GITHUB_OUTPUT
|
||||||
|
echo "platforms=linux/amd64,linux/arm64/v8" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Build ${{ matrix.docker-rev }}
|
- name: Build ${{ matrix.docker-rev }}
|
||||||
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
|
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
|
||||||
with:
|
with:
|
||||||
@@ -455,13 +526,15 @@ jobs:
|
|||||||
push: false
|
push: false
|
||||||
cache-from: type=gha,scope=${{ matrix.cache-scope }}
|
cache-from: type=gha,scope=${{ matrix.cache-scope }}
|
||||||
cache-to: type=gha,mode=max,scope=${{ matrix.cache-scope }}
|
cache-to: type=gha,mode=max,scope=${{ matrix.cache-scope }}
|
||||||
platforms: linux/amd64,linux/arm64/v8
|
platforms: ${{ steps.build-params.outputs.platforms }}
|
||||||
|
build-args: |
|
||||||
|
BASE_IMAGE=${{ steps.build-params.outputs.base_image }}
|
||||||
provenance: true
|
provenance: true
|
||||||
sbom: true
|
sbom: true
|
||||||
|
|
||||||
- name: Upload Reports
|
- name: Upload Reports
|
||||||
if: always()
|
if: always()
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: reports-docker-${{ matrix.artifact-suffix }}
|
name: reports-docker-${{ matrix.artifact-suffix }}
|
||||||
path: |
|
path: |
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ jobs:
|
|||||||
pull-requests: write # Allow writing to pull requests
|
pull-requests: write # Allow writing to pull requests
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -196,7 +196,7 @@ jobs:
|
|||||||
core.exportVariable("REFERENCE_FILE", referenceFilePath);
|
core.exportVariable("REFERENCE_FILE", referenceFilePath);
|
||||||
|
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
|
||||||
with:
|
with:
|
||||||
python-version: "3.12"
|
python-version: "3.12"
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
name: Clear GitHub Actions Cache
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- clear-github-cache
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
clear-cache:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
actions: write
|
||||||
|
steps:
|
||||||
|
- name: Clear all caches
|
||||||
|
uses: actions/github-script@v7
|
||||||
|
with:
|
||||||
|
script: |
|
||||||
|
const caches = await github.rest.actions.getActionsCacheList({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
per_page: 100,
|
||||||
|
});
|
||||||
|
|
||||||
|
let deleted = 0;
|
||||||
|
for (const cache of caches.data.actions_caches) {
|
||||||
|
console.log(`Deleting cache: ${cache.key} (${cache.id})`);
|
||||||
|
await github.rest.actions.deleteActionsCacheById({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
cache_id: cache.id,
|
||||||
|
});
|
||||||
|
deleted++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle pagination if more than 100 caches
|
||||||
|
let totalCount = caches.data.total_count;
|
||||||
|
while (deleted < totalCount) {
|
||||||
|
const moreCaches = await github.rest.actions.getActionsCacheList({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
per_page: 100,
|
||||||
|
});
|
||||||
|
if (moreCaches.data.actions_caches.length === 0) break;
|
||||||
|
for (const cache of moreCaches.data.actions_caches) {
|
||||||
|
console.log(`Deleting cache: ${cache.key} (${cache.id})`);
|
||||||
|
await github.rest.actions.deleteActionsCacheById({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
cache_id: cache.id,
|
||||||
|
});
|
||||||
|
deleted++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Successfully deleted ${deleted} caches.`);
|
||||||
@@ -17,7 +17,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ jobs:
|
|||||||
licenses-backend: ${{ steps.changes.outputs.licenses-backend }}
|
licenses-backend: ${{ steps.changes.outputs.licenses-backend }}
|
||||||
steps:
|
steps:
|
||||||
- name: Harden the runner (Audit all outbound calls)
|
- name: Harden the runner (Audit all outbound calls)
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ jobs:
|
|||||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
|
||||||
- name: Check for file changes
|
- name: Check for file changes
|
||||||
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
|
uses: dorny/paths-filter@fbd0ab8f3e69293af611ebaee6363fc25e6d187d # v4.0.1
|
||||||
id: changes
|
id: changes
|
||||||
with:
|
with:
|
||||||
filters: .github/config/.files.yaml
|
filters: .github/config/.files.yaml
|
||||||
@@ -49,7 +49,7 @@ jobs:
|
|||||||
repository-projects: write # Required for enabling automerge
|
repository-projects: write # Required for enabling automerge
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -312,7 +312,7 @@ jobs:
|
|||||||
repository-projects: write # Required for enabling automerge
|
repository-projects: write # Required for enabling automerge
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -339,7 +339,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Check licenses and generate report
|
- name: Check licenses and generate report
|
||||||
id: license-check
|
id: license-check
|
||||||
@@ -369,7 +369,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Upload artifact on license issues
|
- name: Upload artifact on license issues
|
||||||
if: env.LICENSE_WARNINGS_EXIST == 'true'
|
if: env.LICENSE_WARNINGS_EXIST == 'true'
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: backend-dependencies-without-allowed-license.json
|
name: backend-dependencies-without-allowed-license.json
|
||||||
path: build/reports/dependency-license/dependencies-without-allowed-license.json
|
path: build/reports/dependency-license/dependencies-without-allowed-license.json
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ jobs:
|
|||||||
issues: write
|
issues: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ jobs:
|
|||||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
|
||||||
- name: Run Labeler
|
- name: Run Labeler
|
||||||
uses: crazy-max/ghaction-github-labeler@24d110aa46a59976b8a7f35518cb7f14f434c916 # v5.3.0
|
uses: crazy-max/ghaction-github-labeler@548a7c3603594ec17c819e1239f281a3b801ab4d # v6.0.0
|
||||||
with:
|
with:
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
yaml-file: .github/labels.yml
|
yaml-file: .github/labels.yml
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ jobs:
|
|||||||
version: ${{ steps.versionNumber.outputs.versionNumber }}
|
version: ${{ steps.versionNumber.outputs.versionNumber }}
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ jobs:
|
|||||||
distribution: "temurin"
|
distribution: "temurin"
|
||||||
|
|
||||||
- name: Cache Gradle dependencies
|
- name: Cache Gradle dependencies
|
||||||
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
|
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/caches
|
~/.gradle/caches
|
||||||
@@ -61,7 +61,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Get version number
|
- name: Get version number
|
||||||
id: versionNumber
|
id: versionNumber
|
||||||
@@ -119,7 +119,7 @@ jobs:
|
|||||||
file_suffix: "-server"
|
file_suffix: "-server"
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
if: matrix.variant.build_frontend == true
|
if: matrix.variant.build_frontend == true
|
||||||
@@ -162,7 +162,7 @@ jobs:
|
|||||||
cp app/core/build/libs/stirling-pdf-${{ needs.determine-matrix.outputs.version }}.jar ./jar-dist/Stirling-PDF${{ matrix.variant.file_suffix }}.jar
|
cp app/core/build/libs/stirling-pdf-${{ needs.determine-matrix.outputs.version }}.jar ./jar-dist/Stirling-PDF${{ matrix.variant.file_suffix }}.jar
|
||||||
|
|
||||||
- name: Upload JAR artifacts
|
- name: Upload JAR artifacts
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: jar${{ matrix.variant.file_suffix }}
|
name: jar${{ matrix.variant.file_suffix }}
|
||||||
path: ./jar-dist/*.jar
|
path: ./jar-dist/*.jar
|
||||||
@@ -179,9 +179,12 @@ jobs:
|
|||||||
WINDOWS_CERTIFICATE: ${{ secrets.WINDOWS_CERTIFICATE }}
|
WINDOWS_CERTIFICATE: ${{ secrets.WINDOWS_CERTIFICATE }}
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
allowed-endpoints: >
|
||||||
|
one.digicert.com:443
|
||||||
|
clientauth.one.digicert.com:443
|
||||||
|
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
@@ -214,7 +217,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Build Java backend with JLink
|
- name: Build Java backend with JLink
|
||||||
working-directory: ./
|
working-directory: ./
|
||||||
@@ -542,7 +545,7 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Upload build artifacts
|
- name: Upload build artifacts
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: Stirling-PDF-${{ matrix.name }}
|
name: Stirling-PDF-${{ matrix.name }}
|
||||||
path: ./dist/*
|
path: ./dist/*
|
||||||
@@ -556,30 +559,30 @@ jobs:
|
|||||||
contents: write
|
contents: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
- name: Download all Tauri artifacts
|
- name: Download all Tauri artifacts
|
||||||
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
|
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
|
||||||
with:
|
with:
|
||||||
pattern: Stirling-PDF-*
|
pattern: Stirling-PDF-*
|
||||||
path: ./artifacts/tauri
|
path: ./artifacts/tauri
|
||||||
|
|
||||||
- name: Download JAR artifact (default)
|
- name: Download JAR artifact (default)
|
||||||
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
|
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
|
||||||
with:
|
with:
|
||||||
name: jar
|
name: jar
|
||||||
path: ./artifacts/jars
|
path: ./artifacts/jars
|
||||||
|
|
||||||
- name: Download JAR artifact (with login)
|
- name: Download JAR artifact (with login)
|
||||||
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
|
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
|
||||||
with:
|
with:
|
||||||
name: jar-with-login
|
name: jar-with-login
|
||||||
path: ./artifacts/jars
|
path: ./artifacts/jars
|
||||||
|
|
||||||
- name: Download JAR artifact (server only)
|
- name: Download JAR artifact (server only)
|
||||||
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
|
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
|
||||||
with:
|
with:
|
||||||
name: jar-server
|
name: jar-server
|
||||||
path: ./artifacts/jars
|
path: ./artifacts/jars
|
||||||
@@ -588,7 +591,7 @@ jobs:
|
|||||||
run: ls -R ./artifacts
|
run: ls -R ./artifacts
|
||||||
|
|
||||||
- name: Upload binaries to Release
|
- name: Upload binaries to Release
|
||||||
uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2.5.0
|
uses: softprops/action-gh-release@153bb8e04406b158c6c84fc1615b65b24149a1fe # v2.6.1
|
||||||
with:
|
with:
|
||||||
tag_name: v${{ needs.determine-matrix.outputs.version }}
|
tag_name: v${{ needs.determine-matrix.outputs.version }}
|
||||||
generate_release_notes: true
|
generate_release_notes: true
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
name: Nightly E2E Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
schedule:
|
||||||
|
- cron: "0 2 * * *" # 2 AM UTC every night
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
playwright-all-browsers:
|
||||||
|
name: Playwright (chromium + firefox + webkit)
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Harden the runner (Audit all outbound calls)
|
||||||
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
|
with:
|
||||||
|
egress-policy: audit
|
||||||
|
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
|
||||||
|
- name: Set up Node.js
|
||||||
|
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
|
||||||
|
with:
|
||||||
|
node-version: "22"
|
||||||
|
cache: "npm"
|
||||||
|
cache-dependency-path: frontend/package-lock.json
|
||||||
|
|
||||||
|
- name: Install frontend dependencies
|
||||||
|
run: cd frontend && npm ci
|
||||||
|
|
||||||
|
- name: Generate icons
|
||||||
|
run: cd frontend && node scripts/generate-icons.js
|
||||||
|
|
||||||
|
- name: Install all Playwright browsers
|
||||||
|
run: cd frontend && npx playwright install --with-deps
|
||||||
|
|
||||||
|
- name: Run E2E tests (all browsers)
|
||||||
|
run: cd frontend && npx playwright test
|
||||||
|
|
||||||
|
- name: Upload Playwright report
|
||||||
|
if: always()
|
||||||
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
|
with:
|
||||||
|
name: playwright-nightly-${{ github.run_id }}
|
||||||
|
path: frontend/playwright-report/
|
||||||
|
retention-days: 14
|
||||||
@@ -21,7 +21,7 @@ jobs:
|
|||||||
pull-requests: write
|
pull-requests: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ jobs:
|
|||||||
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
|
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
|
||||||
|
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
|
||||||
with:
|
with:
|
||||||
python-version: 3.12
|
python-version: 3.12
|
||||||
cache: "pip" # caching pip dependencies
|
cache: "pip" # caching pip dependencies
|
||||||
@@ -68,7 +68,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: ./gradlew build
|
run: ./gradlew build
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
name: Push Docker Base Image
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- baseDockerImage
|
||||||
|
- accessIssueFix
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
version:
|
||||||
|
description: 'Base image version (e.g., 1.0.0, 1.0.1)'
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
push-base:
|
||||||
|
if: ${{ vars.CI_PROFILE != 'lite' && github.actor == 'Frooodle' }}
|
||||||
|
runs-on: ubuntu-24.04-8core
|
||||||
|
permissions:
|
||||||
|
packages: write
|
||||||
|
id-token: write
|
||||||
|
steps:
|
||||||
|
- name: Verify authorized user
|
||||||
|
run: |
|
||||||
|
if [ "${{ github.actor }}" != "Frooodle" ]; then
|
||||||
|
echo "Error: Only Frooodle is authorized to run this workflow"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Set version
|
||||||
|
id: version
|
||||||
|
run: |
|
||||||
|
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
|
||||||
|
VERSION="${{ github.event.inputs.version }}"
|
||||||
|
elif [ "${{ github.ref_name }}" == "accessIssueFix" ]; then
|
||||||
|
VERSION="1.0.3"
|
||||||
|
else
|
||||||
|
VERSION="1.0.0"
|
||||||
|
fi
|
||||||
|
echo "version=${VERSION}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Harden Runner
|
||||||
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
|
with:
|
||||||
|
egress-policy: audit
|
||||||
|
|
||||||
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_HUB_API }}
|
||||||
|
|
||||||
|
- name: Login to GitHub Container Registry
|
||||||
|
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ github.token }}
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
id: buildx
|
||||||
|
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||||
|
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
|
||||||
|
|
||||||
|
- name: Convert repository owner to lowercase
|
||||||
|
id: repoowner
|
||||||
|
run: echo "lowercase=$(echo ${{ github.repository_owner }} | awk '{print tolower($0)}')" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Generate tags for base image
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
|
||||||
|
with:
|
||||||
|
images: |
|
||||||
|
${{ secrets.DOCKER_HUB_ORG_USERNAME }}/stirling-pdf-base
|
||||||
|
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/stirling-pdf-base
|
||||||
|
tags: |
|
||||||
|
type=raw,value=${{ steps.version.outputs.version }}
|
||||||
|
|
||||||
|
- name: Build and push base image
|
||||||
|
id: build-push-base
|
||||||
|
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
|
||||||
|
with:
|
||||||
|
builder: ${{ steps.buildx.outputs.name }}
|
||||||
|
context: docker/base
|
||||||
|
file: ./docker/base/Dockerfile
|
||||||
|
push: true
|
||||||
|
cache-from: type=gha,scope=stirling-pdf-base
|
||||||
|
cache-to: type=gha,mode=max,scope=stirling-pdf-base
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
platforms: linux/amd64,linux/arm64/v8
|
||||||
|
provenance: true
|
||||||
|
sbom: true
|
||||||
|
|
||||||
|
- name: Install cosign
|
||||||
|
uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0
|
||||||
|
with:
|
||||||
|
cosign-release: "v2.4.1"
|
||||||
|
|
||||||
|
- name: Sign base images
|
||||||
|
env:
|
||||||
|
DIGEST: ${{ steps.build-push-base.outputs.digest }}
|
||||||
|
TAGS: ${{ steps.meta.outputs.tags }}
|
||||||
|
COSIGN_PRIVATE_KEY: ${{ secrets.COSIGN_PRIVATE_KEY }}
|
||||||
|
COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}
|
||||||
|
run: |
|
||||||
|
if [ -n "$COSIGN_PRIVATE_KEY" ]; then
|
||||||
|
echo "$TAGS" | tr ',' '\n' | while read -r tag; do
|
||||||
|
cosign sign --yes \
|
||||||
|
--key env://COSIGN_PRIVATE_KEY \
|
||||||
|
"${tag}@${DIGEST}"
|
||||||
|
done
|
||||||
|
else
|
||||||
|
echo "Warning: COSIGN_PRIVATE_KEY not set, skipping image signing"
|
||||||
|
fi
|
||||||
@@ -33,7 +33,7 @@ jobs:
|
|||||||
id-token: write
|
id-token: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ jobs:
|
|||||||
distribution: "temurin"
|
distribution: "temurin"
|
||||||
|
|
||||||
- name: Cache Gradle dependencies
|
- name: Cache Gradle dependencies
|
||||||
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
|
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/caches
|
~/.gradle/caches
|
||||||
@@ -58,7 +58,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
id: buildx
|
id: buildx
|
||||||
@@ -74,13 +74,13 @@ jobs:
|
|||||||
|
|
||||||
- name: Install cosign
|
- name: Install cosign
|
||||||
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/V2-master'
|
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/V2-master'
|
||||||
uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0
|
uses: sigstore/cosign-installer@ba7bc0a3fef59531c69a25acd34668d6d3fe6f22 # v4.1.0
|
||||||
with:
|
with:
|
||||||
cosign-release: "v2.4.1"
|
cosign-release: "v2.4.1"
|
||||||
|
|
||||||
- name: Install cosign
|
- name: Install cosign
|
||||||
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/V2-master'
|
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/V2-master'
|
||||||
uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0
|
uses: sigstore/cosign-installer@ba7bc0a3fef59531c69a25acd34668d6d3fe6f22 # v4.1.0
|
||||||
with:
|
with:
|
||||||
cosign-release: "v2.4.1"
|
cosign-release: "v2.4.1"
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ jobs:
|
|||||||
password: ${{ github.token }}
|
password: ${{ github.token }}
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
|
uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
|
||||||
|
|
||||||
- name: Convert repository owner to lowercase
|
- name: Convert repository owner to lowercase
|
||||||
id: repoowner
|
id: repoowner
|
||||||
@@ -106,7 +106,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Generate tags for latest
|
- name: Generate tags for latest
|
||||||
id: meta
|
id: meta
|
||||||
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0
|
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
|
||||||
with:
|
with:
|
||||||
images: |
|
images: |
|
||||||
${{ secrets.DOCKER_HUB_USERNAME }}/s-pdf
|
${{ secrets.DOCKER_HUB_USERNAME }}/s-pdf
|
||||||
@@ -130,7 +130,9 @@ jobs:
|
|||||||
cache-to: type=gha,mode=max,scope=stirling-pdf-latest
|
cache-to: type=gha,mode=max,scope=stirling-pdf-latest
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
build-args: VERSION_TAG=${{ steps.versionNumber.outputs.versionNumber }}
|
build-args: |
|
||||||
|
VERSION_TAG=${{ steps.versionNumber.outputs.versionNumber }}
|
||||||
|
BASE_VERSION=1.0.0
|
||||||
platforms: linux/amd64,linux/arm64/v8
|
platforms: linux/amd64,linux/arm64/v8
|
||||||
provenance: true
|
provenance: true
|
||||||
sbom: true
|
sbom: true
|
||||||
@@ -151,7 +153,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Generate tags for latest-fat
|
- name: Generate tags for latest-fat
|
||||||
id: meta-fat
|
id: meta-fat
|
||||||
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0
|
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
|
||||||
if: github.ref != 'refs/heads/main' && github.ref != 'refs/heads/testMain'
|
if: github.ref != 'refs/heads/main' && github.ref != 'refs/heads/testMain'
|
||||||
with:
|
with:
|
||||||
images: |
|
images: |
|
||||||
@@ -195,7 +197,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Generate tags for ultra-lite
|
- name: Generate tags for ultra-lite
|
||||||
id: meta-lite
|
id: meta-lite
|
||||||
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0
|
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
|
||||||
if: github.ref != 'refs/heads/main' && github.ref != 'refs/heads/testMain'
|
if: github.ref != 'refs/heads/main' && github.ref != 'refs/heads/testMain'
|
||||||
with:
|
with:
|
||||||
images: |
|
images: |
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
name: Rollback Latest Tags to Version
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
version:
|
||||||
|
description: "Version to rollback to (e.g. 2.8.0)"
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
rollback:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
packages: write
|
||||||
|
steps:
|
||||||
|
- name: Harden Runner
|
||||||
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
|
with:
|
||||||
|
egress-policy: audit
|
||||||
|
|
||||||
|
- name: Install crane
|
||||||
|
uses: imjasonh/setup-crane@31b88afe9de28ae0ffa220711af4b60be9435f6e # v0.4
|
||||||
|
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_HUB_API }}
|
||||||
|
|
||||||
|
- name: Login to GitHub Container Registry
|
||||||
|
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ github.token }}
|
||||||
|
|
||||||
|
- name: Convert repository owner to lowercase
|
||||||
|
id: repoowner
|
||||||
|
run: echo "lowercase=$(echo ${{ github.repository_owner }} | awk '{print tolower($0)}')" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Rollback all latest tags to v${{ inputs.version }}
|
||||||
|
env:
|
||||||
|
VERSION: ${{ inputs.version }}
|
||||||
|
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
|
||||||
|
DOCKER_HUB_ORG_USERNAME: ${{ secrets.DOCKER_HUB_ORG_USERNAME }}
|
||||||
|
REPO_OWNER: ${{ steps.repoowner.outputs.lowercase }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
IMAGES=(
|
||||||
|
"${DOCKER_HUB_USERNAME}/s-pdf"
|
||||||
|
"ghcr.io/${REPO_OWNER}/s-pdf"
|
||||||
|
"ghcr.io/${REPO_OWNER}/stirling-pdf"
|
||||||
|
"${DOCKER_HUB_ORG_USERNAME}/stirling-pdf"
|
||||||
|
)
|
||||||
|
|
||||||
|
VARIANTS=(
|
||||||
|
"${VERSION}:latest"
|
||||||
|
"${VERSION}-fat:latest-fat"
|
||||||
|
"${VERSION}-ultra-lite:latest-ultra-lite"
|
||||||
|
)
|
||||||
|
|
||||||
|
FAILED=0
|
||||||
|
|
||||||
|
for image in "${IMAGES[@]}"; do
|
||||||
|
for variant in "${VARIANTS[@]}"; do
|
||||||
|
SOURCE_TAG="${variant%%:*}"
|
||||||
|
TARGET_TAG="${variant##*:}"
|
||||||
|
|
||||||
|
echo "::group::${image} — ${SOURCE_TAG} → ${TARGET_TAG}"
|
||||||
|
|
||||||
|
if crane manifest "${image}:${SOURCE_TAG}" > /dev/null 2>&1; then
|
||||||
|
crane cp "${image}:${SOURCE_TAG}" "${image}:${TARGET_TAG}"
|
||||||
|
echo "✅ ${image}:${TARGET_TAG} now points to ${SOURCE_TAG}"
|
||||||
|
else
|
||||||
|
echo "::warning::⚠️ ${image}:${SOURCE_TAG} not found, skipping"
|
||||||
|
FAILED=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "::endgroup::"
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ "$FAILED" -ne 0 ]; then
|
||||||
|
echo "::warning::Some source tags were not found. This is expected if not all variants exist for this version."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "🎉 Rollback to ${VERSION} complete!"
|
||||||
@@ -35,7 +35,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ jobs:
|
|||||||
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
|
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
|
||||||
# format to the repository Actions tab.
|
# format to the repository Actions tab.
|
||||||
- name: "Upload artifact"
|
- name: "Upload artifact"
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: SARIF file
|
name: SARIF file
|
||||||
path: results.sarif
|
path: results.sarif
|
||||||
@@ -75,6 +75,6 @@ jobs:
|
|||||||
|
|
||||||
# Upload the results to GitHub's code scanning dashboard.
|
# Upload the results to GitHub's code scanning dashboard.
|
||||||
- name: "Upload to code-scanning"
|
- name: "Upload to code-scanning"
|
||||||
uses: github/codeql-action/upload-sarif@89a39a4e59826350b863aa6b6252a07ad50cf83e # v3.29.5
|
uses: github/codeql-action/upload-sarif@0d579ffd059c29b07949a3cce3983f0780820c98 # v3.29.5
|
||||||
with:
|
with:
|
||||||
sarif_file: results.sarif
|
sarif_file: results.sarif
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ jobs:
|
|||||||
pull-requests: write
|
pull-requests: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Generate Swagger documentation
|
- name: Generate Swagger documentation
|
||||||
run: ./gradlew :stirling-pdf:generateOpenApiDocs
|
run: ./gradlew :stirling-pdf:generateOpenApiDocs
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ jobs:
|
|||||||
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
|
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
|
||||||
|
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
|
||||||
with:
|
with:
|
||||||
python-version: "3.12"
|
python-version: "3.12"
|
||||||
cache: "pip" # caching pip dependencies
|
cache: "pip" # caching pip dependencies
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ jobs:
|
|||||||
matrix: ${{ steps.set-matrix.outputs.matrix }}
|
matrix: ${{ steps.set-matrix.outputs.matrix }}
|
||||||
steps:
|
steps:
|
||||||
- name: Harden the runner (Audit all outbound calls)
|
- name: Harden the runner (Audit all outbound calls)
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -91,7 +91,7 @@ jobs:
|
|||||||
APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
|
APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Build Java backend with JLink
|
- name: Build Java backend with JLink
|
||||||
working-directory: ./
|
working-directory: ./
|
||||||
@@ -606,7 +606,7 @@ jobs:
|
|||||||
}
|
}
|
||||||
|
|
||||||
- name: Upload artifacts
|
- name: Upload artifacts
|
||||||
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
|
||||||
with:
|
with:
|
||||||
name: Stirling-PDF-${{ matrix.name }}
|
name: Stirling-PDF-${{ matrix.name }}
|
||||||
path: ./dist/*
|
path: ./dist/*
|
||||||
@@ -667,7 +667,7 @@ jobs:
|
|||||||
pull-requests: write
|
pull-requests: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden the runner
|
- name: Harden the runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -759,7 +759,7 @@ jobs:
|
|||||||
if: always()
|
if: always()
|
||||||
steps:
|
steps:
|
||||||
- name: Harden the runner (Audit all outbound calls)
|
- name: Harden the runner (Audit all outbound calls)
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ jobs:
|
|||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
uses: gradle/actions/setup-gradle@f29f5a9d7b09a7c6b29859002d29d24e1674c884 # v5.0.1
|
||||||
with:
|
with:
|
||||||
gradle-version: 8.14
|
gradle-version: 9.3.1
|
||||||
|
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: ./gradlew build
|
run: ./gradlew build
|
||||||
@@ -131,14 +131,14 @@ jobs:
|
|||||||
frontend: ${{ steps.changes.outputs.frontend }}
|
frontend: ${{ steps.changes.outputs.frontend }}
|
||||||
steps:
|
steps:
|
||||||
- name: Harden the runner (Audit all outbound calls)
|
- name: Harden the runner (Audit all outbound calls)
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
|
||||||
- name: Check for file changes
|
- name: Check for file changes
|
||||||
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
|
uses: dorny/paths-filter@fbd0ab8f3e69293af611ebaee6363fc25e6d187d # v4.0.1
|
||||||
id: changes
|
id: changes
|
||||||
with:
|
with:
|
||||||
filters: ".github/config/.files.yaml"
|
filters: ".github/config/.files.yaml"
|
||||||
@@ -150,7 +150,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -186,7 +186,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -29,8 +29,14 @@ clientWebUI/
|
|||||||
exampleYmlFiles/stirling/
|
exampleYmlFiles/stirling/
|
||||||
/stirling/
|
/stirling/
|
||||||
/testing/file_snapshots
|
/testing/file_snapshots
|
||||||
|
/testing/cucumber/junit/
|
||||||
|
/testing/cucumber/report.html
|
||||||
|
/testing/.failed_tests
|
||||||
SwaggerDoc.json
|
SwaggerDoc.json
|
||||||
|
|
||||||
|
# Runtime storage for uploaded files and user data (not Java source code)
|
||||||
|
app/core/storage/
|
||||||
|
|
||||||
# Frontend build artifacts copied to backend static resources
|
# Frontend build artifacts copied to backend static resources
|
||||||
# These are generated by npm build and should not be committed
|
# These are generated by npm build and should not be committed
|
||||||
app/core/src/main/resources/static/assets/
|
app/core/src/main/resources/static/assets/
|
||||||
@@ -197,6 +203,9 @@ out/
|
|||||||
*.jks
|
*.jks
|
||||||
*.asc
|
*.asc
|
||||||
|
|
||||||
|
# Allow test fixture certificates (synthetic, no real credentials)
|
||||||
|
!frontend/src/core/tests/test-fixtures/certs/**
|
||||||
|
|
||||||
# SSH Keys
|
# SSH Keys
|
||||||
*.pub
|
*.pub
|
||||||
*.priv
|
*.priv
|
||||||
|
|||||||
@@ -19,6 +19,14 @@ This file provides guidance to AI Agents when working with code in this reposito
|
|||||||
### Security Mode Development
|
### Security Mode Development
|
||||||
Set `DOCKER_ENABLE_SECURITY=true` environment variable to enable security features during development. This is required for testing the full version locally.
|
Set `DOCKER_ENABLE_SECURITY=true` environment variable to enable security features during development. This is required for testing the full version locally.
|
||||||
|
|
||||||
|
### Python Development
|
||||||
|
Development for the AI engine happens in the `engine/` folder. The frontend calls the Python via Java as a proxy.
|
||||||
|
|
||||||
|
- Follow the engine-specific guidance in [engine/AGENTS.md](engine/AGENTS.md) for Python architecture, code style, and AI usage.
|
||||||
|
- Use Makefile commands for Python work:
|
||||||
|
- From `engine/`: `make check` to lint, type-check, test, etc. and `make fix` to fix easily fixable linting and formatting issues.
|
||||||
|
- The project structure is defined in `engine/pyproject.toml`. Any new dependencies should be listed there, followed by running `make install`.
|
||||||
|
|
||||||
### Frontend Development
|
### Frontend Development
|
||||||
- **Frontend dev server**: `cd frontend && npm run dev` (requires backend on localhost:8080)
|
- **Frontend dev server**: `cd frontend && npm run dev` (requires backend on localhost:8080)
|
||||||
- **Tech Stack**: Vite + React + TypeScript + Mantine UI + TailwindCSS
|
- **Tech Stack**: Vite + React + TypeScript + Mantine UI + TailwindCSS
|
||||||
@@ -43,6 +51,8 @@ Set `DOCKER_ENABLE_SECURITY=true` environment variable to enable security featur
|
|||||||
#### Import Paths - CRITICAL
|
#### Import Paths - CRITICAL
|
||||||
**ALWAYS use `@app/*` for imports.** Do not use `@core/*` or `@proprietary/*` unless explicitly wrapping/extending a lower layer implementation.
|
**ALWAYS use `@app/*` for imports.** Do not use `@core/*` or `@proprietary/*` unless explicitly wrapping/extending a lower layer implementation.
|
||||||
|
|
||||||
|
For a broader explanation of the frontend layering and override architecture, see [frontend/DeveloperGuide.md](frontend/DeveloperGuide.md).
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// ✅ CORRECT - Use @app/* for all imports
|
// ✅ CORRECT - Use @app/* for all imports
|
||||||
import { AppLayout } from "@app/components/AppLayout";
|
import { AppLayout } from "@app/components/AppLayout";
|
||||||
@@ -82,7 +92,7 @@ export function RightRailFooterExtensions(_props: RightRailFooterExtensionsProps
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```tsx
|
||||||
// desktop/components/rightRail/RightRailFooterExtensions.tsx (real implementation)
|
// desktop/components/rightRail/RightRailFooterExtensions.tsx (real implementation)
|
||||||
import { Box } from '@mantine/core';
|
import { Box } from '@mantine/core';
|
||||||
import { BackendHealthIndicator } from '@app/components/BackendHealthIndicator';
|
import { BackendHealthIndicator } from '@app/components/BackendHealthIndicator';
|
||||||
@@ -100,7 +110,7 @@ export function RightRailFooterExtensions({ className }: RightRailFooterExtensio
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```tsx
|
||||||
// core/components/shared/RightRail.tsx (usage - works in ALL builds)
|
// core/components/shared/RightRail.tsx (usage - works in ALL builds)
|
||||||
import { RightRailFooterExtensions } from '@app/components/rightRail/RightRailFooterExtensions';
|
import { RightRailFooterExtensions } from '@app/components/rightRail/RightRailFooterExtensions';
|
||||||
|
|
||||||
|
|||||||
+444
@@ -0,0 +1,444 @@
|
|||||||
|
# File Sharing Feature - Architecture & Workflow
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The File Sharing feature enables users to store files server-side and share them with other registered users or via token-based share links. Files are stored using a pluggable storage provider (local filesystem or database) with optional quota enforcement.
|
||||||
|
|
||||||
|
**Key Capabilities:**
|
||||||
|
- Server-side file storage (upload, update, download, delete)
|
||||||
|
- Optional history bundle and audit log attachments per file
|
||||||
|
- Direct user-to-user sharing with access roles
|
||||||
|
- Token-based share links (requires `system.frontendUrl`)
|
||||||
|
- Optional email notifications for shares (requires `mail.enabled`)
|
||||||
|
- Access audit trail (tracks who accessed a share link and how)
|
||||||
|
- Automatic share link expiration
|
||||||
|
- Storage quotas (per-user and total)
|
||||||
|
- Pluggable storage backend (local filesystem or database BLOB)
|
||||||
|
- Integration with the Shared Signing workflow
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
### Database Schema
|
||||||
|
|
||||||
|
**`stored_files`**
|
||||||
|
- One record per uploaded file
|
||||||
|
- Stores file metadata (name, content type, size, storage key)
|
||||||
|
- Optionally links to a history bundle and audit log as separate stored objects
|
||||||
|
- `workflow_session_id` — nullable link to a `WorkflowSession` (signing feature)
|
||||||
|
- `file_purpose` — enum classifying the file's role: `GENERIC`, `SIGNING_ORIGINAL`, `SIGNING_SIGNED`, `SIGNING_HISTORY`
|
||||||
|
|
||||||
|
**`file_shares`**
|
||||||
|
- One record per sharing relationship
|
||||||
|
- Two share types, distinguished by which fields are set:
|
||||||
|
- **User share**: `shared_with_user_id` is set, `share_token` is null
|
||||||
|
- **Link share**: `share_token` is set (UUID), `shared_with_user_id` is null
|
||||||
|
- `access_role` — `EDITOR`, `COMMENTER`, or `VIEWER`
|
||||||
|
- `expires_at` — nullable expiration for link shares
|
||||||
|
- `workflow_participant_id` — when set, marks this as a **workflow share** (hidden from the file manager, accessible only via workflow endpoints)
|
||||||
|
|
||||||
|
**`file_share_accesses`**
|
||||||
|
- One record per access event on a share link
|
||||||
|
- Tracks: user, share link, access type (`VIEW` or `DOWNLOAD`), timestamp
|
||||||
|
|
||||||
|
**`storage_cleanup_entries`**
|
||||||
|
- Queue of storage keys to be deleted asynchronously
|
||||||
|
- Used when a file is deleted but the physical storage object cleanup is deferred
|
||||||
|
|
||||||
|
### Access Roles
|
||||||
|
|
||||||
|
| Role | Can Read | Can Write |
|
||||||
|
|------|----------|-----------|
|
||||||
|
| `EDITOR` | ✅ | ✅ |
|
||||||
|
| `COMMENTER` | ✅ | ❌ |
|
||||||
|
| `VIEWER` | ✅ | ❌ |
|
||||||
|
|
||||||
|
Default role when none is specified: `EDITOR`.
|
||||||
|
|
||||||
|
Owners always have full access regardless of role.
|
||||||
|
|
||||||
|
#### Role Semantics: COMMENTER vs VIEWER
|
||||||
|
|
||||||
|
In the file storage layer, `COMMENTER` and `VIEWER` are equivalent — both grant read-only access and neither can replace file content. The distinction is meaningful in the **signing workflow** context:
|
||||||
|
|
||||||
|
| Context | COMMENTER | VIEWER |
|
||||||
|
|---------|-----------|--------|
|
||||||
|
| File storage | Read only (same as VIEWER) | Read only |
|
||||||
|
| Signing workflow | Can submit a signing action | Read only |
|
||||||
|
|
||||||
|
`WorkflowParticipant.canEdit()` returns `true` for `COMMENTER` (and `EDITOR`) roles, which the signing workflow uses to determine if a participant can still submit a signature. Once a participant has signed or declined, their effective role is automatically downgraded to `VIEWER` regardless of their configured role.
|
||||||
|
|
||||||
|
The rationale: "annotating" a document (submitting a signature) is not the same as "replacing" it. COMMENTER grants annotation rights without file-replacement rights.
|
||||||
|
|
||||||
|
### Backend Architecture
|
||||||
|
|
||||||
|
#### Service Layer
|
||||||
|
|
||||||
|
**FileStorageService** (`1137 lines`)
|
||||||
|
- Core file management service
|
||||||
|
- Upload, update, download, and delete operations
|
||||||
|
- User share management (share, revoke, leave)
|
||||||
|
- Link share management (create, revoke, access)
|
||||||
|
- Access recording and listing
|
||||||
|
- Storage quota enforcement
|
||||||
|
- Configuration feature gate checks
|
||||||
|
|
||||||
|
**StorageCleanupService**
|
||||||
|
- Scheduled daily: deletes orphaned storage keys from `storage_cleanup_entries`
|
||||||
|
- Scheduled daily: purges expired share links from `file_shares`
|
||||||
|
- Processes cleanup in batches of 50 entries
|
||||||
|
|
||||||
|
#### Storage Providers
|
||||||
|
|
||||||
|
**LocalStorageProvider**
|
||||||
|
- Files stored on the filesystem under `storage.local.basePath` (default: `./storage`)
|
||||||
|
- Storage key is a path relative to the base directory
|
||||||
|
|
||||||
|
**DatabaseStorageProvider**
|
||||||
|
- Files stored as BLOBs in `stored_file_blobs` table
|
||||||
|
- No filesystem dependency
|
||||||
|
|
||||||
|
Provider is selected at startup via `storage.provider: local | database`.
|
||||||
|
|
||||||
|
#### Controller Layer
|
||||||
|
|
||||||
|
**FileStorageController** (`/api/v1/storage`)
|
||||||
|
- All endpoints require authentication
|
||||||
|
- File CRUD and sharing operations
|
||||||
|
|
||||||
|
### Data Flow
|
||||||
|
|
||||||
|
```
|
||||||
|
User uploads file → StorageProvider stores bytes → StoredFile record created
|
||||||
|
↓
|
||||||
|
Owner shares file → FileShare record created (user or link)
|
||||||
|
↓
|
||||||
|
Recipient accesses file → Access recorded → File bytes streamed
|
||||||
|
```
|
||||||
|
|
||||||
|
## File Operations
|
||||||
|
|
||||||
|
### Upload File
|
||||||
|
|
||||||
|
```bash
|
||||||
|
POST /api/v1/storage/files
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
|
||||||
|
file: document.pdf # Required — main file
|
||||||
|
historyBundle: history.json # Optional — version history
|
||||||
|
auditLog: audit.json # Optional — audit trail
|
||||||
|
```
|
||||||
|
|
||||||
|
**Response:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": 42,
|
||||||
|
"fileName": "document.pdf",
|
||||||
|
"contentType": "application/pdf",
|
||||||
|
"sizeBytes": 102400,
|
||||||
|
"owner": "alice",
|
||||||
|
"ownedByCurrentUser": true,
|
||||||
|
"accessRole": "editor",
|
||||||
|
"createdAt": "2025-01-01T12:00:00",
|
||||||
|
"updatedAt": "2025-01-01T12:00:00",
|
||||||
|
"sharedWithUsers": [],
|
||||||
|
"sharedUsers": [],
|
||||||
|
"shareLinks": []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Update File
|
||||||
|
|
||||||
|
Replaces the file content. Only the owner can update.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
PUT /api/v1/storage/files/{fileId}
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
|
||||||
|
file: document_v2.pdf
|
||||||
|
historyBundle: history.json # Optional
|
||||||
|
auditLog: audit.json # Optional
|
||||||
|
```
|
||||||
|
|
||||||
|
### List Files
|
||||||
|
|
||||||
|
Returns all files owned by or shared with the current user. Workflow-shared files (signing participants) are excluded — those are accessible via signing endpoints only.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
GET /api/v1/storage/files
|
||||||
|
```
|
||||||
|
|
||||||
|
Response is sorted by `createdAt` descending.
|
||||||
|
|
||||||
|
### Download File
|
||||||
|
|
||||||
|
```bash
|
||||||
|
GET /api/v1/storage/files/{fileId}/download?inline=false
|
||||||
|
```
|
||||||
|
|
||||||
|
- `inline=false` (default) — `Content-Disposition: attachment`
|
||||||
|
- `inline=true` — `Content-Disposition: inline` (for browser preview)
|
||||||
|
|
||||||
|
### Delete File
|
||||||
|
|
||||||
|
Only the owner can delete. All associated share links and their access records are deleted first, then the database record, then the physical storage object.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
DELETE /api/v1/storage/files/{fileId}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Sharing Operations
|
||||||
|
|
||||||
|
### Share with User
|
||||||
|
|
||||||
|
```bash
|
||||||
|
POST /api/v1/storage/files/{fileId}/shares/users
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"username": "bob", # Username or email address
|
||||||
|
"accessRole": "editor" # "editor", "commenter", or "viewer" (default: "editor")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Behaviour:**
|
||||||
|
- If the target user exists: creates/updates a `FileShare` with `sharedWithUser` set
|
||||||
|
- If `username` is an email address and the user doesn't exist: creates a share link and sends a notification email (requires `sharing.emailEnabled` and `sharing.linkEnabled`)
|
||||||
|
- If the target user is the owner: returns 400
|
||||||
|
- If sharing is disabled: returns 403
|
||||||
|
|
||||||
|
### Revoke User Share
|
||||||
|
|
||||||
|
Only the owner can revoke.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
DELETE /api/v1/storage/files/{fileId}/shares/users/{username}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Leave Shared File
|
||||||
|
|
||||||
|
The recipient removes themselves from a shared file.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
DELETE /api/v1/storage/files/{fileId}/shares/self
|
||||||
|
```
|
||||||
|
|
||||||
|
### Create Share Link
|
||||||
|
|
||||||
|
Creates a token-based link for anonymous/authenticated access. Requires `sharing.linkEnabled` and `system.frontendUrl` to be configured.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
POST /api/v1/storage/files/{fileId}/shares/links
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"accessRole": "viewer" # Optional (default: "editor")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Response:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"token": "550e8400-e29b-41d4-a716-446655440000",
|
||||||
|
"accessRole": "viewer",
|
||||||
|
"createdAt": "2025-01-01T12:00:00",
|
||||||
|
"expiresAt": "2025-01-04T12:00:00"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Expiration is set to `now + sharing.linkExpirationDays` (default: 3 days).
|
||||||
|
|
||||||
|
### Revoke Share Link
|
||||||
|
|
||||||
|
```bash
|
||||||
|
DELETE /api/v1/storage/files/{fileId}/shares/links/{token}
|
||||||
|
```
|
||||||
|
|
||||||
|
Also deletes all access records for that token.
|
||||||
|
|
||||||
|
## Share Link Access
|
||||||
|
|
||||||
|
### Download via Share Link
|
||||||
|
|
||||||
|
Authentication is required (even for share links). Anonymous access is not permitted.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
GET /api/v1/storage/share-links/{token}?inline=false
|
||||||
|
```
|
||||||
|
|
||||||
|
- Returns 401 if unauthenticated
|
||||||
|
- Returns 403 if authenticated but link doesn't permit access
|
||||||
|
- Returns 410 if the link has expired
|
||||||
|
- Records a `FileShareAccess` entry on success
|
||||||
|
|
||||||
|
> **Token-as-credential semantics:** Any authenticated user who holds the token can access the file — the token is the credential. If you need per-user access control (only a specific person can open it), use "Share with User" instead. Share links are appropriate for broader distribution where possession of the token implies authorization.
|
||||||
|
|
||||||
|
### Get Share Link Metadata
|
||||||
|
|
||||||
|
```bash
|
||||||
|
GET /api/v1/storage/share-links/{token}/metadata
|
||||||
|
```
|
||||||
|
|
||||||
|
Returns file name, owner, access role, creation/expiry timestamps, and whether the current user owns the file.
|
||||||
|
|
||||||
|
### List Accessed Share Links
|
||||||
|
|
||||||
|
Returns the most recent access for each non-expired share link the current user has accessed.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
GET /api/v1/storage/share-links/accessed
|
||||||
|
```
|
||||||
|
|
||||||
|
### List Accesses for a Link (Owner Only)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
GET /api/v1/storage/files/{fileId}/shares/links/{token}/accesses
|
||||||
|
```
|
||||||
|
|
||||||
|
Returns per-user access history (username, VIEW/DOWNLOAD, timestamp), sorted descending by time.
|
||||||
|
|
||||||
|
## Workflow Share Integration
|
||||||
|
|
||||||
|
Signing workflow participants access documents via their own `WorkflowParticipant.shareToken`. No `FileShare` record is created for participants; access control is self-contained in the `WorkflowParticipant` entity.
|
||||||
|
|
||||||
|
The `FileShare.workflow_participant_id` column and the `FileShare.isWorkflowShare()` method are **deprecated**. Legacy data (sessions created before this change) may still have `FileShare` records with `workflow_participant_id` set, which continue to work via the existing token lookup path in `UnifiedAccessControlService`. No new records are created.
|
||||||
|
|
||||||
|
`GET /api/v1/storage/files` returns all files owned by or shared with the current user (via `FileShare`). Signing-session PDFs use the `file_purpose` field (`SIGNING_ORIGINAL`, `SIGNING_SIGNED`, etc.) to distinguish them from generic files. The file manager UI can filter on this field if needed.
|
||||||
|
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
| Method | Endpoint | Description | Auth |
|
||||||
|
|--------|----------|-------------|------|
|
||||||
|
| POST | `/api/v1/storage/files` | Upload file | Required |
|
||||||
|
| PUT | `/api/v1/storage/files/{id}` | Update file | Required (owner) |
|
||||||
|
| GET | `/api/v1/storage/files` | List accessible files | Required |
|
||||||
|
| GET | `/api/v1/storage/files/{id}` | Get file metadata | Required |
|
||||||
|
| GET | `/api/v1/storage/files/{id}/download` | Download file | Required |
|
||||||
|
| DELETE | `/api/v1/storage/files/{id}` | Delete file | Required (owner) |
|
||||||
|
| POST | `/api/v1/storage/files/{id}/shares/users` | Share with user | Required (owner) |
|
||||||
|
| DELETE | `/api/v1/storage/files/{id}/shares/users/{username}` | Revoke user share | Required (owner) |
|
||||||
|
| DELETE | `/api/v1/storage/files/{id}/shares/self` | Leave shared file | Required |
|
||||||
|
| POST | `/api/v1/storage/files/{id}/shares/links` | Create share link | Required (owner) |
|
||||||
|
| DELETE | `/api/v1/storage/files/{id}/shares/links/{token}` | Revoke share link | Required (owner) |
|
||||||
|
| GET | `/api/v1/storage/share-links/{token}` | Download via share link | Required |
|
||||||
|
| GET | `/api/v1/storage/share-links/{token}/metadata` | Get share link metadata | Required |
|
||||||
|
| GET | `/api/v1/storage/share-links/accessed` | List accessed share links | Required |
|
||||||
|
| GET | `/api/v1/storage/files/{id}/shares/links/{token}/accesses` | List share accesses | Required (owner) |
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
All storage settings live under the `storage:` key in `settings.yml`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
storage:
|
||||||
|
enabled: true # Requires security.enableLogin = true
|
||||||
|
provider: local # 'local' or 'database'
|
||||||
|
local:
|
||||||
|
basePath: './storage' # Filesystem base directory (local provider only)
|
||||||
|
quotas:
|
||||||
|
maxStorageMbPerUser: -1 # Per-user storage cap in MB; -1 = unlimited
|
||||||
|
maxStorageMbTotal: -1 # Total storage cap in MB; -1 = unlimited
|
||||||
|
maxFileMb: -1 # Max size per upload (main + history + audit) in MB; -1 = unlimited
|
||||||
|
sharing:
|
||||||
|
enabled: false # Master switch for all sharing (opt-in)
|
||||||
|
linkEnabled: false # Enable token-based share links (requires system.frontendUrl)
|
||||||
|
emailEnabled: false # Enable email notifications (requires mail.enabled)
|
||||||
|
linkExpirationDays: 3 # Days until share links expire
|
||||||
|
```
|
||||||
|
|
||||||
|
**Prerequisites:**
|
||||||
|
- `storage.enabled` requires `security.enableLogin = true`
|
||||||
|
- `sharing.linkEnabled` requires `system.frontendUrl` to be set (used to build share link URLs)
|
||||||
|
- `sharing.emailEnabled` requires `mail.enabled = true`
|
||||||
|
|
||||||
|
## Security Considerations
|
||||||
|
|
||||||
|
### Access Control
|
||||||
|
- All endpoints require authentication — there is no anonymous access
|
||||||
|
- Owner-only operations enforced in service layer (not just controller)
|
||||||
|
- `requireReadAccess` / `requireEditorAccess` checked on every download
|
||||||
|
|
||||||
|
### Share Link Security
|
||||||
|
- Tokens are UUIDs (random, not guessable)
|
||||||
|
- Expiration enforced on every access
|
||||||
|
- Expired links return HTTP 410 Gone
|
||||||
|
- Revoked links delete all access records
|
||||||
|
|
||||||
|
### Quota Enforcement
|
||||||
|
- Checked before storing (not after)
|
||||||
|
- Accounts for existing file size when replacing (only the delta counts)
|
||||||
|
- Covers main file + history bundle + audit log in a single check
|
||||||
|
|
||||||
|
## Automatic Cleanup
|
||||||
|
|
||||||
|
`StorageCleanupService` runs two scheduled jobs daily:
|
||||||
|
|
||||||
|
1. **Orphaned storage cleanup** — processes up to 50 `StorageCleanupEntry` records, deletes the physical storage object, then removes the entry. Failed attempts increment `attemptCount` for retry.
|
||||||
|
|
||||||
|
2. **Expired share link cleanup** — deletes all `FileShare` records where `expiresAt` is in the past and `shareToken` is set.
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
**"Storage is disabled":**
|
||||||
|
- Check `storage.enabled: true` in settings
|
||||||
|
- Verify `security.enableLogin: true`
|
||||||
|
|
||||||
|
**"Share links are disabled":**
|
||||||
|
- Check `sharing.linkEnabled: true`
|
||||||
|
- Verify `system.frontendUrl` is set and non-empty
|
||||||
|
|
||||||
|
**"Email sharing is disabled":**
|
||||||
|
- Check `sharing.emailEnabled: true`
|
||||||
|
- Verify `mail.enabled: true` and mail configuration
|
||||||
|
|
||||||
|
**Signing-session PDF appearing in the general file list:**
|
||||||
|
- This is expected — signing PDFs are accessible to owners and shared users
|
||||||
|
- Filter by `file_purpose` (`SIGNING_ORIGINAL`, `SIGNING_SIGNED`) in the UI to distinguish them
|
||||||
|
|
||||||
|
**Share link returns 410:**
|
||||||
|
- Link has expired — check `expires_at` in `file_shares` table
|
||||||
|
- Owner must create a new link
|
||||||
|
|
||||||
|
### Debug Queries
|
||||||
|
|
||||||
|
```sql
|
||||||
|
-- List files and their share counts
|
||||||
|
SELECT sf.stored_file_id, sf.original_filename, u.username as owner,
|
||||||
|
COUNT(DISTINCT fs.file_share_id) FILTER (WHERE fs.shared_with_user_id IS NOT NULL) as user_shares,
|
||||||
|
COUNT(DISTINCT fs.file_share_id) FILTER (WHERE fs.share_token IS NOT NULL) as link_shares
|
||||||
|
FROM stored_files sf
|
||||||
|
LEFT JOIN users u ON sf.owner_id = u.user_id
|
||||||
|
LEFT JOIN file_shares fs ON fs.stored_file_id = sf.stored_file_id
|
||||||
|
GROUP BY sf.stored_file_id, u.username;
|
||||||
|
|
||||||
|
-- Check share link expiration
|
||||||
|
SELECT share_token, access_role, created_at, expires_at,
|
||||||
|
expires_at < NOW() as is_expired
|
||||||
|
FROM file_shares
|
||||||
|
WHERE share_token IS NOT NULL;
|
||||||
|
|
||||||
|
-- Check access history for a share link
|
||||||
|
SELECT u.username, fsa.access_type, fsa.accessed_at
|
||||||
|
FROM file_share_accesses fsa
|
||||||
|
JOIN file_shares fs ON fsa.file_share_id = fs.file_share_id
|
||||||
|
JOIN users u ON fsa.user_id = u.user_id
|
||||||
|
WHERE fs.share_token = '{token}'
|
||||||
|
ORDER BY fsa.accessed_at DESC;
|
||||||
|
|
||||||
|
-- Pending cleanup entries
|
||||||
|
SELECT storage_key, attempt_count, updated_at
|
||||||
|
FROM storage_cleanup_entries
|
||||||
|
ORDER BY updated_at ASC;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
The File Sharing feature provides:
|
||||||
|
- ✅ Server-side file storage with pluggable backend (local/database)
|
||||||
|
- ✅ History bundle and audit log attachments per file
|
||||||
|
- ✅ Direct user-to-user sharing with EDITOR/COMMENTER/VIEWER roles
|
||||||
|
- ✅ Token-based share links with expiration
|
||||||
|
- ✅ Optional email notifications for shares
|
||||||
|
- ✅ Per-access audit trail for share links
|
||||||
|
- ✅ Storage quotas (per-user, total, per-file)
|
||||||
|
- ✅ Automatic cleanup of expired links and orphaned storage
|
||||||
|
- ✅ Workflow integration (signing-session PDFs stored via same infrastructure; participant access via `WorkflowParticipant.shareToken`)
|
||||||
@@ -6,6 +6,8 @@ Portions of this software are licensed as follows:
|
|||||||
|
|
||||||
* All content that resides under the "app/proprietary/" directory of this repository,
|
* All content that resides under the "app/proprietary/" directory of this repository,
|
||||||
if that directory exists, is licensed under the license defined in "app/proprietary/LICENSE".
|
if that directory exists, is licensed under the license defined in "app/proprietary/LICENSE".
|
||||||
|
* All content that resides under the "engine/" directory of this repository,
|
||||||
|
if that directory exists, is licensed under the license defined in "engine/LICENSE".
|
||||||
* All content that resides under the "frontend/src/proprietary/" directory of this repository,
|
* All content that resides under the "frontend/src/proprietary/" directory of this repository,
|
||||||
if that directory exists, is licensed under the license defined in "frontend/src/proprietary/LICENSE".
|
if that directory exists, is licensed under the license defined in "frontend/src/proprietary/LICENSE".
|
||||||
* All content that resides under the "frontend/src/desktop/" directory of this repository,
|
* All content that resides under the "frontend/src/desktop/" directory of this repository,
|
||||||
|
|||||||
@@ -0,0 +1,691 @@
|
|||||||
|
# Shared Signing Feature - Architecture & Workflow
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The Shared Signing feature enables collaborative document signing workflows where a document owner can request signatures from multiple participants. Each participant receives a secure token to access the document, submit their digital signature (with optional wet signature overlay), and track the signing progress.
|
||||||
|
|
||||||
|
**Key Capabilities:**
|
||||||
|
- Multi-participant signing sessions
|
||||||
|
- Digital certificate signatures (P12/PKCS12, JKS, SERVER, USER_CERT, PEM/UPLOAD)
|
||||||
|
- Visual wet signature overlays (drawn, typed, or uploaded) — multiple per participant
|
||||||
|
- Token-based participant access (no authentication required for participants)
|
||||||
|
- Authenticated participant access for registered users via sign-requests API
|
||||||
|
- Progress tracking for session owners
|
||||||
|
- Optional signature summary page appended to finalized PDF
|
||||||
|
- Automatic role downgrade after signing (security)
|
||||||
|
- GDPR-compliant wet signature metadata cleanup
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
### Database Schema
|
||||||
|
|
||||||
|
#### Core Tables
|
||||||
|
|
||||||
|
**`workflow_sessions`**
|
||||||
|
- Tracks signing sessions created by document owners
|
||||||
|
- Links to original and processed (signed) PDF files
|
||||||
|
- Stores session metadata (message, due date, status)
|
||||||
|
|
||||||
|
**`workflow_participants`**
|
||||||
|
- One record per participant per session
|
||||||
|
- Tracks participant status: PENDING → VIEWED → SIGNED/DECLINED
|
||||||
|
- `NOTIFIED` status is reserved for a future email notification feature; no current code path sets it
|
||||||
|
- Stores participant-specific metadata (certificates, wet signatures) as JSONB
|
||||||
|
- Each participant holds their own `shareToken` (UUID) for token-based access — no separate `FileShare` record is created
|
||||||
|
- `accessRole` controls what actions the participant can perform. `COMMENTER` (and `EDITOR`) allow submitting a signature; `VIEWER` does not. After signing/declining, effective role is automatically downgraded to `VIEWER`
|
||||||
|
|
||||||
|
**`user_server_certificates`**
|
||||||
|
- Stores auto-generated certificates per user
|
||||||
|
- Enables "Use My Personal Certificate" option
|
||||||
|
|
||||||
|
#### Extended Tables
|
||||||
|
|
||||||
|
**`stored_files`**
|
||||||
|
- Added `workflow_session_id` to link files to signing sessions
|
||||||
|
- Added `file_purpose` enum (SIGNING_ORIGINAL, SIGNING_SIGNED, etc.)
|
||||||
|
|
||||||
|
**`file_shares`**
|
||||||
|
- Regular file shares are created when the session owner shares the document with other users via the file manager
|
||||||
|
- The `workflow_participant_id` column is deprecated; participant access is self-contained in `WorkflowParticipant.shareToken`
|
||||||
|
|
||||||
|
### Backend Architecture
|
||||||
|
|
||||||
|
#### Service Layer
|
||||||
|
|
||||||
|
**WorkflowSessionService** (`816 lines`)
|
||||||
|
- Core workflow management service
|
||||||
|
- Creates sessions with participants
|
||||||
|
- Handles participant status updates
|
||||||
|
- Stores signature metadata (certificates and wet signatures)
|
||||||
|
- Finalizes sessions by coordinating signing process
|
||||||
|
|
||||||
|
Key responsibilities:
|
||||||
|
- Session lifecycle management (create, list, get details, delete)
|
||||||
|
- Participant management (add, remove, notify)
|
||||||
|
- Certificate submission storage
|
||||||
|
- Wet signature metadata storage
|
||||||
|
- Session finalization orchestration
|
||||||
|
|
||||||
|
**UnifiedAccessControlService**
|
||||||
|
- Validates participant tokens
|
||||||
|
- Checks session status and expiration
|
||||||
|
- Maps participant status to effective access role
|
||||||
|
- Automatic role downgrade after signing: SIGNED/DECLINED → VIEWER role
|
||||||
|
|
||||||
|
**UserServerCertificateService**
|
||||||
|
- Auto-generates personal certificates for users
|
||||||
|
- Manages certificate storage and retrieval
|
||||||
|
- Enables "Use My Personal Certificate" signing option
|
||||||
|
|
||||||
|
#### Controller Layer
|
||||||
|
|
||||||
|
**SigningSessionController** (Owner-facing + Authenticated participant endpoints)
|
||||||
|
- `POST /api/v1/security/cert-sign/sessions` - Create signing session
|
||||||
|
- `GET /api/v1/security/cert-sign/sessions` - List user's sessions
|
||||||
|
- `GET /api/v1/security/cert-sign/sessions/{id}` - Get session details
|
||||||
|
- `GET /api/v1/security/cert-sign/sessions/{id}/pdf` - Download original PDF
|
||||||
|
- `POST /api/v1/security/cert-sign/sessions/{id}/finalize` - Finalize and apply signatures
|
||||||
|
- `GET /api/v1/security/cert-sign/sessions/{id}/signed-pdf` - Download signed PDF
|
||||||
|
- `DELETE /api/v1/security/cert-sign/sessions/{id}` - Delete session
|
||||||
|
- `POST /api/v1/security/cert-sign/sessions/{id}/participants` - Add participants
|
||||||
|
- `DELETE /api/v1/security/cert-sign/sessions/{id}/participants/{participantId}` - Remove participant
|
||||||
|
- `GET /api/v1/security/cert-sign/sign-requests` - List sign requests for authenticated user
|
||||||
|
- `GET /api/v1/security/cert-sign/sign-requests/{id}` - Get sign request details
|
||||||
|
- `GET /api/v1/security/cert-sign/sign-requests/{id}/document` - Download document for signing
|
||||||
|
- `POST /api/v1/security/cert-sign/sign-requests/{id}/sign` - Sign document (authenticated)
|
||||||
|
- `POST /api/v1/security/cert-sign/sign-requests/{id}/decline` - Decline sign request (authenticated)
|
||||||
|
|
||||||
|
**WorkflowParticipantController** (Participant-facing, token-based)
|
||||||
|
- `GET /api/v1/workflow/participant/session?token={token}` - View session details
|
||||||
|
- `GET /api/v1/workflow/participant/details?token={token}` - Get participant details
|
||||||
|
- `GET /api/v1/workflow/participant/document?token={token}` - Download PDF
|
||||||
|
- `POST /api/v1/workflow/participant/submit-signature` - Submit signature
|
||||||
|
- `POST /api/v1/workflow/participant/decline?token={token}` - Decline to sign
|
||||||
|
|
||||||
|
#### Data Flow
|
||||||
|
|
||||||
|
```
|
||||||
|
Owner creates session → Participants receive tokens →
|
||||||
|
Participants access via token (or authenticated) → Participants submit signatures →
|
||||||
|
Owner finalizes → System applies signatures → [Optional: append summary page] → Signed PDF generated
|
||||||
|
```
|
||||||
|
|
||||||
|
### Frontend Architecture
|
||||||
|
|
||||||
|
#### Quick Access Integration
|
||||||
|
|
||||||
|
**SignPopout Component**
|
||||||
|
- Displays in Quick Access Bar (top navigation)
|
||||||
|
- Shows active and completed signing sessions
|
||||||
|
- Auto-refreshes every 15 seconds to show signature progress
|
||||||
|
- Badge indicator shows count of pending sessions
|
||||||
|
|
||||||
|
**ActiveSessionsPanel**
|
||||||
|
- Lists sessions where user is owner or participant
|
||||||
|
- Shows signature progress: "X/Y signatures" (e.g., "2/5 signatures")
|
||||||
|
- Color-coded badges:
|
||||||
|
- Blue: No signatures yet (0/X)
|
||||||
|
- Yellow: Partial signatures (X/Y)
|
||||||
|
- Green: Ready to finalize (X/X)
|
||||||
|
|
||||||
|
**CompletedSessionsPanel**
|
||||||
|
- Lists finalized sessions and declined sign requests
|
||||||
|
- Allows viewing/downloading signed PDFs
|
||||||
|
|
||||||
|
#### Workbench Views
|
||||||
|
|
||||||
|
**SignRequestWorkbenchView**
|
||||||
|
- Full-screen view for participants to sign documents
|
||||||
|
- Integrated PDF viewer with annotation support
|
||||||
|
- Certificate selection (Personal/Organization/Custom P12)
|
||||||
|
- Wet signature input (draw, type, or upload)
|
||||||
|
- Signature placement on PDF pages
|
||||||
|
|
||||||
|
**SessionDetailWorkbenchView**
|
||||||
|
- Owner's view of session details
|
||||||
|
- Participant list with status indicators
|
||||||
|
- Ability to add/remove participants
|
||||||
|
- Finalize button when all signatures collected
|
||||||
|
- Download original/signed PDF
|
||||||
|
|
||||||
|
#### State Management
|
||||||
|
|
||||||
|
**FileContext Integration**
|
||||||
|
- Signing sessions operate within FileContext workflow
|
||||||
|
- PDFs loaded once, persist across tool switches
|
||||||
|
- Memory management for large files (up to 100GB+)
|
||||||
|
|
||||||
|
**ToolWorkflowContext**
|
||||||
|
- Registers custom workbench views
|
||||||
|
- Manages navigation between viewer and signing tools
|
||||||
|
- Preserves file state during signing operations
|
||||||
|
|
||||||
|
#### Services & Hooks
|
||||||
|
|
||||||
|
**workflowService.ts**
|
||||||
|
- API client for all signing endpoints
|
||||||
|
- Handles session creation, listing, and management
|
||||||
|
- Participant operations (submit, decline)
|
||||||
|
|
||||||
|
**useWorkflowSession.ts**
|
||||||
|
- React hook for owner session management
|
||||||
|
- State management for session list and details
|
||||||
|
|
||||||
|
**useParticipantSession.ts**
|
||||||
|
- React hook for participant signing workflow
|
||||||
|
- Manages signature submission state
|
||||||
|
|
||||||
|
## Signing Workflow Process
|
||||||
|
|
||||||
|
### 1. Session Creation (Owner)
|
||||||
|
|
||||||
|
```
|
||||||
|
Owner → Uploads PDF → Selects participants → Creates session
|
||||||
|
↓
|
||||||
|
System creates:
|
||||||
|
- WorkflowSession record
|
||||||
|
- WorkflowParticipant records (one per participant, each with a unique shareToken)
|
||||||
|
↓
|
||||||
|
Participants receive token (via email or share link)
|
||||||
|
```
|
||||||
|
|
||||||
|
**API Call:**
|
||||||
|
```bash
|
||||||
|
POST /api/v1/security/cert-sign/sessions
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
|
||||||
|
file: document.pdf
|
||||||
|
workflowType: SIGNING
|
||||||
|
documentName: "contract.pdf" # Optional display name
|
||||||
|
participantUserIds: [1, 2, 3] # Registered user IDs
|
||||||
|
participantEmails: ["a@b.com"] # External/unregistered users
|
||||||
|
participants: [...] # Detailed participant configs (optional)
|
||||||
|
message: "Please sign this contract"
|
||||||
|
dueDate: "2025-12-31"
|
||||||
|
ownerEmail: "owner@example.com" # Optional, for notifications
|
||||||
|
workflowMetadata: '{"showSignature": false, "showLogo": false, "includeSummaryPage": true}'
|
||||||
|
```
|
||||||
|
|
||||||
|
**Session-level `workflowMetadata` fields:**
|
||||||
|
| Field | Type | Description |
|
||||||
|
|-------|------|-------------|
|
||||||
|
| `showSignature` | boolean | Show visible digital signature block on PDF |
|
||||||
|
| `pageNumber` | integer | Page to place digital signature on |
|
||||||
|
| `showLogo` | boolean | Show logo in digital signature block |
|
||||||
|
| `includeSummaryPage` | boolean | Append a signature summary page before digital signing |
|
||||||
|
|
||||||
|
**Response:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"sessionId": "uuid",
|
||||||
|
"documentName": "contract.pdf",
|
||||||
|
"participants": [
|
||||||
|
{
|
||||||
|
"userId": 1,
|
||||||
|
"email": "user1@example.com",
|
||||||
|
"shareToken": "token1",
|
||||||
|
"status": "PENDING"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"participantCount": 3,
|
||||||
|
"signedCount": 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Participant Access
|
||||||
|
|
||||||
|
```
|
||||||
|
Participant → Clicks token link → Views session details
|
||||||
|
↓
|
||||||
|
Status changes: PENDING/NOTIFIED → VIEWED
|
||||||
|
↓
|
||||||
|
Participant downloads PDF to review
|
||||||
|
```
|
||||||
|
|
||||||
|
**Access URL (unauthenticated):**
|
||||||
|
```
|
||||||
|
https://app.example.com/sign?token={participant_token}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Authenticated participants** can also use:
|
||||||
|
```
|
||||||
|
GET /api/v1/security/cert-sign/sign-requests
|
||||||
|
GET /api/v1/security/cert-sign/sign-requests/{sessionId}
|
||||||
|
GET /api/v1/security/cert-sign/sign-requests/{sessionId}/document
|
||||||
|
```
|
||||||
|
|
||||||
|
**Automatic Status Update:**
|
||||||
|
- First access: PENDING/NOTIFIED → VIEWED
|
||||||
|
- Downloads tracked but don't change status
|
||||||
|
|
||||||
|
### 3. Signature Submission
|
||||||
|
|
||||||
|
```
|
||||||
|
Participant → Selects certificate type → Uploads certificate (if needed)
|
||||||
|
→ Draws/uploads wet signatures (optional, multiple supported)
|
||||||
|
→ Submits signature
|
||||||
|
↓
|
||||||
|
System stores:
|
||||||
|
- Certificate data (P12/JKS keystore as base64)
|
||||||
|
- Certificate password
|
||||||
|
- Wet signatures metadata (JSON array: base64 image + coordinates per signature)
|
||||||
|
↓
|
||||||
|
Status changes: VIEWED → SIGNED
|
||||||
|
Access role: EDITOR → VIEWER (automatic downgrade)
|
||||||
|
```
|
||||||
|
|
||||||
|
**API Call (token-based, unauthenticated):**
|
||||||
|
```bash
|
||||||
|
POST /api/v1/workflow/participant/submit-signature
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
|
||||||
|
participantToken: {token}
|
||||||
|
certType: P12 | JKS | SERVER | USER_CERT
|
||||||
|
p12File: certificate.p12 (if certType=P12)
|
||||||
|
jksFile: keystore.jks (if certType=JKS)
|
||||||
|
password: cert_password
|
||||||
|
showSignature: false
|
||||||
|
pageNumber: 1
|
||||||
|
location: "New York"
|
||||||
|
reason: "I approve this contract"
|
||||||
|
showLogo: false
|
||||||
|
wetSignaturesData: '[{"page":0,"x":100,"y":200,"width":150,"height":50,"type":"IMAGE","data":"base64..."}]'
|
||||||
|
```
|
||||||
|
|
||||||
|
**API Call (authenticated users):**
|
||||||
|
```bash
|
||||||
|
POST /api/v1/security/cert-sign/sign-requests/{sessionId}/sign
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
|
||||||
|
certType: SERVER | USER_CERT | UPLOAD | PEM | PKCS12 | PFX | JKS
|
||||||
|
p12File: certificate.p12 (if applicable)
|
||||||
|
password: cert_password
|
||||||
|
reason: "I approve this contract"
|
||||||
|
location: "New York"
|
||||||
|
wetSignaturesData: '[...]'
|
||||||
|
```
|
||||||
|
|
||||||
|
**Metadata Storage (JSONB):**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"certificateSubmission": {
|
||||||
|
"certType": "P12",
|
||||||
|
"password": "cert_password",
|
||||||
|
"p12Keystore": "base64_encoded_keystore",
|
||||||
|
"showSignature": false,
|
||||||
|
"pageNumber": 1,
|
||||||
|
"location": "New York",
|
||||||
|
"reason": "I approve this contract",
|
||||||
|
"showLogo": false
|
||||||
|
},
|
||||||
|
"wetSignatures": [
|
||||||
|
{
|
||||||
|
"type": "IMAGE",
|
||||||
|
"data": "base64_image",
|
||||||
|
"page": 0,
|
||||||
|
"x": 100,
|
||||||
|
"y": 200,
|
||||||
|
"width": 150,
|
||||||
|
"height": 50
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: Multiple wet signatures are supported per participant (array).
|
||||||
|
|
||||||
|
### 4. Progress Tracking (Owner)
|
||||||
|
|
||||||
|
```
|
||||||
|
Owner → Views session list → Sees "2/5 signatures"
|
||||||
|
→ Clicks session → Views participant status
|
||||||
|
↓
|
||||||
|
Participant list shows:
|
||||||
|
- user1@example.com: SIGNED ✓
|
||||||
|
- user2@example.com: SIGNED ✓
|
||||||
|
- user3@example.com: VIEWED (pending)
|
||||||
|
- user4@example.com: PENDING
|
||||||
|
- user5@example.com: DECLINED ✗
|
||||||
|
↓
|
||||||
|
Auto-refresh every 15 seconds
|
||||||
|
```
|
||||||
|
|
||||||
|
**Badge Colors:**
|
||||||
|
- 🔵 Blue: 0/5 signatures (awaiting)
|
||||||
|
- 🟡 Yellow: 2/5 signatures (partial)
|
||||||
|
- 🟢 Green: 5/5 signatures (ready to finalize)
|
||||||
|
|
||||||
|
### 5. Session Finalization
|
||||||
|
|
||||||
|
```
|
||||||
|
Owner → Clicks "Finalize" → System processes signatures
|
||||||
|
↓
|
||||||
|
Processing steps:
|
||||||
|
1. Apply wet signatures to PDF (visual overlays)
|
||||||
|
1.5. Append signature summary page (if includeSummaryPage=true)
|
||||||
|
2. Apply digital certificates in participant order
|
||||||
|
- Visual signature block suppressed when summary page is enabled
|
||||||
|
3. Store signed PDF
|
||||||
|
4. Clear wet signature metadata (GDPR compliance)
|
||||||
|
↓
|
||||||
|
Owner downloads signed PDF
|
||||||
|
```
|
||||||
|
|
||||||
|
**Finalization Process:**
|
||||||
|
|
||||||
|
1. **Apply Wet Signatures First**
|
||||||
|
```java
|
||||||
|
for (WetSignature sig : wetSignatures) {
|
||||||
|
PDPage page = document.getPage(sig.getPage());
|
||||||
|
byte[] imageBytes = Base64.decode(sig.getData());
|
||||||
|
// Convert Y from top-left (UI) to bottom-left (PDF) coordinate system
|
||||||
|
float pdfY = page.getMediaBox().getHeight() - sig.getY() - sig.getHeight();
|
||||||
|
PDImageXObject image = PDImageXObject.createFromByteArray(document, imageBytes, "signature");
|
||||||
|
contentStream.drawImage(image, sig.getX(), pdfY, sig.getWidth(), sig.getHeight());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Append Summary Page (optional, before digital signing)**
|
||||||
|
|
||||||
|
If `includeSummaryPage=true`, a new A4 page is appended showing:
|
||||||
|
- Stirling logo and "Signature Summary" title
|
||||||
|
- Document name and session owner
|
||||||
|
- Finalization timestamp
|
||||||
|
- Per-participant: name, email, status, signed timestamp, reason, location, certificate type
|
||||||
|
- Supports overflow to additional pages
|
||||||
|
|
||||||
|
This step occurs **before** digital certificate signing so signatures are not invalidated.
|
||||||
|
When a summary page is added, the visual digital signature block (`showSignature`) is suppressed — wet signatures (hand-drawn overlays) are unaffected.
|
||||||
|
|
||||||
|
3. **Apply Digital Certificates (in participant order)**
|
||||||
|
```java
|
||||||
|
for (Participant p : participants) {
|
||||||
|
if (p.status == SIGNED) {
|
||||||
|
KeyStore keystore = buildKeystore(p.certificate);
|
||||||
|
// Reason: participant override > owner default > "Document Signing"
|
||||||
|
// Location: participant-provided only (no default)
|
||||||
|
CertSignController.sign(pdfBytes, keystore, password, settings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **Store and Cleanup**
|
||||||
|
```java
|
||||||
|
StoredFile signedFile = storeFile(signedPdfBytes, SIGNING_SIGNED);
|
||||||
|
session.setProcessedFile(signedFile);
|
||||||
|
session.setFinalized(true);
|
||||||
|
|
||||||
|
// GDPR: Clear sensitive metadata after finalization
|
||||||
|
for (Participant p : participants) {
|
||||||
|
p.metadata.remove("wetSignatures"); // Clears wet signature image data
|
||||||
|
p.metadata.remove("certificateSubmission"); // Clears keystore bytes + password
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**API Call:**
|
||||||
|
```bash
|
||||||
|
POST /api/v1/security/cert-sign/sessions/{sessionId}/finalize
|
||||||
|
Authorization: Bearer {owner_token}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Response:** Binary PDF file with Content-Disposition header
|
||||||
|
|
||||||
|
## Key Technical Features
|
||||||
|
|
||||||
|
### 1. Double JSON Encoding Fix (Recent)
|
||||||
|
|
||||||
|
**Problem:** JSONB columns were storing JSON strings instead of JSON objects, requiring double-parsing.
|
||||||
|
|
||||||
|
**Solution:** Created `JsonMapConverter` JPA AttributeConverter:
|
||||||
|
```java
|
||||||
|
@Convert(converter = JsonMapConverter.class)
|
||||||
|
@Column(name = "participant_metadata", columnDefinition = "jsonb")
|
||||||
|
private Map<String, Object> participantMetadata;
|
||||||
|
```
|
||||||
|
|
||||||
|
**Benefits:**
|
||||||
|
- Single parse on read
|
||||||
|
- Proper JSON storage in PostgreSQL
|
||||||
|
- Type-safe Map access
|
||||||
|
- Backward compatible with legacy data
|
||||||
|
|
||||||
|
### 2. Signature Progress Display (Recent)
|
||||||
|
|
||||||
|
**Implementation:**
|
||||||
|
- `WorkflowSessionResponse` includes `participantCount` and `signedCount`
|
||||||
|
- `WorkflowMapper` calculates counts when converting to DTO
|
||||||
|
- Frontend displays "X/Y signatures" in session list
|
||||||
|
- Auto-refresh every 15 seconds keeps counts updated
|
||||||
|
|
||||||
|
### 3. Token-Based Security
|
||||||
|
|
||||||
|
**No Authentication Required for Participants:**
|
||||||
|
- Participants access via secure token (UUID)
|
||||||
|
- Token linked to specific participant and session
|
||||||
|
- Automatic expiration support
|
||||||
|
- One-time signing (cannot sign twice)
|
||||||
|
|
||||||
|
**Authenticated Participant Access:**
|
||||||
|
- Registered users can also access sign requests via `/api/v1/security/cert-sign/sign-requests`
|
||||||
|
- Standard Spring Security authentication required
|
||||||
|
- Supports additional cert types: UPLOAD, PEM, PKCS12, PFX
|
||||||
|
|
||||||
|
**Automatic Role Downgrade:**
|
||||||
|
- After signing: EDITOR → VIEWER
|
||||||
|
- After declining: EDITOR → VIEWER
|
||||||
|
- Prevents modification after action taken
|
||||||
|
|
||||||
|
### 4. Storage Integration
|
||||||
|
|
||||||
|
**Unified with File Sharing:**
|
||||||
|
- All PDFs stored via `StorageProvider` (Database or Local)
|
||||||
|
- Respects storage quotas
|
||||||
|
- Supports files up to 100GB+ (with Local storage)
|
||||||
|
- Consistent with existing file sharing infrastructure
|
||||||
|
|
||||||
|
### 5. Certificate Types
|
||||||
|
|
||||||
|
**P12/PKCS12/PFX:** User uploads PKCS#12 file + password
|
||||||
|
**JKS:** User uploads Java KeyStore + password
|
||||||
|
**PEM/UPLOAD:** User uploads PEM certificate + private key
|
||||||
|
**SERVER:** Uses organization's server certificate (no upload needed)
|
||||||
|
**USER_CERT:** Uses user's auto-generated personal certificate (one-click)
|
||||||
|
|
||||||
|
Note: UPLOAD, PEM, PKCS12, PFX are available on the authenticated (`sign-requests`) path. The token-based path uses P12, JKS, SERVER, USER_CERT.
|
||||||
|
|
||||||
|
## Frontend Components Overview
|
||||||
|
|
||||||
|
### Owner Workflow Components
|
||||||
|
|
||||||
|
1. **CreateSessionPanel** - Form to create new signing session
|
||||||
|
2. **ActiveSessionsPanel** - List of pending sessions with progress
|
||||||
|
3. **SessionDetailWorkbenchView** - Full session management interface
|
||||||
|
4. **CompletedSessionsPanel** - History of finalized sessions
|
||||||
|
|
||||||
|
### Participant Workflow Components
|
||||||
|
|
||||||
|
1. **SignRequestWorkbenchView** - Main signing interface
|
||||||
|
2. **SignatureSettingsInput** - Certificate selection and configuration
|
||||||
|
3. **WetSignatureInput** - Draw/type/upload signature overlay
|
||||||
|
4. **SignatureSettingsDisplay** - Preview of signature settings
|
||||||
|
|
||||||
|
### Shared Components
|
||||||
|
|
||||||
|
1. **UserSelector** - Multi-select user picker for participants
|
||||||
|
2. **LocalEmbedPDFWithAnnotations** - PDF viewer with signature placement
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
### Backend Configuration
|
||||||
|
|
||||||
|
**application.properties:**
|
||||||
|
```properties
|
||||||
|
# Database (H2 or PostgreSQL)
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
|
||||||
|
# Security
|
||||||
|
DOCKER_ENABLE_SECURITY=true
|
||||||
|
|
||||||
|
# Storage Provider (DATABASE or LOCAL)
|
||||||
|
storage.provider=LOCAL
|
||||||
|
storage.maxFileSize=100GB
|
||||||
|
```
|
||||||
|
|
||||||
|
### Frontend Configuration
|
||||||
|
|
||||||
|
**Quick Access Bar:**
|
||||||
|
- Signing popout accessible from top navigation
|
||||||
|
- Auto-refresh interval: 15 seconds
|
||||||
|
- Badge shows pending session count
|
||||||
|
|
||||||
|
## API Reference Summary
|
||||||
|
|
||||||
|
### Owner Endpoints (Authenticated)
|
||||||
|
|
||||||
|
| Method | Endpoint | Description |
|
||||||
|
|--------|----------|-------------|
|
||||||
|
| POST | `/api/v1/security/cert-sign/sessions` | Create session |
|
||||||
|
| GET | `/api/v1/security/cert-sign/sessions` | List sessions |
|
||||||
|
| GET | `/api/v1/security/cert-sign/sessions/{id}` | Get details |
|
||||||
|
| POST | `/api/v1/security/cert-sign/sessions/{id}/finalize` | Finalize session |
|
||||||
|
| GET | `/api/v1/security/cert-sign/sessions/{id}/pdf` | Download original |
|
||||||
|
| GET | `/api/v1/security/cert-sign/sessions/{id}/signed-pdf` | Download signed |
|
||||||
|
| DELETE | `/api/v1/security/cert-sign/sessions/{id}` | Delete session |
|
||||||
|
| POST | `/api/v1/security/cert-sign/sessions/{id}/participants` | Add participants |
|
||||||
|
| DELETE | `/api/v1/security/cert-sign/sessions/{id}/participants/{pid}` | Remove participant |
|
||||||
|
|
||||||
|
### Authenticated Participant Endpoints
|
||||||
|
|
||||||
|
| Method | Endpoint | Description |
|
||||||
|
|--------|----------|-------------|
|
||||||
|
| GET | `/api/v1/security/cert-sign/sign-requests` | List sign requests |
|
||||||
|
| GET | `/api/v1/security/cert-sign/sign-requests/{id}` | Get sign request details |
|
||||||
|
| GET | `/api/v1/security/cert-sign/sign-requests/{id}/document` | Download document |
|
||||||
|
| POST | `/api/v1/security/cert-sign/sign-requests/{id}/sign` | Sign document |
|
||||||
|
| POST | `/api/v1/security/cert-sign/sign-requests/{id}/decline` | Decline signing |
|
||||||
|
|
||||||
|
### Token-Based Participant Endpoints (No Auth Required)
|
||||||
|
|
||||||
|
| Method | Endpoint | Description |
|
||||||
|
|--------|----------|-------------|
|
||||||
|
| GET | `/api/v1/workflow/participant/session?token={token}` | View session |
|
||||||
|
| GET | `/api/v1/workflow/participant/details?token={token}` | Get participant details |
|
||||||
|
| GET | `/api/v1/workflow/participant/document?token={token}` | Download PDF |
|
||||||
|
| POST | `/api/v1/workflow/participant/submit-signature` | Submit signature |
|
||||||
|
| POST | `/api/v1/workflow/participant/decline?token={token}` | Decline signing |
|
||||||
|
|
||||||
|
## Security Considerations
|
||||||
|
|
||||||
|
### Data Protection
|
||||||
|
- Wet signature image data cleared after finalization (GDPR compliance)
|
||||||
|
- Certificate submission data (keystore bytes + password) cleared after finalization (GDPR compliance)
|
||||||
|
- Certificate passwords are not encrypted at rest while stored (TODO: encrypt at rest)
|
||||||
|
- Token expiration support
|
||||||
|
|
||||||
|
### Access Control
|
||||||
|
- Owner authentication required for session management
|
||||||
|
- Participant access via secure UUID tokens (no auth) or standard auth (sign-requests)
|
||||||
|
- Automatic role downgrade prevents re-signing
|
||||||
|
- Session status checks prevent unauthorized actions
|
||||||
|
|
||||||
|
### Audit Trail
|
||||||
|
- All participant actions tracked
|
||||||
|
- FileShare access logged
|
||||||
|
- Status transitions recorded
|
||||||
|
- Notification history maintained
|
||||||
|
|
||||||
|
## Performance Characteristics
|
||||||
|
|
||||||
|
### Scalability
|
||||||
|
- Supports PDFs up to 100GB+ (with Local storage provider)
|
||||||
|
- Memory-efficient streaming for large files
|
||||||
|
- IndexedDB caching on frontend
|
||||||
|
- Database indexes on session_id, share_token, workflow_session_id
|
||||||
|
|
||||||
|
### Response Times
|
||||||
|
- Session creation: ~500ms (10MB file)
|
||||||
|
- Session listing: ~100ms
|
||||||
|
- Token validation: ~50ms
|
||||||
|
- Finalization: ~2s per MB of PDF (varies by certificate operations)
|
||||||
|
|
||||||
|
## Future Enhancements
|
||||||
|
|
||||||
|
### Planned Features
|
||||||
|
- Email notifications for participants
|
||||||
|
- Reminder system for pending signatures
|
||||||
|
- Bulk signing operations
|
||||||
|
- Template-based signing workflows
|
||||||
|
- Signature validation/verification UI
|
||||||
|
- Certificate password encryption at rest
|
||||||
|
- Certificate keystore cleanup after finalization (GDPR)
|
||||||
|
- Webhook support for external integrations
|
||||||
|
- Analytics dashboard for signing metrics
|
||||||
|
|
||||||
|
### Additional Workflow Types
|
||||||
|
- **REVIEW** - Document review with comments
|
||||||
|
- **APPROVAL** - Multi-level approval chains
|
||||||
|
- **COLLABORATION** - Real-time collaborative editing
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Common Issues
|
||||||
|
|
||||||
|
**"Token invalid" error:**
|
||||||
|
- Check token exists in workflow_participants table
|
||||||
|
- Verify session is not finalized
|
||||||
|
- Check expiration date (expires_at)
|
||||||
|
|
||||||
|
**Signature not appearing on PDF:**
|
||||||
|
- Verify certificate type is correct
|
||||||
|
- Check certificate password
|
||||||
|
- Review logs for signing errors
|
||||||
|
- Ensure PDFDocumentFactory is available
|
||||||
|
|
||||||
|
**"Awaiting signatures" not updating:**
|
||||||
|
- Backend should return participantCount and signedCount
|
||||||
|
- Frontend auto-refresh every 15 seconds
|
||||||
|
- Check network tab for API errors
|
||||||
|
|
||||||
|
**Wet signatures not visible after finalization:**
|
||||||
|
- Wet signatures are applied first as image overlays (Step 1)
|
||||||
|
- Check `wetSignaturesData` was sent as valid JSON array
|
||||||
|
- Verify page index is within document bounds
|
||||||
|
- Note: wet signatures survive regardless of `includeSummaryPage` setting
|
||||||
|
|
||||||
|
### Debug Queries
|
||||||
|
|
||||||
|
```sql
|
||||||
|
-- Check session status
|
||||||
|
SELECT session_id, status, finalized,
|
||||||
|
(SELECT COUNT(*) FROM workflow_participants WHERE workflow_session_id = ws.id) as participant_count,
|
||||||
|
(SELECT COUNT(*) FROM workflow_participants WHERE workflow_session_id = ws.id AND status = 'SIGNED') as signed_count
|
||||||
|
FROM workflow_sessions ws;
|
||||||
|
|
||||||
|
-- Check participant tokens
|
||||||
|
SELECT email, status, share_token, expires_at
|
||||||
|
FROM workflow_participants
|
||||||
|
WHERE workflow_session_id = (SELECT id FROM workflow_sessions WHERE session_id = '{session_id}');
|
||||||
|
|
||||||
|
-- Check metadata storage
|
||||||
|
SELECT email,
|
||||||
|
participant_metadata->'certificateSubmission'->>'certType' as cert_type,
|
||||||
|
jsonb_array_length(participant_metadata->'wetSignatures') as wet_sig_count
|
||||||
|
FROM workflow_participants;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
The Shared Signing feature provides a complete collaborative signing workflow with:
|
||||||
|
- ✅ Multi-participant support with progress tracking
|
||||||
|
- ✅ Multiple certificate types (P12/PKCS12/PFX, JKS, PEM, SERVER, USER_CERT)
|
||||||
|
- ✅ Visual wet signature overlays (multiple per participant)
|
||||||
|
- ✅ Token-based security for unauthenticated participants
|
||||||
|
- ✅ Authenticated participant access via sign-requests API
|
||||||
|
- ✅ Automatic role management
|
||||||
|
- ✅ Large file support (100GB+)
|
||||||
|
- ✅ GDPR-compliant wet signature metadata cleanup
|
||||||
|
- ✅ Real-time progress updates
|
||||||
|
- ✅ Full frontend integration with Quick Access Bar
|
||||||
|
- ✅ Optional signature summary page with logo and participant details
|
||||||
|
|
||||||
|
The architecture leverages existing file sharing infrastructure while adding workflow-specific features, ensuring consistency and maintainability across the application.
|
||||||
@@ -27,10 +27,10 @@ spotless {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
api 'com.google.guava:guava:33.4.8-jre'
|
api 'com.google.guava:guava:33.5.0-jre'
|
||||||
api 'org.springframework.boot:spring-boot-starter-webmvc'
|
api 'org.springframework.boot:spring-boot-starter-webmvc'
|
||||||
api 'org.springframework.boot:spring-boot-starter-aspectj'
|
api 'org.springframework.boot:spring-boot-starter-aspectj'
|
||||||
api 'com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer:20260102.1'
|
api 'com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer:20260313.1'
|
||||||
api 'com.fathzer:javaluator:3.0.6'
|
api 'com.fathzer:javaluator:3.0.6'
|
||||||
api 'com.posthog.java:posthog:1.2.0'
|
api 'com.posthog.java:posthog:1.2.0'
|
||||||
api 'org.apache.commons:commons-lang3:3.20.0'
|
api 'org.apache.commons:commons-lang3:3.20.0'
|
||||||
@@ -43,7 +43,7 @@ dependencies {
|
|||||||
api 'com.github.junrar:junrar:7.5.8' // RAR archive support for CBR files
|
api 'com.github.junrar:junrar:7.5.8' // RAR archive support for CBR files
|
||||||
api 'jakarta.servlet:jakarta.servlet-api:6.1.0'
|
api 'jakarta.servlet:jakarta.servlet-api:6.1.0'
|
||||||
api 'org.snakeyaml:snakeyaml-engine:3.0.1'
|
api 'org.snakeyaml:snakeyaml-engine:3.0.1'
|
||||||
api "org.springdoc:springdoc-openapi-starter-webmvc-ui:3.0.1"
|
api "org.springdoc:springdoc-openapi-starter-webmvc-ui:3.0.2"
|
||||||
// Simple Java Mail for EML/MSG parsing (replaces direct Angus Mail usage)
|
// Simple Java Mail for EML/MSG parsing (replaces direct Angus Mail usage)
|
||||||
api 'org.simplejavamail:simple-java-mail:8.12.6'
|
api 'org.simplejavamail:simple-java-mail:8.12.6'
|
||||||
api 'org.simplejavamail:outlook-module:8.12.6' // MSG file support
|
api 'org.simplejavamail:outlook-module:8.12.6' // MSG file support
|
||||||
|
|||||||
@@ -356,6 +356,7 @@ public class EndpointConfiguration {
|
|||||||
addEndpointToGroup("Security", "cert-sign");
|
addEndpointToGroup("Security", "cert-sign");
|
||||||
addEndpointToGroup("Security", "remove-cert-sign");
|
addEndpointToGroup("Security", "remove-cert-sign");
|
||||||
addEndpointToGroup("Security", "sanitize-pdf");
|
addEndpointToGroup("Security", "sanitize-pdf");
|
||||||
|
addEndpointToGroup("Security", "timestamp-pdf");
|
||||||
addEndpointToGroup("Security", "auto-redact");
|
addEndpointToGroup("Security", "auto-redact");
|
||||||
addEndpointToGroup("Security", "validate-signature");
|
addEndpointToGroup("Security", "validate-signature");
|
||||||
addEndpointToGroup("Security", "add-stamp");
|
addEndpointToGroup("Security", "add-stamp");
|
||||||
@@ -373,7 +374,6 @@ public class EndpointConfiguration {
|
|||||||
addEndpointToGroup("Other", REMOVE_BLANKS);
|
addEndpointToGroup("Other", REMOVE_BLANKS);
|
||||||
addEndpointToGroup("Other", "remove-annotations");
|
addEndpointToGroup("Other", "remove-annotations");
|
||||||
addEndpointToGroup("Other", "get-info-on-pdf");
|
addEndpointToGroup("Other", "get-info-on-pdf");
|
||||||
addEndpointToGroup("Other", "remove-image-pdf");
|
|
||||||
addEndpointToGroup("Other", "add-attachments");
|
addEndpointToGroup("Other", "add-attachments");
|
||||||
addEndpointToGroup("Other", "replace-invert-pdf");
|
addEndpointToGroup("Other", "replace-invert-pdf");
|
||||||
addEndpointToGroup("Other", "edit-table-of-contents");
|
addEndpointToGroup("Other", "edit-table-of-contents");
|
||||||
@@ -473,6 +473,7 @@ public class EndpointConfiguration {
|
|||||||
addEndpointToGroup("Java", "auto-rename");
|
addEndpointToGroup("Java", "auto-rename");
|
||||||
addEndpointToGroup("Java", "auto-split-pdf");
|
addEndpointToGroup("Java", "auto-split-pdf");
|
||||||
addEndpointToGroup("Java", "sanitize-pdf");
|
addEndpointToGroup("Java", "sanitize-pdf");
|
||||||
|
addEndpointToGroup("Java", "timestamp-pdf");
|
||||||
addEndpointToGroup("Java", "crop");
|
addEndpointToGroup("Java", "crop");
|
||||||
addEndpointToGroup("Java", "get-info-on-pdf");
|
addEndpointToGroup("Java", "get-info-on-pdf");
|
||||||
addEndpointToGroup("Java", "pdf-to-single-page");
|
addEndpointToGroup("Java", "pdf-to-single-page");
|
||||||
@@ -488,7 +489,6 @@ public class EndpointConfiguration {
|
|||||||
addEndpointToGroup("Java", REMOVE_BLANKS);
|
addEndpointToGroup("Java", REMOVE_BLANKS);
|
||||||
addEndpointToGroup("Java", "remove-annotations");
|
addEndpointToGroup("Java", "remove-annotations");
|
||||||
addEndpointToGroup("Java", "pdf-to-text");
|
addEndpointToGroup("Java", "pdf-to-text");
|
||||||
addEndpointToGroup("Java", "remove-image-pdf");
|
|
||||||
addEndpointToGroup("Java", "pdf-to-markdown");
|
addEndpointToGroup("Java", "pdf-to-markdown");
|
||||||
addEndpointToGroup("Java", "add-attachments");
|
addEndpointToGroup("Java", "add-attachments");
|
||||||
addEndpointToGroup("Java", "compress-pdf");
|
addEndpointToGroup("Java", "compress-pdf");
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ public class ApplicationProperties {
|
|||||||
private Legal legal = new Legal();
|
private Legal legal = new Legal();
|
||||||
private Security security = new Security();
|
private Security security = new Security();
|
||||||
private System system = new System();
|
private System system = new System();
|
||||||
|
private Storage storage = new Storage();
|
||||||
private Ui ui = new Ui();
|
private Ui ui = new Ui();
|
||||||
private Endpoints endpoints = new Endpoints();
|
private Endpoints endpoints = new Endpoints();
|
||||||
private Metrics metrics = new Metrics();
|
private Metrics metrics = new Metrics();
|
||||||
@@ -150,6 +151,44 @@ public class ApplicationProperties {
|
|||||||
@Data
|
@Data
|
||||||
public static class AutoPipeline {
|
public static class AutoPipeline {
|
||||||
private String outputFolder;
|
private String outputFolder;
|
||||||
|
private FileReadiness fileReadiness = new FileReadiness();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration for the {@link stirling.software.common.util.FileReadinessChecker}.
|
||||||
|
* Controls how the pipeline determines whether a file is fully written and stable before
|
||||||
|
* processing begins.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public static class FileReadiness {
|
||||||
|
/**
|
||||||
|
* Master toggle. When {@code false} every readiness check is skipped and all files are
|
||||||
|
* considered immediately ready (preserves legacy behaviour).
|
||||||
|
*/
|
||||||
|
private boolean enabled = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* How long (in milliseconds) a file must remain unmodified before it is considered
|
||||||
|
* stable. Files modified more recently than this threshold are skipped and retried on
|
||||||
|
* the next scan cycle. Default: 5 000 ms (5 seconds).
|
||||||
|
*/
|
||||||
|
private long settleTimeMillis = 5000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* How long (in milliseconds) to pause between two consecutive file-size reads when
|
||||||
|
* checking whether a file is still being written. If the size differs between the two
|
||||||
|
* reads the file is considered unstable. This catches active copies on Linux/macOS
|
||||||
|
* where advisory locking alone cannot detect a mid-copy file. Default: 500 ms.
|
||||||
|
*/
|
||||||
|
private long sizeCheckDelayMillis = 500;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional list of file extensions (without the leading dot, case-insensitive) that are
|
||||||
|
* allowed through the readiness check. An empty list means all extensions are accepted.
|
||||||
|
* Example: {@code ["pdf", "tiff"]} will skip any file whose extension is not {@code
|
||||||
|
* pdf} or {@code tiff}.
|
||||||
|
*/
|
||||||
|
private List<String> allowedExtensions = new java.util.ArrayList<>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -213,6 +252,7 @@ public class ApplicationProperties {
|
|||||||
private String customGlobalAPIKey;
|
private String customGlobalAPIKey;
|
||||||
private Jwt jwt = new Jwt();
|
private Jwt jwt = new Jwt();
|
||||||
private Validation validation = new Validation();
|
private Validation validation = new Validation();
|
||||||
|
private Timestamp timestamp = new Timestamp();
|
||||||
private String xFrameOptions = "DENY";
|
private String xFrameOptions = "DENY";
|
||||||
|
|
||||||
public Boolean isAltLogin() {
|
public Boolean isAltLogin() {
|
||||||
@@ -531,6 +571,12 @@ public class ApplicationProperties {
|
|||||||
private boolean hardFail = false;
|
private boolean hardFail = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Timestamp {
|
||||||
|
private String defaultTsaUrl = "http://timestamp.digicert.com";
|
||||||
|
private List<String> customTsaUrls = new ArrayList<>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -589,6 +635,41 @@ public class ApplicationProperties {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Storage {
|
||||||
|
private boolean enabled = false;
|
||||||
|
private String provider = "local";
|
||||||
|
private Local local = new Local();
|
||||||
|
private Quotas quotas = new Quotas();
|
||||||
|
private Sharing sharing = new Sharing();
|
||||||
|
private Signing signing = new Signing();
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Local {
|
||||||
|
private String basePath = InstallationPathConfig.getPath() + "storage";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Sharing {
|
||||||
|
private boolean enabled = false;
|
||||||
|
private boolean linkEnabled = false;
|
||||||
|
private boolean emailEnabled = false;
|
||||||
|
private int linkExpirationDays = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Quotas {
|
||||||
|
private long maxStorageMbPerUser = -1;
|
||||||
|
private long maxStorageMbTotal = -1;
|
||||||
|
private long maxFileMb = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Signing {
|
||||||
|
private boolean enabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class DatabaseBackup {
|
public static class DatabaseBackup {
|
||||||
private String cron = "0 0 0 * * ?"; // daily at midnight
|
private String cron = "0 0 0 * * ?"; // daily at midnight
|
||||||
@@ -696,8 +777,7 @@ public class ApplicationProperties {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return
|
return """
|
||||||
"""
|
|
||||||
Driver {
|
Driver {
|
||||||
driverName='%s'
|
driverName='%s'
|
||||||
}
|
}
|
||||||
|
|||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
package stirling.software.common.model.api.security;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class UserSummaryDTO {
|
||||||
|
private Long userId;
|
||||||
|
private String username;
|
||||||
|
private String displayName;
|
||||||
|
private String teamName;
|
||||||
|
private boolean enabled;
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package stirling.software.common.service;
|
||||||
|
|
||||||
|
import java.security.KeyStore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstraction for PDF digital signature operations. Defined in common so that proprietary services
|
||||||
|
* can use it without creating a circular dependency on core.
|
||||||
|
*/
|
||||||
|
public interface PdfSigningService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signs a PDF document using the provided KeyStore.
|
||||||
|
*
|
||||||
|
* @param pdfBytes raw PDF bytes to sign
|
||||||
|
* @param keystore the KeyStore containing the signing key and certificate chain
|
||||||
|
* @param password keystore password
|
||||||
|
* @param showSignature whether to render a visible signature block
|
||||||
|
* @param pageNumber 0-indexed page on which to render the visible signature (may be null)
|
||||||
|
* @param name signer name embedded in the signature
|
||||||
|
* @param location location string embedded in the signature
|
||||||
|
* @param reason reason string embedded in the signature
|
||||||
|
* @param showLogo whether to include the Stirling-PDF logo in the visible signature
|
||||||
|
* @return signed PDF bytes
|
||||||
|
* @throws Exception on any signing failure
|
||||||
|
*/
|
||||||
|
byte[] signWithKeystore(
|
||||||
|
byte[] pdfBytes,
|
||||||
|
KeyStore keystore,
|
||||||
|
char[] password,
|
||||||
|
boolean showSignature,
|
||||||
|
Integer pageNumber,
|
||||||
|
String name,
|
||||||
|
String location,
|
||||||
|
String reason,
|
||||||
|
boolean showLogo)
|
||||||
|
throws Exception;
|
||||||
|
}
|
||||||
+14
-1
@@ -226,10 +226,11 @@ public class SsrfProtectionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPrivateIPv4Range(String ip) {
|
private boolean isPrivateIPv4Range(String ip) {
|
||||||
// Includes RFC1918, loopback, link-local, and unspecified addresses
|
// Includes RFC1918, RFC6598, loopback, link-local, and unspecified addresses
|
||||||
return ip.startsWith("10.")
|
return ip.startsWith("10.")
|
||||||
|| ip.startsWith("192.168.")
|
|| ip.startsWith("192.168.")
|
||||||
|| (ip.startsWith("172.") && isInRange172(ip))
|
|| (ip.startsWith("172.") && isInRange172(ip))
|
||||||
|
|| (ip.startsWith("100.") && isInRange100(ip))
|
||||||
|| ip.startsWith("169.254.")
|
|| ip.startsWith("169.254.")
|
||||||
|| ip.startsWith("127.")
|
|| ip.startsWith("127.")
|
||||||
|| "0.0.0.0".equals(ip);
|
|| "0.0.0.0".equals(ip);
|
||||||
@@ -247,6 +248,18 @@ public class SsrfProtectionService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isInRange100(String ip) {
|
||||||
|
String[] parts = ip.split("\\.");
|
||||||
|
if (parts.length >= 2) {
|
||||||
|
try {
|
||||||
|
int secondOctet = Integer.parseInt(parts[1]);
|
||||||
|
return secondOctet >= 64 && secondOctet <= 127;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isCloudMetadataAddress(String ip) {
|
private boolean isCloudMetadataAddress(String ip) {
|
||||||
String normalizedIp = normalizeIpv4MappedAddress(ip);
|
String normalizedIp = normalizeIpv4MappedAddress(ip);
|
||||||
// Cloud metadata endpoints for AWS, GCP, Azure, Oracle Cloud, and IBM Cloud
|
// Cloud metadata endpoints for AWS, GCP, Azure, Oracle Cloud, and IBM Cloud
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ public interface UserServiceInterface {
|
|||||||
|
|
||||||
String getCurrentUsername();
|
String getCurrentUsername();
|
||||||
|
|
||||||
|
String getCurrentUserApiKey();
|
||||||
|
|
||||||
long getTotalUsersCount();
|
long getTotalUsersCount();
|
||||||
|
|
||||||
boolean isCurrentUserAdmin();
|
boolean isCurrentUserAdmin();
|
||||||
|
|||||||
@@ -341,8 +341,7 @@ public class EmlProcessingUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getFallbackStyles() {
|
private String getFallbackStyles() {
|
||||||
return
|
return """
|
||||||
"""
|
|
||||||
/* Minimal fallback - main CSS resource failed to load */
|
/* Minimal fallback - main CSS resource failed to load */
|
||||||
body {
|
body {
|
||||||
font-family: var(--font-family, Helvetica, sans-serif);
|
font-family: var(--font-family, Helvetica, sans-serif);
|
||||||
|
|||||||
@@ -13,11 +13,18 @@ public class EmlToPdf {
|
|||||||
|
|
||||||
public static String convertEmlToHtml(byte[] emlBytes, EmlToPdfRequest request)
|
public static String convertEmlToHtml(byte[] emlBytes, EmlToPdfRequest request)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
return convertEmlToHtml(emlBytes, request, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String convertEmlToHtml(
|
||||||
|
byte[] emlBytes, EmlToPdfRequest request, CustomHtmlSanitizer customHtmlSanitizer)
|
||||||
|
throws IOException {
|
||||||
EmlProcessingUtils.validateEmlInput(emlBytes);
|
EmlProcessingUtils.validateEmlInput(emlBytes);
|
||||||
|
|
||||||
EmlParser.EmailContent emailContent =
|
EmlParser.EmailContent emailContent =
|
||||||
EmlParser.extractEmailContent(emlBytes, request, null);
|
EmlParser.extractEmailContent(emlBytes, request, customHtmlSanitizer);
|
||||||
return EmlProcessingUtils.generateEnhancedEmailHtml(emailContent, request, null);
|
return EmlProcessingUtils.generateEnhancedEmailHtml(
|
||||||
|
emailContent, request, customHtmlSanitizer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] convertEmlToPdf(
|
public static byte[] convertEmlToPdf(
|
||||||
|
|||||||
@@ -112,8 +112,6 @@ public class FileMonitor {
|
|||||||
All files observed changes in the last iteration will be considered as staging files.
|
All files observed changes in the last iteration will be considered as staging files.
|
||||||
If those files are not modified in current iteration, they will be considered as ready for processing.
|
If those files are not modified in current iteration, they will be considered as ready for processing.
|
||||||
*/
|
*/
|
||||||
stagingFiles = new HashSet<>(newlyDiscoveredFiles);
|
|
||||||
readyForProcessingFiles.clear();
|
|
||||||
|
|
||||||
if (path2KeyMapping.isEmpty()) {
|
if (path2KeyMapping.isEmpty()) {
|
||||||
log.warn("Not monitoring any directories; attempting to re-register root paths.");
|
log.warn("Not monitoring any directories; attempting to re-register root paths.");
|
||||||
@@ -129,8 +127,19 @@ public class FileMonitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WatchKey key;
|
// Skip expensive collection work when there is nothing to track
|
||||||
while ((key = watchService.poll()) != null) {
|
WatchKey firstKey = watchService.poll();
|
||||||
|
if (firstKey == null
|
||||||
|
&& newlyDiscoveredFiles.isEmpty()
|
||||||
|
&& readyForProcessingFiles.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
stagingFiles = new HashSet<>(newlyDiscoveredFiles);
|
||||||
|
readyForProcessingFiles.clear();
|
||||||
|
|
||||||
|
WatchKey key = firstKey;
|
||||||
|
while (key != null) {
|
||||||
final Path watchingDir = (Path) key.watchable();
|
final Path watchingDir = (Path) key.watchable();
|
||||||
key.pollEvents()
|
key.pollEvents()
|
||||||
.forEach(
|
.forEach(
|
||||||
@@ -167,6 +176,7 @@ public class FileMonitor {
|
|||||||
if (!isKeyValid) { // key is invalid when the directory itself is no longer exists
|
if (!isKeyValid) { // key is invalid when the directory itself is no longer exists
|
||||||
path2KeyMapping.remove((Path) key.watchable());
|
path2KeyMapping.remove((Path) key.watchable());
|
||||||
}
|
}
|
||||||
|
key = watchService.poll();
|
||||||
}
|
}
|
||||||
readyForProcessingFiles.addAll(stagingFiles);
|
readyForProcessingFiles.addAll(stagingFiles);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,217 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.nio.channels.FileLock;
|
||||||
|
import java.nio.channels.OverlappingFileLockException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
|
import stirling.software.common.model.ApplicationProperties.AutoPipeline.FileReadiness;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stateless safety checker that decides whether a file is stable and ready for pipeline processing.
|
||||||
|
* Call {@link #isReady(Path)} before moving or processing any file picked up from a watched folder.
|
||||||
|
*
|
||||||
|
* <p>A file is considered ready when ALL of the following hold:
|
||||||
|
*
|
||||||
|
* <ol>
|
||||||
|
* <li>The file exists on disk.
|
||||||
|
* <li>The path refers to a regular file, not a directory.
|
||||||
|
* <li>The file's extension matches the configured allow-list (if one is set).
|
||||||
|
* <li>The file has not been modified within the configured settle window ({@code
|
||||||
|
* settleTimeMillis}), meaning it is no longer being written.
|
||||||
|
* <li>The file size is stable: two reads separated by {@code sizeCheckDelayMillis} return the
|
||||||
|
* same value. This catches active copies on Linux/macOS where advisory file locking alone
|
||||||
|
* cannot detect a mid-copy file.
|
||||||
|
* <li>An exclusive file-system lock can be acquired, confirming no other process holds it.
|
||||||
|
* </ol>
|
||||||
|
*
|
||||||
|
* <p>All behaviour is controlled through {@link FileReadiness} inside {@link
|
||||||
|
* ApplicationProperties.AutoPipeline}. Setting {@code enabled: false} makes every call return
|
||||||
|
* {@code true} so the checker is a no-op drop-in.
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class FileReadinessChecker {
|
||||||
|
|
||||||
|
private final ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} when the file at {@code path} passes every readiness check and is safe
|
||||||
|
* to hand off to the pipeline for processing. Returns {@code false} when any check fails; the
|
||||||
|
* caller should skip the file and retry on the next scan cycle.
|
||||||
|
*/
|
||||||
|
public boolean isReady(Path path) {
|
||||||
|
FileReadiness config = applicationProperties.getAutoPipeline().getFileReadiness();
|
||||||
|
|
||||||
|
if (!config.isEnabled()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!existsAsRegularFile(path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isExtensionAllowed(path, config.getAllowedExtensions())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasSettled(path, config.getSettleTimeMillis())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasSizeStabilized(path, config.getSizeCheckDelayMillis())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isLocked(path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Individual checks
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
private boolean existsAsRegularFile(Path path) {
|
||||||
|
if (!Files.exists(path)) {
|
||||||
|
log.debug("File does not exist, skipping: {}", path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!Files.isRegularFile(path)) {
|
||||||
|
log.debug("Path is not a regular file (directory or symlink?), skipping: {}", path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} when {@code allowedExtensions} is empty (no filter) or when the file's
|
||||||
|
* extension (case-insensitive) appears in the list.
|
||||||
|
*/
|
||||||
|
private boolean isExtensionAllowed(Path path, List<String> allowedExtensions) {
|
||||||
|
if (allowedExtensions == null || allowedExtensions.isEmpty()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
String filename = path.getFileName().toString();
|
||||||
|
String extension =
|
||||||
|
filename.contains(".")
|
||||||
|
? filename.substring(filename.lastIndexOf('.') + 1).toLowerCase(Locale.ROOT)
|
||||||
|
: "";
|
||||||
|
boolean allowed =
|
||||||
|
allowedExtensions.stream().anyMatch(ext -> ext.equalsIgnoreCase(extension));
|
||||||
|
if (!allowed) {
|
||||||
|
log.debug(
|
||||||
|
"File '{}' has extension '{}' which is not in the allowed list {}, skipping",
|
||||||
|
filename,
|
||||||
|
extension,
|
||||||
|
allowedExtensions);
|
||||||
|
}
|
||||||
|
return allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} when the file's last-modified timestamp is at least {@code
|
||||||
|
* settleTimeMillis} milliseconds in the past, indicating the write has completed and the file
|
||||||
|
* has "settled".
|
||||||
|
*/
|
||||||
|
private boolean hasSettled(Path path, long settleTimeMillis) {
|
||||||
|
try {
|
||||||
|
long lastModified = Files.getLastModifiedTime(path).toMillis();
|
||||||
|
long ageMillis = System.currentTimeMillis() - lastModified;
|
||||||
|
boolean settled = ageMillis >= settleTimeMillis;
|
||||||
|
if (!settled) {
|
||||||
|
log.debug(
|
||||||
|
"File '{}' was modified {}ms ago (settle threshold: {}ms), not yet ready",
|
||||||
|
path.getFileName(),
|
||||||
|
ageMillis,
|
||||||
|
settleTimeMillis);
|
||||||
|
}
|
||||||
|
return settled;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn(
|
||||||
|
"Could not read last-modified time for '{}', treating as not settled: {}",
|
||||||
|
path,
|
||||||
|
e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} when the file size is the same before and after a short pause of {@code
|
||||||
|
* sizeCheckDelayMillis} milliseconds. A size change indicates another process is still
|
||||||
|
* appending to the file. This is the primary write-detection mechanism on Linux/macOS, where
|
||||||
|
* mandatory file locking is not enforced by the OS.
|
||||||
|
*/
|
||||||
|
private boolean hasSizeStabilized(Path path, long sizeCheckDelayMillis) {
|
||||||
|
try {
|
||||||
|
long sizeBefore = Files.size(path);
|
||||||
|
Thread.sleep(sizeCheckDelayMillis);
|
||||||
|
long sizeAfter = Files.size(path);
|
||||||
|
boolean stable = sizeBefore == sizeAfter;
|
||||||
|
if (!stable) {
|
||||||
|
log.debug(
|
||||||
|
"File '{}' size changed from {} to {} bytes during stability check,"
|
||||||
|
+ " not yet ready",
|
||||||
|
path.getFileName(),
|
||||||
|
sizeBefore,
|
||||||
|
sizeAfter);
|
||||||
|
}
|
||||||
|
return stable;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn(
|
||||||
|
"Could not read file size for '{}', treating as unstable: {}",
|
||||||
|
path,
|
||||||
|
e.getMessage());
|
||||||
|
return false;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
log.warn(
|
||||||
|
"Size stability check interrupted for '{}', treating as unstable",
|
||||||
|
path.getFileName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} when an exclusive file-system lock cannot be acquired, which indicates
|
||||||
|
* another process still holds the file open for writing.
|
||||||
|
*
|
||||||
|
* <p>{@link OverlappingFileLockException} is also treated as locked: the JVM already holds a
|
||||||
|
* lock on this file (e.g. from another thread), so it is unsafe to process.
|
||||||
|
*/
|
||||||
|
private boolean isLocked(Path path) {
|
||||||
|
try (RandomAccessFile raf = new RandomAccessFile(path.toFile(), "rw");
|
||||||
|
FileChannel channel = raf.getChannel()) {
|
||||||
|
FileLock lock = channel.tryLock();
|
||||||
|
if (lock == null) {
|
||||||
|
log.debug("File '{}' is locked by another process", path.getFileName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
lock.release();
|
||||||
|
return false;
|
||||||
|
} catch (OverlappingFileLockException e) {
|
||||||
|
log.debug("File '{}' is already locked by this JVM", path.getFileName());
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.debug(
|
||||||
|
"Could not acquire lock on '{}', treating as locked: {}",
|
||||||
|
path.getFileName(),
|
||||||
|
e.getMessage());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -86,7 +86,6 @@ public class RequestUriUtils {
|
|||||||
// Blocklist of backend/non-frontend paths that should still go through filters
|
// Blocklist of backend/non-frontend paths that should still go through filters
|
||||||
String[] backendOnlyPrefixes = {
|
String[] backendOnlyPrefixes = {
|
||||||
"/register",
|
"/register",
|
||||||
"/invite",
|
|
||||||
"/pipeline",
|
"/pipeline",
|
||||||
"/pdfjs",
|
"/pdfjs",
|
||||||
"/pdfjs-legacy",
|
"/pdfjs-legacy",
|
||||||
@@ -181,7 +180,9 @@ public class RequestUriUtils {
|
|||||||
|| trimmedUri.startsWith("/readiness")
|
|| trimmedUri.startsWith("/readiness")
|
||||||
|| trimmedUri.startsWith(
|
|| trimmedUri.startsWith(
|
||||||
"/api/v1/mobile-scanner/") // Mobile scanner endpoints (no auth)
|
"/api/v1/mobile-scanner/") // Mobile scanner endpoints (no auth)
|
||||||
|| trimmedUri.startsWith("/v1/api-docs");
|
|| trimmedUri.startsWith("/v1/api-docs")
|
||||||
|
// Workflow participant endpoints — access controlled by share tokens, not login
|
||||||
|
|| trimmedUri.startsWith("/api/v1/workflow/participant/");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String stripContextPath(String contextPath, String requestURI) {
|
private static String stripContextPath(String contextPath, String requestURI) {
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
|
||||||
|
class AppArgsCaptureTest {
|
||||||
|
|
||||||
|
private AppArgsCapture capture;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
capture = new AppArgsCapture();
|
||||||
|
AppArgsCapture.APP_ARGS.set(List.of());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void run_withArgs_capturesArgs() {
|
||||||
|
ApplicationArguments args = mock(ApplicationArguments.class);
|
||||||
|
when(args.getSourceArgs()).thenReturn(new String[] {"--server.port=8080", "--debug"});
|
||||||
|
capture.run(args);
|
||||||
|
assertEquals(List.of("--server.port=8080", "--debug"), AppArgsCapture.APP_ARGS.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void run_withNoArgs_capturesEmptyList() {
|
||||||
|
ApplicationArguments args = mock(ApplicationArguments.class);
|
||||||
|
when(args.getSourceArgs()).thenReturn(new String[] {});
|
||||||
|
capture.run(args);
|
||||||
|
assertEquals(List.of(), AppArgsCapture.APP_ARGS.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void run_calledTwice_overwritesPreviousArgs() {
|
||||||
|
ApplicationArguments args1 = mock(ApplicationArguments.class);
|
||||||
|
when(args1.getSourceArgs()).thenReturn(new String[] {"--first"});
|
||||||
|
capture.run(args1);
|
||||||
|
assertEquals(List.of("--first"), AppArgsCapture.APP_ARGS.get());
|
||||||
|
|
||||||
|
ApplicationArguments args2 = mock(ApplicationArguments.class);
|
||||||
|
when(args2.getSourceArgs()).thenReturn(new String[] {"--second", "--third"});
|
||||||
|
capture.run(args2);
|
||||||
|
assertEquals(List.of("--second", "--third"), AppArgsCapture.APP_ARGS.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void appArgs_defaultValue_isEmptyList() {
|
||||||
|
// After setUp resets it
|
||||||
|
assertTrue(AppArgsCapture.APP_ARGS.get().isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
+108
@@ -0,0 +1,108 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
|
class ApplicationContextProviderTest {
|
||||||
|
|
||||||
|
private ApplicationContextProvider provider;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
provider = new ApplicationContextProvider();
|
||||||
|
// Reset to null state
|
||||||
|
provider.setApplicationContext(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
// Clean up static state
|
||||||
|
provider.setApplicationContext(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getBean_byClass_whenNoContext_returnsNull() {
|
||||||
|
provider.setApplicationContext(null);
|
||||||
|
assertNull(ApplicationContextProvider.getBean(String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getBean_byClass_whenBeanExists_returnsBean() {
|
||||||
|
ApplicationContext ctx = mock(ApplicationContext.class);
|
||||||
|
when(ctx.getBean(String.class)).thenReturn("hello");
|
||||||
|
provider.setApplicationContext(ctx);
|
||||||
|
assertEquals("hello", ApplicationContextProvider.getBean(String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getBean_byClass_whenBeanNotFound_returnsNull() {
|
||||||
|
ApplicationContext ctx = mock(ApplicationContext.class);
|
||||||
|
when(ctx.getBean(String.class)).thenThrow(new NoSuchBeanDefinitionException(""));
|
||||||
|
provider.setApplicationContext(ctx);
|
||||||
|
assertNull(ApplicationContextProvider.getBean(String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getBean_byNameAndClass_whenNoContext_returnsNull() {
|
||||||
|
provider.setApplicationContext(null);
|
||||||
|
assertNull(ApplicationContextProvider.getBean("myBean", String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getBean_byNameAndClass_whenBeanExists_returnsBean() {
|
||||||
|
ApplicationContext ctx = mock(ApplicationContext.class);
|
||||||
|
when(ctx.getBean("myBean", String.class)).thenReturn("world");
|
||||||
|
provider.setApplicationContext(ctx);
|
||||||
|
assertEquals("world", ApplicationContextProvider.getBean("myBean", String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getBean_byNameAndClass_whenBeanNotFound_returnsNull() {
|
||||||
|
ApplicationContext ctx = mock(ApplicationContext.class);
|
||||||
|
when(ctx.getBean("missing", String.class)).thenThrow(new NoSuchBeanDefinitionException(""));
|
||||||
|
provider.setApplicationContext(ctx);
|
||||||
|
assertNull(ApplicationContextProvider.getBean("missing", String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void containsBean_whenNoContext_returnsFalse() {
|
||||||
|
provider.setApplicationContext(null);
|
||||||
|
assertFalse(ApplicationContextProvider.containsBean(String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void containsBean_whenBeanExists_returnsTrue() {
|
||||||
|
ApplicationContext ctx = mock(ApplicationContext.class);
|
||||||
|
when(ctx.getBean(String.class)).thenReturn("exists");
|
||||||
|
provider.setApplicationContext(ctx);
|
||||||
|
assertTrue(ApplicationContextProvider.containsBean(String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void containsBean_whenBeanNotFound_returnsFalse() {
|
||||||
|
ApplicationContext ctx = mock(ApplicationContext.class);
|
||||||
|
when(ctx.getBean(Integer.class)).thenThrow(new NoSuchBeanDefinitionException(""));
|
||||||
|
provider.setApplicationContext(ctx);
|
||||||
|
assertFalse(ApplicationContextProvider.containsBean(Integer.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void setApplicationContext_updatesStaticContext() {
|
||||||
|
ApplicationContext ctx = mock(ApplicationContext.class);
|
||||||
|
when(ctx.getBean(String.class)).thenReturn("test");
|
||||||
|
provider.setApplicationContext(ctx);
|
||||||
|
assertEquals("test", ApplicationContextProvider.getBean(String.class));
|
||||||
|
|
||||||
|
// Now set a different context
|
||||||
|
ApplicationContext ctx2 = mock(ApplicationContext.class);
|
||||||
|
when(ctx2.getBean(String.class)).thenReturn("updated");
|
||||||
|
provider.setApplicationContext(ctx2);
|
||||||
|
assertEquals("updated", ApplicationContextProvider.getBean(String.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.cos.COSDictionary;
|
||||||
|
import org.apache.pdfbox.cos.COSName;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
|
||||||
|
import org.apache.pdfbox.pdmodel.PageMode;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class AttachmentUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should set page mode on catalog")
|
||||||
|
void setsPageMode() {
|
||||||
|
try (PDDocument document = new PDDocument()) {
|
||||||
|
AttachmentUtils.setCatalogViewerPreferences(document, PageMode.USE_ATTACHMENTS);
|
||||||
|
|
||||||
|
PDDocumentCatalog catalog = document.getDocumentCatalog();
|
||||||
|
assertEquals(PageMode.USE_ATTACHMENTS, catalog.getPageMode());
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Should not throw: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should create viewer preferences dictionary if absent")
|
||||||
|
void createsViewerPreferences() {
|
||||||
|
try (PDDocument document = new PDDocument()) {
|
||||||
|
AttachmentUtils.setCatalogViewerPreferences(document, PageMode.USE_ATTACHMENTS);
|
||||||
|
|
||||||
|
COSDictionary catalogDict = document.getDocumentCatalog().getCOSObject();
|
||||||
|
COSDictionary viewerPrefs =
|
||||||
|
(COSDictionary) catalogDict.getDictionaryObject(COSName.VIEWER_PREFERENCES);
|
||||||
|
assertNotNull(viewerPrefs);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Should not throw: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should set DisplayDocTitle to true in viewer preferences")
|
||||||
|
void setsDisplayDocTitle() {
|
||||||
|
try (PDDocument document = new PDDocument()) {
|
||||||
|
AttachmentUtils.setCatalogViewerPreferences(document, PageMode.USE_ATTACHMENTS);
|
||||||
|
|
||||||
|
COSDictionary catalogDict = document.getDocumentCatalog().getCOSObject();
|
||||||
|
COSDictionary viewerPrefs =
|
||||||
|
(COSDictionary) catalogDict.getDictionaryObject(COSName.VIEWER_PREFERENCES);
|
||||||
|
assertTrue(viewerPrefs.getBoolean(COSName.getPDFName("DisplayDocTitle"), false));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Should not throw: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should not throw when catalog returns null from mocked document")
|
||||||
|
void handlesNullCatalogGracefully() {
|
||||||
|
PDDocument document = mock(PDDocument.class);
|
||||||
|
when(document.getDocumentCatalog()).thenReturn(null);
|
||||||
|
|
||||||
|
assertDoesNotThrow(
|
||||||
|
() ->
|
||||||
|
AttachmentUtils.setCatalogViewerPreferences(
|
||||||
|
document, PageMode.USE_ATTACHMENTS));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
class CbrUtilsTest {
|
||||||
|
|
||||||
|
// --- isCbrFile tests ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbrFile_withCbrExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("comic.cbr");
|
||||||
|
assertTrue(CbrUtils.isCbrFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbrFile_withRarExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("archive.rar");
|
||||||
|
assertTrue(CbrUtils.isCbrFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbrFile_withUpperCaseExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("comic.CBR");
|
||||||
|
assertTrue(CbrUtils.isCbrFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbrFile_withPdfExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document.pdf");
|
||||||
|
assertFalse(CbrUtils.isCbrFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbrFile_withNullFilename_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn(null);
|
||||||
|
assertFalse(CbrUtils.isCbrFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbrFile_withCbzExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("comic.cbz");
|
||||||
|
assertFalse(CbrUtils.isCbrFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbrFile_withNoExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("noextension");
|
||||||
|
assertFalse(CbrUtils.isCbrFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbrFile_withMixedCaseRar_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("file.RaR");
|
||||||
|
assertTrue(CbrUtils.isCbrFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- convertCbrToPdf validation tests ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertCbrToPdf_withNullFile_throwsException() {
|
||||||
|
assertThrows(Exception.class, () -> CbrUtils.convertCbrToPdf(null, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertCbrToPdf_withEmptyFile_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(true);
|
||||||
|
assertThrows(Exception.class, () -> CbrUtils.convertCbrToPdf(file, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertCbrToPdf_withNullFilename_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(false);
|
||||||
|
when(file.getOriginalFilename()).thenReturn(null);
|
||||||
|
assertThrows(Exception.class, () -> CbrUtils.convertCbrToPdf(file, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertCbrToPdf_withWrongExtension_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(false);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("file.pdf");
|
||||||
|
assertThrows(Exception.class, () -> CbrUtils.convertCbrToPdf(file, null, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
class CbzUtilsTest {
|
||||||
|
|
||||||
|
// --- isCbzFile tests ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbzFile_withCbzExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("comic.cbz");
|
||||||
|
assertTrue(CbzUtils.isCbzFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbzFile_withZipExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("archive.zip");
|
||||||
|
assertTrue(CbzUtils.isCbzFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbzFile_withUpperCaseExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("comic.CBZ");
|
||||||
|
assertTrue(CbzUtils.isCbzFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbzFile_withPdfExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document.pdf");
|
||||||
|
assertFalse(CbzUtils.isCbzFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbzFile_withNullFilename_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn(null);
|
||||||
|
assertFalse(CbzUtils.isCbzFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCbzFile_withCbrExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("comic.cbr");
|
||||||
|
assertFalse(CbzUtils.isCbzFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- isComicBookFile tests ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isComicBookFile_withCbzExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("comic.cbz");
|
||||||
|
assertTrue(CbzUtils.isComicBookFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isComicBookFile_withZipExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("archive.zip");
|
||||||
|
assertTrue(CbzUtils.isComicBookFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isComicBookFile_withCbrExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("comic.cbr");
|
||||||
|
assertTrue(CbzUtils.isComicBookFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isComicBookFile_withRarExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("archive.rar");
|
||||||
|
assertTrue(CbzUtils.isComicBookFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isComicBookFile_withPdfExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document.pdf");
|
||||||
|
assertFalse(CbzUtils.isComicBookFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isComicBookFile_withNullFilename_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn(null);
|
||||||
|
assertFalse(CbzUtils.isComicBookFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isComicBookFile_withUpperCaseCBR_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("comic.CBR");
|
||||||
|
assertTrue(CbzUtils.isComicBookFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isComicBookFile_withNoExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("noextension");
|
||||||
|
assertFalse(CbzUtils.isComicBookFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- convertCbzToPdf validation tests ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertCbzToPdf_withNullFile_throwsException() {
|
||||||
|
assertThrows(Exception.class, () -> CbzUtils.convertCbzToPdf(null, null, null, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertCbzToPdf_withEmptyFile_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(true);
|
||||||
|
assertThrows(Exception.class, () -> CbzUtils.convertCbzToPdf(file, null, null, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertCbzToPdf_withNullFilename_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(false);
|
||||||
|
when(file.getOriginalFilename()).thenReturn(null);
|
||||||
|
assertThrows(Exception.class, () -> CbzUtils.convertCbzToPdf(file, null, null, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertCbzToPdf_withWrongExtension_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(false);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("file.pdf");
|
||||||
|
assertThrows(Exception.class, () -> CbzUtils.convertCbzToPdf(file, null, null, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
+210
@@ -0,0 +1,210 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
class ChecksumUtilsAdditionalTest {
|
||||||
|
|
||||||
|
private static final byte[] HELLO = "hello".getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
@TempDir Path tempDir;
|
||||||
|
|
||||||
|
private Path writeFile(byte[] data) throws IOException {
|
||||||
|
Path file = tempDir.resolve("testfile.bin");
|
||||||
|
Files.write(file, data);
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- checksum(Path, String) ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumPath_sha256() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
String hex = ChecksumUtils.checksum(file, "SHA-256");
|
||||||
|
assertEquals("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", hex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumPath_md5() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
String hex = ChecksumUtils.checksum(file, "MD5");
|
||||||
|
assertEquals("5d41402abc4b2a76b9719d911017c592", hex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumPath_crc32() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
String hex = ChecksumUtils.checksum(file, "CRC32");
|
||||||
|
assertEquals("3610a686", hex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- checksum(InputStream, String) ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumStream_adler32() throws IOException {
|
||||||
|
try (InputStream is = new ByteArrayInputStream(HELLO)) {
|
||||||
|
String hex = ChecksumUtils.checksum(is, "ADLER32");
|
||||||
|
assertNotNull(hex);
|
||||||
|
assertEquals(8, hex.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumStream_sha1() throws IOException {
|
||||||
|
try (InputStream is = new ByteArrayInputStream(HELLO)) {
|
||||||
|
String hex = ChecksumUtils.checksum(is, "SHA-1");
|
||||||
|
assertEquals("aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", hex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumStream_unsupportedAlgorithm() {
|
||||||
|
assertThrows(
|
||||||
|
IllegalStateException.class,
|
||||||
|
() -> {
|
||||||
|
try (InputStream is = new ByteArrayInputStream(HELLO)) {
|
||||||
|
ChecksumUtils.checksum(is, "FAKE-ALGO");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- checksumBase64(Path, String) ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumBase64Path_md5() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
String b64 = ChecksumUtils.checksumBase64(file, "MD5");
|
||||||
|
assertEquals("XUFAKrxLKna5cZ2REBfFkg==", b64);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumBase64Path_crc32() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
String b64 = ChecksumUtils.checksumBase64(file, "CRC32");
|
||||||
|
assertEquals("NhCmhg==", b64);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- checksumBase64(InputStream, String) ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumBase64Stream_adler32() throws IOException {
|
||||||
|
try (InputStream is = new ByteArrayInputStream(HELLO)) {
|
||||||
|
String b64 = ChecksumUtils.checksumBase64(is, "ADLER32");
|
||||||
|
assertNotNull(b64);
|
||||||
|
assertFalse(b64.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumBase64Stream_sha256() throws IOException {
|
||||||
|
try (InputStream is = new ByteArrayInputStream(HELLO)) {
|
||||||
|
String b64 = ChecksumUtils.checksumBase64(is, "SHA-256");
|
||||||
|
assertNotNull(b64);
|
||||||
|
assertFalse(b64.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- checksums(Path, String...) ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumsPath_multipleAlgorithms() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
Map<String, String> results = ChecksumUtils.checksums(file, "MD5", "SHA-256", "CRC32");
|
||||||
|
assertEquals(3, results.size());
|
||||||
|
assertEquals("5d41402abc4b2a76b9719d911017c592", results.get("MD5"));
|
||||||
|
assertEquals(
|
||||||
|
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
|
||||||
|
results.get("SHA-256"));
|
||||||
|
assertEquals("3610a686", results.get("CRC32"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumsPath_preservesOrder() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
// Digests are output first, then Checksums (CRC32/ADLER32), per implementation
|
||||||
|
Map<String, String> results = ChecksumUtils.checksums(file, "MD5", "SHA-1");
|
||||||
|
String[] keys = results.keySet().toArray(new String[0]);
|
||||||
|
assertEquals("MD5", keys[0]);
|
||||||
|
assertEquals("SHA-1", keys[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumsStream_unsupportedAlgorithm() {
|
||||||
|
assertThrows(
|
||||||
|
IllegalStateException.class,
|
||||||
|
() -> {
|
||||||
|
try (InputStream is = new ByteArrayInputStream(HELLO)) {
|
||||||
|
ChecksumUtils.checksums(is, "BOGUS");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- matches(Path, String, String) ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMatchesPath_correctHash() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
assertTrue(ChecksumUtils.matches(file, "MD5", "5d41402abc4b2a76b9719d911017c592"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMatchesPath_wrongHash() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
assertFalse(ChecksumUtils.matches(file, "MD5", "0000000000000000000000000000000000"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMatchesPath_caseInsensitive() throws IOException {
|
||||||
|
Path file = writeFile(HELLO);
|
||||||
|
assertTrue(ChecksumUtils.matches(file, "MD5", "5D41402ABC4B2A76B9719D911017C592"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- matches(InputStream, String, String) ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMatchesStream_correct() throws IOException {
|
||||||
|
try (InputStream is = new ByteArrayInputStream(HELLO)) {
|
||||||
|
assertTrue(
|
||||||
|
ChecksumUtils.matches(is, "SHA-1", "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMatchesStream_wrong() throws IOException {
|
||||||
|
try (InputStream is = new ByteArrayInputStream(HELLO)) {
|
||||||
|
assertFalse(
|
||||||
|
ChecksumUtils.matches(is, "SHA-1", "0000000000000000000000000000000000000000"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- empty input ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumEmptyInput() throws IOException {
|
||||||
|
byte[] empty = new byte[0];
|
||||||
|
try (InputStream is = new ByteArrayInputStream(empty)) {
|
||||||
|
String hex = ChecksumUtils.checksum(is, "MD5");
|
||||||
|
// MD5 of empty input is d41d8cd98f00b204e9800998ecf8427e
|
||||||
|
assertEquals("d41d8cd98f00b204e9800998ecf8427e", hex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testChecksumCrc32EmptyInput() throws IOException {
|
||||||
|
byte[] empty = new byte[0];
|
||||||
|
try (InputStream is = new ByteArrayInputStream(empty)) {
|
||||||
|
String hex = ChecksumUtils.checksum(is, "CRC32");
|
||||||
|
assertEquals("00000000", hex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class EmlParserTest {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("safeMimeDecode")
|
||||||
|
class SafeMimeDecodeTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return empty string for null input")
|
||||||
|
void nullInput() {
|
||||||
|
assertEquals("", EmlParser.safeMimeDecode(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return empty string for empty input")
|
||||||
|
void emptyInput() {
|
||||||
|
assertEquals("", EmlParser.safeMimeDecode(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return empty string for blank input")
|
||||||
|
void blankInput() {
|
||||||
|
assertEquals("", EmlParser.safeMimeDecode(" "));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return plain text as-is")
|
||||||
|
void plainText() {
|
||||||
|
assertEquals("Hello World", EmlParser.safeMimeDecode("Hello World"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should trim surrounding whitespace")
|
||||||
|
void trimWhitespace() {
|
||||||
|
assertEquals("Hello", EmlParser.safeMimeDecode(" Hello "));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should decode base64 MIME encoded word")
|
||||||
|
void decodeBase64MimeWord() {
|
||||||
|
// =?UTF-8?B?SGVsbG8=?= is Base64 for "Hello"
|
||||||
|
assertEquals("Hello", EmlParser.safeMimeDecode("=?UTF-8?B?SGVsbG8=?="));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should decode quoted-printable MIME encoded word")
|
||||||
|
void decodeQpMimeWord() {
|
||||||
|
// =?UTF-8?Q?Hello_World?= where _ means space in Q encoding
|
||||||
|
assertEquals("Hello World", EmlParser.safeMimeDecode("=?UTF-8?Q?Hello_World?="));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should handle mixed text and encoded words")
|
||||||
|
void mixedTextAndEncoded() {
|
||||||
|
String input = "Re: =?UTF-8?B?SGVsbG8=?= test";
|
||||||
|
String result = EmlParser.safeMimeDecode(input);
|
||||||
|
assertEquals("Re: Hello test", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("extractEmailContent")
|
||||||
|
class ExtractEmailContentTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should throw on null input")
|
||||||
|
void nullInput() {
|
||||||
|
assertThrows(Exception.class, () -> EmlParser.extractEmailContent(null, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should throw on empty input")
|
||||||
|
void emptyInput() {
|
||||||
|
assertThrows(
|
||||||
|
Exception.class, () -> EmlParser.extractEmailContent(new byte[0], null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should throw on invalid content that is not EML or MSG")
|
||||||
|
void invalidContent() {
|
||||||
|
byte[] randomBytes = "This is not an email file at all.".getBytes();
|
||||||
|
assertThrows(
|
||||||
|
Exception.class, () -> EmlParser.extractEmailContent(randomBytes, null, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,293 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class EmlProcessingUtilsTest {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("validateEmlInput")
|
||||||
|
class ValidateEmlInputTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should throw on null input")
|
||||||
|
void nullInput() {
|
||||||
|
assertThrows(Exception.class, () -> EmlProcessingUtils.validateEmlInput(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should throw on empty input")
|
||||||
|
void emptyInput() {
|
||||||
|
assertThrows(Exception.class, () -> EmlProcessingUtils.validateEmlInput(new byte[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should throw on invalid format with insufficient headers")
|
||||||
|
void invalidFormat() {
|
||||||
|
byte[] data = "Hello, this is just random text without email headers.".getBytes();
|
||||||
|
assertThrows(Exception.class, () -> EmlProcessingUtils.validateEmlInput(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should accept valid EML with multiple headers")
|
||||||
|
void validEml() {
|
||||||
|
String emlContent =
|
||||||
|
"From: sender@example.com\r\n"
|
||||||
|
+ "To: recipient@example.com\r\n"
|
||||||
|
+ "Subject: Test\r\n"
|
||||||
|
+ "Date: Mon, 1 Jan 2024 00:00:00 +0000\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ "Body text";
|
||||||
|
assertDoesNotThrow(() -> EmlProcessingUtils.validateEmlInput(emlContent.getBytes()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("isMsgFile")
|
||||||
|
class IsMsgFileTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return false for null")
|
||||||
|
void nullInput() {
|
||||||
|
assertFalse(EmlProcessingUtils.isMsgFile(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return false for short bytes")
|
||||||
|
void shortBytes() {
|
||||||
|
assertFalse(EmlProcessingUtils.isMsgFile(new byte[] {0x01, 0x02}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return true for MSG magic bytes")
|
||||||
|
void msgMagicBytes() {
|
||||||
|
byte[] magic = {
|
||||||
|
(byte) 0xD0,
|
||||||
|
(byte) 0xCF,
|
||||||
|
(byte) 0x11,
|
||||||
|
(byte) 0xE0,
|
||||||
|
(byte) 0xA1,
|
||||||
|
(byte) 0xB1,
|
||||||
|
(byte) 0x1A,
|
||||||
|
(byte) 0xE1,
|
||||||
|
0x00,
|
||||||
|
0x00
|
||||||
|
};
|
||||||
|
assertTrue(EmlProcessingUtils.isMsgFile(magic));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return false for non-MSG bytes")
|
||||||
|
void nonMsgBytes() {
|
||||||
|
byte[] data = new byte[] {0x50, 0x4B, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00};
|
||||||
|
assertFalse(EmlProcessingUtils.isMsgFile(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("escapeHtml")
|
||||||
|
class EscapeHtmlTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return empty string for null")
|
||||||
|
void nullInput() {
|
||||||
|
assertEquals("", EmlProcessingUtils.escapeHtml(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should escape all HTML special characters")
|
||||||
|
void escapeSpecialChars() {
|
||||||
|
String result = EmlProcessingUtils.escapeHtml("<div class=\"test\">'&'</div>");
|
||||||
|
assertEquals("<div class="test">'&'</div>", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should not modify plain text")
|
||||||
|
void plainText() {
|
||||||
|
assertEquals("Hello World", EmlProcessingUtils.escapeHtml("Hello World"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("convertTextToHtml")
|
||||||
|
class ConvertTextToHtmlTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return empty string for null")
|
||||||
|
void nullInput() {
|
||||||
|
assertEquals("", EmlProcessingUtils.convertTextToHtml(null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should convert newlines to br tags")
|
||||||
|
void newlinesToBr() {
|
||||||
|
String result = EmlProcessingUtils.convertTextToHtml("Line1\nLine2", null);
|
||||||
|
assertTrue(result.contains("<br>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should convert CRLF to br tags")
|
||||||
|
void crlfToBr() {
|
||||||
|
String result = EmlProcessingUtils.convertTextToHtml("Line1\r\nLine2", null);
|
||||||
|
assertTrue(result.contains("<br>"));
|
||||||
|
assertFalse(result.contains("\r"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should linkify URLs")
|
||||||
|
void linkifyUrls() {
|
||||||
|
String result =
|
||||||
|
EmlProcessingUtils.convertTextToHtml("Visit https://example.com today", null);
|
||||||
|
assertTrue(result.contains("<a href=\"https://example.com\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should linkify email addresses")
|
||||||
|
void linkifyEmails() {
|
||||||
|
String result = EmlProcessingUtils.convertTextToHtml("Contact test@example.com", null);
|
||||||
|
assertTrue(result.contains("mailto:test@example.com"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("decodeMimeHeader")
|
||||||
|
class DecodeMimeHeaderTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return null for null input")
|
||||||
|
void nullInput() {
|
||||||
|
assertNull(EmlProcessingUtils.decodeMimeHeader(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return empty string for empty input")
|
||||||
|
void emptyInput() {
|
||||||
|
assertEquals("", EmlProcessingUtils.decodeMimeHeader(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return plain text unchanged")
|
||||||
|
void plainText() {
|
||||||
|
assertEquals("Hello World", EmlProcessingUtils.decodeMimeHeader("Hello World"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should decode Base64 encoded header")
|
||||||
|
void decodeBase64() {
|
||||||
|
// "Hello" in Base64
|
||||||
|
String result = EmlProcessingUtils.decodeMimeHeader("=?UTF-8?B?SGVsbG8=?=");
|
||||||
|
assertEquals("Hello", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should decode quoted-printable encoded header")
|
||||||
|
void decodeQuotedPrintable() {
|
||||||
|
String result = EmlProcessingUtils.decodeMimeHeader("=?UTF-8?Q?Hello_World?=");
|
||||||
|
assertEquals("Hello World", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should decode concatenated encoded words")
|
||||||
|
void decodeConcatenated() {
|
||||||
|
String input = "=?UTF-8?B?SGVs?= =?UTF-8?B?bG8=?=";
|
||||||
|
String result = EmlProcessingUtils.decodeMimeHeader(input);
|
||||||
|
assertEquals("Hello", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should handle unknown encoding gracefully")
|
||||||
|
void unknownEncoding() {
|
||||||
|
String input = "=?UTF-8?X?unknown?=";
|
||||||
|
String result = EmlProcessingUtils.decodeMimeHeader(input);
|
||||||
|
assertEquals("=?UTF-8?X?unknown?=", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("detectMimeType")
|
||||||
|
class DetectMimeTypeTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return existing MIME type if provided")
|
||||||
|
void existingMimeType() {
|
||||||
|
assertEquals(
|
||||||
|
"image/jpeg", EmlProcessingUtils.detectMimeType("photo.png", "image/jpeg"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should detect PNG from filename")
|
||||||
|
void detectPng() {
|
||||||
|
assertEquals("image/png", EmlProcessingUtils.detectMimeType("image.png", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should detect JPEG from filename")
|
||||||
|
void detectJpeg() {
|
||||||
|
assertEquals("image/jpeg", EmlProcessingUtils.detectMimeType("photo.jpg", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should default to image/png for unknown extension")
|
||||||
|
void defaultMimeType() {
|
||||||
|
assertEquals("image/png", EmlProcessingUtils.detectMimeType("file.xyz", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should default to image/png for null filename and mime")
|
||||||
|
void nullFilenameAndMime() {
|
||||||
|
assertEquals("image/png", EmlProcessingUtils.detectMimeType(null, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("sanitizeText")
|
||||||
|
class SanitizeTextTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should escape HTML when no sanitizer provided")
|
||||||
|
void noSanitizer() {
|
||||||
|
String result = EmlProcessingUtils.sanitizeText("<script>", null);
|
||||||
|
assertEquals("<script>", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("processEmailHtmlBody")
|
||||||
|
class ProcessEmailHtmlBodyTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return empty string for null body")
|
||||||
|
void nullBody() {
|
||||||
|
assertEquals("", EmlProcessingUtils.processEmailHtmlBody(null, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should strip fixed position CSS")
|
||||||
|
void stripFixedPosition() {
|
||||||
|
String html = "<div style=\"position:fixed; top:0\">content</div>";
|
||||||
|
String result = EmlProcessingUtils.processEmailHtmlBody(html, null, null);
|
||||||
|
assertFalse(result.contains("position:fixed"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("decodeUrlEncoded")
|
||||||
|
class DecodeUrlEncodedTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should decode URL-encoded string")
|
||||||
|
void decodeEncoded() {
|
||||||
|
assertEquals("hello world", EmlProcessingUtils.decodeUrlEncoded("hello%20world"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return original on invalid encoding")
|
||||||
|
void invalidEncoding() {
|
||||||
|
String result = EmlProcessingUtils.decodeUrlEncoded("%ZZinvalid");
|
||||||
|
assertEquals("%ZZinvalid", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,44 +1,114 @@
|
|||||||
package stirling.software.common.util;
|
package stirling.software.common.util;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
public class ErrorUtilsTest {
|
class ErrorUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Nested
|
||||||
public void testExceptionToModel() {
|
@DisplayName("exceptionToModel")
|
||||||
// Create a mock Model
|
class ExceptionToModelTests {
|
||||||
Model model = new org.springframework.ui.ExtendedModelMap();
|
|
||||||
|
|
||||||
// Create a test exception
|
@Test
|
||||||
Exception ex = new Exception("Test Exception");
|
@DisplayName("should add error message to model")
|
||||||
|
void addsErrorMessage() {
|
||||||
|
Model model = mock(Model.class);
|
||||||
|
Exception ex = new RuntimeException("test error");
|
||||||
|
|
||||||
// Call the method under test
|
ErrorUtils.exceptionToModel(model, ex);
|
||||||
Model resultModel = ErrorUtils.exceptionToModel(model, ex);
|
|
||||||
|
|
||||||
// Verify the result
|
verify(model).addAttribute("errorMessage", "test error");
|
||||||
assertNotNull(resultModel);
|
}
|
||||||
assertEquals("Test Exception", resultModel.getAttribute("errorMessage"));
|
|
||||||
assertNotNull(resultModel.getAttribute("stackTrace"));
|
@Test
|
||||||
|
@DisplayName("should add stack trace to model")
|
||||||
|
void addsStackTrace() {
|
||||||
|
Model model = mock(Model.class);
|
||||||
|
Exception ex = new RuntimeException("test error");
|
||||||
|
|
||||||
|
ErrorUtils.exceptionToModel(model, ex);
|
||||||
|
|
||||||
|
verify(model)
|
||||||
|
.addAttribute(
|
||||||
|
eq("stackTrace"),
|
||||||
|
argThat(
|
||||||
|
arg ->
|
||||||
|
arg instanceof String s
|
||||||
|
&& s.contains("RuntimeException")
|
||||||
|
&& s.contains("test error")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return the same model instance")
|
||||||
|
void returnsSameModel() {
|
||||||
|
Model model = mock(Model.class);
|
||||||
|
Exception ex = new RuntimeException("test");
|
||||||
|
|
||||||
|
Model result = ErrorUtils.exceptionToModel(model, ex);
|
||||||
|
|
||||||
|
assertSame(model, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should handle exception with null message")
|
||||||
|
void nullExceptionMessage() {
|
||||||
|
Model model = mock(Model.class);
|
||||||
|
Exception ex = new RuntimeException((String) null);
|
||||||
|
|
||||||
|
ErrorUtils.exceptionToModel(model, ex);
|
||||||
|
|
||||||
|
verify(model).addAttribute("errorMessage", null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Nested
|
||||||
public void testExceptionToModelView() {
|
@DisplayName("exceptionToModelView")
|
||||||
// Create a mock Model
|
class ExceptionToModelViewTests {
|
||||||
Model model = new org.springframework.ui.ExtendedModelMap();
|
|
||||||
|
|
||||||
// Create a test exception
|
@Test
|
||||||
Exception ex = new Exception("Test Exception");
|
@DisplayName("should create ModelAndView with error message")
|
||||||
|
void addsErrorMessage() {
|
||||||
|
Model model = mock(Model.class);
|
||||||
|
Exception ex = new RuntimeException("view error");
|
||||||
|
|
||||||
// Call the method under test
|
ModelAndView result = ErrorUtils.exceptionToModelView(model, ex);
|
||||||
ModelAndView modelAndView = ErrorUtils.exceptionToModelView(model, ex);
|
|
||||||
|
|
||||||
// Verify the result
|
assertNotNull(result);
|
||||||
assertNotNull(modelAndView);
|
assertEquals("view error", result.getModel().get("errorMessage"));
|
||||||
assertEquals("Test Exception", modelAndView.getModel().get("errorMessage"));
|
}
|
||||||
assertNotNull(modelAndView.getModel().get("stackTrace"));
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should create ModelAndView with stack trace")
|
||||||
|
void addsStackTrace() {
|
||||||
|
Model model = mock(Model.class);
|
||||||
|
Exception ex = new RuntimeException("view error");
|
||||||
|
|
||||||
|
ModelAndView result = ErrorUtils.exceptionToModelView(model, ex);
|
||||||
|
|
||||||
|
String stackTrace = (String) result.getModel().get("stackTrace");
|
||||||
|
assertNotNull(stackTrace);
|
||||||
|
assertTrue(stackTrace.contains("RuntimeException"));
|
||||||
|
assertTrue(stackTrace.contains("view error"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should handle nested exception")
|
||||||
|
void nestedException() {
|
||||||
|
Model model = mock(Model.class);
|
||||||
|
Exception cause = new IllegalArgumentException("root cause");
|
||||||
|
Exception ex = new RuntimeException("wrapper", cause);
|
||||||
|
|
||||||
|
ModelAndView result = ErrorUtils.exceptionToModelView(model, ex);
|
||||||
|
|
||||||
|
String stackTrace = (String) result.getModel().get("stackTrace");
|
||||||
|
assertTrue(stackTrace.contains("root cause"));
|
||||||
|
assertEquals("wrapper", result.getModel().get("errorMessage"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class ExecutorFactoryTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("newVirtualThreadExecutor should return non-null executor")
|
||||||
|
void virtualThreadExecutorNotNull() {
|
||||||
|
ExecutorService executor = ExecutorFactory.newVirtualThreadExecutor();
|
||||||
|
assertNotNull(executor);
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("newVirtualThreadExecutor should execute tasks")
|
||||||
|
void virtualThreadExecutorExecutesTasks() throws Exception {
|
||||||
|
ExecutorService executor = ExecutorFactory.newVirtualThreadExecutor();
|
||||||
|
AtomicBoolean ran = new AtomicBoolean(false);
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
executor.submit(
|
||||||
|
() -> {
|
||||||
|
ran.set(true);
|
||||||
|
latch.countDown();
|
||||||
|
});
|
||||||
|
|
||||||
|
assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||||
|
assertTrue(ran.get());
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("newVirtualThreadExecutor should run on virtual threads")
|
||||||
|
void virtualThreadExecutorUsesVirtualThreads() throws Exception {
|
||||||
|
ExecutorService executor = ExecutorFactory.newVirtualThreadExecutor();
|
||||||
|
AtomicReference<Boolean> isVirtual = new AtomicReference<>();
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
executor.submit(
|
||||||
|
() -> {
|
||||||
|
isVirtual.set(Thread.currentThread().isVirtual());
|
||||||
|
latch.countDown();
|
||||||
|
});
|
||||||
|
|
||||||
|
assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||||
|
assertTrue(isVirtual.get());
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("newSingleVirtualThreadScheduledExecutor should return non-null")
|
||||||
|
void scheduledExecutorNotNull() {
|
||||||
|
ScheduledExecutorService executor =
|
||||||
|
ExecutorFactory.newSingleVirtualThreadScheduledExecutor();
|
||||||
|
assertNotNull(executor);
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("newSingleVirtualThreadScheduledExecutor should execute scheduled tasks")
|
||||||
|
void scheduledExecutorExecutesTasks() throws Exception {
|
||||||
|
ScheduledExecutorService executor =
|
||||||
|
ExecutorFactory.newSingleVirtualThreadScheduledExecutor();
|
||||||
|
AtomicBoolean ran = new AtomicBoolean(false);
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
executor.schedule(
|
||||||
|
() -> {
|
||||||
|
ran.set(true);
|
||||||
|
latch.countDown();
|
||||||
|
},
|
||||||
|
10,
|
||||||
|
TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
|
assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||||
|
assertTrue(ran.get());
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,194 +1,110 @@
|
|||||||
package stirling.software.common.util;
|
package stirling.software.common.util;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.attribute.FileTime;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
|
||||||
|
|
||||||
import stirling.software.common.configuration.RuntimePathConfig;
|
import stirling.software.common.configuration.RuntimePathConfig;
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
|
||||||
class FileMonitorTest {
|
class FileMonitorTest {
|
||||||
|
|
||||||
@TempDir Path tempDir;
|
@TempDir Path tempDir;
|
||||||
|
|
||||||
@Mock private RuntimePathConfig runtimePathConfig;
|
private FileMonitor createFileMonitor(Path watchDir) throws IOException {
|
||||||
|
Predicate<Path> acceptAll = path -> true;
|
||||||
@Mock private Predicate<Path> pathFilter;
|
RuntimePathConfig runtimePathConfig = mock(RuntimePathConfig.class);
|
||||||
|
|
||||||
private FileMonitor fileMonitor;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() throws IOException {
|
|
||||||
when(runtimePathConfig.getPipelineWatchedFoldersPaths())
|
when(runtimePathConfig.getPipelineWatchedFoldersPaths())
|
||||||
.thenReturn(List.of(tempDir.toString()));
|
.thenReturn(List.of(watchDir.toString()));
|
||||||
|
return new FileMonitor(acceptAll, runtimePathConfig);
|
||||||
// This mock is used in all tests except testPathFilter
|
|
||||||
// We use lenient to avoid UnnecessaryStubbingException in that test
|
|
||||||
Mockito.lenient().when(pathFilter.test(any())).thenReturn(true);
|
|
||||||
|
|
||||||
fileMonitor = new FileMonitor(pathFilter, runtimePathConfig);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsFileReadyForProcessing_OldFile() throws IOException {
|
void testConstructor_withValidDirectory() throws IOException {
|
||||||
// Capture test time at the beginning for deterministic calculations
|
FileMonitor monitor = createFileMonitor(tempDir);
|
||||||
final Instant testTime = Instant.now();
|
assertNotNull(monitor);
|
||||||
|
|
||||||
// Create a test file
|
|
||||||
Path testFile = tempDir.resolve("test-file.txt");
|
|
||||||
Files.write(testFile, "test content".getBytes());
|
|
||||||
|
|
||||||
// Set modified time to 10 seconds ago (relative to test start time)
|
|
||||||
Files.setLastModifiedTime(testFile, FileTime.from(testTime.minusMillis(10000)));
|
|
||||||
|
|
||||||
// File should be ready for processing as it was modified more than 5 seconds ago
|
|
||||||
assertTrue(fileMonitor.isFileReadyForProcessing(testFile));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsFileReadyForProcessing_RecentFile() throws IOException {
|
void testConstructor_withNonExistentDirectory() throws IOException {
|
||||||
// Capture test time at the beginning for deterministic calculations
|
Path nonExistent = tempDir.resolve("does_not_exist");
|
||||||
final Instant testTime = Instant.now();
|
Predicate<Path> acceptAll = path -> true;
|
||||||
|
RuntimePathConfig config = mock(RuntimePathConfig.class);
|
||||||
|
when(config.getPipelineWatchedFoldersPaths()).thenReturn(List.of(nonExistent.toString()));
|
||||||
|
|
||||||
// Create a test file
|
// Should not throw - just logs an error about non-existent path
|
||||||
Path testFile = tempDir.resolve("recent-file.txt");
|
FileMonitor monitor = new FileMonitor(acceptAll, config);
|
||||||
Files.write(testFile, "test content".getBytes());
|
assertNotNull(monitor);
|
||||||
|
|
||||||
// Set modified time to just now (relative to test start time)
|
|
||||||
Files.setLastModifiedTime(testFile, FileTime.from(testTime));
|
|
||||||
|
|
||||||
// File should not be ready for processing as it was just modified
|
|
||||||
assertFalse(fileMonitor.isFileReadyForProcessing(testFile));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsFileReadyForProcessing_NonExistentFile() {
|
void testConstructor_withEmptyWatchedFolders() throws IOException {
|
||||||
// Create a path to a file that doesn't exist
|
Predicate<Path> acceptAll = path -> true;
|
||||||
Path nonExistentFile = tempDir.resolve("non-existent-file.txt");
|
RuntimePathConfig config = mock(RuntimePathConfig.class);
|
||||||
|
when(config.getPipelineWatchedFoldersPaths()).thenReturn(List.of());
|
||||||
|
|
||||||
// Non-existent file should not be ready for processing
|
FileMonitor monitor = new FileMonitor(acceptAll, config);
|
||||||
assertFalse(fileMonitor.isFileReadyForProcessing(nonExistentFile));
|
assertNotNull(monitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsFileReadyForProcessing_LockedFile() throws IOException {
|
void testTrackFiles_noEventsDoesNotThrow() throws IOException {
|
||||||
// Capture test time at the beginning for deterministic calculations
|
FileMonitor monitor = createFileMonitor(tempDir);
|
||||||
final Instant testTime = Instant.now();
|
// Should not throw even when no events have occurred
|
||||||
|
assertDoesNotThrow(() -> monitor.trackFiles());
|
||||||
// Create a test file
|
|
||||||
Path testFile = tempDir.resolve("locked-file.txt");
|
|
||||||
Files.write(testFile, "test content".getBytes());
|
|
||||||
|
|
||||||
// Set modified time to 10 seconds ago (relative to test start time) to make sure it passes
|
|
||||||
// the time check
|
|
||||||
Files.setLastModifiedTime(testFile, FileTime.from(testTime.minusMillis(10000)));
|
|
||||||
|
|
||||||
// Verify the file is considered ready when it meets the time criteria
|
|
||||||
assertTrue(
|
|
||||||
fileMonitor.isFileReadyForProcessing(testFile),
|
|
||||||
"File should be ready for processing when sufficiently old");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testPathFilter() throws IOException {
|
void testIsFileReadyForProcessing_nonExistentFile() throws IOException {
|
||||||
// Use a simple lambda instead of a mock for better control
|
FileMonitor monitor = createFileMonitor(tempDir);
|
||||||
Predicate<Path> pdfFilter = path -> path.toString().endsWith(".pdf");
|
Path nonExistent = tempDir.resolve("nonexistent.pdf");
|
||||||
|
|
||||||
// Create a new FileMonitor with the PDF filter
|
// Non-existent file should not be ready (file lock check will fail)
|
||||||
FileMonitor pdfMonitor = new FileMonitor(pdfFilter, runtimePathConfig);
|
boolean ready = monitor.isFileReadyForProcessing(nonExistent);
|
||||||
|
assertFalse(ready, "Non-existent file should not be ready for processing");
|
||||||
// Create a PDF file
|
|
||||||
Path pdfFile = tempDir.resolve("test.pdf");
|
|
||||||
Files.write(pdfFile, "pdf content".getBytes());
|
|
||||||
Files.setLastModifiedTime(pdfFile, FileTime.from(Instant.ofEpochMilli(1000000L)));
|
|
||||||
|
|
||||||
// Create a TXT file
|
|
||||||
Path txtFile = tempDir.resolve("test.txt");
|
|
||||||
Files.write(txtFile, "text content".getBytes());
|
|
||||||
Files.setLastModifiedTime(txtFile, FileTime.from(Instant.ofEpochMilli(1000000L)));
|
|
||||||
|
|
||||||
// PDF file should be ready for processing
|
|
||||||
assertTrue(pdfMonitor.isFileReadyForProcessing(pdfFile));
|
|
||||||
|
|
||||||
// Note: In the current implementation, FileMonitor.isFileReadyForProcessing()
|
|
||||||
// doesn't check file filters directly - it only checks criteria like file existence
|
|
||||||
// and modification time. The filtering is likely handled elsewhere in the workflow.
|
|
||||||
|
|
||||||
// To avoid test failures, we'll verify that the filter itself works correctly
|
|
||||||
assertFalse(pdfFilter.test(txtFile), "PDF filter should reject txt files");
|
|
||||||
assertTrue(pdfFilter.test(pdfFile), "PDF filter should accept pdf files");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsFileReadyForProcessing_FileInUse() throws IOException {
|
void testIsFileReadyForProcessing_existingFile() throws IOException, InterruptedException {
|
||||||
// Capture test time at the beginning for deterministic calculations
|
FileMonitor monitor = createFileMonitor(tempDir);
|
||||||
final Instant testTime = Instant.now();
|
Path testFile = tempDir.resolve("test.pdf");
|
||||||
|
Files.writeString(testFile, "test content");
|
||||||
|
|
||||||
// Create a test file
|
// Run trackFiles to process any events
|
||||||
Path testFile = tempDir.resolve("in-use-file.txt");
|
monitor.trackFiles();
|
||||||
Files.write(testFile, "initial content".getBytes());
|
|
||||||
|
|
||||||
// Set modified time to 10 seconds ago (relative to test start time)
|
// The file might or might not be ready depending on timing,
|
||||||
Files.setLastModifiedTime(testFile, FileTime.from(testTime.minusMillis(10000)));
|
// but calling the method should not throw
|
||||||
|
assertDoesNotThrow(() -> monitor.isFileReadyForProcessing(testFile));
|
||||||
// First check that the file is ready when meeting time criteria
|
|
||||||
assertTrue(
|
|
||||||
fileMonitor.isFileReadyForProcessing(testFile),
|
|
||||||
"File should be ready for processing when sufficiently old");
|
|
||||||
|
|
||||||
// After modifying the file to simulate closing, it should still be ready
|
|
||||||
Files.write(testFile, "updated content".getBytes());
|
|
||||||
Files.setLastModifiedTime(testFile, FileTime.from(testTime.minusMillis(10000)));
|
|
||||||
|
|
||||||
assertTrue(
|
|
||||||
fileMonitor.isFileReadyForProcessing(testFile),
|
|
||||||
"File should be ready for processing after updating");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsFileReadyForProcessing_FileWithAbsolutePath() throws IOException {
|
void testTrackFiles_afterFileCreation() throws IOException {
|
||||||
// Capture test time at the beginning for deterministic calculations
|
FileMonitor monitor = createFileMonitor(tempDir);
|
||||||
final Instant testTime = Instant.now();
|
|
||||||
|
|
||||||
// Create a test file
|
// Create a file in the watched directory
|
||||||
Path testFile = tempDir.resolve("absolute-path-file.txt");
|
Path testFile = tempDir.resolve("newfile.txt");
|
||||||
Files.write(testFile, "test content".getBytes());
|
Files.writeString(testFile, "hello");
|
||||||
|
|
||||||
// Set modified time to 10 seconds ago (relative to test start time)
|
// Track files should process the creation event
|
||||||
Files.setLastModifiedTime(testFile, FileTime.from(testTime.minusMillis(10000)));
|
assertDoesNotThrow(() -> monitor.trackFiles());
|
||||||
|
|
||||||
// File should be ready for processing as it was modified more than 5 seconds ago
|
|
||||||
// Use the absolute path to make sure it's handled correctly
|
|
||||||
assertTrue(fileMonitor.isFileReadyForProcessing(testFile.toAbsolutePath()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsFileReadyForProcessing_DirectoryInsteadOfFile() throws IOException {
|
void testConstructor_withPathFilter() throws IOException {
|
||||||
// Create a test directory
|
// Filter that rejects all paths
|
||||||
Path testDir = tempDir.resolve("test-directory");
|
Predicate<Path> rejectAll = path -> false;
|
||||||
Files.createDirectory(testDir);
|
RuntimePathConfig config = mock(RuntimePathConfig.class);
|
||||||
|
when(config.getPipelineWatchedFoldersPaths()).thenReturn(List.of(tempDir.toString()));
|
||||||
|
|
||||||
// Set modified time to 10 seconds ago
|
FileMonitor monitor = new FileMonitor(rejectAll, config);
|
||||||
Files.setLastModifiedTime(testDir, FileTime.from(Instant.ofEpochMilli(1000000L)));
|
assertNotNull(monitor);
|
||||||
|
|
||||||
// A directory should not be considered ready for processing
|
|
||||||
boolean isReady = fileMonitor.isFileReadyForProcessing(testDir);
|
|
||||||
assertFalse(isReady, "A directory should not be considered ready for processing");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,357 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Answers.CALLS_REAL_METHODS;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.nio.channels.FileLock;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.attribute.FileTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
|
import stirling.software.common.model.ApplicationProperties.AutoPipeline.FileReadiness;
|
||||||
|
|
||||||
|
@DisplayName("FileReadinessChecker")
|
||||||
|
class FileReadinessCheckerTest {
|
||||||
|
|
||||||
|
@TempDir Path tempDir;
|
||||||
|
|
||||||
|
@Mock ApplicationProperties applicationProperties;
|
||||||
|
@Mock ApplicationProperties.AutoPipeline autoPipeline;
|
||||||
|
|
||||||
|
/** Real config object — easier to tweak per test than chaining multiple stubs. */
|
||||||
|
FileReadiness config;
|
||||||
|
|
||||||
|
FileReadinessChecker checker;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
MockitoAnnotations.openMocks(this);
|
||||||
|
|
||||||
|
config = new FileReadiness();
|
||||||
|
config.setEnabled(true);
|
||||||
|
config.setSettleTimeMillis(0); // instant settle by default — individual tests override
|
||||||
|
config.setSizeCheckDelayMillis(1); // minimal pause keeps tests fast
|
||||||
|
config.setAllowedExtensions(new ArrayList<>());
|
||||||
|
|
||||||
|
when(applicationProperties.getAutoPipeline()).thenReturn(autoPipeline);
|
||||||
|
when(autoPipeline.getFileReadiness()).thenReturn(config);
|
||||||
|
|
||||||
|
checker = new FileReadinessChecker(applicationProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Master toggle
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("when enabled=false")
|
||||||
|
class WhenDisabled {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("always returns true regardless of file state")
|
||||||
|
void alwaysReady() throws IOException {
|
||||||
|
config.setEnabled(false);
|
||||||
|
|
||||||
|
// Non-existent path — would normally fail check #1
|
||||||
|
Path ghost = tempDir.resolve("does-not-exist.pdf");
|
||||||
|
assertTrue(checker.isReady(ghost));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Check #1 + #2: existence and regular-file guard
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("existence and file-type checks")
|
||||||
|
class ExistenceChecks {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("non-existent path → not ready")
|
||||||
|
void fileDoesNotExist() {
|
||||||
|
Path ghost = tempDir.resolve("ghost.pdf");
|
||||||
|
assertFalse(checker.isReady(ghost));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("path is a directory → not ready")
|
||||||
|
void pathIsDirectory() throws IOException {
|
||||||
|
Path dir = tempDir.resolve("subdir");
|
||||||
|
Files.createDirectory(dir);
|
||||||
|
assertFalse(checker.isReady(dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("path is a regular file → passes existence checks")
|
||||||
|
void regularFilePassesExistenceCheck() throws IOException {
|
||||||
|
Path file = realFile("test.pdf", "content");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
assertTrue(checker.isReady(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Check #3: extension filter
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("extension filter")
|
||||||
|
class ExtensionFilter {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("empty allow-list → all extensions accepted")
|
||||||
|
void emptyAllowListAcceptsAll() throws IOException {
|
||||||
|
config.setAllowedExtensions(new ArrayList<>()); // empty = no filter
|
||||||
|
Path file = realFile("report.docx", "data");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
assertTrue(checker.isReady(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("extension in allow-list → passes")
|
||||||
|
void extensionInAllowList() throws IOException {
|
||||||
|
config.setAllowedExtensions(List.of("pdf", "tiff"));
|
||||||
|
Path file = realFile("scan.pdf", "data");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
assertTrue(checker.isReady(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("extension not in allow-list → not ready")
|
||||||
|
void extensionNotInAllowList() throws IOException {
|
||||||
|
config.setAllowedExtensions(List.of("pdf", "tiff"));
|
||||||
|
Path file = realFile("document.docx", "data");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
assertFalse(checker.isReady(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("extension matching is case-insensitive")
|
||||||
|
void extensionMatchIsCaseInsensitive() throws IOException {
|
||||||
|
config.setAllowedExtensions(List.of("PDF"));
|
||||||
|
Path file = realFile("scan.pdf", "data");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
assertTrue(checker.isReady(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("file without extension and non-empty allow-list → not ready")
|
||||||
|
void fileWithNoExtension() throws IOException {
|
||||||
|
config.setAllowedExtensions(List.of("pdf"));
|
||||||
|
Path file = realFile("README", "data");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
assertFalse(checker.isReady(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Check #4: settle-time (last-modified age)
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("settle-time check")
|
||||||
|
class SettleTime {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("recently modified file → not ready")
|
||||||
|
void recentlyModified_notReady() throws IOException {
|
||||||
|
config.setSettleTimeMillis(60_000); // require 1 minute of quiet
|
||||||
|
Path file = realFile("new.pdf", "data");
|
||||||
|
// last-modified is now (just created) — well within the threshold
|
||||||
|
assertFalse(checker.isReady(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("file settled for longer than threshold → ready")
|
||||||
|
void settled_ready() throws IOException {
|
||||||
|
config.setSettleTimeMillis(5_000);
|
||||||
|
Path file = realFile("old.pdf", "data");
|
||||||
|
setLastModifiedInPast(file, 10_000); // 10 s ago — older than 5 s threshold
|
||||||
|
assertTrue(checker.isReady(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("settle threshold of 0 ms passes any file")
|
||||||
|
void zeroThreshold_alwaysPasses() throws IOException {
|
||||||
|
config.setSettleTimeMillis(0);
|
||||||
|
Path file = realFile("instant.pdf", "data");
|
||||||
|
// last-modified is right now; 0 ms threshold means anything passes
|
||||||
|
assertTrue(checker.isReady(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Check #5: size stability
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("size-stability check")
|
||||||
|
class SizeStability {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("size unchanged between two reads → ready")
|
||||||
|
void sizeStable_ready() throws IOException {
|
||||||
|
config.setSizeCheckDelayMillis(1);
|
||||||
|
Path file = realFile("stable.pdf", "fixed content");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
assertTrue(checker.isReady(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("size changes between two reads → not ready")
|
||||||
|
void sizeChanging_notReady() throws IOException {
|
||||||
|
config.setSizeCheckDelayMillis(1);
|
||||||
|
Path file = realFile("growing.pdf", "initial");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
|
||||||
|
// Use MockedStatic to control what Files.size() returns on each call
|
||||||
|
// while leaving all other Files.* methods intact.
|
||||||
|
AtomicInteger sizeCallCount = new AtomicInteger(0);
|
||||||
|
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class, CALLS_REAL_METHODS)) {
|
||||||
|
mockedFiles
|
||||||
|
.when(() -> Files.size(file))
|
||||||
|
.thenAnswer(
|
||||||
|
inv ->
|
||||||
|
sizeCallCount.incrementAndGet() == 1
|
||||||
|
? 100L // first read: 100 bytes
|
||||||
|
: 200L); // second read: 200 bytes — changed!
|
||||||
|
assertFalse(checker.isReady(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Check #6: file-lock check
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("file-lock check")
|
||||||
|
class FileLockCheck {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("file held open with exclusive lock by another thread → not ready")
|
||||||
|
void fileLocked_notReady() throws IOException, InterruptedException {
|
||||||
|
Path file = realFile("locked.pdf", "data");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
|
||||||
|
CountDownLatch lockAcquired = new CountDownLatch(1);
|
||||||
|
CountDownLatch testDone = new CountDownLatch(1);
|
||||||
|
AtomicInteger lockThreadFailed = new AtomicInteger(0);
|
||||||
|
|
||||||
|
Thread lockHolder =
|
||||||
|
new Thread(
|
||||||
|
() -> {
|
||||||
|
try (RandomAccessFile raf =
|
||||||
|
new RandomAccessFile(file.toFile(), "rw");
|
||||||
|
FileChannel channel = raf.getChannel();
|
||||||
|
FileLock lock = channel.lock()) {
|
||||||
|
lockAcquired.countDown();
|
||||||
|
testDone.await(10, TimeUnit.SECONDS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
lockThreadFailed.set(1);
|
||||||
|
lockAcquired.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
lockHolder.setDaemon(true);
|
||||||
|
lockHolder.start();
|
||||||
|
lockAcquired.await(5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (lockThreadFailed.get() == 0) {
|
||||||
|
// Lock was successfully held — the checker must see it as locked.
|
||||||
|
// On JVM, tryLock() from a second thread in the same process throws
|
||||||
|
// OverlappingFileLockException (or returns null on some platforms), both of
|
||||||
|
// which isLocked() maps to true.
|
||||||
|
assertFalse(checker.isReady(file));
|
||||||
|
}
|
||||||
|
// If locking failed on this platform we simply skip the assertion rather than
|
||||||
|
// failing the build — the logic path is still exercised by other tests.
|
||||||
|
} finally {
|
||||||
|
testDone.countDown();
|
||||||
|
lockHolder.join(5_000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("file with no external lock and all checks passing → ready")
|
||||||
|
void noLock_ready() throws IOException {
|
||||||
|
Path file = realFile("unlocked.pdf", "data");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
assertTrue(checker.isReady(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Full happy-path integration
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("full happy path")
|
||||||
|
class HappyPath {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("all checks pass → ready")
|
||||||
|
void allChecksPass_ready() throws IOException {
|
||||||
|
config.setSettleTimeMillis(5_000);
|
||||||
|
config.setSizeCheckDelayMillis(1);
|
||||||
|
config.setAllowedExtensions(List.of("pdf"));
|
||||||
|
|
||||||
|
Path file = realFile("invoice.pdf", "PDF content");
|
||||||
|
setLastModifiedInPast(file, 10_000);
|
||||||
|
|
||||||
|
assertTrue(checker.isReady(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("first failing check short-circuits evaluation")
|
||||||
|
void shortCircuitsOnFirstFailure() throws IOException {
|
||||||
|
// Extension filter will reject — settle / size / lock checks must never run
|
||||||
|
config.setAllowedExtensions(List.of("pdf"));
|
||||||
|
config.setSettleTimeMillis(0);
|
||||||
|
config.setSizeCheckDelayMillis(1);
|
||||||
|
|
||||||
|
Path file = realFile("archive.zip", "ZIP data");
|
||||||
|
setLastModifiedInPast(file, 60_000);
|
||||||
|
|
||||||
|
assertFalse(checker.isReady(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Helpers
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
private Path realFile(String name, String content) throws IOException {
|
||||||
|
Path file = tempDir.resolve(name);
|
||||||
|
Files.writeString(file, content);
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Back-dates the last-modified time of {@code path} by {@code millisAgo} so that settle-time
|
||||||
|
* checks pass without actually waiting.
|
||||||
|
*/
|
||||||
|
private void setLastModifiedInPast(Path path, long millisAgo) throws IOException {
|
||||||
|
Files.setLastModifiedTime(
|
||||||
|
path, FileTime.fromMillis(System.currentTimeMillis() - millisAgo));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,115 +1,76 @@
|
|||||||
package stirling.software.common.util;
|
package stirling.software.common.util;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import stirling.software.common.model.api.converters.HTMLToPdfRequest;
|
class FileToPdfTest {
|
||||||
import stirling.software.common.service.SsrfProtectionService;
|
|
||||||
|
|
||||||
public class FileToPdfTest {
|
@Test
|
||||||
|
void testSanitizeZipFilename_normalFilename() {
|
||||||
private CustomHtmlSanitizer customHtmlSanitizer;
|
String result = FileToPdf.sanitizeZipFilename("document.html");
|
||||||
|
assertEquals("document.html", result);
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
SsrfProtectionService mockSsrfProtectionService = mock(SsrfProtectionService.class);
|
|
||||||
stirling.software.common.model.ApplicationProperties mockApplicationProperties =
|
|
||||||
mock(stirling.software.common.model.ApplicationProperties.class);
|
|
||||||
stirling.software.common.model.ApplicationProperties.System mockSystem =
|
|
||||||
mock(stirling.software.common.model.ApplicationProperties.System.class);
|
|
||||||
|
|
||||||
when(mockSsrfProtectionService.isUrlAllowed(org.mockito.ArgumentMatchers.anyString()))
|
|
||||||
.thenReturn(true);
|
|
||||||
when(mockApplicationProperties.getSystem()).thenReturn(mockSystem);
|
|
||||||
when(mockSystem.isDisableSanitize()).thenReturn(false);
|
|
||||||
|
|
||||||
customHtmlSanitizer =
|
|
||||||
new CustomHtmlSanitizer(mockSsrfProtectionService, mockApplicationProperties);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the HTML to PDF conversion. This test expects an IOException when an empty HTML input is
|
|
||||||
* provided.
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testConvertHtmlToPdf() {
|
void testSanitizeZipFilename_pathTraversal() {
|
||||||
HTMLToPdfRequest request = new HTMLToPdfRequest();
|
String result = FileToPdf.sanitizeZipFilename("../../etc/passwd");
|
||||||
byte[] fileBytes = new byte[0]; // Sample file bytes (empty input)
|
// Should remove ../ sequences
|
||||||
String fileName = "test.html"; // Sample file name indicating an HTML file
|
assertFalse(result.contains(".."), "Path traversal sequences should be removed");
|
||||||
TempFileManager tempFileManager = mock(TempFileManager.class); // Mock TempFileManager
|
|
||||||
|
|
||||||
// Mock the temp file creation to return real temp files
|
|
||||||
try {
|
|
||||||
when(tempFileManager.createTempFile(anyString()))
|
|
||||||
.thenReturn(Files.createTempFile("test", ".pdf").toFile())
|
|
||||||
.thenReturn(Files.createTempFile("test", ".html").toFile());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Expect an IOException to be thrown due to empty input or invalid weasyprint path
|
|
||||||
Throwable thrown =
|
|
||||||
assertThrows(
|
|
||||||
Exception.class,
|
|
||||||
() ->
|
|
||||||
FileToPdf.convertHtmlToPdf(
|
|
||||||
"/path/",
|
|
||||||
request,
|
|
||||||
fileBytes,
|
|
||||||
fileName,
|
|
||||||
tempFileManager,
|
|
||||||
customHtmlSanitizer));
|
|
||||||
assertNotNull(thrown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Test sanitizeZipFilename with null or empty input. It should return an empty string in these
|
|
||||||
* cases.
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testSanitizeZipFilename_NullOrEmpty() {
|
void testSanitizeZipFilename_driveLetterRemoved() {
|
||||||
assertEquals("", FileToPdf.sanitizeZipFilename(null));
|
String result = FileToPdf.sanitizeZipFilename("C:\\Users\\test\\file.html");
|
||||||
assertEquals("", FileToPdf.sanitizeZipFilename(" "));
|
assertFalse(result.startsWith("C:"), "Drive letter should be removed");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Test sanitizeZipFilename to ensure it removes path traversal sequences. This includes
|
|
||||||
* removing both forward and backward slash sequences.
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testSanitizeZipFilename_RemovesTraversalSequences() {
|
void testSanitizeZipFilename_backslashesNormalized() {
|
||||||
String input = "../some/../path/..\\to\\file.txt";
|
String result = FileToPdf.sanitizeZipFilename("path\\to\\file.html");
|
||||||
String expected = "some/path/to/file.txt";
|
assertFalse(result.contains("\\"), "Backslashes should be normalized to forward slashes");
|
||||||
|
assertTrue(result.contains("/") || !result.contains("\\"));
|
||||||
// Expect that the method replaces backslashes with forward slashes
|
|
||||||
// and removes path traversal sequences
|
|
||||||
assertEquals(expected, FileToPdf.sanitizeZipFilename(input));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Test sanitizeZipFilename to ensure that it removes leading drive letters and slashes. */
|
|
||||||
@Test
|
@Test
|
||||||
public void testSanitizeZipFilename_RemovesLeadingDriveAndSlashes() {
|
void testSanitizeZipFilename_nullInput() {
|
||||||
String input = "C:\\folder\\file.txt";
|
String result = FileToPdf.sanitizeZipFilename(null);
|
||||||
String expected = "folder/file.txt";
|
assertEquals("", result, "Null input should return empty string");
|
||||||
assertEquals(expected, FileToPdf.sanitizeZipFilename(input));
|
|
||||||
|
|
||||||
input = "/folder/file.txt";
|
|
||||||
expected = "folder/file.txt";
|
|
||||||
assertEquals(expected, FileToPdf.sanitizeZipFilename(input));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Test sanitizeZipFilename to verify that safe filenames remain unchanged. */
|
|
||||||
@Test
|
@Test
|
||||||
public void testSanitizeZipFilename_NoChangeForSafeNames() {
|
void testSanitizeZipFilename_emptyInput() {
|
||||||
String input = "folder/subfolder/file.txt";
|
String result = FileToPdf.sanitizeZipFilename("");
|
||||||
assertEquals(input, FileToPdf.sanitizeZipFilename(input));
|
assertEquals("", result, "Empty input should return empty string");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitizeZipFilename_whitespaceOnly() {
|
||||||
|
String result = FileToPdf.sanitizeZipFilename(" ");
|
||||||
|
assertEquals("", result, "Whitespace-only input should return empty string");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitizeZipFilename_leadingSlashes() {
|
||||||
|
String result = FileToPdf.sanitizeZipFilename("///path/to/file.html");
|
||||||
|
assertFalse(result.startsWith("/"), "Leading slashes should be removed");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitizeZipFilename_nestedDirectories() {
|
||||||
|
String result = FileToPdf.sanitizeZipFilename("dir1/dir2/file.html");
|
||||||
|
assertEquals("dir1/dir2/file.html", result, "Normal nested paths should be preserved");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitizeZipFilename_mixedTraversal() {
|
||||||
|
String result = FileToPdf.sanitizeZipFilename("dir/../../../etc/passwd");
|
||||||
|
assertFalse(result.contains(".."), "Mixed path traversal should be removed");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitizeZipFilename_backslashTraversal() {
|
||||||
|
String result = FileToPdf.sanitizeZipFilename("dir\\..\\..\\etc\\passwd");
|
||||||
|
assertFalse(result.contains(".."), "Backslash path traversal should be removed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,163 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDComboBox;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDListBox;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDPushButton;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDTerminalField;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class FormFieldTypeSupportTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withNull_returnsNull() {
|
||||||
|
assertNull(FormFieldTypeSupport.forField(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withTextField_returnsTEXT() {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDTextField field = new PDTextField(form);
|
||||||
|
assertEquals(FormFieldTypeSupport.TEXT, FormFieldTypeSupport.forField(field));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Unexpected exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withCheckBox_returnsCHECKBOX() {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDCheckBox field = new PDCheckBox(form);
|
||||||
|
assertEquals(FormFieldTypeSupport.CHECKBOX, FormFieldTypeSupport.forField(field));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Unexpected exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withRadioButton_returnsRADIO() {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDRadioButton field = new PDRadioButton(form);
|
||||||
|
assertEquals(FormFieldTypeSupport.RADIO, FormFieldTypeSupport.forField(field));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Unexpected exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withComboBox_returnsCOMBOBOX() {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDComboBox field = new PDComboBox(form);
|
||||||
|
assertEquals(FormFieldTypeSupport.COMBOBOX, FormFieldTypeSupport.forField(field));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Unexpected exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withListBox_returnsLISTBOX() {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDListBox field = new PDListBox(form);
|
||||||
|
assertEquals(FormFieldTypeSupport.LISTBOX, FormFieldTypeSupport.forField(field));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Unexpected exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withSignatureField_returnsSIGNATURE() {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDSignatureField field = new PDSignatureField(form);
|
||||||
|
assertEquals(FormFieldTypeSupport.SIGNATURE, FormFieldTypeSupport.forField(field));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Unexpected exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withPushButton_returnsBUTTON() {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDPushButton field = new PDPushButton(form);
|
||||||
|
assertEquals(FormFieldTypeSupport.BUTTON, FormFieldTypeSupport.forField(field));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Unexpected exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forTypeName_withValidNames_returnsCorrectEnum() {
|
||||||
|
assertEquals(FormFieldTypeSupport.TEXT, FormFieldTypeSupport.forTypeName("text"));
|
||||||
|
assertEquals(FormFieldTypeSupport.CHECKBOX, FormFieldTypeSupport.forTypeName("checkbox"));
|
||||||
|
assertEquals(FormFieldTypeSupport.RADIO, FormFieldTypeSupport.forTypeName("radio"));
|
||||||
|
assertEquals(FormFieldTypeSupport.COMBOBOX, FormFieldTypeSupport.forTypeName("combobox"));
|
||||||
|
assertEquals(FormFieldTypeSupport.LISTBOX, FormFieldTypeSupport.forTypeName("listbox"));
|
||||||
|
assertEquals(FormFieldTypeSupport.SIGNATURE, FormFieldTypeSupport.forTypeName("signature"));
|
||||||
|
assertEquals(FormFieldTypeSupport.BUTTON, FormFieldTypeSupport.forTypeName("button"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forTypeName_withNull_returnsNull() {
|
||||||
|
assertNull(FormFieldTypeSupport.forTypeName(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forTypeName_withUnknown_returnsNull() {
|
||||||
|
assertNull(FormFieldTypeSupport.forTypeName("unknown"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void doesNotSupportsDefinitionCreation_textReturnsFalse() {
|
||||||
|
assertFalse(FormFieldTypeSupport.TEXT.doesNotsupportsDefinitionCreation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void doesNotSupportsDefinitionCreation_radioReturnsTrue() {
|
||||||
|
assertTrue(FormFieldTypeSupport.RADIO.doesNotsupportsDefinitionCreation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void doesNotSupportsDefinitionCreation_signatureReturnsTrue() {
|
||||||
|
assertTrue(FormFieldTypeSupport.SIGNATURE.doesNotsupportsDefinitionCreation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void doesNotSupportsDefinitionCreation_buttonReturnsTrue() {
|
||||||
|
assertTrue(FormFieldTypeSupport.BUTTON.doesNotsupportsDefinitionCreation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createField_text_returnsPDTextField() {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDTerminalField field = FormFieldTypeSupport.TEXT.createField(form);
|
||||||
|
assertInstanceOf(PDTextField.class, field);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Unexpected exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createField_checkbox_returnsPDCheckBox() {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDTerminalField field = FormFieldTypeSupport.CHECKBOX.createField(form);
|
||||||
|
assertInstanceOf(PDCheckBox.class, field);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("Unexpected exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,304 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDResources;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class FormUtilsAdditionalTest {
|
||||||
|
|
||||||
|
private record SetupDocument(PDPage page, PDAcroForm acroForm) {}
|
||||||
|
|
||||||
|
private static SetupDocument createBasicDocument(PDDocument document) throws IOException {
|
||||||
|
PDPage page = new PDPage();
|
||||||
|
document.addPage(page);
|
||||||
|
|
||||||
|
PDAcroForm acroForm = new PDAcroForm(document);
|
||||||
|
acroForm.setDefaultResources(new PDResources());
|
||||||
|
acroForm.setNeedAppearances(true);
|
||||||
|
document.getDocumentCatalog().setAcroForm(acroForm);
|
||||||
|
|
||||||
|
return new SetupDocument(page, acroForm);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void attachWidget(
|
||||||
|
SetupDocument setup,
|
||||||
|
org.apache.pdfbox.pdmodel.interactive.form.PDTerminalField field,
|
||||||
|
PDRectangle rectangle)
|
||||||
|
throws IOException {
|
||||||
|
PDAnnotationWidget widget = new PDAnnotationWidget();
|
||||||
|
widget.setRectangle(rectangle);
|
||||||
|
widget.setPage(setup.page);
|
||||||
|
List<PDAnnotationWidget> widgets = new ArrayList<>(field.getWidgets());
|
||||||
|
widgets.add(widget);
|
||||||
|
field.setWidgets(widgets);
|
||||||
|
setup.acroForm.getFields().add(field);
|
||||||
|
setup.page.getAnnotations().add(widget);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- detectFieldType ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDetectFieldType_textField() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
SetupDocument setup = createBasicDocument(doc);
|
||||||
|
PDTextField field = new PDTextField(setup.acroForm);
|
||||||
|
assertEquals("text", FormUtils.detectFieldType(field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDetectFieldType_checkBox() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
SetupDocument setup = createBasicDocument(doc);
|
||||||
|
PDCheckBox field = new PDCheckBox(setup.acroForm);
|
||||||
|
assertEquals("checkbox", FormUtils.detectFieldType(field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- extractFormFields ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExtractFormFields_nullDocument() {
|
||||||
|
List<FormUtils.FormFieldInfo> fields = FormUtils.extractFormFields(null);
|
||||||
|
assertTrue(fields.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExtractFormFields_noAcroForm() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
// No AcroForm set
|
||||||
|
List<FormUtils.FormFieldInfo> fields = FormUtils.extractFormFields(doc);
|
||||||
|
assertTrue(fields.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExtractFormFields_singleTextField() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
SetupDocument setup = createBasicDocument(doc);
|
||||||
|
PDTextField textField = new PDTextField(setup.acroForm);
|
||||||
|
textField.setPartialName("firstName");
|
||||||
|
attachWidget(setup, textField, new PDRectangle(50, 700, 200, 20));
|
||||||
|
|
||||||
|
List<FormUtils.FormFieldInfo> fields = FormUtils.extractFormFields(doc);
|
||||||
|
assertEquals(1, fields.size());
|
||||||
|
assertEquals("firstName", fields.get(0).name());
|
||||||
|
assertEquals("text", fields.get(0).type());
|
||||||
|
assertEquals(0, fields.get(0).pageIndex());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExtractFormFields_multipleFields() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
SetupDocument setup = createBasicDocument(doc);
|
||||||
|
|
||||||
|
PDTextField field1 = new PDTextField(setup.acroForm);
|
||||||
|
field1.setPartialName("name");
|
||||||
|
attachWidget(setup, field1, new PDRectangle(50, 700, 200, 20));
|
||||||
|
|
||||||
|
PDTextField field2 = new PDTextField(setup.acroForm);
|
||||||
|
field2.setPartialName("email");
|
||||||
|
attachWidget(setup, field2, new PDRectangle(50, 660, 200, 20));
|
||||||
|
|
||||||
|
List<FormUtils.FormFieldInfo> fields = FormUtils.extractFormFields(doc);
|
||||||
|
assertEquals(2, fields.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- buildFillTemplateRecord ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBuildFillTemplateRecord_null() {
|
||||||
|
Map<String, Object> result = FormUtils.buildFillTemplateRecord(null);
|
||||||
|
assertTrue(result.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBuildFillTemplateRecord_empty() {
|
||||||
|
Map<String, Object> result = FormUtils.buildFillTemplateRecord(Collections.emptyList());
|
||||||
|
assertTrue(result.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBuildFillTemplateRecord_textField() {
|
||||||
|
FormUtils.FormFieldInfo info =
|
||||||
|
new FormUtils.FormFieldInfo(
|
||||||
|
"name", "Name", "text", "John", null, false, 0, false, null, 0);
|
||||||
|
Map<String, Object> result = FormUtils.buildFillTemplateRecord(List.of(info));
|
||||||
|
assertEquals("John", result.get("name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBuildFillTemplateRecord_checkboxField() {
|
||||||
|
FormUtils.FormFieldInfo info =
|
||||||
|
new FormUtils.FormFieldInfo(
|
||||||
|
"agree", "Agreement", "checkbox", "Yes", null, false, 0, false, null, 0);
|
||||||
|
Map<String, Object> result = FormUtils.buildFillTemplateRecord(List.of(info));
|
||||||
|
assertEquals(Boolean.TRUE, result.get("agree"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBuildFillTemplateRecord_checkboxFieldOff() {
|
||||||
|
FormUtils.FormFieldInfo info =
|
||||||
|
new FormUtils.FormFieldInfo(
|
||||||
|
"agree", "Agreement", "checkbox", "Off", null, false, 0, false, null, 0);
|
||||||
|
Map<String, Object> result = FormUtils.buildFillTemplateRecord(List.of(info));
|
||||||
|
assertEquals(Boolean.FALSE, result.get("agree"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBuildFillTemplateRecord_skipsButton() {
|
||||||
|
FormUtils.FormFieldInfo info =
|
||||||
|
new FormUtils.FormFieldInfo(
|
||||||
|
"submit", "Submit", "button", null, null, false, 0, false, null, 0);
|
||||||
|
Map<String, Object> result = FormUtils.buildFillTemplateRecord(List.of(info));
|
||||||
|
assertFalse(result.containsKey("submit"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBuildFillTemplateRecord_skipsSignature() {
|
||||||
|
FormUtils.FormFieldInfo info =
|
||||||
|
new FormUtils.FormFieldInfo(
|
||||||
|
"sig", "Signature", "signature", null, null, false, 0, false, null, 0);
|
||||||
|
Map<String, Object> result = FormUtils.buildFillTemplateRecord(List.of(info));
|
||||||
|
assertFalse(result.containsKey("sig"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- safeValue ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSafeValue_nonNull() {
|
||||||
|
assertEquals("hello", FormUtils.safeValue("hello"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSafeValue_null() {
|
||||||
|
assertEquals("", FormUtils.safeValue(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- applyFieldValues ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testApplyFieldValues_nullDocument() throws IOException {
|
||||||
|
// Should not throw
|
||||||
|
FormUtils.applyFieldValues(null, Map.of("key", "value"), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testApplyFieldValues_noAcroFormStrict() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
assertThrows(
|
||||||
|
IOException.class,
|
||||||
|
() -> FormUtils.applyFieldValues(doc, Map.of("key", "val"), false, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testApplyFieldValues_noAcroFormNonStrict() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
// Should not throw in non-strict mode
|
||||||
|
FormUtils.applyFieldValues(doc, Map.of("key", "val"), false, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testApplyFieldValues_setsTextValue() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
SetupDocument setup = createBasicDocument(doc);
|
||||||
|
PDTextField textField = new PDTextField(setup.acroForm);
|
||||||
|
textField.setPartialName("company");
|
||||||
|
attachWidget(setup, textField, new PDRectangle(60, 720, 220, 20));
|
||||||
|
|
||||||
|
FormUtils.applyFieldValues(doc, Map.of("company", "Stirling"), false);
|
||||||
|
assertEquals("Stirling", textField.getValueAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testApplyFieldValues_checksCheckbox_nonStrict() throws IOException {
|
||||||
|
// In non-strict mode, checkbox state changes may fail silently
|
||||||
|
// if appearance streams are not properly configured. Just verify no exception.
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
SetupDocument setup = createBasicDocument(doc);
|
||||||
|
PDCheckBox checkBox = new PDCheckBox(setup.acroForm);
|
||||||
|
checkBox.setPartialName("subscribed");
|
||||||
|
checkBox.setExportValues(List.of("Yes"));
|
||||||
|
attachWidget(setup, checkBox, new PDRectangle(60, 680, 16, 16));
|
||||||
|
|
||||||
|
// Should not throw in non-strict mode even if appearance is missing
|
||||||
|
FormUtils.applyFieldValues(doc, Map.of("subscribed", true), false, false);
|
||||||
|
FormUtils.applyFieldValues(doc, Map.of("subscribed", false), false, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- filterSingleChoiceSelection ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFilterSingleChoiceSelection_validSelection() {
|
||||||
|
String result =
|
||||||
|
FormUtils.filterSingleChoiceSelection(
|
||||||
|
"Option A", List.of("Option A", "Option B"), "field1");
|
||||||
|
assertEquals("Option A", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFilterSingleChoiceSelection_invalidSelection() {
|
||||||
|
String result =
|
||||||
|
FormUtils.filterSingleChoiceSelection(
|
||||||
|
"Invalid", List.of("Option A", "Option B"), "field1");
|
||||||
|
assertNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFilterSingleChoiceSelection_nullSelection() {
|
||||||
|
String result = FormUtils.filterSingleChoiceSelection(null, List.of("Option A"), "field1");
|
||||||
|
assertNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFilterSingleChoiceSelection_emptySelection() {
|
||||||
|
String result = FormUtils.filterSingleChoiceSelection(" ", List.of("Option A"), "field1");
|
||||||
|
assertNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- extractFieldsWithTemplate ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExtractFieldsWithTemplate_emptyDocument() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
FormUtils.FormFieldExtraction extraction = FormUtils.extractFieldsWithTemplate(doc);
|
||||||
|
assertNotNull(extraction);
|
||||||
|
assertTrue(extraction.fields().isEmpty());
|
||||||
|
assertTrue(extraction.template().isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- hasAnyRotatedPage ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHasAnyRotatedPage_noRotation() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
assertFalse(FormUtils.hasAnyRotatedPage(doc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class GeneralFormCopyUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasAnyRotatedPage_noRotation_returnsFalse() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
assertFalse(GeneralFormCopyUtils.hasAnyRotatedPage(doc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasAnyRotatedPage_with90Rotation_returnsTrue() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDPage page = new PDPage();
|
||||||
|
page.setRotation(90);
|
||||||
|
doc.addPage(page);
|
||||||
|
assertTrue(GeneralFormCopyUtils.hasAnyRotatedPage(doc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasAnyRotatedPage_with180Rotation_returnsTrue() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDPage page = new PDPage();
|
||||||
|
page.setRotation(180);
|
||||||
|
doc.addPage(page);
|
||||||
|
assertTrue(GeneralFormCopyUtils.hasAnyRotatedPage(doc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasAnyRotatedPage_with360Rotation_returnsFalse() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDPage page = new PDPage();
|
||||||
|
page.setRotation(360);
|
||||||
|
doc.addPage(page);
|
||||||
|
assertFalse(GeneralFormCopyUtils.hasAnyRotatedPage(doc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasAnyRotatedPage_emptyDocument_returnsFalse() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
assertFalse(GeneralFormCopyUtils.hasAnyRotatedPage(doc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasAnyRotatedPage_mixedPages_returnsTrue() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
PDPage rotated = new PDPage();
|
||||||
|
rotated.setRotation(270);
|
||||||
|
doc.addPage(rotated);
|
||||||
|
assertTrue(GeneralFormCopyUtils.hasAnyRotatedPage(doc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void copyAndTransformFormFields_noAcroForm_doesNotThrow() throws Exception {
|
||||||
|
try (PDDocument source = new PDDocument();
|
||||||
|
PDDocument target = new PDDocument()) {
|
||||||
|
source.addPage(new PDPage());
|
||||||
|
target.addPage(new PDPage());
|
||||||
|
// No acro form set on source - should simply return without error
|
||||||
|
assertDoesNotThrow(
|
||||||
|
() ->
|
||||||
|
GeneralFormCopyUtils.copyAndTransformFormFields(
|
||||||
|
source, target, 1, 1, 1, 1, 612f, 792f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void copyAndTransformFormFields_emptyAcroForm_doesNotThrow() throws Exception {
|
||||||
|
try (PDDocument source = new PDDocument();
|
||||||
|
PDDocument target = new PDDocument()) {
|
||||||
|
source.addPage(new PDPage());
|
||||||
|
target.addPage(new PDPage());
|
||||||
|
// Empty acro form
|
||||||
|
var acroForm = new org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm(source);
|
||||||
|
source.getDocumentCatalog().setAcroForm(acroForm);
|
||||||
|
assertDoesNotThrow(
|
||||||
|
() ->
|
||||||
|
GeneralFormCopyUtils.copyAndTransformFormFields(
|
||||||
|
source, target, 1, 1, 1, 1, 612f, 792f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+147
@@ -0,0 +1,147 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDComboBox;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDListBox;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDPushButton;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDTerminalField;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class GeneralFormFieldTypeSupportTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withNull_returnsNull() {
|
||||||
|
assertNull(GeneralFormFieldTypeSupport.forField(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withTextField_returnsTEXT() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDTextField field = new PDTextField(form);
|
||||||
|
assertEquals(
|
||||||
|
GeneralFormFieldTypeSupport.TEXT, GeneralFormFieldTypeSupport.forField(field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withCheckBox_returnsCHECKBOX() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDCheckBox field = new PDCheckBox(form);
|
||||||
|
assertEquals(
|
||||||
|
GeneralFormFieldTypeSupport.CHECKBOX,
|
||||||
|
GeneralFormFieldTypeSupport.forField(field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withRadioButton_returnsRADIO() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDRadioButton field = new PDRadioButton(form);
|
||||||
|
assertEquals(
|
||||||
|
GeneralFormFieldTypeSupport.RADIO, GeneralFormFieldTypeSupport.forField(field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withComboBox_returnsCOMBOBOX() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDComboBox field = new PDComboBox(form);
|
||||||
|
assertEquals(
|
||||||
|
GeneralFormFieldTypeSupport.COMBOBOX,
|
||||||
|
GeneralFormFieldTypeSupport.forField(field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withListBox_returnsLISTBOX() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDListBox field = new PDListBox(form);
|
||||||
|
assertEquals(
|
||||||
|
GeneralFormFieldTypeSupport.LISTBOX,
|
||||||
|
GeneralFormFieldTypeSupport.forField(field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withSignatureField_returnsSIGNATURE() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDSignatureField field = new PDSignatureField(form);
|
||||||
|
assertEquals(
|
||||||
|
GeneralFormFieldTypeSupport.SIGNATURE,
|
||||||
|
GeneralFormFieldTypeSupport.forField(field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void forField_withPushButton_returnsBUTTON() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDPushButton field = new PDPushButton(form);
|
||||||
|
assertEquals(
|
||||||
|
GeneralFormFieldTypeSupport.BUTTON,
|
||||||
|
GeneralFormFieldTypeSupport.forField(field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createField_text_returnsPDTextField() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDTerminalField field = GeneralFormFieldTypeSupport.TEXT.createField(form);
|
||||||
|
assertInstanceOf(PDTextField.class, field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createField_checkbox_returnsPDCheckBox() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDTerminalField field = GeneralFormFieldTypeSupport.CHECKBOX.createField(form);
|
||||||
|
assertInstanceOf(PDCheckBox.class, field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createField_signature_returnsPDSignatureField() throws Exception {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDAcroForm form = new PDAcroForm(doc);
|
||||||
|
PDTerminalField field = GeneralFormFieldTypeSupport.SIGNATURE.createField(form);
|
||||||
|
assertInstanceOf(PDSignatureField.class, field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void typeName_returnsExpectedValues() {
|
||||||
|
assertEquals("text", GeneralFormFieldTypeSupport.TEXT.typeName());
|
||||||
|
assertEquals("checkbox", GeneralFormFieldTypeSupport.CHECKBOX.typeName());
|
||||||
|
assertEquals("radio", GeneralFormFieldTypeSupport.RADIO.typeName());
|
||||||
|
assertEquals("combobox", GeneralFormFieldTypeSupport.COMBOBOX.typeName());
|
||||||
|
assertEquals("listbox", GeneralFormFieldTypeSupport.LISTBOX.typeName());
|
||||||
|
assertEquals("signature", GeneralFormFieldTypeSupport.SIGNATURE.typeName());
|
||||||
|
assertEquals("button", GeneralFormFieldTypeSupport.BUTTON.typeName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fallbackWidgetName_returnsExpectedValues() {
|
||||||
|
assertEquals("textField", GeneralFormFieldTypeSupport.TEXT.fallbackWidgetName());
|
||||||
|
assertEquals("checkBox", GeneralFormFieldTypeSupport.CHECKBOX.fallbackWidgetName());
|
||||||
|
assertEquals("radioButton", GeneralFormFieldTypeSupport.RADIO.fallbackWidgetName());
|
||||||
|
assertEquals("comboBox", GeneralFormFieldTypeSupport.COMBOBOX.fallbackWidgetName());
|
||||||
|
assertEquals("listBox", GeneralFormFieldTypeSupport.LISTBOX.fallbackWidgetName());
|
||||||
|
assertEquals("signature", GeneralFormFieldTypeSupport.SIGNATURE.fallbackWidgetName());
|
||||||
|
assertEquals("pushButton", GeneralFormFieldTypeSupport.BUTTON.fallbackWidgetName());
|
||||||
|
}
|
||||||
|
}
|
||||||
+86
-55
@@ -2,79 +2,110 @@ package stirling.software.common.util;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.awt.image.DataBufferByte;
|
||||||
|
import java.awt.image.DataBufferInt;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class ImageProcessingUtilsTest {
|
class ImageProcessingUtilsTest {
|
||||||
|
|
||||||
private static void fillImageWithColor(BufferedImage image) {
|
@Test
|
||||||
for (int y = 0; y < image.getHeight(); y++) {
|
void convertColorType_greyscale_returnsGrayscaleImage() {
|
||||||
for (int x = 0; x < image.getWidth(); x++) {
|
BufferedImage source = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
||||||
image.setRGB(x, y, Color.RED.getRGB());
|
BufferedImage result = ImageProcessingUtils.convertColorType(source, "greyscale");
|
||||||
}
|
assertEquals(BufferedImage.TYPE_BYTE_GRAY, result.getType());
|
||||||
}
|
assertEquals(10, result.getWidth());
|
||||||
|
assertEquals(10, result.getHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testConvertColorTypeToGreyscale() {
|
void convertColorType_blackwhite_returnsBinaryImage() {
|
||||||
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
|
BufferedImage source = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
||||||
fillImageWithColor(sourceImage);
|
BufferedImage result = ImageProcessingUtils.convertColorType(source, "blackwhite");
|
||||||
|
assertEquals(BufferedImage.TYPE_BYTE_BINARY, result.getType());
|
||||||
BufferedImage convertedImage =
|
|
||||||
ImageProcessingUtils.convertColorType(sourceImage, "greyscale");
|
|
||||||
|
|
||||||
assertNotNull(convertedImage);
|
|
||||||
assertEquals(BufferedImage.TYPE_BYTE_GRAY, convertedImage.getType());
|
|
||||||
assertEquals(sourceImage.getWidth(), convertedImage.getWidth());
|
|
||||||
assertEquals(sourceImage.getHeight(), convertedImage.getHeight());
|
|
||||||
|
|
||||||
// Check if a pixel is correctly converted to greyscale
|
|
||||||
Color grey = new Color(convertedImage.getRGB(0, 0));
|
|
||||||
assertEquals(grey.getRed(), grey.getGreen());
|
|
||||||
assertEquals(grey.getGreen(), grey.getBlue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testConvertColorTypeToBlackWhite() {
|
void convertColorType_fullColor_returnsSameImage() {
|
||||||
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
|
BufferedImage source = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
||||||
fillImageWithColor(sourceImage);
|
BufferedImage result = ImageProcessingUtils.convertColorType(source, "fullcolor");
|
||||||
|
assertSame(source, result);
|
||||||
BufferedImage convertedImage =
|
|
||||||
ImageProcessingUtils.convertColorType(sourceImage, "blackwhite");
|
|
||||||
|
|
||||||
assertNotNull(convertedImage);
|
|
||||||
assertEquals(BufferedImage.TYPE_BYTE_BINARY, convertedImage.getType());
|
|
||||||
assertEquals(sourceImage.getWidth(), convertedImage.getWidth());
|
|
||||||
assertEquals(sourceImage.getHeight(), convertedImage.getHeight());
|
|
||||||
|
|
||||||
// Check if a pixel is converted correctly (binary image will be either black or white)
|
|
||||||
int rgb = convertedImage.getRGB(0, 0);
|
|
||||||
assertTrue(rgb == Color.BLACK.getRGB() || rgb == Color.WHITE.getRGB());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testConvertColorTypeToFullColor() {
|
void convertColorType_unknownType_returnsSameImage() {
|
||||||
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
|
BufferedImage source = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
||||||
fillImageWithColor(sourceImage);
|
BufferedImage result = ImageProcessingUtils.convertColorType(source, "something_else");
|
||||||
|
assertSame(source, result);
|
||||||
BufferedImage convertedImage =
|
|
||||||
ImageProcessingUtils.convertColorType(sourceImage, "fullcolor");
|
|
||||||
|
|
||||||
assertNotNull(convertedImage);
|
|
||||||
assertEquals(sourceImage, convertedImage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testConvertColorTypeInvalid() {
|
void getImageData_byteBuffer_returnsCorrectData() {
|
||||||
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
|
BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_BYTE_GRAY);
|
||||||
fillImageWithColor(sourceImage);
|
byte[] data = ImageProcessingUtils.getImageData(image);
|
||||||
|
assertNotNull(data);
|
||||||
|
assertTrue(data instanceof byte[]);
|
||||||
|
// TYPE_BYTE_GRAY uses DataBufferByte
|
||||||
|
assertTrue(image.getRaster().getDataBuffer() instanceof DataBufferByte);
|
||||||
|
}
|
||||||
|
|
||||||
BufferedImage convertedImage =
|
@Test
|
||||||
ImageProcessingUtils.convertColorType(sourceImage, "invalidtype");
|
void getImageData_intBuffer_returnsCorrectLength() {
|
||||||
|
BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
|
||||||
|
// TYPE_INT_RGB uses DataBufferInt
|
||||||
|
assertTrue(image.getRaster().getDataBuffer() instanceof DataBufferInt);
|
||||||
|
byte[] data = ImageProcessingUtils.getImageData(image);
|
||||||
|
assertNotNull(data);
|
||||||
|
// 2x2 pixels, 4 bytes per int
|
||||||
|
assertEquals(2 * 2 * 4, data.length);
|
||||||
|
}
|
||||||
|
|
||||||
assertNotNull(convertedImage);
|
@Test
|
||||||
assertEquals(sourceImage, convertedImage);
|
void getImageData_ushortBuffer_returnsRGBData() {
|
||||||
|
// TYPE_USHORT_GRAY uses DataBufferUShort which hits the else branch
|
||||||
|
BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_USHORT_GRAY);
|
||||||
|
byte[] data = ImageProcessingUtils.getImageData(image);
|
||||||
|
assertNotNull(data);
|
||||||
|
// 2x2 pixels, 3 bytes per pixel (RGB)
|
||||||
|
assertEquals(2 * 2 * 3, data.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void applyOrientation_zeroRotation_returnsSameImage() {
|
||||||
|
BufferedImage image = new BufferedImage(10, 20, BufferedImage.TYPE_INT_RGB);
|
||||||
|
BufferedImage result = ImageProcessingUtils.applyOrientation(image, 0);
|
||||||
|
assertSame(image, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void applyOrientation_90degrees_returnsRotatedImage() {
|
||||||
|
BufferedImage image = new BufferedImage(10, 20, BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics2D g = image.createGraphics();
|
||||||
|
g.setColor(Color.RED);
|
||||||
|
g.fillRect(0, 0, 10, 20);
|
||||||
|
g.dispose();
|
||||||
|
|
||||||
|
BufferedImage result = ImageProcessingUtils.applyOrientation(image, 90);
|
||||||
|
assertNotNull(result);
|
||||||
|
// The rotated image should have non-zero dimensions
|
||||||
|
assertTrue(result.getWidth() > 0);
|
||||||
|
assertTrue(result.getHeight() > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void applyOrientation_180degrees_returnsRotatedImage() {
|
||||||
|
BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
||||||
|
BufferedImage result = ImageProcessingUtils.applyOrientation(image, 180);
|
||||||
|
assertNotNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void applyOrientation_270degrees_returnsRotatedImage() {
|
||||||
|
BufferedImage image = new BufferedImage(10, 20, BufferedImage.TYPE_INT_RGB);
|
||||||
|
BufferedImage result = ImageProcessingUtils.applyOrientation(image, 270);
|
||||||
|
assertNotNull(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class JarPathUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void currentJar_notRunningFromJar_returnsNull() {
|
||||||
|
// When running tests from IDE/Gradle, we are not in a JAR
|
||||||
|
Path result = JarPathUtil.currentJar();
|
||||||
|
assertNull(result, "Should return null when not running from a JAR file");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void restartHelperJar_notFound_returnsNull() {
|
||||||
|
// Since we're not running from JAR and restart-helper.jar likely doesn't exist
|
||||||
|
Path result = JarPathUtil.restartHelperJar();
|
||||||
|
assertNull(result, "Should return null when restart-helper.jar is not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void javaExecutable_returnsNonNullPath() {
|
||||||
|
String result = JarPathUtil.javaExecutable();
|
||||||
|
assertNotNull(result);
|
||||||
|
assertTrue(result.contains("java"), "Should contain 'java' in the path");
|
||||||
|
assertTrue(result.contains("bin"), "Should contain 'bin' in the path");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void javaExecutable_containsJavaHome() {
|
||||||
|
String javaHome = System.getProperty("java.home");
|
||||||
|
String result = JarPathUtil.javaExecutable();
|
||||||
|
assertTrue(result.startsWith(javaHome), "Should start with java.home system property");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void javaExecutable_windowsHasExeExtension() {
|
||||||
|
String result = JarPathUtil.javaExecutable();
|
||||||
|
if (System.getProperty("os.name").toLowerCase().contains("win")) {
|
||||||
|
assertTrue(result.endsWith(".exe"), "On Windows, should end with .exe");
|
||||||
|
} else {
|
||||||
|
assertFalse(result.endsWith(".exe"), "On non-Windows, should not end with .exe");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class JobContextTest {
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void cleanup() {
|
||||||
|
JobContext.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should return null when no job ID is set")
|
||||||
|
void returnsNullByDefault() {
|
||||||
|
assertNull(JobContext.getJobId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should store and retrieve job ID")
|
||||||
|
void setAndGet() {
|
||||||
|
JobContext.setJobId("job-123");
|
||||||
|
assertEquals("job-123", JobContext.getJobId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should clear job ID")
|
||||||
|
void clearJobId() {
|
||||||
|
JobContext.setJobId("job-456");
|
||||||
|
JobContext.clear();
|
||||||
|
assertNull(JobContext.getJobId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should isolate job IDs between threads")
|
||||||
|
void threadIsolation() throws Exception {
|
||||||
|
JobContext.setJobId("main-job");
|
||||||
|
|
||||||
|
Thread other =
|
||||||
|
new Thread(
|
||||||
|
() -> {
|
||||||
|
assertNull(JobContext.getJobId());
|
||||||
|
JobContext.setJobId("other-job");
|
||||||
|
assertEquals("other-job", JobContext.getJobId());
|
||||||
|
});
|
||||||
|
other.start();
|
||||||
|
other.join();
|
||||||
|
|
||||||
|
assertEquals("main-job", JobContext.getJobId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should allow overwriting job ID")
|
||||||
|
void overwriteJobId() {
|
||||||
|
JobContext.setJobId("first");
|
||||||
|
JobContext.setJobId("second");
|
||||||
|
assertEquals("second", JobContext.getJobId());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||||
|
|
||||||
|
class PDFServiceTest {
|
||||||
|
|
||||||
|
private PDFService pdfService;
|
||||||
|
private CustomPDFDocumentFactory mockFactory;
|
||||||
|
private final List<PDDocument> documentsToClose = new ArrayList<>();
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
mockFactory = mock(CustomPDFDocumentFactory.class);
|
||||||
|
pdfService = new PDFService(mockFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() throws IOException {
|
||||||
|
for (PDDocument doc : documentsToClose) {
|
||||||
|
try {
|
||||||
|
doc.close();
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PDDocument createDocWithPages(int pageCount) {
|
||||||
|
PDDocument doc = new PDDocument();
|
||||||
|
for (int i = 0; i < pageCount; i++) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
}
|
||||||
|
documentsToClose.add(doc);
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void mergeDocuments_twoDocuments_mergesPages() throws IOException {
|
||||||
|
PDDocument merged = new PDDocument();
|
||||||
|
documentsToClose.add(merged);
|
||||||
|
when(mockFactory.createNewDocument()).thenReturn(merged);
|
||||||
|
|
||||||
|
PDDocument doc1 = createDocWithPages(2);
|
||||||
|
PDDocument doc2 = createDocWithPages(3);
|
||||||
|
|
||||||
|
PDDocument result = pdfService.mergeDocuments(List.of(doc1, doc2));
|
||||||
|
assertEquals(5, result.getNumberOfPages());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void mergeDocuments_emptyList_returnsEmptyDocument() throws IOException {
|
||||||
|
PDDocument merged = new PDDocument();
|
||||||
|
documentsToClose.add(merged);
|
||||||
|
when(mockFactory.createNewDocument()).thenReturn(merged);
|
||||||
|
|
||||||
|
PDDocument result = pdfService.mergeDocuments(List.of());
|
||||||
|
assertEquals(0, result.getNumberOfPages());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void mergeDocuments_singleDocument_returnsSamePages() throws IOException {
|
||||||
|
PDDocument merged = new PDDocument();
|
||||||
|
documentsToClose.add(merged);
|
||||||
|
when(mockFactory.createNewDocument()).thenReturn(merged);
|
||||||
|
|
||||||
|
PDDocument doc1 = createDocWithPages(4);
|
||||||
|
|
||||||
|
PDDocument result = pdfService.mergeDocuments(List.of(doc1));
|
||||||
|
assertEquals(4, result.getNumberOfPages());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void mergeDocuments_factoryCalled() throws IOException {
|
||||||
|
PDDocument merged = new PDDocument();
|
||||||
|
documentsToClose.add(merged);
|
||||||
|
when(mockFactory.createNewDocument()).thenReturn(merged);
|
||||||
|
|
||||||
|
PDDocument doc1 = createDocWithPages(1);
|
||||||
|
|
||||||
|
pdfService.mergeDocuments(List.of(doc1));
|
||||||
|
verify(mockFactory).createNewDocument();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class PdfAttachmentHandlerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void formatEmailDate_nullDate_returnsEmptyString() {
|
||||||
|
assertEquals("", PdfAttachmentHandler.formatEmailDate((Date) null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void formatEmailDate_nullZonedDateTime_returnsEmptyString() {
|
||||||
|
assertEquals("", PdfAttachmentHandler.formatEmailDate((ZonedDateTime) null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void formatEmailDate_validDate_returnsFormattedString() {
|
||||||
|
// Create a date: January 15, 2024 10:30 AM UTC
|
||||||
|
GregorianCalendar cal = new GregorianCalendar(java.util.TimeZone.getTimeZone("UTC"));
|
||||||
|
cal.set(2024, 0, 15, 10, 30, 0);
|
||||||
|
cal.set(java.util.Calendar.MILLISECOND, 0);
|
||||||
|
Date date = cal.getTime();
|
||||||
|
|
||||||
|
String result = PdfAttachmentHandler.formatEmailDate(date);
|
||||||
|
assertNotNull(result);
|
||||||
|
assertFalse(result.isEmpty());
|
||||||
|
// Should contain the date components
|
||||||
|
assertTrue(result.contains("2024"));
|
||||||
|
assertTrue(result.contains("Jan"));
|
||||||
|
assertTrue(result.contains("15"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void formatEmailDate_zonedDateTime_returnsUTCFormatted() {
|
||||||
|
ZonedDateTime dateTime =
|
||||||
|
ZonedDateTime.of(2024, 3, 15, 14, 30, 0, 0, ZoneId.of("America/New_York"));
|
||||||
|
String result = PdfAttachmentHandler.formatEmailDate(dateTime);
|
||||||
|
assertNotNull(result);
|
||||||
|
assertFalse(result.isEmpty());
|
||||||
|
// Should be converted to UTC
|
||||||
|
assertTrue(result.contains("UTC"));
|
||||||
|
assertTrue(result.contains("2024"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void processInlineImages_nullHtmlContent_returnsNull() {
|
||||||
|
String result = PdfAttachmentHandler.processInlineImages(null, null);
|
||||||
|
assertNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void processInlineImages_nullEmailContent_returnsOriginal() {
|
||||||
|
String html = "<html><body>test</body></html>";
|
||||||
|
String result = PdfAttachmentHandler.processInlineImages(html, null);
|
||||||
|
assertEquals(html, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void processInlineImages_noCidReferences_returnsOriginal() {
|
||||||
|
EmlParser.EmailContent emailContent = new EmlParser.EmailContent();
|
||||||
|
String html = "<html><body><img src='test.png'/></body></html>";
|
||||||
|
String result = PdfAttachmentHandler.processInlineImages(html, emailContent);
|
||||||
|
assertEquals(html, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void markerPosition_constructorAndGetters() {
|
||||||
|
PdfAttachmentHandler.MarkerPosition pos =
|
||||||
|
new PdfAttachmentHandler.MarkerPosition(2, 100.5f, 200.3f, "@", "test.pdf");
|
||||||
|
assertEquals(2, pos.getPageIndex());
|
||||||
|
assertEquals(100.5f, pos.getX(), 0.001f);
|
||||||
|
assertEquals(200.3f, pos.getY(), 0.001f);
|
||||||
|
assertEquals("@", pos.getCharacter());
|
||||||
|
assertEquals("test.pdf", pos.getFilename());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void markerPosition_setters() {
|
||||||
|
PdfAttachmentHandler.MarkerPosition pos =
|
||||||
|
new PdfAttachmentHandler.MarkerPosition(0, 0f, 0f, "@", null);
|
||||||
|
pos.setPageIndex(5);
|
||||||
|
pos.setX(50.0f);
|
||||||
|
pos.setY(75.0f);
|
||||||
|
pos.setFilename("doc.pdf");
|
||||||
|
assertEquals(5, pos.getPageIndex());
|
||||||
|
assertEquals(50.0f, pos.getX(), 0.001f);
|
||||||
|
assertEquals(75.0f, pos.getY(), 0.001f);
|
||||||
|
assertEquals("doc.pdf", pos.getFilename());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
class PdfErrorUtilsTest {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(
|
||||||
|
strings = {
|
||||||
|
"Missing root object specification",
|
||||||
|
"Header doesn't contain versioninfo",
|
||||||
|
"Expected trailer",
|
||||||
|
"Invalid PDF",
|
||||||
|
"Corrupted",
|
||||||
|
"damaged",
|
||||||
|
"Unknown dir object",
|
||||||
|
"Can't dereference COSObject",
|
||||||
|
"parseCOSString string should start with",
|
||||||
|
"ICCBased colorspace array must have a stream",
|
||||||
|
"1-based index not found",
|
||||||
|
"Invalid dictionary, found:",
|
||||||
|
"AES initialization vector not fully read",
|
||||||
|
"BadPaddingException",
|
||||||
|
"Given final block not properly padded",
|
||||||
|
"End-of-File, expected line"
|
||||||
|
})
|
||||||
|
void isCorruptedPdfError_ioException_corruptionIndicators_returnsTrue(String message) {
|
||||||
|
IOException e = new IOException(message);
|
||||||
|
assertTrue(PdfErrorUtils.isCorruptedPdfError(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(
|
||||||
|
strings = {
|
||||||
|
"Missing root object specification in the file",
|
||||||
|
"Header doesn't contain versioninfo xyz",
|
||||||
|
"Some prefix Corrupted suffix"
|
||||||
|
})
|
||||||
|
void isCorruptedPdfError_ioException_messagesContainingIndicators_returnsTrue(String message) {
|
||||||
|
IOException e = new IOException(message);
|
||||||
|
assertTrue(PdfErrorUtils.isCorruptedPdfError(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCorruptedPdfError_ioException_normalError_returnsFalse() {
|
||||||
|
IOException e = new IOException("File not found");
|
||||||
|
assertFalse(PdfErrorUtils.isCorruptedPdfError(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCorruptedPdfError_ioException_nullMessage_returnsFalse() {
|
||||||
|
IOException e = new IOException((String) null);
|
||||||
|
assertFalse(PdfErrorUtils.isCorruptedPdfError(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCorruptedPdfError_genericException_corruptionMessage_returnsTrue() {
|
||||||
|
Exception e = new RuntimeException("Invalid PDF structure");
|
||||||
|
assertTrue(PdfErrorUtils.isCorruptedPdfError(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCorruptedPdfError_genericException_normalMessage_returnsFalse() {
|
||||||
|
Exception e = new RuntimeException("Something went wrong");
|
||||||
|
assertFalse(PdfErrorUtils.isCorruptedPdfError(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCorruptedPdfError_genericException_nullMessage_returnsFalse() {
|
||||||
|
Exception e = new RuntimeException((String) null);
|
||||||
|
assertFalse(PdfErrorUtils.isCorruptedPdfError(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isCorruptedPdfError_ioException_emptyMessage_returnsFalse() {
|
||||||
|
IOException e = new IOException("");
|
||||||
|
assertFalse(PdfErrorUtils.isCorruptedPdfError(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
class PdfToCbrUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_pdfExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document.pdf");
|
||||||
|
assertTrue(PdfToCbrUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_uppercasePdfExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document.PDF");
|
||||||
|
assertTrue(PdfToCbrUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_mixedCasePdfExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document.Pdf");
|
||||||
|
assertTrue(PdfToCbrUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_nonPdfExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document.txt");
|
||||||
|
assertFalse(PdfToCbrUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_nullFilename_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn(null);
|
||||||
|
assertFalse(PdfToCbrUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_imageExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("image.png");
|
||||||
|
assertFalse(PdfToCbrUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertPdfToCbr_nullFile_throwsException() {
|
||||||
|
assertThrows(Exception.class, () -> PdfToCbrUtils.convertPdfToCbr(null, 300, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertPdfToCbr_emptyFile_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(true);
|
||||||
|
assertThrows(Exception.class, () -> PdfToCbrUtils.convertPdfToCbr(file, 300, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertPdfToCbr_nonPdfFile_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(false);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("image.png");
|
||||||
|
assertThrows(Exception.class, () -> PdfToCbrUtils.convertPdfToCbr(file, 300, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertPdfToCbr_nullFilename_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(false);
|
||||||
|
when(file.getOriginalFilename()).thenReturn(null);
|
||||||
|
assertThrows(Exception.class, () -> PdfToCbrUtils.convertPdfToCbr(file, 300, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
class PdfToCbzUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_pdfExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document.pdf");
|
||||||
|
assertTrue(PdfToCbzUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_uppercasePdfExtension_returnsTrue() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("DOCUMENT.PDF");
|
||||||
|
assertTrue(PdfToCbzUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_nonPdfExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document.docx");
|
||||||
|
assertFalse(PdfToCbzUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_nullFilename_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn(null);
|
||||||
|
assertFalse(PdfToCbzUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPdfFile_noExtension_returnsFalse() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("document");
|
||||||
|
assertFalse(PdfToCbzUtils.isPdfFile(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertPdfToCbz_nullFile_throwsException() {
|
||||||
|
assertThrows(Exception.class, () -> PdfToCbzUtils.convertPdfToCbz(null, 300, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertPdfToCbz_emptyFile_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(true);
|
||||||
|
assertThrows(Exception.class, () -> PdfToCbzUtils.convertPdfToCbz(file, 300, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertPdfToCbz_nonPdfFile_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(false);
|
||||||
|
when(file.getOriginalFilename()).thenReturn("image.jpg");
|
||||||
|
assertThrows(Exception.class, () -> PdfToCbzUtils.convertPdfToCbz(file, 300, null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void convertPdfToCbz_nullFilename_throwsException() {
|
||||||
|
MultipartFile file = mock(MultipartFile.class);
|
||||||
|
when(file.isEmpty()).thenReturn(false);
|
||||||
|
when(file.getOriginalFilename()).thenReturn(null);
|
||||||
|
assertThrows(Exception.class, () -> PdfToCbzUtils.convertPdfToCbz(file, 300, null, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,736 +1,245 @@
|
|||||||
package stirling.software.common.util;
|
package stirling.software.common.util;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.mockStatic;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.awt.Graphics2D;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.ColorModel;
|
|
||||||
import java.awt.image.RenderedImage;
|
import java.awt.image.RenderedImage;
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipInputStream;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
|
|
||||||
import org.apache.pdfbox.cos.COSName;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDResources;
|
import org.apache.pdfbox.pdmodel.PDResources;
|
||||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
|
||||||
import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
|
|
||||||
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
|
|
||||||
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
|
||||||
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
|
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
|
||||||
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||||
import org.apache.pdfbox.rendering.ImageType;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.MockedStatic;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.springframework.mock.web.MockMultipartFile;
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
import stirling.software.common.model.ApplicationProperties;
|
class PdfUtilsTest {
|
||||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
|
||||||
import stirling.software.common.service.PdfMetadataService;
|
|
||||||
|
|
||||||
public class PdfUtilsTest {
|
@ParameterizedTest
|
||||||
|
@CsvSource({"A0", "A1", "A2", "A3", "A4", "A5", "A6", "LETTER", "LEGAL"})
|
||||||
@Test
|
void textToPageSize_validSizes_returnsCorrectRectangle(String size) {
|
||||||
void testTextToPageSize() {
|
PDRectangle result = PdfUtils.textToPageSize(size);
|
||||||
assertEquals(PDRectangle.A0, PdfUtils.textToPageSize("A0"));
|
assertNotNull(result);
|
||||||
assertEquals(PDRectangle.A1, PdfUtils.textToPageSize("A1"));
|
assertTrue(result.getWidth() > 0);
|
||||||
assertEquals(PDRectangle.A2, PdfUtils.textToPageSize("A2"));
|
assertTrue(result.getHeight() > 0);
|
||||||
assertEquals(PDRectangle.A3, PdfUtils.textToPageSize("A3"));
|
|
||||||
assertEquals(PDRectangle.A4, PdfUtils.textToPageSize("A4"));
|
|
||||||
assertEquals(PDRectangle.A5, PdfUtils.textToPageSize("A5"));
|
|
||||||
assertEquals(PDRectangle.A6, PdfUtils.textToPageSize("A6"));
|
|
||||||
assertEquals(PDRectangle.LETTER, PdfUtils.textToPageSize("LETTER"));
|
|
||||||
assertEquals(PDRectangle.LEGAL, PdfUtils.textToPageSize("LEGAL"));
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> PdfUtils.textToPageSize("INVALID"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetAllImages() throws Exception {
|
void textToPageSize_lowercaseA4_returnsA4() {
|
||||||
// Root resources
|
PDRectangle result = PdfUtils.textToPageSize("a4");
|
||||||
PDResources root = mock(PDResources.class);
|
assertEquals(PDRectangle.A4.getWidth(), result.getWidth(), 0.01f);
|
||||||
|
assertEquals(PDRectangle.A4.getHeight(), result.getHeight(), 0.01f);
|
||||||
COSName im1 = COSName.getPDFName("Im1");
|
|
||||||
COSName form1 = COSName.getPDFName("Form1");
|
|
||||||
COSName other1 = COSName.getPDFName("Other1");
|
|
||||||
when(root.getXObjectNames()).thenReturn(Arrays.asList(im1, form1, other1));
|
|
||||||
|
|
||||||
// Direct image at root
|
|
||||||
PDImageXObject imgXObj1 = mock(PDImageXObject.class);
|
|
||||||
BufferedImage img1 = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB);
|
|
||||||
when(imgXObj1.getImage()).thenReturn(img1);
|
|
||||||
when(root.getXObject(im1)).thenReturn(imgXObj1);
|
|
||||||
|
|
||||||
// "Other" XObject that should be ignored
|
|
||||||
PDXObject otherXObj = mock(PDXObject.class);
|
|
||||||
when(root.getXObject(other1)).thenReturn(otherXObj);
|
|
||||||
|
|
||||||
// Form XObject with its own resources
|
|
||||||
PDFormXObject formXObj = mock(PDFormXObject.class);
|
|
||||||
PDResources formRes = mock(PDResources.class);
|
|
||||||
when(formXObj.getResources()).thenReturn(formRes);
|
|
||||||
when(root.getXObject(form1)).thenReturn(formXObj);
|
|
||||||
|
|
||||||
// Inside the form: one image and a nested form
|
|
||||||
COSName im2 = COSName.getPDFName("Im2");
|
|
||||||
COSName nestedForm = COSName.getPDFName("NestedForm");
|
|
||||||
when(formRes.getXObjectNames()).thenReturn(Arrays.asList(im2, nestedForm));
|
|
||||||
|
|
||||||
PDImageXObject imgXObj2 = mock(PDImageXObject.class);
|
|
||||||
BufferedImage img2 = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
|
|
||||||
when(imgXObj2.getImage()).thenReturn(img2);
|
|
||||||
when(formRes.getXObject(im2)).thenReturn(imgXObj2);
|
|
||||||
|
|
||||||
PDFormXObject nestedFormXObj = mock(PDFormXObject.class);
|
|
||||||
PDResources nestedRes = mock(PDResources.class);
|
|
||||||
when(nestedFormXObj.getResources()).thenReturn(nestedRes);
|
|
||||||
when(formRes.getXObject(nestedForm)).thenReturn(nestedFormXObj);
|
|
||||||
|
|
||||||
// Deep nest: another image
|
|
||||||
COSName im3 = COSName.getPDFName("Im3");
|
|
||||||
when(nestedRes.getXObjectNames()).thenReturn(List.of(im3));
|
|
||||||
|
|
||||||
PDImageXObject imgXObj3 = mock(PDImageXObject.class);
|
|
||||||
BufferedImage img3 = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
|
|
||||||
when(imgXObj3.getImage()).thenReturn(img3);
|
|
||||||
when(nestedRes.getXObject(im3)).thenReturn(imgXObj3);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
List<RenderedImage> result = PdfUtils.getAllImages(root);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assertEquals(
|
|
||||||
3, result.size(), "It should find exactly 3 images (root + form + nested form).");
|
|
||||||
assertTrue(
|
|
||||||
result.containsAll(List.of(img1, img2, img3)),
|
|
||||||
"All expected images must be present.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testPageCountComparators() throws Exception {
|
void textToPageSize_invalidSize_throwsException() {
|
||||||
PDDocument doc1 = new PDDocument();
|
assertThrows(Exception.class, () -> PdfUtils.textToPageSize("INVALID"));
|
||||||
doc1.addPage(new PDPage());
|
|
||||||
doc1.addPage(new PDPage());
|
|
||||||
doc1.addPage(new PDPage());
|
|
||||||
assertTrue(PdfUtils.pageCount(doc1, 2, "greater"));
|
|
||||||
|
|
||||||
PDDocument doc2 = new PDDocument();
|
|
||||||
doc2.addPage(new PDPage());
|
|
||||||
doc2.addPage(new PDPage());
|
|
||||||
doc2.addPage(new PDPage());
|
|
||||||
assertTrue(PdfUtils.pageCount(doc2, 3, "equal"));
|
|
||||||
|
|
||||||
PDDocument doc3 = new PDDocument();
|
|
||||||
doc3.addPage(new PDPage());
|
|
||||||
doc3.addPage(new PDPage());
|
|
||||||
assertTrue(PdfUtils.pageCount(doc3, 5, "less"));
|
|
||||||
|
|
||||||
PDDocument doc4 = new PDDocument();
|
|
||||||
doc4.addPage(new PDPage());
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> PdfUtils.pageCount(doc4, 1, "bad"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testPageSize() throws Exception {
|
void getAllImages_emptyResources_returnsEmptyList() throws IOException {
|
||||||
PDDocument doc = new PDDocument();
|
|
||||||
PDPage page = new PDPage(PDRectangle.A4);
|
|
||||||
doc.addPage(page);
|
|
||||||
PDRectangle rect = page.getMediaBox();
|
|
||||||
String expected = rect.getWidth() + "x" + rect.getHeight();
|
|
||||||
assertTrue(PdfUtils.pageSize(doc, expected));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testOverlayImage() throws Exception {
|
|
||||||
PDDocument doc = new PDDocument();
|
|
||||||
doc.addPage(new PDPage(PDRectangle.A4));
|
|
||||||
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
|
|
||||||
doc.save(pdfOut);
|
|
||||||
doc.close();
|
|
||||||
|
|
||||||
BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
|
||||||
Graphics2D g = image.createGraphics();
|
|
||||||
g.setColor(Color.RED);
|
|
||||||
g.fillRect(0, 0, 10, 10);
|
|
||||||
g.dispose();
|
|
||||||
ByteArrayOutputStream imgOut = new ByteArrayOutputStream();
|
|
||||||
ImageIO.write(image, "png", imgOut);
|
|
||||||
|
|
||||||
PdfMetadataService meta =
|
|
||||||
new PdfMetadataService(new ApplicationProperties(), "label", false, null);
|
|
||||||
CustomPDFDocumentFactory factory = new CustomPDFDocumentFactory(meta);
|
|
||||||
|
|
||||||
byte[] result =
|
|
||||||
PdfUtils.overlayImage(
|
|
||||||
factory, pdfOut.toByteArray(), imgOut.toByteArray(), 0, 0, false);
|
|
||||||
try (PDDocument resultDoc = factory.load(result)) {
|
|
||||||
assertEquals(1, resultDoc.getNumberOfPages());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===============================================================
|
|
||||||
// Additional tests (added without modifying existing ones)
|
|
||||||
// ===============================================================
|
|
||||||
|
|
||||||
/* Helper: create a colored test image */
|
|
||||||
private static BufferedImage createImage(int w, int h, Color color) {
|
|
||||||
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
|
|
||||||
Graphics2D g = img.createGraphics();
|
|
||||||
g.setColor(color);
|
|
||||||
g.fillRect(0, 0, w, h);
|
|
||||||
g.dispose();
|
|
||||||
return img;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Helper: create a factory like in existing tests */
|
|
||||||
private static CustomPDFDocumentFactory factory() {
|
|
||||||
PdfMetadataService meta =
|
|
||||||
new PdfMetadataService(new ApplicationProperties(), "label", false, null);
|
|
||||||
return new CustomPDFDocumentFactory(meta);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("convertPdfToPdfImage: creates image-PDF with same page count")
|
|
||||||
void convertPdfToPdfImage_shouldCreateImagePdfWithSamePageCount() throws IOException {
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
PDPage p1 = new PDPage(PDRectangle.A4);
|
PDPage page = new PDPage();
|
||||||
doc.addPage(p1);
|
page.setResources(new PDResources());
|
||||||
try (PDPageContentStream cs =
|
doc.addPage(page);
|
||||||
new PDPageContentStream(doc, p1, AppendMode.APPEND, true, true)) {
|
List<RenderedImage> images = PdfUtils.getAllImages(page.getResources());
|
||||||
cs.beginText();
|
assertTrue(images.isEmpty());
|
||||||
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
|
|
||||||
cs.newLineAtOffset(50, 750);
|
|
||||||
cs.showText("Hello PDF");
|
|
||||||
cs.endText();
|
|
||||||
}
|
|
||||||
PDPage p2 = new PDPage(PDRectangle.A4);
|
|
||||||
doc.addPage(p2);
|
|
||||||
|
|
||||||
PDDocument out = PdfUtils.convertPdfToPdfImage(doc);
|
|
||||||
assertNotNull(out);
|
|
||||||
assertEquals(2, out.getNumberOfPages(), "Page count should be preserved");
|
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("imageToPdf: PNG -> single-page PDF (static ImageProcessingUtils mocked)")
|
void getAllImages_withImage_returnsImage() throws IOException {
|
||||||
void imageToPdf_shouldCreatePdfFromPng() throws Exception {
|
|
||||||
BufferedImage img = createImage(320, 200, Color.RED);
|
|
||||||
ByteArrayOutputStream pngOut = new ByteArrayOutputStream();
|
|
||||||
ImageIO.write(img, "png", pngOut);
|
|
||||||
|
|
||||||
MockMultipartFile file =
|
|
||||||
new MockMultipartFile("files", "test.png", "image/png", pngOut.toByteArray());
|
|
||||||
|
|
||||||
try (MockedStatic<ImageProcessingUtils> mocked = mockStatic(ImageProcessingUtils.class)) {
|
|
||||||
// Assume: loadImageWithExifOrientation/convertColorType exist – static mock
|
|
||||||
mocked.when(() -> ImageProcessingUtils.loadImageWithExifOrientation(any()))
|
|
||||||
.thenReturn(img);
|
|
||||||
mocked.when(
|
|
||||||
() ->
|
|
||||||
ImageProcessingUtils.convertColorType(
|
|
||||||
any(BufferedImage.class), anyString()))
|
|
||||||
.thenAnswer(inv -> inv.getArgument(0, BufferedImage.class));
|
|
||||||
|
|
||||||
byte[] pdfBytes =
|
|
||||||
PdfUtils.imageToPdf(
|
|
||||||
new MockMultipartFile[] {file},
|
|
||||||
"maintainAspectRatio",
|
|
||||||
true,
|
|
||||||
"RGB",
|
|
||||||
factory());
|
|
||||||
|
|
||||||
try (PDDocument result = factory().load(pdfBytes)) {
|
|
||||||
assertEquals(1, result.getNumberOfPages());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("imageToPdf: JPEG -> single-page PDF (JPEGFactory path)")
|
|
||||||
void imageToPdf_shouldCreatePdfFromJpeg_UsingJpegFactory() throws Exception {
|
|
||||||
BufferedImage img = createImage(640, 360, Color.BLUE);
|
|
||||||
ByteArrayOutputStream jpgOut = new ByteArrayOutputStream();
|
|
||||||
ImageIO.write(img, "jpg", jpgOut);
|
|
||||||
|
|
||||||
MockMultipartFile file =
|
|
||||||
new MockMultipartFile("files", "photo.jpg", "image/jpeg", jpgOut.toByteArray());
|
|
||||||
|
|
||||||
try (MockedStatic<ImageProcessingUtils> mocked = mockStatic(ImageProcessingUtils.class)) {
|
|
||||||
mocked.when(() -> ImageProcessingUtils.loadImageWithExifOrientation(any()))
|
|
||||||
.thenReturn(img);
|
|
||||||
mocked.when(
|
|
||||||
() ->
|
|
||||||
ImageProcessingUtils.convertColorType(
|
|
||||||
any(BufferedImage.class), anyString()))
|
|
||||||
.thenAnswer(inv -> inv.getArgument(0, BufferedImage.class));
|
|
||||||
|
|
||||||
byte[] pdfBytes =
|
|
||||||
PdfUtils.imageToPdf(
|
|
||||||
new MockMultipartFile[] {file}, "fillPage", false, "RGB", factory());
|
|
||||||
|
|
||||||
try (PDDocument result = factory().load(pdfBytes)) {
|
|
||||||
assertEquals(1, result.getNumberOfPages());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("addImageToDocument: fitDocumentToImage -> page size = image size")
|
|
||||||
void addImageToDocument_shouldUseImageSizeForPage_whenFitDocumentToImage() throws IOException {
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
BufferedImage img = createImage(300, 500, Color.GREEN);
|
PDPage page = new PDPage();
|
||||||
PDImageXObject ximg = LosslessFactory.createFromImage(doc, img);
|
doc.addPage(page);
|
||||||
|
|
||||||
PdfUtils.addImageToDocument(doc, ximg, "fitDocumentToImage", false);
|
BufferedImage bufferedImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
||||||
|
PDImageXObject pdImage = LosslessFactory.createFromImage(doc, bufferedImage);
|
||||||
|
|
||||||
assertEquals(1, doc.getNumberOfPages());
|
PDResources resources = new PDResources();
|
||||||
PDRectangle box = doc.getPage(0).getMediaBox();
|
resources.add(pdImage);
|
||||||
assertEquals(300, (int) box.getWidth());
|
page.setResources(resources);
|
||||||
assertEquals(500, (int) box.getHeight());
|
|
||||||
|
List<RenderedImage> images = PdfUtils.getAllImages(page.getResources());
|
||||||
|
assertEquals(1, images.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("addImageToDocument: autoRotate rotates A4 for landscape image")
|
void hasImagesOnPage_noImages_returnsFalse() throws IOException {
|
||||||
void addImageToDocument_shouldRotateA4_whenAutoRotateAndLandscape() throws IOException {
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
BufferedImage img = createImage(800, 400, Color.ORANGE); // Landscape
|
PDPage page = new PDPage();
|
||||||
PDImageXObject ximg = LosslessFactory.createFromImage(doc, img);
|
page.setResources(new PDResources());
|
||||||
|
doc.addPage(page);
|
||||||
PdfUtils.addImageToDocument(doc, ximg, "maintainAspectRatio", true);
|
assertFalse(PdfUtils.hasImagesOnPage(page));
|
||||||
|
|
||||||
assertEquals(1, doc.getNumberOfPages());
|
|
||||||
PDRectangle box = doc.getPage(0).getMediaBox();
|
|
||||||
assertTrue(
|
|
||||||
box.getWidth() > box.getHeight(),
|
|
||||||
"A4 should be landscape when auto-rotate + landscape");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("addImageToDocument: fillPage runs without errors")
|
void hasTextOnPage_noText_returnsFalse() throws IOException {
|
||||||
void addImageToDocument_fillPage_executes() throws IOException {
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
BufferedImage img = createImage(200, 200, Color.MAGENTA);
|
PDPage page = new PDPage();
|
||||||
PDImageXObject ximg = LosslessFactory.createFromImage(doc, img);
|
doc.addPage(page);
|
||||||
|
assertFalse(PdfUtils.hasTextOnPage(page, "hello"));
|
||||||
PdfUtils.addImageToDocument(doc, ximg, "fillPage", false);
|
|
||||||
|
|
||||||
assertEquals(1, doc.getNumberOfPages());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("overlayImage: everyPage=true overlays all pages")
|
void pageCount_greaterComparator_correct() throws IOException {
|
||||||
void overlayImage_shouldOverlayAllPages_whenEveryPageTrue() throws IOException {
|
|
||||||
CustomPDFDocumentFactory factory = factory();
|
|
||||||
|
|
||||||
// Create PDF with 2 pages
|
|
||||||
byte[] basePdf;
|
|
||||||
try (PDDocument doc = factory.createNewDocument()) {
|
|
||||||
doc.addPage(new PDPage(PDRectangle.A4));
|
|
||||||
doc.addPage(new PDPage(PDRectangle.A4));
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
doc.save(baos);
|
|
||||||
basePdf = baos.toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create image bytes
|
|
||||||
BufferedImage img = createImage(50, 50, Color.BLACK);
|
|
||||||
ByteArrayOutputStream pngOut = new ByteArrayOutputStream();
|
|
||||||
ImageIO.write(img, "png", pngOut);
|
|
||||||
|
|
||||||
byte[] result = PdfUtils.overlayImage(factory, basePdf, pngOut.toByteArray(), 10, 10, true);
|
|
||||||
|
|
||||||
try (PDDocument out = factory.load(result)) {
|
|
||||||
assertEquals(2, out.getNumberOfPages(), "Page count remains identical");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Helper function: document with text on page1/page2 */
|
|
||||||
private static PDDocument createDocWithText(String p1, String p2) throws IOException {
|
|
||||||
PDDocument doc = new PDDocument();
|
|
||||||
|
|
||||||
PDPage page1 = new PDPage(PDRectangle.A4);
|
|
||||||
doc.addPage(page1);
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, page1, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.beginText();
|
|
||||||
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
|
|
||||||
cs.newLineAtOffset(50, 750);
|
|
||||||
cs.showText(p1);
|
|
||||||
cs.endText();
|
|
||||||
}
|
|
||||||
|
|
||||||
PDPage page2 = new PDPage(PDRectangle.A4);
|
|
||||||
doc.addPage(page2);
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, page2, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.beginText();
|
|
||||||
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
|
|
||||||
cs.newLineAtOffset(50, 750);
|
|
||||||
cs.showText(p2);
|
|
||||||
cs.endText();
|
|
||||||
}
|
|
||||||
|
|
||||||
return doc;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("containsTextInFile: pagesToCheck='all' finds text")
|
|
||||||
void containsTextInFile_allPages_true() throws IOException {
|
|
||||||
try (PDDocument doc = createDocWithText("alpha", "beta")) {
|
|
||||||
assertTrue(PdfUtils.containsTextInFile(doc, "beta", "all"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("containsTextInFile: single page '2' finds text")
|
|
||||||
void containsTextInFile_singlePage_two_true() throws IOException {
|
|
||||||
try (PDDocument doc = createDocWithText("alpha", "beta")) {
|
|
||||||
assertTrue(PdfUtils.containsTextInFile(doc, "beta", "2"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("containsTextInFile: range '1-1' finds text on page 1")
|
|
||||||
void containsTextInFile_range_oneToOne_true() throws IOException {
|
|
||||||
try (PDDocument doc = createDocWithText("findme", "other")) {
|
|
||||||
assertTrue(PdfUtils.containsTextInFile(doc, "findme", "1-1"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("containsTextInFile: list '1,2' finds text (whitespace robust)")
|
|
||||||
void containsTextInFile_list_pages_true() throws IOException {
|
|
||||||
try (PDDocument doc = createDocWithText("foo", "bar")) {
|
|
||||||
assertTrue(PdfUtils.containsTextInFile(doc, "bar", " 1 , 2 "));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("containsTextInFile: text not present -> false")
|
|
||||||
void containsTextInFile_textNotPresent_false() throws IOException {
|
|
||||||
try (PDDocument doc = createDocWithText("xxx", "yyy")) {
|
|
||||||
assertFalse(PdfUtils.containsTextInFile(doc, "zzz", "all"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("pageSize: different size returns false")
|
|
||||||
void pageSize_shouldReturnFalse_whenSizeDoesNotMatch() throws IOException {
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
doc.addPage(new PDPage(PDRectangle.A4));
|
doc.addPage(new PDPage());
|
||||||
assertFalse(PdfUtils.pageSize(doc, "600x842"));
|
doc.addPage(new PDPage());
|
||||||
}
|
doc.addPage(new PDPage());
|
||||||
}
|
assertTrue(PdfUtils.pageCount(doc, 2, "greater"));
|
||||||
|
|
||||||
// ===================== New: convertFromPdf – coverage =====================
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("convertFromPdf: singleImage=true creates combined PNG file (readable)")
|
|
||||||
void convertFromPdf_singleImagePng_combinedReadable() throws Exception {
|
|
||||||
// Create two-page PDF
|
|
||||||
byte[] pdfBytes;
|
|
||||||
PdfMetadataService meta =
|
|
||||||
new PdfMetadataService(new ApplicationProperties(), "label", false, null);
|
|
||||||
CustomPDFDocumentFactory factory = new CustomPDFDocumentFactory(meta);
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
|
||||||
doc.addPage(new PDPage(PDRectangle.A4));
|
|
||||||
doc.addPage(new PDPage(PDRectangle.A4));
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
doc.save(baos);
|
|
||||||
pdfBytes = baos.toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] imageBytes =
|
|
||||||
PdfUtils.convertFromPdf(
|
|
||||||
factory, pdfBytes, "png", ImageType.RGB, true, 72, "test.pdf", false);
|
|
||||||
|
|
||||||
// Should be readable as a single combined PNG image
|
|
||||||
BufferedImage img = ImageIO.read(new java.io.ByteArrayInputStream(imageBytes));
|
|
||||||
assertNotNull(img, "PNG should be readable");
|
|
||||||
assertTrue(img.getWidth() > 0 && img.getHeight() > 0, "Image dimensions > 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName(
|
|
||||||
"convertFromPdf: singleImage=false returns ZIP with PNG entries (first image readable)")
|
|
||||||
void convertFromPdf_multiImagePng_firstReadable() throws Exception {
|
|
||||||
// Create two-page PDF
|
|
||||||
byte[] pdfBytes;
|
|
||||||
PdfMetadataService meta =
|
|
||||||
new PdfMetadataService(new ApplicationProperties(), "label", false, null);
|
|
||||||
CustomPDFDocumentFactory factory = new CustomPDFDocumentFactory(meta);
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
|
||||||
doc.addPage(new PDPage(PDRectangle.A4));
|
|
||||||
doc.addPage(new PDPage(PDRectangle.A4));
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
doc.save(baos);
|
|
||||||
pdfBytes = baos.toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Act: singleImage=false -> ZIP with separate images
|
|
||||||
byte[] zipBytes =
|
|
||||||
PdfUtils.convertFromPdf(
|
|
||||||
factory, pdfBytes, "png", ImageType.RGB, false, 72, "test.pdf", false);
|
|
||||||
|
|
||||||
// Assert: open ZIP, read first entry as PNG
|
|
||||||
try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipBytes))) {
|
|
||||||
ZipEntry entry = zis.getNextEntry();
|
|
||||||
assertNotNull(entry, "ZIP should contain at least one entry");
|
|
||||||
|
|
||||||
ByteArrayOutputStream imgOut = new ByteArrayOutputStream();
|
|
||||||
zis.transferTo(imgOut);
|
|
||||||
BufferedImage first = ImageIO.read(new ByteArrayInputStream(imgOut.toByteArray()));
|
|
||||||
|
|
||||||
assertNotNull(first, "First PNG entry should be readable");
|
|
||||||
assertTrue(first.getWidth() > 0 && first.getHeight() > 0, "Image dimensions > 0");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("hasText: detects phrase on selected pages ('1', '2', 'all')")
|
void pageCount_equalComparator_correct() throws IOException {
|
||||||
void hasText_shouldDetectPhrase_onSelectedPages() throws Exception {
|
|
||||||
// Arrange: PDF with 2 pages and text
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
PDPage p1 = new PDPage(PDRectangle.A4);
|
doc.addPage(new PDPage());
|
||||||
PDPage p2 = new PDPage(PDRectangle.A4);
|
doc.addPage(new PDPage());
|
||||||
doc.addPage(p1);
|
assertTrue(PdfUtils.pageCount(doc, 2, "equal"));
|
||||||
doc.addPage(p2);
|
|
||||||
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p1, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.beginText();
|
|
||||||
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
|
|
||||||
cs.newLineAtOffset(50, 750);
|
|
||||||
cs.showText("alpha on page 1");
|
|
||||||
cs.endText();
|
|
||||||
}
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p2, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.beginText();
|
|
||||||
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
|
|
||||||
cs.newLineAtOffset(50, 750);
|
|
||||||
cs.showText("beta on page 2");
|
|
||||||
cs.endText();
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(PdfUtils.hasText(doc, "1", "alpha"), "Page 1 should contain 'alpha'");
|
|
||||||
}
|
|
||||||
|
|
||||||
// For further checks, create new doc with identical content
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
|
||||||
PDPage p1 = new PDPage(PDRectangle.A4);
|
|
||||||
PDPage p2 = new PDPage(PDRectangle.A4);
|
|
||||||
doc.addPage(p1);
|
|
||||||
doc.addPage(p2);
|
|
||||||
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p1, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.beginText();
|
|
||||||
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
|
|
||||||
cs.newLineAtOffset(50, 750);
|
|
||||||
cs.showText("alpha on page 1");
|
|
||||||
cs.endText();
|
|
||||||
}
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p2, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.beginText();
|
|
||||||
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
|
|
||||||
cs.newLineAtOffset(50, 750);
|
|
||||||
cs.showText("beta on page 2");
|
|
||||||
cs.endText();
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(PdfUtils.hasText(doc, "2", "beta"), "Page 2 should contain 'beta'");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Third doc for 'all'
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
|
||||||
PDPage p1 = new PDPage(PDRectangle.A4);
|
|
||||||
PDPage p2 = new PDPage(PDRectangle.A4);
|
|
||||||
doc.addPage(p1);
|
|
||||||
doc.addPage(p2);
|
|
||||||
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p1, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.beginText();
|
|
||||||
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
|
|
||||||
cs.newLineAtOffset(50, 750);
|
|
||||||
cs.showText("gamma");
|
|
||||||
cs.endText();
|
|
||||||
}
|
|
||||||
assertTrue(PdfUtils.hasText(doc, "all", "gamma"), "'all' should find text on page 1");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("hasTextOnPage: true if page contains phrase, else false")
|
void pageCount_lessComparator_correct() throws IOException {
|
||||||
void hasTextOnPage_shouldReturnTrueOnlyForPagesWithPhrase() throws Exception {
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
PDPage p1 = new PDPage(PDRectangle.A4);
|
doc.addPage(new PDPage());
|
||||||
PDPage p2 = new PDPage(PDRectangle.A4);
|
assertTrue(PdfUtils.pageCount(doc, 5, "less"));
|
||||||
doc.addPage(p1);
|
|
||||||
doc.addPage(p2);
|
|
||||||
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p1, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.beginText();
|
|
||||||
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
|
|
||||||
cs.newLineAtOffset(50, 750);
|
|
||||||
cs.showText("needle");
|
|
||||||
cs.endText();
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(PdfUtils.hasTextOnPage(p1, "needle"));
|
|
||||||
assertTrue(!PdfUtils.hasTextOnPage(p2, "needle"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("hasImages: detects images on selected pages and 'all'")
|
void pageCount_invalidComparator_throwsException() throws IOException {
|
||||||
void hasImages_shouldDetectImages_onSelectedPages() throws Exception {
|
|
||||||
// Case 1: Page 1 without image (but resources set) -> false
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
PDPage p1 = new PDPage(PDRectangle.A4);
|
doc.addPage(new PDPage());
|
||||||
PDPage p2 = new PDPage(PDRectangle.A4);
|
assertThrows(Exception.class, () -> PdfUtils.pageCount(doc, 1, "invalid"));
|
||||||
p1.setResources(new PDResources());
|
|
||||||
p2.setResources(new PDResources());
|
|
||||||
doc.addPage(p1);
|
|
||||||
doc.addPage(p2);
|
|
||||||
|
|
||||||
// Image only on page 2
|
|
||||||
BufferedImage bi = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
|
|
||||||
Graphics2D g = bi.createGraphics();
|
|
||||||
g.setColor(Color.GREEN);
|
|
||||||
g.fillRect(0, 0, 20, 20);
|
|
||||||
g.dispose();
|
|
||||||
|
|
||||||
PDImageXObject ximg = LosslessFactory.createFromImage(doc, bi);
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p2, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.drawImage(ximg, 50, 700, 20, 20);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(!PdfUtils.hasImages(doc, "1"), "Page 1 should have no image");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Case 2: Page 2 with image -> true
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
|
||||||
PDPage p1 = new PDPage(PDRectangle.A4);
|
|
||||||
PDPage p2 = new PDPage(PDRectangle.A4);
|
|
||||||
p1.setResources(new PDResources());
|
|
||||||
p2.setResources(new PDResources());
|
|
||||||
doc.addPage(p1);
|
|
||||||
doc.addPage(p2);
|
|
||||||
|
|
||||||
BufferedImage bi = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
|
|
||||||
Graphics2D g = bi.createGraphics();
|
|
||||||
g.setColor(Color.BLUE);
|
|
||||||
g.fillRect(0, 0, 20, 20);
|
|
||||||
g.dispose();
|
|
||||||
|
|
||||||
PDImageXObject ximg = LosslessFactory.createFromImage(doc, bi);
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p2, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.drawImage(ximg, 50, 700, 20, 20);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(PdfUtils.hasImages(doc, "2"), "Page 2 should have an image");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Case 3: 'all' detects image
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
|
||||||
PDPage p = new PDPage(PDRectangle.A4);
|
|
||||||
p.setResources(new PDResources());
|
|
||||||
doc.addPage(p);
|
|
||||||
|
|
||||||
BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
|
||||||
PDImageXObject ximg = LosslessFactory.createFromImage(doc, bi);
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.drawImage(ximg, 20, 730, 10, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(PdfUtils.hasImages(doc, "all"), "'all' should detect the image");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("hasImagesOnPage: true if page contains an image, else false")
|
void pageSize_matchingSize_returnsTrue() throws IOException {
|
||||||
void hasImagesOnPage_shouldReturnTrueOnlyForPagesWithImage() throws Exception {
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
PDPage p1 = new PDPage(PDRectangle.A4);
|
PDPage page = new PDPage(PDRectangle.A4);
|
||||||
PDPage p2 = new PDPage(PDRectangle.A4);
|
doc.addPage(page);
|
||||||
p1.setResources(new PDResources());
|
String sizeStr = PDRectangle.A4.getWidth() + "x" + PDRectangle.A4.getHeight();
|
||||||
p2.setResources(new PDResources());
|
assertTrue(PdfUtils.pageSize(doc, sizeStr));
|
||||||
doc.addPage(p1);
|
|
||||||
doc.addPage(p2);
|
|
||||||
|
|
||||||
BufferedImage bi = new BufferedImage(12, 12, BufferedImage.TYPE_INT_RGB);
|
|
||||||
Graphics2D g = bi.createGraphics();
|
|
||||||
g.setColor(Color.RED);
|
|
||||||
g.fillRect(0, 0, 12, 12);
|
|
||||||
g.dispose();
|
|
||||||
|
|
||||||
PDImageXObject ximg = LosslessFactory.createFromImage(doc, bi);
|
|
||||||
try (PDPageContentStream cs =
|
|
||||||
new PDPageContentStream(doc, p1, AppendMode.APPEND, true, true)) {
|
|
||||||
cs.drawImage(ximg, 40, 720, 12, 12);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(PdfUtils.hasImagesOnPage(p1));
|
|
||||||
assertTrue(!PdfUtils.hasImagesOnPage(p2));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("convertFromPdf: singleImage=true with JPG -> no alpha, white background")
|
void pageSize_nonMatchingSize_returnsFalse() throws IOException {
|
||||||
void convertFromPdf_singleImageJpg_noAlphaWhiteBackground() throws Exception {
|
|
||||||
// small 1-page PDF
|
|
||||||
byte[] pdfBytes;
|
|
||||||
PdfMetadataService meta =
|
|
||||||
new PdfMetadataService(new ApplicationProperties(), "label", false, null);
|
|
||||||
CustomPDFDocumentFactory factory = new CustomPDFDocumentFactory(meta);
|
|
||||||
try (PDDocument doc = new PDDocument()) {
|
try (PDDocument doc = new PDDocument()) {
|
||||||
PDPage p = new PDPage(PDRectangle.A4);
|
PDPage page = new PDPage(PDRectangle.A4);
|
||||||
doc.addPage(p);
|
doc.addPage(page);
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
assertFalse(PdfUtils.pageSize(doc, "100x100"));
|
||||||
doc.save(baos);
|
|
||||||
pdfBytes = baos.toByteArray();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
byte[] jpgBytes =
|
// --- hasImages ---
|
||||||
PdfUtils.convertFromPdf(
|
|
||||||
factory, pdfBytes, "jpg", ImageType.RGB, true, 72, "sample.pdf", false);
|
|
||||||
|
|
||||||
BufferedImage img = ImageIO.read(new ByteArrayInputStream(jpgBytes));
|
@Test
|
||||||
assertNotNull(img, "JPG should be readable");
|
void hasImages_noImages_returnsFalse() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDPage page = new PDPage();
|
||||||
|
page.setResources(new PDResources());
|
||||||
|
doc.addPage(page);
|
||||||
|
assertFalse(PdfUtils.hasImages(doc, "all"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ColorModel cm = img.getColorModel();
|
@Test
|
||||||
assertFalse(cm.hasAlpha(), "JPG output should have no alpha channel");
|
void hasImages_withImage_returnsTrue() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDPage page = new PDPage();
|
||||||
|
doc.addPage(page);
|
||||||
|
|
||||||
// JPG background should be white (approximate check)
|
BufferedImage bufferedImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
||||||
int rgb = img.getRGB(img.getWidth() / 2, img.getHeight() / 2) & 0x00FFFFFF;
|
PDImageXObject pdImage = LosslessFactory.createFromImage(doc, bufferedImage);
|
||||||
assertEquals(0xFFFFFF, rgb, "Background pixel should be white");
|
PDResources resources = new PDResources();
|
||||||
|
resources.add(pdImage);
|
||||||
|
page.setResources(resources);
|
||||||
|
|
||||||
|
assertTrue(PdfUtils.hasImages(doc, "all"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- hasText ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasText_noText_returnsFalse() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
assertFalse(PdfUtils.hasText(doc, "all", "hello"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- textToPageSize additional ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void textToPageSize_letter_returnsLetter() {
|
||||||
|
PDRectangle result = PdfUtils.textToPageSize("letter");
|
||||||
|
assertEquals(PDRectangle.LETTER.getWidth(), result.getWidth(), 0.01f);
|
||||||
|
assertEquals(PDRectangle.LETTER.getHeight(), result.getHeight(), 0.01f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void textToPageSize_legal_returnsLegal() {
|
||||||
|
PDRectangle result = PdfUtils.textToPageSize("legal");
|
||||||
|
assertEquals(PDRectangle.LEGAL.getWidth(), result.getWidth(), 0.01f);
|
||||||
|
assertEquals(PDRectangle.LEGAL.getHeight(), result.getHeight(), 0.01f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- pageCount additional ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void pageCount_greaterComparator_false() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
assertFalse(PdfUtils.pageCount(doc, 5, "greater"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void pageCount_equalComparator_false() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
assertFalse(PdfUtils.pageCount(doc, 3, "equal"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void pageCount_lessComparator_false() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
doc.addPage(new PDPage());
|
||||||
|
assertFalse(PdfUtils.pageCount(doc, 2, "less"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- hasImagesOnPage with image ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasImagesOnPage_withImage_returnsTrue() throws IOException {
|
||||||
|
try (PDDocument doc = new PDDocument()) {
|
||||||
|
PDPage page = new PDPage();
|
||||||
|
doc.addPage(page);
|
||||||
|
|
||||||
|
BufferedImage bufferedImage = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
|
||||||
|
PDImageXObject pdImage = LosslessFactory.createFromImage(doc, bufferedImage);
|
||||||
|
PDResources resources = new PDResources();
|
||||||
|
resources.add(pdImage);
|
||||||
|
page.setResources(resources);
|
||||||
|
|
||||||
|
assertTrue(PdfUtils.hasImagesOnPage(page));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,85 +2,119 @@ package stirling.software.common.util;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class ProcessExecutorTest {
|
class ProcessExecutorTest {
|
||||||
|
|
||||||
private ProcessExecutor processExecutor;
|
// Use reflection to test private validateCommand method
|
||||||
|
private void invokeValidateCommand(ProcessExecutor executor, List<String> command)
|
||||||
|
throws Exception {
|
||||||
|
Method method = ProcessExecutor.class.getDeclaredMethod("validateCommand", List.class);
|
||||||
|
method.setAccessible(true);
|
||||||
|
try {
|
||||||
|
method.invoke(executor, command);
|
||||||
|
} catch (java.lang.reflect.InvocationTargetException e) {
|
||||||
|
throw (Exception) e.getCause();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@BeforeEach
|
private ProcessExecutor getExecutor() {
|
||||||
public void setUp() {
|
return ProcessExecutor.getInstance(ProcessExecutor.Processes.QPDF);
|
||||||
// Initialize the ProcessExecutor instance
|
|
||||||
processExecutor = ProcessExecutor.getInstance(ProcessExecutor.Processes.LIBRE_OFFICE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRunCommandWithOutputHandling() throws IOException, InterruptedException {
|
void testValidateCommand_nullCommand() {
|
||||||
// Mock the command to execute
|
|
||||||
List<String> command = new ArrayList<>();
|
|
||||||
command.add("java");
|
|
||||||
command.add("-version");
|
|
||||||
|
|
||||||
// Execute the command
|
|
||||||
ProcessExecutor.ProcessExecutorResult result =
|
|
||||||
processExecutor.runCommandWithOutputHandling(command);
|
|
||||||
|
|
||||||
// Check the exit code and output messages
|
|
||||||
assertEquals(0, result.getRc());
|
|
||||||
assertNotNull(result.getMessages()); // Check if messages are not null
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testRunCommandWithOutputHandling_Error() {
|
|
||||||
// Test with a command that will fail to execute (non-existent command)
|
|
||||||
List<String> command = new ArrayList<>();
|
|
||||||
command.add("nonexistent-command-that-does-not-exist");
|
|
||||||
|
|
||||||
// Execute the command and expect an IOException (command not found)
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
IOException.class, () -> processExecutor.runCommandWithOutputHandling(command));
|
IllegalArgumentException.class, () -> invokeValidateCommand(getExecutor(), null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRunCommandWithOutputHandling_PathTraversal() {
|
void testValidateCommand_emptyCommand() {
|
||||||
// Test that path traversal is blocked
|
assertThrows(
|
||||||
List<String> command = new ArrayList<>();
|
IllegalArgumentException.class,
|
||||||
command.add("../../../etc/passwd");
|
() -> invokeValidateCommand(getExecutor(), List.of()));
|
||||||
|
|
||||||
// Execute the command and expect an IllegalArgumentException
|
|
||||||
IllegalArgumentException thrown =
|
|
||||||
assertThrows(
|
|
||||||
IllegalArgumentException.class,
|
|
||||||
() -> processExecutor.runCommandWithOutputHandling(command));
|
|
||||||
|
|
||||||
// Check the exception message
|
|
||||||
String errorMessage = thrown.getMessage();
|
|
||||||
assertTrue(
|
|
||||||
errorMessage.contains("path traversal"),
|
|
||||||
"Unexpected error message: " + errorMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRunCommandWithOutputHandling_NullByte() {
|
void testValidateCommand_nullArgument() {
|
||||||
// Test that null bytes are blocked
|
|
||||||
List<String> command = new ArrayList<>();
|
List<String> command = new ArrayList<>();
|
||||||
command.add("test\0command");
|
command.add("echo");
|
||||||
|
command.add(null);
|
||||||
|
assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> invokeValidateCommand(getExecutor(), command));
|
||||||
|
}
|
||||||
|
|
||||||
// Execute the command and expect an IllegalArgumentException
|
@Test
|
||||||
IllegalArgumentException thrown =
|
void testValidateCommand_nullByteInArgument() {
|
||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> processExecutor.runCommandWithOutputHandling(command));
|
() -> invokeValidateCommand(getExecutor(), List.of("echo", "bad\0arg")));
|
||||||
|
}
|
||||||
|
|
||||||
// Check the exception message
|
@Test
|
||||||
String errorMessage = thrown.getMessage();
|
void testValidateCommand_newlineInArgument() {
|
||||||
assertTrue(
|
assertThrows(
|
||||||
errorMessage.contains("invalid characters"),
|
IllegalArgumentException.class,
|
||||||
"Unexpected error message: " + errorMessage);
|
() -> invokeValidateCommand(getExecutor(), List.of("echo", "bad\narg")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateCommand_carriageReturnInArgument() {
|
||||||
|
assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> invokeValidateCommand(getExecutor(), List.of("echo", "bad\rarg")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateCommand_pathTraversal() {
|
||||||
|
assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> invokeValidateCommand(getExecutor(), List.of("../../bin/evil")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateCommand_blankExecutable() {
|
||||||
|
assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> invokeValidateCommand(getExecutor(), List.of(" ")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateCommand_validSimpleCommand() throws Exception {
|
||||||
|
// Simple command names (no path) should pass validation
|
||||||
|
invokeValidateCommand(getExecutor(), List.of("echo", "hello"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetInstance_returnsSameInstance() {
|
||||||
|
ProcessExecutor e1 = ProcessExecutor.getInstance(ProcessExecutor.Processes.QPDF);
|
||||||
|
ProcessExecutor e2 = ProcessExecutor.getInstance(ProcessExecutor.Processes.QPDF);
|
||||||
|
assertSame(e1, e2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetInstance_differentProcessTypes() {
|
||||||
|
ProcessExecutor e1 = ProcessExecutor.getInstance(ProcessExecutor.Processes.QPDF);
|
||||||
|
ProcessExecutor e2 = ProcessExecutor.getInstance(ProcessExecutor.Processes.TESSERACT);
|
||||||
|
assertNotSame(e1, e2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testProcessExecutorResult() {
|
||||||
|
ProcessExecutor executor = getExecutor();
|
||||||
|
ProcessExecutor.ProcessExecutorResult result =
|
||||||
|
executor.new ProcessExecutorResult(0, "success");
|
||||||
|
assertEquals(0, result.getRc());
|
||||||
|
assertEquals("success", result.getMessages());
|
||||||
|
|
||||||
|
result.setRc(1);
|
||||||
|
result.setMessages("error");
|
||||||
|
assertEquals(1, result.getRc());
|
||||||
|
assertEquals("error", result.getMessages());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,70 +1,82 @@
|
|||||||
package stirling.software.common.util;
|
package stirling.software.common.util;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class PropertyConfigsTest {
|
class PropertyConfigsTest {
|
||||||
|
|
||||||
@Test
|
private static final String TEST_KEY = "stirling.test.property.key";
|
||||||
public void testGetBooleanValue_WithKeys() {
|
private static final String TEST_KEY_2 = "stirling.test.property.key2";
|
||||||
// Define keys and default value
|
|
||||||
List<String> keys = Arrays.asList("test.key1", "test.key2", "test.key3");
|
|
||||||
boolean defaultValue = false;
|
|
||||||
|
|
||||||
// Set property for one of the keys
|
@AfterEach
|
||||||
System.setProperty("test.key2", "true");
|
void tearDown() {
|
||||||
|
System.clearProperty(TEST_KEY);
|
||||||
// Call the method under test
|
System.clearProperty(TEST_KEY_2);
|
||||||
boolean result = PropertyConfigs.getBooleanValue(keys, defaultValue);
|
|
||||||
|
|
||||||
// Verify the result
|
|
||||||
assertTrue(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetStringValue_WithKeys() {
|
void testGetBooleanValue_singleKey_fromSystemProperty() {
|
||||||
// Define keys and default value
|
System.setProperty(TEST_KEY, "true");
|
||||||
List<String> keys = Arrays.asList("test.key1", "test.key2", "test.key3");
|
assertTrue(PropertyConfigs.getBooleanValue(TEST_KEY, false));
|
||||||
String defaultValue = "default";
|
|
||||||
|
|
||||||
// Set property for one of the keys
|
|
||||||
System.setProperty("test.key2", "value");
|
|
||||||
|
|
||||||
// Call the method under test
|
|
||||||
String result = PropertyConfigs.getStringValue(keys, defaultValue);
|
|
||||||
|
|
||||||
// Verify the result
|
|
||||||
assertEquals("value", result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetBooleanValue_WithKey() {
|
void testGetBooleanValue_singleKey_defaultWhenMissing() {
|
||||||
// Define key and default value
|
assertFalse(PropertyConfigs.getBooleanValue(TEST_KEY, false));
|
||||||
String key = "test.key";
|
assertTrue(PropertyConfigs.getBooleanValue(TEST_KEY, true));
|
||||||
boolean defaultValue = true;
|
|
||||||
|
|
||||||
// Call the method under test
|
|
||||||
boolean result = PropertyConfigs.getBooleanValue(key, defaultValue);
|
|
||||||
|
|
||||||
// Verify the result
|
|
||||||
assertTrue(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetStringValue_WithKey() {
|
void testGetBooleanValue_singleKey_falseValue() {
|
||||||
// Define key and default value
|
System.setProperty(TEST_KEY, "false");
|
||||||
String key = "test.key";
|
assertFalse(PropertyConfigs.getBooleanValue(TEST_KEY, true));
|
||||||
String defaultValue = "default";
|
}
|
||||||
|
|
||||||
// Call the method under test
|
@Test
|
||||||
String result = PropertyConfigs.getStringValue(key, defaultValue);
|
void testGetStringValue_singleKey_fromSystemProperty() {
|
||||||
|
System.setProperty(TEST_KEY, "hello");
|
||||||
|
assertEquals("hello", PropertyConfigs.getStringValue(TEST_KEY, "default"));
|
||||||
|
}
|
||||||
|
|
||||||
// Verify the result
|
@Test
|
||||||
assertEquals("default", result);
|
void testGetStringValue_singleKey_defaultWhenMissing() {
|
||||||
|
assertEquals("default", PropertyConfigs.getStringValue(TEST_KEY, "default"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetBooleanValue_listKeys_firstMatch() {
|
||||||
|
System.setProperty(TEST_KEY_2, "true");
|
||||||
|
assertTrue(PropertyConfigs.getBooleanValue(List.of(TEST_KEY, TEST_KEY_2), false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetBooleanValue_listKeys_defaultWhenNoneMatch() {
|
||||||
|
assertFalse(PropertyConfigs.getBooleanValue(List.of(TEST_KEY, TEST_KEY_2), false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetStringValue_listKeys_firstMatch() {
|
||||||
|
System.setProperty(TEST_KEY, "first");
|
||||||
|
System.setProperty(TEST_KEY_2, "second");
|
||||||
|
assertEquals(
|
||||||
|
"first", PropertyConfigs.getStringValue(List.of(TEST_KEY, TEST_KEY_2), "default"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetStringValue_listKeys_defaultWhenNoneMatch() {
|
||||||
|
assertEquals(
|
||||||
|
"default",
|
||||||
|
PropertyConfigs.getStringValue(List.of(TEST_KEY, TEST_KEY_2), "default"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetBooleanValue_nonBooleanString() {
|
||||||
|
System.setProperty(TEST_KEY, "notaboolean");
|
||||||
|
// Boolean.valueOf returns false for non-boolean strings
|
||||||
|
assertFalse(PropertyConfigs.getBooleanValue(TEST_KEY, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+96
@@ -0,0 +1,96 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import stirling.software.common.model.oauth2.Provider;
|
||||||
|
|
||||||
|
class ProviderUtilsAdditionalTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_null() {
|
||||||
|
assertFalse(ProviderUtils.validateProvider(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_nullClientId() {
|
||||||
|
Provider provider = new Provider();
|
||||||
|
provider.setClientId(null);
|
||||||
|
provider.setClientSecret("secret");
|
||||||
|
provider.setScopes("read");
|
||||||
|
assertFalse(ProviderUtils.validateProvider(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_emptyClientId() {
|
||||||
|
Provider provider = new Provider();
|
||||||
|
provider.setClientId("");
|
||||||
|
provider.setClientSecret("secret");
|
||||||
|
provider.setScopes("read");
|
||||||
|
assertFalse(ProviderUtils.validateProvider(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_blankClientId() {
|
||||||
|
Provider provider = new Provider();
|
||||||
|
provider.setClientId(" ");
|
||||||
|
provider.setClientSecret("secret");
|
||||||
|
provider.setScopes("read");
|
||||||
|
assertFalse(ProviderUtils.validateProvider(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_nullClientSecret() {
|
||||||
|
Provider provider = new Provider();
|
||||||
|
provider.setClientId("id");
|
||||||
|
provider.setClientSecret(null);
|
||||||
|
provider.setScopes("read");
|
||||||
|
assertFalse(ProviderUtils.validateProvider(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_emptyClientSecret() {
|
||||||
|
Provider provider = new Provider();
|
||||||
|
provider.setClientId("id");
|
||||||
|
provider.setClientSecret("");
|
||||||
|
provider.setScopes("read");
|
||||||
|
assertFalse(ProviderUtils.validateProvider(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_nullScopes() {
|
||||||
|
Provider provider = new Provider();
|
||||||
|
provider.setClientId("id");
|
||||||
|
provider.setClientSecret("secret");
|
||||||
|
provider.setScopes(null);
|
||||||
|
assertFalse(ProviderUtils.validateProvider(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_emptyScopes() {
|
||||||
|
Provider provider = new Provider();
|
||||||
|
provider.setClientId("id");
|
||||||
|
provider.setClientSecret("secret");
|
||||||
|
provider.setScopes("");
|
||||||
|
assertFalse(ProviderUtils.validateProvider(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_allFieldsValid() {
|
||||||
|
Provider provider = new Provider();
|
||||||
|
provider.setClientId("my-client-id");
|
||||||
|
provider.setClientSecret("my-secret");
|
||||||
|
provider.setScopes("openid,profile");
|
||||||
|
assertTrue(ProviderUtils.validateProvider(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateProvider_singleScope() {
|
||||||
|
Provider provider = new Provider();
|
||||||
|
provider.setClientId("id");
|
||||||
|
provider.setClientSecret("secret");
|
||||||
|
provider.setScopes("email");
|
||||||
|
assertTrue(ProviderUtils.validateProvider(provider));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,338 +3,162 @@ package stirling.software.common.util;
|
|||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
|
||||||
import org.junit.jupiter.params.provider.ValueSource;
|
|
||||||
|
|
||||||
public class RequestUriUtilsTest {
|
class RequestUriUtilsTest {
|
||||||
|
|
||||||
|
// --- isStaticResource tests ---
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsStaticResource() {
|
void testIsStaticResource_nullUri() {
|
||||||
// Test static resources without context path
|
assertFalse(RequestUriUtils.isStaticResource(null));
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/css/styles.css"), "CSS files should be static");
|
|
||||||
assertTrue(RequestUriUtils.isStaticResource("/js/script.js"), "JS files should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/images/logo.png"),
|
|
||||||
"Image files should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/public/index.html"),
|
|
||||||
"Public files should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/pdfjs/pdf.worker.js"),
|
|
||||||
"PDF.js files should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/pdfium/pdfium.wasm"),
|
|
||||||
"PDFium wasm should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/api/v1/info/status"),
|
|
||||||
"API status should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/some-path/icon.svg"),
|
|
||||||
"SVG files should be static");
|
|
||||||
assertTrue(RequestUriUtils.isStaticResource("/login"), "Login page should be static");
|
|
||||||
assertTrue(RequestUriUtils.isStaticResource("/error"), "Error page should be static");
|
|
||||||
|
|
||||||
// Test non-static resources
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isStaticResource("/api/v1/users"),
|
|
||||||
"API users should not be static");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isStaticResource("/api/v1/orders"),
|
|
||||||
"API orders should not be static");
|
|
||||||
assertFalse(RequestUriUtils.isStaticResource("/"), "Root path should not be static");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isStaticResource("/register"),
|
|
||||||
"Register page should not be static");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isStaticResource("/api/v1/products"),
|
|
||||||
"API products should not be static");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsFrontendRoute() {
|
void testIsStaticResource_cssDirectory() {
|
||||||
assertTrue(
|
assertTrue(RequestUriUtils.isStaticResource("/css/style.css"));
|
||||||
RequestUriUtils.isFrontendRoute("", "/"), "Root path should be a frontend route");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isFrontendRoute("", "/app/dashboard"),
|
|
||||||
"React routes without extensions should be frontend routes");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isFrontendRoute("", "/api/v1/users"),
|
|
||||||
"API routes should not be frontend routes");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isFrontendRoute("", "/register"),
|
|
||||||
"Register should not be treated as a frontend route");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isFrontendRoute("", "/pipeline/jobs"),
|
|
||||||
"Pipeline should not be treated as a frontend route");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isFrontendRoute("", "/files/download"),
|
|
||||||
"Files path should not be treated as a frontend route");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsStaticResourceWithContextPath() {
|
void testIsStaticResource_jsDirectory() {
|
||||||
String contextPath = "/myapp";
|
assertTrue(RequestUriUtils.isStaticResource("/js/app.js"));
|
||||||
|
|
||||||
// Test static resources with context path
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource(contextPath, contextPath + "/css/styles.css"),
|
|
||||||
"CSS with context path should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource(contextPath, contextPath + "/js/script.js"),
|
|
||||||
"JS with context path should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource(contextPath, contextPath + "/images/logo.png"),
|
|
||||||
"Images with context path should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource(contextPath, contextPath + "/login"),
|
|
||||||
"Login with context path should be static");
|
|
||||||
|
|
||||||
// Test non-static resources with context path
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isStaticResource(contextPath, contextPath + "/api/v1/users"),
|
|
||||||
"API users with context path should not be static");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isStaticResource(contextPath, "/"),
|
|
||||||
"Root path with context path should not be static");
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@ValueSource(
|
|
||||||
strings = {
|
|
||||||
"robots.txt",
|
|
||||||
"/favicon.ico",
|
|
||||||
"/icon.svg",
|
|
||||||
"/image.png",
|
|
||||||
"/locales/en/translation.toml",
|
|
||||||
"/site.webmanifest",
|
|
||||||
"/app/logo.svg",
|
|
||||||
"/downloads/document.png",
|
|
||||||
"/assets/brand.ico",
|
|
||||||
"/any/path/with/image.svg",
|
|
||||||
"/deep/nested/folder/icon.png",
|
|
||||||
"/pdfium/pdfium.wasm"
|
|
||||||
})
|
|
||||||
void testIsStaticResourceWithFileExtensions(String path) {
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource(path),
|
|
||||||
"Files with specific extensions should be static regardless of path");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsTrackableResource() {
|
void testIsStaticResource_imagesDirectory() {
|
||||||
// Test non-trackable resources (returns false)
|
assertTrue(RequestUriUtils.isStaticResource("/images/logo.png"));
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/js/script.js"),
|
|
||||||
"JS files should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/v1/api-docs"),
|
|
||||||
"API docs should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("robots.txt"),
|
|
||||||
"robots.txt should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/images/logo.png"),
|
|
||||||
"Images should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/styles.css"),
|
|
||||||
"CSS files should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/script.js.map"),
|
|
||||||
"Map files should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/icon.svg"),
|
|
||||||
"SVG files should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/popularity.txt"),
|
|
||||||
"Popularity file should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/script.js"),
|
|
||||||
"JS files should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/pdfium/pdfium.wasm"),
|
|
||||||
"PDFium wasm should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/swagger/index.html"),
|
|
||||||
"Swagger files should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/api/v1/info/status"),
|
|
||||||
"API info should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/site.webmanifest"),
|
|
||||||
"Webmanifest should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/fonts/font.woff"),
|
|
||||||
"Fonts should not be trackable");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/pdfjs/viewer.js"),
|
|
||||||
"PDF.js files should not be trackable");
|
|
||||||
|
|
||||||
// Test trackable resources (returns true)
|
|
||||||
assertTrue(RequestUriUtils.isTrackableResource("/login"), "Login page should be trackable");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isTrackableResource("/register"),
|
|
||||||
"Register page should be trackable");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isTrackableResource("/api/v1/users"),
|
|
||||||
"API users should be trackable");
|
|
||||||
assertTrue(RequestUriUtils.isTrackableResource("/"), "Root path should be trackable");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isTrackableResource("/some-other-path"),
|
|
||||||
"Other paths should be trackable");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsTrackableResourceWithContextPath() {
|
void testIsStaticResource_robotsTxt() {
|
||||||
String contextPath = "/myapp";
|
assertTrue(RequestUriUtils.isStaticResource("/robots.txt"));
|
||||||
|
|
||||||
// Test with context path
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource(contextPath, "/js/script.js"),
|
|
||||||
"JS files should not be trackable with context path");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isTrackableResource(contextPath, "/login"),
|
|
||||||
"Login page should be trackable with context path");
|
|
||||||
|
|
||||||
// Additional tests with context path
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource(contextPath, "/fonts/custom.woff"),
|
|
||||||
"Font files should not be trackable with context path");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource(contextPath, "/images/header.png"),
|
|
||||||
"Images should not be trackable with context path");
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource(contextPath, "/swagger/ui.html"),
|
|
||||||
"Swagger UI should not be trackable with context path");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isTrackableResource(contextPath, "/account/profile"),
|
|
||||||
"Account page should be trackable with context path");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isTrackableResource(contextPath, "/pdf/view"),
|
|
||||||
"PDF view page should be trackable with context path");
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@ValueSource(
|
|
||||||
strings = {
|
|
||||||
"/js/util.js",
|
|
||||||
"/v1/api-docs/swagger.json",
|
|
||||||
"/robots.txt",
|
|
||||||
"/images/header/logo.png",
|
|
||||||
"/styles/theme.css",
|
|
||||||
"/build/app.js.map",
|
|
||||||
"/assets/icon.svg",
|
|
||||||
"/data/popularity.txt",
|
|
||||||
"/bundle.js",
|
|
||||||
"/api/swagger-ui.html",
|
|
||||||
"/api/v1/info/health",
|
|
||||||
"/site.webmanifest",
|
|
||||||
"/fonts/roboto.woff",
|
|
||||||
"/pdfjs/viewer.js",
|
|
||||||
"/pdfium/pdfium.wasm"
|
|
||||||
})
|
|
||||||
void testNonTrackableResources(String path) {
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource(path),
|
|
||||||
"Resources matching patterns should not be trackable: " + path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@ValueSource(
|
|
||||||
strings = {
|
|
||||||
"/",
|
|
||||||
"/home",
|
|
||||||
"/login",
|
|
||||||
"/register",
|
|
||||||
"/pdf/merge",
|
|
||||||
"/pdf/split",
|
|
||||||
"/api/v1/users/1",
|
|
||||||
"/api/v1/documents/process",
|
|
||||||
"/settings",
|
|
||||||
"/account/profile",
|
|
||||||
"/dashboard",
|
|
||||||
"/help",
|
|
||||||
"/about"
|
|
||||||
})
|
|
||||||
void testTrackableResources(String path) {
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isTrackableResource(path),
|
|
||||||
"App routes should be trackable: " + path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEdgeCases() {
|
void testIsStaticResource_faviconIco() {
|
||||||
// Test with empty strings
|
assertTrue(RequestUriUtils.isStaticResource("/favicon.ico"));
|
||||||
assertFalse(RequestUriUtils.isStaticResource("", ""), "Empty path should not be static");
|
|
||||||
assertTrue(RequestUriUtils.isTrackableResource("", ""), "Empty path should be trackable");
|
|
||||||
|
|
||||||
// Test with null-like behavior (would actually throw NPE in real code)
|
|
||||||
// These are not actual null tests but shows handling of odd cases
|
|
||||||
assertFalse(RequestUriUtils.isStaticResource("null"), "String 'null' should not be static");
|
|
||||||
|
|
||||||
// Test String "null" as a path
|
|
||||||
boolean isTrackable = RequestUriUtils.isTrackableResource("null");
|
|
||||||
assertTrue(isTrackable, "String 'null' should be trackable");
|
|
||||||
|
|
||||||
// Mixed case extensions test - note that Java's endsWith() is case-sensitive
|
|
||||||
// We'll check actual behavior and document it rather than asserting
|
|
||||||
|
|
||||||
// Always test the lowercase versions which should definitely work
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/logo.png"), "PNG (lowercase) should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/icon.svg"), "SVG (lowercase) should be static");
|
|
||||||
|
|
||||||
// Path with query parameters
|
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isStaticResource("/api/users?page=1"),
|
|
||||||
"Path with query params should respect base path");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/images/logo.png?v=123"),
|
|
||||||
"Static resource with query params should still be static");
|
|
||||||
|
|
||||||
// Paths with fragments
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/css/styles.css#section1"),
|
|
||||||
"CSS with fragment should be static");
|
|
||||||
|
|
||||||
// Multiple dots in filename
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/js/jquery.min.js"),
|
|
||||||
"JS with multiple dots should be static");
|
|
||||||
|
|
||||||
// Special characters in path
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/images/user's-photo.png"),
|
|
||||||
"Path with special chars should be handled correctly");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testComplexPaths() {
|
void testIsStaticResource_loginPath() {
|
||||||
// Test complex static resource paths
|
assertTrue(RequestUriUtils.isStaticResource("/login"));
|
||||||
assertTrue(
|
}
|
||||||
RequestUriUtils.isStaticResource("/css/theme/dark/styles.css"),
|
|
||||||
"Nested CSS should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/fonts/open-sans/bold/font.woff"),
|
|
||||||
"Nested font should be static");
|
|
||||||
assertTrue(
|
|
||||||
RequestUriUtils.isStaticResource("/js/vendor/jquery/3.5.1/jquery.min.js"),
|
|
||||||
"Versioned JS should be static");
|
|
||||||
|
|
||||||
// Test complex paths with context
|
@Test
|
||||||
String contextPath = "/app";
|
void testIsStaticResource_errorPath() {
|
||||||
assertTrue(
|
assertTrue(RequestUriUtils.isStaticResource("/error"));
|
||||||
RequestUriUtils.isStaticResource(
|
}
|
||||||
contextPath, contextPath + "/css/theme/dark/styles.css"),
|
|
||||||
"Nested CSS with context should be static");
|
|
||||||
|
|
||||||
// Test boundary cases for isTrackableResource
|
@Test
|
||||||
assertFalse(
|
void testIsStaticResource_svgExtension() {
|
||||||
RequestUriUtils.isTrackableResource("/js-framework/components"),
|
assertTrue(RequestUriUtils.isStaticResource("/some/path/icon.svg"));
|
||||||
"Path starting with js- should not be treated as JS resource");
|
}
|
||||||
assertFalse(
|
|
||||||
RequestUriUtils.isTrackableResource("/fonts-selection"),
|
@Test
|
||||||
"Path starting with fonts- should not be treated as font resource");
|
void testIsStaticResource_apiRoute_notStatic() {
|
||||||
|
assertFalse(RequestUriUtils.isStaticResource("/api/v1/convert"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsStaticResource_apiStatusEndpoint() {
|
||||||
|
assertTrue(RequestUriUtils.isStaticResource("/api/v1/info/status"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsStaticResource_withContextPath() {
|
||||||
|
assertTrue(RequestUriUtils.isStaticResource("/app", "/app/css/style.css"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsStaticResource_mobileScannerPath() {
|
||||||
|
assertTrue(RequestUriUtils.isStaticResource("/mobile-scanner"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- isFrontendRoute tests ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsFrontendRoute_nullUri() {
|
||||||
|
assertFalse(RequestUriUtils.isFrontendRoute("", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsFrontendRoute_apiPath() {
|
||||||
|
assertFalse(RequestUriUtils.isFrontendRoute("", "/api/v1/convert"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsFrontendRoute_backendOnlyPath() {
|
||||||
|
assertFalse(RequestUriUtils.isFrontendRoute("", "/swagger"));
|
||||||
|
assertFalse(RequestUriUtils.isFrontendRoute("", "/register"));
|
||||||
|
assertFalse(RequestUriUtils.isFrontendRoute("", "/actuator"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsFrontendRoute_extensionlessPath() {
|
||||||
|
assertTrue(RequestUriUtils.isFrontendRoute("", "/merge"));
|
||||||
|
assertTrue(RequestUriUtils.isFrontendRoute("", "/split-pdf"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsFrontendRoute_pathWithExtension() {
|
||||||
|
assertFalse(RequestUriUtils.isFrontendRoute("", "/some/file.pdf"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsFrontendRoute_blankPath() {
|
||||||
|
assertFalse(RequestUriUtils.isFrontendRoute("", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- isTrackableResource tests ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsTrackableResource_jsPath() {
|
||||||
|
assertFalse(RequestUriUtils.isTrackableResource("/js/app.js"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsTrackableResource_cssFile() {
|
||||||
|
assertFalse(RequestUriUtils.isTrackableResource("/some/file.css"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsTrackableResource_apiPage() {
|
||||||
|
assertTrue(RequestUriUtils.isTrackableResource("/api/v1/convert"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsTrackableResource_swaggerPath() {
|
||||||
|
assertFalse(RequestUriUtils.isTrackableResource("/swagger-ui/index.html"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsTrackableResource_infoApi() {
|
||||||
|
assertFalse(RequestUriUtils.isTrackableResource("/api/v1/info/status"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- isPublicAuthEndpoint tests ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsPublicAuthEndpoint_loginPath() {
|
||||||
|
assertTrue(RequestUriUtils.isPublicAuthEndpoint("/login", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsPublicAuthEndpoint_oauthPath() {
|
||||||
|
assertTrue(RequestUriUtils.isPublicAuthEndpoint("/oauth2/authorization/google", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsPublicAuthEndpoint_healthEndpoint() {
|
||||||
|
assertTrue(RequestUriUtils.isPublicAuthEndpoint("/actuator/health", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsPublicAuthEndpoint_regularApiNotPublic() {
|
||||||
|
assertFalse(RequestUriUtils.isPublicAuthEndpoint("/api/v1/convert", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsPublicAuthEndpoint_withContextPath() {
|
||||||
|
assertTrue(RequestUriUtils.isPublicAuthEndpoint("/app/login", "/app"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
|
import stirling.software.common.service.SsrfProtectionService;
|
||||||
|
|
||||||
|
class SvgSanitizerTest {
|
||||||
|
|
||||||
|
private SvgSanitizer sanitizer;
|
||||||
|
private ApplicationProperties applicationProperties;
|
||||||
|
private SsrfProtectionService ssrfProtectionService;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
applicationProperties = new ApplicationProperties();
|
||||||
|
ssrfProtectionService = mock(SsrfProtectionService.class);
|
||||||
|
sanitizer = new SvgSanitizer(ssrfProtectionService, applicationProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitize_validSvg() throws IOException {
|
||||||
|
String svg = "<svg xmlns=\"http://www.w3.org/2000/svg\"><circle r=\"10\"/></svg>";
|
||||||
|
byte[] result = sanitizer.sanitize(svg.getBytes(StandardCharsets.UTF_8));
|
||||||
|
assertNotNull(result);
|
||||||
|
assertTrue(result.length > 0);
|
||||||
|
String output = new String(result, StandardCharsets.UTF_8);
|
||||||
|
assertTrue(output.contains("circle"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitize_removesScriptElement() throws IOException {
|
||||||
|
String svg =
|
||||||
|
"<svg xmlns=\"http://www.w3.org/2000/svg\"><script>alert('xss')</script><circle r=\"10\"/></svg>";
|
||||||
|
byte[] result = sanitizer.sanitize(svg.getBytes(StandardCharsets.UTF_8));
|
||||||
|
String output = new String(result, StandardCharsets.UTF_8);
|
||||||
|
assertFalse(output.contains("script"));
|
||||||
|
assertTrue(output.contains("circle"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitize_removesEventHandler() throws IOException {
|
||||||
|
String svg =
|
||||||
|
"<svg xmlns=\"http://www.w3.org/2000/svg\"><circle r=\"10\" onclick=\"alert('xss')\"/></svg>";
|
||||||
|
byte[] result = sanitizer.sanitize(svg.getBytes(StandardCharsets.UTF_8));
|
||||||
|
String output = new String(result, StandardCharsets.UTF_8);
|
||||||
|
assertFalse(output.contains("onclick"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitize_removesJavascriptUrl() throws IOException {
|
||||||
|
String svg =
|
||||||
|
"<svg xmlns=\"http://www.w3.org/2000/svg\"><a href=\"javascript:alert('xss')\"><circle r=\"10\"/></a></svg>";
|
||||||
|
byte[] result = sanitizer.sanitize(svg.getBytes(StandardCharsets.UTF_8));
|
||||||
|
String output = new String(result, StandardCharsets.UTF_8);
|
||||||
|
assertFalse(output.contains("javascript"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitize_nullInput() {
|
||||||
|
assertThrows(IOException.class, () -> sanitizer.sanitize(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitize_emptyInput() {
|
||||||
|
assertThrows(IOException.class, () -> sanitizer.sanitize(new byte[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitize_disabledByConfig() throws IOException {
|
||||||
|
applicationProperties.getSystem().setDisableSanitize(true);
|
||||||
|
byte[] input =
|
||||||
|
"<svg xmlns=\"http://www.w3.org/2000/svg\"><script>evil</script></svg>"
|
||||||
|
.getBytes(StandardCharsets.UTF_8);
|
||||||
|
byte[] result = sanitizer.sanitize(input);
|
||||||
|
assertArrayEquals(input, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitize_removesForeignObject() throws IOException {
|
||||||
|
String svg =
|
||||||
|
"<svg xmlns=\"http://www.w3.org/2000/svg\"><foreignObject><body>evil</body></foreignObject><rect width=\"10\" height=\"10\"/></svg>";
|
||||||
|
byte[] result = sanitizer.sanitize(svg.getBytes(StandardCharsets.UTF_8));
|
||||||
|
String output = new String(result, StandardCharsets.UTF_8);
|
||||||
|
assertFalse(output.toLowerCase().contains("foreignobject"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSanitize_invalidXml() {
|
||||||
|
byte[] invalid = "not xml at all".getBytes(StandardCharsets.UTF_8);
|
||||||
|
assertThrows(IOException.class, () -> sanitizer.sanitize(invalid));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
|
|
||||||
|
class TempFileManagerTest {
|
||||||
|
|
||||||
|
private TempFileManager manager;
|
||||||
|
private TempFileRegistry registry;
|
||||||
|
private ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
|
@TempDir Path tempDir;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
registry = new TempFileRegistry();
|
||||||
|
applicationProperties = new ApplicationProperties();
|
||||||
|
applicationProperties.getSystem().getTempFileManagement().setBaseTmpDir(tempDir.toString());
|
||||||
|
applicationProperties.getSystem().getTempFileManagement().setPrefix("test-stirling-");
|
||||||
|
manager = new TempFileManager(registry, applicationProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateTempFile() throws IOException {
|
||||||
|
File file = manager.createTempFile(".pdf");
|
||||||
|
assertNotNull(file);
|
||||||
|
assertTrue(file.exists());
|
||||||
|
assertTrue(file.getName().endsWith(".pdf"));
|
||||||
|
assertTrue(registry.contains(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateManagedTempFile() throws IOException {
|
||||||
|
TempFile tempFile = manager.createManagedTempFile(".txt");
|
||||||
|
assertNotNull(tempFile);
|
||||||
|
assertTrue(tempFile.exists());
|
||||||
|
assertTrue(tempFile.getFile().getName().endsWith(".txt"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateTempDirectory() throws IOException {
|
||||||
|
Path dir = manager.createTempDirectory();
|
||||||
|
assertNotNull(dir);
|
||||||
|
assertTrue(Files.isDirectory(dir));
|
||||||
|
assertTrue(registry.getTempDirectories().contains(dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteTempFile_file() throws IOException {
|
||||||
|
File file = manager.createTempFile(".tmp");
|
||||||
|
assertTrue(file.exists());
|
||||||
|
|
||||||
|
boolean deleted = manager.deleteTempFile(file);
|
||||||
|
assertTrue(deleted);
|
||||||
|
assertFalse(file.exists());
|
||||||
|
assertFalse(registry.contains(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteTempFile_path() throws IOException {
|
||||||
|
File file = manager.createTempFile(".tmp");
|
||||||
|
Path path = file.toPath();
|
||||||
|
assertTrue(Files.exists(path));
|
||||||
|
|
||||||
|
boolean deleted = manager.deleteTempFile(path);
|
||||||
|
assertTrue(deleted);
|
||||||
|
assertFalse(Files.exists(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteTempFile_nullFile() {
|
||||||
|
assertFalse(manager.deleteTempFile((File) null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteTempFile_nullPath() {
|
||||||
|
assertFalse(manager.deleteTempFile((Path) null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteTempFile_nonExistentFile() {
|
||||||
|
File nonExistent = new File(tempDir.toFile(), "does-not-exist.tmp");
|
||||||
|
assertFalse(manager.deleteTempFile(nonExistent));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegister() throws IOException {
|
||||||
|
File file = Files.createTempFile(tempDir, "existing", ".tmp").toFile();
|
||||||
|
File result = manager.register(file);
|
||||||
|
assertSame(file, result);
|
||||||
|
assertTrue(registry.contains(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegister_nullFile() {
|
||||||
|
File result = manager.register(null);
|
||||||
|
assertNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGenerateTempFileName() {
|
||||||
|
String name = manager.generateTempFileName("convert", "pdf");
|
||||||
|
assertNotNull(name);
|
||||||
|
assertTrue(name.startsWith("test-stirling-"));
|
||||||
|
assertTrue(name.contains("convert"));
|
||||||
|
assertTrue(name.endsWith(".pdf"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetMaxAgeMillis() {
|
||||||
|
applicationProperties.getSystem().getTempFileManagement().setMaxAgeHours(2);
|
||||||
|
long millis = manager.getMaxAgeMillis();
|
||||||
|
assertEquals(2 * 60 * 60 * 1000L, millis);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCleanupOldTempFiles() throws IOException, InterruptedException {
|
||||||
|
File file = manager.createTempFile(".tmp");
|
||||||
|
assertTrue(file.exists());
|
||||||
|
|
||||||
|
Thread.sleep(50);
|
||||||
|
int deleted = manager.cleanupOldTempFiles(10);
|
||||||
|
assertTrue(deleted >= 1);
|
||||||
|
assertFalse(file.exists());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
class TempFileRegistryTest {
|
||||||
|
|
||||||
|
private TempFileRegistry registry;
|
||||||
|
|
||||||
|
@TempDir Path tempDir;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
registry = new TempFileRegistry();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegisterFile() throws IOException {
|
||||||
|
File file = Files.createTempFile(tempDir, "test", ".tmp").toFile();
|
||||||
|
File result = registry.register(file);
|
||||||
|
assertSame(file, result);
|
||||||
|
assertTrue(registry.contains(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegisterNull() {
|
||||||
|
registry.register((File) null);
|
||||||
|
assertEquals(0, registry.getAllRegisteredFiles().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegisterPath() throws IOException {
|
||||||
|
Path path = Files.createTempFile(tempDir, "test", ".tmp");
|
||||||
|
Path result = registry.register(path);
|
||||||
|
assertSame(path, result);
|
||||||
|
assertTrue(registry.getAllRegisteredFiles().contains(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUnregisterFile() throws IOException {
|
||||||
|
File file = Files.createTempFile(tempDir, "test", ".tmp").toFile();
|
||||||
|
registry.register(file);
|
||||||
|
assertTrue(registry.contains(file));
|
||||||
|
|
||||||
|
registry.unregister(file);
|
||||||
|
assertFalse(registry.contains(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUnregisterPath() throws IOException {
|
||||||
|
Path path = Files.createTempFile(tempDir, "test", ".tmp");
|
||||||
|
registry.register(path);
|
||||||
|
registry.unregister(path);
|
||||||
|
assertFalse(registry.getAllRegisteredFiles().contains(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUnregisterNull() {
|
||||||
|
// Should not throw
|
||||||
|
registry.unregister((File) null);
|
||||||
|
registry.unregister((Path) null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegisterDirectory() throws IOException {
|
||||||
|
Path dir = Files.createTempDirectory(tempDir, "testdir");
|
||||||
|
Path result = registry.registerDirectory(dir);
|
||||||
|
assertSame(dir, result);
|
||||||
|
assertTrue(registry.getTempDirectories().contains(dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegisterThirdParty() throws IOException {
|
||||||
|
File file = Files.createTempFile(tempDir, "third", ".tmp").toFile();
|
||||||
|
File result = registry.registerThirdParty(file);
|
||||||
|
assertSame(file, result);
|
||||||
|
assertTrue(registry.getThirdPartyTempFiles().contains(file.toPath()));
|
||||||
|
assertTrue(registry.contains(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testContainsNull() {
|
||||||
|
assertFalse(registry.contains(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetFilesOlderThan() throws IOException, InterruptedException {
|
||||||
|
Path path = Files.createTempFile(tempDir, "old", ".tmp");
|
||||||
|
registry.register(path);
|
||||||
|
|
||||||
|
// Files registered just now should not be "older than 0ms" since
|
||||||
|
// getFilesOlderThan uses isBefore(cutoff), meaning strictly before
|
||||||
|
Thread.sleep(50);
|
||||||
|
Set<Path> oldFiles = registry.getFilesOlderThan(10);
|
||||||
|
assertTrue(oldFiles.contains(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetFilesOlderThan_recentFiles() throws IOException {
|
||||||
|
Path path = Files.createTempFile(tempDir, "recent", ".tmp");
|
||||||
|
registry.register(path);
|
||||||
|
|
||||||
|
// With a very large maxAge, no files should be "old"
|
||||||
|
Set<Path> oldFiles = registry.getFilesOlderThan(999_999_999);
|
||||||
|
assertFalse(oldFiles.contains(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testClear() throws IOException {
|
||||||
|
File file = Files.createTempFile(tempDir, "clear", ".tmp").toFile();
|
||||||
|
Path dir = Files.createTempDirectory(tempDir, "cleardir");
|
||||||
|
registry.register(file);
|
||||||
|
registry.registerThirdParty(file);
|
||||||
|
registry.registerDirectory(dir);
|
||||||
|
|
||||||
|
registry.clear();
|
||||||
|
|
||||||
|
assertEquals(0, registry.getAllRegisteredFiles().size());
|
||||||
|
assertEquals(0, registry.getThirdPartyTempFiles().size());
|
||||||
|
assertEquals(0, registry.getTempDirectories().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
|
|
||||||
|
class TempFileTest {
|
||||||
|
|
||||||
|
private TempFileManager manager;
|
||||||
|
|
||||||
|
@TempDir Path tempDir;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
TempFileRegistry registry = new TempFileRegistry();
|
||||||
|
ApplicationProperties props = new ApplicationProperties();
|
||||||
|
props.getSystem().getTempFileManagement().setBaseTmpDir(tempDir.toString());
|
||||||
|
props.getSystem().getTempFileManagement().setPrefix("test-");
|
||||||
|
manager = new TempFileManager(registry, props);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTempFileCreation() throws IOException {
|
||||||
|
TempFile tempFile = new TempFile(manager, ".pdf");
|
||||||
|
assertNotNull(tempFile.getFile());
|
||||||
|
assertTrue(tempFile.exists());
|
||||||
|
assertTrue(tempFile.getFile().getName().endsWith(".pdf"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetPath() throws IOException {
|
||||||
|
TempFile tempFile = new TempFile(manager, ".txt");
|
||||||
|
Path path = tempFile.getPath();
|
||||||
|
assertNotNull(path);
|
||||||
|
assertEquals(tempFile.getFile().toPath(), path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetAbsolutePath() throws IOException {
|
||||||
|
TempFile tempFile = new TempFile(manager, ".tmp");
|
||||||
|
String absPath = tempFile.getAbsolutePath();
|
||||||
|
assertNotNull(absPath);
|
||||||
|
assertEquals(tempFile.getFile().getAbsolutePath(), absPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testClose_deletesFile() throws IOException {
|
||||||
|
TempFile tempFile = new TempFile(manager, ".tmp");
|
||||||
|
assertTrue(tempFile.exists());
|
||||||
|
|
||||||
|
tempFile.close();
|
||||||
|
assertFalse(tempFile.exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTryWithResources() throws IOException {
|
||||||
|
TempFile tempFileRef;
|
||||||
|
try (TempFile tempFile = new TempFile(manager, ".tmp")) {
|
||||||
|
tempFileRef = tempFile;
|
||||||
|
assertTrue(tempFile.exists());
|
||||||
|
}
|
||||||
|
assertFalse(tempFileRef.exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testToString() throws IOException {
|
||||||
|
TempFile tempFile = new TempFile(manager, ".tmp");
|
||||||
|
String str = tempFile.toString();
|
||||||
|
assertTrue(str.startsWith("TempFile{"));
|
||||||
|
assertTrue(str.endsWith("}"));
|
||||||
|
assertTrue(str.contains(tempFile.getFile().getAbsolutePath()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
|
|
||||||
|
class TempFileUtilTest {
|
||||||
|
|
||||||
|
private TempFileManager manager;
|
||||||
|
|
||||||
|
@TempDir Path tempDir;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
TempFileRegistry registry = new TempFileRegistry();
|
||||||
|
ApplicationProperties props = new ApplicationProperties();
|
||||||
|
props.getSystem().getTempFileManagement().setBaseTmpDir(tempDir.toString());
|
||||||
|
props.getSystem().getTempFileManagement().setPrefix("test-");
|
||||||
|
manager = new TempFileManager(registry, props);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testWithTempFile_executesAndCleansUp() throws IOException {
|
||||||
|
final File[] fileRef = new File[1];
|
||||||
|
String result =
|
||||||
|
TempFileUtil.withTempFile(
|
||||||
|
manager,
|
||||||
|
".tmp",
|
||||||
|
file -> {
|
||||||
|
fileRef[0] = file;
|
||||||
|
assertTrue(file.exists());
|
||||||
|
return "done";
|
||||||
|
});
|
||||||
|
assertEquals("done", result);
|
||||||
|
assertFalse(fileRef[0].exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testWithMultipleTempFiles() throws IOException {
|
||||||
|
final List<File>[] filesRef = new List[1];
|
||||||
|
String result =
|
||||||
|
TempFileUtil.withMultipleTempFiles(
|
||||||
|
manager,
|
||||||
|
3,
|
||||||
|
".tmp",
|
||||||
|
files -> {
|
||||||
|
filesRef[0] = files;
|
||||||
|
assertEquals(3, files.size());
|
||||||
|
for (File f : files) {
|
||||||
|
assertTrue(f.exists());
|
||||||
|
}
|
||||||
|
return "ok";
|
||||||
|
});
|
||||||
|
assertEquals("ok", result);
|
||||||
|
for (File f : filesRef[0]) {
|
||||||
|
assertFalse(f.exists());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSafeDeleteFiles() throws IOException {
|
||||||
|
Path file1 = Files.createTempFile(tempDir, "safe", ".tmp");
|
||||||
|
Path file2 = Files.createTempFile(tempDir, "safe", ".tmp");
|
||||||
|
assertTrue(Files.exists(file1));
|
||||||
|
assertTrue(Files.exists(file2));
|
||||||
|
|
||||||
|
TempFileUtil.safeDeleteFiles(Arrays.asList(file1, file2));
|
||||||
|
assertFalse(Files.exists(file1));
|
||||||
|
assertFalse(Files.exists(file2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSafeDeleteFiles_nullList() {
|
||||||
|
// Should not throw
|
||||||
|
TempFileUtil.safeDeleteFiles(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSafeDeleteFiles_nullElement() throws IOException {
|
||||||
|
Path file = Files.createTempFile(tempDir, "safe", ".tmp");
|
||||||
|
// Should handle null elements gracefully
|
||||||
|
TempFileUtil.safeDeleteFiles(Arrays.asList(null, file));
|
||||||
|
assertFalse(Files.exists(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegisterExistingTempFile() throws IOException {
|
||||||
|
File file = Files.createTempFile(tempDir, "existing", ".tmp").toFile();
|
||||||
|
File result = TempFileUtil.registerExistingTempFile(manager, file);
|
||||||
|
assertSame(file, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegisterExistingTempFile_nullManager() throws IOException {
|
||||||
|
File file = Files.createTempFile(tempDir, "existing", ".tmp").toFile();
|
||||||
|
File result = TempFileUtil.registerExistingTempFile(null, file);
|
||||||
|
assertSame(file, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRegisterExistingTempFile_nullFile() {
|
||||||
|
File result = TempFileUtil.registerExistingTempFile(manager, null);
|
||||||
|
assertNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTempFileCollection() throws IOException {
|
||||||
|
TempFileUtil.TempFileCollection collection = new TempFileUtil.TempFileCollection(manager);
|
||||||
|
File f1 = collection.addTempFile(".tmp");
|
||||||
|
File f2 = collection.addTempFile(".pdf");
|
||||||
|
|
||||||
|
assertTrue(f1.exists());
|
||||||
|
assertTrue(f2.exists());
|
||||||
|
assertEquals(2, collection.getFiles().size());
|
||||||
|
|
||||||
|
collection.close();
|
||||||
|
assertFalse(f1.exists());
|
||||||
|
assertFalse(f2.exists());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,276 +1,122 @@
|
|||||||
package stirling.software.common.util;
|
package stirling.software.common.util;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.ServerSocket;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
|
||||||
class UrlUtilsTest {
|
class UrlUtilsTest {
|
||||||
|
|
||||||
@Mock private HttpServletRequest request;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetOrigin() {
|
void testGetOrigin_standardRequest() {
|
||||||
// Arrange
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||||
when(request.getScheme()).thenReturn("http");
|
when(request.getScheme()).thenReturn("http");
|
||||||
when(request.getServerName()).thenReturn("localhost");
|
when(request.getServerName()).thenReturn("localhost");
|
||||||
when(request.getServerPort()).thenReturn(8080);
|
when(request.getServerPort()).thenReturn(8080);
|
||||||
when(request.getContextPath()).thenReturn("/myapp");
|
when(request.getContextPath()).thenReturn("");
|
||||||
|
|
||||||
// Act
|
assertEquals("http://localhost:8080", UrlUtils.getOrigin(request));
|
||||||
String origin = UrlUtils.getOrigin(request);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assertEquals(
|
|
||||||
"http://localhost:8080/myapp", origin, "Origin URL should be correctly formatted");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetOriginWithHttps() {
|
void testGetOrigin_httpsWithContextPath() {
|
||||||
// Arrange
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||||
when(request.getScheme()).thenReturn("https");
|
when(request.getScheme()).thenReturn("https");
|
||||||
when(request.getServerName()).thenReturn("example.com");
|
when(request.getServerName()).thenReturn("example.com");
|
||||||
when(request.getServerPort()).thenReturn(443);
|
when(request.getServerPort()).thenReturn(443);
|
||||||
when(request.getContextPath()).thenReturn("");
|
when(request.getContextPath()).thenReturn("/myapp");
|
||||||
|
|
||||||
// Act
|
assertEquals("https://example.com:443/myapp", UrlUtils.getOrigin(request));
|
||||||
String origin = UrlUtils.getOrigin(request);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assertEquals(
|
|
||||||
"https://example.com:443",
|
|
||||||
origin,
|
|
||||||
"HTTPS origin URL should be correctly formatted");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetOriginWithEmptyContextPath() {
|
void testIsPortAvailable_usedPort() {
|
||||||
// Arrange
|
// Port 0 is special - let the OS pick a port, but commonly used ports should be busy
|
||||||
|
// We test with a high port that might be available
|
||||||
|
// This is inherently environment-dependent
|
||||||
|
boolean result = UrlUtils.isPortAvailable(0);
|
||||||
|
// Port 0 should always be available as the OS assigns an ephemeral port
|
||||||
|
assertTrue(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFindAvailablePort_returnsPort() {
|
||||||
|
// Starting from port 0 should immediately find an available port
|
||||||
|
String port = UrlUtils.findAvailablePort(0);
|
||||||
|
assertNotNull(port);
|
||||||
|
int portNum = Integer.parseInt(port);
|
||||||
|
assertTrue(portNum >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetOrigin_customPort() {
|
||||||
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||||
when(request.getScheme()).thenReturn("http");
|
when(request.getScheme()).thenReturn("http");
|
||||||
when(request.getServerName()).thenReturn("localhost");
|
when(request.getServerName()).thenReturn("192.168.1.1");
|
||||||
when(request.getServerPort()).thenReturn(8080);
|
when(request.getServerPort()).thenReturn(9090);
|
||||||
when(request.getContextPath()).thenReturn("");
|
|
||||||
|
|
||||||
// Act
|
|
||||||
String origin = UrlUtils.getOrigin(request);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assertEquals(
|
|
||||||
"http://localhost:8080",
|
|
||||||
origin,
|
|
||||||
"Origin URL with empty context path should be correct");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testGetOriginWithSpecialCharacters() {
|
|
||||||
// Arrange - Test with server name containing special characters
|
|
||||||
when(request.getScheme()).thenReturn("https");
|
|
||||||
when(request.getServerName()).thenReturn("internal-server.example-domain.com");
|
|
||||||
when(request.getServerPort()).thenReturn(8443);
|
|
||||||
when(request.getContextPath()).thenReturn("/app-v1.2");
|
|
||||||
|
|
||||||
// Act
|
|
||||||
String origin = UrlUtils.getOrigin(request);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assertEquals(
|
|
||||||
"https://internal-server.example-domain.com:8443/app-v1.2",
|
|
||||||
origin,
|
|
||||||
"Origin URL with special characters should be correctly formatted");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testGetOriginWithIPv4Address() {
|
|
||||||
// Arrange
|
|
||||||
when(request.getScheme()).thenReturn("http");
|
|
||||||
when(request.getServerName()).thenReturn("192.168.1.100");
|
|
||||||
when(request.getServerPort()).thenReturn(8080);
|
|
||||||
when(request.getContextPath()).thenReturn("/app");
|
|
||||||
|
|
||||||
// Act
|
|
||||||
String origin = UrlUtils.getOrigin(request);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assertEquals(
|
|
||||||
"http://192.168.1.100:8080/app",
|
|
||||||
origin,
|
|
||||||
"Origin URL with IPv4 address should be correctly formatted");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testGetOriginWithNonStandardPort() {
|
|
||||||
// Arrange
|
|
||||||
when(request.getScheme()).thenReturn("https");
|
|
||||||
when(request.getServerName()).thenReturn("example.org");
|
|
||||||
when(request.getServerPort()).thenReturn(8443);
|
|
||||||
when(request.getContextPath()).thenReturn("/api");
|
when(request.getContextPath()).thenReturn("/api");
|
||||||
|
|
||||||
// Act
|
assertEquals("http://192.168.1.1:9090/api", UrlUtils.getOrigin(request));
|
||||||
String origin = UrlUtils.getOrigin(request);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assertEquals(
|
|
||||||
"https://example.org:8443/api",
|
|
||||||
origin,
|
|
||||||
"Origin URL with non-standard port should be correctly formatted");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsPortAvailable() {
|
void testGetOrigin_defaultPort80() {
|
||||||
// We'll use a real server socket for this test
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||||
ServerSocket socket = null;
|
when(request.getScheme()).thenReturn("http");
|
||||||
int port = 12345; // Choose a port unlikely to be in use
|
when(request.getServerName()).thenReturn("example.com");
|
||||||
|
when(request.getServerPort()).thenReturn(80);
|
||||||
|
when(request.getContextPath()).thenReturn("");
|
||||||
|
|
||||||
try {
|
assertEquals("http://example.com:80", UrlUtils.getOrigin(request));
|
||||||
// First check the port is available
|
|
||||||
boolean initialAvailability = UrlUtils.isPortAvailable(port);
|
|
||||||
|
|
||||||
// Then occupy the port
|
|
||||||
socket = new ServerSocket(port);
|
|
||||||
|
|
||||||
// Now check the port is no longer available
|
|
||||||
boolean afterSocketCreation = UrlUtils.isPortAvailable(port);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assertTrue(initialAvailability, "Port should be available initially");
|
|
||||||
assertFalse(
|
|
||||||
afterSocketCreation, "Port should not be available after socket is created");
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
// This might happen if the port is already in use by another process
|
|
||||||
// We'll just verify the behavior of isPortAvailable matches what we expect
|
|
||||||
assertFalse(
|
|
||||||
UrlUtils.isPortAvailable(port),
|
|
||||||
"Port should not be available if exception is thrown");
|
|
||||||
} finally {
|
|
||||||
if (socket != null && !socket.isClosed()) {
|
|
||||||
try {
|
|
||||||
socket.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Ignore cleanup exceptions
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFindAvailablePort() {
|
void testGetOrigin_emptyContextPath() {
|
||||||
// We'll create a socket on a port and ensure findAvailablePort returns a different port
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||||
ServerSocket socket = null;
|
when(request.getScheme()).thenReturn("https");
|
||||||
int startPort = 12346; // Choose a port unlikely to be in use
|
when(request.getServerName()).thenReturn("app.example.com");
|
||||||
|
when(request.getServerPort()).thenReturn(443);
|
||||||
|
when(request.getContextPath()).thenReturn("");
|
||||||
|
|
||||||
try {
|
assertEquals("https://app.example.com:443", UrlUtils.getOrigin(request));
|
||||||
// Occupy the start port
|
|
||||||
socket = new ServerSocket(startPort);
|
|
||||||
|
|
||||||
// Find an available port
|
|
||||||
String availablePort = UrlUtils.findAvailablePort(startPort);
|
|
||||||
|
|
||||||
// Assert the returned port is not the occupied one
|
|
||||||
assertNotEquals(
|
|
||||||
String.valueOf(startPort),
|
|
||||||
availablePort,
|
|
||||||
"findAvailablePort should not return an occupied port");
|
|
||||||
|
|
||||||
// Verify the returned port is actually available
|
|
||||||
int portNumber = Integer.parseInt(availablePort);
|
|
||||||
|
|
||||||
// Close our test socket before checking the found port
|
|
||||||
socket.close();
|
|
||||||
socket = null;
|
|
||||||
|
|
||||||
// The port should now be available
|
|
||||||
assertTrue(
|
|
||||||
UrlUtils.isPortAvailable(portNumber),
|
|
||||||
"The port returned by findAvailablePort should be available");
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
// If we can't create the socket, skip this assertion
|
|
||||||
} finally {
|
|
||||||
if (socket != null && !socket.isClosed()) {
|
|
||||||
try {
|
|
||||||
socket.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Ignore cleanup exceptions
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFindAvailablePortWithAvailableStartPort() {
|
void testGetOrigin_nestedContextPath() {
|
||||||
// Find an available port without occupying any
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||||
int startPort = 23456; // Choose a different unlikely-to-be-used port
|
when(request.getScheme()).thenReturn("http");
|
||||||
|
when(request.getServerName()).thenReturn("host");
|
||||||
|
when(request.getServerPort()).thenReturn(3000);
|
||||||
|
when(request.getContextPath()).thenReturn("/a/b/c");
|
||||||
|
|
||||||
// Make sure the port is available first
|
assertEquals("http://host:3000/a/b/c", UrlUtils.getOrigin(request));
|
||||||
if (UrlUtils.isPortAvailable(startPort)) {
|
|
||||||
// Find an available port
|
|
||||||
String availablePort = UrlUtils.findAvailablePort(startPort);
|
|
||||||
|
|
||||||
// Assert the returned port is the start port since it's available
|
|
||||||
assertEquals(
|
|
||||||
String.valueOf(startPort),
|
|
||||||
availablePort,
|
|
||||||
"findAvailablePort should return the start port if it's available");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFindAvailablePortWithSequentialUsedPorts() {
|
void testFindAvailablePort_returnsStringOfPort() {
|
||||||
// This test checks that findAvailablePort correctly skips multiple occupied ports
|
String port = UrlUtils.findAvailablePort(49152);
|
||||||
ServerSocket socket1 = null;
|
assertNotNull(port);
|
||||||
ServerSocket socket2 = null;
|
int portNum = Integer.parseInt(port);
|
||||||
int startPort = 34567; // Another unlikely-to-be-used port
|
assertTrue(portNum >= 49152);
|
||||||
|
|
||||||
try {
|
|
||||||
// First verify the port is available
|
|
||||||
if (!UrlUtils.isPortAvailable(startPort)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Occupy two sequential ports
|
|
||||||
socket1 = new ServerSocket(startPort);
|
|
||||||
socket2 = new ServerSocket(startPort + 1);
|
|
||||||
|
|
||||||
// Find an available port starting from our occupied range
|
|
||||||
String availablePort = UrlUtils.findAvailablePort(startPort);
|
|
||||||
int foundPort = Integer.parseInt(availablePort);
|
|
||||||
|
|
||||||
// Should have skipped the two occupied ports
|
|
||||||
assertTrue(
|
|
||||||
foundPort >= startPort + 2,
|
|
||||||
"findAvailablePort should skip sequential occupied ports");
|
|
||||||
|
|
||||||
// Verify the found port is actually available
|
|
||||||
try (ServerSocket testSocket = new ServerSocket(foundPort)) {
|
|
||||||
assertTrue(testSocket.isBound(), "The found port should be bindable");
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Skip test if we encounter IO exceptions
|
|
||||||
} finally {
|
|
||||||
// Clean up resources
|
|
||||||
try {
|
|
||||||
if (socket1 != null && !socket1.isClosed()) socket1.close();
|
|
||||||
if (socket2 != null && !socket2.isClosed()) socket2.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Ignore cleanup exceptions
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsPortAvailableWithPrivilegedPorts() {
|
void testIsPortAvailable_highPort() {
|
||||||
// Skip tests for privileged ports as they typically require root access
|
// Most high ephemeral ports should be available in test environments
|
||||||
// and results are environment-dependent
|
// Using port 0 which OS always considers available
|
||||||
|
assertTrue(UrlUtils.isPortAvailable(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetOrigin_ipv4Address() {
|
||||||
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||||
|
when(request.getScheme()).thenReturn("http");
|
||||||
|
when(request.getServerName()).thenReturn("10.0.0.1");
|
||||||
|
when(request.getServerPort()).thenReturn(8443);
|
||||||
|
when(request.getContextPath()).thenReturn("");
|
||||||
|
|
||||||
|
assertEquals("http://10.0.0.1:8443", UrlUtils.getOrigin(request));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class ValidationUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsStringEmpty_null() {
|
||||||
|
assertTrue(ValidationUtils.isStringEmpty(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsStringEmpty_emptyString() {
|
||||||
|
assertTrue(ValidationUtils.isStringEmpty(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsStringEmpty_blankString() {
|
||||||
|
assertTrue(ValidationUtils.isStringEmpty(" "));
|
||||||
|
assertTrue(ValidationUtils.isStringEmpty("\t\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsStringEmpty_nonEmptyString() {
|
||||||
|
assertFalse(ValidationUtils.isStringEmpty("hello"));
|
||||||
|
assertFalse(ValidationUtils.isStringEmpty(" a "));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsCollectionEmpty_null() {
|
||||||
|
assertTrue(ValidationUtils.isCollectionEmpty(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsCollectionEmpty_emptyCollection() {
|
||||||
|
assertTrue(ValidationUtils.isCollectionEmpty(Collections.emptyList()));
|
||||||
|
assertTrue(ValidationUtils.isCollectionEmpty(new ArrayList<>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsCollectionEmpty_nonEmptyCollection() {
|
||||||
|
assertFalse(ValidationUtils.isCollectionEmpty(List.of("a")));
|
||||||
|
assertFalse(ValidationUtils.isCollectionEmpty(List.of("a", "b", "c")));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,112 +4,91 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.mock.web.MockMultipartFile;
|
|
||||||
|
|
||||||
public class WebResponseUtilsTest {
|
class WebResponseUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBoasToWebResponse() {
|
void testBytesToWebResponse_defaultMediaType() throws IOException {
|
||||||
try {
|
byte[] data = "test content".getBytes(StandardCharsets.UTF_8);
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ResponseEntity<byte[]> response = WebResponseUtils.bytesToWebResponse(data, "output.pdf");
|
||||||
baos.write("Sample PDF content".getBytes());
|
|
||||||
String docName = "sample.pdf";
|
|
||||||
|
|
||||||
ResponseEntity<byte[]> responseEntity =
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
WebResponseUtils.baosToWebResponse(baos, docName);
|
assertEquals(MediaType.APPLICATION_PDF, response.getHeaders().getContentType());
|
||||||
|
assertEquals(data.length, response.getHeaders().getContentLength());
|
||||||
assertNotNull(responseEntity);
|
assertArrayEquals(data, response.getBody());
|
||||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
|
||||||
assertNotNull(responseEntity.getBody());
|
|
||||||
|
|
||||||
HttpHeaders headers = responseEntity.getHeaders();
|
|
||||||
assertNotNull(headers);
|
|
||||||
assertEquals(MediaType.APPLICATION_PDF, headers.getContentType());
|
|
||||||
assertNotNull(headers.getContentDisposition());
|
|
||||||
// assertEquals("attachment; filename=\"sample.pdf\"",
|
|
||||||
// headers.getContentDisposition().toString());
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
fail("Exception thrown: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultiPartFileToWebResponse() {
|
void testBytesToWebResponse_customMediaType() throws IOException {
|
||||||
try {
|
byte[] data = "zip data".getBytes(StandardCharsets.UTF_8);
|
||||||
byte[] fileContent = "Sample file content".getBytes();
|
ResponseEntity<byte[]> response =
|
||||||
MockMultipartFile file =
|
WebResponseUtils.bytesToWebResponse(
|
||||||
new MockMultipartFile(
|
data, "output.zip", MediaType.APPLICATION_OCTET_STREAM);
|
||||||
"file", "sample.txt", MediaType.TEXT_PLAIN_VALUE, fileContent);
|
|
||||||
|
|
||||||
ResponseEntity<byte[]> responseEntity =
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
WebResponseUtils.multiPartFileToWebResponse(file);
|
assertEquals(MediaType.APPLICATION_OCTET_STREAM, response.getHeaders().getContentType());
|
||||||
|
assertArrayEquals(data, response.getBody());
|
||||||
assertNotNull(responseEntity);
|
|
||||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
|
||||||
assertNotNull(responseEntity.getBody());
|
|
||||||
|
|
||||||
HttpHeaders headers = responseEntity.getHeaders();
|
|
||||||
assertNotNull(headers);
|
|
||||||
assertEquals(MediaType.TEXT_PLAIN, headers.getContentType());
|
|
||||||
assertNotNull(headers.getContentDisposition());
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
fail("Exception thrown: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBytesToWebResponse() {
|
void testBaosToWebResponse() throws IOException {
|
||||||
try {
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
byte[] bytes = "Sample bytes".getBytes();
|
baos.write("baos content".getBytes(StandardCharsets.UTF_8));
|
||||||
String docName = "sample.txt";
|
|
||||||
MediaType mediaType = MediaType.TEXT_PLAIN;
|
|
||||||
|
|
||||||
ResponseEntity<byte[]> responseEntity =
|
ResponseEntity<byte[]> response = WebResponseUtils.baosToWebResponse(baos, "doc.pdf");
|
||||||
WebResponseUtils.bytesToWebResponse(bytes, docName, mediaType);
|
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
assertNotNull(response.getBody());
|
||||||
assertNotNull(responseEntity.getBody());
|
assertEquals("baos content", new String(response.getBody(), StandardCharsets.UTF_8));
|
||||||
|
|
||||||
HttpHeaders headers = responseEntity.getHeaders();
|
|
||||||
assertNotNull(headers);
|
|
||||||
assertEquals(MediaType.TEXT_PLAIN, headers.getContentType());
|
|
||||||
assertNotNull(headers.getContentDisposition());
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
fail("Exception thrown: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPdfDocToWebResponse() {
|
void testBaosToWebResponse_withMediaType() throws IOException {
|
||||||
try (PDDocument document = new PDDocument()) {
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
document.addPage(new org.apache.pdfbox.pdmodel.PDPage());
|
baos.write("data".getBytes(StandardCharsets.UTF_8));
|
||||||
String docName = "sample.pdf";
|
|
||||||
|
|
||||||
ResponseEntity<byte[]> responseEntity =
|
ResponseEntity<byte[]> response =
|
||||||
WebResponseUtils.pdfDocToWebResponse(document, docName);
|
WebResponseUtils.baosToWebResponse(baos, "doc.html", MediaType.TEXT_HTML);
|
||||||
|
|
||||||
assertNotNull(responseEntity);
|
assertEquals(MediaType.TEXT_HTML, response.getHeaders().getContentType());
|
||||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
}
|
||||||
assertNotNull(responseEntity.getBody());
|
|
||||||
|
|
||||||
HttpHeaders headers = responseEntity.getHeaders();
|
@Test
|
||||||
assertNotNull(headers);
|
void testBytesToWebResponse_contentDispositionHeader() throws IOException {
|
||||||
assertEquals(MediaType.APPLICATION_PDF, headers.getContentType());
|
byte[] data = "test".getBytes(StandardCharsets.UTF_8);
|
||||||
assertNotNull(headers.getContentDisposition());
|
ResponseEntity<byte[]> response = WebResponseUtils.bytesToWebResponse(data, "my file.pdf");
|
||||||
|
|
||||||
} catch (IOException e) {
|
String contentDisposition = response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION);
|
||||||
fail("Exception thrown: " + e.getMessage());
|
assertNotNull(contentDisposition);
|
||||||
}
|
assertTrue(contentDisposition.contains("attachment"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBytesToWebResponse_specialCharsInFilename() throws IOException {
|
||||||
|
byte[] data = "test".getBytes(StandardCharsets.UTF_8);
|
||||||
|
// A space in the filename gets URL-encoded to '+' then replaced with '%20'
|
||||||
|
ResponseEntity<byte[]> response =
|
||||||
|
WebResponseUtils.bytesToWebResponse(data, "file name.pdf");
|
||||||
|
|
||||||
|
String contentDisposition = response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION);
|
||||||
|
assertNotNull(contentDisposition);
|
||||||
|
// The space in filename should be encoded as %20 (not +)
|
||||||
|
assertTrue(contentDisposition.contains("%20"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBytesToWebResponse_emptyBytes() throws IOException {
|
||||||
|
byte[] data = new byte[0];
|
||||||
|
ResponseEntity<byte[]> response = WebResponseUtils.bytesToWebResponse(data, "empty.pdf");
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertEquals(0, response.getHeaders().getContentLength());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,161 @@
|
|||||||
|
package stirling.software.common.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
import org.snakeyaml.engine.v2.api.LoadSettings;
|
||||||
|
|
||||||
|
class YamlHelperTest {
|
||||||
|
|
||||||
|
private static final String SIMPLE_YAML =
|
||||||
|
"server:\n port: 8080\n host: localhost\napp:\n name: test\n debug: true\n";
|
||||||
|
|
||||||
|
private static final LoadSettings LOAD_SETTINGS =
|
||||||
|
LoadSettings.builder()
|
||||||
|
.setUseMarks(true)
|
||||||
|
.setMaxAliasesForCollections(Integer.MAX_VALUE)
|
||||||
|
.setAllowRecursiveKeys(true)
|
||||||
|
.setParseComments(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
private YamlHelper createHelper(String yaml) {
|
||||||
|
return new YamlHelper(LOAD_SETTINGS, yaml);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetValueByExactKeyPath_scalarValue() {
|
||||||
|
YamlHelper helper = createHelper(SIMPLE_YAML);
|
||||||
|
Object value = helper.getValueByExactKeyPath("server", "port");
|
||||||
|
assertEquals("8080", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetValueByExactKeyPath_stringValue() {
|
||||||
|
YamlHelper helper = new YamlHelper(LOAD_SETTINGS, SIMPLE_YAML);
|
||||||
|
Object value = helper.getValueByExactKeyPath("server", "host");
|
||||||
|
assertEquals("localhost", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetValueByExactKeyPath_nonExistentKey() {
|
||||||
|
YamlHelper helper = new YamlHelper(LOAD_SETTINGS, SIMPLE_YAML);
|
||||||
|
Object value = helper.getValueByExactKeyPath("nonexistent", "key");
|
||||||
|
assertNull(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetAllKeys() {
|
||||||
|
YamlHelper helper = new YamlHelper(LOAD_SETTINGS, SIMPLE_YAML);
|
||||||
|
Set<String> keys = helper.getAllKeys();
|
||||||
|
assertTrue(keys.contains("server"));
|
||||||
|
assertTrue(keys.contains("server.port"));
|
||||||
|
assertTrue(keys.contains("server.host"));
|
||||||
|
assertTrue(keys.contains("app"));
|
||||||
|
assertTrue(keys.contains("app.name"));
|
||||||
|
assertTrue(keys.contains("app.debug"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateValue() {
|
||||||
|
YamlHelper helper = new YamlHelper(LOAD_SETTINGS, SIMPLE_YAML);
|
||||||
|
boolean updated = helper.updateValue(Arrays.asList("server", "port"), "9090");
|
||||||
|
assertTrue(updated);
|
||||||
|
Object newValue = helper.getValueByExactKeyPath("server", "port");
|
||||||
|
assertEquals("9090", newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateValue_nonExistentKey() {
|
||||||
|
YamlHelper helper = new YamlHelper(LOAD_SETTINGS, SIMPLE_YAML);
|
||||||
|
boolean updated = helper.updateValue(Arrays.asList("nonexistent", "key"), "value");
|
||||||
|
assertFalse(updated);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConvertNodeToYaml() {
|
||||||
|
YamlHelper helper = new YamlHelper(LOAD_SETTINGS, SIMPLE_YAML);
|
||||||
|
String yaml = helper.convertNodeToYaml(helper.getUpdatedRootNode());
|
||||||
|
assertNotNull(yaml);
|
||||||
|
assertTrue(yaml.contains("server"));
|
||||||
|
assertTrue(yaml.contains("port"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructorFromFile(@TempDir Path tempDir) throws IOException {
|
||||||
|
Path yamlFile = tempDir.resolve("test.yaml");
|
||||||
|
Files.writeString(yamlFile, SIMPLE_YAML);
|
||||||
|
|
||||||
|
YamlHelper helper = new YamlHelper(yamlFile);
|
||||||
|
Object value = helper.getValueByExactKeyPath("app", "name");
|
||||||
|
assertEquals("test", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSequenceValues() {
|
||||||
|
String yaml = "items:\n - alpha\n - beta\n - gamma\n";
|
||||||
|
YamlHelper helper = new YamlHelper(LOAD_SETTINGS, yaml);
|
||||||
|
Object value = helper.getValueByExactKeyPath("items");
|
||||||
|
assertInstanceOf(List.class, value);
|
||||||
|
List<?> list = (List<?>) value;
|
||||||
|
assertEquals(3, list.size());
|
||||||
|
assertEquals("alpha", list.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Static type check methods ---
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsInteger() {
|
||||||
|
assertTrue(YamlHelper.isInteger(42));
|
||||||
|
assertTrue(YamlHelper.isInteger("123"));
|
||||||
|
assertFalse(YamlHelper.isInteger("abc"));
|
||||||
|
assertFalse(YamlHelper.isInteger(3.14));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsFloat() {
|
||||||
|
assertTrue(YamlHelper.isFloat(3.14f));
|
||||||
|
assertTrue(YamlHelper.isFloat(3.14));
|
||||||
|
assertTrue(YamlHelper.isFloat("3.14"));
|
||||||
|
assertFalse(YamlHelper.isFloat("abc"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsLong() {
|
||||||
|
assertTrue(YamlHelper.isLong(42L));
|
||||||
|
assertTrue(YamlHelper.isLong("9999999999"));
|
||||||
|
assertFalse(YamlHelper.isLong("notALong"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsAnyInteger() {
|
||||||
|
assertTrue(YamlHelper.isAnyInteger(42));
|
||||||
|
assertTrue(YamlHelper.isAnyInteger((short) 5));
|
||||||
|
assertTrue(YamlHelper.isAnyInteger((byte) 1));
|
||||||
|
assertTrue(YamlHelper.isAnyInteger(100L));
|
||||||
|
assertFalse(YamlHelper.isAnyInteger("xyz"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSave_differentPath(@TempDir Path tempDir) throws IOException {
|
||||||
|
Path originalFile = tempDir.resolve("original.yaml");
|
||||||
|
Files.writeString(originalFile, SIMPLE_YAML);
|
||||||
|
|
||||||
|
YamlHelper helper = new YamlHelper(originalFile);
|
||||||
|
helper.updateValue(Arrays.asList("server", "port"), "9090");
|
||||||
|
|
||||||
|
Path savePath = tempDir.resolve("saved.yaml");
|
||||||
|
helper.save(savePath);
|
||||||
|
|
||||||
|
assertTrue(Files.exists(savePath));
|
||||||
|
String content = Files.readString(savePath);
|
||||||
|
assertTrue(content.contains("9090"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,6 +46,7 @@ dependencies {
|
|||||||
implementation project(':common')
|
implementation project(':common')
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-jetty'
|
implementation 'org.springframework.boot:spring-boot-starter-jetty'
|
||||||
implementation 'org.eclipse.jetty.http2:jetty-http2-server'
|
implementation 'org.eclipse.jetty.http2:jetty-http2-server'
|
||||||
|
implementation 'org.eclipse.jetty:jetty-alpn-java-server'
|
||||||
implementation ('org.telegram:telegrambots:6.9.7.1') {
|
implementation ('org.telegram:telegrambots:6.9.7.1') {
|
||||||
// Grizzly server + Jersey JAX-RS stack: only used for webhook mode;
|
// Grizzly server + Jersey JAX-RS stack: only used for webhook mode;
|
||||||
// Stirling-PDF uses long-polling mode so these are dead weight (~3 MB)
|
// Stirling-PDF uses long-polling mode so these are dead weight (~3 MB)
|
||||||
@@ -65,7 +66,7 @@ dependencies {
|
|||||||
implementation 'commons-io:commons-io:2.21.0'
|
implementation 'commons-io:commons-io:2.21.0'
|
||||||
implementation "org.bouncycastle:bcprov-jdk18on:$bouncycastleVersion"
|
implementation "org.bouncycastle:bcprov-jdk18on:$bouncycastleVersion"
|
||||||
implementation "org.bouncycastle:bcpkix-jdk18on:$bouncycastleVersion"
|
implementation "org.bouncycastle:bcpkix-jdk18on:$bouncycastleVersion"
|
||||||
implementation 'io.micrometer:micrometer-core:1.16.2'
|
implementation 'io.micrometer:micrometer-core'
|
||||||
implementation 'com.google.zxing:core:3.5.4'
|
implementation 'com.google.zxing:core:3.5.4'
|
||||||
implementation "org.commonmark:commonmark:$commonmarkVersion" // https://mvnrepository.com/artifact/org.commonmark/commonmark
|
implementation "org.commonmark:commonmark:$commonmarkVersion" // https://mvnrepository.com/artifact/org.commonmark/commonmark
|
||||||
implementation "org.commonmark:commonmark-ext-gfm-tables:$commonmarkVersion"
|
implementation "org.commonmark:commonmark-ext-gfm-tables:$commonmarkVersion"
|
||||||
@@ -81,7 +82,7 @@ dependencies {
|
|||||||
// veraPDF still uses javax.xml.bind, not the new jakarta namespace
|
// veraPDF still uses javax.xml.bind, not the new jakarta namespace
|
||||||
implementation 'javax.xml.bind:jaxb-api:2.3.1'
|
implementation 'javax.xml.bind:jaxb-api:2.3.1'
|
||||||
implementation 'com.sun.xml.bind:jaxb-impl:2.3.9'
|
implementation 'com.sun.xml.bind:jaxb-impl:2.3.9'
|
||||||
implementation 'com.sun.xml.bind:jaxb-core:4.0.6'
|
implementation 'com.sun.xml.bind:jaxb-core:4.0.7'
|
||||||
implementation 'org.apache.poi:poi-ooxml:5.5.1'
|
implementation 'org.apache.poi:poi-ooxml:5.5.1'
|
||||||
|
|
||||||
// https://mvnrepository.com/artifact/technology.tabula/tabula
|
// https://mvnrepository.com/artifact/technology.tabula/tabula
|
||||||
|
|||||||
@@ -73,7 +73,8 @@ public class ExternalAppDepConfig {
|
|||||||
tmp.put("tesseract", List.of("tesseract"));
|
tmp.put("tesseract", List.of("tesseract"));
|
||||||
tmp.put("rar", List.of("rar")); // Required for real CBR output
|
tmp.put("rar", List.of("rar")); // Required for real CBR output
|
||||||
tmp.put(calibrePath, List.of("Calibre"));
|
tmp.put(calibrePath, List.of("Calibre"));
|
||||||
tmp.put("ffmpeg", List.of("FFmpeg"));
|
// ffmpeg disabled due to raised CVEs
|
||||||
|
// tmp.put("ffmpeg", List.of("FFmpeg"));
|
||||||
tmp.put("magick", List.of("ImageMagick"));
|
tmp.put("magick", List.of("ImageMagick"));
|
||||||
this.commandToGroupMapping = Collections.unmodifiableMap(tmp);
|
this.commandToGroupMapping = Collections.unmodifiableMap(tmp);
|
||||||
}
|
}
|
||||||
|
|||||||
+207
-32
@@ -48,25 +48,144 @@ public class MultiPageLayoutController {
|
|||||||
public ResponseEntity<byte[]> mergeMultiplePagesIntoOne(
|
public ResponseEntity<byte[]> mergeMultiplePagesIntoOne(
|
||||||
@ModelAttribute MergeMultiplePagesRequest request) throws IOException {
|
@ModelAttribute MergeMultiplePagesRequest request) throws IOException {
|
||||||
|
|
||||||
int pagesPerSheet = request.getPagesPerSheet();
|
int MAX_PAGES = 100000;
|
||||||
MultipartFile file = request.getFileInput();
|
int MAX_COLS = 300;
|
||||||
boolean addBorder = Boolean.TRUE.equals(request.getAddBorder());
|
int MAX_ROWS = 300;
|
||||||
|
|
||||||
if (pagesPerSheet != 2
|
String mode = request.getMode();
|
||||||
&& pagesPerSheet != 3
|
if (mode == null || mode.trim().isEmpty()) {
|
||||||
&& pagesPerSheet != (int) Math.sqrt(pagesPerSheet) * Math.sqrt(pagesPerSheet)) {
|
mode = "DEFAULT";
|
||||||
|
}
|
||||||
|
|
||||||
|
int rows;
|
||||||
|
int cols;
|
||||||
|
int pagesPerSheet;
|
||||||
|
|
||||||
|
switch (mode) {
|
||||||
|
case "DEFAULT":
|
||||||
|
pagesPerSheet = request.getPagesPerSheet();
|
||||||
|
if (pagesPerSheet != 2
|
||||||
|
&& pagesPerSheet
|
||||||
|
!= (int) Math.sqrt(pagesPerSheet) * Math.sqrt(pagesPerSheet)) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidFormat",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"pagesPerSheet",
|
||||||
|
"must be 2 or a perfect square");
|
||||||
|
}
|
||||||
|
|
||||||
|
cols = pagesPerSheet == 2 ? pagesPerSheet : (int) Math.sqrt(pagesPerSheet);
|
||||||
|
rows = pagesPerSheet == 2 ? 1 : (int) Math.sqrt(pagesPerSheet);
|
||||||
|
break;
|
||||||
|
case "CUSTOM":
|
||||||
|
rows = request.getRows();
|
||||||
|
cols = request.getCols();
|
||||||
|
if (rows <= 0 || cols <= 0) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidFormat",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"rows and cols",
|
||||||
|
"only strictly positive values are allowed");
|
||||||
|
}
|
||||||
|
pagesPerSheet = cols * rows;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidFormat",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"mode",
|
||||||
|
"only 'DEFAULT' and 'CUSTOM' are supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pagesPerSheet > MAX_PAGES) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidArgument",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"pagesPerSheet",
|
||||||
|
"must be less than " + MAX_PAGES);
|
||||||
|
}
|
||||||
|
if (cols > MAX_COLS) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidArgument",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"cols",
|
||||||
|
"must be less than " + MAX_COLS);
|
||||||
|
}
|
||||||
|
if (rows > MAX_ROWS) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidArgument",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"rows",
|
||||||
|
"must be less than " + MAX_ROWS);
|
||||||
|
}
|
||||||
|
|
||||||
|
String orientation = request.getOrientation();
|
||||||
|
if (orientation == null || orientation.trim().isEmpty()) {
|
||||||
|
orientation = "PORTRAIT";
|
||||||
|
}
|
||||||
|
if (!"PORTRAIT".equals(orientation) && !"LANDSCAPE".equals(orientation)) {
|
||||||
throw ExceptionUtils.createIllegalArgumentException(
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
"error.invalidFormat",
|
"error.invalidFormat",
|
||||||
"Invalid {0} format: {1}",
|
"Invalid {0} format: {1}",
|
||||||
"pagesPerSheet",
|
"orientation",
|
||||||
"must be 2, 3 or a perfect square");
|
"only 'PORTRAIT' and 'LANDSCAPE' are supported");
|
||||||
}
|
}
|
||||||
|
|
||||||
int cols =
|
String arrangement = request.getArrangement();
|
||||||
pagesPerSheet == 2 || pagesPerSheet == 3
|
if (arrangement == null || arrangement.trim().isEmpty()) {
|
||||||
? pagesPerSheet
|
arrangement = "BY_ROWS";
|
||||||
: (int) Math.sqrt(pagesPerSheet);
|
}
|
||||||
int rows = pagesPerSheet == 2 || pagesPerSheet == 3 ? 1 : (int) Math.sqrt(pagesPerSheet);
|
if (!"BY_ROWS".equals(arrangement) && !"BY_COLUMNS".equals(arrangement)) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidFormat",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"arrangement",
|
||||||
|
"only 'BY_ROWS' and 'BY_COLUMNS' are supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
String readingDirection = request.getReadingDirection();
|
||||||
|
if (readingDirection == null || readingDirection.trim().isEmpty()) {
|
||||||
|
readingDirection = "LTR";
|
||||||
|
}
|
||||||
|
if (!"LTR".equals(readingDirection) && !"RTL".equals(readingDirection)) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidFormat",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"readingDirection",
|
||||||
|
"only 'LTR' and 'RTL' are supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean addBorder = Boolean.TRUE.equals(request.getAddBorder());
|
||||||
|
|
||||||
|
int topMargin = request.getTopMargin();
|
||||||
|
int bottomMargin = request.getBottomMargin();
|
||||||
|
int leftMargin = request.getLeftMargin();
|
||||||
|
int rightMargin = request.getRightMargin();
|
||||||
|
int innerMargin = request.getInnerMargin();
|
||||||
|
|
||||||
|
if (topMargin < 0
|
||||||
|
|| bottomMargin < 0
|
||||||
|
|| leftMargin < 0
|
||||||
|
|| rightMargin < 0
|
||||||
|
|| innerMargin < 0) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidFormat",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"Margins",
|
||||||
|
"only positive values are allowed");
|
||||||
|
}
|
||||||
|
|
||||||
|
int borderWidth = request.getBorderWidth() == 0 ? 1 : request.getBorderWidth();
|
||||||
|
|
||||||
|
if (addBorder && borderWidth <= 0) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidFormat",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"borderWidth",
|
||||||
|
"only strictly positive values are allowed when addBorder is true");
|
||||||
|
}
|
||||||
|
|
||||||
|
MultipartFile file = request.getFileInput();
|
||||||
|
|
||||||
try (PDDocument sourceDocument = pdfDocumentFactory.load(file)) {
|
try (PDDocument sourceDocument = pdfDocumentFactory.load(file)) {
|
||||||
try (PDDocument newDocument =
|
try (PDDocument newDocument =
|
||||||
@@ -74,16 +193,53 @@ public class MultiPageLayoutController {
|
|||||||
int totalPages = sourceDocument.getNumberOfPages();
|
int totalPages = sourceDocument.getNumberOfPages();
|
||||||
LayerUtility layerUtility = new LayerUtility(newDocument);
|
LayerUtility layerUtility = new LayerUtility(newDocument);
|
||||||
|
|
||||||
|
// Margin between page and content:
|
||||||
|
float pageWidth =
|
||||||
|
"PORTRAIT".equals(orientation)
|
||||||
|
? PDRectangle.A4.getWidth()
|
||||||
|
: PDRectangle.A4.getHeight();
|
||||||
|
float pageHeight =
|
||||||
|
"PORTRAIT".equals(orientation)
|
||||||
|
? PDRectangle.A4.getHeight()
|
||||||
|
: PDRectangle.A4.getWidth();
|
||||||
|
|
||||||
// Calculate cell dimensions once (all output pages are A4) - declare outside try
|
// Calculate cell dimensions once (all output pages are A4) - declare outside try
|
||||||
// blocks
|
// blocks
|
||||||
float cellWidth = PDRectangle.A4.getWidth() / cols;
|
float cellWidth = (pageWidth - leftMargin - rightMargin) / cols;
|
||||||
float cellHeight = PDRectangle.A4.getHeight() / rows;
|
float cellHeight = (pageHeight - topMargin - bottomMargin) / rows;
|
||||||
|
// Validate that outer margins and grid configuration yield positive cell size
|
||||||
|
if (cellWidth <= 0 || cellHeight <= 0) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidFormat",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"margin/layout configuration",
|
||||||
|
"Invalid margin or layout configuration: resulting cell size is non-positive. "
|
||||||
|
+ "Please reduce outer margins or adjust rows/columns.");
|
||||||
|
}
|
||||||
|
|
||||||
|
float innerWidth = cellWidth - 2 * innerMargin;
|
||||||
|
float innerHeight = cellHeight - 2 * innerMargin;
|
||||||
|
// Validate that inner margin fits within each cell
|
||||||
|
if (innerWidth <= 0 || innerHeight <= 0) {
|
||||||
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
|
"error.invalidFormat",
|
||||||
|
"Invalid {0} format: {1}",
|
||||||
|
"inner margin",
|
||||||
|
"Invalid inner margin: resulting inner content area is non-positive. "
|
||||||
|
+ "Please reduce inner margin or adjust outer margins/layout.");
|
||||||
|
}
|
||||||
|
|
||||||
// Process pages in groups of pagesPerSheet, creating a new page and content stream
|
// Process pages in groups of pagesPerSheet, creating a new page and content stream
|
||||||
// for each group
|
// for each group
|
||||||
for (int i = 0; i < totalPages; i += pagesPerSheet) {
|
for (int i = 0; i < totalPages; i += pagesPerSheet) {
|
||||||
// Create a new output page for each group of pagesPerSheet
|
// Create a new output page for each group of pagesPerSheet
|
||||||
PDPage newPage = new PDPage(PDRectangle.A4);
|
// Create a new A4 landscape rectangle that we use when orientation is landscape
|
||||||
|
PDRectangle a4Landscape =
|
||||||
|
new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth());
|
||||||
|
PDPage newPage =
|
||||||
|
"PORTRAIT".equals(orientation)
|
||||||
|
? new PDPage(PDRectangle.A4)
|
||||||
|
: new PDPage(a4Landscape);
|
||||||
newDocument.addPage(newPage);
|
newDocument.addPage(newPage);
|
||||||
|
|
||||||
// Use try-with-resources for each content stream to ensure proper cleanup
|
// Use try-with-resources for each content stream to ensure proper cleanup
|
||||||
@@ -95,30 +251,52 @@ public class MultiPageLayoutController {
|
|||||||
PDPageContentStream.AppendMode.APPEND,
|
PDPageContentStream.AppendMode.APPEND,
|
||||||
true,
|
true,
|
||||||
true)) {
|
true)) {
|
||||||
float borderThickness = 1.5f; // Specify border thickness as required
|
|
||||||
contentStream.setLineWidth(borderThickness);
|
if (addBorder) {
|
||||||
contentStream.setStrokingColor(Color.BLACK);
|
contentStream.setLineWidth(borderWidth);
|
||||||
|
contentStream.setStrokingColor(Color.BLACK);
|
||||||
|
}
|
||||||
|
|
||||||
// Process all pages in this group
|
// Process all pages in this group
|
||||||
for (int j = 0; j < pagesPerSheet && (i + j) < totalPages; j++) {
|
for (int j = 0; j < pagesPerSheet && (i + j) < totalPages; j++) {
|
||||||
int pageIndex = i + j;
|
int pageIndex = i + j;
|
||||||
PDPage sourcePage = sourceDocument.getPage(pageIndex);
|
PDPage sourcePage = sourceDocument.getPage(pageIndex);
|
||||||
PDRectangle rect = sourcePage.getMediaBox();
|
PDRectangle rect = sourcePage.getMediaBox();
|
||||||
float scaleWidth = cellWidth / rect.getWidth();
|
float scaleWidth = innerWidth / rect.getWidth();
|
||||||
float scaleHeight = cellHeight / rect.getHeight();
|
float scaleHeight = innerHeight / rect.getHeight();
|
||||||
float scale = Math.min(scaleWidth, scaleHeight);
|
float scale = Math.min(scaleWidth, scaleHeight);
|
||||||
|
|
||||||
int adjustedPageIndex = j % pagesPerSheet;
|
int adjustedPageIndex = j % pagesPerSheet;
|
||||||
int rowIndex = adjustedPageIndex / cols;
|
int rowIndex;
|
||||||
int colIndex = adjustedPageIndex % cols;
|
int colIndex;
|
||||||
|
|
||||||
|
if ("BY_ROWS".equals(arrangement)) {
|
||||||
|
rowIndex = adjustedPageIndex / cols;
|
||||||
|
if ("LTR".equals(readingDirection)) {
|
||||||
|
colIndex = adjustedPageIndex % cols;
|
||||||
|
} else {
|
||||||
|
colIndex = cols - 1 - (adjustedPageIndex % cols);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rowIndex = adjustedPageIndex % rows;
|
||||||
|
if ("LTR".equals(readingDirection)) {
|
||||||
|
colIndex = adjustedPageIndex / rows;
|
||||||
|
} else {
|
||||||
|
colIndex = cols - 1 - (adjustedPageIndex / rows);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
float x =
|
float x =
|
||||||
colIndex * cellWidth
|
leftMargin
|
||||||
+ (cellWidth - rect.getWidth() * scale) / 2;
|
+ colIndex * cellWidth
|
||||||
|
+ innerMargin
|
||||||
|
+ (innerWidth - rect.getWidth() * scale) / 2;
|
||||||
float y =
|
float y =
|
||||||
newPage.getMediaBox().getHeight()
|
newPage.getMediaBox().getHeight()
|
||||||
|
- topMargin
|
||||||
- ((rowIndex + 1) * cellHeight
|
- ((rowIndex + 1) * cellHeight
|
||||||
- (cellHeight - rect.getHeight() * scale) / 2);
|
- innerMargin
|
||||||
|
- (innerHeight - rect.getHeight() * scale) / 2);
|
||||||
|
|
||||||
contentStream.saveGraphicsState();
|
contentStream.saveGraphicsState();
|
||||||
contentStream.transform(Matrix.getTranslateInstance(x, y));
|
contentStream.transform(Matrix.getTranslateInstance(x, y));
|
||||||
@@ -132,11 +310,8 @@ public class MultiPageLayoutController {
|
|||||||
|
|
||||||
if (addBorder) {
|
if (addBorder) {
|
||||||
// Draw border around each page
|
// Draw border around each page
|
||||||
float borderX = colIndex * cellWidth;
|
contentStream.addRect(
|
||||||
float borderY =
|
x, y, rect.getWidth() * scale, rect.getHeight() * scale);
|
||||||
newPage.getMediaBox().getHeight()
|
|
||||||
- (rowIndex + 1) * cellHeight;
|
|
||||||
contentStream.addRect(borderX, borderY, cellWidth, cellHeight);
|
|
||||||
contentStream.stroke();
|
contentStream.stroke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,7 +320,7 @@ public class MultiPageLayoutController {
|
|||||||
|
|
||||||
// If any source page is rotated, skip form copying/transformation entirely
|
// If any source page is rotated, skip form copying/transformation entirely
|
||||||
boolean hasRotation = GeneralFormCopyUtils.hasAnyRotatedPage(sourceDocument);
|
boolean hasRotation = GeneralFormCopyUtils.hasAnyRotatedPage(sourceDocument);
|
||||||
if (hasRotation) {
|
if (hasRotation || "LANDSCAPE".equals(orientation)) {
|
||||||
log.info("Source document has rotated pages; skipping form field copying.");
|
log.info("Source document has rotated pages; skipping form field copying.");
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
|||||||
-80
@@ -1,80 +0,0 @@
|
|||||||
package stirling.software.SPDF.controller.api;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
import stirling.software.SPDF.config.swagger.StandardPdfResponse;
|
|
||||||
import stirling.software.SPDF.service.PdfImageRemovalService;
|
|
||||||
import stirling.software.common.annotations.AutoJobPostMapping;
|
|
||||||
import stirling.software.common.annotations.api.GeneralApi;
|
|
||||||
import stirling.software.common.model.api.PDFFile;
|
|
||||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
|
||||||
import stirling.software.common.util.GeneralUtils;
|
|
||||||
import stirling.software.common.util.WebResponseUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Controller class for handling PDF image removal requests. Provides an endpoint to remove images
|
|
||||||
* from a PDF file to reduce its size.
|
|
||||||
*/
|
|
||||||
@GeneralApi
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class PdfImageRemovalController {
|
|
||||||
|
|
||||||
// Service for removing images from PDFs
|
|
||||||
private final PdfImageRemovalService pdfImageRemovalService;
|
|
||||||
|
|
||||||
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Endpoint to remove images from a PDF file.
|
|
||||||
*
|
|
||||||
* <p>This method processes the uploaded PDF file, removes all images, and returns the modified
|
|
||||||
* PDF file with a new name indicating that images were removed.
|
|
||||||
*
|
|
||||||
* @param file The PDF file with images to be removed.
|
|
||||||
* @return ResponseEntity containing the modified PDF file as byte array with appropriate
|
|
||||||
* content type and filename.
|
|
||||||
* @throws IOException If an error occurs while processing the PDF file.
|
|
||||||
*/
|
|
||||||
@AutoJobPostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, value = "/remove-image-pdf")
|
|
||||||
@StandardPdfResponse
|
|
||||||
@Operation(
|
|
||||||
summary = "Remove images from file to reduce the file size.",
|
|
||||||
description =
|
|
||||||
"This endpoint remove images from file to reduce the file size.Input:PDF"
|
|
||||||
+ " Output:PDF Type:SISO")
|
|
||||||
public ResponseEntity<byte[]> removeImages(@ModelAttribute PDFFile file) throws IOException {
|
|
||||||
// Load the PDF document with proper resource management
|
|
||||||
try (PDDocument document = pdfDocumentFactory.load(file)) {
|
|
||||||
|
|
||||||
// Remove images from the PDF document using the service
|
|
||||||
try (PDDocument modifiedDocument =
|
|
||||||
pdfImageRemovalService.removeImagesFromPdf(document)) {
|
|
||||||
|
|
||||||
// Create a ByteArrayOutputStream to hold the modified PDF data
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
||||||
|
|
||||||
// Save the modified PDF document to the output stream
|
|
||||||
modifiedDocument.save(outputStream);
|
|
||||||
|
|
||||||
// Generate a new filename for the modified PDF
|
|
||||||
String mergedFileName =
|
|
||||||
GeneralUtils.generateFilename(
|
|
||||||
file.getFileInput().getOriginalFilename(), "_images_removed.pdf");
|
|
||||||
|
|
||||||
// Convert the byte array to a web response and return it
|
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
|
||||||
outputStream.toByteArray(), mergedFileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-23
@@ -150,28 +150,6 @@ public class RearrangePagesPDFController {
|
|||||||
return newPageOrder;
|
return newPageOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Rearrange pages in a PDF file by merging odd and even pages. The first half of the pages will
|
|
||||||
* be the odd pages, and the second half will be the even pages as input. <br>
|
|
||||||
* This method is visible for testing purposes only.
|
|
||||||
*
|
|
||||||
* @param totalPages Total number of pages in the PDF file.
|
|
||||||
* @return List of page numbers in the new order. The first page is 0.
|
|
||||||
*/
|
|
||||||
List<Integer> oddEvenMerge(int totalPages) {
|
|
||||||
List<Integer> newPageOrderZeroBased = new ArrayList<>();
|
|
||||||
int numberOfOddPages = (totalPages + 1) / 2;
|
|
||||||
|
|
||||||
for (int oneBasedIndex = 1; oneBasedIndex < (numberOfOddPages + 1); oneBasedIndex++) {
|
|
||||||
newPageOrderZeroBased.add((oneBasedIndex - 1));
|
|
||||||
if (numberOfOddPages + oneBasedIndex <= totalPages) {
|
|
||||||
newPageOrderZeroBased.add((numberOfOddPages + oneBasedIndex - 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return newPageOrderZeroBased;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Integer> duplicate(int totalPages, String pageOrder) {
|
private List<Integer> duplicate(int totalPages, String pageOrder) {
|
||||||
List<Integer> newPageOrder = new ArrayList<>();
|
List<Integer> newPageOrder = new ArrayList<>();
|
||||||
int duplicateCount;
|
int duplicateCount;
|
||||||
@@ -220,7 +198,6 @@ public class RearrangePagesPDFController {
|
|||||||
case BOOKLET_SORT -> bookletSort(totalPages);
|
case BOOKLET_SORT -> bookletSort(totalPages);
|
||||||
case SIDE_STITCH_BOOKLET_SORT -> sideStitchBooklet(totalPages);
|
case SIDE_STITCH_BOOKLET_SORT -> sideStitchBooklet(totalPages);
|
||||||
case ODD_EVEN_SPLIT -> oddEvenSplit(totalPages);
|
case ODD_EVEN_SPLIT -> oddEvenSplit(totalPages);
|
||||||
case ODD_EVEN_MERGE -> oddEvenMerge(totalPages);
|
|
||||||
case REMOVE_FIRST -> removeFirst(totalPages);
|
case REMOVE_FIRST -> removeFirst(totalPages);
|
||||||
case REMOVE_LAST -> removeLast(totalPages);
|
case REMOVE_LAST -> removeLast(totalPages);
|
||||||
case REMOVE_FIRST_AND_LAST -> removeFirstAndLast(totalPages);
|
case REMOVE_FIRST_AND_LAST -> removeFirstAndLast(totalPages);
|
||||||
|
|||||||
+2
-1
@@ -81,7 +81,8 @@ public class ConvertEmlToPDF {
|
|||||||
|
|
||||||
if (request.isDownloadHtml()) {
|
if (request.isDownloadHtml()) {
|
||||||
try {
|
try {
|
||||||
String htmlContent = EmlToPdf.convertEmlToHtml(fileBytes, request);
|
String htmlContent =
|
||||||
|
EmlToPdf.convertEmlToHtml(fileBytes, request, customHtmlSanitizer);
|
||||||
log.info("Successfully converted email to HTML: {}", originalFilename);
|
log.info("Successfully converted email to HTML: {}", originalFilename);
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
htmlContent.getBytes(StandardCharsets.UTF_8),
|
htmlContent.getBytes(StandardCharsets.UTF_8),
|
||||||
|
|||||||
+3
-17
@@ -8,10 +8,7 @@ import java.awt.Graphics2D;
|
|||||||
import java.awt.geom.AffineTransform;
|
import java.awt.geom.AffineTransform;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.text.DecimalFormat;
|
|
||||||
import java.text.DecimalFormatSymbols;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
@@ -23,31 +20,17 @@ import javax.imageio.ImageIO;
|
|||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.rendering.ImageType;
|
import org.apache.pdfbox.rendering.ImageType;
|
||||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import io.github.pixee.security.Filenames;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
import stirling.software.SPDF.model.api.converters.PdfToVideoRequest;
|
|
||||||
import stirling.software.common.annotations.AutoJobPostMapping;
|
|
||||||
import stirling.software.common.annotations.api.ConvertApi;
|
import stirling.software.common.annotations.api.ConvertApi;
|
||||||
import stirling.software.common.model.ApplicationProperties;
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||||
import stirling.software.common.util.ApplicationContextProvider;
|
import stirling.software.common.util.ApplicationContextProvider;
|
||||||
import stirling.software.common.util.CheckProgramInstall;
|
|
||||||
import stirling.software.common.util.ExceptionUtils;
|
import stirling.software.common.util.ExceptionUtils;
|
||||||
import stirling.software.common.util.ProcessExecutor;
|
|
||||||
import stirling.software.common.util.ProcessExecutor.ProcessExecutorResult;
|
|
||||||
import stirling.software.common.util.TempDirectory;
|
|
||||||
import stirling.software.common.util.TempFile;
|
import stirling.software.common.util.TempFile;
|
||||||
import stirling.software.common.util.TempFileManager;
|
import stirling.software.common.util.TempFileManager;
|
||||||
import stirling.software.common.util.WebResponseUtils;
|
|
||||||
|
|
||||||
@ConvertApi
|
@ConvertApi
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -64,6 +47,8 @@ public class ConvertPdfToVideoController {
|
|||||||
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
||||||
private final TempFileManager tempFileManager;
|
private final TempFileManager tempFileManager;
|
||||||
|
|
||||||
|
// ffmpeg disabled due to raised CVEs
|
||||||
|
/*
|
||||||
@AutoJobPostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, value = "/pdf/video")
|
@AutoJobPostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, value = "/pdf/video")
|
||||||
@Operation(
|
@Operation(
|
||||||
summary = "Convert PDF to Video Slideshow",
|
summary = "Convert PDF to Video Slideshow",
|
||||||
@@ -163,6 +148,7 @@ public class ConvertPdfToVideoController {
|
|||||||
return WebResponseUtils.bytesToWebResponse(videoBytes, outputName, mediaType);
|
return WebResponseUtils.bytesToWebResponse(videoBytes, outputName, mediaType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
private void generateFrames(
|
private void generateFrames(
|
||||||
Path inputPdf,
|
Path inputPdf,
|
||||||
|
|||||||
+251
-84
@@ -1,19 +1,22 @@
|
|||||||
package stirling.software.SPDF.controller.api.misc;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
|
import java.awt.Graphics2D;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.DataBufferByte;
|
|
||||||
import java.awt.image.DataBufferInt;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.EnumMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.cos.COSName;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -21,6 +24,7 @@ import org.springframework.web.bind.annotation.ModelAttribute;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.google.zxing.*;
|
import com.google.zxing.*;
|
||||||
|
import com.google.zxing.common.GlobalHistogramBinarizer;
|
||||||
import com.google.zxing.common.HybridBinarizer;
|
import com.google.zxing.common.HybridBinarizer;
|
||||||
|
|
||||||
import io.github.pixee.security.Filenames;
|
import io.github.pixee.security.Filenames;
|
||||||
@@ -35,7 +39,6 @@ import stirling.software.common.annotations.AutoJobPostMapping;
|
|||||||
import stirling.software.common.annotations.api.MiscApi;
|
import stirling.software.common.annotations.api.MiscApi;
|
||||||
import stirling.software.common.model.ApplicationProperties;
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||||
import stirling.software.common.util.ApplicationContextProvider;
|
|
||||||
import stirling.software.common.util.ExceptionUtils;
|
import stirling.software.common.util.ExceptionUtils;
|
||||||
import stirling.software.common.util.GeneralUtils;
|
import stirling.software.common.util.GeneralUtils;
|
||||||
import stirling.software.common.util.TempFile;
|
import stirling.software.common.util.TempFile;
|
||||||
@@ -48,61 +51,219 @@ import stirling.software.common.util.WebResponseUtils;
|
|||||||
public class AutoSplitPdfController {
|
public class AutoSplitPdfController {
|
||||||
|
|
||||||
private static final Set<String> VALID_QR_CONTENTS =
|
private static final Set<String> VALID_QR_CONTENTS =
|
||||||
new HashSet<>(
|
Set.of(
|
||||||
Set.of(
|
"https://github.com/Stirling-Tools/Stirling-PDF",
|
||||||
"https://github.com/Stirling-Tools/Stirling-PDF",
|
"https://github.com/Frooodle/Stirling-PDF",
|
||||||
"https://github.com/Frooodle/Stirling-PDF",
|
"https://stirlingpdf.com");
|
||||||
"https://stirlingpdf.com"));
|
|
||||||
|
private static final int MAX_IMAGES_FOR_DIRECT_EXTRACTION = 3;
|
||||||
|
|
||||||
|
// 150 DPI is sufficient for QR code detection — higher wastes memory and CPU
|
||||||
|
private static final int QR_DETECTION_DPI = 150;
|
||||||
|
|
||||||
|
// Max total pixels before we downscale to avoid OOM on getRGB() allocation
|
||||||
|
private static final long MAX_IMAGE_PIXELS = 100_000_000L; // ~10000x10000
|
||||||
|
|
||||||
|
// Number of evenly-spaced pixel samples used for the blank image check
|
||||||
|
private static final int BLANK_CHECK_SAMPLES = 20;
|
||||||
|
|
||||||
|
private static final Map<DecodeHintType, Object> DECODE_HINTS;
|
||||||
|
|
||||||
|
static {
|
||||||
|
DECODE_HINTS = new EnumMap<>(DecodeHintType.class);
|
||||||
|
DECODE_HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
|
||||||
|
DECODE_HINTS.put(DecodeHintType.ALSO_INVERTED, Boolean.TRUE);
|
||||||
|
DECODE_HINTS.put(DecodeHintType.POSSIBLE_FORMATS, List.of(BarcodeFormat.QR_CODE));
|
||||||
|
}
|
||||||
|
|
||||||
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
||||||
private final TempFileManager tempFileManager;
|
private final TempFileManager tempFileManager;
|
||||||
|
private final ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
private static String decodeQRCode(BufferedImage bufferedImage) {
|
/**
|
||||||
LuminanceSource source;
|
* Downscale an image if it exceeds the maximum pixel count. Scales uniformly based on the
|
||||||
|
* pixel-count ratio so both portrait and landscape images are handled correctly.
|
||||||
if (bufferedImage.getRaster().getDataBuffer() instanceof DataBufferByte dataBufferByte) {
|
*/
|
||||||
byte[] pixels = dataBufferByte.getData();
|
private static BufferedImage downscaleIfNeeded(BufferedImage image) {
|
||||||
source =
|
long totalPixels = (long) image.getWidth() * image.getHeight();
|
||||||
new PlanarYUVLuminanceSource(
|
if (totalPixels <= MAX_IMAGE_PIXELS) {
|
||||||
pixels,
|
return image;
|
||||||
bufferedImage.getWidth(),
|
|
||||||
bufferedImage.getHeight(),
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
bufferedImage.getWidth(),
|
|
||||||
bufferedImage.getHeight(),
|
|
||||||
false);
|
|
||||||
} else if (bufferedImage.getRaster().getDataBuffer()
|
|
||||||
instanceof DataBufferInt dataBufferInt) {
|
|
||||||
int[] pixels = dataBufferInt.getData();
|
|
||||||
byte[] newPixels = new byte[pixels.length];
|
|
||||||
for (int i = 0; i < pixels.length; i++) {
|
|
||||||
newPixels[i] = (byte) (pixels[i] & 0xff);
|
|
||||||
}
|
|
||||||
source =
|
|
||||||
new PlanarYUVLuminanceSource(
|
|
||||||
newPixels,
|
|
||||||
bufferedImage.getWidth(),
|
|
||||||
bufferedImage.getHeight(),
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
bufferedImage.getWidth(),
|
|
||||||
bufferedImage.getHeight(),
|
|
||||||
false);
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"BufferedImage must have 8-bit gray scale, 24-bit RGB, 32-bit ARGB (packed"
|
|
||||||
+ " int), byte gray, or 3-byte/4-byte RGB image data");
|
|
||||||
}
|
}
|
||||||
|
double scale = Math.sqrt((double) MAX_IMAGE_PIXELS / totalPixels);
|
||||||
|
int newWidth = Math.max(1, (int) (image.getWidth() * scale));
|
||||||
|
int newHeight = Math.max(1, (int) (image.getHeight() * scale));
|
||||||
|
log.debug(
|
||||||
|
"Downscaling image from {}x{} to {}x{} for QR detection",
|
||||||
|
image.getWidth(),
|
||||||
|
image.getHeight(),
|
||||||
|
newWidth,
|
||||||
|
newHeight);
|
||||||
|
BufferedImage scaled = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics2D g = scaled.createGraphics();
|
||||||
|
g.drawImage(image, 0, 0, newWidth, newHeight, null);
|
||||||
|
g.dispose();
|
||||||
|
return scaled;
|
||||||
|
}
|
||||||
|
|
||||||
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
/**
|
||||||
|
* Quick check whether an image appears to be blank (single solid colour). Samples pixels at
|
||||||
|
* evenly-spaced positions — if all samples match the first pixel the image is almost certainly
|
||||||
|
* blank (e.g. a masked image that returned solid white).
|
||||||
|
*/
|
||||||
|
private static boolean isBlankImage(int[] pixels) {
|
||||||
|
if (pixels.length == 0) return true;
|
||||||
|
int first = pixels[0];
|
||||||
|
int step = Math.max(1, pixels.length / BLANK_CHECK_SAMPLES);
|
||||||
|
for (int i = step; i < pixels.length; i += step) {
|
||||||
|
if (pixels[i] != first) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to decode a QR code from pre-extracted RGB pixel data using multiple binarization
|
||||||
|
* strategies. Returns the decoded text or null.
|
||||||
|
*
|
||||||
|
* <p>Strategy 1: HybridBinarizer — good for variable brightness (digital PDFs).
|
||||||
|
*
|
||||||
|
* <p>Strategy 2: GlobalHistogramBinarizer — better for scanned/noisy images with uniform
|
||||||
|
* lighting, and for QR codes with embedded logos that confuse the hybrid approach.
|
||||||
|
*/
|
||||||
|
private static String tryDecodeQR(int[] pixels, int width, int height) {
|
||||||
|
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
|
||||||
|
MultiFormatReader reader = new MultiFormatReader();
|
||||||
|
|
||||||
|
// Strategy 1: HybridBinarizer — good for variable brightness (digital PDFs)
|
||||||
try {
|
try {
|
||||||
Result result = new MultiFormatReader().decode(bitmap);
|
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
||||||
|
Result result = reader.decode(bitmap, DECODE_HINTS);
|
||||||
|
log.debug("QR detected via HybridBinarizer: '{}'", result.getText());
|
||||||
return result.getText();
|
return result.getText();
|
||||||
} catch (NotFoundException e) {
|
} catch (NotFoundException e) {
|
||||||
return null; // there is no QR code in the image
|
// continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Strategy 2: GlobalHistogramBinarizer — better for scanned/noisy images
|
||||||
|
try {
|
||||||
|
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
|
||||||
|
Result result = reader.decode(bitmap, DECODE_HINTS);
|
||||||
|
log.debug("QR detected via GlobalHistogramBinarizer: '{}'", result.getText());
|
||||||
|
return result.getText();
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to decode a QR code from a BufferedImage. Handles downscaling for oversized images
|
||||||
|
* and skips blank images early.
|
||||||
|
*/
|
||||||
|
private static String decodeQRCode(BufferedImage bufferedImage) {
|
||||||
|
bufferedImage = downscaleIfNeeded(bufferedImage);
|
||||||
|
|
||||||
|
int width = bufferedImage.getWidth();
|
||||||
|
int height = bufferedImage.getHeight();
|
||||||
|
int[] pixels = new int[width * height];
|
||||||
|
bufferedImage.getRGB(0, 0, width, height, pixels, 0, width);
|
||||||
|
|
||||||
|
// Skip blank images early (e.g. masked images that decode to solid white)
|
||||||
|
if (isBlankImage(pixels)) {
|
||||||
|
log.debug("Skipping blank {}x{} image", width, height);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tryDecodeQR(pixels, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Count the number of images embedded in a page's resources. */
|
||||||
|
private static int countPageImages(PDPage page) {
|
||||||
|
if (page.getResources() == null || page.getResources().getXObjectNames() == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
for (COSName name : page.getResources().getXObjectNames()) {
|
||||||
|
if (page.getResources().isImageXObject(name)) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract images directly from a page's resources and check each for a QR code. Returns the QR
|
||||||
|
* code text if found, null otherwise.
|
||||||
|
*/
|
||||||
|
private static String checkPageImagesDirect(PDPage page) throws IOException {
|
||||||
|
if (page.getResources() == null || page.getResources().getXObjectNames() == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (COSName name : page.getResources().getXObjectNames()) {
|
||||||
|
if (!page.getResources().isImageXObject(name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
PDImageXObject imageObject = (PDImageXObject) page.getResources().getXObject(name);
|
||||||
|
|
||||||
|
BufferedImage image;
|
||||||
|
try {
|
||||||
|
image = imageObject.getImage();
|
||||||
|
} catch (OutOfMemoryError e) {
|
||||||
|
log.warn(
|
||||||
|
"Skipping oversized embedded image '{}' ({}x{}) - out of memory",
|
||||||
|
name.getName(),
|
||||||
|
imageObject.getWidth(),
|
||||||
|
imageObject.getHeight());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String result = decodeQRCode(image);
|
||||||
|
if (result != null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render the full page to an image and scan it for a QR code. Tries a low DPI first (fast, low
|
||||||
|
* memory) and only retries at the system's maxDPI if detection fails. The first rendered image
|
||||||
|
* is released before the retry to allow GC to reclaim it.
|
||||||
|
*/
|
||||||
|
private String checkPageByRendering(PDFRenderer pdfRenderer, int pageNum) throws IOException {
|
||||||
|
log.debug("Rendering page {} at {} DPI for QR detection", pageNum + 1, QR_DETECTION_DPI);
|
||||||
|
|
||||||
|
BufferedImage bim =
|
||||||
|
ExceptionUtils.handleOomRendering(
|
||||||
|
pageNum + 1,
|
||||||
|
QR_DETECTION_DPI,
|
||||||
|
() -> pdfRenderer.renderImageWithDPI(pageNum, QR_DETECTION_DPI));
|
||||||
|
String result = decodeQRCode(bim);
|
||||||
|
bim = null; // allow GC before potential high-DPI retry
|
||||||
|
|
||||||
|
if (result == null) {
|
||||||
|
int maxDpi = getSystemMaxDpi();
|
||||||
|
if (maxDpi > QR_DETECTION_DPI) {
|
||||||
|
log.debug(
|
||||||
|
"Retrying page {} at {} DPI (low-DPI detection failed)",
|
||||||
|
pageNum + 1,
|
||||||
|
maxDpi);
|
||||||
|
BufferedImage highRes =
|
||||||
|
ExceptionUtils.handleOomRendering(
|
||||||
|
pageNum + 1,
|
||||||
|
maxDpi,
|
||||||
|
() -> pdfRenderer.renderImageWithDPI(pageNum, maxDpi));
|
||||||
|
result = decodeQRCode(highRes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getSystemMaxDpi() {
|
||||||
|
if (applicationProperties != null && applicationProperties.getSystem() != null) {
|
||||||
|
return applicationProperties.getSystem().getMaxDPI();
|
||||||
|
}
|
||||||
|
return QR_DETECTION_DPI;
|
||||||
}
|
}
|
||||||
|
|
||||||
@AutoJobPostMapping(value = "/auto-split-pdf", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@AutoJobPostMapping(value = "/auto-split-pdf", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
@@ -111,42 +272,56 @@ public class AutoSplitPdfController {
|
|||||||
summary = "Auto split PDF pages into separate documents",
|
summary = "Auto split PDF pages into separate documents",
|
||||||
description =
|
description =
|
||||||
"This endpoint accepts a PDF file, scans each page for a specific QR code, and"
|
"This endpoint accepts a PDF file, scans each page for a specific QR code, and"
|
||||||
+ " splits the document at the QR code boundaries. The output is a zip file"
|
+ " splits the document at the QR code boundaries. The output is a zip"
|
||||||
+ " containing each separate PDF document. Input:PDF Output:ZIP-PDF"
|
+ " file containing each separate PDF document. Input:PDF Output:ZIP-PDF"
|
||||||
+ " Type:SISO")
|
+ " Type:SISO")
|
||||||
public ResponseEntity<byte[]> autoSplitPdf(@ModelAttribute AutoSplitPdfRequest request)
|
public ResponseEntity<byte[]> autoSplitPdf(@ModelAttribute AutoSplitPdfRequest request)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
MultipartFile file = request.getFileInput();
|
MultipartFile file = request.getFileInput();
|
||||||
boolean duplexMode = Boolean.TRUE.equals(request.getDuplexMode());
|
boolean duplexMode = Boolean.TRUE.equals(request.getDuplexMode());
|
||||||
|
|
||||||
|
log.info(
|
||||||
|
"Auto-split starting: filename='{}', size={} bytes, duplexMode={}",
|
||||||
|
file.getOriginalFilename(),
|
||||||
|
file.getSize(),
|
||||||
|
duplexMode);
|
||||||
|
|
||||||
List<PDDocument> splitDocuments = new ArrayList<>();
|
List<PDDocument> splitDocuments = new ArrayList<>();
|
||||||
try (TempFile outputTempFile = new TempFile(tempFileManager, ".zip");
|
try (TempFile outputTempFile = new TempFile(tempFileManager, ".zip");
|
||||||
PDDocument document = pdfDocumentFactory.load(file.getInputStream())) {
|
PDDocument document = pdfDocumentFactory.load(file.getInputStream())) {
|
||||||
|
int totalPages = document.getNumberOfPages();
|
||||||
|
log.info("PDF loaded, totalPages={}", totalPages);
|
||||||
|
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
pdfRenderer.setSubsamplingAllowed(true);
|
pdfRenderer.setSubsamplingAllowed(true);
|
||||||
|
|
||||||
for (int page = 0; page < document.getNumberOfPages(); ++page) {
|
for (int page = 0; page < totalPages; ++page) {
|
||||||
BufferedImage bim;
|
PDPage pdPage = document.getPage(page);
|
||||||
|
int imageCount = countPageImages(pdPage);
|
||||||
|
|
||||||
// Use global maximum DPI setting, fallback to 300 if not set
|
String qrResult;
|
||||||
int renderDpi = 150; // Default fallback
|
if (imageCount > 0 && imageCount <= MAX_IMAGES_FOR_DIRECT_EXTRACTION) {
|
||||||
ApplicationProperties properties =
|
// Try extracting images directly from the PDF (faster, avoids rendering)
|
||||||
ApplicationContextProvider.getBean(ApplicationProperties.class);
|
qrResult = checkPageImagesDirect(pdPage);
|
||||||
if (properties != null && properties.getSystem() != null) {
|
if (qrResult == null) {
|
||||||
renderDpi = properties.getSystem().getMaxDPI();
|
// Fall back to rendering — the image may use masking/compositing
|
||||||
|
// that getImage() doesn't resolve, or the QR may be vector-drawn
|
||||||
|
qrResult = checkPageByRendering(pdfRenderer, page);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Too many images or no images — render the full page
|
||||||
|
qrResult = checkPageByRendering(pdfRenderer, page);
|
||||||
}
|
}
|
||||||
final int dpi = renderDpi;
|
|
||||||
final int pageNum = page;
|
|
||||||
|
|
||||||
bim =
|
boolean isValidQrCode = qrResult != null && VALID_QR_CONTENTS.contains(qrResult);
|
||||||
ExceptionUtils.handleOomRendering(
|
if (isValidQrCode) {
|
||||||
pageNum + 1,
|
log.info(
|
||||||
dpi,
|
"Page {}/{} contains QR divider ('{}')",
|
||||||
() -> pdfRenderer.renderImageWithDPI(pageNum, dpi));
|
page + 1,
|
||||||
String result = decodeQRCode(bim);
|
totalPages,
|
||||||
|
qrResult);
|
||||||
|
}
|
||||||
|
|
||||||
boolean isValidQrCode = VALID_QR_CONTENTS.contains(result);
|
|
||||||
log.debug("detected qr code {}, code is vale={}", result, isValidQrCode);
|
|
||||||
if (isValidQrCode && page != 0) {
|
if (isValidQrCode && page != 0) {
|
||||||
splitDocuments.add(new PDDocument());
|
splitDocuments.add(new PDDocument());
|
||||||
}
|
}
|
||||||
@@ -159,32 +334,25 @@ public class AutoSplitPdfController {
|
|||||||
splitDocuments.add(firstDocument);
|
splitDocuments.add(firstDocument);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If duplexMode is true and current page is a divider, then skip next page
|
|
||||||
if (duplexMode && isValidQrCode) {
|
if (duplexMode && isValidQrCode) {
|
||||||
page++;
|
page++; // skip back of divider page
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove split documents that have no pages
|
|
||||||
splitDocuments.removeIf(pdDocument -> pdDocument.getNumberOfPages() == 0);
|
splitDocuments.removeIf(pdDocument -> pdDocument.getNumberOfPages() == 0);
|
||||||
|
log.info("Split complete, {} output documents", splitDocuments.size());
|
||||||
|
|
||||||
String filename =
|
String filename =
|
||||||
GeneralUtils.removeExtension(
|
GeneralUtils.removeExtension(
|
||||||
Filenames.toSimpleFileName(file.getOriginalFilename()));
|
Filenames.toSimpleFileName(file.getOriginalFilename()));
|
||||||
|
|
||||||
try (ZipOutputStream zipOut =
|
// Stream split documents directly into zip — avoids holding all PDFs in memory
|
||||||
new ZipOutputStream(Files.newOutputStream(outputTempFile.getPath()))) {
|
try (OutputStream fileOut = Files.newOutputStream(outputTempFile.getPath());
|
||||||
|
ZipOutputStream zipOut = new ZipOutputStream(fileOut)) {
|
||||||
for (int i = 0; i < splitDocuments.size(); i++) {
|
for (int i = 0; i < splitDocuments.size(); i++) {
|
||||||
String fileName = filename + "_" + (i + 1) + ".pdf";
|
String fileName = filename + "_" + (i + 1) + ".pdf";
|
||||||
PDDocument splitDocument = splitDocuments.get(i);
|
zipOut.putNextEntry(new ZipEntry(fileName));
|
||||||
|
splitDocuments.get(i).save(zipOut);
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
splitDocument.save(baos);
|
|
||||||
byte[] pdf = baos.toByteArray();
|
|
||||||
|
|
||||||
ZipEntry pdfEntry = new ZipEntry(fileName);
|
|
||||||
zipOut.putNextEntry(pdfEntry);
|
|
||||||
zipOut.write(pdf);
|
|
||||||
zipOut.closeEntry();
|
zipOut.closeEntry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -197,7 +365,6 @@ public class AutoSplitPdfController {
|
|||||||
log.error("Error in auto split", e);
|
log.error("Error in auto split", e);
|
||||||
throw e;
|
throw e;
|
||||||
} finally {
|
} finally {
|
||||||
// Clean up split documents
|
|
||||||
for (PDDocument splitDoc : splitDocuments) {
|
for (PDDocument splitDoc : splitDocuments) {
|
||||||
try {
|
try {
|
||||||
splitDoc.close();
|
splitDoc.close();
|
||||||
|
|||||||
+29
@@ -17,6 +17,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import stirling.software.SPDF.config.EndpointConfiguration;
|
import stirling.software.SPDF.config.EndpointConfiguration;
|
||||||
import stirling.software.SPDF.config.EndpointConfiguration.EndpointAvailability;
|
import stirling.software.SPDF.config.EndpointConfiguration.EndpointAvailability;
|
||||||
import stirling.software.SPDF.config.InitialSetup;
|
import stirling.software.SPDF.config.InitialSetup;
|
||||||
|
import stirling.software.SPDF.controller.api.security.TimestampController;
|
||||||
import stirling.software.common.annotations.api.ConfigApi;
|
import stirling.software.common.annotations.api.ConfigApi;
|
||||||
import stirling.software.common.configuration.AppConfig;
|
import stirling.software.common.configuration.AppConfig;
|
||||||
import stirling.software.common.model.ApplicationProperties;
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
@@ -203,6 +204,27 @@ public class ConfigController {
|
|||||||
boolean invitesEnabled = applicationProperties.getMail().isEnableInvites();
|
boolean invitesEnabled = applicationProperties.getMail().isEnableInvites();
|
||||||
configData.put("enableEmailInvites", smtpEnabled && invitesEnabled);
|
configData.put("enableEmailInvites", smtpEnabled && invitesEnabled);
|
||||||
|
|
||||||
|
// Storage settings
|
||||||
|
boolean storageEnabled = enableLogin && applicationProperties.getStorage().isEnabled();
|
||||||
|
boolean sharingEnabled =
|
||||||
|
storageEnabled && applicationProperties.getStorage().getSharing().isEnabled();
|
||||||
|
boolean frontendUrlConfigured = frontendUrl != null && !frontendUrl.trim().isEmpty();
|
||||||
|
boolean shareLinksEnabled =
|
||||||
|
sharingEnabled
|
||||||
|
&& applicationProperties.getStorage().getSharing().isLinkEnabled()
|
||||||
|
&& frontendUrlConfigured;
|
||||||
|
boolean shareEmailEnabled =
|
||||||
|
sharingEnabled
|
||||||
|
&& applicationProperties.getStorage().getSharing().isEmailEnabled()
|
||||||
|
&& applicationProperties.getMail().isEnabled();
|
||||||
|
boolean groupSigningEnabled =
|
||||||
|
storageEnabled && applicationProperties.getStorage().getSigning().isEnabled();
|
||||||
|
configData.put("storageEnabled", storageEnabled);
|
||||||
|
configData.put("storageSharingEnabled", sharingEnabled);
|
||||||
|
configData.put("storageShareLinksEnabled", shareLinksEnabled);
|
||||||
|
configData.put("storageShareEmailEnabled", shareEmailEnabled);
|
||||||
|
configData.put("storageGroupSigningEnabled", groupSigningEnabled);
|
||||||
|
|
||||||
// Check if user is admin using UserServiceInterface
|
// Check if user is admin using UserServiceInterface
|
||||||
boolean isAdmin = false;
|
boolean isAdmin = false;
|
||||||
if (userService != null) {
|
if (userService != null) {
|
||||||
@@ -245,6 +267,13 @@ public class ConfigController {
|
|||||||
// Premium/Enterprise settings
|
// Premium/Enterprise settings
|
||||||
configData.put("premiumEnabled", applicationProperties.getPremium().isEnabled());
|
configData.put("premiumEnabled", applicationProperties.getPremium().isEnabled());
|
||||||
|
|
||||||
|
// Timestamp TSA settings — single source of truth for presets + admin URLs
|
||||||
|
ApplicationProperties.Security.Timestamp tsConfig =
|
||||||
|
applicationProperties.getSecurity().getTimestamp();
|
||||||
|
configData.put("timestampDefaultTsaUrl", tsConfig.getDefaultTsaUrl());
|
||||||
|
configData.put("timestampCustomTsaUrls", tsConfig.getCustomTsaUrls());
|
||||||
|
configData.put("timestampTsaPresets", TimestampController.TSA_PRESETS);
|
||||||
|
|
||||||
// Server certificate settings
|
// Server certificate settings
|
||||||
configData.put(
|
configData.put(
|
||||||
"serverCertificateEnabled",
|
"serverCertificateEnabled",
|
||||||
|
|||||||
+73
-176
@@ -1,20 +1,14 @@
|
|||||||
package stirling.software.SPDF.controller.api.misc;
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.Image;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.RenderedImage;
|
import java.awt.image.RenderedImage;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.zip.Deflater;
|
import java.util.zip.Deflater;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
@@ -43,7 +37,6 @@ import stirling.software.common.annotations.api.MiscApi;
|
|||||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||||
import stirling.software.common.util.ExceptionUtils;
|
import stirling.software.common.util.ExceptionUtils;
|
||||||
import stirling.software.common.util.GeneralUtils;
|
import stirling.software.common.util.GeneralUtils;
|
||||||
import stirling.software.common.util.ImageProcessingUtils;
|
|
||||||
import stirling.software.common.util.TempFile;
|
import stirling.software.common.util.TempFile;
|
||||||
import stirling.software.common.util.TempFileManager;
|
import stirling.software.common.util.TempFileManager;
|
||||||
import stirling.software.common.util.WebResponseUtils;
|
import stirling.software.common.util.WebResponseUtils;
|
||||||
@@ -65,203 +58,107 @@ public class ExtractImagesController {
|
|||||||
+ " file. Users can specify the output image format. Input:PDF"
|
+ " file. Users can specify the output image format. Input:PDF"
|
||||||
+ " Output:IMAGE/ZIP Type:SIMO")
|
+ " Output:IMAGE/ZIP Type:SIMO")
|
||||||
public ResponseEntity<StreamingResponseBody> extractImages(
|
public ResponseEntity<StreamingResponseBody> extractImages(
|
||||||
@ModelAttribute PDFExtractImagesRequest request)
|
@ModelAttribute PDFExtractImagesRequest request) throws IOException {
|
||||||
throws IOException, InterruptedException, ExecutionException {
|
|
||||||
MultipartFile file = request.getFileInput();
|
MultipartFile file = request.getFileInput();
|
||||||
String format = request.getFormat();
|
String imageFormat = request.getFormat();
|
||||||
boolean allowDuplicates = Boolean.TRUE.equals(request.getAllowDuplicates());
|
|
||||||
|
|
||||||
String filename = GeneralUtils.removeExtension(file.getOriginalFilename());
|
String baseFilename = GeneralUtils.removeExtension(file.getOriginalFilename());
|
||||||
Set<byte[]> processedImages = new HashSet<>();
|
Set<Integer> processedImageHashes = new HashSet<>();
|
||||||
|
|
||||||
TempFile zipTempFile = new TempFile(tempFileManager, ".zip");
|
TempFile zipFile = new TempFile(tempFileManager, ".zip");
|
||||||
try (ZipOutputStream zos =
|
try (ZipOutputStream zipStream =
|
||||||
new ZipOutputStream(Files.newOutputStream(zipTempFile.getPath()));
|
new ZipOutputStream(Files.newOutputStream(zipFile.getPath()));
|
||||||
PDDocument document = pdfDocumentFactory.load(file)) {
|
PDDocument pdfDoc = pdfDocumentFactory.load(file)) {
|
||||||
|
|
||||||
// Set compression level
|
zipStream.setLevel(Deflater.BEST_COMPRESSION);
|
||||||
zos.setLevel(Deflater.BEST_COMPRESSION);
|
|
||||||
|
|
||||||
// Determine if multithreading should be used based on PDF size or number of pages
|
int totalPages = pdfDoc.getNumberOfPages();
|
||||||
boolean useMultithreading = shouldUseMultithreading(file, document);
|
for (int pageIndex = 0; pageIndex < totalPages; pageIndex++) {
|
||||||
|
PDPage currentPage = pdfDoc.getPage(pageIndex);
|
||||||
if (useMultithreading) {
|
extractAndAddImagesToZip(
|
||||||
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
|
currentPage,
|
||||||
Set<Future<Void>> futures = new HashSet<>();
|
imageFormat,
|
||||||
|
baseFilename,
|
||||||
try {
|
pageIndex + 1,
|
||||||
int pageCount = document.getPages().getCount();
|
processedImageHashes,
|
||||||
log.debug("Document reports {} pages", pageCount);
|
zipStream);
|
||||||
|
|
||||||
int consecutiveFailures = 0;
|
|
||||||
|
|
||||||
for (int pgNum = 0; pgNum < pageCount; pgNum++) {
|
|
||||||
try {
|
|
||||||
PDPage page = document.getPage(pgNum);
|
|
||||||
consecutiveFailures = 0; // Reset on success
|
|
||||||
final int currentPageNum =
|
|
||||||
pgNum + 1; // Convert to 1-based page numbering
|
|
||||||
Future<Void> future =
|
|
||||||
executor.submit(
|
|
||||||
() -> {
|
|
||||||
try {
|
|
||||||
// Call the image extraction method for each
|
|
||||||
// page
|
|
||||||
extractImagesFromPage(
|
|
||||||
page,
|
|
||||||
format,
|
|
||||||
filename,
|
|
||||||
currentPageNum,
|
|
||||||
processedImages,
|
|
||||||
zos,
|
|
||||||
allowDuplicates);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// Log the error and continue processing other
|
|
||||||
// pages
|
|
||||||
ExceptionUtils.logException(
|
|
||||||
"image extraction from page "
|
|
||||||
+ currentPageNum,
|
|
||||||
e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null; // Callable requires a return type
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the Future object to the list to track completion
|
|
||||||
futures.add(future);
|
|
||||||
} catch (Exception e) {
|
|
||||||
consecutiveFailures++;
|
|
||||||
ExceptionUtils.logException("page access for page " + (pgNum + 1), e);
|
|
||||||
|
|
||||||
if (consecutiveFailures >= 3) {
|
|
||||||
log.warn("Stopping page iteration after 3 consecutive failures");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
ExceptionUtils.logException("page count determination", e);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for all tasks to complete
|
|
||||||
for (Future<Void> future : futures) {
|
|
||||||
future.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close executor service
|
|
||||||
executor.shutdown();
|
|
||||||
} else {
|
|
||||||
// Single-threaded extraction
|
|
||||||
for (int pgNum = 0; pgNum < document.getPages().getCount(); pgNum++) {
|
|
||||||
PDPage page = document.getPage(pgNum);
|
|
||||||
extractImagesFromPage(
|
|
||||||
page,
|
|
||||||
format,
|
|
||||||
filename,
|
|
||||||
pgNum + 1,
|
|
||||||
processedImages,
|
|
||||||
zos,
|
|
||||||
allowDuplicates);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// document and zos closed by try-with-resources
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
zipTempFile.close();
|
zipFile.close();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
return WebResponseUtils.zipFileToWebResponse(
|
return WebResponseUtils.zipFileToWebResponse(
|
||||||
zipTempFile, filename + "_extracted-images.zip");
|
zipFile, baseFilename + "_extracted-images.zip");
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean shouldUseMultithreading(MultipartFile file, PDDocument document) {
|
private void extractAndAddImagesToZip(
|
||||||
// Criteria: Use multithreading if file size > 10MB or number of pages > 20
|
|
||||||
long fileSizeInMB = file.getSize() / (1024 * 1024);
|
|
||||||
int numberOfPages = document.getPages().getCount();
|
|
||||||
return fileSizeInMB > 10 || numberOfPages > 20;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void extractImagesFromPage(
|
|
||||||
PDPage page,
|
PDPage page,
|
||||||
String format,
|
String imageFormat,
|
||||||
String filename,
|
String baseFilename,
|
||||||
int pageNum,
|
int pageNumber,
|
||||||
Set<byte[]> processedImages,
|
Set<Integer> seenImageHashes,
|
||||||
ZipOutputStream zos,
|
ZipOutputStream zipOutput)
|
||||||
boolean allowDuplicates)
|
|
||||||
throws IOException {
|
throws IOException {
|
||||||
MessageDigest md;
|
|
||||||
try {
|
|
||||||
md = MessageDigest.getInstance("MD5");
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
log.error("MD5 algorithm not available for extractImages hash.", e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (page.getResources() == null || page.getResources().getXObjectNames() == null) {
|
if (page.getResources() == null || page.getResources().getXObjectNames() == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int count = 1;
|
|
||||||
for (COSName name : page.getResources().getXObjectNames()) {
|
int imageCount = 1;
|
||||||
|
for (COSName resourceName : page.getResources().getXObjectNames()) {
|
||||||
|
if (!page.getResources().isImageXObject(resourceName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (page.getResources().isImageXObject(name)) {
|
PDImageXObject imageObject =
|
||||||
PDImageXObject image = (PDImageXObject) page.getResources().getXObject(name);
|
(PDImageXObject) page.getResources().getXObject(resourceName);
|
||||||
if (!allowDuplicates) {
|
int imageHashCode = imageObject.hashCode();
|
||||||
byte[] data = ImageProcessingUtils.getImageData(image.getImage());
|
|
||||||
byte[] imageHash = md.digest(data);
|
|
||||||
synchronized (processedImages) {
|
|
||||||
if (processedImages.stream()
|
|
||||||
.anyMatch(hash -> Arrays.equals(hash, imageHash))) {
|
|
||||||
continue; // Skip already processed images
|
|
||||||
}
|
|
||||||
processedImages.add(imageHash);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderedImage renderedImage = image.getImage();
|
if (seenImageHashes.contains(imageHashCode)) {
|
||||||
|
continue;
|
||||||
// Convert to standard RGB colorspace if needed
|
|
||||||
BufferedImage bufferedImage = convertToRGB(renderedImage, format);
|
|
||||||
|
|
||||||
// Encode image outside the lock to allow parallel encoding across threads
|
|
||||||
String imageName = filename + "_page_" + pageNum + "_" + count++ + "." + format;
|
|
||||||
ByteArrayOutputStream imageBaos = new ByteArrayOutputStream();
|
|
||||||
ImageIO.write(bufferedImage, format, imageBaos);
|
|
||||||
byte[] imageData = imageBaos.toByteArray();
|
|
||||||
|
|
||||||
// Write encoded bytes to zip under lock (ZipOutputStream requires
|
|
||||||
// serialization)
|
|
||||||
synchronized (zos) {
|
|
||||||
zos.putNextEntry(new ZipEntry(imageName));
|
|
||||||
zos.write(imageData);
|
|
||||||
zos.closeEntry();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
seenImageHashes.add(imageHashCode);
|
||||||
|
|
||||||
|
RenderedImage sourceImage = imageObject.getImage();
|
||||||
|
BufferedImage convertedImage = convertImageToFormat(sourceImage, imageFormat);
|
||||||
|
|
||||||
|
String imagePath =
|
||||||
|
baseFilename
|
||||||
|
+ "_page_"
|
||||||
|
+ pageNumber
|
||||||
|
+ "_"
|
||||||
|
+ imageCount++
|
||||||
|
+ "."
|
||||||
|
+ imageFormat;
|
||||||
|
ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
|
||||||
|
ImageIO.write(convertedImage, imageFormat, imageBuffer);
|
||||||
|
|
||||||
|
zipOutput.putNextEntry(new ZipEntry(imagePath));
|
||||||
|
zipOutput.write(imageBuffer.toByteArray());
|
||||||
|
zipOutput.closeEntry();
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
ExceptionUtils.logException("image extraction", e);
|
ExceptionUtils.logException("image extraction failed", e);
|
||||||
throw ExceptionUtils.handlePdfException(e, "during image extraction");
|
throw ExceptionUtils.handlePdfException(e, "during image extraction");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private BufferedImage convertToRGB(RenderedImage renderedImage, String format) {
|
private BufferedImage convertImageToFormat(RenderedImage source, String format) {
|
||||||
int width = renderedImage.getWidth();
|
int width = source.getWidth();
|
||||||
int height = renderedImage.getHeight();
|
int height = source.getHeight();
|
||||||
BufferedImage rgbImage;
|
|
||||||
|
|
||||||
|
int imageType = BufferedImage.TYPE_INT_RGB;
|
||||||
if ("png".equalsIgnoreCase(format)) {
|
if ("png".equalsIgnoreCase(format)) {
|
||||||
rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
imageType = BufferedImage.TYPE_INT_ARGB;
|
||||||
} else if ("jpeg".equalsIgnoreCase(format) || "jpg".equalsIgnoreCase(format)) {
|
|
||||||
rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
||||||
} else if ("gif".equalsIgnoreCase(format)) {
|
|
||||||
rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED);
|
|
||||||
} else {
|
|
||||||
rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Graphics2D g = rgbImage.createGraphics();
|
BufferedImage result = new BufferedImage(width, height, imageType);
|
||||||
g.drawImage((Image) renderedImage, 0, 0, null);
|
Graphics2D graphics = result.createGraphics();
|
||||||
g.dispose();
|
graphics.drawImage((Image) source, 0, 0, null);
|
||||||
return rgbImage;
|
graphics.dispose();
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+136
@@ -0,0 +1,136 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.misc;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.cos.COSDictionary;
|
||||||
|
import org.apache.pdfbox.cos.COSName;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDResources;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import stirling.software.common.annotations.AutoJobPostMapping;
|
||||||
|
import stirling.software.common.annotations.api.GeneralApi;
|
||||||
|
import stirling.software.common.model.api.PDFFile;
|
||||||
|
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||||
|
import stirling.software.common.util.ExceptionUtils;
|
||||||
|
import stirling.software.common.util.GeneralUtils;
|
||||||
|
import stirling.software.common.util.WebResponseUtils;
|
||||||
|
|
||||||
|
@GeneralApi
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class RemoveImagesController {
|
||||||
|
|
||||||
|
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
||||||
|
|
||||||
|
@AutoJobPostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, value = "/remove-image-pdf")
|
||||||
|
@Operation(
|
||||||
|
summary = "Remove images from PDF",
|
||||||
|
description =
|
||||||
|
"This endpoint removes all embedded images from a PDF file and returns the"
|
||||||
|
+ " modified document. Input:PDF Output:PDF Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> removeImages(@ModelAttribute PDFFile request) throws IOException {
|
||||||
|
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
|
||||||
|
try (PDDocument pdfDoc = pdfDocumentFactory.load(request)) {
|
||||||
|
|
||||||
|
int totalPages = pdfDoc.getNumberOfPages();
|
||||||
|
int imagesRemoved = 0;
|
||||||
|
|
||||||
|
for (int pageIndex = 0; pageIndex < totalPages; pageIndex++) {
|
||||||
|
PDPage currentPage = pdfDoc.getPage(pageIndex);
|
||||||
|
imagesRemoved += removeImagesFromPage(currentPage);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Removed {} images from PDF with {} pages", imagesRemoved, totalPages);
|
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
pdfDoc.save(baos);
|
||||||
|
byte[] pdfContent = baos.toByteArray();
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
|
pdfContent,
|
||||||
|
GeneralUtils.generateFilename(
|
||||||
|
inputFile.getOriginalFilename(), "_images_removed.pdf"));
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw ExceptionUtils.handlePdfException(e, "during image removal");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int removeImagesFromPage(PDPage page) throws IOException {
|
||||||
|
int imagesRemoved = 0;
|
||||||
|
|
||||||
|
PDResources resources = page.getResources();
|
||||||
|
if (resources == null) {
|
||||||
|
return imagesRemoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
imagesRemoved += removeImagesFromResources(resources);
|
||||||
|
return imagesRemoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int removeImagesFromFormXObject(PDFormXObject formXObject) throws IOException {
|
||||||
|
PDResources resources = formXObject.getResources();
|
||||||
|
if (resources == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return removeImagesFromResources(resources);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int removeImagesFromResources(PDResources resources) throws IOException {
|
||||||
|
if (resources == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
COSDictionary xObjects = resources.getCOSObject().getCOSDictionary(COSName.XOBJECT);
|
||||||
|
if (xObjects == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int imagesRemoved = 0;
|
||||||
|
// Create snapshot to safely iterate while removing
|
||||||
|
List<COSName> names = new ArrayList<>(xObjects.keySet());
|
||||||
|
|
||||||
|
for (COSName name : names) {
|
||||||
|
try {
|
||||||
|
PDXObject xObject = resources.getXObject(name);
|
||||||
|
if (xObject == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove direct images
|
||||||
|
if (xObject instanceof PDImageXObject) {
|
||||||
|
xObjects.removeItem(name);
|
||||||
|
imagesRemoved++;
|
||||||
|
log.debug("Removed image: {}", name.getName());
|
||||||
|
}
|
||||||
|
// Recursively process nested form XObjects
|
||||||
|
else if (xObject instanceof PDFormXObject form) {
|
||||||
|
imagesRemoved += removeImagesFromResources(form.getResources());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("Error processing XObject {}: {}", name.getName(), e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return imagesRemoved;
|
||||||
|
}
|
||||||
|
}
|
||||||
+33
-4
@@ -483,9 +483,19 @@ public class StampController {
|
|||||||
y = overrideY;
|
y = overrideY;
|
||||||
} else {
|
} else {
|
||||||
x = calculatePositionX(pageSize, position, desiredPhysicalWidth, margin);
|
x = calculatePositionX(pageSize, position, desiredPhysicalWidth, margin);
|
||||||
y = calculatePositionY(pageSize, position, desiredPhysicalHeight, margin);
|
// drawImage() places the lower-left corner at (x, y); use image-specific Y logic
|
||||||
|
y = calculateImagePositionY(pageSize, position, desiredPhysicalHeight, margin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float llx = pageSize.getLowerLeftX();
|
||||||
|
float lly = pageSize.getLowerLeftY();
|
||||||
|
float urx = pageSize.getUpperRightX();
|
||||||
|
float ury = pageSize.getUpperRightY();
|
||||||
|
float xMax = Math.max(llx, urx - desiredPhysicalWidth);
|
||||||
|
float yMax = Math.max(lly, ury - desiredPhysicalHeight);
|
||||||
|
x = Math.min(xMax, Math.max(llx, x));
|
||||||
|
y = Math.min(yMax, Math.max(lly, y));
|
||||||
|
|
||||||
contentStream.saveGraphicsState();
|
contentStream.saveGraphicsState();
|
||||||
contentStream.transform(Matrix.getTranslateInstance(x, y));
|
contentStream.transform(Matrix.getTranslateInstance(x, y));
|
||||||
contentStream.transform(Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0));
|
contentStream.transform(Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0));
|
||||||
@@ -495,18 +505,37 @@ public class StampController {
|
|||||||
|
|
||||||
private float calculatePositionX(
|
private float calculatePositionX(
|
||||||
PDRectangle pageSize, int position, float contentWidth, float margin) {
|
PDRectangle pageSize, int position, float contentWidth, float margin) {
|
||||||
|
float llx = pageSize.getLowerLeftX();
|
||||||
|
float urx = pageSize.getUpperRightX();
|
||||||
return switch (position % 3) {
|
return switch (position % 3) {
|
||||||
case 1: // Left
|
case 1: // Left
|
||||||
yield pageSize.getLowerLeftX() + margin;
|
yield llx + margin;
|
||||||
case 2: // Center
|
case 2: // Center
|
||||||
yield (pageSize.getWidth() - contentWidth) / 2;
|
yield llx + (pageSize.getWidth() - contentWidth) / 2;
|
||||||
case 0: // Right
|
case 0: // Right
|
||||||
yield pageSize.getUpperRightX() - contentWidth - margin;
|
yield urx - contentWidth - margin;
|
||||||
default:
|
default:
|
||||||
yield 0;
|
yield 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private float calculateImagePositionY(
|
||||||
|
PDRectangle pageSize, int position, float imageHeight, float margin) {
|
||||||
|
float lly = pageSize.getLowerLeftY();
|
||||||
|
float pageHeight = pageSize.getHeight();
|
||||||
|
float ury = pageSize.getUpperRightY();
|
||||||
|
return switch ((position - 1) / 3) {
|
||||||
|
case 0: // Top - upper image edge flush below top margin
|
||||||
|
yield ury - margin - imageHeight;
|
||||||
|
case 1: // Middle - center image on page
|
||||||
|
yield lly + (pageHeight - imageHeight) / 2;
|
||||||
|
case 2: // Bottom - lower image edge at bottom margin
|
||||||
|
yield lly + margin;
|
||||||
|
default:
|
||||||
|
yield lly;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private float calculatePositionY(
|
private float calculatePositionY(
|
||||||
PDRectangle pageSize, int position, float height, float margin) {
|
PDRectangle pageSize, int position, float height, float margin) {
|
||||||
return switch ((position - 1) / 3) {
|
return switch ((position - 1) / 3) {
|
||||||
|
|||||||
+13
-12
@@ -36,7 +36,7 @@ import stirling.software.SPDF.model.PipelineResult;
|
|||||||
import stirling.software.SPDF.service.ApiDocService;
|
import stirling.software.SPDF.service.ApiDocService;
|
||||||
import stirling.software.common.configuration.RuntimePathConfig;
|
import stirling.software.common.configuration.RuntimePathConfig;
|
||||||
import stirling.software.common.service.PostHogService;
|
import stirling.software.common.service.PostHogService;
|
||||||
import stirling.software.common.util.FileMonitor;
|
import stirling.software.common.util.FileReadinessChecker;
|
||||||
|
|
||||||
import tools.jackson.databind.ObjectMapper;
|
import tools.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
@@ -50,8 +50,8 @@ public class PipelineDirectoryProcessor {
|
|||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final ApiDocService apiDocService;
|
private final ApiDocService apiDocService;
|
||||||
private final PipelineProcessor processor;
|
private final PipelineProcessor processor;
|
||||||
private final FileMonitor fileMonitor;
|
|
||||||
private final PostHogService postHogService;
|
private final PostHogService postHogService;
|
||||||
|
private final FileReadinessChecker fileReadinessChecker;
|
||||||
private final List<String> watchedFoldersDirs;
|
private final List<String> watchedFoldersDirs;
|
||||||
private final String finishedFoldersDir;
|
private final String finishedFoldersDir;
|
||||||
|
|
||||||
@@ -63,14 +63,14 @@ public class PipelineDirectoryProcessor {
|
|||||||
ObjectMapper objectMapper,
|
ObjectMapper objectMapper,
|
||||||
ApiDocService apiDocService,
|
ApiDocService apiDocService,
|
||||||
PipelineProcessor processor,
|
PipelineProcessor processor,
|
||||||
FileMonitor fileMonitor,
|
|
||||||
PostHogService postHogService,
|
PostHogService postHogService,
|
||||||
|
FileReadinessChecker fileReadinessChecker,
|
||||||
RuntimePathConfig runtimePathConfig) {
|
RuntimePathConfig runtimePathConfig) {
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
this.apiDocService = apiDocService;
|
this.apiDocService = apiDocService;
|
||||||
this.processor = processor;
|
this.processor = processor;
|
||||||
this.fileMonitor = fileMonitor;
|
|
||||||
this.postHogService = postHogService;
|
this.postHogService = postHogService;
|
||||||
|
this.fileReadinessChecker = fileReadinessChecker;
|
||||||
this.watchedFoldersDirs = runtimePathConfig.getPipelineWatchedFoldersPaths();
|
this.watchedFoldersDirs = runtimePathConfig.getPipelineWatchedFoldersPaths();
|
||||||
this.finishedFoldersDir = runtimePathConfig.getPipelineFinishedFoldersPath();
|
this.finishedFoldersDir = runtimePathConfig.getPipelineFinishedFoldersPath();
|
||||||
}
|
}
|
||||||
@@ -273,19 +273,20 @@ public class PipelineDirectoryProcessor {
|
|||||||
}
|
}
|
||||||
return isAllowed;
|
return isAllowed;
|
||||||
})
|
})
|
||||||
.map(Path::toAbsolutePath)
|
|
||||||
.filter(
|
.filter(
|
||||||
path -> {
|
path -> {
|
||||||
boolean isReady =
|
if (!fileReadinessChecker.isReady(path)) {
|
||||||
fileMonitor.isFileReadyForProcessing(path);
|
|
||||||
if (!isReady) {
|
|
||||||
log.info(
|
log.info(
|
||||||
"File not ready for processing (locked/created"
|
"File '{}' is not yet ready for processing"
|
||||||
+ " last 5s): {}",
|
+ " (still being written or locked),"
|
||||||
path);
|
+ " will retry on next scan cycle",
|
||||||
|
path.getFileName());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return isReady;
|
return true;
|
||||||
})
|
})
|
||||||
|
.map(Path::toAbsolutePath)
|
||||||
|
.filter(path -> true)
|
||||||
.map(Path::toFile)
|
.map(Path::toFile)
|
||||||
.toArray(File[]::new);
|
.toArray(File[]::new);
|
||||||
log.info(
|
log.info(
|
||||||
|
|||||||
+32
@@ -84,8 +84,39 @@ public class PipelineProcessor {
|
|||||||
return name.substring(0, underscoreIndex) + extension;
|
return name.substring(0, underscoreIndex) + extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allowlist of URL path prefixes permitted through the pipeline.
|
||||||
|
private static final List<String> ALLOWED_PIPELINE_PATH_PREFIXES =
|
||||||
|
List.of(
|
||||||
|
"/api/v1/general/",
|
||||||
|
"/api/v1/misc/",
|
||||||
|
"/api/v1/security/",
|
||||||
|
"/api/v1/convert/",
|
||||||
|
"/api/v1/filter/");
|
||||||
|
|
||||||
|
private void validatePipelineUrl(String url) {
|
||||||
|
// Strip scheme+host to get the path portion for comparison
|
||||||
|
String path = url;
|
||||||
|
int schemeEnd = url.indexOf("://");
|
||||||
|
if (schemeEnd != -1) {
|
||||||
|
int pathStart = url.indexOf('/', schemeEnd + 3);
|
||||||
|
path = pathStart != -1 ? url.substring(pathStart) : "/";
|
||||||
|
}
|
||||||
|
final String pathToCheck = path;
|
||||||
|
boolean allowed = ALLOWED_PIPELINE_PATH_PREFIXES.stream().anyMatch(pathToCheck::contains);
|
||||||
|
if (!allowed) {
|
||||||
|
log.warn("Blocked pipeline request to disallowed URL: {}", url);
|
||||||
|
throw new SecurityException(
|
||||||
|
"Pipeline operation not permitted for endpoint: " + pathToCheck);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String getApiKeyForUser() {
|
private String getApiKeyForUser() {
|
||||||
if (userService == null) return "";
|
if (userService == null) return "";
|
||||||
|
String username = userService.getCurrentUsername();
|
||||||
|
if (username != null && !username.equals("anonymousUser")) {
|
||||||
|
return userService.getApiKeyForUser(username);
|
||||||
|
}
|
||||||
|
// Scheduled/internal context — no user in security context
|
||||||
return userService.getApiKeyForUser(Role.INTERNAL_API_USER.getRoleId());
|
return userService.getApiKeyForUser(Role.INTERNAL_API_USER.getRoleId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,6 +314,7 @@ public class PipelineProcessor {
|
|||||||
|
|
||||||
/* package */ ResponseEntity<Resource> sendWebRequest(
|
/* package */ ResponseEntity<Resource> sendWebRequest(
|
||||||
String url, MultiValueMap<String, Object> body) {
|
String url, MultiValueMap<String, Object> body) {
|
||||||
|
validatePipelineUrl(url);
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
// Set up headers, including API key
|
// Set up headers, including API key
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
|||||||
+2
-2
@@ -113,7 +113,7 @@ public class CertSignController {
|
|||||||
this.serverCertificateService = serverCertificateService;
|
this.serverCertificateService = serverCertificateService;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sign(
|
public static void sign(
|
||||||
CustomPDFDocumentFactory pdfDocumentFactory,
|
CustomPDFDocumentFactory pdfDocumentFactory,
|
||||||
MultipartFile input,
|
MultipartFile input,
|
||||||
OutputStream output,
|
OutputStream output,
|
||||||
@@ -304,7 +304,7 @@ public class CertSignController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CreateSignature extends CreateSignatureBase {
|
public static class CreateSignature extends CreateSignatureBase {
|
||||||
File logoFile;
|
File logoFile;
|
||||||
|
|
||||||
public CreateSignature(KeyStore keystore, char[] pin)
|
public CreateSignature(KeyStore keystore, char[] pin)
|
||||||
|
|||||||
+264
@@ -0,0 +1,264 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.security;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.security.Security;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.cos.COSName;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
|
||||||
|
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
||||||
|
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
|
||||||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
|
import org.bouncycastle.tsp.TimeStampRequest;
|
||||||
|
import org.bouncycastle.tsp.TimeStampRequestGenerator;
|
||||||
|
import org.bouncycastle.tsp.TimeStampResponse;
|
||||||
|
import org.bouncycastle.tsp.TimeStampToken;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import stirling.software.SPDF.config.swagger.StandardPdfResponse;
|
||||||
|
import stirling.software.SPDF.model.api.security.TimestampPdfRequest;
|
||||||
|
import stirling.software.common.annotations.AutoJobPostMapping;
|
||||||
|
import stirling.software.common.annotations.api.SecurityApi;
|
||||||
|
import stirling.software.common.model.ApplicationProperties;
|
||||||
|
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||||
|
import stirling.software.common.util.GeneralUtils;
|
||||||
|
import stirling.software.common.util.WebResponseUtils;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@SecurityApi
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TimestampController {
|
||||||
|
|
||||||
|
static {
|
||||||
|
Security.addProvider(new BouncyCastleProvider());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Built-in TSA presets with labels — single source of truth for backend + frontend. */
|
||||||
|
public static final List<Map<String, String>> TSA_PRESETS =
|
||||||
|
List.of(
|
||||||
|
Map.of("label", "DigiCert", "url", "http://timestamp.digicert.com"),
|
||||||
|
Map.of("label", "Sectigo", "url", "http://timestamp.sectigo.com"),
|
||||||
|
Map.of("label", "SSL.com", "url", "http://ts.ssl.com"),
|
||||||
|
Map.of("label", "FreeTSA", "url", "https://freetsa.org/tsr"),
|
||||||
|
Map.of("label", "MeSign", "url", "http://tsa.mesign.com"));
|
||||||
|
|
||||||
|
private static final Set<String> ALLOWED_TSA_PRESET_URLS =
|
||||||
|
TSA_PRESETS.stream().map(p -> p.get("url")).collect(Collectors.toUnmodifiableSet());
|
||||||
|
|
||||||
|
private static final int MAX_TSA_RESPONSE_SIZE = 1024 * 1024; // 1 MB
|
||||||
|
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
|
||||||
|
|
||||||
|
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
||||||
|
private final ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
|
@AutoJobPostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, value = "/timestamp-pdf")
|
||||||
|
@StandardPdfResponse
|
||||||
|
@Operation(
|
||||||
|
summary = "Add RFC 3161 document timestamp to a PDF",
|
||||||
|
description =
|
||||||
|
"Contacts a trusted Time Stamp Authority (TSA) server and embeds an RFC 3161"
|
||||||
|
+ " document timestamp into the PDF. Only a SHA-256 hash of the"
|
||||||
|
+ " document is sent to the TSA — the PDF itself never leaves the"
|
||||||
|
+ " server. Input:PDF Output:PDF Type:SISO")
|
||||||
|
public ResponseEntity<byte[]> timestampPdf(@ModelAttribute TimestampPdfRequest request)
|
||||||
|
throws Exception {
|
||||||
|
MultipartFile inputFile = request.getFileInput();
|
||||||
|
ApplicationProperties.Security.Timestamp tsConfig =
|
||||||
|
applicationProperties.getSecurity().getTimestamp();
|
||||||
|
|
||||||
|
// Determine effective TSA URL: use request value if provided, otherwise config default
|
||||||
|
String tsaUrl =
|
||||||
|
(request.getTsaUrl() != null && !request.getTsaUrl().isBlank())
|
||||||
|
? request.getTsaUrl()
|
||||||
|
: tsConfig.getDefaultTsaUrl();
|
||||||
|
|
||||||
|
// Build allowed set: built-in presets + admin-configured custom URLs
|
||||||
|
// Filter null/blank entries and validate protocol (TASK-6)
|
||||||
|
Set<String> allowedUrls = new HashSet<>(ALLOWED_TSA_PRESET_URLS);
|
||||||
|
if (tsConfig.getDefaultTsaUrl() != null
|
||||||
|
&& !tsConfig.getDefaultTsaUrl().isBlank()
|
||||||
|
&& isValidTsaUrlProtocol(tsConfig.getDefaultTsaUrl())) {
|
||||||
|
allowedUrls.add(tsConfig.getDefaultTsaUrl());
|
||||||
|
}
|
||||||
|
List<String> customUrls = tsConfig.getCustomTsaUrls();
|
||||||
|
if (customUrls != null) {
|
||||||
|
customUrls.stream()
|
||||||
|
.filter(u -> u != null && !u.isBlank() && isValidTsaUrlProtocol(u))
|
||||||
|
.forEach(allowedUrls::add);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize for case-insensitive comparison (TASK-12)
|
||||||
|
Set<String> normalizedAllowed =
|
||||||
|
allowedUrls.stream()
|
||||||
|
.map(TimestampController::normalizeTsaUrl)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
|
// Validate TSA URL against allowed set to prevent SSRF
|
||||||
|
if (!normalizedAllowed.contains(normalizeTsaUrl(tsaUrl))) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"TSA URL is not in the allowed list. Contact your administrator to add it"
|
||||||
|
+ " via settings.yml (security.timestamp.customTsaUrls).");
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
try (PDDocument document = pdfDocumentFactory.load(inputFile)) {
|
||||||
|
PDSignature signature = new PDSignature();
|
||||||
|
signature.setType(COSName.DOC_TIME_STAMP);
|
||||||
|
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
|
||||||
|
signature.setSubFilter(COSName.getPDFName("ETSI.RFC3161"));
|
||||||
|
signature.setSignDate(Calendar.getInstance());
|
||||||
|
|
||||||
|
document.addSignature(signature, content -> requestTimestampToken(content, tsaUrl));
|
||||||
|
|
||||||
|
document.saveIncremental(outputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
|
outputStream.toByteArray(),
|
||||||
|
GeneralUtils.generateFilename(inputFile.getOriginalFilename(), "_timestamped.pdf"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] requestTimestampToken(InputStream content, String tsaUrl) throws IOException {
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
try {
|
||||||
|
// Hash the PDF content byte range with SHA-256
|
||||||
|
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
int read;
|
||||||
|
while ((read = content.read(buffer)) != -1) {
|
||||||
|
digest.update(buffer, 0, read);
|
||||||
|
}
|
||||||
|
byte[] hash = digest.digest();
|
||||||
|
|
||||||
|
// Build the RFC 3161 timestamp request
|
||||||
|
TimeStampRequestGenerator generator = new TimeStampRequestGenerator();
|
||||||
|
generator.setCertReq(true);
|
||||||
|
BigInteger nonce = BigInteger.valueOf(SECURE_RANDOM.nextLong() & Long.MAX_VALUE);
|
||||||
|
ASN1ObjectIdentifier digestAlgorithm = NISTObjectIdentifiers.id_sha256;
|
||||||
|
TimeStampRequest tsaRequest = generator.generate(digestAlgorithm, hash, nonce);
|
||||||
|
byte[] requestBytes = tsaRequest.getEncoded();
|
||||||
|
|
||||||
|
// Contact the TSA server (redirects disabled to prevent SSRF via redirect)
|
||||||
|
connection = (HttpURLConnection) URI.create(tsaUrl).toURL().openConnection();
|
||||||
|
connection.setInstanceFollowRedirects(false);
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("Content-Type", "application/timestamp-query");
|
||||||
|
connection.setRequestProperty("Content-Length", String.valueOf(requestBytes.length));
|
||||||
|
connection.setConnectTimeout(30_000);
|
||||||
|
connection.setReadTimeout(30_000);
|
||||||
|
|
||||||
|
try (OutputStream out = connection.getOutputStream()) {
|
||||||
|
out.write(requestBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||||||
|
// Read error stream for debugging (TASK-5)
|
||||||
|
String errorBody = readErrorStream(connection);
|
||||||
|
throw new IOException(
|
||||||
|
"TSA server returned HTTP "
|
||||||
|
+ responseCode
|
||||||
|
+ " for URL: "
|
||||||
|
+ tsaUrl
|
||||||
|
+ (errorBody.isEmpty() ? "" : " — " + errorBody));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read response with size limit to prevent OOM (TASK-4)
|
||||||
|
byte[] responseBytes;
|
||||||
|
try (InputStream in = connection.getInputStream()) {
|
||||||
|
responseBytes = in.readNBytes(MAX_TSA_RESPONSE_SIZE);
|
||||||
|
if (in.read() != -1) {
|
||||||
|
throw new IOException(
|
||||||
|
"TSA response exceeds maximum allowed size of "
|
||||||
|
+ MAX_TSA_RESPONSE_SIZE
|
||||||
|
+ " bytes");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse and validate the TSA response
|
||||||
|
TimeStampResponse tsaResponse = new TimeStampResponse(responseBytes);
|
||||||
|
tsaResponse.validate(tsaRequest);
|
||||||
|
|
||||||
|
TimeStampToken token = tsaResponse.getTimeStampToken();
|
||||||
|
if (token == null) {
|
||||||
|
throw new IOException(
|
||||||
|
"TSA server did not return a timestamp token. Status: "
|
||||||
|
+ tsaResponse.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info(
|
||||||
|
"RFC 3161 timestamp obtained from {} at {}",
|
||||||
|
tsaUrl,
|
||||||
|
token.getTimeStampInfo().getGenTime());
|
||||||
|
|
||||||
|
return token.getEncoded();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException(
|
||||||
|
"Failed to obtain RFC 3161 timestamp from " + tsaUrl + ": " + e.getMessage(),
|
||||||
|
e);
|
||||||
|
} finally {
|
||||||
|
// Always disconnect to release the underlying socket (TASK-1)
|
||||||
|
if (connection != null) {
|
||||||
|
connection.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isValidTsaUrlProtocol(String url) {
|
||||||
|
String lower = url.toLowerCase(Locale.ROOT);
|
||||||
|
return lower.startsWith("http://") || lower.startsWith("https://");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String normalizeTsaUrl(String url) {
|
||||||
|
try {
|
||||||
|
URI uri = URI.create(url.trim());
|
||||||
|
String scheme = uri.getScheme() == null ? "" : uri.getScheme().toLowerCase(Locale.ROOT);
|
||||||
|
String host = uri.getHost() == null ? "" : uri.getHost().toLowerCase(Locale.ROOT);
|
||||||
|
int port = uri.getPort();
|
||||||
|
String path = uri.getPath() == null ? "" : uri.getPath();
|
||||||
|
return scheme + "://" + host + (port == -1 ? "" : ":" + port) + path;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return url.toLowerCase(Locale.ROOT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String readErrorStream(HttpURLConnection connection) {
|
||||||
|
try (InputStream err = connection.getErrorStream()) {
|
||||||
|
if (err == null) return "";
|
||||||
|
byte[] body = err.readNBytes(2048);
|
||||||
|
return new String(body, StandardCharsets.UTF_8).trim();
|
||||||
|
} catch (IOException e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user