Search in sources :

Example 1 with PartialSuccessException

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) PartialSuccessException(org.springframework.integration.support.PartialSuccessException) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) Test(org.junit.Test)

Example 2 with PartialSuccessException

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;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) ArrayList(java.util.ArrayList) PartialSuccessException(org.springframework.integration.support.PartialSuccessException) IOException(java.io.IOException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) IOException(java.io.IOException) PartialSuccessException(org.springframework.integration.support.PartialSuccessException) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) AbstractFileInfo(org.springframework.integration.file.remote.AbstractFileInfo)

Example 3 with PartialSuccessException

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;
}
Also used : ArrayList(java.util.ArrayList) PartialSuccessException(org.springframework.integration.support.PartialSuccessException) File(java.io.File) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) IOException(java.io.IOException) PartialSuccessException(org.springframework.integration.support.PartialSuccessException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with PartialSuccessException

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;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) ArrayList(java.util.ArrayList) PartialSuccessException(org.springframework.integration.support.PartialSuccessException) IOException(java.io.IOException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) IOException(java.io.IOException) PartialSuccessException(org.springframework.integration.support.PartialSuccessException) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) AbstractFileInfo(org.springframework.integration.file.remote.AbstractFileInfo)

Example 5 with PartialSuccessException

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."));
    }
}
Also used : PartialSuccessException(org.springframework.integration.support.PartialSuccessException) FTPFile(org.apache.commons.net.ftp.FTPFile) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

PartialSuccessException (org.springframework.integration.support.PartialSuccessException)6 File (java.io.File)4 IOException (java.io.IOException)4 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 FTPFile (org.apache.commons.net.ftp.FTPFile)3 Test (org.junit.Test)3 MessageHandlingException (org.springframework.messaging.MessageHandlingException)3 MessagingException (org.springframework.messaging.MessagingException)3 List (java.util.List)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 AbstractFileInfo (org.springframework.integration.file.remote.AbstractFileInfo)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1