11/*
2- * Copyright 2013-2019 the original author or authors.
2+ * Copyright 2013-2020 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2323import java .io .FileNotFoundException ;
2424import java .io .IOException ;
2525import java .io .InputStream ;
26+ import java .nio .charset .Charset ;
27+ import java .nio .charset .StandardCharsets ;
2628import java .util .concurrent .atomic .AtomicInteger ;
2729
2830import org .apache .commons .logging .Log ;
3234import org .springframework .beans .factory .BeanFactory ;
3335import org .springframework .beans .factory .BeanFactoryAware ;
3436import org .springframework .beans .factory .InitializingBean ;
37+ import org .springframework .core .io .Resource ;
3538import org .springframework .expression .Expression ;
3639import org .springframework .integration .file .DefaultFileNameGenerator ;
3740import org .springframework .integration .file .FileNameGenerator ;
@@ -65,7 +68,7 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
6568 private final Log logger = LogFactory .getLog (this .getClass ());
6669
6770 /**
68- * the {@link SessionFactory} for acquiring remote file Sessions.
71+ * The {@link SessionFactory} for acquiring remote file Sessions.
6972 */
7073 protected final SessionFactory <F > sessionFactory ; // NOSONAR
7174
@@ -76,29 +79,29 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
7679
7780 private final AtomicInteger activeTemplateCallbacks = new AtomicInteger ();
7881
79- private volatile String temporaryFileSuffix = ".writing" ;
82+ private String temporaryFileSuffix = ".writing" ;
8083
81- private volatile boolean autoCreateDirectory = false ;
84+ private boolean autoCreateDirectory = false ;
8285
83- private volatile boolean useTemporaryFileName = true ;
86+ private boolean useTemporaryFileName = true ;
8487
85- private volatile ExpressionEvaluatingMessageProcessor <String > directoryExpressionProcessor ;
88+ private ExpressionEvaluatingMessageProcessor <String > directoryExpressionProcessor ;
8689
87- private volatile ExpressionEvaluatingMessageProcessor <String > temporaryDirectoryExpressionProcessor ;
90+ private ExpressionEvaluatingMessageProcessor <String > temporaryDirectoryExpressionProcessor ;
8891
89- private volatile ExpressionEvaluatingMessageProcessor <String > fileNameProcessor ;
92+ private ExpressionEvaluatingMessageProcessor <String > fileNameProcessor ;
9093
91- private volatile FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator ();
94+ private FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator ();
9295
93- private volatile boolean fileNameGeneratorSet ;
96+ private boolean fileNameGeneratorSet ;
9497
95- private volatile String charset = "UTF-8" ;
98+ private Charset charset = StandardCharsets . UTF_8 ;
9699
97- private volatile String remoteFileSeparator = "/" ;
100+ private String remoteFileSeparator = "/" ;
98101
99- private volatile boolean hasExplicitlySetSuffix ;
102+ private boolean hasExplicitlySetSuffix ;
100103
101- private volatile BeanFactory beanFactory ;
104+ private BeanFactory beanFactory ;
102105
103106 /**
104107 * Construct a {@link RemoteFileTemplate} with the supplied session factory.
@@ -216,7 +219,7 @@ public void setFileNameGenerator(FileNameGenerator fileNameGenerator) {
216219 * @param charset the charset.
217220 */
218221 public void setCharset (String charset ) {
219- this .charset = charset ;
222+ this .charset = Charset . forName ( charset ) ;
220223 }
221224
222225 /**
@@ -264,12 +267,12 @@ public void afterPropertiesSet() {
264267 }
265268
266269 @ Override
267- public String append (final Message <?> message ) {
270+ public String append (Message <?> message ) {
268271 return append (message , null );
269272 }
270273
271274 @ Override
272- public String append (final Message <?> message , String subDirectory ) {
275+ public String append (Message <?> message , String subDirectory ) {
273276 return send (message , subDirectory , FileExistsMode .APPEND );
274277 }
275278
@@ -279,14 +282,14 @@ public String send(Message<?> message, FileExistsMode... mode) {
279282 }
280283
281284 @ Override
282- public String send (final Message <?> message , String subDirectory , FileExistsMode ... mode ) {
285+ public String send (Message <?> message , String subDirectory , FileExistsMode ... mode ) {
283286 FileExistsMode modeToUse = mode == null || mode .length < 1 || mode [0 ] == null
284287 ? FileExistsMode .REPLACE
285288 : mode [0 ];
286289 return send (message , subDirectory , modeToUse );
287290 }
288291
289- private String send (final Message <?> message , final String subDirectory , final FileExistsMode mode ) {
292+ private String send (Message <?> message , String subDirectory , FileExistsMode mode ) {
290293 Assert .notNull (this .directoryExpressionProcessor , "'remoteDirectoryExpression' is required" );
291294 Assert .isTrue (!FileExistsMode .APPEND .equals (mode ) || !this .useTemporaryFileName ,
292295 "Cannot append when using a temporary file name" );
@@ -354,17 +357,17 @@ private String doSend(Message<?> message, String subDirectory, FileExistsMode mo
354357 }
355358
356359 @ Override
357- public boolean exists (final String path ) {
360+ public boolean exists (String path ) {
358361 return execute (session -> session .exists (path ));
359362 }
360363
361364 @ Override
362- public boolean remove (final String path ) {
365+ public boolean remove (String path ) {
363366 return execute (session -> session .remove (path ));
364367 }
365368
366369 @ Override
367- public void rename (final String fromPath , final String toPath ) {
370+ public void rename (String fromPath , String toPath ) {
368371 Assert .hasText (fromPath , "Old filename cannot be null or empty" );
369372 Assert .hasText (toPath , "New filename cannot be null or empty" );
370373
@@ -390,7 +393,7 @@ public boolean get(Message<?> message, InputStreamCallback callback) {
390393 }
391394
392395 @ Override
393- public boolean get (final String remotePath , final InputStreamCallback callback ) {
396+ public boolean get (String remotePath , InputStreamCallback callback ) {
394397 Assert .notNull (remotePath , "'remotePath' cannot be null" );
395398 return execute (session -> {
396399 try (InputStream inputStream = session .readRaw (remotePath )) {
@@ -419,7 +422,6 @@ public Session<F> getSession() {
419422 return this .sessionFactory .getSession ();
420423 }
421424
422- @ SuppressWarnings ("rawtypes" )
423425 @ Override
424426 public <T > T execute (SessionCallback <F , T > callback ) {
425427 Session <F > session = null ;
@@ -440,7 +442,7 @@ public <T> T execute(SessionCallback<F, T> callback) {
440442 if (session != null ) {
441443 session .dirty ();
442444 }
443- if (e instanceof MessagingException ) {
445+ if (e instanceof MessagingException ) { // NOSONAR
444446 throw (MessagingException ) e ;
445447 }
446448 throw new MessagingException ("Failed to execute on session" , e );
@@ -498,7 +500,7 @@ private StreamHolder payloadToInputStream(Message<?> message) throws MessageDeli
498500 }
499501 }
500502 else if (payload instanceof byte [] || payload instanceof String ) {
501- byte [] bytes = null ;
503+ byte [] bytes ;
502504 if (payload instanceof String ) {
503505 bytes = ((String ) payload ).getBytes (this .charset );
504506 name = "String payload" ;
@@ -513,6 +515,12 @@ else if (payload instanceof InputStream) {
513515 dataInputStream = (InputStream ) payload ;
514516 name = "InputStream payload" ;
515517 }
518+ else if (payload instanceof Resource ) {
519+ Resource resource = (Resource ) payload ;
520+ dataInputStream = resource .getInputStream ();
521+ String filename = resource .getFilename ();
522+ name = filename != null ? filename : "Resource payload" ;
523+ }
516524 else {
517525 throw new IllegalArgumentException ("Unsupported payload type ["
518526 + payload .getClass ().getName ()
@@ -563,6 +571,7 @@ private void sendFileToRemoteDirectory(InputStream inputStream, String temporary
563571
564572 private void doSend (Session <F > session , FileExistsMode mode , String remoteFilePath , String tempFilePath ,
565573 InputStream stream ) throws IOException {
574+
566575 boolean rename = this .useTemporaryFileName ;
567576 if (FileExistsMode .REPLACE .equals (mode )) {
568577 session .write (stream , tempFilePath );
0 commit comments