Search in sources :

Example 61 with IntegrationMessageHeaderAccessor

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

the class ChannelSecurityInterceptorSecuredChannelAnnotationTests method testSecurityContextPropagationPublishSubscribeChannel.

@Test
public void testSecurityContextPropagationPublishSubscribeChannel() {
    login("bob", "bobspassword", "ROLE_ADMIN", "ROLE_PRESIDENT");
    this.publishSubscribeChannel.send(new GenericMessage<String>("test"));
    Message<?> receive = this.securedChannelQueue.receive(10000);
    assertNotNull(receive);
    IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(receive);
    assertEquals(0, headerAccessor.getSequenceNumber());
    receive = this.securedChannelQueue2.receive(10000);
    assertNotNull(receive);
    headerAccessor = new IntegrationMessageHeaderAccessor(receive);
    assertEquals(0, headerAccessor.getSequenceNumber());
    this.publishSubscribeChannel.setApplySequence(true);
    this.publishSubscribeChannel.send(new GenericMessage<String>("test"));
    receive = this.securedChannelQueue.receive(10000);
    assertNotNull(receive);
    headerAccessor = new IntegrationMessageHeaderAccessor(receive);
    assertEquals(1, headerAccessor.getSequenceNumber());
    receive = this.securedChannelQueue2.receive(10000);
    assertNotNull(receive);
    headerAccessor = new IntegrationMessageHeaderAccessor(receive);
    assertEquals(2, headerAccessor.getSequenceNumber());
    this.publishSubscribeChannel.setApplySequence(false);
    SecurityContextHolder.clearContext();
    this.publishSubscribeChannel.send(new GenericMessage<String>("test"));
    Message<?> errorMessage = this.errorChannel.receive(10000);
    assertNotNull(errorMessage);
    Object payload = errorMessage.getPayload();
    assertThat(payload, instanceOf(MessageHandlingException.class));
    assertThat(((MessageHandlingException) payload).getCause(), instanceOf(AuthenticationCredentialsNotFoundException.class));
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) AuthenticationCredentialsNotFoundException(org.springframework.security.authentication.AuthenticationCredentialsNotFoundException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 62 with IntegrationMessageHeaderAccessor

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

the class FtpTests method testFtpInboundStreamFlow.

@Test
public void testFtpInboundStreamFlow() throws Exception {
    QueueChannel out = new QueueChannel();
    StandardIntegrationFlow flow = IntegrationFlows.from(Ftp.inboundStreamingAdapter(new FtpRemoteFileTemplate(sessionFactory())).remoteDirectory("ftpSource").maxFetchSize(11).regexFilter(".*\\.txt$"), e -> e.id("ftpInboundAdapter").poller(Pollers.fixedDelay(100))).channel(out).get();
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    Message<?> message = out.receive(10_000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" ftpSource1.txt", "ftpSource2.txt"));
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();
    message = out.receive(10_000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" ftpSource1.txt", "ftpSource2.txt"));
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();
    MessageSource<?> source = context.getBean(FtpStreamingMessageSource.class);
    assertThat(TestUtils.getPropertyValue(source, "maxFetchSize"), equalTo(11));
    registration.destroy();
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) FtpRemoteFileTemplate(org.springframework.integration.ftp.session.FtpRemoteFileTemplate) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Test(org.junit.Test)

Example 63 with IntegrationMessageHeaderAccessor

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

the class TcpMessageMapperTests method testToMessageSequence.

