Skip to content

Commit ce4ce74

Browse files
committed
Some code clean up for RemoteFileOutboundGateway
* Also apply suggested by IDE refactoring to `FtpServerOutboundTests`
1 parent 44433ed commit ce4ce74

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ public AbstractRemoteFileOutboundGateway(RemoteFileTemplate<F> remoteFileTemplat
151151
}
152152

153153
/**
154-
* Construct an instance with the supplied session factory, a command ('ls', 'get'
155-
* etc), and an expression to determine the filename.
154+
* Construct an instance with the supplied session factory,
155+
* a command ('ls', 'get' etc.), and an expression to determine the filename.
156156
* @param sessionFactory the session factory.
157157
* @param command the command.
158158
* @param expression the filename expression.
@@ -164,8 +164,8 @@ public AbstractRemoteFileOutboundGateway(SessionFactory<F> sessionFactory, Strin
164164
}
165165

166166
/**
167-
* Construct an instance with the supplied session factory, a command ('ls', 'get'
168-
* etc), and an expression to determine the filename.
167+
* Construct an instance with the supplied session factory,
168+
* a command ('ls', 'get' etc.), and an expression to determine the filename.
169169
* @param sessionFactory the session factory.
170170
* @param command the command.
171171
* @param expression the filename expression.
@@ -178,8 +178,8 @@ public AbstractRemoteFileOutboundGateway(SessionFactory<F> sessionFactory, Comma
178178
}
179179

180180
/**
181-
* Construct an instance with the supplied remote file template, a command ('ls',
182-
* 'get' etc), and an expression to determine the filename.
181+
* Construct an instance with the supplied remote file template,
182+
* a command ('ls', 'get' etc.), and an expression to determine the filename.
183183
* @param remoteFileTemplate the remote file template.
184184
* @param command the command.
185185
* @param expression the filename expression.
@@ -191,8 +191,8 @@ public AbstractRemoteFileOutboundGateway(RemoteFileTemplate<F> remoteFileTemplat
191191
}
192192

193193
/**
194-
* Construct an instance with the supplied remote file template, a command ('ls',
195-
* 'get' etc), and an expression to determine the filename.
194+
* Construct an instance with the supplied remote file template,
195+
* a command ('ls', 'get' etc.), and an expression to determine the filename.
196196
* @param remoteFileTemplate the remote file template.
197197
* @param command the command.
198198
* @param expressionArg the filename expression.
@@ -823,7 +823,7 @@ private String doPut(Message<?> requestMessage, String subDirectory) {
823823
* The session argument isn't used in the default implementation.
824824
* @param message the request message related to this put command
825825
* @param session the remote protocol session related to this invocation context
826-
* @param subDirectory the target sub directory to put
826+
* @param subDirectory the target sub-directory to put
827827
* @return The remote path, or null if no local file was found.
828828
* @since 5.0
829829
*/
@@ -854,19 +854,19 @@ protected void doChmod(RemoteFileOperations<F> remoteFileOperations, String path
854854
private Object doMput(Message<?> requestMessage) {
855855
File file = null;
856856
Object payload = requestMessage.getPayload();
857-
if (payload instanceof File) {
858-
file = (File) payload;
857+
if (payload instanceof File filePayload) {
858+
file = filePayload;
859859
}
860-
else if (payload instanceof String) {
861-
file = new File((String) payload);
860+
else if (payload instanceof String fileName) {
861+
file = new File(fileName);
862862
}
863863
else if (!(payload instanceof Collection)) {
864864
throw new IllegalArgumentException(
865865
"Only File or String payloads (or Collection of File/String) allowed for 'mput', received: "
866866
+ payload.getClass());
867867
}
868-
if ((payload instanceof Collection)) {
869-
return ((Collection<?>) payload).stream()
868+
if (payload instanceof Collection<?> files) {
869+
return files.stream()
870870
.map(p -> doMput(new MutableMessage<>(p, requestMessage.getHeaders())))
871871
.collect(Collectors.toList());
872872
}
@@ -926,7 +926,7 @@ else if (this.options.contains(Option.RECURSIVE)) {
926926
private RuntimeException handlePutException(Message<?> requestMessage, String subDirectory,
927927
List<File> filteredFiles, List<String> replies, RuntimeException ex) {
928928

929-
if (replies.size() > 0 || ex instanceof PartialSuccessException) {
929+
if (!replies.isEmpty() || ex instanceof PartialSuccessException) {
930930
return new PartialSuccessException(requestMessage,
931931
"Partially successful 'mput' operation" +
932932
(subDirectory == null ? "" : (" on " + subDirectory)), ex, replies, filteredFiles);
@@ -1124,7 +1124,7 @@ protected File get(Message<?> message, Session<F> session, String remoteDir, //
11241124
session.read(remoteFilePath, outputStream);
11251125
}
11261126
catch (Exception ex) {
1127-
/* Some operation systems acquire exclusive file-lock during file processing
1127+
/* Some operational systems acquire exclusive file-lock during file processing
11281128
and the file can't be deleted without closing streams before.
11291129
*/
11301130
outputStream.close();
@@ -1223,17 +1223,17 @@ private List<File> mGetWithoutRecursion(Message<?> message, Session<F> session,
12231223
private RuntimeException processMgetException(Message<?> message, String remoteDirectory, List<File> files,
12241224
List<AbstractFileInfo<F>> remoteFiles, Exception ex) {
12251225

1226-
if (files.size() > 0) {
1226+
if (!files.isEmpty()) {
12271227
return new PartialSuccessException(message,
12281228
"Partially successful recursive 'mget' operation on "
12291229
+ (remoteDirectory != null ? remoteDirectory : "Client Working Directory"),
12301230
ex, files, remoteFiles);
12311231
}
1232-
else if (ex instanceof MessagingException) {
1233-
return (MessagingException) ex;
1232+
else if (ex instanceof MessagingException messagingException) {
1233+
return messagingException;
12341234
}
1235-
else if (ex instanceof IOException) {
1236-
throw new UncheckedIOException((IOException) ex);
1235+
else if (ex instanceof IOException ioException) {
1236+
throw new UncheckedIOException(ioException);
12371237
}
12381238
else {
12391239
return new MessagingException("Failed to process MGET", ex);
@@ -1265,7 +1265,7 @@ private List<AbstractFileInfo<F>> lsRemoteFilesForMget(Message<?> message, Sessi
12651265

12661266
@SuppressWarnings("unchecked")
12671267
List<AbstractFileInfo<F>> remoteFiles = (List<AbstractFileInfo<F>>) ls(message, session, remotePath);
1268-
if (remoteFiles.size() == 0 && this.options.contains(Option.EXCEPTION_WHEN_EMPTY)) {
1268+
if (remoteFiles.isEmpty() && this.options.contains(Option.EXCEPTION_WHEN_EMPTY)) {
12691269
throw new MessagingException("No files found at "
12701270
+ (remoteDirectory != null ? remoteDirectory : "Client Working Directory")
12711271
+ " with pattern " + remoteFilename);
@@ -1291,7 +1291,7 @@ private File getRemoteFileForMget(Message<?> message, Session<F> session, String
12911291

12921292
private String getRemoteDirectory(String remoteFilePath, String remoteFilename) {
12931293
String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf(remoteFilename));
1294-
if (remoteDir.length() == 0) {
1294+
if (remoteDir.isEmpty()) {
12951295
return null;
12961296
}
12971297
return remoteDir;

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 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.
@@ -303,8 +303,8 @@ public void testInt3172LocalDirectoryExpressionMGETRecursive() throws IOExceptio
303303
ByteArrayOutputStream localContents = new ByteArrayOutputStream();
304304
FileUtils.copyFile(secondRemote, remoteContents);
305305
FileUtils.copyFile(secondTarget, localContents);
306-
String localAsString = new String(localContents.toByteArray());
307-
assertThat(localAsString).isEqualTo(new String(remoteContents.toByteArray()));
306+
String localAsString = localContents.toString();
307+
assertThat(localAsString).isEqualTo(remoteContents.toString());
308308
long oldLastModified = secondRemote.lastModified();
309309
FileUtils.copyInputStreamToFile(new ByteArrayInputStream("junk".getBytes()), secondRemote);
310310
long newLastModified = secondRemote.lastModified();
@@ -313,13 +313,13 @@ public void testInt3172LocalDirectoryExpressionMGETRecursive() throws IOExceptio
313313
this.output.receive(0);
314314
localContents = new ByteArrayOutputStream();
315315
FileUtils.copyFile(secondTarget, localContents);
316-
assertThat(new String(localContents.toByteArray())).isEqualTo(localAsString);
316+
assertThat(localContents.toString()).isEqualTo(localAsString);
317317
secondRemote.setLastModified(newLastModified);
318318
this.inboundMGetRecursive.send(new GenericMessage<Object>("*"));
319319
this.output.receive(0);
320320
localContents = new ByteArrayOutputStream();
321321
FileUtils.copyFile(secondTarget, localContents);
322-
assertThat(new String(localContents.toByteArray())).isEqualTo("junk");
322+
assertThat(localContents.toString()).isEqualTo("junk");
323323
// restore the remote file contents
324324
FileUtils.copyInputStreamToFile(new ByteArrayInputStream(localAsString.getBytes()), secondRemote);
325325
}
@@ -364,12 +364,12 @@ public void testInt3100RawGET() throws Exception {
364364
ByteArrayOutputStream baos = new ByteArrayOutputStream();
365365
FileCopyUtils.copy(session.readRaw("ftpSource/ ftpSource1.txt"), baos);
366366
assertThat(session.finalizeRaw()).isTrue();
367-
assertThat(new String(baos.toByteArray())).isEqualTo("source1");
367+
assertThat(baos.toString()).isEqualTo("source1");
368368

369369
baos = new ByteArrayOutputStream();
370370
FileCopyUtils.copy(session.readRaw("ftpSource/ftpSource2.txt"), baos);
371371
assertThat(session.finalizeRaw()).isTrue();
372-
assertThat(new String(baos.toByteArray())).isEqualTo("source2");
372+
assertThat(baos.toString()).isEqualTo("source2");
373373

374374
session.close();
375375
}
@@ -383,12 +383,12 @@ public void testRawGETWithTemplate() {
383383
final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
384384
assertThat(template.get(new GenericMessage<>("ftpSource/ ftpSource1.txt"),
385385
stream -> FileCopyUtils.copy(stream, baos1))).isTrue();
386-
assertThat(new String(baos1.toByteArray())).isEqualTo("source1");
386+
assertThat(baos1.toString()).isEqualTo("source1");
387387

388388
final ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
389389
assertThat(template.get(new GenericMessage<>("ftpSource/ftpSource2.txt"),
390390
stream -> FileCopyUtils.copy(stream, baos2))).isTrue();
391-
assertThat(new String(baos2.toByteArray())).isEqualTo("source2");
391+
assertThat(baos2.toString()).isEqualTo("source2");
392392
}
393393

394394
@Test
@@ -817,7 +817,7 @@ public FtpRemoteFileTemplate template(SessionFactory<FTPFile> sf) {
817817
@EventListener
818818
public void handleEvent(ApacheMinaFtpEvent event) {
819819
if (this.latch != null) {
820-
if (this.events.size() > 0 || event instanceof SessionOpenedEvent) {
820+
if (!this.events.isEmpty() || event instanceof SessionOpenedEvent) {
821821
if (event instanceof SessionOpenedEvent) {
822822
this.clientAddress = event.getSession().getClientAddress();
823823
this.events.add(event);

0 commit comments

Comments
 (0)