mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2026-09-03 05:10:16 +03:00
Compare commits
49
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cbc8af1951 | ||
|
|
aca0e40c37 | ||
|
|
1b2a3118a6 | ||
|
|
3056e5ff44 | ||
|
|
798ba57f0b | ||
|
|
42bdce155c | ||
|
|
2cf355c5cd | ||
|
|
c57a2a45de | ||
|
|
d30faf246b | ||
|
|
f6661a8f87 | ||
|
|
ceeec53df4 | ||
|
|
4ef2e3811c | ||
|
|
31d52d4c32 | ||
|
|
d55d8acbfa | ||
|
|
af97e1b27b | ||
|
|
01c908e95d | ||
|
|
0920ea9493 | ||
|
|
96207a7304 | ||
|
|
d93049db9f | ||
|
|
54e839ae65 | ||
|
|
3aca7a26f6 | ||
|
|
5fe7df3933 | ||
|
|
b1e857fd01 | ||
|
|
b92f88361e | ||
|
|
f6124223e4 | ||
|
|
3235645203 | ||
|
|
1f4cc2612d | ||
|
|
e539eb1ab1 | ||
|
|
1bb6961414 | ||
|
|
34694c6f5e | ||
|
|
0b7b4e02c2 | ||
|
|
74be5bf0ad | ||
|
|
8c00fffe18 | ||
|
|
c5da4177c4 | ||
|
|
ddc0ac0ced | ||
|
|
8bdd00b2fa | ||
|
|
3718af45ff | ||
|
|
41cbd97b48 | ||
|
|
993adaa3cd | ||
|
|
ead8a536d2 | ||
|
|
c22d9ecf58 | ||
|
|
d3708c1e63 | ||
|
|
1c055f3d18 | ||
|
|
4ab2505a6c | ||
|
|
658aa54c20 | ||
|
|
0a3f0c1814 | ||
|
|
849d616451 | ||
|
|
a48356a2d2 | ||
|
|
f71b0247da |
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/claude-code-settings.json",
|
||||
"hooks": {
|
||||
"Stop": [
|
||||
{
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node",
|
||||
"args": [
|
||||
"${CLAUDE_PROJECT_DIR}/scripts/lint/comment-lint-hook.mjs"
|
||||
],
|
||||
"timeout": 60
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ Closes #(issue_number)
|
||||
- [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable)
|
||||
- [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable)
|
||||
- [ ] I have performed a self-review of my own code
|
||||
- [ ] Every comment I added says something the code does not ([guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/CODE_COMMENTS.md))
|
||||
- [ ] My changes generate no new warnings
|
||||
|
||||
### Documentation
|
||||
|
||||
@@ -182,7 +182,7 @@ jobs:
|
||||
fetch-depth: 0 # Fetch full history for commit hash detection
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
|
||||
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
|
||||
|
||||
- name: Get version number
|
||||
id: versionNumber
|
||||
@@ -220,6 +220,42 @@ jobs:
|
||||
echo "app_short=${APP_HASH:0:8}" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
# The Stirling account previews connect to. Derived from the ref rather than stored as a URL
|
||||
# so it cannot drift from the key: a mismatched pair is accepted by the browser and rejected
|
||||
# by Supabase, surfacing much later as "session expired" on Usage rather than at sign-in.
|
||||
# Secret only to match Saas-Dev-Deploy.yml, which owns the same value; a project ref is not
|
||||
# itself sensitive, which is why SAAS_API_BASE_URL next to it is a plain variable.
|
||||
- name: Resolve Stirling account config
|
||||
id: saas
|
||||
env:
|
||||
PROJECT_REF: ${{ secrets.SAAS_DB_PROJECT_REF }}
|
||||
API_BASE_OVERRIDE: ${{ vars.SAAS_API_BASE_URL }}
|
||||
run: |
|
||||
# Set, this is the one value both halves use: the browser's portal reads and the backend's
|
||||
# register/entitlement calls have to land on the same SaaS, and nothing checks that they
|
||||
# do. Unset, only the backend gets a base, from its own compiled-in default.
|
||||
API_BASE="${API_BASE_OVERRIDE:-https://stirling.com/app}"
|
||||
echo "backend_base=${API_BASE}" >> "$GITHUB_OUTPUT"
|
||||
|
||||
if [ -z "${PROJECT_REF}" ]; then
|
||||
echo "Not configured for this environment: the preview will build without a Stirling"
|
||||
echo "account, and the connect dialog will say so. To wire one up, set on the"
|
||||
echo "pr-preview environment the secrets SAAS_DB_PROJECT_REF and"
|
||||
echo "SAAS_SUPABASE_PUBLISHABLE_KEY, both from the same Supabase project."
|
||||
echo "supabase_url=" >> "$GITHUB_OUTPUT"
|
||||
echo "frontend_base=" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
# Only whether, not which: the ref is a secret here, so Actions masks it out of any
|
||||
# line it appears in, derived URL included.
|
||||
echo "Stirling account configured, at ${API_BASE}."
|
||||
echo "supabase_url=https://${PROJECT_REF}.supabase.co" >> "$GITHUB_OUTPUT"
|
||||
# Deliberately the override and not API_BASE: the backend's default is a subpath URL
|
||||
# nobody has confirmed answers /api/v1, and prod CORS does not list preview hostnames,
|
||||
# so portal reads stay off until someone sets a base they have checked. Empty leaves the
|
||||
# committed .env default alone, which is the clean "not configured" state.
|
||||
echo "frontend_base=${API_BASE_OVERRIDE}" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
- name: Check if image exists
|
||||
id: check-image
|
||||
run: |
|
||||
@@ -246,6 +282,9 @@ jobs:
|
||||
build-args: |
|
||||
VERSION_TAG=v2-alpha
|
||||
BUILD_PORTAL=${{ env.BUILD_PORTAL }}
|
||||
VITE_SUPABASE_URL=${{ steps.saas.outputs.supabase_url }}
|
||||
VITE_SUPABASE_PUBLISHABLE_DEFAULT_KEY=${{ secrets.SAAS_SUPABASE_PUBLISHABLE_KEY }}
|
||||
VITE_SAAS_API_URL=${{ steps.saas.outputs.frontend_base }}
|
||||
platforms: linux/amd64
|
||||
|
||||
- name: Set up SSH
|
||||
@@ -279,6 +318,13 @@ jobs:
|
||||
environment:
|
||||
DISABLE_ADDITIONAL_FEATURES: "false"
|
||||
STIRLING_BILLING_ACCOUNT_LINK_ENABLED: "true"
|
||||
STIRLING_BILLING_ACCOUNT_LINK_SAAS_BASE_URL: "${{ steps.saas.outputs.backend_base }}"
|
||||
# Off so preview traffic never accrues against a real wallet or trips its cap. The
|
||||
# 402 gate is separate and stays on, so gating is still testable here.
|
||||
STIRLING_BILLING_ACCOUNT_LINK_METERING_ENABLED: "false"
|
||||
# Stated rather than inferred from the request: the callback has to come back to the
|
||||
# preview hostname, not to the container's own :8080 behind this proxy.
|
||||
SYSTEM_FRONTENDURL: "https://${V2_PORT}.ssl.stirlingpdf.cloud"
|
||||
SECURITY_ENABLELOGIN: "true"
|
||||
SECURITY_INITIALLOGIN_USERNAME: "${TEST_LOGIN_USERNAME}"
|
||||
SECURITY_INITIALLOGIN_PASSWORD: "${TEST_LOGIN_PASSWORD}"
|
||||
@@ -353,7 +399,7 @@ jobs:
|
||||
|
||||
- name: Install Task for Storybook
|
||||
if: steps.sb-changes.outputs.storybook == 'true'
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Build and deploy Storybook
|
||||
id: storybook
|
||||
|
||||
@@ -206,7 +206,7 @@ jobs:
|
||||
distribution: "temurin"
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Run Gradle Command
|
||||
run: |
|
||||
if [ "${{ needs.check-comment.outputs.disable_security }}" == "true" ]; then
|
||||
@@ -222,7 +222,7 @@ jobs:
|
||||
STIRLING_PDF_DESKTOP_UI: false
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
|
||||
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
|
||||
|
||||
- name: Login to GitHub Container Registry
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
name: Auto SaaS Dev Deployment
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- saas-prod
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
FRONTEND_PORT: "901"
|
||||
BACKEND_PORT: "902"
|
||||
DEPLOY_DIR: /stirling/SAAS-DEV
|
||||
|
||||
jobs:
|
||||
deploy-saas-dev:
|
||||
runs-on: ubuntu-latest
|
||||
environment: saas-dev
|
||||
concurrency:
|
||||
group: saas-dev-deploy
|
||||
cancel-in-progress: true
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Harden Runner
|
||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
||||
with:
|
||||
egress-policy: audit
|
||||
|
||||
- name: Check SaaS configuration
|
||||
id: config
|
||||
env:
|
||||
PROJECT_REF: ${{ secrets.SAAS_DB_PROJECT_REF }}
|
||||
run: |
|
||||
echo "supabase_url=https://${PROJECT_REF}.supabase.co" >> "$GITHUB_OUTPUT"
|
||||
echo "meter_endpoint=https://${PROJECT_REF}.supabase.co/functions/v1/meter-payg-units" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
|
||||
|
||||
- name: Login to GitHub Container Registry
|
||||
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.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: Get commit hash
|
||||
id: commit-hash
|
||||
run: echo "app_short=$(git rev-parse --short=8 HEAD)" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Build and push backend image
|
||||
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
|
||||
with:
|
||||
context: .
|
||||
file: ./docker/backend/Dockerfile
|
||||
push: true
|
||||
cache-from: type=gha,scope=stirling-saas-backend
|
||||
cache-to: type=gha,mode=max,scope=stirling-saas-backend
|
||||
tags: |
|
||||
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/stirling-pdf-test:saas-backend-${{ steps.commit-hash.outputs.app_short }}
|
||||
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/stirling-pdf-test:saas-backend-latest
|
||||
build-args: |
|
||||
VERSION_TAG=v2-alpha
|
||||
STIRLING_FLAVOR=saas
|
||||
platforms: linux/amd64
|
||||
|
||||
- name: Build and push frontend image
|
||||
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
|
||||
with:
|
||||
context: .
|
||||
file: ./docker/frontend/Dockerfile
|
||||
push: true
|
||||
cache-from: type=gha,scope=stirling-saas-frontend
|
||||
cache-to: type=gha,mode=max,scope=stirling-saas-frontend
|
||||
tags: |
|
||||
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/stirling-pdf-test:saas-frontend-${{ steps.commit-hash.outputs.app_short }}
|
||||
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/stirling-pdf-test:saas-frontend-latest
|
||||
build-args: |
|
||||
VERSION_TAG=v2-alpha
|
||||
STIRLING_FLAVOR=saas
|
||||
VITE_BUILD_MODE=development
|
||||
VITE_SUPABASE_URL=${{ steps.config.outputs.supabase_url }}
|
||||
VITE_SUPABASE_PUBLISHABLE_DEFAULT_KEY=${{ secrets.SAAS_SUPABASE_PUBLISHABLE_KEY }}
|
||||
platforms: linux/amd64
|
||||
|
||||
- name: Build and push AI engine image
|
||||
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
|
||||
with:
|
||||
context: .
|
||||
file: ./engine/Dockerfile
|
||||
push: true
|
||||
cache-from: type=gha,scope=stirling-saas-engine
|
||||
cache-to: type=gha,mode=max,scope=stirling-saas-engine
|
||||
tags: |
|
||||
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/stirling-pdf-test:saas-engine-${{ steps.commit-hash.outputs.app_short }}
|
||||
ghcr.io/${{ steps.repoowner.outputs.lowercase }}/stirling-pdf-test:saas-engine-latest
|
||||
platforms: linux/amd64
|
||||
|
||||
- name: Set up SSH
|
||||
env:
|
||||
SSH_KEY: ${{ secrets.NEW_VPS_SSH_KEY }}
|
||||
run: |
|
||||
mkdir -p ~/.ssh/
|
||||
echo "$SSH_KEY" > ../private.key
|
||||
sudo chmod 600 ../private.key
|
||||
|
||||
- name: Deploy to VPS
|
||||
env:
|
||||
IMAGE_BASE: ghcr.io/${{ steps.repoowner.outputs.lowercase }}/stirling-pdf-test
|
||||
IMAGE_TAG: ${{ steps.commit-hash.outputs.app_short }}
|
||||
GHCR_USER: ${{ github.actor }}
|
||||
GHCR_TOKEN: ${{ github.token }}
|
||||
VPS_USERNAME: ${{ secrets.NEW_VPS_USERNAME }}
|
||||
VPS_HOST: ${{ secrets.NEW_VPS_HOST }}
|
||||
SAAS_DB_URL: ${{ secrets.SAAS_DB_URL }}
|
||||
SAAS_DB_USERNAME: ${{ secrets.SAAS_DB_USERNAME || 'postgres' }}
|
||||
SAAS_DB_PASSWORD: ${{ secrets.SAAS_DB_PASSWORD }}
|
||||
SAAS_DB_PROJECT_REF: ${{ secrets.SAAS_DB_PROJECT_REF }}
|
||||
SUPABASE_EDGE_FUNCTION_SECRET: ${{ secrets.SUPABASE_EDGE_FUNCTION_SECRET }}
|
||||
PAYG_METER_ENDPOINT: ${{ steps.config.outputs.meter_endpoint }}
|
||||
STIRLING_KEYGEN_ENABLED: ${{ secrets.KEYGEN_ACCOUNT_ID != '' && secrets.KEYGEN_API_TOKEN != '' && secrets.KEYGEN_POLICY_ID != '' }}
|
||||
KEYGEN_ACCOUNT_ID: ${{ secrets.KEYGEN_ACCOUNT_ID }}
|
||||
KEYGEN_API_TOKEN: ${{ secrets.KEYGEN_API_TOKEN }}
|
||||
KEYGEN_POLICY_ID: ${{ secrets.KEYGEN_POLICY_ID }}
|
||||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
BASE_URL="http://${VPS_HOST}:${FRONTEND_PORT}"
|
||||
|
||||
yaml() {
|
||||
printf "'%s'" "$(printf '%s' "$1" | sed -e "s/'/''/g" -e 's/\$/$$/g')"
|
||||
}
|
||||
|
||||
ENGINE_SECRET="$(openssl rand -hex 32)"
|
||||
AI_BACKEND_VARS="
|
||||
SYSTEM_AIENGINE_ENABLED: \"true\"
|
||||
SYSTEM_AIENGINE_URL: \"http://saas-engine:5001\"
|
||||
APP_AI_SERVICEBASEURL: \"http://saas-engine:5001\"
|
||||
STIRLING_ENGINE_SHARED_SECRET: $(yaml "$ENGINE_SECRET")"
|
||||
AI_SERVICE="
|
||||
|
||||
saas-engine:
|
||||
container_name: stirling-saas-dev-engine
|
||||
image: ${IMAGE_BASE}:saas-engine-${IMAGE_TAG}
|
||||
environment:
|
||||
ANTHROPIC_API_KEY: $(yaml "$ANTHROPIC_API_KEY")
|
||||
VOYAGE_API_KEY: $(yaml "$VOYAGE_API_KEY")
|
||||
STIRLING_ENGINE_SHARED_SECRET: $(yaml "$ENGINE_SECRET")
|
||||
restart: on-failure:5"
|
||||
|
||||
cat > docker-compose.yml << EOF
|
||||
version: '3.3'
|
||||
services:
|
||||
saas-backend:
|
||||
container_name: stirling-saas-dev-backend
|
||||
image: ${IMAGE_BASE}:saas-backend-${IMAGE_TAG}
|
||||
ports:
|
||||
- "${BACKEND_PORT}:8080"
|
||||
volumes:
|
||||
- ${DEPLOY_DIR}/config:/configs:rw
|
||||
- ${DEPLOY_DIR}/logs:/logs:rw
|
||||
- ${DEPLOY_DIR}/storage:/storage:rw
|
||||
environment:
|
||||
SPRING_PROFILES_ACTIVE: "saas"
|
||||
DISABLE_ADDITIONAL_FEATURES: "false"
|
||||
SAAS_DB_URL: $(yaml "$SAAS_DB_URL")
|
||||
SAAS_DB_USERNAME: $(yaml "$SAAS_DB_USERNAME")
|
||||
SAAS_DB_PASSWORD: $(yaml "$SAAS_DB_PASSWORD")
|
||||
SAAS_DB_PROJECT_REF: $(yaml "$SAAS_DB_PROJECT_REF")
|
||||
SUPABASE_EDGE_FUNCTION_SECRET: $(yaml "$SUPABASE_EDGE_FUNCTION_SECRET")
|
||||
PAYG_METER_ENDPOINT: $(yaml "$PAYG_METER_ENDPOINT")
|
||||
STIRLING_KEYGEN_ENABLED: $(yaml "$STIRLING_KEYGEN_ENABLED")
|
||||
KEYGEN_ACCOUNT_ID: $(yaml "$KEYGEN_ACCOUNT_ID")
|
||||
KEYGEN_API_TOKEN: $(yaml "$KEYGEN_API_TOKEN")
|
||||
KEYGEN_POLICY_ID: $(yaml "$KEYGEN_POLICY_ID")
|
||||
SYSTEM_DEFAULTLOCALE: en-US
|
||||
SYSTEM_MAXFILESIZE: "100"
|
||||
METRICS_ENABLED: "true"
|
||||
SYSTEM_GOOGLEVISIBILITY: "false"
|
||||
SWAGGER_SERVER_URL: "${BASE_URL}"
|
||||
baseUrl: "${BASE_URL}"${AI_BACKEND_VARS}
|
||||
restart: on-failure:5
|
||||
|
||||
saas-frontend:
|
||||
container_name: stirling-saas-dev-frontend
|
||||
image: ${IMAGE_BASE}:saas-frontend-${IMAGE_TAG}
|
||||
ports:
|
||||
- "${FRONTEND_PORT}:80"
|
||||
environment:
|
||||
VITE_API_BASE_URL: "http://saas-backend:8080"
|
||||
depends_on:
|
||||
- saas-backend
|
||||
restart: on-failure:5${AI_SERVICE}
|
||||
EOF
|
||||
|
||||
SSH_OPTS=(-i ../private.key -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null)
|
||||
|
||||
scp "${SSH_OPTS[@]}" docker-compose.yml "${VPS_USERNAME}@${VPS_HOST}:/tmp/saas-dev-docker-compose.yml"
|
||||
|
||||
ssh "${SSH_OPTS[@]}" -T "${VPS_USERNAME}@${VPS_HOST}" << ENDSSH
|
||||
set -e
|
||||
mkdir -p ${DEPLOY_DIR}/{config,logs,storage}
|
||||
mv /tmp/saas-dev-docker-compose.yml ${DEPLOY_DIR}/docker-compose.yml
|
||||
chmod 600 ${DEPLOY_DIR}/docker-compose.yml
|
||||
cd ${DEPLOY_DIR}
|
||||
printf '%s' "${GHCR_TOKEN}" | docker login ghcr.io -u "${GHCR_USER}" --password-stdin
|
||||
docker-compose down --remove-orphans 2>/dev/null || true
|
||||
docker-compose pull
|
||||
docker-compose up -d
|
||||
docker logout ghcr.io >/dev/null 2>&1 || true
|
||||
docker image prune -af --filter "until=336h" --filter "label!=keep=true" || true
|
||||
ENDSSH
|
||||
|
||||
- name: Wait for the backend to answer
|
||||
env:
|
||||
VPS_HOST: ${{ secrets.NEW_VPS_HOST }}
|
||||
run: |
|
||||
URL="http://${VPS_HOST}:${BACKEND_PORT}/api/v1/info/status"
|
||||
for i in $(seq 1 60); do
|
||||
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "$URL" || true)
|
||||
if [ "$code" = "200" ]; then echo "Healthy after $((i * 10))s"; exit 0; fi
|
||||
sleep 10
|
||||
done
|
||||
echo "::error::SaaS dev backend did not become healthy within 10 minutes"
|
||||
exit 1
|
||||
|
||||
- name: Cleanup temporary files
|
||||
if: always()
|
||||
run: rm -f ../private.key docker-compose.yml
|
||||
continue-on-error: true
|
||||
@@ -34,10 +34,9 @@ jobs:
|
||||
cache-dependency-glob: |
|
||||
engine/pyproject.toml
|
||||
engine/uv.lock
|
||||
cache-suffix: ai-engine
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Quality-check engine
|
||||
id: engine-check
|
||||
|
||||
@@ -52,7 +52,7 @@ jobs:
|
||||
distribution: "temurin"
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Check Java formatting (Spotless)
|
||||
# Runs once per matrix combination - pick the cheapest leg
|
||||
# (core - no proprietary, no saas) so we don't wait for the
|
||||
|
||||
@@ -95,7 +95,7 @@ jobs:
|
||||
cache: "npm"
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Install Playwright (chromium only)
|
||||
run: task e2e:install -- chromium
|
||||
- name: Build frontend (needed for playwright's vite preview webServer)
|
||||
|
||||
@@ -42,7 +42,6 @@ jobs:
|
||||
cache-dependency-glob: |
|
||||
engine/pyproject.toml
|
||||
engine/uv.lock
|
||||
cache-suffix: generated-models
|
||||
|
||||
- name: Restore cache Gradle User Home
|
||||
if: inputs.use_shared_cache
|
||||
@@ -76,7 +75,7 @@ jobs:
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Verify generated models are up to date
|
||||
id: models-check
|
||||
|
||||
@@ -38,7 +38,7 @@ jobs:
|
||||
distribution: "temurin"
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Check licenses for compatibility
|
||||
run: task backend:licenses:check
|
||||
env:
|
||||
|
||||
@@ -39,7 +39,7 @@ jobs:
|
||||
distribution: "temurin"
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Generate OpenAPI documentation
|
||||
run: task backend:swagger
|
||||
env:
|
||||
|
||||
@@ -57,7 +57,7 @@ jobs:
|
||||
# runtime token isn't exposed) since the docker driver can't use it.
|
||||
- name: Set up Docker Buildx
|
||||
if: inputs.docker-base-changed != 'true'
|
||||
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
|
||||
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
|
||||
|
||||
# Expose ACTIONS_RUNTIME_TOKEN / ACTIONS_RESULTS_URL for docker buildx type=gha cache backend.
|
||||
- name: Expose GitHub runtime for Buildx cache
|
||||
|
||||
@@ -45,7 +45,7 @@ jobs:
|
||||
cache: "npm"
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Install Playwright (chromium only)
|
||||
run: task e2e:install -- chromium
|
||||
- name: Build frontend (production bundle for vite preview)
|
||||
|
||||
@@ -44,7 +44,7 @@ jobs:
|
||||
cache: "npm"
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Build frontend (production bundle for vite preview)
|
||||
env:
|
||||
VITE_BUILD_FOR_PREVIEW: "1"
|
||||
|
||||
@@ -36,7 +36,7 @@ jobs:
|
||||
cache: "npm"
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: a11y gate (changed stories)
|
||||
run: task frontend:storybook:a11y:changed -- origin/${{ github.base_ref || 'main' }}
|
||||
- name: Upload scan reports
|
||||
|
||||
@@ -97,7 +97,7 @@ jobs:
|
||||
run: npm ci --ignore-scripts --audit=false --fund=false
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Generate frontend license report (Push only)
|
||||
if: github.event_name == 'push'
|
||||
@@ -367,7 +367,7 @@ jobs:
|
||||
distribution: "temurin"
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Check licenses and generate report
|
||||
id: license-check
|
||||
|
||||
@@ -27,7 +27,7 @@ jobs:
|
||||
cache: "npm"
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Quality-check frontend
|
||||
id: frontend-check
|
||||
run: task frontend:check:all
|
||||
|
||||
@@ -69,7 +69,7 @@ jobs:
|
||||
distribution: "temurin"
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Get version number
|
||||
id: versionNumber
|
||||
run: |
|
||||
@@ -169,7 +169,7 @@ jobs:
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Build JAR
|
||||
run: ./gradlew build ${{ matrix.variant.build_frontend && '-PbuildWithFrontend=true' || '' }} -x spotlessApply -x spotlessCheck -x test -x sonarqube
|
||||
@@ -268,7 +268,7 @@ jobs:
|
||||
distribution: ${{ matrix.platform == 'windows-11-arm' && 'microsoft' || 'temurin' }}
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
# Build the universal JRE before desktop:prepare so the jlink:runtime
|
||||
# task short-circuits on its `test -d runtime/jre` status check.
|
||||
|
||||
@@ -38,7 +38,7 @@ jobs:
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Install all Playwright browsers
|
||||
run: task e2e:install
|
||||
|
||||
@@ -89,7 +89,7 @@ jobs:
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: a11y gate (every story, ${{ matrix.theme }})
|
||||
run: task frontend:storybook:a11y:${{ matrix.theme }}
|
||||
@@ -162,7 +162,7 @@ jobs:
|
||||
engine/uv.lock
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Start the fat image with login and storage enabled
|
||||
run: docker compose -f docker/embedded/compose/test_cicd.yml up -d --build
|
||||
|
||||
@@ -31,10 +31,14 @@ jobs:
|
||||
cache-dependency-glob: |
|
||||
engine/pyproject.toml
|
||||
engine/uv.lock
|
||||
cache-suffix: pre-commit
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Run pre-commit checks
|
||||
run: task pre-commit
|
||||
|
||||
# The fixture corpus checks the comment rules themselves, so it runs here
|
||||
# rather than on every local commit.
|
||||
- name: Check the comment-lint fixture corpus
|
||||
run: task pre-commit:comment-lint:selftest
|
||||
|
||||
@@ -69,7 +69,7 @@ jobs:
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
|
||||
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4.2.0
|
||||
|
||||
@@ -85,10 +85,10 @@ jobs:
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
|
||||
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Get version number
|
||||
id: versionNumber
|
||||
run: echo "versionNumber=$(./gradlew printVersion --quiet | tail -1)" >> $GITHUB_OUTPUT
|
||||
|
||||
@@ -75,6 +75,6 @@ jobs:
|
||||
|
||||
# Upload the results to GitHub's code scanning dashboard.
|
||||
- name: "Upload to code-scanning"
|
||||
uses: github/codeql-action/upload-sarif@ff2f1c621b7f889edc0d3c761ac2e6a3f8cdb0dd # v4.37.7
|
||||
uses: github/codeql-action/upload-sarif@db488ddef3bf6cb639b32c2e9a7c0a7ea8271d28 # v4.37.8
|
||||
with:
|
||||
sarif_file: results.sarif
|
||||
|
||||
@@ -63,7 +63,7 @@ jobs:
|
||||
SWAGGERHUB_USER: "Frooodle"
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Get version number
|
||||
id: versionNumber
|
||||
run: echo "versionNumber=$(./gradlew printVersion --quiet | tail -1)" >> $GITHUB_OUTPUT
|
||||
|
||||
@@ -59,14 +59,13 @@ jobs:
|
||||
cache-dependency-glob: |
|
||||
engine/pyproject.toml
|
||||
engine/uv.lock
|
||||
cache-suffix: sync-files
|
||||
|
||||
- name: Install Python dependencies
|
||||
run: |
|
||||
uv sync --project engine --locked --group tools
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Sync translation TOML files
|
||||
run: |
|
||||
|
||||
@@ -212,7 +212,7 @@ jobs:
|
||||
distribution: ${{ matrix.platform == 'windows-11-arm' && 'microsoft' || 'temurin' }}
|
||||
|
||||
- name: Setup Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
|
||||
- name: Build universal macOS JRE
|
||||
if: matrix.platform == 'macos-15'
|
||||
|
||||
@@ -127,7 +127,7 @@ jobs:
|
||||
distribution: "temurin"
|
||||
|
||||
- name: Install Task
|
||||
uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0
|
||||
uses: go-task/setup-task@a00fbb05ce67b35648be3c78cbc9fd85354c757e # v2.2.0
|
||||
- name: Build application
|
||||
run: task backend:build
|
||||
env:
|
||||
@@ -142,7 +142,7 @@ jobs:
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
|
||||
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
|
||||
|
||||
- name: Set base image and platform for this build
|
||||
id: build-params
|
||||
@@ -229,7 +229,7 @@ jobs:
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0
|
||||
uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0
|
||||
|
||||
- name: Build docker/unoserver/Dockerfile
|
||||
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
|
||||
|
||||
+8
-2
@@ -298,8 +298,13 @@ docs/type3/signatures/
|
||||
|
||||
**/application-dev-local.properties
|
||||
|
||||
# Claude
|
||||
.claude/
|
||||
# Claude. Contents are ignored so personal config stays local, with the two
|
||||
# shared pieces re-included: settings.json (the comment-lint hook) and skills/.
|
||||
# The directory itself cannot be ignored or git will not look inside it.
|
||||
.claude/*
|
||||
!.claude/settings.json
|
||||
!.claude/skills/
|
||||
.claude/settings.local.json
|
||||
|
||||
# Playwright MCP screenshots / traces
|
||||
.playwright-mcp/
|
||||
@@ -307,3 +312,4 @@ docs/type3/signatures/
|
||||
|
||||
# Local screenshot artifacts from *-screenshots.spec.ts
|
||||
frontend/editor/screenshots/
|
||||
frontend/editor/src-tauri/libs/.variant
|
||||
|
||||
@@ -11,6 +11,7 @@ vars:
|
||||
'.github/scripts/*.py'
|
||||
'app/core/src/main/resources/static/python/*.py'
|
||||
':(exclude)*split_photos.py'
|
||||
':(exclude)scripts/lint/fixtures/*'
|
||||
SPELL_FILES: >-
|
||||
'*.html'
|
||||
'*.css'
|
||||
@@ -59,6 +60,7 @@ tasks:
|
||||
- task: gitleaks
|
||||
- task: whitespace
|
||||
- task: toml-sort
|
||||
- task: comment-lint
|
||||
|
||||
fix:
|
||||
desc: "Auto-fix formatting, spelling, and secrets issues across the repo"
|
||||
@@ -75,6 +77,7 @@ tasks:
|
||||
vars: { FIX: '1' }
|
||||
- task: codespell
|
||||
- task: gitleaks
|
||||
- task: comment-lint
|
||||
|
||||
install:
|
||||
desc: "Install the pinned pre-commit Python tools"
|
||||
@@ -130,6 +133,85 @@ tasks:
|
||||
cmds:
|
||||
- "{{.GITLEAKS_BIN}} git --pre-commit --redact --staged --verbose"
|
||||
|
||||
comment-lint:
|
||||
desc: "Check comment quality on the lines this branch adds"
|
||||
summary: |
|
||||
Blocks a comment that restates the code below it, a section banner, or a
|
||||
block of commented-out code. Everything else it reports is advisory.
|
||||
|
||||
Scoped to added lines, so touching an old file never surfaces the standing
|
||||
backlog. The standard is devGuide/CODE_COMMENTS.md.
|
||||
|
||||
With no arguments it diffs the working tree against HEAD, which is what a
|
||||
pre-commit run wants: the lines you are about to commit. On a CI pull request
|
||||
it diffs against the target branch instead, via GITHUB_BASE_REF.
|
||||
|
||||
To ask what a whole branch adds instead, use the branch variant, which
|
||||
needs no argument passing:
|
||||
task comment-lint:branch
|
||||
|
||||
Full tree (report only): task pre-commit:comment-lint:all
|
||||
Fixture corpus: task pre-commit:comment-lint:selftest
|
||||
# Depends on the frontend install because the .ts/.tsx half of the rule set
|
||||
# runs as an oxlint plugin. Without it the TS engine warns and skips, which
|
||||
# would leave the frontend silently unchecked on CI.
|
||||
deps: [":frontend:install"]
|
||||
cmds:
|
||||
- node scripts/lint/comment-lint.mjs {{.CLI_ARGS}}
|
||||
|
||||
comment-lint:branch:
|
||||
desc: "Check comment quality on everything this branch adds over its base"
|
||||
summary: |
|
||||
Like `task comment-lint`, but scoped to the whole branch rather than to
|
||||
uncommitted work, so it still reports after you commit.
|
||||
|
||||
Exists as its own task because passing `-- --since origin/main` through Task
|
||||
is not portable: with the npm build of Task the launcher is a PowerShell
|
||||
script, and PowerShell strips the `--` before Task sees it, leaving Task to
|
||||
print its own usage.
|
||||
|
||||
Override the base with BASE=<ref>.
|
||||
vars:
|
||||
BASE: '{{.BASE | default "origin/main"}}'
|
||||
deps: [":frontend:install"]
|
||||
cmds:
|
||||
- node scripts/lint/comment-lint.mjs --since {{.BASE}}
|
||||
|
||||
comment-lint:ci:
|
||||
desc: "Comment gate as CI runs it: fixture corpus, then the diff"
|
||||
summary: |
|
||||
The corpus checks the rules themselves rather than the code under review, so
|
||||
it belongs on CI and not on every local commit. Run this before changing a
|
||||
rule, and let CI run it on every pull request.
|
||||
deps: [":frontend:install"]
|
||||
cmds:
|
||||
- node scripts/lint/comment-lint.mjs --selftest
|
||||
- node scripts/lint/comment-lint.mjs {{.CLI_ARGS}}
|
||||
|
||||
comment-lint:hook:
|
||||
desc: "Comment gate for the editor hook: everything this turn changed"
|
||||
summary: |
|
||||
Same scope as `task comment-lint`, kept as its own name so the hook has a
|
||||
stable entry point and the taskfile shows every way the linter is invoked.
|
||||
|
||||
Not in the frontend-install dependency chain on purpose: this runs at the end
|
||||
of every turn, so it stays as short as it can be. If oxlint is missing the TS
|
||||
half warns and skips.
|
||||
cmds:
|
||||
- node scripts/lint/comment-lint.mjs
|
||||
|
||||
comment-lint:all:
|
||||
desc: "Report every comment finding in the tree (never fails)"
|
||||
deps: [":frontend:install"]
|
||||
cmds:
|
||||
- node scripts/lint/comment-lint.mjs --all
|
||||
|
||||
comment-lint:selftest:
|
||||
desc: "Check both comment-lint engines against the fixture corpus"
|
||||
deps: [":frontend:install"]
|
||||
cmds:
|
||||
- node scripts/lint/comment-lint.mjs --selftest
|
||||
|
||||
gitleaks-bin:
|
||||
internal: true
|
||||
desc: "Ensure the pinned, checksum-verified gitleaks binary is cached in .task/bin"
|
||||
|
||||
@@ -21,6 +21,43 @@ Task `desc:` fields should describe **what** the task does, not **how** it does
|
||||
- `task docker:build` — build standard Docker image
|
||||
- `task docker:up` — start Docker compose stack
|
||||
|
||||
## Comments
|
||||
|
||||
A comment must carry information the code cannot. If a reader could derive it from the code in front of them, delete it.
|
||||
|
||||
Comment the current state. Not what the code used to do, not what changed, not why it changed: git holds that. Where history explains the shape, state the reason instead, so "this used to reimplement the modal internals" becomes "thin wrapper over the shared Modal: duplicating its portal and focus trap is how dialogs drift apart". Future state goes in a TODO with an issue.
|
||||
|
||||
Write a comment when it does one of these four jobs:
|
||||
|
||||
- **Contract.** What a caller must know that the signature cannot say: preconditions, invariants, units, ownership and lifetime, thread-safety, error semantics, side effects. Document the contract of everything a caller outside the file can reach, and nothing else. Goes on the type/method/module as Javadoc, JSDoc, or a docstring.
|
||||
- **Why.** The constraint the code satisfies, the bug it avoids, the alternative rejected and the reason.
|
||||
- **Hazard.** "Must stay in sync with X", "order matters because Y", "do not remove, it prevents Z".
|
||||
- **Map.** A short orientation at the top of a genuinely complex file: what it owns, and what it deliberately does not.
|
||||
|
||||
Never write:
|
||||
|
||||
- A comment that restates the next line. `// Handle drag start` above `handleDragStart` is noise.
|
||||
- Section banners or position markers: `// --- Types ---`, `// Helpers`, `// =====`.
|
||||
- Step narration in a function body (`// Step 1:`, `// Then we`). If the steps need labels they need names: extract functions. Numbering a genuinely numbered thing, like a wizard step, is fine.
|
||||
- Commented-out code. Delete it.
|
||||
- Doc tags that restate the signature. `@param blob - The blob` says nothing; omit the tag rather than pad it.
|
||||
- Docs on self-explanatory members with no constraint to state.
|
||||
|
||||
Two tests before keeping a comment:
|
||||
|
||||
- **Delete it.** Is any information lost? If not, it stays deleted.
|
||||
- **Could a name carry it instead?** A better identifier, an extracted function, or a named constant beats a comment. Prefer the code change.
|
||||
|
||||
A comment at the end of a line usually decodes that line, and that is worth keeping: `{0x25, 0x50} // "%PDF"`, `50L * 1024 * 1024 // 50 MB`. The rules that compare a comment against the code below it do not apply there, but a trailing TODO or a trailing bit of history is judged like any other.
|
||||
|
||||
A reference is supplementary, never load-bearing: the comment must survive deleting it. `// See #1234` is a dead end; `// saving first loses every annotation (#6865)` is not. Prefer a spec (`RFC 3161`) or CVE where one applies.
|
||||
|
||||
A TODO needs an issue, not an owner: `// TODO(#1234): re-enable the gate once account syncing lands`. If it is not worth an issue, it is not worth a TODO. A question is not a TODO.
|
||||
|
||||
A comment block over ~12 lines outside a file or type header usually means the code needs restructuring, or that the prose is product documentation and belongs in the docs repo.
|
||||
|
||||
`task comment-lint` checks the mechanical part of this on the lines you add, and runs inside `task pre-commit`. Reasoning, worked examples and the linter's own rules: @devGuide/CODE_COMMENTS.md
|
||||
|
||||
## Common Development Commands
|
||||
|
||||
### Build and Test
|
||||
@@ -70,7 +107,7 @@ The project structure is defined in `engine/pyproject.toml`. Any new dependencie
|
||||
- Avoid nested functions and nested classes unless the language construct requires them.
|
||||
- Prefer composition to inheritance when combining concepts.
|
||||
- Avoid speculative abstractions. Add a layer only when it removes real duplication or clarifies lifecycle.
|
||||
- Add comments sparingly and only when they explain non-obvious intent.
|
||||
- Comments follow the repo-wide rules in the "Comments" section above.
|
||||
|
||||
#### Python Typing and Models
|
||||
- Deserialize into Pydantic models as early as possible.
|
||||
|
||||
@@ -42,6 +42,7 @@ Please make sure your Pull Request adheres to the following guidelines:
|
||||
- Keep commits atomic. One commit should contain one change. If you want to make multiple changes, submit multiple Pull Requests.
|
||||
- Commits should be clear, concise, and easy to understand.
|
||||
- References to the Issue number in the Pull Request and/or Commit message.
|
||||
- Every comment in the diff should say something the code does not. See [Code comments](devGuide/CODE_COMMENTS.md); `task comment-lint` checks the mechanical part.
|
||||
|
||||
## Translations
|
||||
|
||||
|
||||
@@ -266,6 +266,20 @@ tasks:
|
||||
cmds:
|
||||
- task: frontend:lint
|
||||
- task: engine:lint
|
||||
- task: comment-lint
|
||||
|
||||
comment-lint:
|
||||
desc: "Check comment quality on the lines this branch adds"
|
||||
aliases: [comments]
|
||||
cmds:
|
||||
- task: pre-commit:comment-lint
|
||||
vars: { CLI_ARGS: '{{.CLI_ARGS}}' }
|
||||
|
||||
comment-lint:branch:
|
||||
desc: "Check comment quality on everything this branch adds over its base"
|
||||
cmds:
|
||||
- task: pre-commit:comment-lint:branch
|
||||
vars: { BASE: '{{.BASE}}' }
|
||||
|
||||
fix:
|
||||
desc: "Auto-fix all components"
|
||||
|
||||
@@ -48,7 +48,7 @@ public class EndpointConfiguration {
|
||||
private final ApplicationProperties applicationProperties;
|
||||
@Getter private Map<String, Boolean> endpointStatuses = new ConcurrentHashMap<>();
|
||||
private Map<String, Set<String>> endpointGroups = new ConcurrentHashMap<>();
|
||||
private Set<String> disabledGroups = new HashSet<>();
|
||||
private Set<String> disabledGroups = ConcurrentHashMap.newKeySet();
|
||||
private Map<String, DisableReason> endpointDisableReasons = new ConcurrentHashMap<>();
|
||||
private Map<String, DisableReason> groupDisableReasons = new ConcurrentHashMap<>();
|
||||
private Map<String, Set<String>> endpointAlternatives = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -237,7 +237,7 @@ public class TabulaTableParser implements TableParser {
|
||||
score -= 0.3f;
|
||||
}
|
||||
|
||||
return Math.max(0f, Math.min(1f, score));
|
||||
return Math.clamp(score, 0f, 1f);
|
||||
}
|
||||
|
||||
private Bounds tableBounds(Table table) {
|
||||
|
||||
+2
-1
@@ -15,7 +15,8 @@ public class StringToMapPropertyEditor extends PropertyEditorSupport {
|
||||
@Override
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
try {
|
||||
TypeReference<HashMap<String, String>> typeRef = new TypeReference<>() {};
|
||||
TypeReference<HashMap<String, String>> typeRef =
|
||||
new TypeReference<HashMap<String, String>>() {};
|
||||
Map<String, String> map = objectMapper.readValue(text, typeRef);
|
||||
setValue(map);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -113,6 +113,16 @@ class RequestUriUtilsTest {
|
||||
assertTrue(RequestUriUtils.isFrontendRoute("", "/split-pdf"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsFrontendRoute_editorRouteOwnedByFrontend() {
|
||||
// /editor (and its tool routes) is an SPA route: a direct-nav/refresh must
|
||||
// serve index.html, not the auth filter's 302-to-/login. Regression test for
|
||||
// the editor moving from / to /editor, whose refresh bounced processor users
|
||||
// to the processor because the redirect dropped the return path.
|
||||
assertTrue(RequestUriUtils.isFrontendRoute("", "/editor"));
|
||||
assertTrue(RequestUriUtils.isFrontendRoute("/app", "/app/editor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsFrontendRoute_filesRouteOwnedByFrontend() {
|
||||
// /files and /files/<folder-uuid> are FileManagerView routes - they
|
||||
|
||||
@@ -306,6 +306,9 @@ tasks.register('copyFrontendAssets', Copy) {
|
||||
// Exclude files that conflict with backend static resources
|
||||
exclude 'robots.txt' // Backend already has this
|
||||
exclude 'favicon.ico' // Backend already has this
|
||||
// Backend ships its own NotoSans-Regular.ttf here and it is git-tracked;
|
||||
// letting the editor's copy win would dirty the source tree on every build.
|
||||
exclude 'fonts/NotoSans-Regular.ttf'
|
||||
}
|
||||
into resourcesStaticDir
|
||||
duplicatesStrategy = DuplicatesStrategy.INCLUDE // Let frontend overwrite when needed
|
||||
|
||||
+1
-1
@@ -237,7 +237,7 @@ public class EditTextController {
|
||||
|
||||
Matcher matcher = edit.pattern().matcher(joined);
|
||||
List<MatchSpan> spans = new ArrayList<>();
|
||||
StringBuffer interpolation = new StringBuffer();
|
||||
StringBuilder interpolation = new StringBuilder();
|
||||
int previousAppendPosition = 0;
|
||||
while (matcher.find()) {
|
||||
if (matcher.start() == matcher.end()) {
|
||||
|
||||
+598
@@ -0,0 +1,598 @@
|
||||
package stirling.software.SPDF.controller.api;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDResources;
|
||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.common.annotations.api.GeneralApi;
|
||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||
|
||||
/**
|
||||
* Charcode-encode helper for the v2 PDF text editor.
|
||||
*
|
||||
* <p>The frontend editor uses PDFium-WASM, which exposes {@code FPDFText_SetCharcodes} for writing
|
||||
* new text using raw font charcodes (skipping PDFium's broken reverse Unicode→CID lookup for
|
||||
* embedded subset fonts). What PDFium does NOT expose is the byte-encoding side of an existing font
|
||||
* - given a PDFont and a Unicode string, what are the bytes the font's encoding produces? PDFBox
|
||||
* does have that ({@link PDFont#encode}).
|
||||
*
|
||||
* <p>This endpoint accepts the source PDF + a "locator" describing where to find the font in
|
||||
* question (page index + a sample char known to render in the target font, optionally narrowed by
|
||||
* the font's /BaseFont name) + the Unicode text the frontend wants to encode. It returns the
|
||||
* charcode sequence the frontend can pass to {@code FPDFText_SetCharcodes}.
|
||||
*
|
||||
* <p>If the locator can't find a matching text fragment, or if the font can't encode some chars,
|
||||
* the response reports which chars are missing so the frontend can fall back to Helvetica per char.
|
||||
*/
|
||||
@Slf4j
|
||||
@GeneralApi
|
||||
@RequiredArgsConstructor
|
||||
public class PdfTextEditorCharcodeController {
|
||||
|
||||
/** Reject JSON bodies whose base64 implies a decoded PDF larger than this. */
|
||||
private static final int MAX_PDF_BYTES = 100 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* Upper bound on {@code request.text} code units. Editor requests are word-sized; an unbounded
|
||||
* text drove a per-code-point encode/exception loop (CPU burn) on crafted requests.
|
||||
*/
|
||||
private static final int MAX_TEXT_CHARS = 4096;
|
||||
|
||||
/** Nested form-XObject resource dictionaries visited per lookup (cycle/DoS guard). */
|
||||
private static final int MAX_RESOURCE_DICTS = 32;
|
||||
|
||||
/** Bound on the reverse-map cache so a busy multi-document server can't grow it forever. */
|
||||
private static final int REVERSE_MAP_CACHE_MAX = 32;
|
||||
|
||||
/** Access-ordered LRU bounded at {@link #REVERSE_MAP_CACHE_MAX} entries. */
|
||||
private static final class BoundedReverseMapCache
|
||||
extends java.util.LinkedHashMap<String, java.util.Map<String, Long>> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
BoundedReverseMapCache() {
|
||||
super(16, 0.75f, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(
|
||||
java.util.Map.Entry<String, java.util.Map<String, Long>> eldest) {
|
||||
return size() > REVERSE_MAP_CACHE_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
private static final java.util.Map<String, java.util.Map<String, Long>> REVERSE_MAP_CACHE =
|
||||
java.util.Collections.synchronizedMap(new BoundedReverseMapCache());
|
||||
|
||||
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
||||
|
||||
// NOTE: PDFBox's PDSimpleFont emits one "No Unicode mapping for .notdef" WARN per probed
|
||||
// charcode when buildReverseUnicodeMap iterates 0..0xFFFF, which once flooded info.log to
|
||||
// ~1.4 GB overnight. That logger is silenced DECLARATIVELY in logback.xml (a config entry ops
|
||||
// can see and revert) rather than by mutating the global logger from a static block here -
|
||||
// mutating it at class-load time hid the same warnings from every other tool in the JVM with
|
||||
// no trace in configuration.
|
||||
|
||||
@Data
|
||||
public static class EncodeCharcodesRequest {
|
||||
|
||||
/** Base64-encoded original PDF. The frontend already has the bytes loaded. */
|
||||
private String pdfBase64;
|
||||
|
||||
/** 0-based page index containing the font sample. */
|
||||
private int pageIndex;
|
||||
|
||||
/**
|
||||
* A char known to exist on the page in the target font. Combined with {@code fontName}
|
||||
* (when supplied) it locates the source PDFont via its ToUnicode CMap.
|
||||
*/
|
||||
private String locatorChar;
|
||||
|
||||
/**
|
||||
* Optional /BaseFont name of the target font (as PDFium's FPDFFont_GetBaseFontName reports
|
||||
* it). When a page has TWO fonts that both render {@code locatorChar}, this disambiguates
|
||||
* which one to encode against - otherwise the first font found wins and a cross-font edit
|
||||
* gets the wrong font's charcode. Null = keep the legacy first-match behaviour.
|
||||
*/
|
||||
private String fontName;
|
||||
|
||||
/**
|
||||
* Optional SHA-256 (lowercase hex) of the target font's embedded program bytes (what
|
||||
* PDFium's FPDFFont_GetFontData returns = the decoded FontFile/FontFile2/FontFile3 stream).
|
||||
* This is the ONLY unambiguous font identity: PDFium strips the "ABCDEF+" subset tag from
|
||||
* font names, so every subset of one family reports the same {@code fontName} and a
|
||||
* name-based lookup can land on a SIBLING subset whose charcode space is different -
|
||||
* returning valid-but-wrong charcodes that scramble the edited text. When present and a
|
||||
* font on the page matches, it wins over name matching.
|
||||
*/
|
||||
private String fontSha256;
|
||||
|
||||
/** Unicode text the frontend wants to encode. */
|
||||
private String text;
|
||||
}
|
||||
|
||||
@Data
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public static class EncodeCharcodesResponse {
|
||||
/**
|
||||
* Per-char charcode array (one entry per code point in {@code request.text}). When the
|
||||
* font's encoding produces multi-byte sequences, each char gets the full unsigned int value
|
||||
* of its bytes packed big-endian (so a 2-byte CID like 0x004D becomes 77).
|
||||
*/
|
||||
private List<Long> charcodes;
|
||||
|
||||
/** Chars from the request that the font couldn't encode. */
|
||||
private List<String> missing;
|
||||
|
||||
/** Diagnostic note - included so the frontend HUD can show what happened. */
|
||||
private String note;
|
||||
|
||||
/** Set when the request failed entirely (bad pdf bytes, no matching font, etc.). */
|
||||
private String error;
|
||||
}
|
||||
|
||||
@Operation(
|
||||
summary = "Encode Unicode → font charcodes for the v2 PDF text editor",
|
||||
description =
|
||||
"""
|
||||
Frontend-only helper: takes the source PDF, a locator pointing at an existing
|
||||
char rendered in the target font, and a Unicode string. Returns the byte
|
||||
sequence the target font produces for that Unicode, packed as one unsigned
|
||||
int per char. The frontend then calls FPDFText_SetCharcodes with the
|
||||
returned ints to inject new text that reuses the embedded font's actual
|
||||
glyphs. Chars the font can't encode are listed in `missing` so the caller
|
||||
can fall back per-char.
|
||||
""")
|
||||
@PostMapping(
|
||||
value = "/pdf-text-editor/encode-charcodes",
|
||||
consumes = "application/json",
|
||||
produces = "application/json")
|
||||
public ResponseEntity<EncodeCharcodesResponse> encodeCharcodes(
|
||||
@RequestBody EncodeCharcodesRequest request) {
|
||||
EncodeCharcodesResponse resp = new EncodeCharcodesResponse();
|
||||
if (request == null
|
||||
|| request.getPdfBase64() == null
|
||||
|| request.getText() == null
|
||||
|| request.getLocatorChar() == null) {
|
||||
resp.setError("missing required fields");
|
||||
return ResponseEntity.badRequest().body(resp);
|
||||
}
|
||||
// length/4*3 bounds the decoded size without decoding, so we reject early before
|
||||
// allocating.
|
||||
String b64 = request.getPdfBase64();
|
||||
if ((long) b64.length() / 4 * 3 > MAX_PDF_BYTES) {
|
||||
resp.setError("pdf too large");
|
||||
return ResponseEntity.status(413).body(resp);
|
||||
}
|
||||
// Reported separately: a combined check names only one cause and misleads the caller.
|
||||
if (request.getText().length() > MAX_TEXT_CHARS) {
|
||||
resp.setError("text too long");
|
||||
return ResponseEntity.badRequest().body(resp);
|
||||
}
|
||||
if (request.getLocatorChar().length() > 4) {
|
||||
resp.setError("locatorChar too long");
|
||||
return ResponseEntity.badRequest().body(resp);
|
||||
}
|
||||
byte[] pdfBytes;
|
||||
try {
|
||||
pdfBytes = Base64.getDecoder().decode(b64);
|
||||
} catch (IllegalArgumentException e) {
|
||||
resp.setError("pdfBase64 is not valid base64");
|
||||
return ResponseEntity.badRequest().body(resp);
|
||||
}
|
||||
try (PDDocument doc = pdfDocumentFactory.load(pdfBytes, true)) {
|
||||
if (request.getPageIndex() < 0 || request.getPageIndex() >= doc.getNumberOfPages()) {
|
||||
resp.setError("pageIndex out of range");
|
||||
return ResponseEntity.badRequest().body(resp);
|
||||
}
|
||||
PDPage page = doc.getPage(request.getPageIndex());
|
||||
// Skip walking the page's content stream (it crashes on Type3 fonts with
|
||||
// UnsupportedOperationException("Not implemented: Type3") before we can do anything
|
||||
// useful). Instead enumerate the page's font resources and pick the one identified by
|
||||
// the request's font-program hash (definitive), falling back to name matching.
|
||||
// For Chrome/Skia-printed PDFs that emit one Type3 font per glyph, this lands on
|
||||
// the exact font that renders the locator char.
|
||||
ResourceFont located =
|
||||
findFontByToUnicode(
|
||||
page,
|
||||
request.getLocatorChar(),
|
||||
request.getFontName(),
|
||||
request.getFontSha256(),
|
||||
doc);
|
||||
if (located == null) {
|
||||
resp.setError(
|
||||
"no font on page "
|
||||
+ request.getPageIndex()
|
||||
+ " renders locatorChar="
|
||||
+ request.getLocatorChar()
|
||||
+ (request.getFontName() != null
|
||||
? " (fontName=" + request.getFontName() + ")"
|
||||
: ""));
|
||||
return ResponseEntity.ok(resp);
|
||||
}
|
||||
// Build a reverse Unicode→charcode map by walking the font's ToUnicode CMap.
|
||||
// This is the ONLY path that works for Type3 fonts (PDFBox's font.encode() throws
|
||||
// "Not implemented: Type3" on them), and it also acts as a more reliable fallback
|
||||
// for subset fonts whose encode() rejects chars not in the original document.
|
||||
//
|
||||
// For Sample.pdf specifically, every embedded font is Type3 (Chrome/Skia output),
|
||||
// but they all carry a ToUnicode CMap mapping CIDs back to Unicode. We iterate
|
||||
// charcodes 0..0xFFFF, call font.toUnicode(cc) for each, and record the inverse
|
||||
// mapping for the chars the user wants to write.
|
||||
PDFont font = located.font();
|
||||
java.util.Map<String, Long> reverseMap =
|
||||
buildReverseUnicodeMap(pdfBytes, located, request.getPageIndex());
|
||||
List<Long> charcodes = new ArrayList<>();
|
||||
List<String> missing = new ArrayList<>();
|
||||
String text = request.getText();
|
||||
int i = 0;
|
||||
while (i < text.length()) {
|
||||
int cp = text.codePointAt(i);
|
||||
String oneChar = new String(Character.toChars(cp));
|
||||
i += Character.charCount(cp);
|
||||
// Whitespace is NEVER charcode-reused. Subset Type1/LaTeX fonts
|
||||
// usually have no real space glyph, yet font.encode(0x20) still
|
||||
// returns code 0x20 without throwing - and SetCharcodes(0x20)
|
||||
// then paints whatever glyph sits at that subset code (e.g. „
|
||||
// quotedblbase in LMRoman). Report whitespace as missing so the
|
||||
// frontend emits it as a positional gap instead.
|
||||
if (Character.isWhitespace(cp)) {
|
||||
missing.add(oneChar);
|
||||
continue;
|
||||
}
|
||||
// 1st try: font.encode() - works for Type0/TrueType/Type1
|
||||
Long packed = null;
|
||||
try {
|
||||
byte[] encoded = font.encode(oneChar);
|
||||
long p = 0L;
|
||||
for (byte b : encoded) p = (p << 8) | (b & 0xff);
|
||||
packed = p;
|
||||
} catch (IOException
|
||||
| IllegalArgumentException
|
||||
| UnsupportedOperationException encodeEx) {
|
||||
// 2nd try: ToUnicode reverse lookup - works for Type3 + anything with a CMap
|
||||
packed = reverseMap.get(oneChar);
|
||||
}
|
||||
if (packed != null) charcodes.add(packed);
|
||||
else missing.add(oneChar);
|
||||
}
|
||||
resp.setCharcodes(charcodes);
|
||||
if (!missing.isEmpty()) resp.setMissing(missing);
|
||||
resp.setNote(
|
||||
"font="
|
||||
+ font.getName()
|
||||
+ " encoded "
|
||||
+ charcodes.size()
|
||||
+ " of "
|
||||
+ (charcodes.size() + missing.size())
|
||||
+ " chars");
|
||||
return ResponseEntity.ok(resp);
|
||||
} catch (IOException e) {
|
||||
log.warn("encodeCharcodes: failed to load PDF", e);
|
||||
resp.setError("failed to load PDF");
|
||||
return ResponseEntity.badRequest().body(resp);
|
||||
} catch (RuntimeException e) {
|
||||
log.warn("encodeCharcodes: unexpected error", e);
|
||||
resp.setError("unexpected error");
|
||||
return ResponseEntity.status(500).body(resp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate the font the request targets. Identity sources, strongest first:
|
||||
*
|
||||
* <ol>
|
||||
* <li><b>Program hash</b>: SHA-256 of the embedded font program bytes. Definitive - two
|
||||
* different subsets NEVER share program bytes, and PDFium's FPDFFont_GetFontData returns
|
||||
* exactly the decoded FontFile stream, so frontend and backend hash the same bytes.
|
||||
* <li><b>Exact /BaseFont name</b> (subset tag included), then <b>tag-stripped name</b>. Name
|
||||
* matches are only accepted when UNAMBIGUOUS: PDFium reports subset fonts WITHOUT their
|
||||
* "ABCDEF+" tag, so a page with several subsets of one family ("AAAAAC+Garamond",
|
||||
* "AAAAAG+Garamond", ...) has them ALL match the stripped name - and encoding against the
|
||||
* wrong sibling returns valid-but-wrong charcodes that scramble the edited text ("RUSSELL
|
||||
* W. MANGUM" rendered "US EEL W. MANGS M"). With 2+ candidates we return null so the
|
||||
* frontend takes its safe fallback instead of a coin flip.
|
||||
* </ol>
|
||||
*
|
||||
* <p>This avoids running PDFStreamEngine.processPage, which throws
|
||||
* UnsupportedOperationException on Type3 font glyph rendering. The PDFont lookup itself is
|
||||
* purely metadata-driven and works on all subtypes.
|
||||
*/
|
||||
private static ResourceFont findFontByToUnicode(
|
||||
PDPage page, String wantChar, String fontName, String fontSha256, PDDocument doc) {
|
||||
try {
|
||||
List<ResourceFont> fonts = collectResourceTreeFonts(page.getResources());
|
||||
|
||||
// 1) Program-hash identity. When several dicts share one program (identical bytes
|
||||
// re-embedded), any of them renders the same glyphs for the same codes; prefer the
|
||||
// one whose ToUnicode covers the locator char so the reverse map is usable.
|
||||
if (fontSha256 != null && !fontSha256.isEmpty()) {
|
||||
List<ResourceFont> hashMatches = new ArrayList<>();
|
||||
for (ResourceFont rf : fonts) {
|
||||
String sha = fontProgramSha256(rf.font());
|
||||
if (fontSha256.equalsIgnoreCase(sha)) hashMatches.add(rf);
|
||||
}
|
||||
for (ResourceFont rf : hashMatches) {
|
||||
if (probesToUnicode(rf.font(), wantChar)) return rf;
|
||||
}
|
||||
if (!hashMatches.isEmpty()) return hashMatches.get(0);
|
||||
// No program on this page hashes to what the frontend is editing (e.g. PDFium
|
||||
// returned a substitute font's bytes for a non-embedded font). Fall through to
|
||||
// name matching rather than failing outright.
|
||||
}
|
||||
|
||||
// 2) Name identity - exact tag-included first, then tag-stripped - each accepted
|
||||
// only when it selects a single font.
|
||||
if (fontName != null && !fontName.isEmpty()) {
|
||||
ResourceFont exact =
|
||||
selectUnambiguous(
|
||||
fonts, wantChar, f -> fontName.equals(f.getName()), "exact");
|
||||
if (exact != null) return exact;
|
||||
String wantStripped = stripSubsetTag(fontName);
|
||||
ResourceFont stripped =
|
||||
selectUnambiguous(
|
||||
fonts,
|
||||
wantChar,
|
||||
f -> wantStripped.equals(stripSubsetTag(f.getName())),
|
||||
"stripped");
|
||||
if (stripped != null) return stripped;
|
||||
// The frontend NAMED the font it is editing. Falling back to "any font that
|
||||
// renders the char" would hand back a DIFFERENT font's charcodes, which the
|
||||
// frontend then writes into the named font's text object - wrong glyph, and the
|
||||
// backend strategy skips all frontend validation. Report the char missing
|
||||
// instead so the caller takes its own fallback path.
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3) Legacy locator-only behaviour: first font whose ToUnicode renders the char.
|
||||
for (ResourceFont rf : fonts) {
|
||||
if (probesToUnicode(rf.font(), wantChar)) return rf;
|
||||
}
|
||||
} catch (RuntimeException ignore) {
|
||||
// Be defensive: any single bad font shouldn't sink the whole request.
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply {@code nameFilter}, then decide: exactly one candidate whose ToUnicode covers {@code
|
||||
* wantChar} wins; two+ probe-hits are AMBIGUOUS (null). With zero probe-hits, a single
|
||||
* name-matching font is still returned (font.encode() may handle chars without a ToUnicode -
|
||||
* common for Type0/Identity-H), but two+ name matches are again ambiguous.
|
||||
*/
|
||||
private static ResourceFont selectUnambiguous(
|
||||
List<ResourceFont> fonts,
|
||||
String wantChar,
|
||||
java.util.function.Predicate<PDFont> nameFilter,
|
||||
String modeLabel) {
|
||||
List<ResourceFont> named = new ArrayList<>();
|
||||
for (ResourceFont rf : fonts) {
|
||||
try {
|
||||
if (rf.font().getName() != null && nameFilter.test(rf.font())) named.add(rf);
|
||||
} catch (RuntimeException ignore) {
|
||||
}
|
||||
}
|
||||
if (named.isEmpty()) return null;
|
||||
List<ResourceFont> probed = new ArrayList<>();
|
||||
for (ResourceFont rf : named) {
|
||||
if (probesToUnicode(rf.font(), wantChar)) probed.add(rf);
|
||||
}
|
||||
if (probed.size() == 1) return probed.get(0);
|
||||
if (probed.size() > 1) {
|
||||
log.debug(
|
||||
"encodeCharcodes: {} name match ambiguous ({} fonts render locator '{}') -"
|
||||
+ " refusing cross-subset guess",
|
||||
modeLabel,
|
||||
probed.size(),
|
||||
wantChar);
|
||||
return null;
|
||||
}
|
||||
return named.size() == 1 ? named.get(0) : null;
|
||||
}
|
||||
|
||||
/** True when some charcode in the font's ToUnicode CMap maps to {@code wantChar}. */
|
||||
private static boolean probesToUnicode(PDFont font, String wantChar) {
|
||||
// Cheap inverse-CMap probe: iterate codes until we hit one whose toUnicode is wantChar.
|
||||
// For Type3 with at most ~16 glyphs, this is microseconds. For full Type0 subsets
|
||||
// it's a few-thousand-iteration scan.
|
||||
int upper = font.isStandard14() ? 256 : 0x10000;
|
||||
for (int cc = 0; cc < upper; cc++) {
|
||||
String u;
|
||||
try {
|
||||
u = font.toUnicode(cc);
|
||||
} catch (Exception ignore) {
|
||||
continue;
|
||||
}
|
||||
if (u != null && u.equals(wantChar)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private record ResourceFont(PDFont font, String path) {}
|
||||
|
||||
private record PendingResources(PDResources resources, String path) {}
|
||||
|
||||
/**
|
||||
* Breadth-first collection of every distinct font reachable from the page's resources AND every
|
||||
* nested form XObject's resources (bounded by {@link #MAX_RESOURCE_DICTS}, cycle-safe, deduped
|
||||
* by COS dictionary identity). The v2 reader surfaces form-XObject text as editable, so its
|
||||
* fonts must be findable too.
|
||||
*/
|
||||
private static List<ResourceFont> collectResourceTreeFonts(PDResources resources) {
|
||||
List<ResourceFont> out = new ArrayList<>();
|
||||
java.util.ArrayDeque<PendingResources> queue = new java.util.ArrayDeque<>();
|
||||
java.util.Set<org.apache.pdfbox.cos.COSDictionary> seenDicts =
|
||||
java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<>());
|
||||
java.util.Set<org.apache.pdfbox.cos.COSDictionary> seenFonts =
|
||||
java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<>());
|
||||
if (resources != null) queue.add(new PendingResources(resources, ""));
|
||||
int visited = 0;
|
||||
// Bound a crafted page declaring many fonts none of which match (CPU-DoS guard).
|
||||
final int MAX_FONTS = 64;
|
||||
while (!queue.isEmpty() && visited < MAX_RESOURCE_DICTS) {
|
||||
PendingResources pending = queue.poll();
|
||||
PDResources res = pending.resources();
|
||||
if (!seenDicts.add(res.getCOSObject())) continue;
|
||||
visited++;
|
||||
for (org.apache.pdfbox.cos.COSName name : res.getFontNames()) {
|
||||
if (out.size() >= MAX_FONTS) break;
|
||||
PDFont font;
|
||||
try {
|
||||
font = res.getFont(name);
|
||||
} catch (IOException | RuntimeException e) {
|
||||
continue;
|
||||
}
|
||||
if (font == null || !seenFonts.add(font.getCOSObject())) continue;
|
||||
out.add(new ResourceFont(font, pending.path() + "/" + name.getName()));
|
||||
}
|
||||
try {
|
||||
for (org.apache.pdfbox.cos.COSName xn : res.getXObjectNames()) {
|
||||
try {
|
||||
org.apache.pdfbox.pdmodel.graphics.PDXObject xo = res.getXObject(xn);
|
||||
if (xo
|
||||
instanceof
|
||||
org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject form) {
|
||||
PDResources fr = form.getResources();
|
||||
if (fr != null) {
|
||||
queue.add(
|
||||
new PendingResources(
|
||||
fr, pending.path() + "/" + xn.getName()));
|
||||
}
|
||||
}
|
||||
} catch (IOException | RuntimeException ignore) {
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException ignore) {
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* SHA-256 (lowercase hex) of a font's embedded program bytes - the decoded
|
||||
* FontFile/FontFile2/FontFile3 stream, which is byte-identical to what PDFium's
|
||||
* FPDFFont_GetFontData hands the frontend. Null when the font embeds no program.
|
||||
*/
|
||||
private static String fontProgramSha256(PDFont font) {
|
||||
try {
|
||||
org.apache.pdfbox.pdmodel.font.PDFontDescriptor fd = font.getFontDescriptor();
|
||||
if (fd == null && font instanceof org.apache.pdfbox.pdmodel.font.PDType0Font type0) {
|
||||
fd = type0.getDescendantFont().getFontDescriptor();
|
||||
}
|
||||
if (fd == null) return null;
|
||||
org.apache.pdfbox.pdmodel.common.PDStream stream = fd.getFontFile2();
|
||||
if (stream == null) stream = fd.getFontFile3();
|
||||
if (stream == null) stream = fd.getFontFile();
|
||||
if (stream == null) return null;
|
||||
return sha256Hex(stream.toByteArray());
|
||||
} catch (IOException | RuntimeException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Drop the 6-letter "ABCDEF+" subset prefix PDF puts on subset /BaseFont names. */
|
||||
private static String stripSubsetTag(String fontName) {
|
||||
if (fontName == null) return null;
|
||||
if (fontName.length() > 7
|
||||
&& fontName.charAt(6) == '+'
|
||||
&& fontName.chars().limit(6).allMatch(c -> c >= 'A' && c <= 'Z')) {
|
||||
return fontName.substring(7);
|
||||
}
|
||||
return fontName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a Unicode→charcode map for a font by iterating every charcode in 0..0xFFFF and asking
|
||||
* the font's ToUnicode CMap what Unicode it maps to. Charcodes that aren't in the CMap throw
|
||||
* inside toUnicode (PDFBox returns null or throws depending on font subtype), and those are
|
||||
* skipped silently.
|
||||
*
|
||||
* <p>This is the encoding inverse PDFBox doesn't expose directly. For Type3 fonts (where
|
||||
* font.encode() throws "Not implemented"), this is the ONLY way to write text in the same font
|
||||
* - we look up the user's char in the reverse map and pass that charcode to
|
||||
* FPDFText_SetCharcodes on the frontend.
|
||||
*
|
||||
* <p>The 0..0xFFFF range is sufficient for Type0/CIDFontType2 fonts (CIDs are 16-bit). For
|
||||
* single-byte fonts the loop short-circuits after 256. We don't go higher because no PDF font
|
||||
* has a CID outside that range in practice; the per-font result is memoised in {@link
|
||||
* #REVERSE_MAP_CACHE} so the 65 536-entry probe runs once per document+font, not per request.
|
||||
*/
|
||||
private static java.util.Map<String, Long> buildReverseUnicodeMap(
|
||||
byte[] pdfBytes, ResourceFont located, int pageIndex) {
|
||||
String key = sha256Hex(pdfBytes) + "|" + fontCacheIdentity(located, pageIndex);
|
||||
// Compound get/put under the map's own monitor. The 0..0xFFFF probe runs OUTSIDE the
|
||||
// lock so one slow build can't block every other request on the shared cache.
|
||||
java.util.Map<String, Long> cached;
|
||||
synchronized (REVERSE_MAP_CACHE) {
|
||||
cached = REVERSE_MAP_CACHE.get(key);
|
||||
}
|
||||
if (cached != null) return cached;
|
||||
java.util.Map<String, Long> built = computeReverseUnicodeMap(located.font());
|
||||
synchronized (REVERSE_MAP_CACHE) {
|
||||
java.util.Map<String, Long> raced = REVERSE_MAP_CACHE.putIfAbsent(key, built);
|
||||
return raced != null ? raced : built;
|
||||
}
|
||||
}
|
||||
|
||||
private static String fontCacheIdentity(ResourceFont located, int pageIndex) {
|
||||
org.apache.pdfbox.cos.COSObjectKey objectKey = null;
|
||||
try {
|
||||
objectKey = located.font().getCOSObject().getKey();
|
||||
} catch (RuntimeException ignore) {
|
||||
}
|
||||
if (objectKey != null) {
|
||||
return "obj|" + objectKey.getNumber() + "." + objectKey.getGeneration();
|
||||
}
|
||||
return "res|p" + pageIndex + located.path();
|
||||
}
|
||||
|
||||
/** Lowercase hex SHA-256 of the PDF bytes; used as the reverse-map cache key. */
|
||||
private static String sha256Hex(byte[] bytes) {
|
||||
try {
|
||||
byte[] digest = java.security.MessageDigest.getInstance("SHA-256").digest(bytes);
|
||||
StringBuilder sb = new StringBuilder(digest.length * 2);
|
||||
for (byte b : digest) {
|
||||
sb.append(Character.forDigit((b >> 4) & 0xf, 16));
|
||||
sb.append(Character.forDigit(b & 0xf, 16));
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (java.security.NoSuchAlgorithmException e) {
|
||||
// SHA-256 is always present in a JRE; fall back to a length+hash key just in case so
|
||||
// the cache still functions (correctness holds - collisions only cost a rebuild).
|
||||
return bytes.length + ":" + java.util.Arrays.hashCode(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
private static java.util.Map<String, Long> computeReverseUnicodeMap(PDFont font) {
|
||||
java.util.Map<String, Long> out = new java.util.HashMap<>();
|
||||
int upper = font.isStandard14() ? 256 : 0x10000;
|
||||
for (int cc = 0; cc < upper; cc++) {
|
||||
String u;
|
||||
try {
|
||||
u = font.toUnicode(cc);
|
||||
} catch (Exception ignore) {
|
||||
continue;
|
||||
}
|
||||
if (u == null || u.isEmpty()) continue;
|
||||
// First charcode wins for a given Unicode (the canonical mapping).
|
||||
out.putIfAbsent(u, (long) cc);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -95,7 +95,8 @@ public class UIDataController {
|
||||
|
||||
try (InputStream is = resource.getInputStream()) {
|
||||
Map<String, List<Dependency>> licenseData =
|
||||
objectMapper.readValue(is, new TypeReference<>() {});
|
||||
objectMapper.readValue(
|
||||
is, new TypeReference<Map<String, List<Dependency>>>() {});
|
||||
data.setDependencies(licenseData.get("dependencies"));
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to load licenses data", e);
|
||||
|
||||
+6
-3
@@ -25,12 +25,15 @@ final class FormPayloadParser {
|
||||
private static final String KEY_VALUE = "value";
|
||||
private static final String KEY_DEFAULT_VALUE = "defaultValue";
|
||||
|
||||
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {};
|
||||
private static final TypeReference<Map<String, Object>> MAP_TYPE =
|
||||
new TypeReference<Map<String, Object>>() {};
|
||||
private static final TypeReference<List<FormUtils.ModifyFormFieldDefinition>>
|
||||
MODIFY_FIELD_LIST_TYPE = new TypeReference<>() {};
|
||||
MODIFY_FIELD_LIST_TYPE =
|
||||
new TypeReference<List<FormUtils.ModifyFormFieldDefinition>>() {};
|
||||
private static final TypeReference<List<FormUtils.NewFormFieldDefinition>> NEW_FIELD_LIST_TYPE =
|
||||
new TypeReference<>() {};
|
||||
private static final TypeReference<List<String>> STRING_LIST_TYPE = new TypeReference<>() {};
|
||||
private static final TypeReference<List<String>> STRING_LIST_TYPE =
|
||||
new TypeReference<List<String>>() {};
|
||||
|
||||
private FormPayloadParser() {}
|
||||
|
||||
|
||||
+3
-1
@@ -96,7 +96,9 @@ public class AddCommentsController {
|
||||
|
||||
List<CommentSpecDto> dtos;
|
||||
try {
|
||||
dtos = objectMapper.readValue(commentsJson, new TypeReference<>() {});
|
||||
dtos =
|
||||
objectMapper.readValue(
|
||||
commentsJson, new TypeReference<List<CommentSpecDto>>() {});
|
||||
} catch (JacksonException e) {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.BAD_REQUEST, "comments must be a JSON array of CommentSpec objects");
|
||||
|
||||
+13
@@ -338,6 +338,19 @@ public class ConfigController {
|
||||
// Premium/Enterprise settings
|
||||
configData.put("premiumEnabled", applicationProperties.getPremium().isEnabled());
|
||||
|
||||
// Whether this instance can link a Stirling (SaaS) account at all. The account-link
|
||||
// beans live in :proprietary and are @ConditionalOnProperty on this same key, so when
|
||||
// it is off they are absent and /api/v1/account-link/* returns 404. The frontend cannot
|
||||
// tell that 404 apart from "not linked yet", so it needs this told to it explicitly
|
||||
// before it can prompt anyone to link. Read from the environment rather than
|
||||
// AccountLinkProperties because :core must not depend on :proprietary.
|
||||
configData.put(
|
||||
"accountLinkAvailable",
|
||||
applicationContext
|
||||
.getEnvironment()
|
||||
.getProperty(
|
||||
"stirling.billing.account-link.enabled", Boolean.class, false));
|
||||
|
||||
// AI Engine settings
|
||||
ApplicationProperties.AiEngine aiEngineConfig = applicationProperties.getAiEngine();
|
||||
configData.put("aiEngineEnabled", aiEngineConfig.isEnabled());
|
||||
|
||||
+11
-8
@@ -4,7 +4,9 @@ import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -21,7 +23,7 @@ public class WeeklyActiveUsersService {
|
||||
private final Map<String, Instant> activeBrowsers = new ConcurrentHashMap<>();
|
||||
|
||||
// Track total unique browsers seen (overall)
|
||||
private long totalUniqueBrowsers = 0;
|
||||
private final AtomicLong totalUniqueBrowsers = new AtomicLong(0);
|
||||
|
||||
// Application start time
|
||||
private final Instant startTime = Instant.now();
|
||||
@@ -36,12 +38,12 @@ public class WeeklyActiveUsersService {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean isNewBrowser = !activeBrowsers.containsKey(browserId);
|
||||
activeBrowsers.put(browserId, Instant.now());
|
||||
Instant now = Instant.now();
|
||||
Instant previous = activeBrowsers.put(browserId, now);
|
||||
|
||||
if (isNewBrowser) {
|
||||
totalUniqueBrowsers++;
|
||||
log.debug("New browser recorded: {} (Total: {})", browserId, totalUniqueBrowsers);
|
||||
if (previous == null) {
|
||||
long total = totalUniqueBrowsers.incrementAndGet();
|
||||
log.debug("New browser recorded: {} (Total: {})", browserId, total);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +63,7 @@ public class WeeklyActiveUsersService {
|
||||
* @return Total unique browsers count
|
||||
*/
|
||||
public long getTotalUniqueBrowsers() {
|
||||
return totalUniqueBrowsers;
|
||||
return totalUniqueBrowsers.get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +90,8 @@ public class WeeklyActiveUsersService {
|
||||
activeBrowsers.entrySet().removeIf(entry -> entry.getValue().isBefore(sevenDaysAgo));
|
||||
}
|
||||
|
||||
/** Manual cleanup trigger (can be called by scheduled task if needed) */
|
||||
/** Scheduled cleanup trigger running every hour */
|
||||
@Scheduled(fixedRate = 3600000)
|
||||
public void performCleanup() {
|
||||
int sizeBefore = activeBrowsers.size();
|
||||
cleanupOldEntries();
|
||||
|
||||
+4
-2
@@ -154,7 +154,8 @@ public class PdfJsonFontService {
|
||||
return "otf";
|
||||
}
|
||||
if (signature == 0x74746366) {
|
||||
return "cff";
|
||||
log.debug("[FONT-DEBUG] TrueType Collection ('ttcf') font program is unsupported");
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -175,7 +176,8 @@ public class PdfJsonFontService {
|
||||
return "otf";
|
||||
}
|
||||
if (signature == 0x74746366) {
|
||||
return "cff";
|
||||
log.debug("[FONT-DEBUG] TrueType Collection ('ttcf') FontFile2 is unsupported");
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -15,26 +15,63 @@
|
||||
<encoder>
|
||||
<pattern>%d %p %c{1} [%thread] %m%n</pattern>
|
||||
</encoder>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_PATH}/auth-%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
<!-- SizeAndTime, not Time alone: the size trigger is what stops a
|
||||
runaway logger filling the disk (see GENERAL appender note).
|
||||
Archives are gzipped, so 64 MB of them holds far more than a
|
||||
day. Worst case on disk is one 100 MB live file plus the cap. -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_PATH}/auth-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
|
||||
<maxFileSize>100MB</maxFileSize>
|
||||
<maxHistory>7</maxHistory>
|
||||
<totalSizeCap>64MB</totalSizeCap>
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
<!-- Rolling File Appender for General Logs -->
|
||||
<!-- Rolling File Appender for General Logs
|
||||
|
||||
Why SizeAndTimeBased + totalSizeCap: a previous build of the v2 PDF
|
||||
text editor's reverse-CMap probe loop triggered PDSimpleFont to emit
|
||||
one "No Unicode mapping for .notdef" WARN per probed charcode per
|
||||
font per request. With TimeBasedRollingPolicy alone there was no
|
||||
size ceiling; info.log grew to 1.4 GB in a single day before the JVM
|
||||
choked. The class-level silencer fixes the specific offender, but
|
||||
this size cap is the defence-in-depth: any future logger that
|
||||
floods unexpectedly will roll + auto-delete instead of starving
|
||||
disk + Jetty threads. -->
|
||||
<appender name="GENERAL" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${LOG_PATH}/info.log</file>
|
||||
<encoder>
|
||||
<pattern>%d %p %c{1} [%thread] %m%n</pattern>
|
||||
</encoder>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_PATH}/info-%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_PATH}/info-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
|
||||
<maxFileSize>100MB</maxFileSize>
|
||||
<maxHistory>7</maxHistory>
|
||||
<totalSizeCap>256MB</totalSizeCap>
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
<!-- Suppress PDFBox PDSimpleFont's per-charcode .notdef WARN.
|
||||
|
||||
Required by the v2 PDF text editor's `buildReverseUnicodeMap`
|
||||
which DELIBERATELY iterates every charcode in 0..0xFFFF to
|
||||
discover the encoding-to-Unicode map of an embedded subset
|
||||
font. For any subset font ~99% of those probes hit .notdef,
|
||||
and the default WARN level for those misses turned info.log
|
||||
into a 1.4 GB monster overnight.
|
||||
|
||||
This declarative logback entry is the SOLE mechanism: it is
|
||||
visible to ops and revertable via configuration. An earlier
|
||||
build also mutated this logger's level from a static block in
|
||||
PdfTextEditorCharcodeController, which silenced the same
|
||||
warnings JVM-wide with no trace in any config file - that
|
||||
static block has been removed in favour of this entry. -->
|
||||
<logger name="org.apache.pdfbox.pdmodel.font.PDSimpleFont"
|
||||
level="ERROR" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="GENERAL"/>
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
|
||||
@@ -94,7 +94,7 @@
|
||||
{
|
||||
"moduleName": "com.fasterxml.jackson.core:jackson-core",
|
||||
"moduleUrl": "https://github.com/FasterXML/jackson-core",
|
||||
"moduleVersion": "2.22.1",
|
||||
"moduleVersion": "2.22.2",
|
||||
"moduleLicense": "The Apache Software License, Version 2.0",
|
||||
"moduleLicenseUrl": "https://www.apache.org/licenses/LICENSE-2.0.txt"
|
||||
},
|
||||
@@ -108,7 +108,7 @@
|
||||
{
|
||||
"moduleName": "com.fasterxml.jackson.core:jackson-databind",
|
||||
"moduleUrl": "https://github.com/FasterXML/jackson",
|
||||
"moduleVersion": "2.22.1",
|
||||
"moduleVersion": "2.22.2",
|
||||
"moduleLicense": "The Apache Software License, Version 2.0",
|
||||
"moduleLicenseUrl": "https://www.apache.org/licenses/LICENSE-2.0.txt"
|
||||
},
|
||||
@@ -143,7 +143,7 @@
|
||||
{
|
||||
"moduleName": "com.fasterxml.jackson:jackson-bom",
|
||||
"moduleUrl": "https://github.com/FasterXML/jackson-bom",
|
||||
"moduleVersion": "2.22.1",
|
||||
"moduleVersion": "2.22.2",
|
||||
"moduleLicense": "The Apache Software License, Version 2.0",
|
||||
"moduleLicenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.txt"
|
||||
},
|
||||
@@ -440,42 +440,14 @@
|
||||
{
|
||||
"moduleName": "com.stirling:jpdfium",
|
||||
"moduleUrl": "https://github.com/Stirling-Tools/JPDFium",
|
||||
"moduleVersion": "1.0.4",
|
||||
"moduleLicense": "MIT License",
|
||||
"moduleLicenseUrl": "https://opensource.org/licenses/MIT"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.stirling:jpdfium-natives-darwin-arm64",
|
||||
"moduleUrl": "https://github.com/Stirling-Tools/JPDFium",
|
||||
"moduleVersion": "1.0.4",
|
||||
"moduleLicense": "MIT License",
|
||||
"moduleLicenseUrl": "https://opensource.org/licenses/MIT"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.stirling:jpdfium-natives-darwin-x64",
|
||||
"moduleUrl": "https://github.com/Stirling-Tools/JPDFium",
|
||||
"moduleVersion": "1.0.4",
|
||||
"moduleLicense": "MIT License",
|
||||
"moduleLicenseUrl": "https://opensource.org/licenses/MIT"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.stirling:jpdfium-natives-linux-arm64",
|
||||
"moduleUrl": "https://github.com/Stirling-Tools/JPDFium",
|
||||
"moduleVersion": "1.0.4",
|
||||
"moduleVersion": "1.1.3",
|
||||
"moduleLicense": "MIT License",
|
||||
"moduleLicenseUrl": "https://opensource.org/licenses/MIT"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.stirling:jpdfium-natives-linux-x64",
|
||||
"moduleUrl": "https://github.com/Stirling-Tools/JPDFium",
|
||||
"moduleVersion": "1.0.4",
|
||||
"moduleLicense": "MIT License",
|
||||
"moduleLicenseUrl": "https://opensource.org/licenses/MIT"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.stirling:jpdfium-natives-windows-x64",
|
||||
"moduleUrl": "https://github.com/Stirling-Tools/JPDFium",
|
||||
"moduleVersion": "1.0.4",
|
||||
"moduleVersion": "1.1.3",
|
||||
"moduleLicense": "MIT License",
|
||||
"moduleLicenseUrl": "https://opensource.org/licenses/MIT"
|
||||
},
|
||||
@@ -521,36 +493,18 @@
|
||||
"moduleLicense": "GNU General Public License, version 2 with the GNU Classpath Exception",
|
||||
"moduleLicenseUrl": "https://www.gnu.org/software/classpath/license.html"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.common:common-image",
|
||||
"moduleVersion": "3.13.1",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.common:common-image",
|
||||
"moduleVersion": "3.14.0",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.common:common-io",
|
||||
"moduleVersion": "3.13.1",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.common:common-io",
|
||||
"moduleVersion": "3.14.0",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.common:common-lang",
|
||||
"moduleVersion": "3.13.1",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.common:common-lang",
|
||||
"moduleVersion": "3.14.0",
|
||||
@@ -569,12 +523,6 @@
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.imageio:imageio-core",
|
||||
"moduleVersion": "3.13.1",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.imageio:imageio-core",
|
||||
"moduleVersion": "3.14.0",
|
||||
@@ -587,12 +535,6 @@
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.imageio:imageio-metadata",
|
||||
"moduleVersion": "3.13.1",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.imageio:imageio-metadata",
|
||||
"moduleVersion": "3.14.0",
|
||||
@@ -605,24 +547,12 @@
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.imageio:imageio-tiff",
|
||||
"moduleVersion": "3.13.1",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.imageio:imageio-tiff",
|
||||
"moduleVersion": "3.14.0",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.imageio:imageio-webp",
|
||||
"moduleVersion": "3.13.1",
|
||||
"moduleLicense": "The BSD License",
|
||||
"moduleLicenseUrl": "https://github.com/haraldk/TwelveMonkeys#license"
|
||||
},
|
||||
{
|
||||
"moduleName": "com.twelvemonkeys.imageio:imageio-webp",
|
||||
"moduleVersion": "3.14.0",
|
||||
@@ -769,13 +699,6 @@
|
||||
"moduleLicense": "The Apache Software License, Version 2.0",
|
||||
"moduleLicenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.txt"
|
||||
},
|
||||
{
|
||||
"moduleName": "commons-beanutils:commons-beanutils",
|
||||
"moduleUrl": "https://commons.apache.org/proper/commons-beanutils",
|
||||
"moduleVersion": "1.11.0",
|
||||
"moduleLicense": "Apache-2.0",
|
||||
"moduleLicenseUrl": "https://www.apache.org/licenses/LICENSE-2.0.txt"
|
||||
},
|
||||
{
|
||||
"moduleName": "commons-cli:commons-cli",
|
||||
"moduleUrl": "http://commons.apache.org/proper/commons-cli/",
|
||||
@@ -790,13 +713,6 @@
|
||||
"moduleLicense": "Apache-2.0",
|
||||
"moduleLicenseUrl": "https://www.apache.org/licenses/LICENSE-2.0.txt"
|
||||
},
|
||||
{
|
||||
"moduleName": "commons-collections:commons-collections",
|
||||
"moduleUrl": "http://commons.apache.org/collections/",
|
||||
"moduleVersion": "3.2.2",
|
||||
"moduleLicense": "Apache License, Version 2.0",
|
||||
"moduleLicenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.txt"
|
||||
},
|
||||
{
|
||||
"moduleName": "commons-io:commons-io",
|
||||
"moduleUrl": "https://commons.apache.org/proper/commons-io/",
|
||||
@@ -1360,13 +1276,6 @@
|
||||
"moduleLicense": "Apache-2.0",
|
||||
"moduleLicenseUrl": "https://www.apache.org/licenses/LICENSE-2.0.txt"
|
||||
},
|
||||
{
|
||||
"moduleName": "org.apache.commons:commons-math3",
|
||||
"moduleUrl": "http://commons.apache.org/proper/commons-math/",
|
||||
"moduleVersion": "3.6.1",
|
||||
"moduleLicense": "Apache License, Version 2.0",
|
||||
"moduleLicenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.txt"
|
||||
},
|
||||
{
|
||||
"moduleName": "org.apache.commons:commons-text",
|
||||
"moduleUrl": "https://commons.apache.org/proper/commons-text",
|
||||
|
||||
+2
@@ -57,6 +57,8 @@ class ToolIODeclarationCoverageTest {
|
||||
// documents.
|
||||
"/api/v1/convert/pdf/text-editor",
|
||||
"/api/v1/convert/text-editor/pdf",
|
||||
// Charcode lookup for the v2 editor: returns glyph mappings, not a document.
|
||||
"/api/v1/general/pdf-text-editor",
|
||||
// Signing sessions, certificate checks and hardware token enumeration; the
|
||||
// signing tool itself is /api/v1/security/cert-sign, which is declared.
|
||||
"/api/v1/security/cert-sign/sessions",
|
||||
|
||||
+516
@@ -0,0 +1,516 @@
|
||||
package stirling.software.SPDF.controller.api;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.apache.pdfbox.Loader;
|
||||
import org.apache.pdfbox.cos.COSName;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.PDResources;
|
||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType0Font;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType3Font;
|
||||
import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Probe: what can PDFBox actually do for font ENCODING on real-world PDFs. This is a diagnostic
|
||||
* test (not a regression) - run with --tests PdfBoxFontEncodingProbeTest -i to see stdout.
|
||||
*
|
||||
* <p>Answers these questions:
|
||||
*
|
||||
* <ol>
|
||||
* <li>Type0/CIDFontType2 subset: can we add a new glyph not in the original subset? (no, encode
|
||||
* throws IllegalArgumentException).
|
||||
* <li>Type1: same question.
|
||||
* <li>TrueType: same question.
|
||||
* <li>Can we load a fresh TTF via PDType0Font.load(doc, file) and write text with it? (yes,
|
||||
* primary path).
|
||||
* <li>Round-trip via getFontStream / re-embed - can it rehabilitate Type3? (no - Type3 has no
|
||||
* FontFile* program at all).
|
||||
* <li>What fonts ship with PDFBox / fontbox? (only LiberationSans-Regular.ttf + AFM for the 14
|
||||
* standard fonts; CFF/Type1 binaries are NOT bundled - Standard14Fonts.getMappedFontName
|
||||
* redirects unmappable ones to LiberationSans).
|
||||
* </ol>
|
||||
*/
|
||||
@Disabled(
|
||||
"Diagnostic probe: dumps PDFBox font encoding tables to stdout and asserts nothing. Kept for font debugging; run manually.")
|
||||
public class PdfBoxFontEncodingProbeTest {
|
||||
|
||||
private static final Path PROJECT_ROOT =
|
||||
Paths.get(System.getProperty("user.dir")).getParent().getParent();
|
||||
|
||||
private static final Path SAMPLE =
|
||||
PROJECT_ROOT.resolve("frontend/editor/public/samples/Sample.pdf");
|
||||
|
||||
private static final Path[] EXTRA_FIXTURES = {
|
||||
PROJECT_ROOT.resolve("frontend/editor/src/core/tests/test-fixtures/stirling-marketing.pdf"),
|
||||
PROJECT_ROOT.resolve("frontend/editor/src/core/tests/test-fixtures/multi-page-sample.pdf"),
|
||||
PROJECT_ROOT.resolve("frontend/editor/src/core/tests/test-fixtures/big-sample.pdf"),
|
||||
PROJECT_ROOT.resolve("frontend/editor/src/core/tests/test-fixtures/paragraph-sample.pdf"),
|
||||
PROJECT_ROOT.resolve("frontend/editor/src/core/tests/test-fixtures/user-sample.pdf"),
|
||||
};
|
||||
|
||||
/**
|
||||
* Rasterize the Q4b output (Sample.pdf with injected Liberation text) to confirm the new text
|
||||
* actually renders on top of the existing Type3 content.
|
||||
*/
|
||||
@Test
|
||||
public void probeRenderInjectedSample() throws IOException {
|
||||
Path liberation =
|
||||
PROJECT_ROOT.resolve(
|
||||
"app/core/src/main/resources/static/fonts/LiberationSans-Regular.ttf");
|
||||
byte[] pdfBytes = Files.readAllBytes(SAMPLE);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try (PDDocument doc = Loader.loadPDF(pdfBytes)) {
|
||||
PDPage page = doc.getPage(0);
|
||||
PDType0Font ttf;
|
||||
try (InputStream in = Files.newInputStream(liberation)) {
|
||||
ttf = PDType0Font.load(doc, in, true);
|
||||
}
|
||||
try (PDPageContentStream cs =
|
||||
new PDPageContentStream(
|
||||
doc, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
|
||||
cs.beginText();
|
||||
cs.setFont(ttf, 24);
|
||||
cs.newLineAtOffset(50, 120);
|
||||
cs.showText("INJECTED via PDType0Font.load - $@#&Z");
|
||||
cs.endText();
|
||||
}
|
||||
doc.save(out);
|
||||
}
|
||||
// Rasterize page 0 to a PNG so we can eyeball it.
|
||||
try (PDDocument check = Loader.loadPDF(out.toByteArray())) {
|
||||
PDFRenderer renderer = new PDFRenderer(check);
|
||||
java.awt.image.BufferedImage img = renderer.renderImageWithDPI(0, 100);
|
||||
// Build dir, not the repo root: this render is a debugging aid and was
|
||||
// twice committed by accident when it landed in the working tree.
|
||||
Path png =
|
||||
Paths.get(System.getProperty("user.dir"), "build", "probe-output")
|
||||
.resolve("pdfbox-probe-q4b-rendered.png");
|
||||
Files.createDirectories(png.getParent());
|
||||
ImageIO.write(img, "PNG", png.toFile());
|
||||
System.out.println(
|
||||
"Rendered injected sample to "
|
||||
+ png
|
||||
+ " - "
|
||||
+ img.getWidth()
|
||||
+ "x"
|
||||
+ img.getHeight());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a PDF in memory that uses a Type0/CIDFontType2 subset font (the kind Word / InDesign /
|
||||
* LibreOffice produce), then probe whether encode() can add a glyph that wasn't in the original
|
||||
* subset.
|
||||
*/
|
||||
@Test
|
||||
public void probeType0CIDFontType2Subset() throws IOException {
|
||||
System.out.println(
|
||||
"\n##################################################################\n"
|
||||
+ "Q1 probe: Type0/CIDFontType2 SUBSET can/cannot add new glyphs\n"
|
||||
+ "##################################################################\n");
|
||||
Path liberation =
|
||||
PROJECT_ROOT.resolve(
|
||||
"app/core/src/main/resources/static/fonts/LiberationSans-Regular.ttf");
|
||||
|
||||
// Build a PDF that contains only "abc" subsetted from LiberationSans.
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
PDPage page = new PDPage();
|
||||
doc.addPage(page);
|
||||
PDType0Font subset;
|
||||
try (InputStream in = Files.newInputStream(liberation)) {
|
||||
subset = PDType0Font.load(doc, in, true /* embedSubset */);
|
||||
}
|
||||
try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
|
||||
cs.beginText();
|
||||
cs.setFont(subset, 12);
|
||||
cs.newLineAtOffset(100, 700);
|
||||
cs.showText("abc");
|
||||
cs.endText();
|
||||
}
|
||||
doc.save(baos);
|
||||
}
|
||||
|
||||
// Reload the produced PDF and try to add a NEW glyph through the embedded subset font.
|
||||
byte[] subsetPdf = baos.toByteArray();
|
||||
try (PDDocument doc = Loader.loadPDF(subsetPdf)) {
|
||||
PDResources res = doc.getPage(0).getResources();
|
||||
for (COSName fn : res.getFontNames()) {
|
||||
PDFont f = res.getFont(fn);
|
||||
System.out.println(
|
||||
" Subset font in saved PDF: "
|
||||
+ f.getName()
|
||||
+ " ("
|
||||
+ f.getClass().getSimpleName()
|
||||
+ ", subType="
|
||||
+ f.getSubType()
|
||||
+ ")");
|
||||
for (String ch : new String[] {"a", "b", "c", "Z", "z", "0", "$", "@", "X", " "}) {
|
||||
try {
|
||||
byte[] enc = f.encode(ch);
|
||||
StringBuilder hex = new StringBuilder();
|
||||
for (byte b : enc) hex.append(String.format("%02X ", b & 0xff));
|
||||
System.out.println(
|
||||
" encode('" + ch + "') -> [" + hex.toString().trim() + "] OK");
|
||||
} catch (UnsupportedOperationException uoe) {
|
||||
System.out.println(" encode('" + ch + "') UNSUPPORTED");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.out.println(
|
||||
" encode('" + ch + "') MISSING - " + iae.getMessage());
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(" encode('" + ch + "') IO ERR - " + ioe.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void probeExtraFixtures() throws IOException {
|
||||
System.out.println(
|
||||
"\n##################################################################\n"
|
||||
+ "Extra fixture font-class probe\n"
|
||||
+ "##################################################################\n");
|
||||
for (Path fixture : EXTRA_FIXTURES) {
|
||||
if (!Files.exists(fixture)) {
|
||||
System.out.println("(missing) " + fixture);
|
||||
continue;
|
||||
}
|
||||
System.out.println("\n=== " + fixture.getFileName() + " ===");
|
||||
byte[] bytes = Files.readAllBytes(fixture);
|
||||
try (PDDocument doc = Loader.loadPDF(bytes)) {
|
||||
Set<COSName> seen = new HashSet<>();
|
||||
for (int p = 0; p < doc.getNumberOfPages(); p++) {
|
||||
PDPage page = doc.getPage(p);
|
||||
PDResources res = page.getResources();
|
||||
if (res == null) continue;
|
||||
for (COSName name : res.getFontNames()) {
|
||||
if (!seen.add(name)) continue;
|
||||
try {
|
||||
PDFont f = res.getFont(name);
|
||||
if (f == null) continue;
|
||||
String fontFile = "none";
|
||||
PDFontDescriptor d = f.getFontDescriptor();
|
||||
if (d != null) {
|
||||
if (d.getFontFile() != null) fontFile = "FontFile";
|
||||
else if (d.getFontFile2() != null) fontFile = "FontFile2";
|
||||
else if (d.getFontFile3() != null) fontFile = "FontFile3";
|
||||
}
|
||||
String z = "?";
|
||||
try {
|
||||
f.encode("Z");
|
||||
z = "OK";
|
||||
} catch (UnsupportedOperationException ex) {
|
||||
z = "UNSUPPORTED";
|
||||
} catch (IllegalArgumentException ex) {
|
||||
z = "MISSING";
|
||||
} catch (IOException ex) {
|
||||
z = "IO_ERR";
|
||||
}
|
||||
System.out.println(
|
||||
" page "
|
||||
+ p
|
||||
+ " "
|
||||
+ name.getName()
|
||||
+ " -> "
|
||||
+ f.getName()
|
||||
+ " "
|
||||
+ f.getClass().getSimpleName()
|
||||
+ " ("
|
||||
+ f.getSubType()
|
||||
+ ", "
|
||||
+ fontFile
|
||||
+ ", embed="
|
||||
+ f.isEmbedded()
|
||||
+ ") encode('Z')="
|
||||
+ z);
|
||||
} catch (IOException e) {
|
||||
System.out.println(
|
||||
" page "
|
||||
+ p
|
||||
+ " "
|
||||
+ name.getName()
|
||||
+ " load failed: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void probeAllQuestions() throws IOException {
|
||||
System.out.println(
|
||||
"\n##################################################################\n"
|
||||
+ "PDFBox font-encoding probe (Sample.pdf + bundled fallback fonts)\n"
|
||||
+ "##################################################################\n");
|
||||
|
||||
// Discover every font in Sample.pdf so we have a real-world test set.
|
||||
byte[] pdfBytes = Files.readAllBytes(SAMPLE);
|
||||
try (PDDocument doc = Loader.loadPDF(pdfBytes)) {
|
||||
List<PDFont> allFonts = new ArrayList<>();
|
||||
Set<COSName> seen = new HashSet<>();
|
||||
for (int p = 0; p < doc.getNumberOfPages(); p++) {
|
||||
PDPage page = doc.getPage(p);
|
||||
PDResources res = page.getResources();
|
||||
if (res == null) continue;
|
||||
for (COSName name : res.getFontNames()) {
|
||||
if (!seen.add(name)) continue;
|
||||
try {
|
||||
PDFont f = res.getFont(name);
|
||||
if (f != null) allFonts.add(f);
|
||||
} catch (Exception e) {
|
||||
System.out.println(
|
||||
" (skipped " + name.getName() + " - " + e.getMessage() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(
|
||||
"Discovered " + allFonts.size() + " unique fonts across Sample.pdf:");
|
||||
for (PDFont f : allFonts) {
|
||||
System.out.println(
|
||||
" - "
|
||||
+ f.getName()
|
||||
+ " ("
|
||||
+ f.getClass().getSimpleName()
|
||||
+ ", subType="
|
||||
+ f.getSubType()
|
||||
+ ", embedded="
|
||||
+ f.isEmbedded()
|
||||
+ ")");
|
||||
}
|
||||
|
||||
// Q1/Q2/Q3
|
||||
// Try encoding a char that is NEVER in Sample.pdf via each font.
|
||||
// 'Z' is unlikely to be in the subset for most marketing pages.
|
||||
// Try several candidates to surface what each font can/can't add.
|
||||
String[] candidates = {"Z", "$", "@", "#", "Q", "&", "A", "0", "M"};
|
||||
for (PDFont f : allFonts) {
|
||||
System.out.println("\n=== Encode-probe for font: " + f.getName() + " ===");
|
||||
for (String ch : candidates) {
|
||||
try {
|
||||
byte[] enc = f.encode(ch);
|
||||
StringBuilder hex = new StringBuilder();
|
||||
for (byte b : enc) hex.append(String.format("%02X ", b & 0xff));
|
||||
System.out.println(
|
||||
" encode('" + ch + "') -> [" + hex.toString().trim() + "] OK");
|
||||
} catch (UnsupportedOperationException uoe) {
|
||||
System.out.println(
|
||||
" encode('" + ch + "') UNSUPPORTED: " + uoe.getMessage());
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.out.println(" encode('" + ch + "') MISSING: " + iae.getMessage());
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(" encode('" + ch + "') IO ERR: " + ioe.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Q5
|
||||
// For each font, see what's in the FontFile* stream - this is what we'd
|
||||
// have to round-trip through to "rehabilitate" a Type3 font.
|
||||
System.out.println("\n=== FontFile stream availability (Q5) ===");
|
||||
for (PDFont f : allFonts) {
|
||||
String kind = "none";
|
||||
int size = 0;
|
||||
PDFontDescriptor d = f.getFontDescriptor();
|
||||
if (d != null) {
|
||||
if (d.getFontFile() != null) {
|
||||
kind = "FontFile (Type1)";
|
||||
size = streamBytes(d.getFontFile().getCOSObject().createInputStream());
|
||||
} else if (d.getFontFile2() != null) {
|
||||
kind = "FontFile2 (TTF)";
|
||||
size = streamBytes(d.getFontFile2().getCOSObject().createInputStream());
|
||||
} else if (d.getFontFile3() != null) {
|
||||
kind = "FontFile3 (CFF/OpenType)";
|
||||
size = streamBytes(d.getFontFile3().getCOSObject().createInputStream());
|
||||
}
|
||||
}
|
||||
System.out.println(
|
||||
" "
|
||||
+ f.getName()
|
||||
+ " ("
|
||||
+ f.getClass().getSimpleName()
|
||||
+ "): "
|
||||
+ kind
|
||||
+ " ("
|
||||
+ size
|
||||
+ " bytes)");
|
||||
if (f instanceof PDType3Font) {
|
||||
System.out.println(
|
||||
" -> Type3 has CharProc streams, NOT a FontFile binary."
|
||||
+ " getFontStream() returns null. Round-trip rehab is impossible:");
|
||||
System.out.println(
|
||||
" each glyph is a mini content stream, not a glyph outline in a"
|
||||
+ " standard font format. We'd need to rasterize each CharProc to"
|
||||
+ " glyph outlines + build a fresh TTF/CFF from scratch.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Q4: PDType0Font.load(doc, file) round-trip
|
||||
System.out.println("\n=== Q4: load fresh TTF and write text to a fresh PDF ===");
|
||||
Path liberation =
|
||||
PROJECT_ROOT.resolve(
|
||||
"app/core/src/main/resources/static/fonts/LiberationSans-Regular.ttf");
|
||||
if (!Files.exists(liberation)) {
|
||||
System.out.println(" Liberation TTF not found at " + liberation);
|
||||
} else {
|
||||
try (PDDocument out = new PDDocument()) {
|
||||
PDPage page = new PDPage();
|
||||
out.addPage(page);
|
||||
PDType0Font ttf;
|
||||
try (InputStream in = Files.newInputStream(liberation)) {
|
||||
ttf = PDType0Font.load(out, in, true /* embedSubset */);
|
||||
}
|
||||
System.out.println(
|
||||
" Loaded TTF -> "
|
||||
+ ttf.getName()
|
||||
+ " ("
|
||||
+ ttf.getClass().getSimpleName()
|
||||
+ ")");
|
||||
String testText = "Hello world! 0123 Z $ @";
|
||||
byte[] encoded = ttf.encode(testText);
|
||||
System.out.println(
|
||||
" Encoded "
|
||||
+ testText.length()
|
||||
+ " chars -> "
|
||||
+ encoded.length
|
||||
+ " bytes (Identity-H = 2 bytes/glyph)");
|
||||
try (PDPageContentStream cs = new PDPageContentStream(out, page)) {
|
||||
cs.beginText();
|
||||
cs.setFont(ttf, 12);
|
||||
cs.newLineAtOffset(100, 700);
|
||||
cs.showText(testText);
|
||||
cs.endText();
|
||||
}
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
out.save(baos);
|
||||
Path tmp = Files.createTempFile("pdfbox-probe-q4-", ".pdf");
|
||||
Files.write(tmp, baos.toByteArray());
|
||||
System.out.println(
|
||||
" Wrote fresh-TTF PDF to "
|
||||
+ tmp
|
||||
+ " ("
|
||||
+ baos.size()
|
||||
+ " bytes) - opens cleanly.");
|
||||
|
||||
// Re-load to confirm the new font is embedded properly.
|
||||
try (PDDocument check = Loader.loadPDF(baos.toByteArray())) {
|
||||
PDResources res = check.getPage(0).getResources();
|
||||
for (COSName fn : res.getFontNames()) {
|
||||
PDFont f = res.getFont(fn);
|
||||
System.out.println(
|
||||
" embedded font: "
|
||||
+ f.getName()
|
||||
+ " ("
|
||||
+ f.getClass().getSimpleName()
|
||||
+ ", embedded="
|
||||
+ f.isEmbedded()
|
||||
+ ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Q4b: load TTF into an EXISTING PDF (Sample.pdf) and append text
|
||||
System.out.println(
|
||||
"\n=== Q4b: load TTF into EXISTING Sample.pdf and write text on page 0 ===");
|
||||
try (PDDocument doc = Loader.loadPDF(pdfBytes)) {
|
||||
PDPage page = doc.getPage(0);
|
||||
PDType0Font ttf;
|
||||
try (InputStream in = Files.newInputStream(liberation)) {
|
||||
ttf = PDType0Font.load(doc, in, true);
|
||||
}
|
||||
// append-mode content stream so we don't disturb existing graphics
|
||||
try (PDPageContentStream cs =
|
||||
new PDPageContentStream(
|
||||
doc,
|
||||
page,
|
||||
PDPageContentStream.AppendMode.APPEND,
|
||||
true /* compress */,
|
||||
true /* resetContext */)) {
|
||||
cs.beginText();
|
||||
cs.setFont(ttf, 12);
|
||||
cs.newLineAtOffset(50, 50);
|
||||
cs.showText("Injected via PDType0Font.load - $@#&");
|
||||
cs.endText();
|
||||
}
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
doc.save(baos);
|
||||
Path tmp = Files.createTempFile("pdfbox-probe-q4b-", ".pdf");
|
||||
Files.write(tmp, baos.toByteArray());
|
||||
System.out.println(
|
||||
" Wrote injected-text PDF to " + tmp + " (" + baos.size() + " bytes).");
|
||||
|
||||
// Verify by re-reading: how many fonts now on page 0?
|
||||
try (PDDocument check = Loader.loadPDF(baos.toByteArray())) {
|
||||
PDResources res = check.getPage(0).getResources();
|
||||
int count = 0;
|
||||
for (COSName fn : res.getFontNames()) {
|
||||
PDFont f = res.getFont(fn);
|
||||
count++;
|
||||
System.out.println(
|
||||
" page-0 font: "
|
||||
+ fn.getName()
|
||||
+ " -> "
|
||||
+ f.getName()
|
||||
+ " ("
|
||||
+ f.getClass().getSimpleName()
|
||||
+ ")");
|
||||
}
|
||||
System.out.println(" Total fonts on page 0: " + count);
|
||||
}
|
||||
}
|
||||
|
||||
// Q6: what fonts ship in PDFBox / fontbox
|
||||
System.out.println("\n=== Q6: bundled fonts (Standard14 redirect probe) ===");
|
||||
for (Standard14Fonts.FontName fn : Standard14Fonts.FontName.values()) {
|
||||
PDType1Font f = new PDType1Font(fn);
|
||||
String mapped = "" + Standard14Fonts.getMappedFontName(fn.getName());
|
||||
System.out.println(
|
||||
" Standard14 "
|
||||
+ fn.getName()
|
||||
+ " -> mapped='"
|
||||
+ mapped
|
||||
+ "' name="
|
||||
+ f.getName());
|
||||
}
|
||||
System.out.println(
|
||||
" (PDFBox bundles ONLY LiberationSans-Regular.ttf as a binary; the AFMs cover"
|
||||
+ " metrics for the 14 standard fonts but rendering Helvetica/Times/Courier"
|
||||
+ " glyphs falls back to LiberationSans glyphs at runtime when no system font"
|
||||
+ " is found.)");
|
||||
}
|
||||
|
||||
private static int streamBytes(InputStream is) {
|
||||
try (InputStream it = is) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[4096];
|
||||
int n;
|
||||
while ((n = it.read(buf)) >= 0) baos.write(buf, 0, n);
|
||||
return baos.size();
|
||||
} catch (IOException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
+755
@@ -0,0 +1,755 @@
|
||||
package stirling.software.SPDF.controller.api;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Base64;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||
import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import stirling.software.SPDF.controller.api.PdfTextEditorCharcodeController.EncodeCharcodesRequest;
|
||||
import stirling.software.SPDF.controller.api.PdfTextEditorCharcodeController.EncodeCharcodesResponse;
|
||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||
import stirling.software.common.service.PdfMetadataService;
|
||||
|
||||
/**
|
||||
* Regression coverage for the v2 text editor "spaces render as „" bug.
|
||||
*
|
||||
* <p>mushroom-life.pdf is a LaTeX document whose embedded LMRoman subset font has NO real space
|
||||
* glyph, yet {@code font.encode(" ")} still returns charcode 0x20 without throwing. Reusing that
|
||||
* code via {@code FPDFText_SetCharcodes} paints whatever glyph sits at subset code 0x20 - the
|
||||
* quotedblbase „. The controller must therefore report whitespace as {@code missing} so the
|
||||
* frontend emits it as a positional gap instead of a reused glyph.
|
||||
*/
|
||||
class PdfTextEditorCharcodeControllerTest {
|
||||
|
||||
private static PdfTextEditorCharcodeController controller() {
|
||||
return new PdfTextEditorCharcodeController(
|
||||
new CustomPDFDocumentFactory(mock(PdfMetadataService.class)));
|
||||
}
|
||||
|
||||
private static String mushroomBase64() throws Exception {
|
||||
try (InputStream in =
|
||||
PdfTextEditorCharcodeControllerTest.class.getResourceAsStream(
|
||||
"/pdftexteditor/mushroom-life.pdf")) {
|
||||
assertThat(in).as("mushroom-life.pdf test resource").isNotNull();
|
||||
return Base64.getEncoder().encodeToString(in.readAllBytes());
|
||||
}
|
||||
}
|
||||
|
||||
private static EncodeCharcodesRequest request(String text) throws Exception {
|
||||
EncodeCharcodesRequest req = new EncodeCharcodesRequest();
|
||||
req.setPdfBase64(mushroomBase64());
|
||||
req.setPageIndex(0);
|
||||
// findFontByToUnicode locates the font via the ToUnicode CMap - "M" exists on page 0.
|
||||
req.setLocatorChar("M");
|
||||
req.setText(text);
|
||||
return req;
|
||||
}
|
||||
|
||||
@Test
|
||||
void spaceIsReportedMissingNeverEncoded() throws Exception {
|
||||
PdfTextEditorCharcodeController controller = controller();
|
||||
ResponseEntity<EncodeCharcodesResponse> resp = controller.encodeCharcodes(request(" "));
|
||||
|
||||
EncodeCharcodesResponse body = resp.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNull();
|
||||
// The space must be reported missing, NOT handed back as a charcode
|
||||
// (0x20) the frontend would reuse into the „ glyph.
|
||||
assertThat(body.getMissing()).containsExactly(" ");
|
||||
assertThat(body.getCharcodes()).isNullOrEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void realCharsEncodeWhileWhitespaceStaysAGap() throws Exception {
|
||||
PdfTextEditorCharcodeController controller = controller();
|
||||
// "M M" - both M's must encode to real charcodes; only the space is a gap.
|
||||
ResponseEntity<EncodeCharcodesResponse> resp = controller.encodeCharcodes(request("M M"));
|
||||
|
||||
EncodeCharcodesResponse body = resp.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNull();
|
||||
assertThat(body.getCharcodes()).as("both M glyphs encode").hasSize(2);
|
||||
assertThat(body.getMissing()).containsExactly(" ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void tabAndNewlineAreAlsoTreatedAsGaps() throws Exception {
|
||||
PdfTextEditorCharcodeController controller = controller();
|
||||
ResponseEntity<EncodeCharcodesResponse> resp = controller.encodeCharcodes(request("\t\n"));
|
||||
|
||||
EncodeCharcodesResponse body = resp.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getMissing()).containsExactly("\t", "\n");
|
||||
assertThat(body.getCharcodes()).isNullOrEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* A page with two fonts that BOTH render 'A'. {@code fontName} must select which one to encode
|
||||
* against - the cross-font fix. Without it the first font in resources order won wins and a
|
||||
* cross-font edit got the wrong font's charcode.
|
||||
*/
|
||||
private static String twoFontBase64() throws Exception {
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
PDPage page = new PDPage();
|
||||
doc.addPage(page);
|
||||
PDType1Font helvetica = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
|
||||
PDType1Font times = new PDType1Font(Standard14Fonts.FontName.TIMES_ROMAN);
|
||||
try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
|
||||
cs.beginText();
|
||||
cs.setFont(helvetica, 12);
|
||||
cs.newLineAtOffset(72, 720);
|
||||
cs.showText("A");
|
||||
cs.endText();
|
||||
cs.beginText();
|
||||
cs.setFont(times, 12);
|
||||
cs.newLineAtOffset(72, 700);
|
||||
cs.showText("A");
|
||||
cs.endText();
|
||||
}
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
doc.save(bos);
|
||||
return Base64.getEncoder().encodeToString(bos.toByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static EncodeCharcodesRequest twoFontRequest(String fontName) throws Exception {
|
||||
EncodeCharcodesRequest req = new EncodeCharcodesRequest();
|
||||
req.setPdfBase64(twoFontBase64());
|
||||
req.setPageIndex(0);
|
||||
req.setLocatorChar("A");
|
||||
req.setFontName(fontName);
|
||||
req.setText("A");
|
||||
return req;
|
||||
}
|
||||
|
||||
@Test
|
||||
void fontNameDisambiguatesBetweenTwoFontsRenderingTheSameChar() throws Exception {
|
||||
PdfTextEditorCharcodeController controller = controller();
|
||||
|
||||
// Targeting Times-Roman must encode against Times-Roman, not whichever
|
||||
// font happens to appear first in the page's font resources.
|
||||
EncodeCharcodesResponse times =
|
||||
controller.encodeCharcodes(twoFontRequest("Times-Roman")).getBody();
|
||||
assertThat(times).isNotNull();
|
||||
assertThat(times.getError()).isNull();
|
||||
assertThat(times.getNote()).contains("Times-Roman");
|
||||
assertThat(times.getCharcodes()).hasSize(1);
|
||||
|
||||
// Targeting Helvetica must encode against Helvetica.
|
||||
EncodeCharcodesResponse helv =
|
||||
controller.encodeCharcodes(twoFontRequest("Helvetica")).getBody();
|
||||
assertThat(helv).isNotNull();
|
||||
assertThat(helv.getError()).isNull();
|
||||
assertThat(helv.getNote()).contains("Helvetica");
|
||||
assertThat(helv.getCharcodes()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownFontNameReportsNoFontInsteadOfWrongFont() throws Exception {
|
||||
PdfTextEditorCharcodeController controller = controller();
|
||||
// A name that matches no font on the page must NOT silently encode
|
||||
// against a different font: the frontend writes the returned charcodes
|
||||
// into the NAMED font's text object, so a first-match fallback would
|
||||
// bake wrong glyphs. It must report failure so the caller falls back.
|
||||
EncodeCharcodesResponse body =
|
||||
controller.encodeCharcodes(twoFontRequest("DoesNotExist")).getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).contains("no font");
|
||||
assertThat(body.getCharcodes()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingRequiredFieldsReturns400() {
|
||||
EncodeCharcodesRequest req = new EncodeCharcodesRequest();
|
||||
req.setPdfBase64("AAAA");
|
||||
req.setLocatorChar("M");
|
||||
// text is null
|
||||
ResponseEntity<EncodeCharcodesResponse> resp = controller().encodeCharcodes(req);
|
||||
assertThat(resp.getStatusCode().value()).isEqualTo(400);
|
||||
assertThat(resp.getBody()).isNotNull();
|
||||
assertThat(resp.getBody().getError()).isEqualTo("missing required fields");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidBase64Returns400() {
|
||||
EncodeCharcodesRequest req = new EncodeCharcodesRequest();
|
||||
req.setPdfBase64("!!!notbase64!!!");
|
||||
req.setLocatorChar("M");
|
||||
req.setText("M");
|
||||
ResponseEntity<EncodeCharcodesResponse> resp = controller().encodeCharcodes(req);
|
||||
assertThat(resp.getStatusCode().value()).isEqualTo(400);
|
||||
assertThat(resp.getBody()).isNotNull();
|
||||
assertThat(resp.getBody().getError()).isEqualTo("pdfBase64 is not valid base64");
|
||||
}
|
||||
|
||||
@Test
|
||||
void pageIndexOutOfRangeReturns400() throws Exception {
|
||||
EncodeCharcodesRequest req = request("M");
|
||||
req.setPageIndex(999);
|
||||
ResponseEntity<EncodeCharcodesResponse> resp = controller().encodeCharcodes(req);
|
||||
assertThat(resp.getStatusCode().value()).isEqualTo(400);
|
||||
assertThat(resp.getBody()).isNotNull();
|
||||
assertThat(resp.getBody().getError()).isEqualTo("pageIndex out of range");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonPdfBytesReturnsGenericError() {
|
||||
EncodeCharcodesRequest req = new EncodeCharcodesRequest();
|
||||
req.setPdfBase64(Base64.getEncoder().encodeToString("not a pdf".getBytes()));
|
||||
req.setLocatorChar("M");
|
||||
req.setText("M");
|
||||
// Must not throw, and must not leak the raw PDFBox parser message.
|
||||
ResponseEntity<EncodeCharcodesResponse> resp = controller().encodeCharcodes(req);
|
||||
assertThat(resp.getStatusCode().is4xxClientError()).isTrue();
|
||||
assertThat(resp.getBody()).isNotNull();
|
||||
assertThat(resp.getBody().getError()).isEqualTo("failed to load PDF");
|
||||
}
|
||||
|
||||
@Test
|
||||
void absentLocatorCharReturns200WithError() throws Exception {
|
||||
// U+FFFF never appears in the document, so no font matches.
|
||||
ResponseEntity<EncodeCharcodesResponse> resp =
|
||||
controller().encodeCharcodes(requestWithLocator(""));
|
||||
assertThat(resp.getStatusCode().value()).isEqualTo(200);
|
||||
EncodeCharcodesResponse body = resp.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNotNull();
|
||||
assertThat(body.getCharcodes()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void oversizePdfRejected() {
|
||||
EncodeCharcodesRequest req = new EncodeCharcodesRequest();
|
||||
// A base64 string long enough that length/4*3 exceeds the 100MB cap, without
|
||||
// ever allocating the decoded bytes (the guard runs before decode).
|
||||
char[] huge = new char[140 * 1024 * 1024];
|
||||
java.util.Arrays.fill(huge, 'A');
|
||||
req.setPdfBase64(new String(huge));
|
||||
req.setLocatorChar("M");
|
||||
req.setText("M");
|
||||
ResponseEntity<EncodeCharcodesResponse> resp = controller().encodeCharcodes(req);
|
||||
assertThat(resp.getStatusCode().value()).isEqualTo(413);
|
||||
assertThat(resp.getBody()).isNotNull();
|
||||
assertThat(resp.getBody().getError()).isEqualTo("pdf too large");
|
||||
}
|
||||
|
||||
private static EncodeCharcodesRequest requestWithLocator(String locator) throws Exception {
|
||||
EncodeCharcodesRequest req = request("M");
|
||||
req.setLocatorChar(locator);
|
||||
return req;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a page whose resources declare {@code filler} fonts that do NOT render 'A' (Symbol /
|
||||
* ZapfDingbats have non-Latin encodings) plus, optionally, a trailing Helvetica that does. The
|
||||
* Standard14 probe upper bound is 256 so each scan is cheap.
|
||||
*/
|
||||
private static String manyFontsBase64(int filler, boolean trailingTarget) throws Exception {
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
PDPage page = new PDPage();
|
||||
doc.addPage(page);
|
||||
org.apache.pdfbox.pdmodel.PDResources resources =
|
||||
new org.apache.pdfbox.pdmodel.PDResources();
|
||||
for (int n = 0; n < filler; n++) {
|
||||
Standard14Fonts.FontName fn =
|
||||
(n % 2 == 0)
|
||||
? Standard14Fonts.FontName.SYMBOL
|
||||
: Standard14Fonts.FontName.ZAPF_DINGBATS;
|
||||
resources.put(
|
||||
org.apache.pdfbox.cos.COSName.getPDFName("Ff" + n), new PDType1Font(fn));
|
||||
}
|
||||
if (trailingTarget) {
|
||||
resources.put(
|
||||
org.apache.pdfbox.cos.COSName.getPDFName("Target"),
|
||||
new PDType1Font(Standard14Fonts.FontName.HELVETICA));
|
||||
}
|
||||
page.setResources(resources);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
doc.save(bos);
|
||||
return Base64.getEncoder().encodeToString(bos.toByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static EncodeCharcodesRequest manyFontsRequest(String base64) {
|
||||
EncodeCharcodesRequest req = new EncodeCharcodesRequest();
|
||||
req.setPdfBase64(base64);
|
||||
req.setPageIndex(0);
|
||||
req.setLocatorChar("A");
|
||||
req.setText("A");
|
||||
return req;
|
||||
}
|
||||
|
||||
@Test
|
||||
void targetFontFoundAmongManyFonts() throws Exception {
|
||||
// 60 non-matching fonts then the Helvetica target, all within the 64-font cap.
|
||||
ResponseEntity<EncodeCharcodesResponse> resp =
|
||||
controller().encodeCharcodes(manyFontsRequest(manyFontsBase64(60, true)));
|
||||
assertThat(resp.getStatusCode().value()).isEqualTo(200);
|
||||
EncodeCharcodesResponse body = resp.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNull();
|
||||
assertThat(body.getCharcodes()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void targetBeyondFontCapReturnsGracefulNoFont() throws Exception {
|
||||
// 64 non-matching fonts then the target at position 65 - the scan cap stops
|
||||
// before reaching it, so we get a graceful no-font error rather than a full scan.
|
||||
ResponseEntity<EncodeCharcodesResponse> resp =
|
||||
controller().encodeCharcodes(manyFontsRequest(manyFontsBase64(64, true)));
|
||||
assertThat(resp.getStatusCode().value()).isEqualTo(200);
|
||||
EncodeCharcodesResponse body = resp.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNotNull();
|
||||
assertThat(body.getCharcodes()).isNull();
|
||||
}
|
||||
|
||||
// Same-family sibling subsets. One document can embed several subsets of
|
||||
// one family, each re-encoded by order of first glyph use, so a letter has
|
||||
// a different charcode in each ("R" = 0x21 in one, 0x22 in its sibling).
|
||||
// FPDFFont_GetBaseFontName strips the "ABCDEF+" tag, so a name-based
|
||||
// lookup cannot tell them apart and borrows the wrong subset's codes.
|
||||
//
|
||||
// The doc below mirrors that with two TrueType subsets differing only by
|
||||
// subset tag. PUA code points keep it deterministic: font.encode() cannot
|
||||
// resolve them by glyph name, so the charcode can only come from the
|
||||
// selected font's ToUnicode reverse map - proving WHICH font was picked.
|
||||
|
||||
private static final String PUA = "";
|
||||
|
||||
/** ToUnicode CMap mapping each supplied charcode to a BMP code point. */
|
||||
private static byte[] toUnicodeCmap(int[][] codeToUnicode) {
|
||||
StringBuilder sb =
|
||||
new StringBuilder(
|
||||
"""
|
||||
/CIDInit /ProcSet findresource begin
|
||||
12 dict begin
|
||||
begincmap
|
||||
/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def
|
||||
/CMapName /Adobe-Identity-UCS def
|
||||
/CMapType 2 def
|
||||
1 begincodespacerange
|
||||
<00><FF>
|
||||
endcodespacerange
|
||||
""");
|
||||
sb.append(codeToUnicode.length).append(" beginbfchar\n");
|
||||
for (int[] pair : codeToUnicode) {
|
||||
sb.append(String.format("<%02X><%04X>%n", pair[0], pair[1]));
|
||||
}
|
||||
sb.append(
|
||||
"""
|
||||
endbfchar
|
||||
endcmap
|
||||
CMapName currentdict /CMap defineresource pop
|
||||
end
|
||||
end
|
||||
""");
|
||||
return sb.toString().getBytes(java.nio.charset.StandardCharsets.US_ASCII);
|
||||
}
|
||||
|
||||
private static org.apache.pdfbox.cos.COSDictionary subsetFontDict(
|
||||
PDDocument doc, String baseName, byte[] fontProgram, byte[] toUnicode)
|
||||
throws Exception {
|
||||
org.apache.pdfbox.cos.COSDictionary font = new org.apache.pdfbox.cos.COSDictionary();
|
||||
font.setItem(org.apache.pdfbox.cos.COSName.TYPE, org.apache.pdfbox.cos.COSName.FONT);
|
||||
font.setItem(
|
||||
org.apache.pdfbox.cos.COSName.SUBTYPE, org.apache.pdfbox.cos.COSName.TRUE_TYPE);
|
||||
if (baseName != null) {
|
||||
font.setName(org.apache.pdfbox.cos.COSName.BASE_FONT, baseName);
|
||||
}
|
||||
font.setInt(org.apache.pdfbox.cos.COSName.FIRST_CHAR, 0x21);
|
||||
font.setInt(org.apache.pdfbox.cos.COSName.LAST_CHAR, 0x22);
|
||||
org.apache.pdfbox.cos.COSArray widths = new org.apache.pdfbox.cos.COSArray();
|
||||
widths.add(org.apache.pdfbox.cos.COSInteger.get(500));
|
||||
widths.add(org.apache.pdfbox.cos.COSInteger.get(500));
|
||||
font.setItem(org.apache.pdfbox.cos.COSName.WIDTHS, widths);
|
||||
|
||||
org.apache.pdfbox.cos.COSDictionary fd = new org.apache.pdfbox.cos.COSDictionary();
|
||||
fd.setItem(org.apache.pdfbox.cos.COSName.TYPE, org.apache.pdfbox.cos.COSName.FONT_DESC);
|
||||
if (baseName != null) {
|
||||
fd.setName(org.apache.pdfbox.cos.COSName.FONT_NAME, baseName);
|
||||
}
|
||||
fd.setInt(org.apache.pdfbox.cos.COSName.FLAGS, 4);
|
||||
fd.setItem(
|
||||
org.apache.pdfbox.cos.COSName.FONT_BBOX,
|
||||
new org.apache.pdfbox.pdmodel.common.PDRectangle(0, 0, 1000, 1000).getCOSArray());
|
||||
fd.setInt(org.apache.pdfbox.cos.COSName.ITALIC_ANGLE, 0);
|
||||
fd.setInt(org.apache.pdfbox.cos.COSName.ASCENT, 800);
|
||||
fd.setInt(org.apache.pdfbox.cos.COSName.DESCENT, -200);
|
||||
fd.setInt(org.apache.pdfbox.cos.COSName.CAP_HEIGHT, 700);
|
||||
fd.setInt(org.apache.pdfbox.cos.COSName.STEM_V, 80);
|
||||
if (fontProgram != null) {
|
||||
org.apache.pdfbox.pdmodel.common.PDStream ff2 =
|
||||
new org.apache.pdfbox.pdmodel.common.PDStream(
|
||||
doc, new java.io.ByteArrayInputStream(fontProgram));
|
||||
ff2.getCOSObject().setInt(org.apache.pdfbox.cos.COSName.LENGTH1, fontProgram.length);
|
||||
fd.setItem(org.apache.pdfbox.cos.COSName.FONT_FILE2, ff2.getCOSObject());
|
||||
}
|
||||
font.setItem(org.apache.pdfbox.cos.COSName.FONT_DESC, fd);
|
||||
|
||||
org.apache.pdfbox.pdmodel.common.PDStream tu =
|
||||
new org.apache.pdfbox.pdmodel.common.PDStream(
|
||||
doc, new java.io.ByteArrayInputStream(toUnicode));
|
||||
font.setItem(org.apache.pdfbox.cos.COSName.getPDFName("ToUnicode"), tu.getCOSObject());
|
||||
return font;
|
||||
}
|
||||
|
||||
// Distinct fake font programs - hashing distinguishes the subsets by these bytes.
|
||||
private static final byte[] PROGRAM_A =
|
||||
"fake-ttf-program-A".getBytes(java.nio.charset.StandardCharsets.US_ASCII);
|
||||
private static final byte[] PROGRAM_B =
|
||||
"fake-ttf-program-B".getBytes(java.nio.charset.StandardCharsets.US_ASCII);
|
||||
|
||||
/**
|
||||
* Two sibling subsets of "FakeGaramond" whose ToUnicode maps give U+E000 DIFFERENT charcodes:
|
||||
* 0x22 in subset A (AAAAAC+), 0x21 in subset B (AAAAAG+) - exactly the CV's shifted-code
|
||||
* layout. {@code includeSecond=false} keeps only subset A for the unambiguous-fallback case.
|
||||
*/
|
||||
private static String siblingSubsetsBase64(boolean includeSecond) throws Exception {
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
PDPage page = new PDPage();
|
||||
doc.addPage(page);
|
||||
org.apache.pdfbox.cos.COSDictionary fonts = new org.apache.pdfbox.cos.COSDictionary();
|
||||
fonts.setItem(
|
||||
org.apache.pdfbox.cos.COSName.getPDFName("TTA"),
|
||||
subsetFontDict(
|
||||
doc,
|
||||
"AAAAAC+FakeGaramond",
|
||||
PROGRAM_A,
|
||||
toUnicodeCmap(new int[][] {{0x21, 0xE001}, {0x22, 0xE000}})));
|
||||
if (includeSecond) {
|
||||
fonts.setItem(
|
||||
org.apache.pdfbox.cos.COSName.getPDFName("TTB"),
|
||||
subsetFontDict(
|
||||
doc,
|
||||
"AAAAAG+FakeGaramond",
|
||||
PROGRAM_B,
|
||||
toUnicodeCmap(new int[][] {{0x21, 0xE000}, {0x22, 0xE002}})));
|
||||
}
|
||||
org.apache.pdfbox.pdmodel.PDResources resources =
|
||||
new org.apache.pdfbox.pdmodel.PDResources();
|
||||
resources.getCOSObject().setItem(org.apache.pdfbox.cos.COSName.FONT, fonts);
|
||||
page.setResources(resources);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
doc.save(bos);
|
||||
return Base64.getEncoder().encodeToString(bos.toByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static String sha256Hex(byte[] bytes) throws Exception {
|
||||
byte[] digest = java.security.MessageDigest.getInstance("SHA-256").digest(bytes);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : digest) sb.append(String.format("%02x", b));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static EncodeCharcodesRequest siblingRequest(
|
||||
String base64, String fontName, String fontSha256) {
|
||||
EncodeCharcodesRequest req = new EncodeCharcodesRequest();
|
||||
req.setPdfBase64(base64);
|
||||
req.setPageIndex(0);
|
||||
req.setLocatorChar(PUA);
|
||||
req.setFontName(fontName);
|
||||
req.setFontSha256(fontSha256);
|
||||
req.setText(PUA);
|
||||
return req;
|
||||
}
|
||||
|
||||
@Test
|
||||
void fontProgramHashSelectsTheExactSubset() throws Exception {
|
||||
String base64 = siblingSubsetsBase64(true);
|
||||
PdfTextEditorCharcodeController controller = controller();
|
||||
|
||||
// Both requests carry the SAME tag-stripped name PDFium reports ("FakeGaramond"),
|
||||
// so only the program hash can tell the subsets apart.
|
||||
EncodeCharcodesResponse viaA =
|
||||
controller
|
||||
.encodeCharcodes(
|
||||
siblingRequest(base64, "FakeGaramond", sha256Hex(PROGRAM_A)))
|
||||
.getBody();
|
||||
assertThat(viaA).isNotNull();
|
||||
assertThat(viaA.getError()).isNull();
|
||||
assertThat(viaA.getNote()).contains("AAAAAC+FakeGaramond");
|
||||
assertThat(viaA.getCharcodes()).containsExactly(0x22L);
|
||||
|
||||
EncodeCharcodesResponse viaB =
|
||||
controller
|
||||
.encodeCharcodes(
|
||||
siblingRequest(base64, "FakeGaramond", sha256Hex(PROGRAM_B)))
|
||||
.getBody();
|
||||
assertThat(viaB).isNotNull();
|
||||
assertThat(viaB.getError()).isNull();
|
||||
assertThat(viaB.getNote()).contains("AAAAAG+FakeGaramond");
|
||||
assertThat(viaB.getCharcodes()).containsExactly(0x21L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ambiguousStrippedNameRefusesToGuessBetweenSiblingSubsets() throws Exception {
|
||||
// No hash, and the tag-stripped name matches BOTH subsets which both render the
|
||||
// locator char. Guessing here is what scrambled "RUSSELL W. MANGUM III" into
|
||||
// "US EEL W. MANGS M III" - the sibling's codes hit different glyphs. The
|
||||
// backend must refuse so the frontend takes its safe fallback.
|
||||
EncodeCharcodesResponse body =
|
||||
controller()
|
||||
.encodeCharcodes(
|
||||
siblingRequest(siblingSubsetsBase64(true), "FakeGaramond", null))
|
||||
.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).contains("no font");
|
||||
assertThat(body.getCharcodes()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactTaggedNameStillSelectsItsSubset() throws Exception {
|
||||
// A caller that DOES know the full tagged /BaseFont name keeps working.
|
||||
EncodeCharcodesResponse body =
|
||||
controller()
|
||||
.encodeCharcodes(
|
||||
siblingRequest(
|
||||
siblingSubsetsBase64(true), "AAAAAG+FakeGaramond", null))
|
||||
.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNull();
|
||||
assertThat(body.getNote()).contains("AAAAAG+FakeGaramond");
|
||||
assertThat(body.getCharcodes()).containsExactly(0x21L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void strippedNameStillWorksWhenUnambiguous() throws Exception {
|
||||
// With a SINGLE subset on the page, the tag-stripped name (what PDFium
|
||||
// reports) must keep resolving - the ambiguity guard only bites when
|
||||
// two+ siblings could answer.
|
||||
EncodeCharcodesResponse body =
|
||||
controller()
|
||||
.encodeCharcodes(
|
||||
siblingRequest(siblingSubsetsBase64(false), "FakeGaramond", null))
|
||||
.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNull();
|
||||
assertThat(body.getNote()).contains("AAAAAC+FakeGaramond");
|
||||
assertThat(body.getCharcodes()).containsExactly(0x22L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void staleHashFallsBackToNameMatching() throws Exception {
|
||||
// A hash matching NO font on the page (e.g. PDFium handed back a substitute
|
||||
// font's bytes) must not brick the request: name matching still runs, and an
|
||||
// exact tagged name resolves.
|
||||
EncodeCharcodesResponse body =
|
||||
controller()
|
||||
.encodeCharcodes(
|
||||
siblingRequest(
|
||||
siblingSubsetsBase64(true),
|
||||
"AAAAAC+FakeGaramond",
|
||||
"0000000000000000000000000000000000000000000000000000000000000000"))
|
||||
.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNull();
|
||||
assertThat(body.getNote()).contains("AAAAAC+FakeGaramond");
|
||||
assertThat(body.getCharcodes()).containsExactly(0x22L);
|
||||
}
|
||||
|
||||
private static final String PUA_E000 = "";
|
||||
private static final String PUA_E002 = "";
|
||||
|
||||
private static final byte[] SHARED_PROGRAM =
|
||||
"fake-ttf-program-shared".getBytes(java.nio.charset.StandardCharsets.US_ASCII);
|
||||
|
||||
private static String cacheIdentityPairBase64(String baseName, byte[] program)
|
||||
throws Exception {
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
PDPage page = new PDPage();
|
||||
doc.addPage(page);
|
||||
org.apache.pdfbox.cos.COSDictionary fonts = new org.apache.pdfbox.cos.COSDictionary();
|
||||
fonts.setItem(
|
||||
org.apache.pdfbox.cos.COSName.getPDFName("C1"),
|
||||
subsetFontDict(
|
||||
doc,
|
||||
baseName,
|
||||
program,
|
||||
toUnicodeCmap(new int[][] {{0x21, 0xE001}, {0x22, 0xE000}})));
|
||||
fonts.setItem(
|
||||
org.apache.pdfbox.cos.COSName.getPDFName("C2"),
|
||||
subsetFontDict(
|
||||
doc,
|
||||
baseName,
|
||||
program,
|
||||
toUnicodeCmap(new int[][] {{0x21, 0xE002}, {0x22, 0xE003}})));
|
||||
org.apache.pdfbox.pdmodel.PDResources resources =
|
||||
new org.apache.pdfbox.pdmodel.PDResources();
|
||||
resources.getCOSObject().setItem(org.apache.pdfbox.cos.COSName.FONT, fonts);
|
||||
page.setResources(resources);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
doc.save(bos);
|
||||
return Base64.getEncoder().encodeToString(bos.toByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static EncodeCharcodesRequest cacheIdentityRequest(
|
||||
String base64, String locator, String fontName, String fontSha256) {
|
||||
EncodeCharcodesRequest req = new EncodeCharcodesRequest();
|
||||
req.setPdfBase64(base64);
|
||||
req.setPageIndex(0);
|
||||
req.setLocatorChar(locator);
|
||||
req.setFontName(fontName);
|
||||
req.setFontSha256(fontSha256);
|
||||
req.setText(locator);
|
||||
return req;
|
||||
}
|
||||
|
||||
@Test
|
||||
void unnamedFontsSharingOneProgramDoNotShareACachedMap() throws Exception {
|
||||
String base64 = cacheIdentityPairBase64(null, SHARED_PROGRAM);
|
||||
String sha = sha256Hex(SHARED_PROGRAM);
|
||||
PdfTextEditorCharcodeController controller = controller();
|
||||
|
||||
EncodeCharcodesResponse first =
|
||||
controller
|
||||
.encodeCharcodes(cacheIdentityRequest(base64, PUA_E000, null, sha))
|
||||
.getBody();
|
||||
assertThat(first).isNotNull();
|
||||
assertThat(first.getError()).isNull();
|
||||
assertThat(first.getCharcodes()).containsExactly(0x22L);
|
||||
|
||||
EncodeCharcodesResponse second =
|
||||
controller
|
||||
.encodeCharcodes(cacheIdentityRequest(base64, PUA_E002, null, sha))
|
||||
.getBody();
|
||||
assertThat(second).isNotNull();
|
||||
assertThat(second.getError()).isNull();
|
||||
assertThat(second.getMissing()).isNullOrEmpty();
|
||||
assertThat(second.getCharcodes())
|
||||
.as("second font must not be served the first font's cached map")
|
||||
.containsExactly(0x21L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fontsSharingOneNameDoNotShareACachedMap() throws Exception {
|
||||
String base64 = cacheIdentityPairBase64("SharedName", null);
|
||||
PdfTextEditorCharcodeController controller = controller();
|
||||
|
||||
EncodeCharcodesResponse first =
|
||||
controller
|
||||
.encodeCharcodes(cacheIdentityRequest(base64, PUA_E000, "SharedName", null))
|
||||
.getBody();
|
||||
assertThat(first).isNotNull();
|
||||
assertThat(first.getError()).isNull();
|
||||
assertThat(first.getCharcodes()).containsExactly(0x22L);
|
||||
|
||||
EncodeCharcodesResponse second =
|
||||
controller
|
||||
.encodeCharcodes(cacheIdentityRequest(base64, PUA_E002, "SharedName", null))
|
||||
.getBody();
|
||||
assertThat(second).isNotNull();
|
||||
assertThat(second.getError()).isNull();
|
||||
assertThat(second.getMissing()).isNullOrEmpty();
|
||||
assertThat(second.getCharcodes())
|
||||
.as("same-name fonts must not share one cached map")
|
||||
.containsExactly(0x21L);
|
||||
}
|
||||
|
||||
private static String formXObjectFontBase64() throws Exception {
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
PDPage page = new PDPage();
|
||||
doc.addPage(page);
|
||||
|
||||
org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject outer =
|
||||
new org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject(doc);
|
||||
outer.setBBox(new org.apache.pdfbox.pdmodel.common.PDRectangle(0, 0, 200, 200));
|
||||
org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject inner =
|
||||
new org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject(doc);
|
||||
inner.setBBox(new org.apache.pdfbox.pdmodel.common.PDRectangle(0, 0, 100, 100));
|
||||
|
||||
org.apache.pdfbox.pdmodel.PDResources innerResources =
|
||||
new org.apache.pdfbox.pdmodel.PDResources();
|
||||
innerResources.put(
|
||||
org.apache.pdfbox.cos.COSName.getPDFName("F1"),
|
||||
new PDType1Font(Standard14Fonts.FontName.HELVETICA));
|
||||
inner.setResources(innerResources);
|
||||
|
||||
org.apache.pdfbox.pdmodel.PDResources outerResources =
|
||||
new org.apache.pdfbox.pdmodel.PDResources();
|
||||
outerResources.put(org.apache.pdfbox.cos.COSName.getPDFName("Fm1"), inner);
|
||||
outer.setResources(outerResources);
|
||||
|
||||
org.apache.pdfbox.pdmodel.PDResources pageResources =
|
||||
new org.apache.pdfbox.pdmodel.PDResources();
|
||||
pageResources.put(org.apache.pdfbox.cos.COSName.getPDFName("Fm0"), outer);
|
||||
page.setResources(pageResources);
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
doc.save(bos);
|
||||
return Base64.getEncoder().encodeToString(bos.toByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static String cyclicFormXObjectsBase64() throws Exception {
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
PDPage page = new PDPage();
|
||||
doc.addPage(page);
|
||||
|
||||
org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject formA =
|
||||
new org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject(doc);
|
||||
formA.setBBox(new org.apache.pdfbox.pdmodel.common.PDRectangle(0, 0, 100, 100));
|
||||
org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject formB =
|
||||
new org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject(doc);
|
||||
formB.setBBox(new org.apache.pdfbox.pdmodel.common.PDRectangle(0, 0, 100, 100));
|
||||
|
||||
org.apache.pdfbox.pdmodel.PDResources resA =
|
||||
new org.apache.pdfbox.pdmodel.PDResources();
|
||||
org.apache.pdfbox.pdmodel.PDResources resB =
|
||||
new org.apache.pdfbox.pdmodel.PDResources();
|
||||
resA.put(org.apache.pdfbox.cos.COSName.getPDFName("Self"), formA);
|
||||
resA.put(org.apache.pdfbox.cos.COSName.getPDFName("Fb"), formB);
|
||||
resB.put(org.apache.pdfbox.cos.COSName.getPDFName("Fa"), formA);
|
||||
resB.put(
|
||||
org.apache.pdfbox.cos.COSName.getPDFName("F1"),
|
||||
new PDType1Font(Standard14Fonts.FontName.HELVETICA));
|
||||
formA.setResources(resA);
|
||||
formB.setResources(resB);
|
||||
|
||||
org.apache.pdfbox.pdmodel.PDResources pageResources =
|
||||
new org.apache.pdfbox.pdmodel.PDResources();
|
||||
pageResources.put(org.apache.pdfbox.cos.COSName.getPDFName("Fm0"), formA);
|
||||
page.setResources(pageResources);
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
doc.save(bos);
|
||||
return Base64.getEncoder().encodeToString(bos.toByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void fontReachableOnlyThroughAFormXObjectIsFound() throws Exception {
|
||||
ResponseEntity<EncodeCharcodesResponse> resp =
|
||||
controller().encodeCharcodes(manyFontsRequest(formXObjectFontBase64()));
|
||||
assertThat(resp.getStatusCode().value()).isEqualTo(200);
|
||||
EncodeCharcodesResponse body = resp.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNull();
|
||||
assertThat(body.getNote()).contains("Helvetica");
|
||||
assertThat(body.getCharcodes()).containsExactly((long) 'A');
|
||||
}
|
||||
|
||||
@Test
|
||||
@org.junit.jupiter.api.Timeout(60)
|
||||
void cyclicFormXObjectResourcesTerminate() throws Exception {
|
||||
ResponseEntity<EncodeCharcodesResponse> resp =
|
||||
controller().encodeCharcodes(manyFontsRequest(cyclicFormXObjectsBase64()));
|
||||
assertThat(resp.getStatusCode().value()).isEqualTo(200);
|
||||
EncodeCharcodesResponse body = resp.getBody();
|
||||
assertThat(body).isNotNull();
|
||||
assertThat(body.getError()).isNull();
|
||||
assertThat(body.getCharcodes()).containsExactly((long) 'A');
|
||||
}
|
||||
}
|
||||
+340
@@ -0,0 +1,340 @@
|
||||
package stirling.software.SPDF.controller.api;
|
||||
|
||||
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.nio.file.Paths;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.apache.pdfbox.Loader;
|
||||
import org.apache.pdfbox.contentstream.PDFStreamEngine;
|
||||
import org.apache.pdfbox.contentstream.operator.state.Concatenate;
|
||||
import org.apache.pdfbox.contentstream.operator.state.Restore;
|
||||
import org.apache.pdfbox.contentstream.operator.state.Save;
|
||||
import org.apache.pdfbox.contentstream.operator.state.SetGraphicsStateParameters;
|
||||
import org.apache.pdfbox.contentstream.operator.state.SetMatrix;
|
||||
import org.apache.pdfbox.contentstream.operator.text.BeginText;
|
||||
import org.apache.pdfbox.contentstream.operator.text.EndText;
|
||||
import org.apache.pdfbox.contentstream.operator.text.SetFontAndSize;
|
||||
import org.apache.pdfbox.contentstream.operator.text.SetTextHorizontalScaling;
|
||||
import org.apache.pdfbox.contentstream.operator.text.SetTextLeading;
|
||||
import org.apache.pdfbox.contentstream.operator.text.SetTextRenderingMode;
|
||||
import org.apache.pdfbox.contentstream.operator.text.SetTextRise;
|
||||
import org.apache.pdfbox.contentstream.operator.text.SetWordSpacing;
|
||||
import org.apache.pdfbox.contentstream.operator.text.ShowText;
|
||||
import org.apache.pdfbox.cos.COSBase;
|
||||
import org.apache.pdfbox.cos.COSDictionary;
|
||||
import org.apache.pdfbox.cos.COSName;
|
||||
import org.apache.pdfbox.cos.COSStream;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDResources;
|
||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType3CharProc;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType3Font;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Diagnostic test: enumerate every font referenced by Sample.pdf and dump its subtype, encoding,
|
||||
* ToUnicode, and embedded font program info. For Type3 fonts also dump CharProcs glyph names and
|
||||
* the content stream of one glyph (the 'M' if present).
|
||||
*
|
||||
* <p>Not a real regression test - run with --tests SamplePdfFontDumpTest -i to see the stdout
|
||||
* output.
|
||||
*/
|
||||
@Disabled(
|
||||
"Diagnostic probe: dumps Sample.pdf font internals to stdout and asserts nothing. Kept for font debugging; run manually.")
|
||||
public class SamplePdfFontDumpTest {
|
||||
|
||||
private static final Path SAMPLE =
|
||||
Paths.get(System.getProperty("user.dir"))
|
||||
.getParent()
|
||||
.getParent()
|
||||
.resolve("frontend/editor/public/samples/Sample.pdf");
|
||||
|
||||
@Test
|
||||
public void dumpFonts() throws IOException {
|
||||
byte[] pdfBytes = Files.readAllBytes(SAMPLE);
|
||||
try (PDDocument doc = Loader.loadPDF(pdfBytes)) {
|
||||
int numPages = doc.getNumberOfPages();
|
||||
System.out.println("Sample.pdf has " + numPages + " pages.");
|
||||
Set<COSDictionary> seenFontDicts = new HashSet<>();
|
||||
for (int p = 0; p < numPages; p++) {
|
||||
PDPage page = doc.getPage(p);
|
||||
System.out.println("\n=== Page " + p + " ===");
|
||||
PDResources resources = page.getResources();
|
||||
if (resources == null) {
|
||||
System.out.println(" (no resources)");
|
||||
continue;
|
||||
}
|
||||
for (COSName fontName : resources.getFontNames()) {
|
||||
PDFont font;
|
||||
try {
|
||||
font = resources.getFont(fontName);
|
||||
} catch (IOException e) {
|
||||
System.out.println(
|
||||
" Font "
|
||||
+ fontName.getName()
|
||||
+ ": failed to load - "
|
||||
+ e.getMessage());
|
||||
continue;
|
||||
}
|
||||
if (font == null) continue;
|
||||
COSDictionary dict = font.getCOSObject();
|
||||
if (!seenFontDicts.add(dict)) {
|
||||
System.out.println(
|
||||
" Font " + fontName.getName() + " -> already seen above");
|
||||
continue;
|
||||
}
|
||||
dumpFont(fontName.getName(), font);
|
||||
}
|
||||
}
|
||||
// Scan: for every text-show operation, record per-font (charcode, unicode) pairs.
|
||||
System.out.println("\n=== All (font, charcode, unicode) seen on page ===");
|
||||
for (int p = 0; p < numPages; p++) {
|
||||
PDPage page = doc.getPage(p);
|
||||
AllCharsScanner scanner = new AllCharsScanner();
|
||||
scanner.processPage(page);
|
||||
System.out.println("\nPage " + p + ":");
|
||||
for (var entry : scanner.perFont.entrySet()) {
|
||||
PDFont font = entry.getKey();
|
||||
var seen = entry.getValue();
|
||||
System.out.println(" Font " + font.getName() + " " + font.getSubType() + ":");
|
||||
var sortedSeen = new java.util.TreeMap<Integer, String>(seen);
|
||||
for (var s : sortedSeen.entrySet()) {
|
||||
System.out.println(
|
||||
" charcode 0x"
|
||||
+ Integer.toHexString(s.getKey())
|
||||
+ " ("
|
||||
+ s.getKey()
|
||||
+ ") -> '"
|
||||
+ s.getValue()
|
||||
+ "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Confirm font.encode() works for Type3 fonts.
|
||||
System.out.println("\n=== Can we encode existing chars in F27/F28? ===");
|
||||
PDPage page0 = doc.getPage(0);
|
||||
PDResources r0 = page0.getResources();
|
||||
for (String fname : new String[] {"F27", "F28"}) {
|
||||
PDFont f = r0.getFont(COSName.getPDFName(fname));
|
||||
if (f == null) {
|
||||
System.out.println(" " + fname + ": NOT FOUND on page 0");
|
||||
continue;
|
||||
}
|
||||
System.out.println(" " + fname + ": " + f.getClass().getSimpleName());
|
||||
for (String ch : new String[] {"M", "0", "1", "+", "Z", "a"}) {
|
||||
try {
|
||||
byte[] enc = f.encode(ch);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : enc) sb.append(String.format("%02X ", b & 0xff));
|
||||
System.out.println(
|
||||
" encode('" + ch + "') -> [" + sb.toString().trim() + "]");
|
||||
} catch (Exception e) {
|
||||
System.out.println(
|
||||
" encode('"
|
||||
+ ch
|
||||
+ "') FAILED: "
|
||||
+ e.getClass().getSimpleName()
|
||||
+ " "
|
||||
+ e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dump page 0 content stream so we can see how "10M+" is composed.
|
||||
System.out.println("\n=== Page 0 RAW content stream (first 4kb) ===");
|
||||
try (InputStream is = doc.getPage(0).getContents()) {
|
||||
byte[] bytes = is.readAllBytes();
|
||||
System.out.println("Total content stream size: " + bytes.length + " bytes");
|
||||
String asStr = new String(bytes, StandardCharsets.ISO_8859_1);
|
||||
int idx = asStr.indexOf("F27");
|
||||
if (idx >= 0) {
|
||||
int start = Math.max(0, idx - 100);
|
||||
int end = Math.min(asStr.length(), idx + 2500);
|
||||
System.out.println("--- F27 context ---");
|
||||
System.out.println(asStr.substring(start, end));
|
||||
System.out.println("---");
|
||||
}
|
||||
int idx2 = asStr.indexOf("F28");
|
||||
if (idx2 >= 0) {
|
||||
int start = Math.max(0, idx2 - 200);
|
||||
int end = Math.min(asStr.length(), idx2 + 600);
|
||||
System.out.println("--- F28 context ---");
|
||||
System.out.println(asStr.substring(start, end));
|
||||
System.out.println("---");
|
||||
}
|
||||
}
|
||||
|
||||
// Dump a CharProc for each font's first non-zero glyph, with focus on any 'M' or "0".
|
||||
System.out.println("\n=== Sample CharProc dumps for Type3 fonts ===");
|
||||
Set<COSDictionary> printed = new HashSet<>();
|
||||
for (int p = 0; p < numPages; p++) {
|
||||
PDPage page = doc.getPage(p);
|
||||
PDResources resources = page.getResources();
|
||||
if (resources == null) continue;
|
||||
for (COSName fn : resources.getFontNames()) {
|
||||
PDFont font = resources.getFont(fn);
|
||||
if (!(font instanceof PDType3Font)) continue;
|
||||
if (!printed.add(font.getCOSObject())) continue;
|
||||
PDType3Font t3 = (PDType3Font) font;
|
||||
// Iterate charcodes 0..255 looking for any that map to 'M' or '0' or '+'.
|
||||
for (int cc = 0; cc < 256; cc++) {
|
||||
String u = null;
|
||||
try {
|
||||
u = t3.toUnicode(cc);
|
||||
} catch (Exception e) {
|
||||
/* */
|
||||
}
|
||||
if (u == null) continue;
|
||||
if (u.equals("M") || u.equals("0") || u.equals("+") || u.equals("1")) {
|
||||
System.out.println(
|
||||
"Page "
|
||||
+ p
|
||||
+ " font '"
|
||||
+ fn.getName()
|
||||
+ "' charcode "
|
||||
+ cc
|
||||
+ " maps to '"
|
||||
+ u
|
||||
+ "':");
|
||||
dumpType3Glyph(t3, cc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void dumpFont(String resourceName, PDFont font) {
|
||||
COSDictionary dict = font.getCOSObject();
|
||||
String subtype = dict.getNameAsString(COSName.SUBTYPE);
|
||||
String baseFont = dict.getNameAsString(COSName.BASE_FONT);
|
||||
boolean hasEncoding = dict.containsKey(COSName.ENCODING);
|
||||
boolean hasToUnicode = dict.containsKey(COSName.TO_UNICODE);
|
||||
PDFontDescriptor descriptor = font.getFontDescriptor();
|
||||
boolean hasEmbedded = false;
|
||||
String embeddedKind = "none";
|
||||
if (descriptor != null) {
|
||||
COSDictionary dDict = descriptor.getCOSObject();
|
||||
if (dDict.containsKey(COSName.FONT_FILE)) {
|
||||
hasEmbedded = true;
|
||||
embeddedKind = "FontFile (Type1)";
|
||||
} else if (dDict.containsKey(COSName.FONT_FILE2)) {
|
||||
hasEmbedded = true;
|
||||
embeddedKind = "FontFile2 (TrueType)";
|
||||
} else if (dDict.containsKey(COSName.FONT_FILE3)) {
|
||||
hasEmbedded = true;
|
||||
COSBase ff3 = dDict.getDictionaryObject(COSName.FONT_FILE3);
|
||||
if (ff3 instanceof COSStream) {
|
||||
String ff3Subtype = ((COSStream) ff3).getNameAsString(COSName.SUBTYPE);
|
||||
embeddedKind = "FontFile3 (" + ff3Subtype + ")";
|
||||
} else {
|
||||
embeddedKind = "FontFile3";
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(
|
||||
" Font resource '"
|
||||
+ resourceName
|
||||
+ "': base='"
|
||||
+ baseFont
|
||||
+ "' subtype="
|
||||
+ subtype
|
||||
+ " hasEncoding="
|
||||
+ hasEncoding
|
||||
+ " hasToUnicode="
|
||||
+ hasToUnicode
|
||||
+ " embedded="
|
||||
+ hasEmbedded
|
||||
+ " ("
|
||||
+ embeddedKind
|
||||
+ ")");
|
||||
|
||||
if (font instanceof PDType3Font) {
|
||||
PDType3Font t3 = (PDType3Font) font;
|
||||
COSDictionary charProcs = t3.getCharProcs();
|
||||
int count = charProcs == null ? 0 : charProcs.size();
|
||||
System.out.println(" Type3 CharProcs count = " + count);
|
||||
if (charProcs != null) {
|
||||
TreeSet<String> names = new TreeSet<>();
|
||||
for (COSName k : charProcs.keySet()) names.add(k.getName());
|
||||
System.out.println(" glyph names: " + names);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void dumpType3Glyph(PDType3Font font, int charcode) throws IOException {
|
||||
String name = font.getEncoding() != null ? font.getEncoding().getName(charcode) : null;
|
||||
System.out.println(" Type3 charcode " + charcode + " -> glyph name '" + name + "'");
|
||||
PDType3CharProc proc = font.getCharProc(charcode);
|
||||
if (proc == null) {
|
||||
System.out.println(" (no CharProc for that charcode)");
|
||||
return;
|
||||
}
|
||||
COSStream stream = proc.getCOSObject();
|
||||
byte[] raw;
|
||||
try (InputStream is = stream.createInputStream()) {
|
||||
raw = is.readAllBytes();
|
||||
}
|
||||
System.out.println(" CharProc content stream (" + raw.length + " bytes):");
|
||||
System.out.println("---");
|
||||
System.out.println(new String(raw, StandardCharsets.ISO_8859_1));
|
||||
System.out.println("---");
|
||||
}
|
||||
|
||||
/** Records every (font, charcode -> unicode) tuple seen on a page. */
|
||||
static final class AllCharsScanner extends PDFStreamEngine {
|
||||
final java.util.LinkedHashMap<PDFont, java.util.Map<Integer, String>> perFont =
|
||||
new java.util.LinkedHashMap<>();
|
||||
|
||||
AllCharsScanner() {
|
||||
addOperator(new BeginText(this));
|
||||
addOperator(new EndText(this));
|
||||
addOperator(new SetFontAndSize(this));
|
||||
addOperator(new SetTextHorizontalScaling(this));
|
||||
addOperator(new SetTextLeading(this));
|
||||
addOperator(new SetTextRenderingMode(this));
|
||||
addOperator(new SetTextRise(this));
|
||||
addOperator(new SetWordSpacing(this));
|
||||
addOperator(new SetMatrix(this));
|
||||
addOperator(new Save(this));
|
||||
addOperator(new Restore(this));
|
||||
addOperator(new Concatenate(this));
|
||||
addOperator(new SetGraphicsStateParameters(this));
|
||||
addOperator(new ShowText(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void showText(byte[] string) throws IOException {
|
||||
PDFont font = getGraphicsState().getTextState().getFont();
|
||||
if (font == null) return;
|
||||
var seen = perFont.computeIfAbsent(font, k -> new java.util.LinkedHashMap<>());
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(string);
|
||||
while (in.available() > 0) {
|
||||
int code;
|
||||
try {
|
||||
code = font.readCode(in);
|
||||
} catch (IOException e) {
|
||||
break;
|
||||
}
|
||||
String u;
|
||||
try {
|
||||
u = font.toUnicode(code);
|
||||
} catch (RuntimeException e) {
|
||||
u = null;
|
||||
}
|
||||
seen.putIfAbsent(code, u);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -485,9 +485,9 @@ class PdfJsonFontServiceMoreTest {
|
||||
class DetectExtra {
|
||||
|
||||
@Test
|
||||
@DisplayName("detectFontFlavor recognises ttcf as cff and otf via OTTO")
|
||||
@DisplayName("detectFontFlavor rejects ttcf collections and recognises otf via OTTO")
|
||||
void detectFlavorExtra() {
|
||||
assertEquals("cff", service.detectFontFlavor(new byte[] {0x74, 0x74, 0x63, 0x66}));
|
||||
assertNull(service.detectFontFlavor(new byte[] {0x74, 0x74, 0x63, 0x66}));
|
||||
List<byte[]> otfVariants = List.of(new byte[] {0x4F, 0x54, 0x54, 0x4F});
|
||||
for (byte[] otf : otfVariants) {
|
||||
assertEquals("otf", service.detectFontFlavor(otf));
|
||||
|
||||
+6
-7
@@ -57,10 +57,9 @@ class PdfJsonFontServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectFontFlavor_cffSignature_returnsCff() {
|
||||
// 0x74746366 = "ttcf"
|
||||
byte[] cff = {0x74, 0x74, 0x63, 0x66};
|
||||
assertEquals("cff", service.detectFontFlavor(cff));
|
||||
void detectFontFlavor_ttcSignature_returnsNull() {
|
||||
byte[] ttc = {0x74, 0x74, 0x63, 0x66};
|
||||
assertNull(service.detectFontFlavor(ttc));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -94,9 +93,9 @@ class PdfJsonFontServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectTrueTypeFormat_cffSignature_returnsCff() {
|
||||
byte[] cff = {0x74, 0x74, 0x63, 0x66};
|
||||
assertEquals("cff", service.detectTrueTypeFormat(cff));
|
||||
void detectTrueTypeFormat_ttcSignature_returnsNull() {
|
||||
byte[] ttc = {0x74, 0x74, 0x63, 0x66};
|
||||
assertNull(service.detectTrueTypeFormat(ttc));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Binary file not shown.
@@ -59,7 +59,7 @@ public enum AuditLevel {
|
||||
*/
|
||||
public static AuditLevel fromInt(int level) {
|
||||
// Ensure level is within valid bounds
|
||||
int boundedLevel = Math.min(Math.max(level, 0), 3);
|
||||
int boundedLevel = Math.clamp(level, 0, 3);
|
||||
|
||||
for (AuditLevel auditLevel : values()) {
|
||||
if (auditLevel.level == boundedLevel) {
|
||||
|
||||
+11
-9
@@ -17,16 +17,16 @@ import org.springframework.data.redis.core.ScanOptions;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.common.cluster.JobStore;
|
||||
import stirling.software.common.cluster.JobStoreEntry;
|
||||
|
||||
import tools.jackson.core.JacksonException;
|
||||
import tools.jackson.core.type.TypeReference;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* Valkey-backed {@link JobStore}. Each job is one hash; a reverse index maps fileId to jobId.
|
||||
*
|
||||
@@ -44,8 +44,10 @@ public class ValkeyJobStore implements JobStore {
|
||||
private static final String FILE_INDEX_PREFIX = "stirling:file2job:";
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final TypeReference<List<String>> LIST_STRING = new TypeReference<>() {};
|
||||
private static final TypeReference<Map<String, String>> MAP_STRING = new TypeReference<>() {};
|
||||
private static final TypeReference<List<String>> LIST_STRING =
|
||||
new TypeReference<List<String>>() {};
|
||||
private static final TypeReference<Map<String, String>> MAP_STRING =
|
||||
new TypeReference<Map<String, String>>() {};
|
||||
|
||||
private final StringRedisTemplate template;
|
||||
|
||||
@@ -265,7 +267,7 @@ public class ValkeyJobStore implements JobStore {
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(v.toString(), MAP_STRING);
|
||||
} catch (JsonProcessingException e) {
|
||||
} catch (JacksonException e) {
|
||||
log.warn(
|
||||
"JobStore {} field 'resultMeta' is not valid JSON '{}' - treating as empty",
|
||||
key,
|
||||
@@ -277,7 +279,7 @@ public class ValkeyJobStore implements JobStore {
|
||||
private static String writeJson(Object value) {
|
||||
try {
|
||||
return MAPPER.writeValueAsString(value);
|
||||
} catch (JsonProcessingException e) {
|
||||
} catch (JacksonException e) {
|
||||
throw new IllegalStateException("Failed to JSON-serialize JobStore field", e);
|
||||
}
|
||||
}
|
||||
@@ -286,7 +288,7 @@ public class ValkeyJobStore implements JobStore {
|
||||
try {
|
||||
List<String> parsed = MAPPER.readValue(json, LIST_STRING);
|
||||
return parsed == null ? new ArrayList<>() : parsed;
|
||||
} catch (JsonProcessingException e) {
|
||||
} catch (JacksonException e) {
|
||||
log.warn(
|
||||
"JobStore {} field 'fileIds' is not valid JSON '{}' - treating as empty",
|
||||
key,
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ public class AuditConfigurationProperties {
|
||||
|
||||
// Ensure level is within valid bounds (0-3)
|
||||
int configLevel = auditConfig.getLevel();
|
||||
this.level = Math.min(Math.max(configLevel, 0), 3);
|
||||
this.level = Math.clamp(configLevel, 0, 3);
|
||||
|
||||
// Retention days (0 means infinite)
|
||||
this.retentionDays = auditConfig.getRetentionDays();
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ public class UsageRestController {
|
||||
@RequestParam(value = "dataType", defaultValue = "all") String dataType,
|
||||
@RequestParam(value = "days", defaultValue = "30") Integer days) {
|
||||
|
||||
int lookbackDays = Math.max(1, Math.min(days, 365));
|
||||
int lookbackDays = Math.clamp(days, 1, 365);
|
||||
|
||||
// Get audit events filtered by type
|
||||
List<PersistentAuditEvent> events = getEventsByDataType(dataType, lookbackDays);
|
||||
|
||||
+13
@@ -18,6 +18,19 @@ public enum FailureActionId {
|
||||
|
||||
DISMISS(Execution.SERVER, "Dismiss"),
|
||||
|
||||
/**
|
||||
* Open the failed operation in the client with its document, for the owner to run again
|
||||
* themselves. Not a re-run: the settings are theirs to check first.
|
||||
*/
|
||||
OPEN_IN_TOOL(Execution.CLIENT, "Retry"),
|
||||
|
||||
/**
|
||||
* Ask the owner for the password and unlock the document in their client. Re-running is implied
|
||||
* rather than named: an id says what a caller must supply, and a {@link
|
||||
* FailureActionSlot#RESOLUTION} runs the failed work again once it has it.
|
||||
*/
|
||||
DECRYPT(Execution.CLIENT, "Decrypt and retry"),
|
||||
|
||||
/** Open the document behind the incident, in whichever client can resolve its id. */
|
||||
VIEW_FILE(Execution.CLIENT, "View file"),
|
||||
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package stirling.software.proprietary.failure;
|
||||
|
||||
/** Placement intent, not layout: the client promotes, knowing what it can actually run. */
|
||||
public enum FailureActionSlot {
|
||||
|
||||
/** The action that resolves the failure. At most one per kind. */
|
||||
RESOLUTION,
|
||||
|
||||
/** Offered alongside the resolution, for a caller the resolution is not aimed at. */
|
||||
SECONDARY,
|
||||
|
||||
/** Available but folded away: correct, rarely what anyone wants to press next. */
|
||||
OVERFLOW
|
||||
}
|
||||
+55
-37
@@ -1,8 +1,12 @@
|
||||
package stirling.software.proprietary.failure;
|
||||
|
||||
import static stirling.software.proprietary.failure.FailureActionId.DECRYPT;
|
||||
import static stirling.software.proprietary.failure.FailureActionId.DISMISS;
|
||||
import static stirling.software.proprietary.failure.FailureActionId.OPEN_IN_TOOL;
|
||||
import static stirling.software.proprietary.failure.FailureActionId.VIEW_FILE;
|
||||
import static stirling.software.proprietary.failure.FailureActionId.VIEW_IN_PROCESSOR;
|
||||
import static stirling.software.proprietary.failure.FailureActionSlot.OVERFLOW;
|
||||
import static stirling.software.proprietary.failure.FailureActionSlot.SECONDARY;
|
||||
import static stirling.software.proprietary.failure.FailureAudience.ANYONE_WHO_SEES;
|
||||
import static stirling.software.proprietary.failure.FailureAudience.OWNER;
|
||||
import static stirling.software.proprietary.failure.FailureAudience.TEAM_REVIEWER;
|
||||
@@ -21,11 +25,8 @@ import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* The registry of failure kinds, described as data: a stable id, i18n keys and an English fallback
|
||||
* like {@code ExceptionUtils.ErrorCode}, plus the facets a review surface needs.
|
||||
*
|
||||
* <p>A new kind ships as a registry entry plus copy. Each offer also says who it is for, since one
|
||||
* incident is read both by whoever hit it and by whoever reviews after them.
|
||||
* The registry of failure kinds as data: id, i18n keys, English fallback, plus the facets a review
|
||||
* surface needs. A new kind ships as an entry plus copy; each offer says who it is for and where.
|
||||
*/
|
||||
@Getter
|
||||
public enum FailureKind {
|
||||
@@ -36,9 +37,12 @@ public enum FailureKind {
|
||||
FailureScope.FILE,
|
||||
errorCodes("E004"),
|
||||
fallback("This document is password-protected, so the pipeline could not read it."),
|
||||
offer(VIEW_FILE, OWNER),
|
||||
offer(VIEW_IN_PROCESSOR, TEAM_REVIEWER),
|
||||
offer(DISMISS, ANYONE_WHO_SEES)),
|
||||
// The password is the fix; the owner's own document is the runner-up.
|
||||
resolution(DECRYPT, OWNER),
|
||||
global(VIEW_FILE, OWNER, SECONDARY),
|
||||
global(VIEW_IN_PROCESSOR, TEAM_REVIEWER, OVERFLOW),
|
||||
global(OPEN_IN_TOOL, OWNER, OVERFLOW),
|
||||
global(DISMISS, ANYONE_WHO_SEES, OVERFLOW)),
|
||||
|
||||
UNKNOWN(
|
||||
FailureStage.INTERNAL,
|
||||
@@ -47,11 +51,11 @@ public enum FailureKind {
|
||||
FailureScope.RUN,
|
||||
noErrorCodes(),
|
||||
fallback("This run failed for a reason Stirling does not yet recognise."),
|
||||
// Same order as every other kind: declaration order is display order, so the document
|
||||
// leads wherever it is offered rather than moving between failures.
|
||||
offer(VIEW_FILE, OWNER),
|
||||
offer(VIEW_IN_PROCESSOR, TEAM_REVIEWER),
|
||||
offer(DISMISS, ANYONE_WHO_SEES));
|
||||
// No known fix to declare, so a plain retry leads: these are often one-offs.
|
||||
global(OPEN_IN_TOOL, OWNER, SECONDARY),
|
||||
global(VIEW_FILE, OWNER, SECONDARY),
|
||||
global(VIEW_IN_PROCESSOR, TEAM_REVIEWER, OVERFLOW),
|
||||
global(DISMISS, ANYONE_WHO_SEES, OVERFLOW));
|
||||
|
||||
private static final String KEY_PREFIX = "portal.failures.kind.";
|
||||
private static final String ACTION_KEY_PREFIX = "portal.failures.action.";
|
||||
@@ -98,27 +102,37 @@ public enum FailureKind {
|
||||
this.offers = List.of(offers);
|
||||
}
|
||||
|
||||
/**
|
||||
* One ordered list rather than ids plus parallel maps of audiences and labels, which could
|
||||
* disagree with each other.
|
||||
*
|
||||
* @param labelKeySuffix key under {@code portal.failures.action.}, or null for the generic
|
||||
* label
|
||||
*/
|
||||
private record Offer(FailureActionId id, FailureAudience audience, String labelKeySuffix) {}
|
||||
/** One ordered list, not parallel maps of audiences, slots and labels that could disagree. */
|
||||
private record Offer(
|
||||
FailureActionId id,
|
||||
FailureAudience audience,
|
||||
FailureActionSlot slot,
|
||||
String labelKeySuffix) {}
|
||||
|
||||
/** Declaration order is display order. */
|
||||
private static Offer offer(FailureActionId id, FailureAudience audience) {
|
||||
return new Offer(id, audience, null);
|
||||
/** The action that fixes this kind. One per kind: needing two would make it two kinds. */
|
||||
private static Offer resolution(FailureActionId id, FailureAudience audience) {
|
||||
return new Offer(id, audience, FailureActionSlot.RESOLUTION, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #offer(FailureActionId, FailureAudience)}, but labelled by this kind's own wording
|
||||
* where the shared one reads badly.
|
||||
*/
|
||||
private static Offer offer(
|
||||
/** As {@link #resolution(FailureActionId, FailureAudience)}, with this kind's own wording. */
|
||||
private static Offer resolution(
|
||||
FailureActionId id, FailureAudience audience, String labelKeySuffix) {
|
||||
return new Offer(id, audience, labelKeySuffix);
|
||||
return new Offer(id, audience, FailureActionSlot.RESOLUTION, labelKeySuffix);
|
||||
}
|
||||
|
||||
/** Not this kind's fix: an offer any kind can make, with the shared wording. */
|
||||
private static Offer global(
|
||||
FailureActionId id, FailureAudience audience, FailureActionSlot slot) {
|
||||
return new Offer(id, audience, slot, null);
|
||||
}
|
||||
|
||||
/** As above, with this kind's own wording where the shared one reads badly. */
|
||||
private static Offer global(
|
||||
FailureActionId id,
|
||||
FailureAudience audience,
|
||||
FailureActionSlot slot,
|
||||
String labelKeySuffix) {
|
||||
return new Offer(id, audience, slot, labelKeySuffix);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,21 +171,25 @@ public enum FailureKind {
|
||||
return offers.stream().map(Offer::id).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* What this kind offers, in declaration order, each with its label resolved. What a review
|
||||
* surface reads, so it never has to ask two separate questions about one offer.
|
||||
*/
|
||||
/** What this kind offers, in declaration order, each with label and placement resolved. */
|
||||
public List<OfferedAction> getOfferedActions() {
|
||||
return offers.stream()
|
||||
.map(
|
||||
offer ->
|
||||
new OfferedAction(
|
||||
offer.id(), labelKeyFor(offer.id()), offer.audience()))
|
||||
offer.id(),
|
||||
labelKeyFor(offer.id()),
|
||||
offer.audience(),
|
||||
offer.slot()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/** One action as a kind declares it: what to call it and who it is for. */
|
||||
public record OfferedAction(FailureActionId id, String labelKey, FailureAudience audience) {}
|
||||
/** One action as a kind declares it: what to call it, who it is for, where it wants to sit. */
|
||||
public record OfferedAction(
|
||||
FailureActionId id,
|
||||
String labelKey,
|
||||
FailureAudience audience,
|
||||
FailureActionSlot slot) {}
|
||||
|
||||
/** Whether this kind offers {@code action}. The dispatch guard: see {@code FailureActionId}. */
|
||||
public boolean declares(FailureActionId action) {
|
||||
|
||||
+5
-4
@@ -64,16 +64,17 @@ public interface FileRunEventRepository extends JpaRepository<FileRunEventEntity
|
||||
int fold(@Param("id") String id, @Param("now") Instant now, @Param("detail") String detail);
|
||||
|
||||
/**
|
||||
* Reopen a resolved incident whose failure has recurred. Guarded on the current status so only
|
||||
* {@code RESOLVED} flips; a concurrent dismiss is never overwritten back to {@code NEW}.
|
||||
* A recurrence reopens {@code RESOLVED} (the fix did not hold) and {@code FILE_REMOVED} (the
|
||||
* document is back). Guarded, so a reviewer's {@code DISMISSED} is never overwritten.
|
||||
*/
|
||||
@Modifying(clearAutomatically = true)
|
||||
@Transactional
|
||||
@Query(
|
||||
"update FileRunEventEntity e set"
|
||||
+ " e.status = stirling.software.proprietary.failure.FileRunEventStatus.NEW,"
|
||||
+ " e.statusActor = null, e.statusAt = null where e.id = :id and e.status ="
|
||||
+ " stirling.software.proprietary.failure.FileRunEventStatus.RESOLVED")
|
||||
+ " e.statusActor = null, e.statusAt = null where e.id = :id and e.status in"
|
||||
+ " (stirling.software.proprietary.failure.FileRunEventStatus.RESOLVED,"
|
||||
+ " stirling.software.proprietary.failure.FileRunEventStatus.FILE_REMOVED)")
|
||||
int reopenIfResolved(@Param("id") String id);
|
||||
|
||||
/**
|
||||
|
||||
+53
-4
@@ -1,5 +1,9 @@
|
||||
package stirling.software.proprietary.failure;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -171,6 +175,18 @@ public class FileRunEventService {
|
||||
return action.execute(event, inputs == null ? Map.of() : inputs, currentActor());
|
||||
}
|
||||
|
||||
/** Mark an incident resolved after a client's own retry worked. Idempotent. */
|
||||
public FileRunEvent resolve(String eventId) {
|
||||
FileRunEvent event = requireVisible(eventId);
|
||||
// No terminal pre-check: the store's guarded UPDATE decides, rather than racing a read.
|
||||
return store.applyStatusOnce(
|
||||
event.id(),
|
||||
event.teamId(),
|
||||
FileRunEventStatus.RESOLVED,
|
||||
currentActor(),
|
||||
FileRunEventStatus.open());
|
||||
}
|
||||
|
||||
/** "No such event" rather than a refusal, so trying does not confirm a colleague's exists. */
|
||||
private FileRunEvent requireVisible(String eventId) {
|
||||
ReadScope scope = readScope();
|
||||
@@ -222,7 +238,8 @@ public class FileRunEventService {
|
||||
boolean unattended,
|
||||
boolean documentless) {
|
||||
String reason = disabledReasonFor(offer.audience(), closed, unattended, documentless);
|
||||
return new AvailableAction(offer.id(), offer.labelKey(), reason == null, reason);
|
||||
return new AvailableAction(
|
||||
offer.id(), offer.labelKey(), offer.slot(), reason == null, reason);
|
||||
}
|
||||
|
||||
/** Closed wins over everything, then the owner-only reasons, most specific first. */
|
||||
@@ -251,11 +268,38 @@ public class FileRunEventService {
|
||||
};
|
||||
}
|
||||
|
||||
/** Login disabled has no roles, so its one operator triages everything. */
|
||||
private boolean reviewsTeam() {
|
||||
/** Whether the caller triages the team's incidents, not only their own. Login disabled: all. */
|
||||
public boolean reviewsTeam() {
|
||||
return !enforced() || policyManagementAuthority.canEditPolicies();
|
||||
}
|
||||
|
||||
/**
|
||||
* An opaque, stable discriminator for the calling viewer, for a client scoping per-browser read
|
||||
* state. Hashed rather than the username itself: a client only needs to tell one viewer from
|
||||
* another, and the value ends up in that browser's own storage.
|
||||
*
|
||||
* <p>{@code "anonymous"} with login disabled, where the one operator is every viewer.
|
||||
*/
|
||||
public String viewerKey() {
|
||||
String actor = currentActor();
|
||||
return actor == null || actor.isBlank() ? "anonymous" : sha256Prefix(actor);
|
||||
}
|
||||
|
||||
/** First 8 bytes of SHA-256 as hex: stable, one-way, and collision-safe enough to key on. */
|
||||
private static String sha256Prefix(String value) {
|
||||
try {
|
||||
byte[] digest =
|
||||
MessageDigest.getInstance("SHA-256")
|
||||
.digest(value.getBytes(StandardCharsets.UTF_8));
|
||||
return HexFormat.of().formatHex(digest, 0, 8);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// Every JVM ships SHA-256; a constant here would silently merge two viewers' read
|
||||
// state, so the caller gets no key and the client falls back to showing everything.
|
||||
log.warn("SHA-256 unavailable, so notifications cannot be scoped to a viewer", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private FailureActionId parseActionId(String actionId) {
|
||||
for (FailureActionId candidate : FailureActionId.values()) {
|
||||
if (candidate.name().equals(actionId)) {
|
||||
@@ -326,6 +370,11 @@ public class FileRunEventService {
|
||||
return applicationProperties.getSecurity().isEnableLogin();
|
||||
}
|
||||
|
||||
/** One action offered to one caller, availability resolved. */
|
||||
public record AvailableAction(
|
||||
FailureActionId id, String labelKey, boolean enabled, String disabledReasonKey) {}
|
||||
FailureActionId id,
|
||||
String labelKey,
|
||||
FailureActionSlot slot,
|
||||
boolean enabled,
|
||||
String disabledReasonKey) {}
|
||||
}
|
||||
|
||||
+3
-7
@@ -3,10 +3,7 @@ package stirling.software.proprietary.failure;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Disposition of one recorded failure. {@code RESOLVED} is declared but not set yet (it becomes
|
||||
* system-set later); the rollup already defines what a repeat means for it, which is to reopen.
|
||||
*/
|
||||
/** Disposition of one recorded failure. {@code RESOLVED} is system-set; a repeat reopens it. */
|
||||
public enum FileRunEventStatus {
|
||||
NEW(false),
|
||||
ACKNOWLEDGED(false),
|
||||
@@ -14,9 +11,8 @@ public enum FileRunEventStatus {
|
||||
RESOLVED(true),
|
||||
|
||||
/**
|
||||
* The document this incident was about was deleted from its owner's editor, so there is nothing
|
||||
* left to act on. Distinct from {@code DISMISSED}, which is a reviewer's decision, and from
|
||||
* {@code RESOLVED}, which reopens on recurrence: this one cannot recur, the file is gone.
|
||||
* The document was deleted, so there is nothing left to act on. A recurrence reopens it like
|
||||
* {@code RESOLVED}: a fresh failure is proof the document is back.
|
||||
*/
|
||||
FILE_REMOVED(true);
|
||||
|
||||
|
||||
+4
-2
@@ -61,14 +61,15 @@ public record FileRunEventView(
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code defaultLabel} and {@code execution} let a client render and route an action it was
|
||||
* never built with. Declaration order is display order.
|
||||
* {@code defaultLabel} and {@code execution} let a client render an action it was never built
|
||||
* with; {@code slot} is placement intent. See {@link FailureActionSlot}.
|
||||
*/
|
||||
public record ActionView(
|
||||
String id,
|
||||
String labelKey,
|
||||
String defaultLabel,
|
||||
FailureActionId.Execution execution,
|
||||
FailureActionSlot slot,
|
||||
boolean enabled,
|
||||
String disabledReasonKey) {
|
||||
|
||||
@@ -78,6 +79,7 @@ public record FileRunEventView(
|
||||
action.labelKey(),
|
||||
action.id().getDefaultLabel(),
|
||||
action.id().getExecution(),
|
||||
action.slot(),
|
||||
action.enabled(),
|
||||
action.disabledReasonKey());
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package stirling.software.proprietary.model;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
@@ -19,7 +20,7 @@ import lombok.*;
|
||||
@ToString
|
||||
public class UserLicenseSettings implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final Long SINGLETON_ID = 1L;
|
||||
|
||||
|
||||
+3
@@ -25,4 +25,7 @@ public class AiWorkflowRequest {
|
||||
"Prior chat messages exchanged between the user and the assistant, ordered"
|
||||
+ " oldest-first. Excludes the current userMessage.")
|
||||
private List<AiConversationMessage> conversationHistory = new ArrayList<>();
|
||||
|
||||
@Schema(description = "IETF language tag the reply should be written in", example = "fr-FR")
|
||||
private String locale;
|
||||
}
|
||||
|
||||
+35
-4
@@ -2,10 +2,14 @@ package stirling.software.proprietary.notification;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Hidden;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -13,9 +17,11 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import stirling.software.proprietary.failure.FailureActionException;
|
||||
|
||||
/**
|
||||
* Open to any authenticated user, unlike the failure endpoints it draws on: each source scopes its
|
||||
* own rows. Read-only, because every action a notification offers runs on the client's own device.
|
||||
* Open to any authenticated user: each source scopes its own rows. Every action runs on the
|
||||
* client's own device, so the only write is it reporting a fix.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/notifications")
|
||||
@@ -40,9 +46,34 @@ public class NotificationController {
|
||||
+ " to mark read here yet: the client tracks what it has shown.")
|
||||
public NotificationsResponse list(@RequestParam(required = false) Integer limit) {
|
||||
int capped = Math.min(limit == null ? DEFAULT_LIMIT : Math.max(1, limit), MAX_LIMIT);
|
||||
return new NotificationsResponse(notifications.list(capped));
|
||||
return new NotificationsResponse(
|
||||
notifications.list(capped),
|
||||
notifications.callerReviewsTeam(),
|
||||
notifications.callerViewerKey());
|
||||
}
|
||||
|
||||
@PostMapping("/{notificationId}/resolved")
|
||||
@Operation(
|
||||
summary = "Record that a client-side retry fixed what a notification was about",
|
||||
description =
|
||||
"Takes the prefixed notification id, not the producing row's id. Not an action:"
|
||||
+ " nobody is offered a resolve button, and a recurrence brings the"
|
||||
+ " notification back.")
|
||||
public NotificationView resolved(@PathVariable String notificationId) {
|
||||
try {
|
||||
return notifications.resolve(notificationId);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage(), e);
|
||||
} catch (FailureActionException e) {
|
||||
throw new ResponseStatusException(
|
||||
FailureActionException.statusOf(e.getReason()), e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/** Wrapped so paging or a total can be added without breaking clients. */
|
||||
public record NotificationsResponse(List<NotificationView> notifications) {}
|
||||
public record NotificationsResponse(
|
||||
List<NotificationView> notifications,
|
||||
boolean viewerReviewsTeam,
|
||||
/** Opaque; the client scopes its own read state on it. Empty means "cannot scope". */
|
||||
String viewerKey) {}
|
||||
}
|
||||
|
||||
+40
-2
@@ -20,9 +20,47 @@ public class NotificationService {
|
||||
|
||||
private final FileRunEventService fileRunEvents;
|
||||
|
||||
/** Newest first, and only open failures: one already dealt with is not news. */
|
||||
/**
|
||||
* Newest first, and only open failures about a document: one already dealt with is not news,
|
||||
* and a row naming no file has nothing the bell can offer beyond saying so.
|
||||
*
|
||||
* <p>Filtered on the named file rather than the kind's scope, because a RUN-scoped kind still
|
||||
* names one when the editor reported it: a failed tool run belongs here. Applied after the
|
||||
* limit, so a page can come back short while unattributed rows exist - the review surface is
|
||||
* where those are meant to be read, and it lists them unfiltered.
|
||||
*/
|
||||
public List<NotificationView> list(int limit) {
|
||||
return fileRunEvents.list(null, null, limit).stream().map(this::fromFailure).toList();
|
||||
return fileRunEvents.list(null, null, limit).stream()
|
||||
.filter(event -> event.fileId() != null && !event.fileId().isBlank())
|
||||
.map(this::fromFailure)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/** Whether the caller sees the whole team's incidents rather than only their own. */
|
||||
public boolean callerReviewsTeam() {
|
||||
return fileRunEvents.reviewsTeam();
|
||||
}
|
||||
|
||||
/** Opaque and stable, so a shared browser can keep one viewer's read state off another's. */
|
||||
public String callerViewerKey() {
|
||||
return fileRunEvents.viewerKey();
|
||||
}
|
||||
|
||||
/** Takes the prefixed id, so the bell cannot reach a failure endpoint even by accident. */
|
||||
public NotificationView resolve(String notificationId) {
|
||||
NotificationSource.QualifiedId qualified = qualify(notificationId);
|
||||
return switch (qualified.source()) {
|
||||
case FAILURE -> fromFailure(fileRunEvents.resolve(qualified.rowId()));
|
||||
};
|
||||
}
|
||||
|
||||
/** The source and row id behind a notification id, refusing anything that is not one. */
|
||||
private static NotificationSource.QualifiedId qualify(String notificationId) {
|
||||
return NotificationSource.parse(notificationId)
|
||||
.orElseThrow(
|
||||
() ->
|
||||
new IllegalArgumentException(
|
||||
"Not a notification id: " + notificationId));
|
||||
}
|
||||
|
||||
/** Prefixes the row id on the way out, so it is never sent bare. */
|
||||
|
||||
+22
@@ -1,6 +1,8 @@
|
||||
package stirling.software.proprietary.notification;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Which subsystem produced a notification. Every id is prefixed with it, so a client never holds
|
||||
@@ -18,4 +20,24 @@ public enum NotificationSource {
|
||||
public String qualify(String sourceRowId) {
|
||||
return prefix() + sourceRowId;
|
||||
}
|
||||
|
||||
/** Empty rather than throwing for an unprefixed or unknown id: both arrive from clients. */
|
||||
public static Optional<QualifiedId> parse(String notificationId) {
|
||||
if (notificationId == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
int separator = notificationId.indexOf(SEPARATOR);
|
||||
if (separator <= 0 || separator == notificationId.length() - 1) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String prefix = notificationId.substring(0, separator);
|
||||
String rowId = notificationId.substring(separator + 1);
|
||||
return Arrays.stream(values())
|
||||
.filter(source -> source.name().equalsIgnoreCase(prefix))
|
||||
.findFirst()
|
||||
.map(source -> new QualifiedId(source, rowId));
|
||||
}
|
||||
|
||||
/** A notification id split into the source that owns it and that source's own row id. */
|
||||
public record QualifiedId(NotificationSource source, String rowId) {}
|
||||
}
|
||||
|
||||
+13
-1
@@ -336,6 +336,15 @@ public class PolicyController {
|
||||
* nothing to check.
|
||||
*/
|
||||
private void requireAccessibleOutput(Policy policy) {
|
||||
// An editor policy hands its results back to the workspace the file came from. A stored
|
||||
// destination would send the run to a folder or bucket instead, leaving the editor's copy
|
||||
// untouched - and the editor's import would then have nothing to collect.
|
||||
if (policy.editor().allowed() && !policy.outputIds().isEmpty()) {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"An editor policy delivers back to the editor and can't also have a"
|
||||
+ " destination");
|
||||
}
|
||||
for (String outputId : policy.outputIds()) {
|
||||
Source destination =
|
||||
sourceStore
|
||||
@@ -389,11 +398,14 @@ public class PolicyController {
|
||||
policy.name(),
|
||||
owner,
|
||||
policy.enabled(),
|
||||
policy.required(),
|
||||
policy.icon(),
|
||||
policy.inputs(),
|
||||
policy.steps(),
|
||||
policy.output(),
|
||||
policy.outputIds(),
|
||||
teamId);
|
||||
teamId,
|
||||
policy.editor());
|
||||
}
|
||||
|
||||
/** Output secrets never leave the server: reads return the redaction sentinel instead. */
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package stirling.software.proprietary.policy.model;
|
||||
|
||||
/**
|
||||
* How a policy participates in the editor: it fires in the browser as each file passes through,
|
||||
* rather than being swept from a stored {@code Source} on a trigger.
|
||||
*
|
||||
* <p>An object rather than a bare flag so the moment it fires ({@code runOn}) travels with the
|
||||
* decision, and so later editor-only settings have somewhere to live.
|
||||
*
|
||||
* @param allowed whether the editor may run this policy at all
|
||||
* @param runOn which moment it fires on: {@code "upload"} or {@code "export"}
|
||||
*/
|
||||
public record EditorConfig(boolean allowed, String runOn) {
|
||||
|
||||
public static final String UPLOAD = "upload";
|
||||
public static final String EXPORT = "export";
|
||||
|
||||
public EditorConfig {
|
||||
runOn = EXPORT.equals(runOn) ? EXPORT : UPLOAD;
|
||||
}
|
||||
|
||||
/** Not an editor policy: swept server-side, or run only on demand. */
|
||||
public static EditorConfig disabled() {
|
||||
return new EditorConfig(false, UPLOAD);
|
||||
}
|
||||
|
||||
public static EditorConfig onUpload() {
|
||||
return new EditorConfig(true, UPLOAD);
|
||||
}
|
||||
|
||||
public static EditorConfig onExport() {
|
||||
return new EditorConfig(true, EXPORT);
|
||||
}
|
||||
}
|
||||
+71
-4
@@ -1,6 +1,7 @@
|
||||
package stirling.software.proprietary.policy.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* A stored automation: ordered tool steps, input bindings, and output destinations.
|
||||
@@ -20,17 +21,59 @@ public record Policy(
|
||||
String name,
|
||||
String owner,
|
||||
boolean enabled,
|
||||
boolean required,
|
||||
String icon,
|
||||
List<PipelineInput> inputs,
|
||||
List<PipelineStep> steps,
|
||||
OutputSpec output,
|
||||
List<String> outputIds,
|
||||
Long teamId) {
|
||||
Long teamId,
|
||||
EditorConfig editor) {
|
||||
|
||||
public Policy {
|
||||
icon = icon == null ? "" : icon;
|
||||
inputs = inputs == null ? List.of() : List.copyOf(inputs);
|
||||
steps = steps == null ? List.of() : steps;
|
||||
output = output == null ? OutputSpec.inline() : output;
|
||||
outputIds = outputIds == null ? List.of() : List.copyOf(outputIds);
|
||||
editor = editor == null ? EditorConfig.disabled() : editor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Without the {@code required} flag, {@code icon}, or editor participation: defaults to not
|
||||
* org-required, no icon, and a swept/on-demand policy. Kept for the many callers and tests
|
||||
* written before those fields; the frontend and stores that care use the full constructor.
|
||||
*/
|
||||
public Policy(
|
||||
String id,
|
||||
String name,
|
||||
String owner,
|
||||
boolean enabled,
|
||||
List<PipelineInput> inputs,
|
||||
List<PipelineStep> steps,
|
||||
OutputSpec output,
|
||||
List<String> outputIds,
|
||||
Long teamId) {
|
||||
this(id, name, owner, enabled, false, "", inputs, steps, output, outputIds, teamId, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Without the {@code required} flag or {@code icon} but with explicit editor participation: the
|
||||
* seeded Classification policy runs on the editor, so it must set {@link EditorConfig} even
|
||||
* though it predates the org-required and icon fields.
|
||||
*/
|
||||
public Policy(
|
||||
String id,
|
||||
String name,
|
||||
String owner,
|
||||
boolean enabled,
|
||||
List<PipelineInput> inputs,
|
||||
List<PipelineStep> steps,
|
||||
OutputSpec output,
|
||||
List<String> outputIds,
|
||||
Long teamId,
|
||||
EditorConfig editor) {
|
||||
this(id, name, owner, enabled, false, "", inputs, steps, output, outputIds, teamId, editor);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,6 +113,14 @@ public record Policy(
|
||||
return inputs.stream().map(PipelineInput::sourceId).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* The moment this policy fires in the editor ("upload" / "export"), or empty when the editor
|
||||
* does not run it. Legacy blobs are lifted onto {@link EditorConfig} when they are read.
|
||||
*/
|
||||
public Optional<String> editorRunOn() {
|
||||
return editor.allowed() ? Optional.of(editor.runOn()) : Optional.empty();
|
||||
}
|
||||
|
||||
/** The distinct trigger types configured across this policy's inputs (manual inputs aside). */
|
||||
public List<String> triggerTypes() {
|
||||
return inputs.stream()
|
||||
@@ -82,17 +133,33 @@ public record Policy(
|
||||
|
||||
/** A copy with the inline output replaced (e.g. resolved for the engine, or migrated). */
|
||||
public Policy withOutput(OutputSpec resolved) {
|
||||
return new Policy(id, name, owner, enabled, inputs, steps, resolved, outputIds, teamId);
|
||||
return new Policy(
|
||||
id, name, owner, enabled, required, icon, inputs, steps, resolved, outputIds,
|
||||
teamId, editor);
|
||||
}
|
||||
|
||||
/** A copy under a different owner (e.g. moving a seed off a placeholder name). */
|
||||
public Policy withOwner(String newOwner) {
|
||||
return new Policy(id, name, newOwner, enabled, inputs, steps, output, outputIds, teamId);
|
||||
return new Policy(
|
||||
id, name, newOwner, enabled, required, icon, inputs, steps, output, outputIds,
|
||||
teamId, editor);
|
||||
}
|
||||
|
||||
/** A copy referencing the given saved output destinations. */
|
||||
public Policy withOutputIds(List<String> newOutputIds) {
|
||||
return new Policy(id, name, owner, enabled, inputs, steps, output, newOutputIds, teamId);
|
||||
return new Policy(
|
||||
id,
|
||||
name,
|
||||
owner,
|
||||
enabled,
|
||||
required,
|
||||
icon,
|
||||
inputs,
|
||||
steps,
|
||||
output,
|
||||
newOutputIds,
|
||||
teamId,
|
||||
editor);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+32
-24
@@ -20,28 +20,23 @@ import stirling.software.proprietary.policy.source.SourceStore;
|
||||
import stirling.software.proprietary.policy.store.PolicyStore;
|
||||
|
||||
/**
|
||||
* Builds the Pipelines overview: one row per policy the caller's team built on the Pipelines page,
|
||||
* with its sources resolved to live display names, its steps, and a trigger/output summary.
|
||||
* Frontend/catalogue policies (marked by a {@code categoryId} in their output options) belong to
|
||||
* the user-facing Policies page and are excluded; a folder-watch trigger is not a signal.
|
||||
* Builds the unified Pipelines overview: one row per policy the caller's team owns, with its
|
||||
* sources resolved to live display names, its steps, and a trigger/output summary. This lists EVERY
|
||||
* policy - both pipelines built in the full builder and the friendly "suggested" policies - since
|
||||
* the two surfaces were merged (a policy is a pipeline the org requires). No catalogue filter any
|
||||
* more.
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PolicyOverviewService {
|
||||
|
||||
// Output-options key marking a frontend/catalogue policy (set by the Policies page and seeder).
|
||||
private static final String CATEGORY_OPTION = "categoryId";
|
||||
|
||||
private final PolicyStore policyStore;
|
||||
private final SourceStore sourceStore;
|
||||
private final PolicyAccessGuard policyAccessGuard;
|
||||
private final SourceAccessGuard sourceAccessGuard;
|
||||
|
||||
public PoliciesOverviewResponse overview() {
|
||||
List<Policy> policies =
|
||||
policyAccessGuard.visibleFrom(policyStore).stream()
|
||||
.filter(PolicyOverviewService::isPipeline)
|
||||
.toList();
|
||||
List<Policy> policies = policyAccessGuard.visibleFrom(policyStore).stream().toList();
|
||||
Map<String, String> sourceNames = sourceNames();
|
||||
|
||||
List<PolicyView> views =
|
||||
@@ -55,18 +50,6 @@ public class PolicyOverviewService {
|
||||
return new PoliciesOverviewResponse(buildKpis(policies), views);
|
||||
}
|
||||
|
||||
private static boolean isPipeline(Policy policy) {
|
||||
return !isCataloguePolicy(policy);
|
||||
}
|
||||
|
||||
/** A frontend/catalogue policy, marked by a {@code categoryId} in its output options. */
|
||||
private static boolean isCataloguePolicy(Policy policy) {
|
||||
OutputSpec output = policy.output();
|
||||
return output != null
|
||||
&& output.options().get(CATEGORY_OPTION) instanceof String category
|
||||
&& !category.isBlank();
|
||||
}
|
||||
|
||||
/** Display names for every source the caller's team can see, keyed by source id. */
|
||||
private Map<String, String> sourceNames() {
|
||||
Map<String, String> names = new HashMap<>();
|
||||
@@ -88,6 +71,8 @@ public class PolicyOverviewService {
|
||||
policy.id(),
|
||||
policy.name(),
|
||||
policy.enabled(),
|
||||
policy.required(),
|
||||
iconKey(policy),
|
||||
policy.enabled() ? "active" : "paused",
|
||||
triggerSummary(policy),
|
||||
sources,
|
||||
@@ -111,13 +96,36 @@ public class PolicyOverviewService {
|
||||
return outputSummary(policy.output());
|
||||
}
|
||||
|
||||
/**
|
||||
* The list-row icon key. The policy's first-class {@code icon} wins; otherwise a
|
||||
* template-derived policy falls back to its {@code categoryId} (the template-identity marker
|
||||
* the frontend maps to the category glyph). Empty when neither is set, so the frontend shows
|
||||
* its default.
|
||||
*/
|
||||
private static String iconKey(Policy policy) {
|
||||
if (!policy.icon().isBlank()) {
|
||||
return policy.icon();
|
||||
}
|
||||
OutputSpec output = policy.output();
|
||||
if (output != null
|
||||
&& output.options().get("categoryId") instanceof String category
|
||||
&& !category.isBlank()) {
|
||||
return category;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarise a policy's triggers for the overview row: "manual" when no input is triggered,
|
||||
* otherwise the distinct trigger types across its inputs (e.g. "folder-watch, schedule").
|
||||
*
|
||||
* <p>An editor policy has no wire input to trigger, but it is not manual either - it fires in
|
||||
* the editor on every upload or export, so it reports that rather than reading as on-demand.
|
||||
*/
|
||||
private static String triggerSummary(Policy policy) {
|
||||
List<String> types = policy.triggerTypes();
|
||||
return types.isEmpty() ? "manual" : String.join(", ", types);
|
||||
if (!types.isEmpty()) return String.join(", ", types);
|
||||
return policy.editorRunOn().map(runOn -> "editor-" + runOn).orElse("manual");
|
||||
}
|
||||
|
||||
private static String outputSummary(OutputSpec output) {
|
||||
|
||||
+7
-4
@@ -3,15 +3,18 @@ package stirling.software.proprietary.policy.overview;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* One row in the Pipelines overview: a stored policy shown for the admin portal, with its
|
||||
* referenced sources resolved to names and its pipeline summarised. The portal's "all pipelines"
|
||||
* surface lists every backend policy (the user-facing Policies page builds only a friendly subset
|
||||
* of these).
|
||||
* One row in the unified Pipelines overview: a stored policy shown for the admin portal, with its
|
||||
* referenced sources resolved to names and its pipeline summarised. This surface lists every
|
||||
* backend policy - both the pipelines built in the full builder and the friendly "suggested"
|
||||
* policies - so a {@code required} policy (one the org mandates) reads the same as any other
|
||||
* pipeline here.
|
||||
*/
|
||||
public record PolicyView(
|
||||
String id,
|
||||
String name,
|
||||
boolean enabled,
|
||||
boolean required,
|
||||
String icon,
|
||||
String status,
|
||||
String trigger,
|
||||
List<SourceRef> sources,
|
||||
|
||||
+6
-3
@@ -14,6 +14,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.proprietary.model.TeamCreatedEvent;
|
||||
import stirling.software.proprietary.policy.model.EditorConfig;
|
||||
import stirling.software.proprietary.policy.model.OutputSpec;
|
||||
import stirling.software.proprietary.policy.model.PipelineStep;
|
||||
import stirling.software.proprietary.policy.model.Policy;
|
||||
@@ -98,9 +99,8 @@ public class DefaultClassificationPolicySeeder {
|
||||
static Policy defaultPolicy(Long teamId) {
|
||||
Map<String, Object> options = new HashMap<>();
|
||||
options.put("categoryId", CATEGORY);
|
||||
options.put("runOn", "upload");
|
||||
options.put("mode", "new_version");
|
||||
options.put("sources", List.of("editor"));
|
||||
options.put("sources", List.of());
|
||||
options.put("scopeTypes", List.of());
|
||||
options.put("reviewerEmail", "");
|
||||
return new Policy(
|
||||
@@ -113,6 +113,9 @@ public class DefaultClassificationPolicySeeder {
|
||||
List.of(),
|
||||
List.of(new PipelineStep(CLASSIFY_ENDPOINT, Map.of())),
|
||||
new OutputSpec("inline", options),
|
||||
teamId);
|
||||
List.of(),
|
||||
teamId,
|
||||
// Classification runs in the editor on every upload.
|
||||
EditorConfig.onUpload());
|
||||
}
|
||||
}
|
||||
|
||||
+4
-6
@@ -107,14 +107,12 @@ public class SourceOverviewService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a policy runs from the editor. Editor membership is carried in the policy's output
|
||||
* metadata ({@code output.options.sources}) - a client-side list the editor writes when a
|
||||
* policy targets it - rather than as a persisted {@code sourceId}, because the editor is
|
||||
* virtual and has no stored source to reference.
|
||||
* Whether a policy runs from the editor. Read from the policy's first-class {@link
|
||||
* stirling.software.proprietary.policy.model.EditorConfig}, never inferred from a sources list
|
||||
* (the editor is not a real source).
|
||||
*/
|
||||
private static boolean runsFromEditor(Policy policy) {
|
||||
Object sources = policy.output().options().get("sources");
|
||||
return sources instanceof List<?> list && list.contains(EditorSource.ID);
|
||||
return policy.editor().allowed();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+4
-1
@@ -34,11 +34,14 @@ public class InProcessPolicyStore implements PolicyStore {
|
||||
policy.name(),
|
||||
policy.owner(),
|
||||
policy.enabled(),
|
||||
policy.required(),
|
||||
policy.icon(),
|
||||
policy.inputs(),
|
||||
policy.steps(),
|
||||
policy.output(),
|
||||
policy.outputIds(),
|
||||
policy.teamId());
|
||||
policy.teamId(),
|
||||
policy.editor());
|
||||
policies.put(id, stored);
|
||||
// Existing policy keeps its position; a new one appends to the end of its team's queue.
|
||||
sortOrders.computeIfAbsent(id, key -> nextSortOrder(stored.teamId()));
|
||||
|
||||
+76
-3
@@ -3,6 +3,7 @@ package stirling.software.proprietary.policy.store;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -11,9 +12,12 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.proprietary.policy.model.EditorConfig;
|
||||
import stirling.software.proprietary.policy.model.Policy;
|
||||
import stirling.software.proprietary.policy.model.PolicyBinding;
|
||||
import stirling.software.proprietary.policy.source.EditorSource;
|
||||
|
||||
import tools.jackson.databind.DeserializationFeature;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
import tools.jackson.databind.node.ArrayNode;
|
||||
@@ -44,11 +48,14 @@ public class JpaPolicyStore implements PolicyStore {
|
||||
policy.name(),
|
||||
policy.owner(),
|
||||
policy.enabled(),
|
||||
policy.required(),
|
||||
policy.icon(),
|
||||
policy.inputs(),
|
||||
policy.steps(),
|
||||
policy.output(),
|
||||
policy.outputIds(),
|
||||
policy.teamId());
|
||||
policy.teamId(),
|
||||
policy.editor());
|
||||
|
||||
PolicyEntity entity = new PolicyEntity();
|
||||
entity.setId(id);
|
||||
@@ -148,8 +155,17 @@ public class JpaPolicyStore implements PolicyStore {
|
||||
// One unreadable row must never abort a bulk read or crash startup.
|
||||
private Optional<Policy> toPolicy(PolicyEntity entity) {
|
||||
try {
|
||||
JsonNode node = upgradeLegacyShape(objectMapper.readTree(entity.getPolicyJson()));
|
||||
return Optional.of(objectMapper.treeToValue(node, Policy.class));
|
||||
JsonNode node =
|
||||
liftEditorConfig(
|
||||
upgradeLegacyShape(objectMapper.readTree(entity.getPolicyJson())));
|
||||
// A blob written by an older version won't carry fields added since (e.g. required,
|
||||
// icon). Default absent primitives rather than rejecting the whole policy, so upgrades
|
||||
// don't drop existing pipelines.
|
||||
return Optional.of(
|
||||
objectMapper
|
||||
.readerFor(Policy.class)
|
||||
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
|
||||
.readValue(node));
|
||||
} catch (Exception e) {
|
||||
log.error(
|
||||
"Skipping unreadable policy id={} name={}: stored JSON could not be parsed"
|
||||
@@ -191,4 +207,61 @@ public class JpaPolicyStore implements PolicyStore {
|
||||
obj.remove("sourceIds");
|
||||
return obj;
|
||||
}
|
||||
|
||||
/** Categories whose editor moment defaulted to export before it was stored (see runOn.ts). */
|
||||
private static final Set<String> EXPORT_BY_DEFAULT = Set.of("security");
|
||||
|
||||
/**
|
||||
* Derive {@code editor} for a blob written before editor participation had its own field, from
|
||||
* its {@code output.options}: allowed when {@code sources} lists {@code "editor"}, or - for a
|
||||
* catalogue policy - when there is no {@code sources} list at all (an unnarrowed catalogue
|
||||
* policy runs in the editor).
|
||||
*
|
||||
* <p>Runs on every read, deliberately outside {@link #upgradeLegacyShape}'s early return: a
|
||||
* blob written after triggers moved onto {@code inputs} but before this field existed still
|
||||
* needs lifting, and that early return would skip exactly those rows.
|
||||
*/
|
||||
private JsonNode liftEditorConfig(JsonNode root) {
|
||||
if (!(root instanceof ObjectNode obj) || obj.hasNonNull("editor")) {
|
||||
return root;
|
||||
}
|
||||
JsonNode options = obj.path("output").path("options");
|
||||
String categoryId = text(options, "categoryId");
|
||||
JsonNode sources = options.get("sources");
|
||||
boolean listed = sources != null && sources.isArray() && !sources.isEmpty();
|
||||
boolean allowed;
|
||||
if (listed) {
|
||||
// An explicit scope list decides: only the editor's own id puts it on the editor.
|
||||
allowed = false;
|
||||
for (JsonNode source : sources) {
|
||||
if (source.isValueNode() && EditorSource.ID.equals(source.asString())) {
|
||||
allowed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No list: a catalogue policy ran in the editor by default, but a builder pipeline
|
||||
// (no category) could not reach the editor at all, so silence is not consent there.
|
||||
allowed = !categoryId.isBlank();
|
||||
}
|
||||
ObjectNode editor = objectMapper.createObjectNode();
|
||||
editor.put("allowed", allowed);
|
||||
editor.put("runOn", legacyRunOn(options, categoryId));
|
||||
obj.set("editor", editor);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/** The stored moment, or the category default the client applied when none was stored. */
|
||||
private static String legacyRunOn(JsonNode options, String categoryId) {
|
||||
String stored = text(options, "runOn");
|
||||
if (EditorConfig.EXPORT.equals(stored) || EditorConfig.UPLOAD.equals(stored)) {
|
||||
return stored;
|
||||
}
|
||||
return EXPORT_BY_DEFAULT.contains(categoryId) ? EditorConfig.EXPORT : EditorConfig.UPLOAD;
|
||||
}
|
||||
|
||||
private static String text(JsonNode parent, String field) {
|
||||
JsonNode node = parent.path(field);
|
||||
return node.isValueNode() ? node.asString() : "";
|
||||
}
|
||||
}
|
||||
|
||||
+17
-15
@@ -70,21 +70,23 @@ public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
|
||||
|
||||
if (!response.isCommitted()) {
|
||||
if (authentication != null) {
|
||||
if (authentication instanceof Saml2Authentication samlAuthentication) {
|
||||
// Handle SAML2 logout redirection
|
||||
getRedirect_saml2(request, response, samlAuthentication);
|
||||
} else if (authentication instanceof OAuth2AuthenticationToken oAuthToken) {
|
||||
// Handle OAuth2 logout redirection
|
||||
getRedirect_oauth2(request, response, oAuthToken);
|
||||
} else if (authentication instanceof UsernamePasswordAuthenticationToken) {
|
||||
// Handle Username/Password logout
|
||||
getRedirectStrategy().sendRedirect(request, response, LOGOUT_PATH);
|
||||
} else {
|
||||
// Handle unknown authentication types
|
||||
log.error(
|
||||
"Authentication class unknown: {}",
|
||||
authentication.getClass().getSimpleName());
|
||||
getRedirectStrategy().sendRedirect(request, response, LOGOUT_PATH);
|
||||
switch (authentication) {
|
||||
case Saml2Authentication samlAuthentication ->
|
||||
// Handle SAML2 logout redirection
|
||||
getRedirect_saml2(request, response, samlAuthentication);
|
||||
case OAuth2AuthenticationToken oAuthToken ->
|
||||
// Handle OAuth2 logout redirection
|
||||
getRedirect_oauth2(request, response, oAuthToken);
|
||||
case UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken ->
|
||||
// Handle Username/Password logout
|
||||
getRedirectStrategy().sendRedirect(request, response, LOGOUT_PATH);
|
||||
default -> {
|
||||
// Handle unknown authentication types
|
||||
log.error(
|
||||
"Authentication class unknown: {}",
|
||||
authentication.getClass().getSimpleName());
|
||||
getRedirectStrategy().sendRedirect(request, response, LOGOUT_PATH);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (jwtService != null) {
|
||||
|
||||
+37
-37
@@ -357,12 +357,12 @@ public class SecurityConfiguration {
|
||||
req -> {
|
||||
String uri = req.getRequestURI();
|
||||
String contextPath = req.getContextPath();
|
||||
// Check if it's a public auth endpoint or static
|
||||
// resource
|
||||
return RequestUriUtils.isStaticResource(
|
||||
contextPath, uri)
|
||||
|| RequestUriUtils.isPublicAuthEndpoint(
|
||||
uri, contextPath);
|
||||
uri, contextPath)
|
||||
|| RequestUriUtils.isFrontendRoute(
|
||||
contextPath, uri);
|
||||
})
|
||||
.permitAll()
|
||||
.anyRequest()
|
||||
@@ -392,40 +392,40 @@ public class SecurityConfiguration {
|
||||
// Handle OAUTH2 Logins
|
||||
if (securityProperties.isOauth2Active()) {
|
||||
http.oauth2Login(
|
||||
oauth2 -> {
|
||||
oauth2.loginPage("/login")
|
||||
.authorizationEndpoint(
|
||||
authorizationEndpoint -> {
|
||||
if (clientRegistrationRepository != null) {
|
||||
authorizationEndpoint
|
||||
.authorizationRequestResolver(
|
||||
new TauriAuthorizationRequestResolver(
|
||||
clientRegistrationRepository));
|
||||
}
|
||||
})
|
||||
.successHandler(
|
||||
new CustomOAuth2AuthenticationSuccessHandler(
|
||||
loginAttemptService,
|
||||
securityProperties.getOauth2(),
|
||||
userService,
|
||||
jwtService,
|
||||
licenseSettingsService,
|
||||
applicationProperties))
|
||||
.failureHandler(new CustomOAuth2AuthenticationFailureHandler())
|
||||
// Add existing Authorities from the database
|
||||
.userInfoEndpoint(
|
||||
userInfoEndpoint ->
|
||||
userInfoEndpoint
|
||||
.oidcUserService(
|
||||
new CustomOAuth2UserService(
|
||||
securityProperties
|
||||
.getOauth2(),
|
||||
userService,
|
||||
loginAttemptService))
|
||||
.userAuthoritiesMapper(
|
||||
oAuth2userAuthoritiesMapper))
|
||||
.permitAll();
|
||||
});
|
||||
oauth2 ->
|
||||
oauth2.loginPage("/login")
|
||||
.authorizationEndpoint(
|
||||
authorizationEndpoint -> {
|
||||
if (clientRegistrationRepository != null) {
|
||||
authorizationEndpoint
|
||||
.authorizationRequestResolver(
|
||||
new TauriAuthorizationRequestResolver(
|
||||
clientRegistrationRepository));
|
||||
}
|
||||
})
|
||||
.successHandler(
|
||||
new CustomOAuth2AuthenticationSuccessHandler(
|
||||
loginAttemptService,
|
||||
securityProperties.getOauth2(),
|
||||
userService,
|
||||
jwtService,
|
||||
licenseSettingsService,
|
||||
applicationProperties))
|
||||
.failureHandler(
|
||||
new CustomOAuth2AuthenticationFailureHandler())
|
||||
// Add existing Authorities from the database
|
||||
.userInfoEndpoint(
|
||||
userInfoEndpoint ->
|
||||
userInfoEndpoint
|
||||
.oidcUserService(
|
||||
new CustomOAuth2UserService(
|
||||
securityProperties
|
||||
.getOauth2(),
|
||||
userService,
|
||||
loginAttemptService))
|
||||
.userAuthoritiesMapper(
|
||||
oAuth2userAuthoritiesMapper))
|
||||
.permitAll());
|
||||
}
|
||||
// Handle SAML
|
||||
if (securityProperties.isSaml2Active() && runningProOrHigher) {
|
||||
|
||||
+12
-11
@@ -703,17 +703,18 @@ public class AuthController {
|
||||
}
|
||||
|
||||
private long extractEpochMillis(Object claimValue) {
|
||||
if (claimValue == null) {
|
||||
return -1L;
|
||||
}
|
||||
|
||||
if (claimValue instanceof java.util.Date date) {
|
||||
return date.getTime();
|
||||
}
|
||||
|
||||
if (claimValue instanceof Number number) {
|
||||
long epochSeconds = number.longValue();
|
||||
return epochSeconds * 1000L;
|
||||
switch (claimValue) {
|
||||
case null -> {
|
||||
return -1L;
|
||||
}
|
||||
case java.util.Date date -> {
|
||||
return date.getTime();
|
||||
}
|
||||
case Number number -> {
|
||||
long epochSeconds = number.longValue();
|
||||
return epochSeconds * 1000L;
|
||||
}
|
||||
default -> {}
|
||||
}
|
||||
|
||||
return -1L;
|
||||
|
||||
+8
-8
@@ -760,14 +760,14 @@ public class UserController {
|
||||
for (Object principal : principals) {
|
||||
List<SessionInformation> sessionsInformation =
|
||||
sessionRegistry.getAllSessions(principal, false);
|
||||
if (principal instanceof UserDetails detailsUser) {
|
||||
userNameP = detailsUser.getUsername();
|
||||
} else if (principal instanceof OAuth2User oAuth2User) {
|
||||
userNameP = oAuth2User.getName();
|
||||
} else if (principal instanceof CustomSaml2AuthenticatedPrincipal saml2User) {
|
||||
userNameP = saml2User.name();
|
||||
} else if (principal instanceof String stringUser) {
|
||||
userNameP = stringUser;
|
||||
switch (principal) {
|
||||
case null -> {}
|
||||
case UserDetails detailsUser -> userNameP = detailsUser.getUsername();
|
||||
case OAuth2User oAuth2User -> userNameP = oAuth2User.getName();
|
||||
case CustomSaml2AuthenticatedPrincipal saml2User ->
|
||||
userNameP = saml2User.name();
|
||||
case String stringUser -> userNameP = stringUser;
|
||||
default -> {}
|
||||
}
|
||||
if (userNameP.equalsIgnoreCase(username)) {
|
||||
for (SessionInformation sessionInfo : sessionsInformation) {
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package stirling.software.proprietary.security.model;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
@@ -28,7 +29,7 @@ import lombok.Setter;
|
||||
@Setter
|
||||
public class Authority implements GrantedAuthority, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package stirling.software.proprietary.security.model;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -18,7 +19,7 @@ import lombok.Setter;
|
||||
@Setter
|
||||
public class InviteToken implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
||||
+52
-47
@@ -36,57 +36,62 @@ public class CustomOAuth2AuthenticationFailureHandler
|
||||
AuthenticationException exception)
|
||||
throws IOException, ServletException {
|
||||
|
||||
if (exception instanceof BadCredentialsException) {
|
||||
log.error("BadCredentialsException", exception);
|
||||
getRedirectStrategy().sendRedirect(request, response, "/login?error=badCredentials");
|
||||
return;
|
||||
}
|
||||
if (exception instanceof DisabledException) {
|
||||
log.error("User is deactivated: ", exception);
|
||||
getRedirectStrategy().sendRedirect(request, response, "/logout?userIsDisabled=true");
|
||||
return;
|
||||
}
|
||||
if (exception instanceof LockedException) {
|
||||
log.error("Account locked: ", exception);
|
||||
getRedirectStrategy().sendRedirect(request, response, "/logout?error=locked");
|
||||
return;
|
||||
}
|
||||
if (exception instanceof OAuth2AuthenticationException oAuth2Exception) {
|
||||
OAuth2Error error = oAuth2Exception.getError();
|
||||
|
||||
String errorCode = error.getErrorCode();
|
||||
|
||||
if ("Password must not be null".equals(error.getErrorCode())) {
|
||||
errorCode = "userAlreadyExistsWeb";
|
||||
switch (exception) {
|
||||
case BadCredentialsException badCredentialsException -> {
|
||||
log.error("BadCredentialsException", exception);
|
||||
getRedirectStrategy()
|
||||
.sendRedirect(request, response, "/login?error=badCredentials");
|
||||
return;
|
||||
}
|
||||
case DisabledException disabledException -> {
|
||||
log.error("User is deactivated: ", exception);
|
||||
getRedirectStrategy()
|
||||
.sendRedirect(request, response, "/logout?userIsDisabled=true");
|
||||
return;
|
||||
}
|
||||
case LockedException lockedException -> {
|
||||
log.error("Account locked: ", exception);
|
||||
getRedirectStrategy().sendRedirect(request, response, "/logout?error=locked");
|
||||
return;
|
||||
}
|
||||
case OAuth2AuthenticationException oAuth2Exception -> {
|
||||
OAuth2Error error = oAuth2Exception.getError();
|
||||
|
||||
log.error(
|
||||
"OAuth2 Authentication error: {}",
|
||||
errorCode != null ? errorCode : exception.getMessage(),
|
||||
exception);
|
||||
String errorValue = errorCode != null ? errorCode : "oauth2AuthenticationError";
|
||||
clearRedirectCookie(response);
|
||||
boolean tauriState = TauriOAuthUtils.isTauriState(request);
|
||||
String redirectUrl;
|
||||
if (tauriState) {
|
||||
String basePath =
|
||||
TauriOAuthUtils.defaultTauriCallbackPath(request.getContextPath());
|
||||
redirectUrl = basePath;
|
||||
String stateParam = request.getParameter("state");
|
||||
if (stateParam != null && !stateParam.isBlank()) {
|
||||
redirectUrl = appendQueryParam(redirectUrl, "state", stateParam);
|
||||
// Extract and pass nonce for CSRF validation
|
||||
String nonce = TauriOAuthUtils.extractNonceFromState(stateParam);
|
||||
if (nonce != null) {
|
||||
redirectUrl = appendQueryParam(redirectUrl, "nonce", nonce);
|
||||
}
|
||||
String errorCode = error.getErrorCode();
|
||||
|
||||
if ("Password must not be null".equals(error.getErrorCode())) {
|
||||
errorCode = "userAlreadyExistsWeb";
|
||||
}
|
||||
redirectUrl = appendQueryParam(redirectUrl, "errorOAuth", errorValue);
|
||||
} else {
|
||||
redirectUrl = buildFailureRedirectUrl(request, errorValue);
|
||||
|
||||
log.error(
|
||||
"OAuth2 Authentication error: {}",
|
||||
errorCode != null ? errorCode : exception.getMessage(),
|
||||
exception);
|
||||
String errorValue = errorCode != null ? errorCode : "oauth2AuthenticationError";
|
||||
clearRedirectCookie(response);
|
||||
boolean tauriState = TauriOAuthUtils.isTauriState(request);
|
||||
String redirectUrl;
|
||||
if (tauriState) {
|
||||
String basePath =
|
||||
TauriOAuthUtils.defaultTauriCallbackPath(request.getContextPath());
|
||||
redirectUrl = basePath;
|
||||
String stateParam = request.getParameter("state");
|
||||
if (stateParam != null && !stateParam.isBlank()) {
|
||||
redirectUrl = appendQueryParam(redirectUrl, "state", stateParam);
|
||||
// Extract and pass nonce for CSRF validation
|
||||
String nonce = TauriOAuthUtils.extractNonceFromState(stateParam);
|
||||
if (nonce != null) {
|
||||
redirectUrl = appendQueryParam(redirectUrl, "nonce", nonce);
|
||||
}
|
||||
}
|
||||
redirectUrl = appendQueryParam(redirectUrl, "errorOAuth", errorValue);
|
||||
} else {
|
||||
redirectUrl = buildFailureRedirectUrl(request, errorValue);
|
||||
}
|
||||
getRedirectStrategy().sendRedirect(request, response, redirectUrl);
|
||||
return;
|
||||
}
|
||||
getRedirectStrategy().sendRedirect(request, response, redirectUrl);
|
||||
return;
|
||||
default -> {}
|
||||
}
|
||||
log.error("Unhandled authentication exception", exception);
|
||||
super.onAuthenticationFailure(request, response, exception);
|
||||
|
||||
+6
-1
@@ -61,7 +61,12 @@ public class CustomSaml2ResponseAuthenticationConverter
|
||||
|
||||
@Override
|
||||
public Saml2Authentication convert(ResponseToken responseToken) {
|
||||
Assertion assertion = responseToken.getResponse().getAssertions().getFirst();
|
||||
List<Assertion> assertions = responseToken.getResponse().getAssertions();
|
||||
if (assertions == null || assertions.isEmpty()) {
|
||||
log.error("SAML response contains no assertions");
|
||||
return null;
|
||||
}
|
||||
Assertion assertion = assertions.getFirst();
|
||||
Map<String, List<Object>> attributes = extractAttributes(assertion);
|
||||
|
||||
// Debug log with actual values
|
||||
|
||||
+5
-2
@@ -213,8 +213,11 @@ public class CustomOAuth2UserService implements OAuth2UserService<OidcUserReques
|
||||
}
|
||||
|
||||
sb.append(
|
||||
"\nWARNING: this block contains PII. Set security.oauth2.debugLogging=false once"
|
||||
+ " troubleshooting is complete.\n");
|
||||
"""
|
||||
|
||||
WARNING: this block contains PII. Set security.oauth2.debugLogging=false once\
|
||||
troubleshooting is complete.
|
||||
""");
|
||||
sb.append("========== [/OAUTH2 DEBUG] ==========");
|
||||
|
||||
if (failure) {
|
||||
|
||||
+3
-1
@@ -132,7 +132,9 @@ public class KeyPersistenceService implements KeyPersistenceServiceInterface {
|
||||
verifyingKeyCache.put(
|
||||
key.getKeyId(), new JwtVerificationKey(key.getKeyId(), key.getVerifyingKey()));
|
||||
}
|
||||
activeKey = new JwtVerificationKey(keys.get(0).getKeyId(), keys.get(0).getVerifyingKey());
|
||||
activeKey =
|
||||
new JwtVerificationKey(
|
||||
keys.getFirst().getKeyId(), keys.getFirst().getVerifyingKey());
|
||||
log.info("Loaded {} JWT key(s) from DB, active key: {}", keys.size(), activeKey.getKeyId());
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -640,14 +640,14 @@ public class UserService implements UserServiceInterface {
|
||||
for (Object principal : sessionRegistry.getAllPrincipals()) {
|
||||
for (SessionInformation sessionsInformation :
|
||||
sessionRegistry.getAllSessions(principal, false)) {
|
||||
if (principal instanceof UserDetails detailsUser) {
|
||||
usernameP = detailsUser.getUsername();
|
||||
} else if (principal instanceof OAuth2User oAuth2User) {
|
||||
usernameP = oAuth2User.getName();
|
||||
} else if (principal instanceof CustomSaml2AuthenticatedPrincipal saml2User) {
|
||||
usernameP = saml2User.name();
|
||||
} else if (principal instanceof String stringUser) {
|
||||
usernameP = stringUser;
|
||||
switch (principal) {
|
||||
case null -> {}
|
||||
case UserDetails detailsUser -> usernameP = detailsUser.getUsername();
|
||||
case OAuth2User oAuth2User -> usernameP = oAuth2User.getName();
|
||||
case CustomSaml2AuthenticatedPrincipal saml2User ->
|
||||
usernameP = saml2User.name();
|
||||
case String stringUser -> usernameP = stringUser;
|
||||
default -> {}
|
||||
}
|
||||
if (usernameP.equalsIgnoreCase(username)) {
|
||||
sessionRegistry.expireSession(sessionsInformation.getSessionId());
|
||||
|
||||
+14
-16
@@ -47,14 +47,13 @@ public class SessionPersistentRegistry implements SessionRegistry {
|
||||
List<SessionInformation> sessionInformations = new ArrayList<>();
|
||||
String principalName = null;
|
||||
|
||||
if (principal instanceof UserDetails detailsUser) {
|
||||
principalName = detailsUser.getUsername();
|
||||
} else if (principal instanceof OAuth2User oAuth2User) {
|
||||
principalName = oAuth2User.getName();
|
||||
} else if (principal instanceof CustomSaml2AuthenticatedPrincipal saml2User) {
|
||||
principalName = saml2User.name();
|
||||
} else if (principal instanceof String stringUser) {
|
||||
principalName = stringUser;
|
||||
switch (principal) {
|
||||
case null -> {}
|
||||
case UserDetails detailsUser -> principalName = detailsUser.getUsername();
|
||||
case OAuth2User oAuth2User -> principalName = oAuth2User.getName();
|
||||
case CustomSaml2AuthenticatedPrincipal saml2User -> principalName = saml2User.name();
|
||||
case String stringUser -> principalName = stringUser;
|
||||
default -> {}
|
||||
}
|
||||
|
||||
if (principalName != null) {
|
||||
@@ -78,14 +77,13 @@ public class SessionPersistentRegistry implements SessionRegistry {
|
||||
public void registerNewSession(String sessionId, Object principal) {
|
||||
String principalName = null;
|
||||
|
||||
if (principal instanceof UserDetails detailsUser) {
|
||||
principalName = detailsUser.getUsername();
|
||||
} else if (principal instanceof OAuth2User oAuth2User) {
|
||||
principalName = oAuth2User.getName();
|
||||
} else if (principal instanceof CustomSaml2AuthenticatedPrincipal saml2User) {
|
||||
principalName = saml2User.name();
|
||||
} else if (principal instanceof String stringUser) {
|
||||
principalName = stringUser;
|
||||
switch (principal) {
|
||||
case null -> {}
|
||||
case UserDetails detailsUser -> principalName = detailsUser.getUsername();
|
||||
case OAuth2User oAuth2User -> principalName = oAuth2User.getName();
|
||||
case CustomSaml2AuthenticatedPrincipal saml2User -> principalName = saml2User.name();
|
||||
case String stringUser -> principalName = stringUser;
|
||||
default -> {}
|
||||
}
|
||||
|
||||
if (principalName != null) {
|
||||
|
||||
+5
@@ -185,6 +185,7 @@ public class AiWorkflowService {
|
||||
initialRequest.setConversationHistory(
|
||||
new ArrayList<>(request.getConversationHistory()));
|
||||
initialRequest.setEnabledEndpoints(endpointResolver.getEnabledEndpointUrls());
|
||||
initialRequest.setLocale(request.getLocale());
|
||||
listener.onProgress(AiWorkflowProgressEvent.of(AiWorkflowPhase.ANALYZING));
|
||||
|
||||
WorkflowState state = new WorkflowState.Pending(initialRequest);
|
||||
@@ -287,6 +288,7 @@ public class AiWorkflowService {
|
||||
nextRequest.setArtifacts(pdfContentExtractor.buildArtifacts(contentResults));
|
||||
nextRequest.setResumeWith(response.getResumeWith());
|
||||
nextRequest.setEnabledEndpoints(request.getEnabledEndpoints());
|
||||
nextRequest.setLocale(request.getLocale());
|
||||
return new WorkflowState.Pending(nextRequest);
|
||||
} finally {
|
||||
for (LoadedFile lf : loadedFiles) {
|
||||
@@ -338,6 +340,7 @@ public class AiWorkflowService {
|
||||
nextRequest.setFiles(request.getFiles());
|
||||
nextRequest.setConversationHistory(request.getConversationHistory());
|
||||
nextRequest.setResumeWith(response.getResumeWith());
|
||||
nextRequest.setLocale(request.getLocale());
|
||||
return new WorkflowState.Pending(nextRequest);
|
||||
}
|
||||
|
||||
@@ -530,6 +533,7 @@ public class AiWorkflowService {
|
||||
new PdfContentExtractor.ToolReportArtifact(
|
||||
result.reportTool(), result.report()));
|
||||
resumeRequest.setResumeWith(resumeWith);
|
||||
resumeRequest.setLocale(previousRequest.getLocale());
|
||||
return new WorkflowState.Pending(resumeRequest);
|
||||
}
|
||||
|
||||
@@ -802,5 +806,6 @@ public class AiWorkflowService {
|
||||
private List<WorkflowArtifact> artifacts = new ArrayList<>();
|
||||
private String resumeWith;
|
||||
private List<String> enabledEndpoints = new ArrayList<>();
|
||||
private String locale;
|
||||
}
|
||||
}
|
||||
|
||||
+43
@@ -17,6 +17,7 @@ import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDResources;
|
||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||
import org.apache.pdfbox.text.PDFTextStripper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -158,6 +159,9 @@ public class FontEmbeddingService {
|
||||
if (after.getNumberOfPages() != before.getNumberOfPages()) {
|
||||
return false;
|
||||
}
|
||||
if (lostText(before, after)) {
|
||||
return false;
|
||||
}
|
||||
long beforeBytes = contentBytes(before);
|
||||
long afterBytes = contentBytes(after);
|
||||
if (beforeBytes == 0) {
|
||||
@@ -170,6 +174,45 @@ public class FontEmbeddingService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fraction of the original's extracted text a rewrite must still carry. The embedder re-encodes
|
||||
* text, so a few characters either way mean nothing; a tenth of the document going missing is
|
||||
* content loss.
|
||||
*/
|
||||
private static final double TEXT_RETENTION_FLOOR = 0.9;
|
||||
|
||||
/**
|
||||
* True when the rewrite dropped a meaningful share of the document's text.
|
||||
*
|
||||
* <p>Content-stream bytes cannot answer this on their own: the embedder recompresses, so they
|
||||
* move for reasons unrelated to the page keeping its content. An 80-page document measured here
|
||||
* came back with each page truncated to its first half - 422070 characters down to 211230 -
|
||||
* while its content streams stayed well inside the byte ratio below.
|
||||
*
|
||||
* <p>Growth is not loss: flattening a widget annotation into the page legitimately adds text.
|
||||
* Only a shortfall fails.
|
||||
*/
|
||||
private static boolean lostText(PDDocument before, PDDocument after) {
|
||||
String textBefore = extractText(before);
|
||||
String textAfter = extractText(after);
|
||||
if (textBefore == null || textAfter == null || textBefore.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
return textAfter.length() < textBefore.length() * TEXT_RETENTION_FLOOR;
|
||||
}
|
||||
|
||||
/** Extracted text, or null when the document cannot be read - never a partial read. */
|
||||
private static String extractText(PDDocument document) {
|
||||
try {
|
||||
PDFTextStripper stripper = new PDFTextStripper();
|
||||
stripper.setSortByPosition(false);
|
||||
return stripper.getText(document);
|
||||
} catch (IOException | RuntimeException e) {
|
||||
log.debug("Could not extract text while checking the rewrite: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static long contentBytes(PDDocument document) {
|
||||
long total = 0;
|
||||
for (PDPage page : document.getPages()) {
|
||||
|
||||
+8
-8
@@ -3,16 +3,16 @@ package stirling.software.proprietary.storage.converter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import jakarta.persistence.AttributeConverter;
|
||||
import jakarta.persistence.Converter;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import tools.jackson.core.JacksonException;
|
||||
import tools.jackson.core.type.TypeReference;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* JPA AttributeConverter for storing Map<String, Object> as JSON in database columns.
|
||||
*
|
||||
@@ -33,7 +33,7 @@ public class JsonMapConverter implements AttributeConverter<Map<String, Object>,
|
||||
|
||||
try {
|
||||
return objectMapper.writeValueAsString(attribute);
|
||||
} catch (JsonProcessingException e) {
|
||||
} catch (JacksonException e) {
|
||||
log.error("Failed to convert map to JSON", e);
|
||||
throw new RuntimeException("Failed to convert map to JSON", e);
|
||||
}
|
||||
@@ -48,7 +48,7 @@ public class JsonMapConverter implements AttributeConverter<Map<String, Object>,
|
||||
try {
|
||||
// Try normal parsing first
|
||||
return objectMapper.readValue(dbData, new TypeReference<Map<String, Object>>() {});
|
||||
} catch (JsonProcessingException e) {
|
||||
} catch (JacksonException e) {
|
||||
// Fallback: try double-parsing for legacy double-encoded data
|
||||
// This handles data that was stored as JSON strings instead of JSON objects
|
||||
log.debug("Attempting double-decode fallback for legacy metadata format");
|
||||
@@ -69,7 +69,7 @@ public class JsonMapConverter implements AttributeConverter<Map<String, Object>,
|
||||
return objectMapper.readValue(
|
||||
node.asText(), new TypeReference<Map<String, Object>>() {});
|
||||
}
|
||||
} catch (JsonProcessingException e2) {
|
||||
} catch (JacksonException e2) {
|
||||
log.error("Failed to parse metadata even with double-decode fallback", e2);
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package stirling.software.proprietary.storage.model;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -46,7 +47,7 @@ import stirling.software.proprietary.security.model.User;
|
||||
@Setter
|
||||
public class FileShare implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package stirling.software.proprietary.storage.model;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -39,7 +40,7 @@ import stirling.software.proprietary.security.model.User;
|
||||
@Setter
|
||||
public class FileShareAccess implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package stirling.software.proprietary.storage.model;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -24,7 +25,7 @@ import lombok.Setter;
|
||||
@Setter
|
||||
public class StorageCleanupEntry implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package stirling.software.proprietary.storage.model;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashSet;
|
||||
@@ -45,7 +46,7 @@ import stirling.software.proprietary.workflow.model.WorkflowSession;
|
||||
@Setter
|
||||
public class StoredFile implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package stirling.software.proprietary.storage.model;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
@@ -19,7 +20,7 @@ import lombok.Setter;
|
||||
@Setter
|
||||
public class StoredFileBlob implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@Column(name = "storage_key", nullable = false, length = 128)
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.slf4j.MDC;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
@@ -64,7 +65,7 @@ public class AuditWebFilter extends OncePerRequestFilter {
|
||||
if (auth != null && auth.getAuthorities() != null) {
|
||||
String roles =
|
||||
auth.getAuthorities().stream()
|
||||
.map(a -> a.getAuthority())
|
||||
.map(GrantedAuthority::getAuthority)
|
||||
.reduce((a, b) -> a + "," + b)
|
||||
.orElse("");
|
||||
MDC.put("userRoles", roles);
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user