KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > processors > wsdl2 > WSDLToSoapProcessor


1 package org.objectweb.celtix.tools.processors.wsdl2;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Writer JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import javax.wsdl.Binding;
10 import javax.wsdl.BindingFault;
11 import javax.wsdl.BindingInput;
12 import javax.wsdl.BindingOperation;
13 import javax.wsdl.BindingOutput;
14 import javax.wsdl.Fault;
15 import javax.wsdl.Input;
16 import javax.wsdl.Operation;
17 import javax.wsdl.Output;
18 import javax.wsdl.Part;
19 import javax.wsdl.PortType;
20 import javax.wsdl.WSDLException;
21 import javax.wsdl.extensions.ExtensionRegistry;
22 import javax.wsdl.extensions.soap.SOAPBinding;
23 import javax.wsdl.extensions.soap.SOAPBody;
24 import javax.wsdl.extensions.soap.SOAPFault;
25 import javax.wsdl.extensions.soap.SOAPOperation;
26 import javax.wsdl.xml.WSDLWriter;
27 import javax.xml.namespace.QName JavaDoc;
28
29 import org.objectweb.celtix.common.i18n.Message;
30 import org.objectweb.celtix.tools.common.ToolConstants;
31 import org.objectweb.celtix.tools.common.ToolException;
32 import org.objectweb.celtix.tools.common.WSDLConstants;
33
34 public class WSDLToSoapProcessor extends WSDLToProcessor {
35
36     private static final String JavaDoc NEW_FILE_NAME_MODIFIER = "-soapbinding";
37
38     private ExtensionRegistry extReg;
39
40     private Map JavaDoc portTypes;
41     private PortType portType;
42     private Binding binding;
43
44     public void process() throws ToolException {
45         init();
46         validate();
47         extReg = this.wsdlReader.getExtensionRegistry();
48         doAppendBinding();
49     }
50
51     private void validate() throws ToolException {
52         if (isBindingExisted()) {
53             Message msg = new Message("BINDING_ALREADY_EXIST", LOG);
54             throw new ToolException(msg);
55         }
56         if (!isPortTypeExisted()) {
57             Message msg = new Message("PORTTYPE_NOT_EXIST", LOG);
58             throw new ToolException(msg);
59         }
60         if (!nameSpaceCheck()) {
61             Message msg = new Message("SOAPBINDING_STYLE_NOT_PROVIEDED", LOG);
62             throw new ToolException(msg);
63         }
64         if (WSDLConstants.RPC.equalsIgnoreCase((String JavaDoc)env.get(ToolConstants.CFG_STYLE))) {
65             Iterator JavaDoc it = portType.getOperations().iterator();
66             while (it.hasNext()) {
67                 Operation op = (Operation)it.next();
68                 Input input = op.getInput();
69                 if (input != null && input.getMessage() != null) {
70                     Iterator JavaDoc itParts = input.getMessage().getParts().values().iterator();
71                     while (itParts.hasNext()) {
72                         Part part = (Part)itParts.next();
73                         if (part.getTypeName() == null || "".equals(part.getTypeName().toString())) {
74                             Message msg = new Message("RPC_PART_ILLEGAL", LOG, new Object JavaDoc[] {part.getName()});
75                             throw new ToolException(msg);
76                         }
77                     }
78                 }
79                 Output output = op.getOutput();
80                 if (output != null && output.getMessage() != null) {
81                     Iterator JavaDoc itParts = output.getMessage().getParts().values().iterator();
82                     while (itParts.hasNext()) {
83                         Part part = (Part)itParts.next();
84                         if (part.getTypeName() == null || "".equals(part.getTypeName().toString())) {
85                             Message msg = new Message("RPC_PART_ILLEGAL", LOG, new Object JavaDoc[] {part.getName()});
86                             throw new ToolException(msg);
87                         }
88                     }
89                 }
90             }
91         }
92     }
93
94     private boolean isPortTypeExisted() {
95         portTypes = wsdlDefinition.getPortTypes();
96         if (portTypes == null) {
97             return false;
98         }
99         Iterator JavaDoc it = portTypes.keySet().iterator();
100         while (it.hasNext()) {
101             QName JavaDoc existPortQName = (QName JavaDoc)it.next();
102             String JavaDoc existPortName = existPortQName.getLocalPart();
103             if (existPortName.equals(env.get(ToolConstants.CFG_PORTTYPE))) {
104                 portType = (PortType)portTypes.get(existPortQName);
105                 break;
106             }
107         }
108         return (portType == null) ? false : true;
109     }
110
111     private boolean isBindingExisted() {
112         Map JavaDoc bindings = wsdlDefinition.getBindings();
113         if (bindings == null) {
114             return false;
115         }
116         Iterator JavaDoc it = bindings.keySet().iterator();
117         while (it.hasNext()) {
118             QName JavaDoc existBindingQName = (QName JavaDoc)it.next();
119             String JavaDoc existBindingName = existBindingQName.getLocalPart();
120             String JavaDoc bindingName = (String JavaDoc)env.get(ToolConstants.CFG_BINDING);
121             if (bindingName.equals(existBindingName)) {
122                 binding = (Binding)bindings.get(existBindingQName);
123             }
124         }
125         return (binding == null) ? false : true;
126     }
127
128     private boolean nameSpaceCheck() {
129         if (WSDLConstants.RPC.equalsIgnoreCase((String JavaDoc)env.get(ToolConstants.CFG_STYLE))
130             && !env.optionSet(ToolConstants.CFG_NAMESPACE)) {
131             return false;
132         }
133         return true;
134     }
135
136     protected void init() throws ToolException {
137         parseWSDL((String JavaDoc)env.get(ToolConstants.CFG_WSDLURL));
138     }
139
140     private void doAppendBinding() throws ToolException {
141         if (binding == null) {
142             binding = wsdlDefinition.createBinding();
143             binding.setQName(new QName JavaDoc(wsdlDefinition.getTargetNamespace(), (String JavaDoc)env
144                 .get(ToolConstants.CFG_BINDING)));
145             binding.setUndefined(false);
146             binding.setPortType(portType);
147         }
148         setSoapBindingExtElement();
149         addBindingOperation();
150         wsdlDefinition.addBinding(binding);
151
152         WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
153         Writer JavaDoc outputWriter = getOutputWriter(NEW_FILE_NAME_MODIFIER);
154         try {
155             wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
156         } catch (WSDLException wse) {
157             Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG);
158             throw new ToolException(msg);
159
160         }
161         try {
162             outputWriter.close();
163         } catch (IOException JavaDoc ioe) {
164             Message msg = new Message("PORTTYPE_NOT_EXIST", LOG);
165             throw new ToolException(msg);
166         }
167     }
168
169     private void setSoapBindingExtElement() throws ToolException {
170         if (extReg == null) {
171             extReg = wsdlFactory.newPopulatedExtensionRegistry();
172         }
173         SOAPBinding soapBinding = null;
174         try {
175             soapBinding = (SOAPBinding)extReg.createExtension(Binding.class, WSDLConstants.NS_SOAP_BINDING);
176         } catch (WSDLException wse) {
177             Message msg = new Message("FAIL_TO_CREATE_SOAPBINDING", LOG);
178             throw new ToolException(msg, wse);
179         }
180         soapBinding.setStyle((String JavaDoc)env.get(ToolConstants.CFG_STYLE));
181         soapBinding.setTransportURI(WSDLConstants.NS_SOAP11_HTTP_BINDING);
182         binding.addExtensibilityElement(soapBinding);
183     }
184
185     @SuppressWarnings JavaDoc("unchecked")
186     private void addBindingOperation() throws ToolException {
187         /**
188          * This method won't do unique operation name checking on portType The
189          * WS-I Basic Profile[17] R2304 requires that operations within a
190          * wsdl:portType have unique values for their name attribute so mapping
191          * of WS-I compliant WSDLdescriptions will not generate Java interfaces
192          * with overloaded methods. However, for backwards compatibility, JAX-WS
193          * supports operation name overloading provided the overloading does not
194          * cause conflicts (as specified in the Java Language Specification[25])
195          * in the mapped Java service endpoint interface declaration.
196          */

197         List JavaDoc<Operation> ops = portType.getOperations();
198         for (Operation op : ops) {
199             BindingOperation bindingOperation = wsdlDefinition.createBindingOperation();
200             setSoapOperationExtElement(bindingOperation);
201             bindingOperation.setName(op.getName());
202             if (op.getInput() != null) {
203                 bindingOperation.setBindingInput(getBindingInput(op.getInput()));
204             }
205             if (op.getOutput() != null) {
206                 bindingOperation.setBindingOutput(getBindingOutput(op.getOutput()));
207             }
208             if (op.getFaults() != null && op.getFaults().size() > 0) {
209                 addSoapFaults(op, bindingOperation);
210             }
211             bindingOperation.setOperation(op);
212             binding.addBindingOperation(bindingOperation);
213         }
214     }
215
216     private void setSoapOperationExtElement(BindingOperation bo) throws ToolException {
217         if (extReg == null) {
218             extReg = wsdlFactory.newPopulatedExtensionRegistry();
219         }
220         SOAPOperation soapOperation = null;
221         try {
222             soapOperation = (SOAPOperation)extReg.createExtension(BindingOperation.class,
223                                                                   WSDLConstants.NS_SOAP_OPERATION);
224         } catch (WSDLException wse) {
225             Message msg = new Message("FAIL_TO_CREATE_SOAPBINDING", LOG);
226             throw new ToolException(msg, wse);
227         }
228         soapOperation.setStyle((String JavaDoc)env.get(ToolConstants.CFG_STYLE));
229         soapOperation.setSoapActionURI("");
230         bo.addExtensibilityElement(soapOperation);
231     }
232
233     private BindingInput getBindingInput(Input input) throws ToolException {
234         BindingInput bi = wsdlDefinition.createBindingInput();
235         bi.setName(input.getName());
236         // As command line won't specify the details of body/header for message
237
// parts
238
// All input message's parts will be added into one soap body element
239
bi.addExtensibilityElement(getSoapBody(BindingInput.class));
240         return bi;
241     }
242
243     private BindingOutput getBindingOutput(Output output) throws ToolException {
244         BindingOutput bo = wsdlDefinition.createBindingOutput();
245         bo.setName(output.getName());
246         // As command line won't specify the details of body/header for message
247
// parts
248
// All output message's parts will be added into one soap body element
249
bo.addExtensibilityElement(getSoapBody(BindingOutput.class));
250         return bo;
251     }
252
253     private SOAPBody getSoapBody(Class JavaDoc parent) throws ToolException {
254         if (extReg == null) {
255             extReg = wsdlFactory.newPopulatedExtensionRegistry();
256         }
257         SOAPBody soapBody = null;
258         try {
259             soapBody = (SOAPBody)extReg.createExtension(parent, WSDLConstants.NS_SOAP_BODY);
260         } catch (WSDLException wse) {
261             Message msg = new Message("FAIL_TO_CREATE_SOAPBINDING", LOG);
262             throw new ToolException(msg, wse);
263         }
264         soapBody.setUse((String JavaDoc)env.get(ToolConstants.CFG_USE));
265         if (WSDLConstants.RPC.equalsIgnoreCase((String JavaDoc)env.get(ToolConstants.CFG_STYLE))
266             && env.optionSet(ToolConstants.CFG_NAMESPACE)) {
267             soapBody.setNamespaceURI((String JavaDoc)env.get(ToolConstants.CFG_NAMESPACE));
268         }
269         return soapBody;
270     }
271
272     private void addSoapFaults(Operation op, BindingOperation bindingOperation) throws ToolException {
273         Map JavaDoc faults = op.getFaults();
274         Iterator JavaDoc it = faults.keySet().iterator();
275         while (it.hasNext()) {
276             String JavaDoc key = (String JavaDoc)it.next();
277             Fault fault = (Fault)faults.get(key);
278             BindingFault bf = wsdlDefinition.createBindingFault();
279             bf.setName(fault.getName());
280             setSoapFaultExtElement(bf);
281             bindingOperation.addBindingFault(bf);
282         }
283     }
284
285     private void setSoapFaultExtElement(BindingFault bf) throws ToolException {
286         if (extReg == null) {
287             extReg = wsdlFactory.newPopulatedExtensionRegistry();
288         }
289         SOAPFault soapFault = null;
290         try {
291             soapFault = (SOAPFault)extReg.createExtension(BindingFault.class, WSDLConstants.NS_SOAP_FAULT);
292         } catch (WSDLException wse) {
293             Message msg = new Message("FAIL_TO_CREATE_SOAPBINDING", LOG);
294             throw new ToolException(msg, wse);
295         }
296         soapFault.setName(bf.getName());
297         soapFault.setUse((String JavaDoc)env.get(ToolConstants.CFG_USE));
298         if (WSDLConstants.RPC.equalsIgnoreCase((String JavaDoc)env.get(ToolConstants.CFG_STYLE))
299             && env.optionSet(ToolConstants.CFG_NAMESPACE)) {
300             soapFault.setNamespaceURI((String JavaDoc)env.get(ToolConstants.CFG_NAMESPACE));
301         }
302         bf.addExtensibilityElement(soapFault);
303     }
304
305 }
306
Popular Tags