Search in sources :

Example 51 with DirectChannel

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

the class HttpRequestHandlingMessagingGatewayWithPathMappingTests method withoutPayloadExpressionPointingToUriVariables.

@SuppressWarnings("unchecked")
@Test
public void withoutPayloadExpressionPointingToUriVariables() throws Exception {
    DirectChannel echoChannel = new DirectChannel();
    echoChannel.subscribe(message -> {
        MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
        replyChannel.send(message);
    });
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setMethod("POST");
    request.setContentType("text/plain");
    request.setParameter("foo", "bar");
    request.setContent("hello".getBytes());
    String requestURI = "/fname/bill/lname/clinton";
    // See org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleMatch
    Map<String, String> uriTemplateVariables = new AntPathMatcher().extractUriTemplateVariables("/fname/{f}/lname/{l}", requestURI);
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
    request.setRequestURI(requestURI);
    HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    gateway.setBeanFactory(mock(BeanFactory.class));
    RequestMapping requestMapping = new RequestMapping();
    requestMapping.setPathPatterns("/fname/{f}/lname/{l}");
    gateway.setRequestMapping(requestMapping);
    gateway.setRequestChannel(echoChannel);
    gateway.setPayloadExpression(PARSER.parseExpression("#pathVariables"));
    gateway.afterPropertiesSet();
    gateway.start();
    Object result = gateway.doHandleRequest(request, response);
    assertThat(result, instanceOf(Message.class));
    assertEquals("bill", ((Map<String, Object>) ((Message<?>) result).getPayload()).get("f"));
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) AntPathMatcher(org.springframework.util.AntPathMatcher) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 52 with DirectChannel

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

the class HttpRequestHandlingMessagingGatewayWithPathMappingTests method withoutExpression.

@Test
public void withoutExpression() throws Exception {
    DirectChannel echoChannel = new DirectChannel();
    echoChannel.subscribe(message -> {
        MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
        replyChannel.send(message);
    });
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("text/plain");
    request.setParameter("foo", "bar");
    request.setContent("hello".getBytes());
    request.setRequestURI("/fname/bill/lname/clinton");
    HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    gateway.setBeanFactory(mock(BeanFactory.class));
    RequestMapping requestMapping = new RequestMapping();
    requestMapping.setPathPatterns("/fname/{f}/lname/{l}");
    gateway.setRequestMapping(requestMapping);
    gateway.afterPropertiesSet();
    gateway.start();
    gateway.setRequestChannel(echoChannel);
    MockHttpServletResponse response = new MockHttpServletResponse();
    Object result = gateway.doHandleRequest(request, response);
    assertThat(result, instanceOf(Message.class));
    assertEquals("hello", ((Message<?>) result).getPayload());
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 53 with DirectChannel

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

the class HttpRequestHandlingMessagingGatewayTests method INT2680DuplicateContentTypeHeader.

@Test
public void INT2680DuplicateContentTypeHeader() throws Exception {
    final DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return MessageBuilder.withPayload("Cartman".getBytes()).setHeader("Content-type", "text/plain").build();
        }
    });
    final List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
    supportedMediaTypes.add(MediaType.TEXT_HTML);
    final ByteArrayHttpMessageConverter messageConverter = new ByteArrayHttpMessageConverter();
    messageConverter.setSupportedMediaTypes(supportedMediaTypes);
    final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(messageConverter);
    final HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setMessageConverters(messageConverters);
    gateway.setRequestChannel(requestChannel);
    gateway.start();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    final ContentTypeCheckingMockHttpServletResponse response = new ContentTypeCheckingMockHttpServletResponse();
    gateway.handleRequest(request, response);
    assertEquals("Cartman", response.getContentAsString());
    /* Before fixing INT2680, 2 content type headers were being written. */
    final List<String> contentTypes = response.getContentTypeList();
    assertEquals("Exptecting only 1 content type being set.", Integer.valueOf(1), Integer.valueOf(contentTypes.size()));
    assertEquals("text/plain", contentTypes.get(0));
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) AbstractHttpMessageConverter(org.springframework.http.converter.AbstractHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 54 with DirectChannel

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

the class HttpRequestHandlingMessagingGatewayTests method timeoutErrorFlow.

@Test
public void timeoutErrorFlow() throws Exception {
    QueueChannel requestChannel = new QueueChannel();
    HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setRequestChannel(requestChannel);
    gateway.setReplyTimeout(0);
    DirectChannel errorChannel = new DirectChannel();
    errorChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new GenericMessage<String>("foo", Collections.<String, Object>singletonMap(HttpHeaders.STATUS_CODE, HttpStatus.GATEWAY_TIMEOUT));
        }
    });
    gateway.setErrorChannel(errorChannel);
    gateway.afterPropertiesSet();
    gateway.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    MockHttpServletResponse response = new MockHttpServletResponse();
    gateway.handleRequest(request, response);
    Message<?> message = requestChannel.receive(0);
    assertNotNull(message);
    assertEquals(504, response.getStatus());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 55 with DirectChannel

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

the class IntegrationGraphControllerTests method testIntegrationGraphControllerParser.

@Test
public void testIntegrationGraphControllerParser() throws Exception {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("IntegrationGraphControllerParserTests-context.xml", getClass());
    HandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class.getName(), HandlerMapping.class);
    HandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/foo");
    request.addHeader(HttpHeaders.ORIGIN, "http://foo.bar.com");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HandlerExecutionChain executionChain = handlerMapping.getHandler(request);
    assertNotNull(executionChain);
    Object handler = executionChain.getHandler();
    for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
        // Assert the CORS config
        assertTrue(handlerInterceptor.preHandle(request, response, handler));
    }
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpStatus.OK.value(), response.getStatus());
    assertThat(response.getContentAsString(), containsString("\"name\":\"nullChannel\","));
    assertThat(response.getContentAsString(), not(containsString("\"name\":\"myChannel\",")));
    context.getBeanFactory().registerSingleton("myChannel", new DirectChannel());
    request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/foo/refresh");
    response = new MockHttpServletResponse();
    executionChain = handlerMapping.getHandler(request);
    assertNotNull(executionChain);
    handler = executionChain.getHandler();
    for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
        // Assert the CORS config
        assertTrue(handlerInterceptor.preHandle(request, response, handler));
    }
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpStatus.OK.value(), response.getStatus());
    assertThat(response.getContentAsString(), containsString("\"name\":\"myChannel\","));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) HandlerAdapter(org.springframework.web.servlet.HandlerAdapter) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

DirectChannel (org.springframework.integration.channel.DirectChannel)215 Test (org.junit.Test)182 MessageChannel (org.springframework.messaging.MessageChannel)71 Message (org.springframework.messaging.Message)68 QueueChannel (org.springframework.integration.channel.QueueChannel)63 BeanFactory (org.springframework.beans.factory.BeanFactory)45 GenericMessage (org.springframework.messaging.support.GenericMessage)38 MessageHandler (org.springframework.messaging.MessageHandler)32 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)28 CountDownLatch (java.util.concurrent.CountDownLatch)27 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)26 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)25 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)23 HashMap (java.util.HashMap)22 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)22 MessagingException (org.springframework.messaging.MessagingException)18 Properties (java.util.Properties)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 SubscribableChannel (org.springframework.messaging.SubscribableChannel)15 Expression (org.springframework.expression.Expression)14