File size: 1,144 Bytes
253a5d4
1
Implement demo.App as a Java17 HttpServer on port 8080. Define endpoints: GET /health returns {"status":"ok"}; POST /api/pdf/analyze requires Authorization header 'Bearer demo-pdf'. Enforce method/path checks before auth to return 405/404 appropriately. Parse JSON body strictly for single 'base64' field; reject extra fields or non-canonical Base64 with 400 INVALID_INPUT. Limit request size to 180001 bytes and decoded PDF to 131072 bytes (413 PDF_TOO_LARGE). Validate Content-Type starts with application/json. Use PDFBox 3.0.8 Loader.loadPDF(byte[]) in try-with-resources. Check document.isEncrypted() -> 422 ENCRYPTED_PDF. Verify page count: 0 pages -> 400 INVALID_PDF, >20 pages -> 422 PAGE_LIMIT. Extract text per page using PDFTextStripper (sortByPosition=true), normalize CRLF/CR to LF, strip surrounding whitespace. Count Unicode code points. If all pages empty -> status NO_TEXT, else TEXT_EXTRACTED. Check total UTF-8 text length <= 24000 and serialized response <= 60000 bytes; otherwise 413 TEXT_LIMIT. Return JSON with pageCount, pages array, totalCharacters, and status. Ensure no external dependencies beyond pinned PDFBox jar.