davsclaus opened a new pull request, #30886: URL: https://github.com/apache/camel/pull/30886
CAMEL-18685: camel-file - add charsetErrorAction to replace or drop characters the charset cannot read or write JIRA: https://issues.apache.org/jira/browse/CAMEL-18685 Based on #24000 by @rpamu (closed after CI failures), redone on current main. rpamu is credited as co-author of the commit. ## Problem The file component fails when the content of a file does not fit its charset, and there is no way to change that: - the consumer fails with `MalformedInputException` when a file is read as text with a charset its bytes are not valid in (an ISO-8859-1 file read as UTF-8), and with `UnmappableCharacterException` for a byte the charset does not map (`0x81` in windows-1252), which is what the ticket reports; - the producer with `charset` fails with `UnmappableCharacterException` for a character the charset cannot encode (`€` written as ISO-8859-1). ## Change - New endpoint option `charsetErrorAction` on the `file` component only (`FileEndpoint`, not `GenericFileEndpoint`: ftp, sftp and smb replace these characters already, so the option would do nothing there). It is an enum, `REPORT` (default, the behaviour of today), `REPLACE` and `IGNORE`, mapped to `java.nio.charset.CodingErrorAction` for both malformed input and unmappable characters. An unknown value fails when the endpoint is created. - Consumer: the setting travels on the `GenericFile` (as `charset` does), so `GenericFileConverter` builds the decoder with it, for a `Reader` or `String` (with `charset`, or the exchange or default charset without it) and an `InputStream` (with `charset`, still an `IOHelper.EncodingInputStream`, which `XmlConverter` looks for). `GenericFile.copyFrom` copies it. `FileConsumer.asGenericFile` keeps its signature (Zip/Tar aggregation and tests use it): the consumer sets the action on the file it creates. - Producer: `FileOperations.writeFileByReaderWithCharset` sets the action on the existing encoder, so the buffering and the checksum digest stay as they are. - camel-util: two small additions, `IOHelper.toReader(Path, CharsetDecoder)` and an `EncodingInputStream(Path, CharsetDecoder)` constructor. The String/enum mapping is in camel-file. - Docs: a subsection in the charset section of `file-component.adoc` with the values, when the option applies, a warning that `REPLACE` and `IGNORE` change the data, and Java/XML/YAML examples. Regenerated `file.json`, the configurer, the UriFactory, the endpoint DSL, and the catalog copies. Compared to #24000: the option is on `FileEndpoint` only, it is an enum (no silent fallback to REPORT for a typo, no `toUpperCase()` without a Locale), it has a broader name (it covers malformed input, the common case, not only unmappable characters), `asGenericFile` keeps its signature, `copyFrom` copies it, the producer keeps the buffered writer and the digest, and it has tests. Not covered: a consumer with `preMove`. The rename strategy binds the moved file to the exchange again, and the message body is then a plain `java.io.File`, which the core `IOConverter` reads (with the charset from the exchange property, which is why `charset` itself works there). The docs say so. ## Tests - `FileConsumeCharsetErrorActionTest` (camel-core): windows-1252 file with `0x81` and an ISO-8859-1 file read as UTF-8, as `String`: REPORT fails with the exception (as today, and as the default), REPLACE gives `U+FFFD`, IGNORE drops it; IGNORE as an `InputStream` (still an `EncodingInputStream`); `copyFrom` keeps the action; the default value; an invalid value is rejected. - `FileProducerCharsetErrorActionTest` (camel-core): `"a€b"` with `charset=iso-8859-1`: REPORT and the default fail with `UnmappableCharacterException`, REPLACE writes `a?b`, IGNORE writes `ab`, also with `checksumFileAlgorithm` (the checksum is of the bytes written). - Fails on main: 12 of the 14 tests that only use the URI option fail with `ResolveEndpointFailedException` (unknown option `charsetErrorAction`); the two `testDefaultIsReport` tests pass, which shows the failure of today. - With the change: the 16 new tests pass; camel-core `org.apache.camel.component.file.**` and `org.apache.camel.converter.**`: 921 tests, 0 failures (13 skipped); camel-file: 54 tests; camel-util `IOHelper*`/`UtilEdgeCases*`: 27 tests. ## Upgrade notes None: the default `REPORT` keeps the behaviour of today. _Claude Code on behalf of davsclaus_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
