Skip to content

Commit 0750867

Browse files
artembilangaryrussell
authored andcommitted
GH-3038: Delegate more RemoteFileTempalte options
Fixes #3038 * Add more delegating setters into the `AbstractRemoteFileOutboundGateway` for its `RemoteFileTemplate` property * Restrict externally provided `RemoteFileTemplate` from modifications in the `AbstractRemoteFileOutboundGateway`: those options must be configured on that external `RemoteFileTemplate` * Expose new options in the Java DSL specs
1 parent 1b892fc commit 0750867

File tree

6 files changed

+327
-30
lines changed

6 files changed

+327
-30
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/dsl/RemoteFileOutboundGatewaySpec.java

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.integration.dsl.ComponentsRegistration;
2626
import org.springframework.integration.dsl.MessageHandlerSpec;
2727
import org.springframework.integration.expression.FunctionExpression;
28+
import org.springframework.integration.file.FileNameGenerator;
2829
import org.springframework.integration.file.filters.CompositeFileListFilter;
2930
import org.springframework.integration.file.filters.ExpressionFileListFilter;
3031
import org.springframework.integration.file.filters.FileListFilter;
@@ -313,8 +314,21 @@ public S localFilenameExpression(String localFilenameExpression) {
313314
* @param localFilenameFunction the {@link Function} to use.
314315
* @param <P> the expected payload type.
315316
* @return the Spec.
317+
* @deprecated since 5.2 in favor of {@link #localFilenameFunction(Function)}
316318
*/
319+
@Deprecated
317320
public <P> S localFilename(Function<Message<P>, String> localFilenameFunction) {
321+
return localFilenameFunction(localFilenameFunction);
322+
}
323+
324+
/**
325+
* Specify a {@link Function} for local files renaming after downloading.
326+
* @param localFilenameFunction the {@link Function} to use.
327+
* @param <P> the expected payload type.
328+
* @return the Spec.
329+
* @since 5.2
330+
*/
331+
public <P> S localFilenameFunction(Function<Message<P>, String> localFilenameFunction) {
318332
return localFilenameExpression(new FunctionExpression<>(localFilenameFunction));
319333
}
320334

@@ -351,6 +365,167 @@ public S fileExistsMode(FileExistsMode fileExistsMode) {
351365
return _this();
352366
}
353367

368+
/**
369+
* Determine whether the remote directory should automatically be created when
370+
* sending files to the remote system.
371+
* @param autoCreateDirectory true to create the directory.
372+
* @return the current Spec
373+
* @since 5.2
374+
* @see AbstractRemoteFileOutboundGateway#setAutoCreateDirectory(boolean)
375+
*/
376+
public S autoCreateDirectory(boolean autoCreateDirectory) {
377+
this.target.setAutoCreateDirectory(autoCreateDirectory);
378+
return _this();
379+
}
380+
381+
/**
382+
* Set the remote directory expression used to determine the remote directory to which
383+
* files will be sent.
384+
* @param remoteDirectoryExpression the remote directory expression.
385+
* @return the current Spec
386+
* @since 5.2
387+
* @see AbstractRemoteFileOutboundGateway#setRemoteDirectoryExpression
388+
*/
389+
public S remoteDirectoryExpression(String remoteDirectoryExpression) {
390+
return remoteDirectoryExpression(PARSER.parseExpression(remoteDirectoryExpression));
391+
}
392+
393+
/**
394+
* Specify a {@link Function} for remote directory.
395+
* @param remoteDirectoryFunction the {@link Function} to use.
396+
* @param <P> the expected payload type.
397+
* @return the Spec.
398+
* @since 5.2
399+
* @see AbstractRemoteFileOutboundGateway#setRemoteDirectoryExpression
400+
* @see FunctionExpression
401+
*/
402+
public <P> S remoteDirectoryFunction(Function<Message<P>, String> remoteDirectoryFunction) {
403+
return remoteDirectoryExpression(new FunctionExpression<>(remoteDirectoryFunction));
404+
}
405+
406+
/**
407+
* Set the remote directory expression used to determine the remote directory to which
408+
* files will be sent.
409+
* @param remoteDirectoryExpression the remote directory expression.
410+
* @return the current Spec
411+
* @since 5.2
412+
* @see AbstractRemoteFileOutboundGateway#setRemoteDirectoryExpression
413+
*/
414+
public S remoteDirectoryExpression(Expression remoteDirectoryExpression) {
415+
this.target.setRemoteDirectoryExpression(remoteDirectoryExpression);
416+
return _this();
417+
}
418+
419+
/**
420+
* Set a temporary remote directory expression; used when transferring files to the remote
421+
* system.
422+
* @param temporaryRemoteDirectoryExpression the temporary remote directory expression.
423+
* @return the current Spec
424+
* @since 5.2
425+
* @see AbstractRemoteFileOutboundGateway#setRemoteDirectoryExpression
426+
*/
427+
public S temporaryRemoteDirectoryExpression(String temporaryRemoteDirectoryExpression) {
428+
return temporaryRemoteDirectoryExpression(PARSER.parseExpression(temporaryRemoteDirectoryExpression));
429+
}
430+
431+
/**
432+
* Set a temporary remote directory function; used when transferring files to the remote
433+
* system.
434+
* @param temporaryRemoteDirectoryFunction the file name expression.
435+
* @param <P> the expected payload type.
436+
* @return the current Spec
437+
* @since 5.2
438+
* @see AbstractRemoteFileOutboundGateway#setRemoteDirectoryExpression
439+
*/
440+
public <P> S temporaryRemoteDirectoryFunction(Function<Message<P>, String> temporaryRemoteDirectoryFunction) {
441+
return temporaryRemoteDirectoryExpression(new FunctionExpression<>(temporaryRemoteDirectoryFunction));
442+
}
443+
444+
/**
445+
* Set a temporary remote directory expression; used when transferring files to the remote
446+
* system.
447+
* @param temporaryRemoteDirectoryExpression the temporary remote directory expression.
448+
* @return the current Spec
449+
* @since 5.2
450+
* @see AbstractRemoteFileOutboundGateway#setRemoteDirectoryExpression
451+
*/
452+
public S temporaryRemoteDirectoryExpression(Expression temporaryRemoteDirectoryExpression) {
453+
this.target.setTemporaryRemoteDirectoryExpression(temporaryRemoteDirectoryExpression);
454+
return _this();
455+
}
456+
457+
/**
458+
* Set the file name expression to determine the full path to the remote file.
459+
* @param fileNameExpression the file name expression.
460+
* @return the current Spec
461+
* @since 5.2
462+
* @see AbstractRemoteFileOutboundGateway#setFileNameExpression
463+
*/
464+
public S fileNameExpression(String fileNameExpression) {
465+
return fileNameExpression(PARSER.parseExpression(fileNameExpression));
466+
}
467+
468+
/**
469+
* Set the file name function to determine the full path to the remote file.
470+
* @param fileNameFunction the file name expression.
471+
* @param <P> the expected payload type.
472+
* @return the current Spec
473+
* @since 5.2
474+
* @see AbstractRemoteFileOutboundGateway#setFileNameExpression
475+
*/
476+
public <P> S fileNameFunction(Function<Message<P>, String> fileNameFunction) {
477+
return fileNameExpression(new FunctionExpression<>(fileNameFunction));
478+
}
479+
480+
/**
481+
* Set the file name expression to determine the full path to the remote file.
482+
* @param fileNameExpression the file name expression.
483+
* @return the current Spec
484+
* @since 5.2
485+
* @see AbstractRemoteFileOutboundGateway#setFileNameExpression
486+
*/
487+
public S fileNameExpression(Expression fileNameExpression) {
488+
this.target.setFileNameExpression(fileNameExpression);
489+
return _this();
490+
}
491+
492+
/**
493+
* Set whether a temporary file name is used when sending files to the remote system.
494+
* @param useTemporaryFileName true to use a temporary file name.
495+
* @return the current Spec
496+
* @since 5.2
497+
* @see AbstractRemoteFileOutboundGateway#setUseTemporaryFileName
498+
*/
499+
public S useTemporaryFileName(boolean useTemporaryFileName) {
500+
this.target.setUseTemporaryFileName(useTemporaryFileName);
501+
return _this();
502+
}
503+
504+
/**
505+
* Set the file name generator used to generate the remote filename to be used when transferring
506+
* files to the remote system.
507+
* @param fileNameGenerator the file name generator.
508+
* @return the current Spec
509+
* @since 5.2
510+
* @see AbstractRemoteFileOutboundGateway#setFileNameGenerator
511+
*/
512+
public S fileNameGenerator(FileNameGenerator fileNameGenerator) {
513+
this.target.setFileNameGenerator(fileNameGenerator);
514+
return _this();
515+
}
516+
517+
/**
518+
* Set the charset to use when converting String payloads to bytes as the content of the
519+
* remote file. Default {@code UTF-8}.
520+
* @param charset the charset.
521+
* @return the current Spec
522+
* @since 5.2
523+
* @see AbstractRemoteFileOutboundGateway#setCharset
524+
*/
525+
public S charset(String charset) {
526+
this.target.setCharset(charset);
527+
return _this();
528+
}
354529

355530
@Override
356531
public Map<Object, String> getComponentsToRegister() {

spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.integration.expression.FunctionExpression;
4242
import org.springframework.integration.expression.ValueExpression;
4343
import org.springframework.integration.file.FileHeaders;
44+
import org.springframework.integration.file.FileNameGenerator;
4445
import org.springframework.integration.file.filters.FileListFilter;
4546
import org.springframework.integration.file.remote.AbstractFileInfo;
4647
import org.springframework.integration.file.remote.MessageSessionCallback;
@@ -108,6 +109,8 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
108109

109110
private Integer chmod;
110111

112+
private boolean remoteFileTemplateExplicitlySet;
113+
111114
/**
112115
* Construct an instance using the provided session factory and callback for
113116
* performing operations on the session.
@@ -135,6 +138,7 @@ public AbstractRemoteFileOutboundGateway(RemoteFileTemplate<F> remoteFileTemplat
135138
this.messageSessionCallback = messageSessionCallback;
136139
this.fileNameProcessor = null;
137140
this.command = null;
141+
remoteFileTemplateExplicitlySet(true);
138142
}
139143

140144
/**
@@ -204,6 +208,16 @@ public AbstractRemoteFileOutboundGateway(RemoteFileTemplate<F> remoteFileTemplat
204208
setPrimaryExpression(parsedExpression);
205209
}
206210
this.messageSessionCallback = null;
211+
remoteFileTemplateExplicitlySet(true);
212+
}
213+
214+
protected final void remoteFileTemplateExplicitlySet(boolean remoteFileTemplateExplicitlySet) {
215+
this.remoteFileTemplateExplicitlySet = remoteFileTemplateExplicitlySet;
216+
}
217+
218+
protected void assertRemoteFileTemplateMutability(String propertyName) {
219+
Assert.state(!this.remoteFileTemplateExplicitlySet,
220+
() -> "The '" + propertyName + "' must be set on the externally provided: " + this.remoteFileTemplate);
207221
}
208222

209223
/**
@@ -242,6 +256,7 @@ public void setOption(Option... options) {
242256
* @see RemoteFileTemplate#setRemoteFileSeparator(String)
243257
*/
244258
public void setRemoteFileSeparator(String remoteFileSeparator) {
259+
assertRemoteFileTemplateMutability("remoteFileSeparator");
245260
this.remoteFileTemplate.setRemoteFileSeparator(remoteFileSeparator);
246261
}
247262

@@ -290,9 +305,93 @@ public void setAutoCreateLocalDirectory(boolean autoCreateLocalDirectory) {
290305
* @see RemoteFileTemplate#setTemporaryFileSuffix(String)
291306
*/
292307
public void setTemporaryFileSuffix(String temporaryFileSuffix) {
308+
assertRemoteFileTemplateMutability("temporaryFileSuffix");
293309
this.remoteFileTemplate.setTemporaryFileSuffix(temporaryFileSuffix);
294310
}
295311

312+
/**
313+
* Determine whether the remote directory should automatically be created when
314+
* sending files to the remote system.
315+
* @param autoCreateDirectory true to create the directory.
316+
* @since 5.2
317+
* @see RemoteFileTemplate#setAutoCreateDirectory(boolean)
318+
*/
319+
public void setAutoCreateDirectory(boolean autoCreateDirectory) {
320+
assertRemoteFileTemplateMutability("autoCreateDirectory");
321+
this.remoteFileTemplate.setAutoCreateDirectory(autoCreateDirectory);
322+
}
323+
324+
/**
325+
* Set the remote directory expression used to determine the remote directory to which
326+
* files will be sent.
327+
* @param remoteDirectoryExpression the remote directory expression.
328+
* @since 5.2
329+
* @see RemoteFileTemplate#setRemoteDirectoryExpression
330+
*/
331+
public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) {
332+
assertRemoteFileTemplateMutability("remoteDirectoryExpression");
333+
this.remoteFileTemplate.setRemoteDirectoryExpression(remoteDirectoryExpression);
334+
}
335+
336+
/**
337+
* Set a temporary remote directory expression; used when transferring files to the remote
338+
* system. After a successful transfer the file is renamed using the
339+
* {@link #setRemoteDirectoryExpression(Expression) remoteDirectoryExpression}.
340+
* @param temporaryRemoteDirectoryExpression the temporary remote directory expression.
341+
* @since 5.2
342+
* @see RemoteFileTemplate#setTemporaryRemoteDirectoryExpression
343+
*/
344+
public void setTemporaryRemoteDirectoryExpression(Expression temporaryRemoteDirectoryExpression) {
345+
assertRemoteFileTemplateMutability("temporaryRemoteDirectoryExpression");
346+
this.remoteFileTemplate.setTemporaryRemoteDirectoryExpression(temporaryRemoteDirectoryExpression);
347+
}
348+
349+
/**
350+
* Set the file name expression to determine the full path to the remote file.
351+
* @param fileNameExpression the file name expression.
352+
* @since 5.2
353+
* @see RemoteFileTemplate#setFileNameExpression
354+
*/
355+
public void setFileNameExpression(Expression fileNameExpression) {
356+
assertRemoteFileTemplateMutability("fileNameExpression");
357+
this.remoteFileTemplate.setFileNameExpression(fileNameExpression);
358+
}
359+
360+
/**
361+
* Set whether a temporary file name is used when sending files to the remote system.
362+
* @param useTemporaryFileName true to use a temporary file name.
363+
* @since 5.2
364+
* @see RemoteFileTemplate#setUseTemporaryFileName
365+
*/
366+
public void setUseTemporaryFileName(boolean useTemporaryFileName) {
367+
assertRemoteFileTemplateMutability("useTemporaryFileName");
368+
this.remoteFileTemplate.setUseTemporaryFileName(useTemporaryFileName);
369+
}
370+
371+
/**
372+
* Set the file name generator used to generate the remote filename to be used when transferring
373+
* files to the remote system.
374+
* @param fileNameGenerator the file name generator.
375+
* @since 5.2
376+
* @see RemoteFileTemplate#setFileNameGenerator
377+
*/
378+
public void setFileNameGenerator(FileNameGenerator fileNameGenerator) {
379+
assertRemoteFileTemplateMutability("fileNameGenerator");
380+
this.remoteFileTemplate.setFileNameGenerator(fileNameGenerator);
381+
}
382+
383+
/**
384+
* Set the charset to use when converting String payloads to bytes as the content of the
385+
* remote file. Default {@code UTF-8}.
386+
* @param charset the charset.
387+
* @since 5.2
388+
* @see RemoteFileTemplate#setCharset
389+
*/
390+
public void setCharset(String charset) {
391+
assertRemoteFileTemplateMutability("charset");
392+
this.remoteFileTemplate.setCharset(charset);
393+
}
394+
296395
/**
297396
* Set a {@link FileListFilter} to filter remote files.
298397
* @param filter the filter to set

0 commit comments

Comments
 (0)