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.module.ibeans.spi.support;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.endpoint.EndpointBuilder;
13  import org.mule.api.endpoint.MalformedEndpointException;
14  import org.mule.api.transformer.Transformer;
15  import org.mule.config.endpoint.AnnotatedEndpointData;
16  import org.mule.config.i18n.CoreMessages;
17  import org.mule.transport.AbstractConnector;
18  import org.mule.util.UriParamFilter;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.ibeans.api.channel.CHANNEL;
28  
29  /**
30   * A dynamic inbound endpoint used for request calls defined using the {@link org.ibeans.annotation.Call} annotation.
31   * Note that call requests look the same as normal call endpoints except request calls do not define any headers or payload.
32   * <p/>
33   * The endpoint scheme is the only part of the URI that cannot be replaced at runtime.
34   *
35   * @see CallOutboundEndpoint
36   */
37  public class CallRequestEndpoint extends DynamicRequestEndpoint
38  {
39      /**
40       * logger used by this class
41       */
42      protected transient final Log logger = LogFactory.getLog(CallRequestEndpoint.class);
43  
44      private static final long serialVersionUID = 1861985949279708458L;
45  
46      //This is a hack to create a ref to the transformers collection, then we add the transformers once the endpoint type has been
47      //determined
48      private static List<Transformer> transformers = new ArrayList<Transformer>();
49      private static List<Transformer> responseTransformers = new ArrayList<Transformer>();
50      
51      private UriParamFilter filter = new UriParamFilter();
52  
53      public CallRequestEndpoint(MuleContext context, AnnotatedEndpointData epData) throws MalformedEndpointException
54      {
55          super( context, createInboundBuilder(context, epData), epData.getAddress());
56      }
57  
58      @Override
59      protected void validateUriTemplate(String uri) throws MalformedEndpointException
60      {
61          if (uri.indexOf(parser.getStyle().getPrefix()) > -1 && uri.indexOf(":") > uri.indexOf(parser.getStyle().getPrefix()))
62          {
63              throw new MalformedEndpointException(CoreMessages.dynamicEndpointsMustSpecifyAScheme(), uri);
64          }
65      }
66  
67      private static EndpointBuilder createInboundBuilder(MuleContext context, AnnotatedEndpointData epData)
68      {
69          try
70          {
71              EndpointBuilder builder = context.getEndpointFactory().getEndpointBuilder("dynamic://null");
72              builder.setExchangePattern(epData.getMep());
73              builder.setConnector(epData.getConnector());
74              builder.setName(epData.getName());
75              builder.setProperties(epData.getProperties() == null ? new HashMap() : epData.getProperties());
76  
77              return builder;
78          }
79          catch (MuleException e)
80          {
81              throw new RuntimeException(e);
82          }
83      }
84  
85      @Override
86      protected String parseURIString(String uri, MuleMessage message)
87      {
88          //We do additional processing here to parse the URI template
89          Map<String, Object> props = getPropertiesForTemplate(message);
90  
91          String newUriString = parser.parse(props, uri);
92          //Remove optional params completely if null
93          newUriString = filter.filterParamsByValue(newUriString, CallOutboundEndpoint.NULL_PARAM);
94  
95          return super.parseURIString(newUriString, message);
96      }
97  
98      @Override
99      protected Map<String, Object> getPropertiesForTemplate(MuleMessage message)
100     {
101         Map<String, Object> props = (Map) message.findPropertyInAnyScope(CHANNEL.URI_PARAM_PROPERTIES, null);
102         if (props == null)
103         {
104             throw new IllegalStateException(CHANNEL.URI_PARAM_PROPERTIES + " not set on message");
105         }
106         return props;
107     }
108 
109     @Override
110     public List getTransformers()
111     {
112         if (transformers.size() == 0)
113         {
114             try
115             {
116                 transformers.addAll(((AbstractConnector)getConnector()).getDefaultInboundTransformers(this));
117                 for (Transformer tran : transformers)
118                 {
119                     tran.setEndpoint(this);
120                     tran.setMuleContext(getMuleContext());
121                     tran.initialise();
122                 }
123             }
124             catch (MuleException e)
125             {
126                 throw new RuntimeException(e);
127             }
128         }
129         return transformers;
130     }
131 
132     @Override
133     public List getResponseTransformers()
134     {
135         if (responseTransformers.size() == 0)
136         {
137             try
138             {
139                 responseTransformers.addAll(((AbstractConnector)getConnector()).getDefaultResponseTransformers(this));
140                 for (Transformer tran : responseTransformers)
141                 {
142                     tran.setEndpoint(this);
143                     tran.setMuleContext(getMuleContext());
144                     tran.initialise();
145                 }
146             }
147             catch (MuleException e)
148             {
149                 throw new RuntimeException(e);
150             }
151         }
152         return transformers;
153     }
154 }