Skip to content

Commit

Permalink
SmbOutboundGateway: Other remote file operations
Browse files Browse the repository at this point in the history
* Enhanced SmbOutboundGateway with supported remote file operations
* Updated SmbOutboundGateway code based on PR review feedback
* Fully implemented listNames() in SmbSession, added more JUnit tests
* Updated SmbSession code based on PR review comments
  • Loading branch information
GregBragg committed May 20, 2022
1 parent 274b272 commit 4203a23
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.springframework.integration.file.remote.MessageSessionCallback;
import org.springframework.integration.file.remote.RemoteFileTemplate;
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.file.support.FileExistsMode;
import org.springframework.integration.smb.outbound.SmbOutboundGateway;
Expand Down Expand Up @@ -126,6 +127,70 @@ public static SmbMessageHandlerSpec outboundAdapter(SmbRemoteFileTemplate smbRem
return new SmbMessageHandlerSpec(smbRemoteFileTemplate, fileExistsMode);
}

/**
* Produce a {@link SmbOutboundGatewaySpec} based on the {@link SessionFactory},
* {@link AbstractRemoteFileOutboundGateway.Command} and {@code expression} for the
* remoteFilePath.
* @param sessionFactory the {@link SessionFactory}.
* @param command the command to perform on the SMB.
* @param expression the remoteFilePath SpEL expression.
* @return the {@link SmbOutboundGatewaySpec}
*/
public static SmbOutboundGatewaySpec outboundGateway(SessionFactory<SmbFile> sessionFactory,
AbstractRemoteFileOutboundGateway.Command command, String expression) {

return outboundGateway(sessionFactory, command.getCommand(), expression);
}

/**
* Produce a {@link SmbOutboundGatewaySpec} based on the {@link SessionFactory},
* {@link AbstractRemoteFileOutboundGateway.Command} and {@code expression} for the
* remoteFilePath.
* @param sessionFactory the {@link SessionFactory}.
* @param command the command to perform on the SMB.
* @param expression the remoteFilePath SpEL expression.
* @return the {@link SmbOutboundGatewaySpec}
* @see RemoteFileTemplate
*/
public static SmbOutboundGatewaySpec outboundGateway(SessionFactory<SmbFile> sessionFactory,
String command, String expression) {

return new SmbOutboundGatewaySpec(new SmbOutboundGateway(sessionFactory, command, expression));
}

/**
* Produce a {@link SmbOutboundGatewaySpec} based on the {@link RemoteFileTemplate},
* {@link AbstractRemoteFileOutboundGateway.Command} and {@code expression} for the
* remoteFilePath.
* @param remoteFileTemplate the {@link RemoteFileTemplate}.
* @param command the command to perform on the SMB.
* @param expression the remoteFilePath SpEL expression.
* @return the {@link SmbOutboundGatewaySpec}
* @see RemoteFileTemplate
*/
public static SmbOutboundGatewaySpec outboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate,
AbstractRemoteFileOutboundGateway.Command command, String expression) {

return outboundGateway(remoteFileTemplate, command.getCommand(), expression);
}

/**
* Produce a {@link SmbOutboundGatewaySpec} based on the {@link RemoteFileTemplate},
* {@link AbstractRemoteFileOutboundGateway.Command} and {@code expression} for the
* remoteFilePath.
* @param remoteFileTemplate the {@link RemoteFileTemplate}.
* @param command the command to perform on the SMB.
* @param expression the remoteFilePath SpEL expression.
* @return the {@link SmbOutboundGatewaySpec}
* @see RemoteFileTemplate
*/
public static SmbOutboundGatewaySpec outboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate,
String command, String expression) {

return new SmbOutboundGatewaySpec(new SmbOutboundGateway(remoteFileTemplate, command, expression));
}


/**
* Produce a {@link SmbOutboundGatewaySpec} based on the
* {@link MessageSessionCallback}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,53 @@ public SmbOutboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate,
super(remoteFileTemplate, messageSessionCallback);
}

/**
* Construct an instance with the supplied session factory, a command ('ls', 'get'
* etc), and an expression to determine the filename.
* @param sessionFactory the session factory.
* @param command the command.
* @param expression the filename expression.
*/
public SmbOutboundGateway(SessionFactory<SmbFile> sessionFactory, String command, String expression) {
this(new SmbRemoteFileTemplate(sessionFactory), command, expression);
remoteFileTemplateExplicitlySet(false);
}

/**
* Construct an instance with the supplied remote file template, a command ('ls',
* 'get' etc), and an expression to determine the filename.
* @param remoteFileTemplate the remote file template.
* @param command the command.
* @param expression the filename expression.
*/
public SmbOutboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate, String command, String expression) {
super(remoteFileTemplate, command, expression);
}

/**
* Construct an instance with the supplied session factory
* and command ('ls', 'nlst', 'put' or 'mput').
* <p> The {@code remoteDirectory} expression is {@code null} assuming to use
* the {@code workingDirectory} from the SMB Client.
* @param sessionFactory the session factory.
* @param command the command.
*/
public SmbOutboundGateway(SessionFactory<SmbFile> sessionFactory, String command) {
this(sessionFactory, command, null);
}

/**
* Construct an instance with the supplied remote file template
* and command ('ls', 'nlst', 'put' or 'mput').
* <p> The {@code remoteDirectory} expression is {@code null} assuming to use
* the {@code workingDirectory} from the SMB Client.
* @param remoteFileTemplate the remote file template.
* @param command the command.
*/
public SmbOutboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate, String command) {
this(remoteFileTemplate, command, null);
}

