KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > model > wsdl > XmlBeanWSDLProcessor


1 /*
2  * XmlBeanWSDLProcessor.java
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.beehive.wsm.model.wsdl;
20
21 import java.io.File JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Array JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.jws.WebParam;
35 import javax.jws.soap.SOAPBinding;
36 import javax.xml.namespace.QName JavaDoc;
37
38 import org.apache.beehive.wsm.databinding.BindingLookupService;
39 import org.apache.beehive.wsm.util.TypeMappingUtil;
40 import org.apache.beehive.wsm.util.XmlBeanTypeMappingUtil;
41 import org.apache.beehive.wsm.wsdl.WSDLProcessor;
42
43 import org.apache.beehive.wsm.model.BeehiveWsSOAPBindingInfo;
44 import org.apache.beehive.wsm.model.BeehiveWsMethodMetadata;
45 import org.apache.beehive.wsm.model.BeehiveWsParameterMetadata;
46 import org.apache.beehive.wsm.model.BeehiveWsTypeMetadata;
47 import org.apache.beehive.wsm.model.jsr181.Jsr181MethodMetadataImpl;
48 import org.apache.beehive.wsm.model.jsr181.Jsr181ParameterMetadataImpl;
49 import org.apache.beehive.wsm.model.jsr181.Jsr181TypeMetadataImpl;
50 import org.apache.beehive.wsm.model.jsr181.SOAPBindingInfo;
51 import org.apache.xmlbeans.SchemaType;
52 import org.apache.xmlbeans.SchemaTypeLoader;
53 import org.apache.xmlbeans.XmlBeans;
54 import org.apache.xmlbeans.XmlCursor;
55 import org.apache.xmlbeans.XmlException;
56 import org.apache.xmlbeans.XmlObject;
57 import org.xmlsoap.schemas.wsdl.DefinitionsDocument;
58 import org.xmlsoap.schemas.wsdl.TBinding;
59 import org.xmlsoap.schemas.wsdl.TBindingOperation;
60 import org.xmlsoap.schemas.wsdl.TBindingOperationMessage;
61 import org.xmlsoap.schemas.wsdl.TDefinitions;
62 import org.xmlsoap.schemas.wsdl.TMessage;
63 import org.xmlsoap.schemas.wsdl.TOperation;
64 import org.xmlsoap.schemas.wsdl.TParam;
65 import org.xmlsoap.schemas.wsdl.TPart;
66 import org.xmlsoap.schemas.wsdl.TPort;
67 import org.xmlsoap.schemas.wsdl.TPortType;
68 import org.xmlsoap.schemas.wsdl.TService;
69 import org.xmlsoap.schemas.wsdl.TTypes;
70 import org.xmlsoap.schemas.wsdl.soap.HeaderDocument;
71 import org.xmlsoap.schemas.wsdl.soap.TStyleChoice;
72 import org.xmlsoap.schemas.wsdl.soap.UseChoice;
73
74 import org.apache.xmlbeans.impl.xb.xsdschema.Element;
75 import org.apache.xmlbeans.impl.xb.xsdschema.Group;
76 import org.apache.xmlbeans.impl.xb.xsdschema.ComplexType;
77 import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
78 import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument.Schema;
79
80 /*******************************************************************************
81  * An Implementation of WSDLProcessor that uses XmlBeans to convert the WSDL
82  * into the WSM Object Model.
83  *
84  * @author Jonathan Colwell
85  */

