Skip to content

Commit 4203a23

Browse files
authored
SmbOutboundGateway: Other remote file operations
* 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
1 parent 274b272 commit 4203a23

File tree

4 files changed

+263
-14
lines changed

4 files changed

+263
-14
lines changed

spring-integration-smb/src/main/java/org/springframework/integration/smb/dsl/Smb.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.integration.file.remote.MessageSessionCallback;
2323
import org.springframework.integration.file.remote.RemoteFileTemplate;
24+
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway;
2425
import org.springframework.integration.file.remote.session.SessionFactory;
2526
import org.springframework.integration.file.support.FileExistsMode;
2627
import org.springframework.integration.smb.outbound.SmbOutboundGateway;
@@ -126,6 +127,70 @@ public static SmbMessageHandlerSpec outboundAdapter(SmbRemoteFileTemplate smbRem
126127
return new SmbMessageHandlerSpec(smbRemoteFileTemplate, fileExistsMode);
127128
}
128129

130+
/**
131+
* Produce a {@link SmbOutboundGatewaySpec} based on the {@link SessionFactory},
132+
* {@link AbstractRemoteFileOutboundGateway.Command} and {@code expression} for the
133+
* remoteFilePath.
134+
* @param sessionFactory the {@link SessionFactory}.
135+
* @param command the command to perform on the SMB.
136+
* @param expression the remoteFilePath SpEL expression.
137+
* @return the {@link SmbOutboundGatewaySpec}
138+
*/
139+
public static SmbOutboundGatewaySpec outboundGateway(SessionFactory<SmbFile> sessionFactory,
140+
AbstractRemoteFileOutboundGateway.Command command, String expression) {
141+
142+
return outboundGateway(sessionFactory, command.getCommand(), expression);
143+
}
144+
145+
/**
146+
* Produce a {@link SmbOutboundGatewaySpec} based on the {@link SessionFactory},
147+
* {@link AbstractRemoteFileOutboundGateway.Command} and {@code expression} for the
148+
* remoteFilePath.
149+
* @param sessionFactory the {@link SessionFactory}.
150+
* @param command the command to perform on the SMB.
151+
* @param expression the remoteFilePath SpEL expression.
152+
* @return the {@link SmbOutboundGatewaySpec}
153+
* @see RemoteFileTemplate
154+
*/
155+
public static SmbOutboundGatewaySpec outboundGateway(SessionFactory<SmbFile> sessionFactory,
156+
String command, String expression) {
157+
158+
return new SmbOutboundGatewaySpec(new SmbOutboundGateway(sessionFactory, command, expression));
159+
}
160+
161+
/**
162+
* Produce a {@link SmbOutboundGatewaySpec} based on the {@link RemoteFileTemplate},
163+
* {@link AbstractRemoteFileOutboundGateway.Command} and {@code expression} for the
164+
* remoteFilePath.
165+
* @param remoteFileTemplate the {@link RemoteFileTemplate}.
166+
* @param command the command to perform on the SMB.
167+
* @param expression the remoteFilePath SpEL expression.
168+
* @return the {@link SmbOutboundGatewaySpec}
169+
* @see RemoteFileTemplate
170+
*/
171+
public static SmbOutboundGatewaySpec outboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate,
172+
AbstractRemoteFileOutboundGateway.Command command, String expression) {
173+
174+
return outboundGateway(remoteFileTemplate, command.getCommand(), expression);
175+
}
176+
177+
/**
178+
* Produce a {@link SmbOutboundGatewaySpec} based on the {@link RemoteFileTemplate},
179+
* {@link AbstractRemoteFileOutboundGateway.Command} and {@code expression} for the
180+
* remoteFilePath.
181+
* @param remoteFileTemplate the {@link RemoteFileTemplate}.
182+
* @param command the command to perform on the SMB.
183+
* @param expression the remoteFilePath SpEL expression.
184+
* @return the {@link SmbOutboundGatewaySpec}
185+
* @see RemoteFileTemplate
186+
*/
187+
public static SmbOutboundGatewaySpec outboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate,
188+
String command, String expression) {
189+
190+
return new SmbOutboundGatewaySpec(new SmbOutboundGateway(remoteFileTemplate, command, expression));
191+
}
192+
193+
129194
/**
130195
* Produce a {@link SmbOutboundGatewaySpec} based on the
131196
* {@link MessageSessionCallback}.

spring-integration-smb/src/main/java/org/springframework/integration/smb/outbound/SmbOutboundGateway.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,53 @@ public SmbOutboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate,
7272
super(remoteFileTemplate, messageSessionCallback);
7373
}
7474

75+
/**
76+
* Construct an instance with the supplied session factory, a command ('ls', 'get'
77+
* etc), and an expression to determine the filename.
78+
* @param sessionFactory the session factory.
79+
* @param command the command.
80+
* @param expression the filename expression.
81+
*/
82+
public SmbOutboundGateway(SessionFactory<SmbFile> sessionFactory, String command, String expression) {
83+
this(new SmbRemoteFileTemplate(sessionFactory), command, expression);
84+
remoteFileTemplateExplicitlySet(false);
85+
}
86+
87+
/**
88+
* Construct an instance with the supplied remote file template, a command ('ls',
89+
* 'get' etc), and an expression to determine the filename.
90+
* @param remoteFileTemplate the remote file template.
91+
* @param command the command.
92+
* @param expression the filename expression.
93+
*/
94+
public SmbOutboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate, String command, String expression) {
95+
super(remoteFileTemplate, command, expression);
96+
}
97+
98+
/**
99+
* Construct an instance with the supplied session factory
100+
* and command ('ls', 'nlst', 'put' or 'mput').
101+
* <p> The {@code remoteDirectory} expression is {@code null} assuming to use
102+
* the {@code workingDirectory} from the SMB Client.
103+
* @param sessionFactory the session factory.
104+
* @param command the command.
105+
*/
106+
public SmbOutboundGateway(SessionFactory<SmbFile> sessionFactory, String command) {
107+
this(sessionFactory, command, null);
108+
}
109+
110+
/**
111+
* Construct an instance with the supplied remote file template
112+
* and command ('ls', 'nlst', 'put' or 'mput').
113+
* <p> The {@code remoteDirectory} expression is {@code null} assuming to use
114+
* the {@code workingDirectory} from the SMB Client.
115+
* @param remoteFileTemplate the remote file template.
116+
* @param command the command.
117+
*/
118+
public SmbOutboundGateway(RemoteFileTemplate<SmbFile> remoteFileTemplate, String command) {
119+
this(remoteFileTemplate, command, null);
120+
}
121+
75122
@Override
76123
public String getComponentType() {
77124
return "smb:outbound-gateway";

spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public SmbSession(SmbShare _smbShare) {
9797
}
9898

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

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

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

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

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

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

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

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

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

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

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

483+
/**
484+
* Return the contents of the specified SMB resource as an array of SmbFile filenames.
485+
* In case the remote resource does not exist, an empty array is returned.
486+
* @param _path path to a remote directory
487+
* @return array of SmbFile filenames
488+
* @throws IOException on error conditions returned by a CIFS server or if the remote resource is not a directory.
489+
*/
483490
@Override
484-
public String[] listNames(String path) {
485-
throw new UnsupportedOperationException("Not implemented yet");
491+
public String[] listNames(String _path) throws IOException {
492+
String[] fileNames = new String[0];
493+
try {
494+
SmbFile smbDir = createSmbDirectoryObject(_path);
495+
if (!smbDir.exists()) {
496+
if (logger.isWarnEnabled()) {
497+
logger.warn("Remote directory [" + _path + "] does not exist. Cannot list resources.");
498+
}
499+
return fileNames;
500+
}
501+
else if (!smbDir.isDirectory()) {
502+
throw new IOException("Resource [" + _path + "] is not a directory. Cannot list resources.");
503+
}
504+
505+
fileNames = smbDir.list();
506+
}
507+
catch (SmbException _ex) {
508+
throw new IOException("Failed to list resources in [" + _path + "].", _ex);
509+
}
510+
511+
if (logger.isDebugEnabled()) {
512+
logger.debug("Successfully listed " + fileNames.length + " resource(s) in [" + _path + "]"
513+
+ ": " + Arrays.toString(fileNames));
514+
}
515+
else if (logger.isInfoEnabled()) {
516+
logger.info("Successfully listed " + fileNames.length + " resource(s) in [" + _path + "]" + ".");
517+
}
518+
519+
return fileNames;
486520
}
487521

488522
}

0 commit comments

Comments
 (0)