View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.spring.parsers.specific;
8   
9   import org.mule.api.processor.MessageProcessor;
10  import org.mule.api.processor.MessageProcessorBuilder;
11  import org.mule.config.spring.factories.MessageProcessorChainFactoryBean;
12  import org.mule.config.spring.factories.ResponseMessageProcessorsFactoryBean;
13  import org.mule.config.spring.parsers.delegate.ParentContextDefinitionParser;
14  import org.mule.config.spring.parsers.generic.ChildDefinitionParser;
15  import org.mule.processor.chain.DefaultMessageProcessorChainBuilder;
16  
17  public class ResponseDefinitionParser extends ParentContextDefinitionParser
18  {
19  
20      public ResponseDefinitionParser()
21      {
22          super("endpoint", new ChildDefinitionParser("responseMessageProcessor",
23              EndpointResponseMessageProcessorChainFactoryBean.class));
24          and("inbound-endpoint", new ChildDefinitionParser("responseMessageProcessor",
25              EndpointResponseMessageProcessorChainFactoryBean.class));
26          and("outbound-endpoint", new ChildDefinitionParser("responseMessageProcessor",
27              EndpointResponseMessageProcessorChainFactoryBean.class));
28          otherwise(new ChildDefinitionParser("messageProcessor", ResponseMessageProcessorsFactoryBean.class));
29      }
30  
31      // This is used to avoid additional wrapping of endpoint response message processor which shouldn't even be wrapped to start with
32      // The optimal solution is to have response message processors contained inside the <response> element injected into their grandparent element directly.
33      private static class EndpointResponseMessageProcessorChainFactoryBean extends MessageProcessorChainFactoryBean
34      {
35          public Object getObject() throws Exception
36          {
37              DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
38              builder.setName("processor chain '" + name + "'");
39              for (Object processor : processors)
40              {
41                  if (processor instanceof MessageProcessor)
42                  {
43                      builder.chain((MessageProcessor) processor);
44                  }
45                  else if (processor instanceof MessageProcessorBuilder)
46                  {
47                      builder.chain((MessageProcessorBuilder) processor);
48                  }
49                  else
50                  {
51                      throw new IllegalArgumentException(
52                          "MessageProcessorBuilder should only have MessageProcessor's or MessageProcessorBuilder's configured");
53                  }
54              }
55              return builder.build();
56          }
57      }
58  
59  }