1
2
3
4
5
6
7
8
9
10 package org.mule.module.ibeans.spi.support;
11
12 import org.mule.api.MuleContext;
13 import org.mule.api.MuleEvent;
14 import org.mule.api.MuleException;
15 import org.mule.api.MuleMessage;
16 import org.mule.api.annotations.param.Payload;
17 import org.mule.api.endpoint.EndpointBuilder;
18 import org.mule.api.endpoint.EndpointURI;
19 import org.mule.api.endpoint.MalformedEndpointException;
20 import org.mule.api.transport.PropertyScope;
21 import org.mule.config.endpoint.AnnotatedEndpointData;
22 import org.mule.endpoint.MuleEndpointURI;
23 import org.mule.transport.AbstractConnector;
24 import org.mule.transport.service.TransportFactory;
25 import org.mule.util.BeanUtils;
26 import org.mule.util.TemplateParser;
27 import org.mule.util.UriParamFilter;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.ibeans.annotation.Call;
35 import org.ibeans.annotation.param.HeaderParam;
36 import org.ibeans.api.channel.CHANNEL;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class CallOutboundEndpoint extends org.mule.endpoint.DynamicOutboundEndpoint
53 {
54 public static final String NULL_PARAM = "null.param";
55
56
57
58
59 protected transient final Log logger = LogFactory.getLog(CallOutboundEndpoint.class);
60
61 private static final long serialVersionUID = 1861985949279708638L;
62
63
64 protected TemplateParser parser = TemplateParser.createCurlyBracesStyleParser();
65
66 private UriParamFilter filter = new UriParamFilter();
67
68 public CallOutboundEndpoint(MuleContext context, AnnotatedEndpointData epData) throws MalformedEndpointException
69 {
70 super(createBuilder(context, epData), epData.getAddress());
71 }
72
73 private synchronized static EndpointBuilder createBuilder(MuleContext context, AnnotatedEndpointData epData)
74 {
75 try
76 {
77 String address = epData.getAddress();
78 int i = address.indexOf(":/");
79 String scheme;
80 if (i > -1)
81 {
82 scheme = address.substring(0, i);
83 address = scheme + "://dynamic";
84
85 EndpointURI tempUri = new MuleEndpointURI(address, context);
86 AbstractConnector cnn = null;
87
88 if (epData.getConnectorName() != null)
89 {
90 cnn = (AbstractConnector) context.getRegistry().lookupConnector(epData.getConnectorName());
91 }
92 if (cnn == null)
93 {
94 cnn = (AbstractConnector) new TransportFactory(context).createConnector(tempUri);
95 if (epData.getConnectorName() != null)
96 {
97 cnn.setName(epData.getConnectorName());
98 }
99 context.getRegistry().registerConnector(cnn);
100 }
101
102
103 Map props = epData.getProperties();
104 if (props == null)
105 {
106 props = new HashMap();
107 }
108 else
109 {
110 BeanUtils.populateWithoutFail(cnn, props, false);
111 }
112 EndpointBuilder builder = context.getEndpointFactory().getEndpointBuilder(address);
113 builder.setConnector(cnn);
114 builder.setName(epData.getName());
115 builder.setProperties(props);
116
117 return builder;
118
119
120 }
121 else
122 {
123 throw new IllegalArgumentException("When defining a dynamic endpoint the endpoint scheme must be set i.e. http://{dynamic}");
124 }
125 }
126 catch (Exception e)
127 {
128 throw new RuntimeException(e);
129 }
130 }
131
132 @Override
133 protected void validateUriTemplate(String uri) throws MalformedEndpointException
134 {
135
136 }
137
138 @Override
139 protected String parseURIString(String uri, MuleMessage message)
140 {
141
142 Map<String, Object> props = getPropertiesForUriTemplate(message);
143
144 String newUriString = parser.parse(props, uri);
145
146 newUriString = filter.filterParamsByValue(newUriString, NULL_PARAM);
147
148 return super.parseURIString(newUriString, message);
149 }
150
151 protected Map<String, Object> getPropertiesForUriTemplate(MuleMessage message)
152 {
153 Map<String, Object> props = (Map) message.getOutboundProperty(CHANNEL.URI_PARAM_PROPERTIES);
154 if (props == null)
155 {
156 throw new IllegalStateException(CHANNEL.URI_PARAM_PROPERTIES + " not set on message");
157 }
158 return props;
159 }
160
161 @Override
162 public MuleEvent process(MuleEvent event) throws MuleException
163 {
164 MuleEvent result = super.process(event);
165 if (result != null)
166 {
167 result.getMessage().setProperty(CHANNEL.CALL_URI_PROPERTY, result.getMessageSourceURI().toString(), PropertyScope.OUTBOUND);
168 }
169 return result;
170 }
171 }