Sonar fixes according latest report (#2676)
* Sonar fixes according latest report * Fix initialization order in the `GatewayMethodInboundMessageMapper` * Fix mock in the `DelegatingSessionFactoryTests` * Fix `AbstractRemoteFileOutboundGateway.listFilesInRemoteDir` * * PR comments * * Fix `AbstractRemoteFileOutboundGateway.listFilesInRemoteDir` complexity
This commit is contained in:
committed by
Gary Russell
parent
1943c15afe
commit
81b4ea1bef
@@ -743,13 +743,13 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
|
||||
catch (Exception e) {
|
||||
if (replies.size() > 0) {
|
||||
throw new PartialSuccessException(requestMessage,
|
||||
"Partially successful 'mput' operation" + (subDirectory == null ? "" : (" on " + subDirectory)),
|
||||
e, replies, filteredFiles);
|
||||
"Partially successful 'mput' operation" +
|
||||
(subDirectory == null ? "" : (" on " + subDirectory)), e, replies, filteredFiles);
|
||||
}
|
||||
else if (e instanceof PartialSuccessException) {
|
||||
throw new PartialSuccessException(requestMessage,
|
||||
"Partially successful 'mput' operation" + (subDirectory == null ? "" : (" on " + subDirectory)),
|
||||
e, replies, filteredFiles);
|
||||
"Partially successful 'mput' operation" +
|
||||
(subDirectory == null ? "" : (" on " + subDirectory)), e, replies, filteredFiles);
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
@@ -801,28 +801,15 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
|
||||
private List<F> listFilesInRemoteDir(Session<F> session, String directory, String subDirectory)
|
||||
throws IOException {
|
||||
|
||||
List<F> lsFiles = new ArrayList<F>();
|
||||
List<F> lsFiles = new ArrayList<>();
|
||||
String remoteDirectory = buildRemotePath(directory, subDirectory);
|
||||
|
||||
F[] files = session.list(remoteDirectory);
|
||||
boolean recursion = this.options.contains(Option.RECURSIVE);
|
||||
if (!ObjectUtils.isEmpty(files)) {
|
||||
Collection<F> filteredFiles = this.filterFiles(files);
|
||||
for (F file : filteredFiles) {
|
||||
String fileName = this.getFilename(file);
|
||||
for (F file : filterFiles(files)) {
|
||||
if (file != null) {
|
||||
if (this.options.contains(Option.SUBDIRS) || !this.isDirectory(file)) {
|
||||
if (recursion && StringUtils.hasText(subDirectory)) {
|
||||
lsFiles.add(enhanceNameWithSubDirectory(file, subDirectory));
|
||||
}
|
||||
else {
|
||||
lsFiles.add(file);
|
||||
}
|
||||
}
|
||||
if (recursion && this.isDirectory(file) && !(".".equals(fileName)) && !("..".equals(fileName))) {
|
||||
lsFiles.addAll(listFilesInRemoteDir(session, directory, subDirectory + fileName
|
||||
+ this.remoteFileTemplate.getRemoteFileSeparator()));
|
||||
}
|
||||
processFile(session, directory, subDirectory, lsFiles, recursion, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -844,6 +831,24 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
|
||||
return (this.filter != null) ? this.filter.filterFiles(files) : Arrays.asList(files);
|
||||
}
|
||||
|
||||
private void processFile(Session<F> session, String directory, String subDirectory, List<F> lsFiles,
|
||||
boolean recursion, F file) throws IOException {
|
||||
|
||||
if (this.options.contains(Option.SUBDIRS) || !isDirectory(file)) {
|
||||
if (recursion && StringUtils.hasText(subDirectory)) {
|
||||
lsFiles.add(enhanceNameWithSubDirectory(file, subDirectory));
|
||||
}
|
||||
else {
|
||||
lsFiles.add(file);
|
||||
}
|
||||
}
|
||||
String fileName = getFilename(file);
|
||||
if (recursion && isDirectory(file) && !(".".equals(fileName)) && !("..".equals(fileName))) {
|
||||
lsFiles.addAll(listFilesInRemoteDir(session, directory,
|
||||
subDirectory + fileName + this.remoteFileTemplate.getRemoteFileSeparator()));
|
||||
}
|
||||
}
|
||||
|
||||
protected final List<File> filterMputFiles(File[] files) {
|
||||
if (files == null) {
|
||||
return Collections.emptyList();
|
||||
@@ -1009,7 +1014,8 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
|
||||
*/
|
||||
String fileName = this.getRemoteFilename(fullFileName);
|
||||
String actualRemoteDirectory = this.getRemoteDirectory(fullFileName, fileName);
|
||||
File file = get(message, session, actualRemoteDirectory, fullFileName, fileName, lsEntry.getFileInfo());
|
||||
File file = get(message, session, actualRemoteDirectory, fullFileName, fileName,
|
||||
lsEntry.getFileInfo());
|
||||
if (file != null) {
|
||||
files.add(file);
|
||||
}
|
||||
@@ -1044,16 +1050,18 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
|
||||
}
|
||||
try {
|
||||
for (AbstractFileInfo<F> lsEntry : fileNames) {
|
||||
String fullFileName = remoteDirectory != null
|
||||
? remoteDirectory + getFilename(lsEntry)
|
||||
: getFilename(lsEntry);
|
||||
String fullFileName =
|
||||
remoteDirectory != null
|
||||
? remoteDirectory + getFilename(lsEntry)
|
||||
: getFilename(lsEntry);
|
||||
/*
|
||||
* With recursion, the filename might contain subdirectory information
|
||||
* normalize each file separately.
|
||||
*/
|
||||
String fileName = this.getRemoteFilename(fullFileName);
|
||||
String actualRemoteDirectory = this.getRemoteDirectory(fullFileName, fileName);
|
||||
File file = get(message, session, actualRemoteDirectory, fullFileName, fileName, lsEntry.getFileInfo());
|
||||
File file = get(message, session, actualRemoteDirectory, fullFileName, fileName,
|
||||
lsEntry.getFileInfo());
|
||||
if (file != null) {
|
||||
files.add(file);
|
||||
}
|
||||
|
||||
@@ -421,13 +421,12 @@ public class RemoteFileOutboundGatewayTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
MessageBuilder<List<TestLsEntry>> out = (MessageBuilder<List<TestLsEntry>>) gw
|
||||
.handleRequestMessage(new GenericMessage<>("testremote/x"));
|
||||
assertEquals(6, out.getPayload().size());
|
||||
assertEquals(5, out.getPayload().size());
|
||||
assertEquals("f1", out.getPayload().get(0).getFilename());
|
||||
assertEquals("d1", out.getPayload().get(1).getFilename());
|
||||
assertEquals("d1/d2", out.getPayload().get(2).getFilename());
|
||||
assertEquals("d1/d2/f4", out.getPayload().get(3).getFilename());
|
||||
assertEquals("d1/f3", out.getPayload().get(4).getFilename());
|
||||
assertEquals("f2", out.getPayload().get(5).getFilename());
|
||||
assertEquals("d1/f3", out.getPayload().get(3).getFilename());
|
||||
assertEquals("f2", out.getPayload().get(4).getFilename());
|
||||
assertEquals("testremote/x/",
|
||||
out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
* Copyright 2015-2018 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.
|
||||
@@ -20,6 +20,8 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@@ -50,6 +52,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 4.2
|
||||
*
|
||||
*/
|
||||
@@ -94,7 +98,9 @@ public class DelegatingSessionFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testFlow() throws Exception {
|
||||
in.send(new GenericMessage<String>("foo"));
|
||||
given(foo.mockSession.list(anyString()))
|
||||
.willReturn(new String[0]);
|
||||
in.send(new GenericMessage<>("foo"));
|
||||
Message<?> received = out.receive(0);
|
||||
assertNotNull(received);
|
||||
verify(foo.mockSession).list("foo/");
|
||||
@@ -102,7 +108,8 @@ public class DelegatingSessionFactoryTests {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:/org/springframework/integration/file/remote/session/delegating-session-factory-context.xml")
|
||||
@ImportResource(
|
||||
"classpath:/org/springframework/integration/file/remote/session/delegating-session-factory-context.xml")
|
||||
@EnableIntegration
|
||||
public static class Config {
|
||||
|
||||
@@ -119,59 +126,59 @@ public class DelegatingSessionFactoryTests {
|
||||
@Bean
|
||||
DelegatingSessionFactory<String> dsf() {
|
||||
SessionFactoryLocator<String> sff = sessionFactoryLocator();
|
||||
return new DelegatingSessionFactory<String>(sff);
|
||||
return new DelegatingSessionFactory<>(sff);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SessionFactoryLocator<String> sessionFactoryLocator() {
|
||||
Map<Object, SessionFactory<String>> factories = new HashMap<Object, SessionFactory<String>>();
|
||||
Map<Object, SessionFactory<String>> factories = new HashMap<>();
|
||||
factories.put("foo", foo());
|
||||
TestSessionFactory bar = bar();
|
||||
factories.put("bar", bar);
|
||||
SessionFactoryLocator<String> sff = new DefaultSessionFactoryLocator<String>(factories, bar);
|
||||
return sff;
|
||||
return new DefaultSessionFactoryLocator<>(factories, bar);
|
||||
}
|
||||
|
||||
@ServiceActivator(inputChannel = "c1")
|
||||
@Bean
|
||||
MessageHandler handler() {
|
||||
AbstractRemoteFileOutboundGateway<String> gateway = new AbstractRemoteFileOutboundGateway<String>(dsf(), "ls", "payload") {
|
||||
AbstractRemoteFileOutboundGateway<String> gateway =
|
||||
new AbstractRemoteFileOutboundGateway<String>(dsf(), "ls", "payload") {
|
||||
|
||||
@Override
|
||||
protected boolean isDirectory(String file) {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
protected boolean isDirectory(String file) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isLink(String file) {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
protected boolean isLink(String file) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFilename(String file) {
|
||||
return file;
|
||||
}
|
||||
@Override
|
||||
protected String getFilename(String file) {
|
||||
return file;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFilename(AbstractFileInfo<String> file) {
|
||||
return file.getFilename();
|
||||
}
|
||||
@Override
|
||||
protected String getFilename(AbstractFileInfo<String> file) {
|
||||
return file.getFilename();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected long getModified(String file) {
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
protected long getModified(String file) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<AbstractFileInfo<String>> asFileInfoList(Collection<String> files) {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
protected List<AbstractFileInfo<String>> asFileInfoList(Collection<String> files) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String enhanceNameWithSubDirectory(String file, String directory) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
@Override
|
||||
protected String enhanceNameWithSubDirectory(String file, String directory) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
gateway.setOutputChannelName("c2");
|
||||
gateway.setOptions("-1");
|
||||
return gateway;
|
||||
|
||||
Reference in New Issue
Block a user