This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch smb2 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 41d773c378db2cf153001154c3e995bf4511e4b8 Author: Claus Ibsen <[email protected]> AuthorDate: Sun Jul 13 15:41:39 2025 +0200 CAMEL-22235: camel-smb - SmbOperations may swallow exception and can cause NPE with camel-file --- .../apache/camel/component/smb/SmbOperations.java | 63 +++++++++------------- 1 file changed, 26 insertions(+), 37 deletions(-) diff --git a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java index 6f9839a6caf..9d466d8a92c 100644 --- a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java +++ b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java @@ -168,7 +168,6 @@ public class SmbOperations implements SmbFileOperations { try (File f = share.openFile(name, EnumSet.of(AccessMask.GENERIC_ALL), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null)) { - f.deleteOnClose(); } } @@ -199,10 +198,8 @@ public class SmbOperations implements SmbFileOperations { src.deleteOnClose(); } catch (SMBRuntimeException smbre) { - if (smbre.getCause() instanceof TransportException) { - disconnect(); - throw smbre; - } + safeDisconnect(smbre); + throw smbre; } return true; } @@ -211,14 +208,11 @@ public class SmbOperations implements SmbFileOperations { public boolean buildDirectory(String directory, boolean absolute) throws GenericFileOperationFailedException { connectIfNecessary(); SmbFiles files = new SmbFiles(); - try { files.mkdirs(share, normalize(directory)); } catch (SMBRuntimeException smbre) { - if (smbre.getCause() instanceof TransportException) { - disconnect(); - throw smbre; - } + safeDisconnect(smbre); + throw smbre; } return true; } @@ -237,16 +231,12 @@ public class SmbOperations implements SmbFileOperations { public boolean existsFolder(String name) { connectIfNecessary(); - boolean result = false; try { - result = share.folderExists(name); + return share.folderExists(name); } catch (SMBRuntimeException smbre) { - if (smbre.getCause() instanceof TransportException) { - disconnect(); - throw smbre; - } + safeDisconnect(smbre); + throw smbre; } - return result; } private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException { @@ -274,10 +264,8 @@ public class SmbOperations implements SmbFileOperations { exchange.getIn().setHeader(SmbConstants.SMB_UNC_PATH, shareFile.getUncPath()); } catch (SMBRuntimeException smbre) { - if (smbre.getCause() instanceof TransportException) { - disconnect(); - throw smbre; - } + safeDisconnect(smbre); + throw smbre; } return true; } @@ -526,16 +514,12 @@ public class SmbOperations implements SmbFileOperations { public FileIdBothDirectoryInformation[] listFiles(String path, String searchPattern) throws GenericFileOperationFailedException { connectIfNecessary(); - FileIdBothDirectoryInformation[] result = null; try { - result = share.list(path, searchPattern).toArray(FileIdBothDirectoryInformation[]::new); + return share.list(path, searchPattern).toArray(FileIdBothDirectoryInformation[]::new); } catch (SMBRuntimeException smbre) { - if (smbre.getCause() instanceof TransportException) { - disconnect(); - throw smbre; - } + safeDisconnect(smbre); + throw smbre; } - return result; } public byte[] getBody(String path) { @@ -549,17 +533,14 @@ public class SmbOperations implements SmbFileOperations { throw new GenericFileOperationFailedException(e.getMessage(), e); } } catch (SMBRuntimeException smbre) { - if (smbre.getCause() instanceof TransportException) { - disconnect(); - throw smbre; - } + safeDisconnect(smbre); + throw smbre; } - return null; } public InputStream getBodyAsInputStream(Exchange exchange, String path) { connectIfNecessary(); - InputStream is = null; + InputStream is; try { File shareFile = share.openFile(path, EnumSet.of(AccessMask.GENERIC_READ), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null); @@ -567,12 +548,20 @@ public class SmbOperations implements SmbFileOperations { exchange.getIn().setHeader(SmbComponent.SMB_FILE_INPUT_STREAM, is); exchange.getIn().setHeader(SmbConstants.SMB_UNC_PATH, shareFile.getUncPath()); } catch (SMBRuntimeException smbre) { - if (smbre.getCause() instanceof TransportException) { + safeDisconnect(smbre); + throw smbre; + } + return is; + } + + private void safeDisconnect(SMBRuntimeException smbre) { + if (smbre.getCause() instanceof TransportException) { + try { disconnect(); - throw smbre; + } catch (Exception e) { + // ignore } } - return is; } /*
