1 package org.objectweb.celtix.tools.processors.wsdl2; 2 3 import java.io.IOException ; 4 import java.io.Writer ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 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 ; 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 NEW_FILE_NAME_MODIFIER = "-soapbinding"; 37 38 private ExtensionRegistry extReg; 39 40 private Map 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 )env.get(ToolConstants.CFG_STYLE))) { 65 Iterator 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 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 [] {part.getName()}); 75 throw new ToolException(msg); 76 } 77 } 78 } 79 Output output = op.getOutput(); 80 if (output != null && output.getMessage() != null) { 81 Iterator 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 [] {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 it = portTypes.keySet().iterator(); 100 while (it.hasNext()) { 101 QName existPortQName = (QName )it.next(); 102 String 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 bindings = wsdlDefinition.getBindings(); 113 if (bindings == null) { 114 return false; 115 } 116 Iterator it = bindings.keySet().iterator(); 117 while (it.hasNext()) { 118 QName existBindingQName = (QName )it.next(); 119 String existBindingName = existBindingQName.getLocalPart(); 120 String bindingName = (String )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 )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 )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 (wsdlDefinition.getTargetNamespace(), (String )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 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 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 )env.get(ToolConstants.CFG_STYLE)); 181 soapBinding.setTransportURI(WSDLConstants.NS_SOAP11_HTTP_BINDING); 182 binding.addExtensibilityElement(soapBinding); 183 } 184 185 @SuppressWarnings ("unchecked") 186 private void addBindingOperation() throws ToolException { 187 197 List <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 )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 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 bo.addExtensibilityElement(getSoapBody(BindingOutput.class)); 250 return bo; 251 } 252 253 private SOAPBody getSoapBody(Class 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 )env.get(ToolConstants.CFG_USE)); 265 if (WSDLConstants.RPC.equalsIgnoreCase((String )env.get(ToolConstants.CFG_STYLE)) 266 && env.optionSet(ToolConstants.CFG_NAMESPACE)) { 267 soapBody.setNamespaceURI((String )env.get(ToolConstants.CFG_NAMESPACE)); 268 } 269 return soapBody; 270 } 271 272 private void addSoapFaults(Operation op, BindingOperation bindingOperation) throws ToolException { 273 Map faults = op.getFaults(); 274 Iterator it = faults.keySet().iterator(); 275 while (it.hasNext()) { 276 String key = (String )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 )env.get(ToolConstants.CFG_USE)); 298 if (WSDLConstants.RPC.equalsIgnoreCase((String )env.get(ToolConstants.CFG_STYLE)) 299 && env.optionSet(ToolConstants.CFG_NAMESPACE)) { 300 soapFault.setNamespaceURI((String )env.get(ToolConstants.CFG_NAMESPACE)); 301 } 302 bf.addExtensibilityElement(soapFault); 303 } 304 305 } 306 | Popular Tags |