1 package org.objectweb.celtix.bus.bindings; 2 3 import java.lang.annotation.Annotation ; 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Map ; 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 ; 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 soapAction; 38 String targetNs; 39 QName requestWrapperQName; 40 QName responseWrapperQName; 41 WebResult result; 42 List <OperationWebParam> params; 43 44 public WSDLOperationInfo(WSDLMetaDataCache c, BindingOperation op) { 45 cache = c; 46 bindingOp = op; 47 } 48 49 public String 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 getOperationName() { 90 return getName(); 91 } 92 93 public String 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 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 getWebResultQName() { 152 WebResult res = getWebResult(); 153 if (null != res) { 154 if (getSOAPStyle() == Style.DOCUMENT) { 155 if ("".equals(res.name())) { 156 return new QName (res.targetNamespace(), 157 "return"); 158 } 159 return new QName (res.targetNamespace(), 160 res.name()); 161 } else { 162 return new QName ("", res.partName()); 163 } 164 } 165 return new QName ("", ""); 166 } 167 168 public QName getRequestWrapperQName() { 169 if (requestWrapperQName == null) { 170 if (getSOAPParameterStyle() == SOAPBinding.ParameterStyle.BARE 171 || getSOAPStyle() == SOAPBinding.Style.RPC) { 172 requestWrapperQName = new QName ("", ""); 173 } else { 174 Message msg = getMessage(true); 175 List parts = msg.getOrderedParts(null); 176 Part part = (Part)parts.get(0); 177 requestWrapperQName = part.getElementName(); 178 } 179 } 180 return requestWrapperQName; 181 } 182 public QName getResponseWrapperQName() { 183 if (responseWrapperQName == null) { 184 if (getSOAPParameterStyle() == SOAPBinding.ParameterStyle.BARE 185 || getSOAPStyle() == SOAPBinding.Style.RPC) { 186 responseWrapperQName = new QName ("", ""); 187 } else { 188 Message msg = getMessage(false); 189 if (msg != null) { 190 List parts = msg.getOrderedParts(null); 191 Part part = (Part)parts.get(0); 192 responseWrapperQName = part.getElementName(); 193 } else { 194 responseWrapperQName = new QName ("", ""); 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 parts = msg.getOrderedParts(null); 214 if (parts.size() != 1) { 215 return false; 216 } 217 Part part = (Part)parts.get(0); 218 QName element = part.getElementName(); 219 if (element == null 220 || !element.getLocalPart().equals(bindingOp.getOperation().getName())) { 221 return false; 222 } 223 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 } 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 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 <OperationWebParam> parms, 274 Map <String , OperationWebParam> parmMap, 275 Operation operation) { 276 277 Input input = operation.getInput(); 278 Output output = operation.getOutput(); 279 if (input == null) { 280 return; 282 } 283 Collection parts = input.getMessage().getParts().values(); 284 for (Iterator 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 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 <OperationWebParam> parms, 316 Map <String , OperationWebParam> parmMap, 317 Operation operation) { 318 319 Input input = operation.getInput(); 320 Output output = operation.getOutput(); 321 Collection parts = input.getMessage().getParts().values(); 322 for (Iterator 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 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 <OperationWebParam> parms, 351 Map <String , OperationWebParam> parmMap, 352 Operation operation) { 353 } 355 356 private synchronized void initParams() { 357 List <OperationWebParam> parms = new ArrayList <OperationWebParam>(); 358 Map <String , OperationWebParam> parmMap = new HashMap <String , 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 if (isDocLitWrapped()) { 369 initDocLitWrappedParam(parms, parmMap, operation); 370 } else { 371 initDocLitBareParam(parms, parmMap, operation); 372 } 373 } 374 375 BindingInput bindingInput = bindingOp.getBindingInput(); 377 if (bindingInput != null) { 378 javax.wsdl.Message message = operation.getInput().getMessage(); 379 List elements = bindingInput.getExtensibilityElements(); 380 for (Iterator i = elements.iterator(); i.hasNext();) { 381 Object 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 elements = bindingOutput.getExtensibilityElements(); 395 for (Iterator i = elements.iterator(); i.hasNext();) { 396 Object 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 extensibilityElement) { 426 Part part = null; 427 if (extensibilityElement instanceof SOAPHeader) { 428 SOAPHeader soapHeader = (SOAPHeader)extensibilityElement; 429 QName 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 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 elements, Class <T> type) { 443 for (Iterator i = elements.iterator(); i.hasNext();) { 444 Object 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 name() { 460 return parm.name(); 461 } 462 463 public String targetNamespace() { 464 return parm.webResultTargetNamespace(); 465 } 466 467 public boolean header() { 468 return parm.header(); 469 } 470 471 public String partName() { 472 return parm.partName(); 473 } 474 475 public Class <? extends Annotation > annotationType() { 476 return WebResult.class; 477 } 478 } 479 480 private class OperationWebParam implements WebParam { 481 Mode md; 482 String name; 483 String targetNs; 484 String webResultTNS; 485 boolean isHeader; 486 String partname; 487 488 public OperationWebParam(String n, String pn, Mode m, String ns) { 489 this(n, pn, m, ns, ns); 490 } 491 public OperationWebParam(String n, String pn, Mode m, String ns, String wrns) { 492 name = n; 493 md = m; 494 targetNs = ns; 495 partname = pn; 496 webResultTNS = wrns; 497 } 498 499 public String 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 targetNamespace() { 512 return targetNs; 513 } 514 public String 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 partName() { 526 return partname; 527 } 528 529 public Class <? extends Annotation > annotationType() { 530 return WebParam.class; 531 } 532 533 } 534 } 535 | Popular Tags |