@Test
public void testToMessageSequence() throws Exception {
    TcpMessageMapper mapper = new TcpMessageMapper();
    Socket socket = SocketFactory.getDefault().createSocket();
    TcpConnection connection = new TcpConnectionSupport(socket, false, false, null, null) {

        @Override
        public void run() {
        }

        @Override
        public void send(Message<?> message) throws Exception {
        }

        @Override
        public boolean isOpen() {
            return false;
        }

        @Override
        public int getPort() {
            return 1234;
        }

        @Override
        public Object getPayload() throws Exception {
            return TEST_PAYLOAD.getBytes();
        }

        @Override
        public String getHostName() {
            return "MyHost";
        }

        @Override
        public String getHostAddress() {
            return "1.1.1.1";
        }

        @Override
        public String getConnectionId() {
            return "anId";
        }

        @Override
        public Object getDeserializerStateKey() {
            return null;
        }

        @Override
        public SSLSession getSslSession() {
            return null;
        }
    };
    Message<?> message = mapper.toMessage(connection);
    assertEquals(TEST_PAYLOAD, new String((byte[]) message.getPayload()));
    assertEquals("MyHost", message.getHeaders().get(IpHeaders.HOSTNAME));
    assertEquals("1.1.1.1", message.getHeaders().get(IpHeaders.IP_ADDRESS));
    assertEquals(1234, message.getHeaders().get(IpHeaders.REMOTE_PORT));
    assertEquals(0, new IntegrationMessageHeaderAccessor(message).getSequenceNumber());
    message = mapper.toMessage(connection);
    assertEquals(TEST_PAYLOAD, new String((byte[]) message.getPayload()));
    assertEquals("MyHost", message.getHeaders().get(IpHeaders.HOSTNAME));
    assertEquals("1.1.1.1", message.getHeaders().get(IpHeaders.IP_ADDRESS));
    assertEquals(1234, message.getHeaders().get(IpHeaders.REMOTE_PORT));
    assertEquals(0, new IntegrationMessageHeaderAccessor(message).getSequenceNumber());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Message(org.springframework.messaging.Message) Matchers.containsString(org.hamcrest.Matchers.containsString) Socket(java.net.Socket) Test(org.junit.Test)

Example 64 with IntegrationMessageHeaderAccessor

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

the class RedisChannelPriorityMessageStore method addMessageToGroup.

@Override
public MessageGroup addMessageToGroup(Object groupId, Message<?> message) {
    Assert.isInstanceOf(String.class, groupId);
    String key = (String) groupId;
    Integer priority = new IntegrationMessageHeaderAccessor(message).getPriority();
    if (priority != null && priority < 10 && priority >= 0) {
        key = key + ":" + priority;
    }
    return super.addMessageToGroup(key, message);
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor)

Example 65 with IntegrationMessageHeaderAccessor

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

the class SftpTests method testSftpInboundStreamFlow.

@Test
public void testSftpInboundStreamFlow() throws Exception {
    QueueChannel out = new QueueChannel();
    StandardIntegrationFlow flow = IntegrationFlows.from(Sftp.inboundStreamingAdapter(new SftpRemoteFileTemplate(sessionFactory())).remoteDirectory("sftpSource").regexFilter(".*\\.txt$"), e -> e.id("sftpInboundAdapter").poller(Pollers.fixedDelay(100))).channel(out).get();
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    Message<?> message = out.receive(10_000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" sftpSource1.txt", "sftpSource2.txt"));
    ((InputStream) message.getPayload()).close();
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();
    message = out.receive(10_000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" sftpSource1.txt", "sftpSource2.txt"));
    ((InputStream) message.getPayload()).close();
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();
    registration.destroy();
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) InputStream(java.io.InputStream) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Test(org.junit.Test)

Aggregations

IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)68 Test (org.junit.Test)56 QueueChannel (org.springframework.integration.channel.QueueChannel)32 Message (org.springframework.messaging.Message)21 GenericMessage (org.springframework.messaging.support.GenericMessage)19 MessageChannel (org.springframework.messaging.MessageChannel)15 ArrayList (java.util.ArrayList)10 MessagingTemplate (org.springframework.integration.core.MessagingTemplate)9 DirectChannel (org.springframework.integration.channel.DirectChannel)8 ServiceActivatingHandler (org.springframework.integration.handler.ServiceActivatingHandler)6 List (java.util.List)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 MessageSequenceComparator (org.springframework.integration.aggregator.MessageSequenceComparator)4 Aggregator (org.springframework.integration.annotation.Aggregator)4 InputStream (java.io.InputStream)3 LinkedList (java.util.LinkedList)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)3 MessageGroup (org.springframework.integration.store.MessageGroup)3 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)3