View Javadoc

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