From ce4ce74db6e64902d543e9138198a97b77e67792 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 11 Oct 2023 14:09:02 -0400 Subject: [PATCH] Some code clean up for RemoteFileOutboundGateway * Also apply suggested by IDE refactoring to `FtpServerOutboundTests` --- .../AbstractRemoteFileOutboundGateway.java | 48 +++++++++---------- .../ftp/outbound/FtpServerOutboundTests.java | 20 ++++---- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java index 600f228787..bf28676d09 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java @@ -151,8 +151,8 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply } /** - * Construct an instance with the supplied session factory, a command ('ls', 'get' - * etc), and an expression to determine the filename. + * 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. @@ -164,8 +164,8 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply } /** - * Construct an instance with the supplied session factory, a command ('ls', 'get' - * etc), and an expression to determine the filename. + * 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. @@ -178,8 +178,8 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply } /** - * Construct an instance with the supplied remote file template, a command ('ls', - * 'get' etc), and an expression to determine the filename. + * 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. @@ -191,8 +191,8 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply } /** - * Construct an instance with the supplied remote file template, a command ('ls', - * 'get' etc), and an expression to determine the filename. + * 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 expressionArg the filename expression. @@ -823,7 +823,7 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply * The session argument isn't used in the default implementation. * @param message the request message related to this put command * @param session the remote protocol session related to this invocation context - * @param subDirectory the target sub directory to put + * @param subDirectory the target sub-directory to put * @return The remote path, or null if no local file was found. * @since 5.0 */ @@ -854,19 +854,19 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply private Object doMput(Message requestMessage) { File file = null; Object payload = requestMessage.getPayload(); - if (payload instanceof File) { - file = (File) payload; + if (payload instanceof File filePayload) { + file = filePayload; } - else if (payload instanceof String) { - file = new File((String) payload); + else if (payload instanceof String fileName) { + file = new File(fileName); } else if (!(payload instanceof Collection)) { throw new IllegalArgumentException( "Only File or String payloads (or Collection of File/String) allowed for 'mput', received: " + payload.getClass()); } - if ((payload instanceof Collection)) { - return ((Collection) payload).stream() + if (payload instanceof Collection files) { + return files.stream() .map(p -> doMput(new MutableMessage<>(p, requestMessage.getHeaders()))) .collect(Collectors.toList()); } @@ -926,7 +926,7 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply private RuntimeException handlePutException(Message requestMessage, String subDirectory, List filteredFiles, List replies, RuntimeException ex) { - if (replies.size() > 0 || ex instanceof PartialSuccessException) { + if (!replies.isEmpty() || ex instanceof PartialSuccessException) { return new PartialSuccessException(requestMessage, "Partially successful 'mput' operation" + (subDirectory == null ? "" : (" on " + subDirectory)), ex, replies, filteredFiles); @@ -1124,7 +1124,7 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply session.read(remoteFilePath, outputStream); } catch (Exception ex) { - /* Some operation systems acquire exclusive file-lock during file processing + /* Some operational systems acquire exclusive file-lock during file processing and the file can't be deleted without closing streams before. */ outputStream.close(); @@ -1223,17 +1223,17 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply private RuntimeException processMgetException(Message message, String remoteDirectory, List files, List> remoteFiles, Exception ex) { - if (files.size() > 0) { + if (!files.isEmpty()) { return new PartialSuccessException(message, "Partially successful recursive 'mget' operation on " + (remoteDirectory != null ? remoteDirectory : "Client Working Directory"), ex, files, remoteFiles); } - else if (ex instanceof MessagingException) { - return (MessagingException) ex; + else if (ex instanceof MessagingException messagingException) { + return messagingException; } - else if (ex instanceof IOException) { - throw new UncheckedIOException((IOException) ex); + else if (ex instanceof IOException ioException) { + throw new UncheckedIOException(ioException); } else { return new MessagingException("Failed to process MGET", ex); @@ -1265,7 +1265,7 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply @SuppressWarnings("unchecked") List> remoteFiles = (List>) ls(message, session, remotePath); - if (remoteFiles.size() == 0 && this.options.contains(Option.EXCEPTION_WHEN_EMPTY)) { + if (remoteFiles.isEmpty() && this.options.contains(Option.EXCEPTION_WHEN_EMPTY)) { throw new MessagingException("No files found at " + (remoteDirectory != null ? remoteDirectory : "Client Working Directory") + " with pattern " + remoteFilename); @@ -1291,7 +1291,7 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply private String getRemoteDirectory(String remoteFilePath, String remoteFilename) { String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf(remoteFilename)); - if (remoteDir.length() == 0) { + if (remoteDir.isEmpty()) { return null; } return remoteDir; diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java index f0a144defc..7a1db935b0 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2022 the original author or authors. + * Copyright 2013-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -303,8 +303,8 @@ public class FtpServerOutboundTests extends FtpTestSupport { ByteArrayOutputStream localContents = new ByteArrayOutputStream(); FileUtils.copyFile(secondRemote, remoteContents); FileUtils.copyFile(secondTarget, localContents); - String localAsString = new String(localContents.toByteArray()); - assertThat(localAsString).isEqualTo(new String(remoteContents.toByteArray())); + String localAsString = localContents.toString(); + assertThat(localAsString).isEqualTo(remoteContents.toString()); long oldLastModified = secondRemote.lastModified(); FileUtils.copyInputStreamToFile(new ByteArrayInputStream("junk".getBytes()), secondRemote); long newLastModified = secondRemote.lastModified(); @@ -313,13 +313,13 @@ public class FtpServerOutboundTests extends FtpTestSupport { this.output.receive(0); localContents = new ByteArrayOutputStream(); FileUtils.copyFile(secondTarget, localContents); - assertThat(new String(localContents.toByteArray())).isEqualTo(localAsString); + assertThat(localContents.toString()).isEqualTo(localAsString); secondRemote.setLastModified(newLastModified); this.inboundMGetRecursive.send(new GenericMessage("*")); this.output.receive(0); localContents = new ByteArrayOutputStream(); FileUtils.copyFile(secondTarget, localContents); - assertThat(new String(localContents.toByteArray())).isEqualTo("junk"); + assertThat(localContents.toString()).isEqualTo("junk"); // restore the remote file contents FileUtils.copyInputStreamToFile(new ByteArrayInputStream(localAsString.getBytes()), secondRemote); } @@ -364,12 +364,12 @@ public class FtpServerOutboundTests extends FtpTestSupport { ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("ftpSource/ ftpSource1.txt"), baos); assertThat(session.finalizeRaw()).isTrue(); - assertThat(new String(baos.toByteArray())).isEqualTo("source1"); + assertThat(baos.toString()).isEqualTo("source1"); baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("ftpSource/ftpSource2.txt"), baos); assertThat(session.finalizeRaw()).isTrue(); - assertThat(new String(baos.toByteArray())).isEqualTo("source2"); + assertThat(baos.toString()).isEqualTo("source2"); session.close(); } @@ -383,12 +383,12 @@ public class FtpServerOutboundTests extends FtpTestSupport { final ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); assertThat(template.get(new GenericMessage<>("ftpSource/ ftpSource1.txt"), stream -> FileCopyUtils.copy(stream, baos1))).isTrue(); - assertThat(new String(baos1.toByteArray())).isEqualTo("source1"); + assertThat(baos1.toString()).isEqualTo("source1"); final ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); assertThat(template.get(new GenericMessage<>("ftpSource/ftpSource2.txt"), stream -> FileCopyUtils.copy(stream, baos2))).isTrue(); - assertThat(new String(baos2.toByteArray())).isEqualTo("source2"); + assertThat(baos2.toString()).isEqualTo("source2"); } @Test @@ -817,7 +817,7 @@ public class FtpServerOutboundTests extends FtpTestSupport { @EventListener public void handleEvent(ApacheMinaFtpEvent event) { if (this.latch != null) { - if (this.events.size() > 0 || event instanceof SessionOpenedEvent) { + if (!this.events.isEmpty() || event instanceof SessionOpenedEvent) { if (event instanceof SessionOpenedEvent) { this.clientAddress = event.getSession().getClientAddress(); this.events.add(event);