KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > bindings > WSDLOperationInfo


1 package org.objectweb.celtix.bus.bindings;
2
3 import java.lang.annotation.Annotation JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import javax.jws.WebParam;
12 import javax.jws.WebParam.Mode;
13 import javax.jws.WebResult;
14 import javax.jws.soap.SOAPBinding;
15 import javax.jws.soap.SOAPBinding.Style;
16 import javax.wsdl.BindingInput;
17 import javax.wsdl.BindingOperation;
18 import javax.wsdl.BindingOutput;
19 import javax.wsdl.Input;
20 import javax.wsdl.Message;
21 import javax.wsdl.Operation;
22 import javax.wsdl.Output;
23 import javax.wsdl.Part;
24 import javax.wsdl.extensions.soap.SOAPBody;
25 import javax.wsdl.extensions.soap.SOAPHeader;
26 import javax.wsdl.extensions.soap.SOAPOperation;
27 import javax.xml.namespace.QName JavaDoc;
28
29
30 public class WSDLOperationInfo {
31     BindingOperation bindingOp;
32     WSDLMetaDataCache cache;
33     
34     SOAPBinding.Style style;
35     SOAPBinding.Use use;
36     SOAPBinding.ParameterStyle paramStyle;
37     String JavaDoc soapAction;
38     String JavaDoc targetNs;
39     QName JavaDoc requestWrapperQName;
40     QName JavaDoc responseWrapperQName;
41     WebResult result;
42     List JavaDoc<OperationWebParam> params;
43     
44     public WSDLOperationInfo(WSDLMetaDataCache c, BindingOperation op) {
45         cache = c;
46         bindingOp = op;
47     }
48     
49     public String JavaDoc getName() {
50         return bindingOp.getName();
51     }
52     
53     public boolean isOneWay() {
54         return getMessage(true) != null && getMessage(false) == null;
55     }
56     
57     public SOAPBinding.Style getSOAPStyle() {
58         if (style == null) {
59             SOAPOperation soapOperation = getExtensibilityElement(bindingOp.getExtensibilityElements(),
60                                                                   SOAPOperation.class);
61             if (soapOperation != null
62                 && soapOperation.getStyle() != null
63                 && !"".equals(soapOperation.getStyle())) {
64                 style = SOAPBinding.Style.valueOf(soapOperation.getStyle().toUpperCase());
65             }
66             if (style == null) {
67                 style = cache.getStyle();
68             }
69         }
70         return style;
71     }
72     
73     public SOAPBinding.Use getSOAPUse() {
74         if (use == null) {
75             SOAPBody body = getSOAPBody(true);
76             
77             if (body != null
78                 && body.getUse() != null
79                 && !"".equals(body.getUse())) {
80                 use = SOAPBinding.Use.valueOf(body.getUse().toUpperCase());
81             }
82             if (use == null) {
83                 use = SOAPBinding.Use.LITERAL;
84             }
85         }
86         return use;
87     }
88     
89     public String JavaDoc getOperationName() {
90         return getName();
91     }
92     
93     public String JavaDoc getSOAPAction() {
94         if (soapAction == null) {
95             SOAPOperation soapOp = getExtensibilityElement(bindingOp.getExtensibilityElements(),
96                                                            SOAPOperation.class);
97             if (soapOp != null) {
98                 soapAction = soapOp.getSoapActionURI();
99             }
100             if (soapAction == null) {
101                 soapAction = "";
102             }
103         }
104         return soapAction;
105     }
106     public String JavaDoc getTargetNamespace() {
107         if (targetNs == null) {
108             SOAPBody soapBody = getSOAPBody(true);
109             if (soapBody != null) {
110                 targetNs = soapBody.getNamespaceURI();
111             }
112             if (targetNs == null) {
113                 targetNs = cache.getTargetNamespace();
114             }
115         }
116         return targetNs;
117     }
118     
119     
120     
121     public SOAPBinding.ParameterStyle getSOAPParameterStyle() {
122         if (paramStyle == null) {
123             if (isDocLitWrapped() || getSOAPStyle() == SOAPBinding.Style.RPC) {
124                 paramStyle = SOAPBinding.ParameterStyle.WRAPPED;
125             } else {
126                 paramStyle = SOAPBinding.ParameterStyle.BARE;
127             }
128         }
129         return paramStyle;
130     }
131     
132     public WebResult getWebResult() {
133         if (params == null) {
134             initParams();
135         }
136         return result;
137     }
138     public WebParam getWebParam(int index) {
139         if (params == null) {
140             initParams();
141         }
142         return params.get(index);
143     }
144     public int getParamsLength() {
145         if (params == null) {
146             initParams();
147         }
148         return params.size();
149     }
150     
151     public QName JavaDoc getWebResultQName() {
152         WebResult res = getWebResult();
153         if (null != res) {
154             if (getSOAPStyle() == Style.DOCUMENT) {
155                 if ("".equals(res.name())) {
156                     return new QName JavaDoc(res.targetNamespace(),
157                             "return");
158                 }
159                 return new QName JavaDoc(res.targetNamespace(),
160                                  res.name());
161             } else {
162                 return new QName JavaDoc("", res.partName());
163             }
164         }
165         return new QName JavaDoc("", "");
166     }
167
168     public QName JavaDoc getRequestWrapperQName() {
169         if (requestWrapperQName == null) {
170             if (getSOAPParameterStyle() == SOAPBinding.ParameterStyle.BARE
171                 || getSOAPStyle() == SOAPBinding.Style.RPC) {
172                 requestWrapperQName = new QName JavaDoc("", "");
173             } else {
174                 Message msg = getMessage(true);
175                 List JavaDoc parts = msg.getOrderedParts(null);
176                 Part part = (Part)parts.get(0);
177                 requestWrapperQName = part.getElementName();
178             }
179         }
180         return requestWrapperQName;
181     }
182     public QName JavaDoc getResponseWrapperQName() {
183         if (responseWrapperQName == null) {
184             if (getSOAPParameterStyle() == SOAPBinding.ParameterStyle.BARE
185                 || getSOAPStyle() == SOAPBinding.Style.RPC) {
186                 responseWrapperQName = new QName JavaDoc("", "");
187             } else {
188                 Message msg = getMessage(false);
189                 if (msg != null) {
190                     List JavaDoc parts = msg.getOrderedParts(null);
191                     Part part = (Part)parts.get(0);
192                     responseWrapperQName = part.getElementName();
193                 } else {
194                     responseWrapperQName = new QName JavaDoc("", "");
195                 }
196             }
197         }
198         return responseWrapperQName;
199     }
200     
201     
202     
203     private boolean isDocLitWrapped() {
204         boolean flag = getSOAPStyle() == SOAPBinding.Style.DOCUMENT
205             && getSOAPUse() == SOAPBinding.Use.LITERAL;
206         if (!flag) {
207             return false;
208         }
209         Message msg = getMessage(true);
210         if (msg == null) {
211             return false;
212         }
213         List JavaDoc parts = msg.getOrderedParts(null);
214         if (parts.size() != 1) {
215             return false;
216         }
217         Part part = (Part)parts.get(0);
218         QName JavaDoc element = part.getElementName();
219         if (element == null
220             || !element.getLocalPart().equals(bindingOp.getOperation().getName())) {
221             return false;
222         }
223         //REVISIT check for squence
224

225         msg = getMessage(true);
226         if (msg != null) {
227             parts = msg.getOrderedParts(null);
228             if (parts.size() != 1) {
229                 flag = false;
230             } else {
231                 part = (Part)parts.get(0);
232                 element = part.getElementName();
233                 if (element == null
234                     || !element.getLocalPart().startsWith(bindingOp.getOperation().getName())) {
235                     flag = false;
236                 }
237                 //REVISIT check for squence
238
}
239         }
240         return true;
241     }
242     private Message getMessage(boolean isInput) {
243         Operation operation = bindingOp.getOperation();
244         if (operation == null) {
245             return null;
246         }
247
248         if (isInput) {
249             final Input input = operation.getInput();
250             return input == null ? null : input.getMessage();
251         }
252         final Output output = operation.getOutput();
253         return output == null ? null : output.getMessage();
254     }
255     private SOAPBody getSOAPBody(boolean input) {
256         List JavaDoc elements = null;
257         if (input) {
258             BindingInput bindingInput = bindingOp.getBindingInput();
259             if (bindingInput == null) {
260                 return null;
261             }
262             elements = bindingInput.getExtensibilityElements();
263         } else {
264             BindingOutput bindingOutput = bindingOp.getBindingOutput();
265             if (bindingOutput == null) {
266                 return null;
267             }
268             elements = bindingOutput.getExtensibilityElements();
269         }
270         return getExtensibilityElement(elements, SOAPBody.class);
271     }
272     
273     private void initRPCLitParam(List JavaDoc<OperationWebParam> parms,
274                                  Map JavaDoc<String JavaDoc, OperationWebParam> parmMap,
275                                  Operation operation) {
276         
277         Input input = operation.getInput();
278         Output output = operation.getOutput();
279         if (input == null) {
280             //unsupported op type, output only
281
return;
282         }
283         Collection JavaDoc parts = input.getMessage().getParts().values();
284         for (Iterator JavaDoc i = parts.iterator(); i.hasNext();) {
285             Part part = (Part)i.next();
286             OperationWebParam p = new OperationWebParam(part.getName(),
287                                                         part.getName(),
288                                                         Mode.IN,
289                                                         "",
290                                                         cache.getTargetNamespace());
291             parms.add(p);
292             parmMap.put(part.getName(), p);
293         }
294         if (output != null) {
295             parts = output.getMessage().getParts().values();
296             for (Iterator JavaDoc i = parts.iterator(); i.hasNext();) {
297                 Part part = (Part)i.next();
298                 
299                 OperationWebParam p = parmMap.get(part.getName());
300                 if (p == null) {
301                     p = new OperationWebParam(part.getName(),
302                                               part.getName(),
303                                               Mode.OUT,
304                                               "",
305                                               cache.getTargetNamespace());
306                     parms.add(p);
307                     parmMap.put(part.getName(), p);
308                 } else {
309                     p.setMode(Mode.INOUT);
310                 }
311             }
312         }
313     }
314                                      
315     private void initDocLitBareParam(List JavaDoc<OperationWebParam> parms,
316                                      Map JavaDoc<String JavaDoc, OperationWebParam> parmMap,
317                                      Operation operation) {
318         
319         Input input = operation.getInput();
320         Output output = operation.getOutput();
321         Collection JavaDoc parts = input.getMessage().getParts().values();
322         for (Iterator JavaDoc i = parts.iterator(); i.hasNext();) {
323             Part part = (Part)i.next();
324             OperationWebParam p = new OperationWebParam(part.getElementName().getLocalPart(),
325                                                         part.getName(),
326                                                         Mode.IN,
327                                                         part.getElementName().getNamespaceURI());
328             parms.add(p);
329             parmMap.put(part.getName(), p);
330         }
331         if (output != null) {
332             parts = output.getMessage().getParts().values();
333             for (Iterator JavaDoc i = parts.iterator(); i.hasNext();) {
334                 Part part = (Part)i.next();
335                 
336                 OperationWebParam p = parmMap.get(part.getName());
337                 if (p == null) {
338                     p = new OperationWebParam(part.getElementName().getLocalPart(),
339                                               part.getName(),
340                                               Mode.OUT,
341                                               part.getElementName().getNamespaceURI());
342                     parms.add(p);
343                     parmMap.put(part.getName(), p);
344                 } else {
345                     p.setMode(Mode.INOUT);
346                 }
347             }
348         }
349     }
350     private void initDocLitWrappedParam(List JavaDoc<OperationWebParam> parms,
351                                      Map JavaDoc<String JavaDoc, OperationWebParam> parmMap,
352                                      Operation operation) {
353         //REVISIT - how to handle the wrapped docs/lit cases. Must dig into schema and stuff.
354
}
355
356     private synchronized void initParams() {
357         List JavaDoc<OperationWebParam> parms = new ArrayList JavaDoc<OperationWebParam>();
358         Map JavaDoc<String JavaDoc, OperationWebParam> parmMap = new HashMap JavaDoc<String JavaDoc, OperationWebParam>();
359         
360         Operation operation = bindingOp.getOperation();
361         if (operation != null) {
362             SOAPBinding.Style st = getSOAPStyle();
363             
364             if (st == SOAPBinding.Style.RPC) {
365                 initRPCLitParam(parms, parmMap, operation);
366             } else {
367                 // DOC style
368
if (isDocLitWrapped()) {
369                     initDocLitWrappedParam(parms, parmMap, operation);
370                 } else {
371                     initDocLitBareParam(parms, parmMap, operation);
372                 }
373             }
374
375             // Set the header flags
376
BindingInput bindingInput = bindingOp.getBindingInput();
377             if (bindingInput != null) {
378                 javax.wsdl.Message message = operation.getInput().getMessage();
379                 List JavaDoc elements = bindingInput.getExtensibilityElements();
380                 for (Iterator JavaDoc i = elements.iterator(); i.hasNext();) {
381                     Object JavaDoc extensibilityElement = i.next();
382                     Part part = getPartFromSOAPHeader(message, extensibilityElement);
383                     if (part != null) {
384                         OperationWebParam p = parmMap.get(part.getName());
385                         if (p != null) {
386                             p.setHeader(true);
387                         }
388                     }
389                 }
390             }
391             BindingOutput bindingOutput = bindingOp.getBindingOutput();
392             if (bindingOutput != null) {
393                 javax.wsdl.Message message = operation.getOutput().getMessage();
394                 List JavaDoc elements = bindingOutput.getExtensibilityElements();
395                 for (Iterator JavaDoc i = elements.iterator(); i.hasNext();) {
396                     Object JavaDoc extensibilityElement = i.next();
397                     Part part = getPartFromSOAPHeader(message, extensibilityElement);
398                     if (part != null) {
399                         OperationWebParam p = parmMap.get(part.getName());
400                         if (p != null) {
401                             p.setHeader(true);
402                         }
403                     }
404                 }
405             }
406         }
407         
408         OperationWebParam returnVal = null;
409         for (OperationWebParam p : parms) {
410             if (p.mode() == Mode.INOUT) {
411                 break;
412             } else if (p.mode() == Mode.OUT) {
413                 returnVal = p;
414                 break;
415             }
416         }
417         if (returnVal != null) {
418             parms.remove(returnVal);
419             result = new OperationWebResult(returnVal);
420         }
421         
422         params = parms;
423     }
424  
425     private Part getPartFromSOAPHeader(Message message, Object JavaDoc extensibilityElement) {
426         Part part = null;
427         if (extensibilityElement instanceof SOAPHeader) {
428             SOAPHeader soapHeader = (SOAPHeader)extensibilityElement;
429             QName JavaDoc msgName = soapHeader.getMessage();
430             if (message.getQName().equals(msgName)) {
431                 part = message.getPart(soapHeader.getPart());
432             }
433         } else if (extensibilityElement instanceof SOAPHeader) {
434             SOAPHeader soapHeader = (SOAPHeader)extensibilityElement;
435             QName JavaDoc msgName = soapHeader.getMessage();
436             if (message.getQName().equals(msgName)) {
437                 part = message.getPart(soapHeader.getPart());
438             }
439         }
440         return part;
441     }
442     private static <T> T getExtensibilityElement(List JavaDoc elements, Class JavaDoc<T> type) {
443         for (Iterator JavaDoc i = elements.iterator(); i.hasNext();) {
444             Object JavaDoc element = i.next();
445             if (type.isInstance(element)) {
446                 return type.cast(element);
447             }
448         }
449         return null;
450     }
451     
452     private class OperationWebResult implements WebResult {
453         OperationWebParam parm;
454         
455         public OperationWebResult(OperationWebParam p) {
456             parm = p;
457         }
458
459         public String JavaDoc name() {
460             return parm.name();
461         }
462
463         public String JavaDoc targetNamespace() {
464             return parm.webResultTargetNamespace();
465         }
466
467         public boolean header() {
468             return parm.header();
469         }
470
471         public String JavaDoc partName() {
472             return parm.partName();
473         }
474
475         public Class JavaDoc<? extends Annotation JavaDoc> annotationType() {
476             return WebResult.class;
477         }
478     }
479     
480     private class OperationWebParam implements WebParam {
481         Mode md;
482         String JavaDoc name;
483         String JavaDoc targetNs;
484         String JavaDoc webResultTNS;
485         boolean isHeader;
486         String JavaDoc partname;
487         
488         public OperationWebParam(String JavaDoc n, String JavaDoc pn, Mode m, String JavaDoc ns) {
489             this(n, pn, m, ns, ns);
490         }
491         public OperationWebParam(String JavaDoc n, String JavaDoc pn, Mode m, String JavaDoc ns, String JavaDoc wrns) {
492             name = n;
493             md = m;
494             targetNs = ns;
495             partname = pn;
496             webResultTNS = wrns;
497         }
498
499         public String JavaDoc name() {
500             return name;
501         }
502
503         public Mode mode() {
504             return md;
505         }
506         public void setMode(Mode m) {
507             md = m;
508         }
509
510
511         public String JavaDoc targetNamespace() {
512             return targetNs;
513         }
514         public String JavaDoc webResultTargetNamespace() {
515             return webResultTNS;
516         }
517
518         public boolean header() {
519             return isHeader;
520         }
521         public void setHeader(boolean b) {
522             isHeader = b;
523         }
524
525         public String JavaDoc partName() {
526             return partname;
527         }
528
529         public Class JavaDoc<? extends Annotation JavaDoc> annotationType() {
530             return WebParam.class;
531         }
532         
533     }
534 }
535
Popular Tags