use of org.springframework.integration.support.PartialSuccessException in project spring-integration by spring-projects.
the class FtpServerOutboundTests method testMputRecursivePartial.
@Test
public void testMputRecursivePartial() throws Exception {
Session<FTPFile> session = spyOnSession();
File sourceLocalSubDirectory = new File(getSourceLocalDirectory(), "subLocalSource");
assertTrue(sourceLocalSubDirectory.isDirectory());
File extra = new File(sourceLocalSubDirectory, "subLocalSource2.txt");
FileOutputStream writer = new FileOutputStream(extra);
writer.write("foo".getBytes());
writer.close();
doAnswer(invocation -> {
throw new IOException("Failed to send subLocalSource2");
}).when(session).write(Mockito.any(InputStream.class), Mockito.contains("subLocalSource2"));
try {
this.inboundMPutRecursive.send(new GenericMessage<File>(getSourceLocalDirectory()));
fail("expected exception");
} catch (PartialSuccessException e) {
assertEquals(3, e.getDerivedInput().size());
assertEquals(2, e.getPartialResults().size());
assertThat(e.getCause(), Matchers.instanceOf(PartialSuccessException.class));
PartialSuccessException cause = (PartialSuccessException) e.getCause();
assertEquals(2, cause.getDerivedInput().size());
assertEquals(1, cause.getPartialResults().size());
assertThat(cause.getCause().getMessage(), containsString("Failed to send subLocalSource2"));
}
extra.delete();
}
use of org.springframework.integration.support.PartialSuccessException in project spring-integration by spring-projects.
the class AbstractRemoteFileOutboundGateway method mGetWithoutRecursion.
private List<File> mGetWithoutRecursion(Message<?> message, Session<F> session, String remoteDirectory, String remoteFilename) throws IOException {
List<File> files = new ArrayList<File>();
String remotePath = buildRemotePath(remoteDirectory, remoteFilename);
@SuppressWarnings("unchecked") List<AbstractFileInfo<F>> remoteFiles = (List<AbstractFileInfo<F>>) ls(message, session, remotePath);
if (remoteFiles.size() == 0 && this.options.contains(Option.EXCEPTION_WHEN_EMPTY)) {
throw new MessagingException("No files found at " + (remoteDirectory != null ? remoteDirectory : "Client Working Directory") + " with pattern " + remoteFilename);
}
try {
for (AbstractFileInfo<F> lsEntry : remoteFiles) {
if (lsEntry.isDirectory()) {
continue;
}
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());
if (file != null) {
files.add(file);
}
}
} catch (Exception e) {
if (files.size() > 0) {
throw new PartialSuccessException(message, "Partially successful recursive 'mget' operation on " + (remoteDirectory != null ? remoteDirectory : "Client Working Directory"), e, files, remoteFiles);
} else if (e instanceof MessagingException) {
throw (MessagingException) e;
} else if (e instanceof IOException) {
throw (IOException) e;
}
}
return files;
}
use of org.springframework.integration.support.PartialSuccessException in project spring-integration by spring-projects.
the class AbstractRemoteFileOutboundGateway method putLocalDirectory.
private List<String> putLocalDirectory(Message<?> requestMessage, File file, String subDirectory) {
File[] files = file.listFiles();
List<File> filteredFiles = this.filterMputFiles(files);
List<String> replies = new ArrayList<>();
try {
for (File filteredFile : filteredFiles) {
if (!filteredFile.isDirectory()) {
String path = doPut(new MutableMessage<>(filteredFile, requestMessage.getHeaders()), subDirectory);
if (path == null) {
// NOSONAR - false positive
if (logger.isDebugEnabled()) {
logger.debug("File " + filteredFile.getAbsolutePath() + " removed before transfer; ignoring");
}
} else {
replies.add(path);
}
} else if (this.options.contains(Option.RECURSIVE)) {
String newSubDirectory = (StringUtils.hasText(subDirectory) ? subDirectory + this.remoteFileTemplate.getRemoteFileSeparator() : "") + filteredFile.getName();
replies.addAll(putLocalDirectory(requestMessage, filteredFile, newSubDirectory));
}
}
} catch (Exception e) {
if (replies.size() > 0) {
throw new PartialSuccessException(requestMessage, "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);
} else {
throw e;
}
}
return replies;
}
use of org.springframework.integration.support.PartialSuccessException in project spring-integration by spring-projects.
the class AbstractRemoteFileOutboundGateway method mGetWithRecursion.
private List<File> mGetWithRecursion(Message<?> message, Session<F> session, String remoteDirectory, String remoteFilename) throws IOException {
List<File> files = new ArrayList<File>();
@SuppressWarnings("unchecked") List<AbstractFileInfo<F>> fileNames = (List<AbstractFileInfo<F>>) ls(message, session, remoteDirectory);
if (fileNames.size() == 0 && this.options.contains(Option.EXCEPTION_WHEN_EMPTY)) {
throw new MessagingException("No files found at " + (remoteDirectory != null ? remoteDirectory : "Client Working Directory") + " with pattern " + remoteFilename);
}
try {
for (AbstractFileInfo<F> lsEntry : fileNames) {
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());
if (file != null) {
files.add(file);
}
}
} catch (Exception e) {
if (files.size() > 0) {
throw new PartialSuccessException(message, "Partially successful recursive 'mget' operation on " + (remoteDirectory != null ? remoteDirectory : "Client Working Directory"), e, files, fileNames);
} else if (e instanceof MessagingException) {
throw (MessagingException) e;
} else if (e instanceof IOException) {
throw (IOException) e;
} else {
throw new MessagingException("Failed to process MGET on first file", e);
}
}
return files;
}
use of org.springframework.integration.support.PartialSuccessException in project spring-integration by spring-projects.
the class FtpServerOutboundTests method testMgetPartial.
@Test
public void testMgetPartial() throws Exception {
Session<FTPFile> session = spyOnSession();
doAnswer(invocation -> {
FTPFile[] files = (FTPFile[]) invocation.callRealMethod();
// add an extra file where the get will fail
files = Arrays.copyOf(files, files.length + 1);
FTPFile bogusFile = new FTPFile();
bogusFile.setName("bogus.txt");
files[files.length - 1] = bogusFile;
return files;
}).when(session).list("ftpSource/subFtpSource/*");
String dir = "ftpSource/subFtpSource/";
try {
this.inboundMGet.send(new GenericMessage<Object>(dir + "*"));
fail("expected exception");
} catch (PartialSuccessException e) {
assertEquals(2, e.getDerivedInput().size());
assertEquals(1, e.getPartialResults().size());
assertThat(e.getCause().getMessage(), containsString("/ftpSource/subFtpSource/bogus.txt: No such file or directory."));
}
}
Aggregations