View Javadoc

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