shashank created CAMEL-25524:
--------------------------------

             Summary: camel-mina-sftp - the producer writes a file in the 
charset of the exchange instead of the charset option, and fails for a null 
body with allowNullBody=true
                 Key: CAMEL-25524
                 URL: https://issues.apache.org/jira/browse/CAMEL-25524
             Project: Camel
          Issue Type: Bug
          Components: camel-ftp
            Reporter: shashank


{{MinaSftpOperations.doStoreFile}} (lines 1306-1373 at main 586855a18315) 
writes the body as {{exchange.getIn().getMandatoryBody(InputStream.class)}}. 
The {{charset}} option of the endpoint ("This option is used to specify the 
encoding of the file") is not used, so a String body is encoded in the charset 
of the exchange ({{CamelCharsetName}}, UTF-8 by default). And a null body fails 
with {{InvalidPayloadException: No body available of type: 
java.io.InputStream}} although {{allowNullBody=true}}. camel-sftp, camel-ftp 
and camel-smb do both in their {{storeFile}}:

{code:java}
if (exchange.getIn().getBody() == null) {
    if (endpoint.isAllowNullBody()) { is = new ByteArrayInputStream(new byte[] 
{}); } else { throw ... }
}
...
String charset = endpoint.getCharset();
if (charset != null) { is = GenericFileHelper.toInputStream(exchange, charset); 
} else { is = getMandatoryBody(InputStream.class); }
{code}

The existing {{SftpProducerWithCharsetIT}} of camel-mina-sftp passes only 
because it sends with a {{ProducerTemplate}} directly to the endpoint: 
{{DefaultEndpoint.createExchange}} then sets the endpoint charset on the 
exchange. In a route 
({{from("file:in").to("mina-sftp:...?charset=iso-8859-1")}}) the exchange comes 
from another endpoint, and every non-ASCII character of the file is written in 
UTF-8, without any error.

h3. Reproduction

New test {{SftpProducerWithCharsetIT.testProducerWithCharsetInRoute}} (embedded 
SFTP server as in the other tests): {{"æøå ©"}} sent through {{direct:start}} 
to the endpoint with {{charset=iso-8859-1}}; new 
{{SftpProducerAllowNullBodyIT}} (as {{FtpProducerAllowNullBodyIT}} of 
camel-ftp). On main, twice:
{noformat}
SftpProducerWithCharsetIT.testProducerWithCharsetInRoute: The file must be 
written in the charset of the endpoint ==> expected: <æøå ©> but was: <æøå 
©>
SftpProducerAllowNullBodyIT.testAllowNullBodyTrue: CamelExecutionException ... 
Caused by: InvalidPayloadException: No body available of type: 
java.io.InputStream
{noformat}

h3. Proposed fix

{{doStoreFile}} gets the input stream as camel-sftp does: an empty file for a 
null body with {{allowNullBody=true}} (and the same "Cannot write null body to 
file" failure as camel-sftp when it is false), 
{{GenericFileHelper.toInputStream(exchange, charset)}} (streaming, CAMEL-25167) 
when the {{charset}} option is set, the body as before otherwise. Files written 
without a {{charset}} option, or from exchanges created by the endpoint, are 
unchanged. 4.23 upgrade guide note added. camel-mina-sftp with the fix: 26 unit 
tests (1 skipped) and 270 integration tests with the embedded SFTP server (7 
skipped), 0 failures; one {{SftpBulkRequestsIT}} test timed out once waiting 
for the embedded server to start and passed on the rerun (7 of 7).

Found with a Lean 4 model of the store (body as code points or null, UTF-8 and 
ISO-8859-1, the endpoint options and the exchange charset): "the file is the 
body in the endpoint charset, an empty file for a null body with allowNullBody" 
(camel-sftp) fails for "æ" ({{C3 A6}} instead of {{E6}}) and for a null body, 
and main is proved to write every character from U+0080 to U+00FF wrong when 
the two charsets differ. The fix is proved to equal camel-sftp's behaviour for 
every body and configuration, and to write the same bytes as main when no 
charset option is set or it is the exchange charset. Confirmed with the real 
producer.

Not changed here: camel-azure-files has the same omission 
({{FilesOperations.storeFile}} ignores {{charset}}); it needs its own change 
because it computes the length of the upload.

Affected: main, camel-4.22.x, camel-4.18.x (same code, GitHub contents API); 
the component was added in 4.18.

Duplicate check (2026-10-10): JIRA text "mina-sftp" (19, none about charset or 
allowNullBody; CAMEL-25167 changed the charset conversion of ftp, sftp and smb 
only); GitHub pull requests "mina-sftp charset" (none), "MinaSftpOperations" 
(4, thread leak and path containment). No open pull request changes 
{{MinaSftpOperations}}.

_Filed with Claude Code on behalf of allthingssecurity._




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to