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 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 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 private boolean lazyInit = true;
69
70 protected static transient Log logger = LogFactory.getLog(WSProxyService.class);
71
72
73
74
75 public String getWsdlEndpoint()
76 {
77 return wsdlEndpoint;
78 }
79
80
81
82
83
84 public void setWsdlEndpoint(String urlWsdl)
85 {
86 this.wsdlEndpoint = urlWsdl;
87 }
88
89
90
91
92 public String getWsdlFile()
93 {
94 return wsdlFile;
95 }
96
97
98
99
100 public void setWsdlFile(String wsdlFile)
101 {
102 this.wsdlFile = wsdlFile;
103 }
104
105 public Object onCall(MuleEventContext eventContext) throws Exception
106 {
107 if (wsdlEndpoint == null && lazyInit)
108 {
109 initialise();
110 }
111
112
113 MuleMessage message = eventContext.getMessage();
114
115
116
117 String httpRequest = message.<String>getInboundProperty(HTTP_REQUEST).toLowerCase();
118
119
120 if ((httpRequest.indexOf(WSDL_PARAM_1) != -1) || (httpRequest.indexOf(WSDL_PARAM_2) != -1))
121 {
122 logger.debug("Retrieving WSDL from web service");
123
124 String wsdlString;
125
126 if (this.useFile)
127 {
128
129
130 eventContext.setStopFurtherProcessing(true);
131 return wsdlFileContents;
132 }
133 MuleContext muleContext = eventContext.getMuleContext();
134 InboundEndpoint webServiceEndpoint =
135 muleContext.getEndpointFactory().getInboundEndpoint(this.wsdlEndpoint);
136
137 MuleMessage replyWSDL = eventContext.requestEvent(webServiceEndpoint, eventContext.getTimeout());
138
139 wsdlString = replyWSDL.getPayloadAsString();
140
141
142 String realWSDLURI = wsdlEndpoint.split("\\?")[0];
143 String proxyWSDLURI = eventContext.getEndpointURI().toString();
144
145 wsdlString = wsdlString.replaceAll(realWSDLURI, proxyWSDLURI);
146 if (wsdlString.indexOf("localhost") > -1)
147 {
148 wsdlString = wsdlString.replaceAll("localhost", InetAddress.getLocalHost().getHostName());
149 }
150
151 DefaultMuleMessage modifiedWsdl = new DefaultMuleMessage(wsdlString, muleContext);
152 logger.debug("WSDL retrieved successfully");
153
154
155
156 eventContext.setStopFurtherProcessing(true);
157
158 return modifiedWsdl;
159 }
160 else
161
162 {
163 logger.debug("Forwarding SOAP message");
164 return eventContext.getMessage().getPayload();
165 }
166 }
167
168
169 public void setService(Service service)
170 {
171 this.service = service;
172 }
173
174 public void initialise() throws InitialisationException
175 {
176 if (service != null)
177 {
178 OutboundRouter router = null;
179 if (service.getOutboundMessageProcessor() instanceof OutboundRouterCollection)
180 {
181 router = (OutboundRouter) ((OutboundRouterCollection) service.getOutboundMessageProcessor()).getRoutes()
182 .get(0);
183 }
184 else if (service.getOutboundMessageProcessor() instanceof OutboundRouter)
185 {
186 router = (OutboundRouter) service.getOutboundMessageProcessor();
187 }
188 else
189 {
190 throw new IllegalStateException(
191 "WSProxyService is only supported when using an OutboundRouter");
192 }
193 ImmutableEndpoint endpoint = (ImmutableEndpoint) router.getRoutes().get(0);
194 this.urlWebservice = endpoint.getEndpointURI().getAddress();
195
196
197 int paramIndex;
198 if ((paramIndex = this.urlWebservice.indexOf("?")) != -1)
199 {
200 this.urlWebservice = this.urlWebservice.substring(0, paramIndex);
201 }
202
203
204 if (StringUtils.isNotBlank(this.wsdlFile))
205 {
206 try
207 {
208 this.wsdlFileContents = IOUtils.getResourceAsString(this.wsdlFile, getClass());
209
210 if (StringUtils.isNotBlank(this.wsdlFileContents))
211 {
212 this.useFile = true;
213 logger.info("Using file " + this.wsdlFile + " as WSDL file");
214 }
215 }
216 catch (IOException fileError)
217 {
218 throw new InitialisationException(CoreMessages.failedToLoad(this.wsdlFile), this);
219 }
220 }
221
222 if (!this.useFile)
223 {
224
225
226 if (StringUtils.isBlank(this.wsdlEndpoint))
227 {
228 if (urlWebservice == null)
229 {
230 throw new InitialisationException(MessageFactory.createStaticMessage("urlWebservice has not been set, service has not been initialized properly"), this);
231 }
232 this.wsdlEndpoint = this.urlWebservice.concat("?wsdl");
233 logger.info("Defaulting to: " + this.wsdlEndpoint);
234 }
235 else
236 {
237 logger.info("Using url " + this.wsdlEndpoint + " as WSDL");
238 }
239 }
240 }
241 else if (!lazyInit)
242 {
243
244 logger.debug("Service has not yet been injected, lazy initialization will be used.");
245 lazyInit = true;
246 }
247 else
248 {
249
250 throw new InitialisationException(MessageFactory.createStaticMessage("Service not set, this service has not been initialized properly."), this);
251 }
252 }
253 }