1
2
3
4
5
6
7
8
9
10
11 package org.mule.providers.soap;
12
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.impl.MuleMessage;
15 import org.mule.impl.UMODescriptorAware;
16 import org.mule.impl.endpoint.MuleEndpoint;
17 import org.mule.providers.NullPayload;
18 import org.mule.umo.UMODescriptor;
19 import org.mule.umo.UMOEventContext;
20 import org.mule.umo.UMOMessage;
21 import org.mule.umo.endpoint.UMOEndpoint;
22 import org.mule.umo.lifecycle.Callable;
23 import org.mule.umo.lifecycle.Initialisable;
24 import org.mule.umo.lifecycle.InitialisationException;
25 import org.mule.umo.routing.UMOOutboundRouter;
26 import org.mule.util.StringUtils;
27 import org.mule.util.IOUtils;
28
29 import java.io.IOException;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class WSProxyService implements Callable, UMODescriptorAware, Initialisable
55 {
56
57 private String urlWebservice;
58 private String wsdlEndpoint;
59 private String wsdlFile;
60 private String wsdlFileContents;
61 private boolean useFile = false;
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 protected static transient Log logger = LogFactory.getLog(WSProxyService.class);
68
69
70
71
72 public String getWsdlEndpoint()
73 {
74 return wsdlEndpoint;
75 }
76
77
78
79
80
81 public void setWsdlEndpoint(String urlWsdl)
82 {
83 this.wsdlEndpoint = urlWsdl;
84 }
85
86
87
88
89 public String getWsdlFile()
90 {
91 return wsdlFile;
92 }
93
94
95
96
97 public void setWsdlFile(String wsdlFile)
98 {
99 this.wsdlFile = wsdlFile;
100 }
101
102 public Object onCall(UMOEventContext eventContext) throws Exception
103 {
104
105 UMOMessage message = eventContext.getMessage();
106
107
108
109 String httpRequest = ((String)message.getProperty(HTTP_REQUEST)).toLowerCase();
110
111
112 if ((httpRequest.indexOf(WSDL_PARAM_1) != -1) || (httpRequest.indexOf(WSDL_PARAM_2) != -1))
113 {
114 logger.debug("Retrieving WSDL from web service");
115
116 String wsdlString;
117
118 if (this.useFile)
119 {
120
121
122 eventContext.setStopFurtherProcessing(true);
123 return wsdlFileContents;
124 }
125
126 UMOEndpoint webServiceEndpoint = new MuleEndpoint(this.wsdlEndpoint, false);
127 UMOMessage replyWSDL = eventContext.sendEvent(new MuleMessage(NullPayload.getInstance()), webServiceEndpoint);
128
129 wsdlString = replyWSDL.getPayloadAsString();
130
131
132 wsdlString = wsdlString.replaceAll(this.urlWebservice, eventContext.getEndpointURI().getAddress());
133
134
135 MuleMessage modifiedWsdl = new MuleMessage(wsdlString, message);
136
137 logger.debug("WSDL retrieved successfully");
138
139
140
141 eventContext.setStopFurtherProcessing(true);
142
143 return modifiedWsdl;
144 }
145 else
146
147 {
148 logger.debug("Forwarding SOAP message");
149 return eventContext.getMessage();
150 }
151 }
152
153
154 public void setDescriptor(UMODescriptor descriptor)
155 {
156 UMOOutboundRouter router = (UMOOutboundRouter)descriptor.getOutboundRouter().getRouters().get(0);
157 UMOEndpoint endpoint = (UMOEndpoint)router.getEndpoints().get(0);
158 this.urlWebservice = endpoint.getEndpointURI().getAddress();
159
160
161 int paramIndex;
162 if ((paramIndex = this.urlWebservice.indexOf("?")) != -1)
163 {
164 this.urlWebservice = this.urlWebservice.substring(0, paramIndex);
165 }
166 }
167
168 public void initialise() throws InitialisationException
169 {
170
171 if (StringUtils.isNotBlank(this.wsdlFile))
172 {
173 try
174 {
175 this.wsdlFileContents = IOUtils.getResourceAsString(this.wsdlFile, getClass());
176
177 if (StringUtils.isNotBlank(this.wsdlFileContents))
178 {
179 this.useFile = true;
180 logger.info("Using file " + this.wsdlFile + " as WSDL file");
181 }
182 }
183 catch (IOException fileError)
184 {
185 throw new InitialisationException(CoreMessages.failedToLoad(this.wsdlFile), this);
186 }
187 }
188
189 if (!this.useFile)
190 {
191
192
193 if (StringUtils.isBlank(this.wsdlEndpoint))
194 {
195 this.wsdlEndpoint = this.urlWebservice.concat("?WSDL");
196 logger.info("Defaulting to: " + this.wsdlEndpoint);
197 }
198 else
199 {
200 logger.info("Using url " + this.wsdlEndpoint + " as WSDL");
201 }
202 }
203
204 }
205 }