1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package uk.ac.soton.itinnovation.wsdlutils;
51
52 import java.net.URI;
53 import java.util.ArrayList;
54 import java.util.HashMap;
55 import java.util.Iterator;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Vector;
59
60 import javax.wsdl.Binding;
61 import javax.wsdl.Message;
62 import javax.wsdl.Operation;
63 import javax.wsdl.Port;
64 import javax.wsdl.Service;
65 import javax.wsdl.extensions.soap.SOAPAddress;
66 import javax.xml.namespace.QName;
67
68 import org.apache.axis.client.Call;
69 import org.apache.axis.EngineConfiguration;
70 import org.apache.axis.client.AxisClient;
71 import org.apache.axis.message.SOAPHeaderElement;
72 import org.apache.axis.configuration.FileProvider;
73 import org.apache.axis.wsdl.gen.Parser;
74 import org.apache.axis.wsdl.symbolTable.BindingEntry;
75 import org.apache.axis.wsdl.symbolTable.Parameter;
76 import org.apache.axis.wsdl.symbolTable.Parameters;
77 import org.apache.axis.wsdl.symbolTable.ServiceEntry;
78 import org.apache.axis.wsdl.symbolTable.SymTabEntry;
79 import org.apache.axis.wsdl.symbolTable.SymbolTable;
80 import org.w3c.dom.Node;
81
82
83
84
85
86
87
88 public class WSDLService {
89
90 public static boolean DEBUG = false;
91
92
93 private Parser wsdlParser = null;
94 private URI uri = null;
95
96 private Map operations = new HashMap();
97
98 private EngineConfiguration config=null;
99 private AxisClient engine=null;
100
101
102 public WSDLService(URI wsdlURL) throws Exception {
103 this(wsdlURL
104 }
105
106 public WSDLService(URI wsdlURL
107 uri = wsdlURL;
108
109
110 if (conf!=null)setConfiguration(conf);
111
112
113 wsdlParser = new Parser();
114 if(DEBUG) wsdlParser.setVerbose(true);
115 String strURL=wsdlURL.toString();
116 if (strURL.endsWith("?wsdl")); else strURL=strURL+"?wsdl";
117 wsdlParser.run(strURL);
118
119 if(DEBUG) System.out.println("Reading operations");
120 readOperations();
121 }
122
123 public void setConfiguration(String confFile){
124 if (confFile!=null){
125 config=new FileProvider(confFile);
126 engine=new AxisClient(config);
127 }
128 }
129
130
131
132
133
134
135 static public WSDLService createService(String wsdlLoc) throws Exception {
136 return createService(URI.create(wsdlLoc),null);
137 }
138 static public WSDLService createService(String wsdlLoc, String conf) throws Exception {
139 return createService(URI.create(wsdlLoc),conf);
140 }
141
142 static public WSDLService createService(URI wsdlLoc, String conf) throws Exception {
143 return new WSDLService(wsdlLoc,conf);
144 }
145
146 private String createURI(QName qname) {
147 return qname.getNamespaceURI() + "#" + qname.getLocalPart();
148 }
149
150 private String createURI(String localName) {
151 return uri + "#" + localName;
152 }
153
154 private void readOperations() {
155 try {
156 String serviceNS = null;
157 String serviceName = null;
158 String operationName = null;
159 String portName = null;
160
161 Service service = selectService(serviceNS, serviceName);
162 org.apache.axis.client.Service dpf =
163 new org.apache.axis.client.Service(wsdlParser, service.getQName());
164 if (engine!=null){
165 dpf.setEngine(engine);
166 }
167
168 Port port = selectPort(service.getPorts(), portName);
169 if (portName == null) {
170 portName = port.getName();
171 }
172 Binding binding = port.getBinding();
173
174 SymbolTable symbolTable = wsdlParser.getSymbolTable();
175 BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
176 Parameters parameters = null;
177 Iterator i = bEntry.getParameters().keySet().iterator();
178
179 while (i.hasNext()) {
180 Operation o = (Operation) i.next();
181 operationName = o.getName();
182
183 Call call = (Call) dpf.createCall(QName.valueOf(portName), QName.valueOf(operationName));
184
185
186
187
188
189 WSDLOperation op = new WSDLOperation(call);
190 op.setService(this);
191
192 operations.put(operationName, op);
193
194 Message inputMessage = o.getInput().getMessage();
195 Message outputMessage = o.getOutput().getMessage();
196
197 op.setOperationName(createURI(operationName));
198 op.setInputMessageName(createURI(inputMessage.getQName()));
199 op.setOutputMessageName(createURI(outputMessage.getQName()));
200 op.setPortName(createURI(port.getName()));
201
202 if(DEBUG) {
203 System.out.println(" Operation : " + operationName+", "+ call.getOperationName());
204 System.out.println(" Port : " + portName + " -> " + op.getPortName());
205 System.out.println(" Input Msg : " + inputMessage.getQName() + " -> " + op.getInputMessageName());
206 System.out.println(" Output Msg: " + outputMessage.getQName() + " -> " + op.getOutputMessageName());
207 }
208
209
210 if(o.getDocumentationElement() != null) {
211 Node doc = o.getDocumentationElement().getFirstChild();
212 if(doc != null) op.setDocumentation(doc.getNodeValue());
213 }
214
215 parameters = (Parameters) bEntry.getParameters().get(o);
216
217
218 for (int j = 0; j < parameters.list.size(); ++j) {
219 Parameter p = (Parameter) parameters.list.get(j);
220 String name = createURI(p.getName());
221 QName type = p.getType().getQName();
222
223 if (p.getMode() == Parameter.IN) {
224 op.addInput(name, type);
225
226 if(DEBUG)
227 System.out.println(" Input : " + name + " " + type);
228 } else if (p.getMode() == Parameter.OUT) {
229 op.addOutput(name, type);
230
231 if(DEBUG)
232 System.out.println(" Output : " + name + " " + type);
233 } else if (p.getMode() == Parameter.INOUT) {
234 op.addInput(name, type);
235 op.addOutput(name, type);
236 System.err.println("WARNING: A wsdl parameter is defined as INOUT is not tested yet");
237 System.err.println(" Parameter = " + name);
238
239 if(DEBUG)
240 System.out.println(" InOut : " + name + " " + type);
241 }
242
243 }
244
245
246 if (parameters.returnParam != null) {
247 Parameter p = parameters.returnParam;
248 String name = createURI(p.getName());
249 QName type = p.getType().getQName();
250
251 op.addOutput(name, type);
252
253 if(DEBUG)
254 System.out.println(" Return : " + name + " " + type);
255 }
256
257 if(DEBUG) {
258 System.out.println(" Inputs : " + op.getInputs().size());
259 System.out.println(" Outputs : " + op.getOutputs().size());
260 System.out.println(" Document : " + op.getDocumentation());
261 System.out.println();
262 }
263 }
264
265 } catch(Exception e) {
266 System.err.println(e);
267 e.printStackTrace();
268 }
269 }
270
271 private Service selectService(String serviceNS, String serviceName)
272 throws Exception {
273 QName serviceQName = (((serviceNS != null)
274 && (serviceName != null))
275 ? new QName(serviceNS, serviceName)
276 : null);
277 ServiceEntry serviceEntry = (ServiceEntry) getSymTabEntry(serviceQName,
278 ServiceEntry.class);
279 return serviceEntry.getService();
280 }
281
282 private SymTabEntry getSymTabEntry(QName qname, Class cls) {
283 HashMap map = wsdlParser.getSymbolTable().getHashMap();
284 Iterator iterator = map.entrySet().iterator();
285
286 while (iterator.hasNext()) {
287 Map.Entry entry = (Map.Entry) iterator.next();
288 Vector v = (Vector) entry.getValue();
289
290 if ((qname == null) || qname.equals(qname)) {
291 for (int i = 0; i < v.size(); ++i) {
292 SymTabEntry symTabEntry = (SymTabEntry) v.elementAt(i);
293
294 if (cls.isInstance(symTabEntry)) {
295 return symTabEntry;
296 }
297 }
298 }
299 }
300 return null;
301 }
302
303 private Port selectPort(Map ports, String portName) throws Exception {
304 Iterator valueIterator = ports.keySet().iterator();
305 while (valueIterator.hasNext()) {
306 String name = (String) valueIterator.next();
307
308 if ((portName == null) || (portName.length() == 0)) {
309 Port port = (Port) ports.get(name);
310 List list = port.getExtensibilityElements();
311
312 for (int i = 0; (list != null) && (i < list.size()); i++) {
313 Object obj = list.get(i);
314 if (obj instanceof SOAPAddress) {
315 return port;
316 }
317 }
318 } else if ((name != null) && name.equals(portName)) {
319 return (Port) ports.get(name);
320 }
321 }
322 return null;
323 }
324
325 public URI getFileURI() {
326 return uri;
327 }
328
329 public List getOperations() {
330 return new ArrayList(operations.values());
331 }
332
333 public WSDLOperation getOperation(String opName) {
334 return (WSDLOperation) operations.get(opName);
335 }
336 }