86 public class XmlBeanWSDLProcessor {
87
88   private BindingLookupService lookupService;
89   DefinitionsDocument defDoc;
90
91
92
93     public QName JavaDoc getServiceName() {
94         TService[] services = defDoc.getDefinitions().getServiceArray();
95         return new QName JavaDoc(services[0].getName());
96     }
97
98   public static final String JavaDoc TRANSPORT = "http://schemas.xmlsoap.org/soap/http";
99
100   public static final String JavaDoc SOAPENCODING = "http://schemas.xmlsoap.org/soap/encoding/";
101
102   public static final String JavaDoc WSDLNS = "http://schemas.xmlsoap.org/wsdl/soap/";
103
104   public static final String JavaDoc WSDLSOAPNS = "http://schemas.xmlsoap.org/wsdl/soap/";
105
106   public BeehiveWsTypeMetadata getObjectModel(BindingLookupService lookupService)
107       throws Exception JavaDoc {
108     this.lookupService = lookupService;
109
110     BeehiveWsTypeMetadata wsm = new Jsr181TypeMetadataImpl();
111     Map JavaDoc<String JavaDoc, BeehiveWsMethodMetadata> methodMap = new HashMap JavaDoc<String JavaDoc, BeehiveWsMethodMetadata>();
112     TDefinitions defs = defDoc.getDefinitions();
113     wsm.setWsTargetNamespace(defs.getTargetNamespace());
114     Map JavaDoc<String JavaDoc, TPart[]> messageMap = new HashMap JavaDoc<String JavaDoc, TPart[]>();
115
116     if (defs.sizeOfServiceArray() > 0) {
117       wsm.setWsServiceName(defs.getServiceArray(0).getName());
118     }
119
120     if (defs.sizeOfMessageArray() > 0) {
121       TMessage[] messages = defs.getMessageArray();
122       for (TMessage msg : messages) {
123         messageMap.put(msg.getName(), msg.getPartArray());
124       }
125     }
126
127     TBinding tBind = null;
128     if (defs.sizeOfBindingArray() > 0) {
129       tBind = defs.getBindingArray(0);
130       processTBinding(tBind, wsm);
131     }
132
133     if (defs.sizeOfPortTypeArray() > 0) {
134       TPortType tP = defs.getPortTypeArray(0);
135       wsm.setWsName(tP.getName());
136       TOperation[] operations = tP.getOperationArray();
137       TTypes types = null;
138       if (defs.sizeOfTypesArray() > 0) {
139         types = defs.getTypesArray(0);
140       }
141
142       for (TOperation op : operations) {
143
144         processTOperation(op, wsm, methodMap, messageMap, tBind, types);
145       }
146     }
147
148     if (tBind != null) {
149       processTBindingOperation(tBind, wsm, methodMap);
150     }
151     return wsm;
152   }
153
154
155   private void processTOperation(TOperation op, BeehiveWsTypeMetadata wsm,
156       Map JavaDoc<String JavaDoc, BeehiveWsMethodMetadata> methodMap,
157       Map JavaDoc<String JavaDoc, TPart[]> messageMap, TBinding tBind, TTypes types) throws Exception JavaDoc {
158     String JavaDoc opName = op.getName();
159     TParam outputParam = op.getOutput();
160
161     Map JavaDoc<String JavaDoc, BeehiveWsParameterMetadata> outParamMap = null;
162
163     BeehiveWsParameterMetadata[] paraMeta = new BeehiveWsParameterMetadata[0]; // defualt value
164
if( null != outputParam ) {
165
166           TPart[] messageParts = messageMap.get(outputParam.getMessage()
167               .getLocalPart());
168                 paraMeta = processParameters(opName, messageParts, tBind, types,
169                         messageMap, wsm, true); // output parameters are not wrapped
170
}
171     Class JavaDoc returnType;
172     QName JavaDoc returnXMLType;
173
174     if (paraMeta == null) {
175       // Should not happen, may be need to send runtime exception, or
176
// define a new type of exception!
177
throw new Exception JavaDoc("Can't resolve the return type");
178     } else if (paraMeta.length == 1) {
179       returnType = paraMeta[0].getJavaType();
180       returnXMLType = paraMeta[0].getXmlType();
181     } else {
182       // either there is no return value or there are multiple so make
183
// this a
184
// void function and later set OUT parameters if needed.
185
returnType = Void.TYPE;
186       returnXMLType = null;
187       if (paraMeta.length > 1) {
188         outParamMap = new HashMap JavaDoc<String JavaDoc, BeehiveWsParameterMetadata>(
189             paraMeta.length);
190         boolean returnIsSet = false;
191         for (BeehiveWsParameterMetadata cpm : paraMeta) {
192           outParamMap.put(cpm.getWpName(), cpm);
193         }
194       }
195     }
196     BeehiveWsMethodMetadata wmm = new Jsr181MethodMetadataImpl(opName, returnType,
197         returnXMLType);
198
199     wmm.setWmOperationName(opName);
200     // FIXME jcolwell@bea.com 2004-Nov-10 --
201
// do something better with the action
202
wmm.setWmAction(opName);
203     if (Void.TYPE.equals(returnType)) {
204
205       if (paraMeta.length == 0) {
206         // FIXME jcolwell@bea.com 2004-Nov-22 --
207
// also check for faults before setting as oneway.
208
wmm.setOneWay(true);
209       }
210     } else {
211       wmm.setWrName(paraMeta[0].getWpName());
212       wmm.setWrTargetNamespace(paraMeta[0].getWpTargetNamespace());
213     }
214
215     methodMap.put(opName, wmm);
216
217     List JavaDoc paramOrder = op.getParameterOrder(); // this is the one used by
218
// rpc wsdls.
219
TParam inputParam = op.getInput();
220     if (inputParam != null) {
221
222               TPart[] messageParts = messageMap.get(inputParam.getMessage()
223                   .getLocalPart());
224               
225
226               BeehiveWsParameterMetadata[] params = processParameters(opName, messageParts, tBind, types,
227           messageMap, wsm, false);
228
229       if (paramOrder != null) {
230         // Paramorder is only used for rpc.
231
// if there is a paramoder then order the parameters, remove all the in/out parameters
232
// from the out map
233
for (Object JavaDoc ord : paramOrder) {
234           for (BeehiveWsParameterMetadata wpm : params) {
235             if (ord.equals(wpm.getWpName())) {
236                 if (outParamMap != null && outParamMap.containsKey(wpm.getWpName())) {
237                     outParamMap.remove(wpm.getWpName()); // important...
238
// if this param is in.out it was in the out list
239
// also, so removeit.
240
wpm.setWpMode(WebParam.Mode.INOUT);
241                   } else {
242                     wpm.setWpMode(WebParam.Mode.IN);
243                   }
244               wmm.addParam(wpm);
245               break;
246             }
247           }
248         }
249       } else if (params.length > 0) {
250         for (BeehiveWsParameterMetadata wpm : params) {
251           // FIXME jcolwell@bea.com 2005-Jan-04 --
252
// Double check DOC/Lit rules
253
if (outParamMap != null && outParamMap.containsKey(wpm.getWpName())) {
254             outParamMap.remove(wpm.getWpName()); // important...
255
// if
256
// this param is
257
// in.out it was
258
// in the out list
259
// also, so remove
260
// it.
261
wpm.setWpMode(WebParam.Mode.INOUT);
262           } else {
263             wpm.setWpMode(WebParam.Mode.IN);
264           }
265           wmm.addParam(wpm);
266         }
267       }
268     }
269
270     // do the pure out parameters.
271
if (outParamMap != null && outParamMap.size() > 0) {
272       if (outParamMap.size() == 1) {
273         BeehiveWsParameterMetadata wpm = outParamMap.values().iterator().next();
274         wmm.setXmlReturnType(wpm.getXmlType());
275         wmm.setReturnType(wpm.getJavaType());
276         wmm.setOneWay(false);
277       } else {
278         for (BeehiveWsParameterMetadata wpm : outParamMap.values()) {
279           wpm.setWpMode(WebParam.Mode.OUT);
280           wmm.addParam(wpm);
281         }
282       }
283     }
284
285     
286     /*
287      * Collection<BeehiveWsParameterMetadata> params = wmm.getParams();
288      * System.out.println("adding method " + wmm.getWmOperationName() + "
289      * returning " + wmm.getJavaReturnType() + " with the following " +
290      * params.size() + " parameters");
291      *
292      * for (Jsr181ParameterMetadata wpm : params) {
293      * System.out.println(wpm.getWpName() + ':' + wpm.getJavaType()); }
294      */

295     wsm.addMethod(wmm);
296
297   }
298
299   private BeehiveWsParameterMetadata[] processParameters(String JavaDoc operationName, TPart[] messageParts, TBinding tBind,
300       TTypes types, Map JavaDoc<String JavaDoc, TPart[]> messageMap, BeehiveWsTypeMetadata wsm, boolean isOutputParameters) {
301
302       // When you have parts of the message in the header, you would have multiple wsdl:part. In case of
303
// wrapped we would have JUST ONE part that has name="parameters"
304
// but the header parts would have their name which looks like a bare
305
// So in the processing if there is any part that has name=parameters then the service is called wrapped, and from then
306
// on the parameter style is not changed.
307
// Update.... "parameters" test is not enough, in some cases it may be called "body" For wrapped there can only be one body element, and the element attribute's local
308
// name should be the same as the name of the opeartion.
309
boolean paramStyleIsSet = false;
310       
311       boolean shouldUnwrap;
312
313       if(isOutputParameters ) {
314           paramStyleIsSet = true; // only set this for input parameters
315
}
316           shouldUnwrap = isWrapped(operationName, messageParts, tBind, isOutputParameters);
317
318       List JavaDoc<BeehiveWsParameterMetadata> paramList = new ArrayList JavaDoc<BeehiveWsParameterMetadata>(
319           messageParts.length);
320       int j = 0;
321       for (TPart messagePart : messageParts) {
322         QName JavaDoc paramXmlType;
323         // if element attribute is set we are looking at document style
324
// in case of rpc message part would look something like:
325
// <part name="key type="xsd:string"/>
326
// otherwise it case of document it looks liek
327
// <part element="xxxx" name="parameters">
328
if (messagePart.isSetElement()) {
329           // we know we are document, and thus literal. and most
330
// likely using xml beans, but can use axis too.
331
QName JavaDoc element = messagePart.getElement();
332           Class JavaDoc javaType = findClassForQname(element);
333           // in case of xmlbeans(or other registered types) we don't
334
// need
335
// to dig any further
336
String JavaDoc name = messagePart.getName();
337           // if name is "parameters" then we need to unwrap the type
338
// and the service is doc/literal wrapped./
339
if (shouldUnwrap &&
340                   unwrapMessagePart(operationName, isOutputParameters, messagePart) // when we do want to unwrap, we should only unwrap the parts the parameters
341
// the parts that are for headers, for instance, should remain intact.
342
) {
343               if( !isOutputParameters) {// only set the mode if this is input parameters
344
wsm.getSoapBinding().setParameterStyle(
345                       SOAPBinding.ParameterStyle.WRAPPED);
346                   paramStyleIsSet = true;
347               }
348               if (types != null) { // if there are some type
349
try {
350                 Schema[] schemas = selectChildren(types, Schema.class);
351                 // We are looking at schemas that are defined in
352
// this schema.
353
// ---- // THIS WOULD NOT RECOGNIZE IMPORTED
354
// SCHEMAS.
355

356                 for (Schema s : schemas) {
357                   if (!s.getTargetNamespace().equals(element.getNamespaceURI()))
358                     continue;
359                   Element[] elements = s.getElementArray();
360                   for (Element e : elements) {
361                     if (!e.getName().equals(element.getLocalPart()))
362                       continue;
363
364                     if (e.isSetType()) {
365                       /*
366                        * simple type, element has name and type in same node.
367                        * complextype only has name and type is defined as
368                        * <complexType> child
369                        */

370                       paramList.add(elementToParamMetaData(s
371                           .getTargetNamespace(), e));
372                     } else {
373                       // we have <element
374
// name=xxx> and its child
375
// must be comlexType, or
376
// somethig is wrong
377

378                       ComplexType ct = e.getComplexType();
379                       if (ct != null && ct.isSetSequence()) {
380                         // DOC/LIT WSDL
381
/* NOTE: There may be other groupings that this code needs
382                          * to understand also. At this point we expect:
383                          * <element name="xxx" type="yyy">
384                          * or <element ref="zzz">
385                          */

386                         Group g = ct.getSequence();
387                         for (Element el : g.getElementArray()) {
388                           paramList.add(elementToParamMetaData(s
389                               .getTargetNamespace(), el));
390                         }
391                       }
392                     }
393                     break;
394                   }
395                   break;
396                 }
397               } catch (Exception JavaDoc e) {
398                 e.printStackTrace();
399               }
400             }
401           } else {
402             // if we did find object form qname we know
403
// that there are xml-aware classes that can decode
404
// the xml we let them do the
405
// processing.
406
if(!paramStyleIsSet) // Only set the style to bare if it hasn't been done in previous parts processing
407
wsm.getSoapBinding().setParameterStyle(
408                           SOAPBinding.ParameterStyle.BARE);
409
410             BeehiveWsParameterMetadata wpm = new Jsr181ParameterMetadataImpl();
411
412             wpm.setWpTargetNamespace(element.getNamespaceURI());
413             wpm.setWpName(name); // NAME SHOULD BE THE NAME OF THE MESSAGE PART. element.getLocalPart());
414
wpm.setXmlType(element);
415             wpm.setJavaType(javaType);
416             paramList.add(wpm);
417           }
418         } else { // this is an rpc and with no element attribute
419
BeehiveWsParameterMetadata wpm = new Jsr181ParameterMetadataImpl();
420           // FIXME jcolwell@bea.com 2004-Nov-09 -- figure out where
421
// RPC parameter namespaces should be specified in the WSDL.
422
wpm.setWpTargetNamespace(wsm.getWsTargetNamespace());
423           wpm.setWpName(messagePart.getName());
424           QName JavaDoc type = messagePart.getType();
425           wpm.setXmlType(type);
426           wpm.setJavaType(findClassForQname(type));
427           /*
428            * System.out.println(wpm.getWpName() + " of type " +
429            * wpm.getJavaType());
430            */

431           paramList.add(wpm);
432         }
433       }
434       return paramList.toArray(new BeehiveWsParameterMetadata[paramList.size()]);
435
436   }
437
438
439 private Class JavaDoc findClassForQname(QName JavaDoc element) {
440     //return new XmlBeanTypeMappingUtil().q2Class(element);
441
return lookupService.qname2class(element);
442 }
443
444   // To detect the wrapped walk the message parts, if there is a part that has name="parameters"
445
// or if there is a part that its element name is the same as the operation name, return true.
446
// NOTE: for MIME multiparts, or when there are headers the tBind should be checked to determine
447
// if a part is on the header or is a MIME. The header and MIME parts should not effect the wrapped
448
// determination.
449
// TODO: the logic here may not work with MIME types, verify later.
450
private boolean isWrapped( String JavaDoc operationName, TPart[] messagePart, TBinding tBind,boolean isOutputParameters) {
451       for( TPart message : messagePart) {
452           if( !message.isSetElement()) return false; // RPC-Encoded
453
if( true == unwrapMessagePart(operationName, isOutputParameters, message)) return true;
454       }
455       return false;
456   }
457
458   private boolean unwrapMessagePart(String JavaDoc operationName, boolean isOutputParameters, TPart message) {
459     String JavaDoc name = message.getName();
460     if("parameters".equals(name)) return true;
461     if( isOutputParameters) {
462           if(message.getElement().getLocalPart().equals(operationName+"Response")) return true;
463           
464       } else {
465           if(message.getElement().getLocalPart().equals(operationName)) return true;
466       }
467     return false;
468 }
469   /**
470    * @param paramList
471    * @param s
472    * @param el
473    */

474   private BeehiveWsParameterMetadata elementToParamMetaData(
475       String JavaDoc targetNamespace, Element el) {
476     BeehiveWsParameterMetadata wpm = new Jsr181ParameterMetadataImpl();
477     // FIXME
478
// jcolwell@bea.com
479
// 2004-Nov-09
480
// double check the
481
// namespace stuff
482
wpm.setWpTargetNamespace(targetNamespace);
483     boolean isArray = false;
484     if (el.isSetMaxOccurs()) {
485       if (0 != "1".compareTo(el.getMaxOccurs().toString()))
486         isArray = true;
487     } else if (el.isSetMinOccurs()) {
488       String JavaDoc minOccur = el.getMinOccurs().toString();
489       // If minoccur is other than 0, or 1 then it is array also
490
if ("0" != minOccur && "1" != minOccur)
491         isArray = true;
492     }
493
494     String JavaDoc name = null;
495     QName JavaDoc xmlType = null;
496     if (el.isSetName() && el.isSetType()) {
497
498       name = el.getName();
499
500       xmlType = el.getType();
501
502     } else if (el.isSetRef()) {
503       QName JavaDoc ref = el.getRef();
504       name = ref.getLocalPart();
505       xmlType = ref;
506
507     } else
508       throw new RuntimeException JavaDoc("invalid element: " + el);
509     Class JavaDoc javaType = findClassForQname(xmlType);
510     if (isArray) {
511       // create an array of the type, then get its type.
512
Object JavaDoc realType = Array.newInstance(javaType, 1);
513       javaType = realType.getClass();
514     }
515     wpm.setWpName(name);
516     wpm.setXmlType(xmlType);
517     wpm.setJavaType(javaType);
518     return wpm;
519   }
520
521   /**
522    * @param types
523    */

524   private void doUnknownTypes(TTypes types) {
525     // sections.
526

527   }
528
529   private void processTBinding(TBinding tBind, BeehiveWsTypeMetadata wsm)
530       throws IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc {
531
532     org.xmlsoap.schemas.wsdl.soap.TBinding[] soapBinding = getSOAPBinding(tBind);
533     BeehiveWsSOAPBindingInfo soapInfo = new SOAPBindingInfo();
534     if (soapBinding != null && soapBinding.length > 0) {
535       if (TStyleChoice.RPC.equals(soapBinding[0].getStyle())) {
536         soapInfo.setStyle(SOAPBinding.Style.RPC);
537       }
538       wsm.setSoapBinding(soapInfo);
539     }
540   }
541
542   private void processTBindingOperation(TBinding tBind,
543             BeehiveWsTypeMetadata wsm,
544             Map JavaDoc<String JavaDoc, BeehiveWsMethodMetadata> methodMap)
545             throws IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc {
546
547         TBindingOperation[] tBops = tBind.getOperationArray();
548         for (TBindingOperation tBop : tBops) {
549             BeehiveWsMethodMetadata wmm = methodMap.get(tBop.getName());
550             if (wmm == null)
551                 throw new RuntimeException JavaDoc("Invalid method name: "
552                         + tBop.getName());
553             org.xmlsoap.schemas.wsdl.soap.TOperation[] soapOperations = getSOAPOperations(tBop);
554             if (soapOperations != null && soapOperations.length > 0) {
555                 wmm.setWmAction(soapOperations[0].getSoapAction());
556             }
557             TBindingOperationMessage tbMsg = tBop.getInput();
558             if (tbMsg == null) {
559                 tbMsg = tBop.getOutput();
560             }
561             if (tbMsg != null) {
562                 org.xmlsoap.schemas.wsdl.soap.TBody[] bodies = getSOAPBody(tbMsg);
563                 if (bodies.length > 0) {
564                     if (wsm.getWsTargetNamespace() == null) {
565                         wsm.setWsTargetNamespace(bodies[0].getNamespace());
566                     }
567                     BeehiveWsSOAPBindingInfo soapInfo = wsm.getSoapBinding();
568                     if (UseChoice.ENCODED.equals(bodies[0].getUse())) {
569                         soapInfo.setUse(SOAPBinding.Use.ENCODED);
570                     }
571                 }
572                 
573                 setSoapHeaders(wmm, tbMsg);
574                 
575             }
576             
577             // tBop.getOutput and get the headers out of it.
578
setSoapHeaders(wmm, tBop.getOutput());
579             
580         }
581     }
582
583     /**
584      * @param tbMsg
585      * @throws IllegalAccessException
586      * @throws NoSuchFieldException
587      */

588     private void setSoapHeaders(BeehiveWsMethodMetadata wmm,
589             TBindingOperationMessage tbMsg) throws IllegalAccessException JavaDoc,
590             NoSuchFieldException JavaDoc {
591         if(null == tbMsg) return;
592         
593         // get the header
594
org.xmlsoap.schemas.wsdl.soap.THeader[] soapHeaders = getSOAPHeader(tbMsg);
595         if (soapHeaders != null && soapHeaders.length > 0) { // there are method arguments that are
596
// in the header
597
for (org.xmlsoap.schemas.wsdl.soap.THeader nxtHeader : soapHeaders) {
598                 String JavaDoc part = nxtHeader.getPart();
599                if (part == null)
600                     throw new RuntimeException JavaDoc(
601                             "Missing part attribute in soap:header method: "
602                                     + tbMsg.getName());
603                 BeehiveWsParameterMetadata argument = wmm.findParam(part);
604                 if(argument == null) throw new RuntimeException JavaDoc ("Couldn't find part name: " + part + " in method " + wmm.getJavaMethodName());
605                 argument.setWpHeader(true);
606             }
607
608         }
609     }
610
611  
612
613 // public BeehiveWsTypeMetadata loadWebServiceMetadataFromWSDL(String wsdlLocation)
614
// throws Exception {
615
// return toWebServiceMetadata(parseWSDL(wsdlLocation));
616
// }
617

618   public static DefinitionsDocument parseWSDL(String JavaDoc wsdlLocation)
619       throws IOException JavaDoc, MalformedURLException JavaDoc, XmlException {
620     if (wsdlLocation.indexOf("://") > 2) {
621       return parseWSDL(new URL JavaDoc(wsdlLocation));
622     } else {
623       return parseWSDL(new File JavaDoc(wsdlLocation));
624     }
625   }
626
627   public static DefinitionsDocument parseWSDL(File JavaDoc wsdlFile)
628       throws IOException JavaDoc, XmlException {
629     return DefinitionsDocument.Factory.parse(wsdlFile);
630   }
631
632   public static DefinitionsDocument parseWSDL(URL JavaDoc wsdlURL) throws IOException JavaDoc,
633       MalformedURLException JavaDoc, XmlException {
634     return DefinitionsDocument.Factory.parse(wsdlURL);
635   }
636
637   public static DefinitionsDocument parseWSDL(InputStream JavaDoc wsdlStream)
638       throws IOException JavaDoc, MalformedURLException JavaDoc, XmlException {
639     return DefinitionsDocument.Factory.parse(wsdlStream);
640   }
641
642   public static org.xmlsoap.schemas.wsdl.soap.TOperation[] getSOAPOperations(
643       TBindingOperation bo) throws IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc {
644     return selectChildren(bo, org.xmlsoap.schemas.wsdl.soap.TOperation.class);
645   }
646
647   public static org.xmlsoap.schemas.wsdl.soap.TBinding[] getSOAPBinding(
648       TBinding b) throws IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc {
649     XmlObject[] kids = b.selectChildren(new QName JavaDoc(
650         "http://schemas.xmlsoap.org/wsdl/soap/", "binding"));
651
652     org.xmlsoap.schemas.wsdl.soap.TBinding[] res = new org.xmlsoap.schemas.wsdl.soap.TBinding[kids.length];
653     for (int i = 0; i < kids.length; i++)
654       res[i] = (org.xmlsoap.schemas.wsdl.soap.TBinding) kids[i];
655
656     return res;
657   }
658
659   public static org.xmlsoap.schemas.wsdl.soap.TBody[] getSOAPBody(
660       TBindingOperationMessage bom) throws IllegalAccessException JavaDoc,
661       NoSuchFieldException JavaDoc {
662       XmlObject[] kids = bom.selectChildren(new QName JavaDoc(
663               "http://schemas.xmlsoap.org/wsdl/soap/", "body"));
664
665           org.xmlsoap.schemas.wsdl.soap.TBody[] res = new org.xmlsoap.schemas.wsdl.soap.TBody[kids.length];
666           for (int i = 0; i < kids.length; i++)
667             res[i] = (org.xmlsoap.schemas.wsdl.soap.TBody) kids[i];
668
669           return res;
670    }
671   
672   public static org.xmlsoap.schemas.wsdl.soap.THeader[] getSOAPHeader(
673           TBindingOperationMessage bom) throws IllegalAccessException JavaDoc,
674           NoSuchFieldException JavaDoc {
675       XmlObject[] kids = bom.selectChildren(new QName JavaDoc(
676               "http://schemas.xmlsoap.org/wsdl/soap/", "header"));
677
678           org.xmlsoap.schemas.wsdl.soap.THeader[] res = new org.xmlsoap.schemas.wsdl.soap.THeader[kids.length];
679           for (int i = 0; i < kids.length; i++)
680             res[i] = (org.xmlsoap.schemas.wsdl.soap.THeader) kids[i];
681
682           return res;
683       }
684   
685
686   public static org.xmlsoap.schemas.wsdl.soap.TAddress[] getSOAPAddress(
687       TPort port) throws IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc {
688     return selectChildren(port, org.xmlsoap.schemas.wsdl.soap.TAddress.class);
689   }
690
691   private static <T extends XmlObject> T[] selectChildren(XmlObject parent,
692       Class JavaDoc<T> childClass) throws IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc {
693     // retrieve the SchemaType from the static type field
694
if(parent == null) return null;
695     SchemaType st = (SchemaType) childClass.getField("type").get(null);
696     SchemaType originalType = null;
697     QName JavaDoc element;
698     if (st.isAnonymousType()) {
699       originalType = st;
700       st = st.getOuterType();
701     }
702     if (st.isDocumentType()) {
703       element = st.getDocumentElementName();
704       if (originalType != null) {
705         st = originalType;
706       }
707     } else {
708       element = st.getName();
709     }
710     /*
711      * System.out.println("Selecting children " + element +" of class " +
712      * st.getJavaClass());
713      */

714     XmlObject[] kids = parent.selectChildren(element);
715
716     T[] castKids = (T[]) Array.newInstance(childClass, kids.length);
717     for (int j = 0; j < castKids.length; j++) {
718
719       if (!st.getJavaClass().isAssignableFrom(kids[j].getClass())) {
720         // System.out.println("before typechange " +
721
// kids[j].getClass());
722
kids[j] = kids[j].changeType(st);
723         // System.out.println("after typechange " + kids[j].getClass());
724
} else {
725         // System.out.println("no typechange needed");
726
}
727       castKids[j] = childClass.cast(kids[j]);
728     }
729     return castKids;
730   }
731
732     public XmlBeanWSDLProcessor( InputStream JavaDoc wsdlStream) throws MalformedURLException JavaDoc, IOException JavaDoc, XmlException {
733         super();
734         this.defDoc = parseWSDL(wsdlStream);
735
736     }
737     
738     private XmlBeanWSDLProcessor() {
739       // no default constrcutor!
740
}
741 }
742
Popular Tags