Skip to content

Commit

Permalink
Some code clean up for RemoteFileOutboundGateway
Browse files Browse the repository at this point in the history
* Also apply suggested by IDE refactoring to `FtpServerOutboundTests`
  • Loading branch information
artembilan committed Oct 11, 2023
1 parent 44433ed commit ce4ce74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ public AbstractRemoteFileOutboundGateway(RemoteFileTemplate<F> remoteFileTemplat
}

/**
* 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.
Expand All @@ -164,8 +164,8 @@ public AbstractRemoteFileOutboundGateway(SessionFactory<F> sessionFactory, Strin
}

/**
* 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.
Expand All @@ -178,8 +178,8 @@ public AbstractRemoteFileOutboundGateway(SessionFactory<F> sessionFactory, Comma
}

/**
* 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.
Expand All @@ -191,8 +191,8 @@ public AbstractRemoteFileOutboundGateway(RemoteFileTemplate<F> remoteFileTemplat
}

/**
* 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.
Expand Down Expand Up @@ -823,7 +823,7 @@ private String doPut(Message<?> requestMessage, String subDirectory) {
* 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
*/
Expand Down Expand Up @@ -854,19 +854,19 @@ protected void doChmod(RemoteFileOperations<F> remoteFileOperations, String path
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());
}
Expand Down Expand Up @@ -926,7 +926,7 @@ else if (this.options.contains(Option.RECURSIVE)) {
private RuntimeException handlePutException(Message<?> requestMessage, String subDirectory,
List<File> filteredFiles, List<String> 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);
Expand Down Expand Up @@ -1124,7 +1124,7 @@ protected File get(Message<?> message, Session<F> session, String remoteDir, //
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();
Expand Down Expand Up @@ -1223,17 +1223,17 @@ private List<File> mGetWithoutRecursion(Message<?> message, Session<F> session,
private RuntimeException processMgetException(Message<?> message, String remoteDirectory, List<File> files,
List<AbstractFileInfo<F>> 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);
Expand Down Expand Up @@ -1265,7 +1265,7 @@ private List<AbstractFileInfo<F>> lsRemoteFilesForMget(Message<?> message, Sessi

@SuppressWarnings("unchecked")
List<AbstractFileInfo<F>> remoteFiles = (List<AbstractFileInfo<F>>) 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);
Expand All @@ -1291,7 +1291,7 @@ private File getRemoteFileForMget(Message<?> message, Session<F> session, String

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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -303,8 +303,8 @@ public void testInt3172LocalDirectoryExpressionMGETRecursive() throws IOExceptio
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();
Expand All @@ -313,13 +313,13 @@ public void testInt3172LocalDirectoryExpressionMGETRecursive() throws IOExceptio
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<Object>("*"));
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);
}
Expand Down Expand Up @@ -364,12 +364,12 @@ public void testInt3100RawGET() throws Exception {
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();
}
Expand All @@ -383,12 +383,12 @@ public void testRawGETWithTemplate() {
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
Expand Down Expand Up @@ -817,7 +817,7 @@ public FtpRemoteFileTemplate template(SessionFactory<FTPFile> sf) {
@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);
Expand Down

0 comments on commit ce4ce74

Please sign in to comment.