@Override
public String getComponentType() {
return "smb:outbound-gateway";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public SmbSession(SmbShare _smbShare) {
}

/**
* Deletes the file or directory at the specified path.
* Delete the file or directory at the specified path.
* @param _path path to a remote file or directory
* @return true if delete successful, false if resource is non-existent
* @throws IOException on error conditions returned by a CIFS server
Expand All @@ -123,7 +123,7 @@ else if (logger.isInfoEnabled()) {
}

/**
* Returns the contents of the specified SMB resource as an array of SmbFile objects.
* Return the contents of the specified SMB resource as an array of SmbFile objects.
* In case the remote resource does not exist, an empty array is returned.
* @param _path path to a remote directory
* @return array of SmbFile objects
Expand Down Expand Up @@ -162,7 +162,7 @@ else if (logger.isInfoEnabled()) {
}

/**
* Reads the remote resource specified by path and copies its contents to the specified
* Read the remote resource specified by path and copies its contents to the specified
* {@link OutputStream}.
* @param _path path to a remote file
* @param _outputStream output stream
Expand Down Expand Up @@ -190,7 +190,7 @@ public void read(String _path, OutputStream _outputStream) throws IOException {
}

/**
* Writes contents of the specified {@link InputStream} to the remote resource
* Write contents of the specified {@link InputStream} to the remote resource
* specified by path. Remote directories are created implicitly as required.
* @param _inputStream input stream
* @param _path remote path (of a file) to write to
Expand Down Expand Up @@ -237,7 +237,7 @@ public SmbFile write(byte[] _contents, String _path) throws IOException {
}

/**
* Creates the specified remote path if not yet exists.
* Create the specified remote path if not yet exists.
* If the specified resource is a file rather than a path, creates all directories leading
* to that file.
* @param _path remote path to create
Expand Down Expand Up @@ -268,7 +268,7 @@ public boolean mkdir(String _path) throws IOException {
}

/**
* Checks whether the remote resource exists.
* Check whether the remote resource exists.
* @param _path remote path
* @return true if exists, false otherwise
* @throws IOException on error conditions returned by a CIFS server
Expand All @@ -279,7 +279,7 @@ public boolean exists(String _path) throws IOException {
}

/**
* Checks whether the remote resource is a file.
* Check whether the remote resource is a file.
* @param _path remote path
* @return true if resource is a file, false otherwise
* @throws IOException on error conditions returned by a CIFS server
Expand All @@ -290,7 +290,7 @@ public boolean isFile(String _path) throws IOException {
}

/**
* Checks whether the remote resource is a directory.
* Check whether the remote resource is a directory.
* @param _path remote path
* @return true if resource is a directory, false otherwise
* @throws IOException on error conditions returned by a CIFS server
Expand Down Expand Up @@ -387,8 +387,8 @@ public void close() {
}

/**
* Checks with this SMB session is open and ready for work by attempting
* to list remote files and checking for error conditions..
* Check whether this SMB session is open and ready for work by attempting
* to list remote files and checking for error conditions.
* @return true if the session is open, false otherwise
*/
@Override
Expand Down Expand Up @@ -455,7 +455,7 @@ private SmbFile createSmbFileObject(String path, Boolean isDirectory) throws IOE
}

/**
* Creates an SMB file object pointing to a remote file.
* Create an SMB file object pointing to a remote file.
* @param _path the remote file path
* @return the {@link SmbFile} for remote path
* @throws IOException the IO exception
Expand All @@ -465,7 +465,7 @@ public SmbFile createSmbFileObject(String _path) throws IOException {
}

/**
* Creates an SMB file object pointing to a remote directory.
* Create an SMB file object pointing to a remote directory.
* @param _path the remote directory path
* @return the {@link SmbFile} for remote path
* @throws IOException the IO exception
Expand All @@ -480,9 +480,43 @@ public String getHostPort() {
return url.getHost() + ":" + url.getPort();
}

/**
* Return the contents of the specified SMB resource as an array of SmbFile filenames.
* In case the remote resource does not exist, an empty array is returned.
* @param _path path to a remote directory
* @return array of SmbFile filenames
* @throws IOException on error conditions returned by a CIFS server or if the remote resource is not a directory.
*/
@Override
public String[] listNames(String path) {
throw new UnsupportedOperationException("Not implemented yet");
public String[] listNames(String _path) throws IOException {
String[] fileNames = new String[0];
try {
SmbFile smbDir = createSmbDirectoryObject(_path);
if (!smbDir.exists()) {
if (logger.isWarnEnabled()) {
logger.warn("Remote directory [" + _path + "] does not exist. Cannot list resources.");
}
return fileNames;
}
else if (!smbDir.isDirectory()) {
throw new IOException("Resource [" + _path + "] is not a directory. Cannot list resources.");
}

fileNames = smbDir.list();
}
catch (SmbException _ex) {
throw new IOException("Failed to list resources in [" + _path + "].", _ex);
}

if (logger.isDebugEnabled()) {
logger.debug("Successfully listed " + fileNames.length + " resource(s) in [" + _path + "]"
+ ": " + Arrays.toString(fileNames));
}
else if (logger.isInfoEnabled()) {
logger.info("Successfully listed " + fileNames.length + " resource(s) in [" + _path + "]" + ".");
}

return fileNames;
}

}
Loading

0 comments on commit 4203a23

Please sign in to comment.