oscerd opened a new pull request, #28972: URL: https://github.com/apache/camel/pull/28972
Fixes [CAMEL-26223](https://issues.apache.org/jira/browse/CAMEL-26223), found while adding the catalog body types (CAMEL-24961). Fixes [CAMEL-26223](https://issues.apache.org/jira/browse/CAMEL-26223). ## 1. The stream of a File body was not closed **Was:** `WatsonSpeechToTextProducer.recognize` first called `getBody(InputStream.class)`. For a `File` body the type converter opened a `FileInputStream`, and the same happened for a `Path` and for the file of a file consumer (`GenericFile`). `ownStream` stayed false, as only the `File` branch behind it set it, and that branch never ran, so the `finally` block did not close the stream: one open file handle per message until garbage collection. The `CamelIBMWatsonSTTAudioFile` header was not affected (CAMEL-24488 closes the stream it opens). **Now:** an `InputStream` body is used as it is and left open, as before: CAMEL-24488 (#25737) made the caller the owner of a stream it gives. Any other body is converted with `getBody(InputStream.class)`, and the producer owns and closes that stream in the `finally` block, also when the call fails. This covers a `File`, a `Path`, the file of a file consumer and bytes. The `getBody(File.class)` fallback is removed: it ran only for a body that does not convert to an `InputStream`, and the bodies that convert to a `File` (a `Path`, a `GenericFile`, a `String`) all convert to an `InputStream` first, so it never ran. The headers are now read before the stream is opened, so a header that fails to convert cannot leave a stream open. ## Tests - New `WatsonSpeechToTextAudioStreamTest` (9 tests): the IBM Watson SDK, with `NoAuthAuthenticator`, against a JDK `HttpServer` that plays Speech to Text and records the audio it receives. It checks that the audio bytes (with non-UTF-8 bytes such as 0xff) arrive unchanged, and that: - a stream the producer gets by conversion is closed, also when the service answers 400 (a body type with a test type converter that returns a stream which records `close()`); - an `InputStream` body is not closed; - on Linux, no file descriptor in `/proc/self/fd` still points to the audio file after a `File` body, a `Path` body, the `GenericFile` of a file consumer, and the `CamelIBMWatsonSTTAudioFile` header; - bytes are sent as they are, and no body fails as before. - With the `src/main` change reverted, 5 of the 9 new tests fail on their assertion: the converted stream is not closed (also on a failed call), and the file of the File, Path and GenericFile bodies is still open. The 4 that pass are the InputStream body left open, the bytes, no body, and the audio file header, which CAMEL-24488 already closes. - Module: 14 unit tests, 0 failures (the ITs need Watson credentials and are skipped). ## Docs and generated files - The component doc has an "Audio input" section: where recognize reads the audio, which streams the producer closes, and that an `InputStream` body is the caller's. - `@BodyTypes` is unchanged: it already lists bytes, a stream and a file. The component JSON does not change; the doc is copied into the catalog. - No upgrade guide entry: only a stream the caller never had is now closed. Main only, as the other fixes of the CAMEL-24961 follow-ups. _Claude Code on behalf of Andrea Cosentino (oscerd)_ 🤖 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]
