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