Search in sources :

Example 1 with DefaultFileNameGenerator

use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.

the class FileTransferringMessageHandlerSpec method fileNameExpression.

/**
 * Set the {@link DefaultFileNameGenerator} based on the provided SpEL expression.
 * @param fileNameGeneratorExpression the SpEL expression for file names generation.
 * @return the current Spec
 */
public S fileNameExpression(String fileNameGeneratorExpression) {
    Assert.isNull(this.fileNameGenerator, "'fileNameGenerator' and 'fileNameGeneratorExpression' are mutually exclusive.");
    this.defaultFileNameGenerator = new DefaultFileNameGenerator();
    this.defaultFileNameGenerator.setExpression(fileNameGeneratorExpression);
    return fileNameGenerator(this.defaultFileNameGenerator);
}
Also used : DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator)

Example 2 with DefaultFileNameGenerator

use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.

the class SftpOutboundTests method testHandleBytesMessage.

@Test
public void testHandleBytesMessage() throws Exception {
    File file = new File("remote-target-dir", "foo.txt");
    if (file.exists()) {
        file.delete();
    }
    SessionFactory<LsEntry> sessionFactory = new TestSftpSessionFactory();
    FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory);
    DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator();
    fGenerator.setBeanFactory(mock(BeanFactory.class));
    fGenerator.setExpression("'foo.txt'");
    handler.setFileNameGenerator(fGenerator);
    handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    handler.handleMessage(new GenericMessage<byte[]>("byte[] data".getBytes()));
    assertTrue(new File("remote-target-dir", "foo.txt").exists());
    byte[] inFile = FileCopyUtils.copyToByteArray(file);
    assertEquals("byte[] data", new String(inFile));
    file.delete();
}
Also used : FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Example 3 with DefaultFileNameGenerator

use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.

the class SftpOutboundTests method testHandleStringMessage.

@Test
public void testHandleStringMessage() throws Exception {
    File file = new File("remote-target-dir", "foo.txt");
    if (file.exists()) {
        file.delete();
    }
    SessionFactory<LsEntry> sessionFactory = new TestSftpSessionFactory();
    FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<>(sessionFactory);
    DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator();
    fGenerator.setBeanFactory(mock(BeanFactory.class));
    fGenerator.setExpression("'foo.txt'");
    handler.setFileNameGenerator(fGenerator);
    handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    handler.handleMessage(new GenericMessage<String>("String data"));
    assertTrue(new File("remote-target-dir", "foo.txt").exists());
    byte[] inFile = FileCopyUtils.copyToByteArray(file);
    assertEquals("String data", new String(inFile));
    file.delete();
}
Also used : FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Example 4 with DefaultFileNameGenerator

use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.

the class FileTests method testFileHandler.

@Test
public void testFileHandler() throws Exception {
    Message<?> message = MessageBuilder.withPayload("foo").setHeader(FileHeaders.FILENAME, "foo").build();
    try {
        this.fileFlow1Input.send(message);
        fail("NullPointerException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageHandlingException.class));
        assertThat(e.getCause(), instanceOf(NullPointerException.class));
    }
    DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
    fileNameGenerator.setBeanFactory(this.beanFactory);
    Object targetFileWritingMessageHandler = this.fileWritingMessageHandler;
    if (this.fileWritingMessageHandler instanceof Advised) {
        TargetSource targetSource = ((Advised) this.fileWritingMessageHandler).getTargetSource();
        if (targetSource != null) {
            targetFileWritingMessageHandler = targetSource.getTarget();
        }
    }
    DirectFieldAccessor dfa = new DirectFieldAccessor(targetFileWritingMessageHandler);
    assertEquals(Boolean.FALSE, dfa.getPropertyValue("flushWhenIdle"));
    assertEquals(60000L, dfa.getPropertyValue("flushInterval"));
    dfa.setPropertyValue("fileNameGenerator", fileNameGenerator);
    this.fileFlow1Input.send(message);
    assertTrue(new File(tmpDir.getRoot(), "foo").exists());
    this.fileTriggerFlowInput.send(new GenericMessage<>("trigger"));
    assertTrue(this.flushPredicateCalled.await(10, TimeUnit.SECONDS));
}
Also used : TargetSource(org.springframework.aop.TargetSource) Advised(org.springframework.aop.framework.Advised) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) File(java.io.File) MessageHandlingException(org.springframework.messaging.MessageHandlingException) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Example 5 with DefaultFileNameGenerator

use of org.springframework.integration.file.DefaultFileNameGenerator in project spring-integration by spring-projects.

the class FileOutboundGatewayParserTests method checkOrderedGateway.

@Test
public void checkOrderedGateway() throws Exception {
    DirectFieldAccessor gatewayAccessor = new DirectFieldAccessor(ordered);
    FileWritingMessageHandler handler = (FileWritingMessageHandler) gatewayAccessor.getPropertyValue("handler");
    assertEquals(Boolean.FALSE, gatewayAccessor.getPropertyValue("autoStartup"));
    DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
    assertEquals(777, handlerAccessor.getPropertyValue("order"));
    assertEquals(Boolean.TRUE, handlerAccessor.getPropertyValue("requiresReply"));
    DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator");
    assertNotNull(fileNameGenerator);
    Expression expression = TestUtils.getPropertyValue(fileNameGenerator, "expression", Expression.class);
    assertNotNull(expression);
    assertEquals("'foo.txt'", expression.getExpressionString());
    Long sendTimeout = TestUtils.getPropertyValue(handler, "messagingTemplate.sendTimeout", Long.class);
    assertEquals(Long.valueOf(777), sendTimeout);
}
Also used : Expression(org.springframework.expression.Expression) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) FileWritingMessageHandler(org.springframework.integration.file.FileWritingMessageHandler) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Aggregations

DefaultFileNameGenerator (org.springframework.integration.file.DefaultFileNameGenerator)11 Test (org.junit.Test)9 File (java.io.File)6 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 FileTransferringMessageHandler (org.springframework.integration.file.remote.handler.FileTransferringMessageHandler)3 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Bean (org.springframework.context.annotation.Bean)2 Configuration (org.springframework.context.annotation.Configuration)2 Expression (org.springframework.expression.Expression)2 FileWritingMessageHandler (org.springframework.integration.file.FileWritingMessageHandler)2 ClientCallbackWithoutResult (org.springframework.integration.file.remote.ClientCallbackWithoutResult)2 SessionCallbackWithoutResult (org.springframework.integration.file.remote.SessionCallbackWithoutResult)2