1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.cxf; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.api.MuleContext; |
11 | |
import org.mule.api.MuleEventContext; |
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
14 | |
import org.mule.api.endpoint.InboundEndpoint; |
15 | |
import org.mule.api.lifecycle.Callable; |
16 | |
import org.mule.api.lifecycle.Initialisable; |
17 | |
import org.mule.api.lifecycle.InitialisationException; |
18 | |
import org.mule.api.routing.OutboundRouter; |
19 | |
import org.mule.api.routing.OutboundRouterCollection; |
20 | |
import org.mule.api.service.Service; |
21 | |
import org.mule.api.service.ServiceAware; |
22 | |
import org.mule.config.i18n.CoreMessages; |
23 | |
import org.mule.config.i18n.MessageFactory; |
24 | |
import org.mule.util.IOUtils; |
25 | |
import org.mule.util.StringUtils; |
26 | |
|
27 | |
import java.io.IOException; |
28 | |
import java.net.InetAddress; |
29 | |
|
30 | |
import org.apache.commons.logging.Log; |
31 | |
import org.apache.commons.logging.LogFactory; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class WSProxyService implements Callable, ServiceAware, Initialisable |
53 | |
{ |
54 | |
|
55 | |
private String urlWebservice; |
56 | |
private String wsdlEndpoint; |
57 | |
private String wsdlFile; |
58 | |
private String wsdlFileContents; |
59 | 0 | private boolean useFile = false; |
60 | |
|
61 | |
private Service service; |
62 | |
|
63 | |
private static final String HTTP_REQUEST = "http.request"; |
64 | |
private static final String WSDL_PARAM_1 = "?wsdl"; |
65 | |
private static final String WSDL_PARAM_2 = "&wsdl"; |
66 | |
|
67 | |
|
68 | 0 | private boolean lazyInit = true; |
69 | |
|
70 | 0 | protected static transient Log logger = LogFactory.getLog(WSProxyService.class); |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public String getWsdlEndpoint() |
76 | |
{ |
77 | 0 | return wsdlEndpoint; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public void setWsdlEndpoint(String urlWsdl) |
85 | |
{ |
86 | 0 | this.wsdlEndpoint = urlWsdl; |
87 | 0 | } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public String getWsdlFile() |
93 | |
{ |
94 | 0 | return wsdlFile; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public void setWsdlFile(String wsdlFile) |
101 | |
{ |
102 | 0 | this.wsdlFile = wsdlFile; |
103 | 0 | } |
104 | |
|
105 | |
public Object onCall(MuleEventContext eventContext) throws Exception |
106 | |
{ |
107 | 0 | if (wsdlEndpoint == null && lazyInit) |
108 | |
{ |
109 | 0 | initialise(); |
110 | |
} |
111 | |
|
112 | |
|
113 | 0 | MuleMessage message = eventContext.getMessage(); |
114 | |
|
115 | |
|
116 | |
|
117 | 0 | String httpRequest = message.<String>getInboundProperty(HTTP_REQUEST).toLowerCase(); |
118 | |
|
119 | |
|
120 | 0 | if ((httpRequest.indexOf(WSDL_PARAM_1) != -1) || (httpRequest.indexOf(WSDL_PARAM_2) != -1)) |
121 | |
{ |
122 | 0 | logger.debug("Retrieving WSDL from web service"); |
123 | |
|
124 | |
String wsdlString; |
125 | |
|
126 | 0 | if (this.useFile) |
127 | |
{ |
128 | |
|
129 | |
|
130 | 0 | eventContext.setStopFurtherProcessing(true); |
131 | 0 | return wsdlFileContents; |
132 | |
} |
133 | 0 | MuleContext muleContext = eventContext.getMuleContext(); |
134 | 0 | InboundEndpoint webServiceEndpoint = |
135 | |
muleContext.getEndpointFactory().getInboundEndpoint(this.wsdlEndpoint); |
136 | |
|
137 | 0 | MuleMessage replyWSDL = eventContext.requestEvent(webServiceEndpoint, eventContext.getTimeout()); |
138 | |
|
139 | 0 | wsdlString = replyWSDL.getPayloadAsString(); |
140 | |
|
141 | |
|
142 | 0 | String realWSDLURI = wsdlEndpoint.split("\\?")[0]; |
143 | 0 | String proxyWSDLURI = eventContext.getEndpointURI().toString(); |
144 | |
|
145 | 0 | wsdlString = wsdlString.replaceAll(realWSDLURI, proxyWSDLURI); |
146 | 0 | if (wsdlString.indexOf("localhost") > -1) |
147 | |
{ |
148 | 0 | wsdlString = wsdlString.replaceAll("localhost", InetAddress.getLocalHost().getHostName()); |
149 | |
} |
150 | |
|
151 | 0 | DefaultMuleMessage modifiedWsdl = new DefaultMuleMessage(wsdlString, muleContext); |
152 | 0 | logger.debug("WSDL retrieved successfully"); |
153 | |
|
154 | |
|
155 | |
|
156 | 0 | eventContext.setStopFurtherProcessing(true); |
157 | |
|
158 | 0 | return modifiedWsdl; |
159 | |
} |
160 | |
else |
161 | |
|
162 | |
{ |
163 | 0 | logger.debug("Forwarding SOAP message"); |
164 | 0 | return eventContext.getMessage().getPayload(); |
165 | |
} |
166 | |
} |
167 | |
|
168 | |
|
169 | |
public void setService(Service service) |
170 | |
{ |
171 | 0 | this.service = service; |
172 | 0 | } |
173 | |
|
174 | |
public void initialise() throws InitialisationException |
175 | |
{ |
176 | 0 | if (service != null) |
177 | |
{ |
178 | 0 | OutboundRouter router = null; |
179 | 0 | if (service.getOutboundMessageProcessor() instanceof OutboundRouterCollection) |
180 | |
{ |
181 | 0 | router = (OutboundRouter) ((OutboundRouterCollection) service.getOutboundMessageProcessor()).getRoutes() |
182 | |
.get(0); |
183 | |
} |
184 | 0 | else if (service.getOutboundMessageProcessor() instanceof OutboundRouter) |
185 | |
{ |
186 | 0 | router = (OutboundRouter) service.getOutboundMessageProcessor(); |
187 | |
} |
188 | |
else |
189 | |
{ |
190 | 0 | throw new IllegalStateException( |
191 | |
"WSProxyService is only supported when using an OutboundRouter"); |
192 | |
} |
193 | 0 | ImmutableEndpoint endpoint = (ImmutableEndpoint) router.getRoutes().get(0); |
194 | 0 | this.urlWebservice = endpoint.getEndpointURI().getAddress(); |
195 | |
|
196 | |
|
197 | |
int paramIndex; |
198 | 0 | if ((paramIndex = this.urlWebservice.indexOf("?")) != -1) |
199 | |
{ |
200 | 0 | this.urlWebservice = this.urlWebservice.substring(0, paramIndex); |
201 | |
} |
202 | |
|
203 | |
|
204 | 0 | if (StringUtils.isNotBlank(this.wsdlFile)) |
205 | |
{ |
206 | |
try |
207 | |
{ |
208 | 0 | this.wsdlFileContents = IOUtils.getResourceAsString(this.wsdlFile, getClass()); |
209 | |
|
210 | 0 | if (StringUtils.isNotBlank(this.wsdlFileContents)) |
211 | |
{ |
212 | 0 | this.useFile = true; |
213 | 0 | logger.info("Using file " + this.wsdlFile + " as WSDL file"); |
214 | |
} |
215 | |
} |
216 | 0 | catch (IOException fileError) |
217 | |
{ |
218 | 0 | throw new InitialisationException(CoreMessages.failedToLoad(this.wsdlFile), this); |
219 | 0 | } |
220 | |
} |
221 | |
|
222 | 0 | if (!this.useFile) |
223 | |
{ |
224 | |
|
225 | |
|
226 | 0 | if (StringUtils.isBlank(this.wsdlEndpoint)) |
227 | |
{ |
228 | 0 | if (urlWebservice == null) |
229 | |
{ |
230 | 0 | throw new InitialisationException(MessageFactory.createStaticMessage("urlWebservice has not been set, service has not been initialized properly"), this); |
231 | |
} |
232 | 0 | this.wsdlEndpoint = this.urlWebservice.concat("?wsdl"); |
233 | 0 | logger.info("Defaulting to: " + this.wsdlEndpoint); |
234 | |
} |
235 | |
else |
236 | |
{ |
237 | 0 | logger.info("Using url " + this.wsdlEndpoint + " as WSDL"); |
238 | |
} |
239 | |
} |
240 | 0 | } |
241 | 0 | else if (!lazyInit) |
242 | |
{ |
243 | |
|
244 | 0 | logger.debug("Service has not yet been injected, lazy initialization will be used."); |
245 | 0 | lazyInit = true; |
246 | |
} |
247 | |
else |
248 | |
{ |
249 | |
|
250 | 0 | throw new InitialisationException(MessageFactory.createStaticMessage("Service not set, this service has not been initialized properly."), this); |
251 | |
} |
252 | 0 | } |
253 | |
} |