1 57 58 package org.apache.wsif.providers.jms; 59 60 import java.io.InputStream ; 61 import java.io.OutputStream ; 62 import java.io.Serializable ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 import javax.jms.JMSException ; 68 import javax.wsdl.Binding; 69 import javax.wsdl.BindingFault; 70 import javax.wsdl.BindingOperation; 71 import javax.wsdl.Fault; 72 import javax.wsdl.Operation; 73 import javax.wsdl.extensions.ExtensibilityElement; 74 75 import org.apache.wsif.WSIFConstants; 76 import org.apache.wsif.WSIFException; 77 import org.apache.wsif.WSIFMessage; 78 import org.apache.wsif.WSIFRequest; 79 import org.apache.wsif.WSIFResponse; 80 import org.apache.wsif.format.WSIFFormatter; 81 import org.apache.wsif.logging.Trc; 82 import org.apache.wsif.util.WSIFUtils; 83 import org.apache.wsif.wsdl.extensions.jms.JMSFault; 84 import org.apache.wsif.wsdl.extensions.jms.JMSFaultIndicator; 85 import org.apache.wsif.wsdl.extensions.jms.JMSFaultProperty; 86 import org.apache.wsif.wsdl.extensions.jms.JMSInput; 87 import org.apache.wsif.wsdl.extensions.jms.JMSOutput; 88 import org.apache.wsif.wsdl.extensions.jms.JMSProperty; 89 90 96 public class JMSFormatter implements WSIFFormatter, Serializable { 97 98 private static final long serialVersionUID = 1L; 99 100 private javax.wsdl.Definition fieldDefinition; 101 private javax.wsdl.Port fieldPort; 102 103 private String reqOpName; 104 private String reqInputName; 105 private String reqOutputName; 106 private BindingFault lastBindingFault = null; 107 108 111 public JMSFormatter(javax.wsdl.Definition def, javax.wsdl.Port port) { 112 super(); 113 Trc.entry(this,def,port); 114 fieldDefinition = def; 115 fieldPort = port; 116 Trc.exit(); 117 } 118 119 122 public void formatRequest(WSIFRequest req, OutputStream out) { 123 Trc.entry(this, req); 124 Trc.exit(); 125 } 126 127 130 public WSIFRequest unformatRequest(InputStream in) { 131 Trc.entry(this); 132 Trc.exit(); 133 return null; 134 } 135 136 139 public void formatResponse(WSIFResponse resp, OutputStream out) { 140 Trc.entry(this, resp); 141 Trc.exit(); 142 } 143 144 147 public WSIFResponse unformatResponse(InputStream in) { 148 Trc.entry(this); 149 Trc.exit(); 150 return null; 151 } 152 153 public void formatRequest(WSIFRequest req, javax.jms.Message out) 154 throws WSIFException { 155 Trc.entry(this, req, out); 156 157 Binding binding = fieldPort.getBinding(); 158 BindingOperation bop = 159 binding.getBindingOperation( 160 req.getOperationName(), 161 req.getInputName(), 162 req.getOutputName()); 163 javax.wsdl.Message inMessage = bop.getOperation().getInput().getMessage(); 164 165 org.apache.wsif.providers.jms.JMSMessage fhMsg = 166 new org.apache.wsif.providers.jms.JMSMessage( 167 fieldDefinition, 168 binding, 169 inMessage, 170 getInputParts(bop)); 171 172 copyTo(req.getIncomingMessage(), fhMsg); 173 fhMsg.write(out); 174 175 setOperationName( req.getOperationName() ); 176 setInputName( req.getInputName() ); 177 setOutputName( req.getOutputName() ); 178 179 Trc.exit(); 180 } 181 182 public WSIFRequest unformatRequest(javax.jms.Message in) 183 throws WSIFException { 184 Trc.entry(this, in); 185 WSIFRequest req = createRequest(in); 187 188 Binding binding = fieldPort.getBinding(); 189 BindingOperation bop = 190 binding.getBindingOperation( 191 req.getOperationName(), 192 req.getInputName(), 193 req.getOutputName()); 194 195 javax.wsdl.Message inMessage = bop.getOperation().getInput().getMessage(); 196 197 org.apache.wsif.providers.jms.JMSMessage fhMsg = 198 new org.apache.wsif.providers.jms.JMSMessage( 199 fieldDefinition, 200 binding, 201 inMessage, 202 getInputParts(bop)); 203 204 fhMsg.read(in); 205 206 req.setIncomingMessage(fhMsg); 207 208 Trc.exit(req); 209 return req; 210 } 211 212 protected WSIFRequest createRequest(javax.jms.Message msg) 213 throws WSIFException { 214 Trc.entry(this, msg); 215 javax.xml.namespace.QName serviceName = null; 216 String operationName = null; 217 String input = null; 218 String output = null; 219 220 Binding fieldBinding = fieldPort.getBinding(); 221 if (fieldBinding == null) 222 throw new WSIFException("Unable to locate Binding"); 223 224 Map sm = WSIFUtils.getAllItems( fieldDefinition, "Service" ); 226 Iterator services = sm.values().iterator(); 227 while (services.hasNext() && serviceName ==null) { 228 javax.wsdl.Service s = (javax.wsdl.Service)services.next(); 229 230 for (Iterator ports = s.getPorts().values().iterator(); ports.hasNext();) { 231 javax.wsdl.Port p = (javax.wsdl.Port)ports.next(); 232 if (p.getBinding() != null && p.getBinding().getQName().equals(fieldBinding.getQName())) { 233 serviceName = s.getQName(); 235 break; 236 } 237 } 238 } 239 240 if (serviceName == null) 241 throw new WSIFException("Unable to locate Service"); 242 243 244 javax.wsdl.PortType portType = fieldBinding.getPortType(); 247 if (portType!= null) { 248 List operations = portType.getOperations(); 249 if (operations != null && operations.size() == 1 ) { 250 javax.wsdl.Operation o = (javax.wsdl.Operation)operations.get(0); 251 operationName = o.getName(); 252 if (o.getInput() != null) { 253 input = o.getInput().getName(); 254 } 255 if (o.getOutput() != null) { 256 output = o.getOutput().getName(); 257 } 258 } 259 } 260 261 if (operationName == null) { 262 try { 263 operationName = msg.getStringProperty( WSIFConstants.JMS_PROP_OPERATION_NAME ); 264 input = msg.getStringProperty( WSIFConstants.JMS_PROP_INPUT_NAME ); 265 output = msg.getStringProperty( WSIFConstants.JMS_PROP_OUTPUT_NAME ); 266 } catch (javax.jms.JMSException e) { 267 Trc.exception(e); 268 } 269 } 270 271 if (operationName == null) 272 throw new WSIFException( "Unable to determine Operation" ); 273 274 275 WSIFRequest request = new WSIFRequest( serviceName ); 276 request.setPortName( fieldPort.getName() ); 277 request.setOperationName( operationName ); 278 request.setInputName( input ); 279 request.setOutputName( output ); 280 Trc.exit( request ); 281 return request; 282 } 283 284 protected WSIFResponse createResponse(javax.jms.Message msg) 285 throws WSIFException { 286 Trc.entry(this, msg); 287 javax.xml.namespace.QName serviceName = null; 288 String operationName = null; 289 String input = null; 290 String output = null; 291 292 Binding fieldBinding = fieldPort.getBinding(); 293 if (fieldBinding == null) 294 throw new WSIFException("Unable to locate Binding"); 295 296 Map sm = WSIFUtils.getAllItems( fieldDefinition, "Service" ); 298 Iterator services = sm.values().iterator(); 299 while (services.hasNext() && serviceName ==null) { 300 javax.wsdl.Service s = (javax.wsdl.Service)services.next(); 301 302 for (Iterator ports = s.getPorts().values().iterator(); ports.hasNext();) { 303 javax.wsdl.Port p = (javax.wsdl.Port)ports.next(); 304 if (p.getBinding() != null && p.getBinding().getQName().equals(fieldBinding.getQName())) { 305 serviceName = s.getQName(); 307 break; 308 } 309 } 310 } 311 312 if (serviceName == null) 313 throw new WSIFException("Unable to locate Service"); 314 315 operationName = getOperationName(); 317 input = getInputName(); 318 output = getOutputName(); 319 320 if (operationName == null || operationName.equals("")) { 321 javax.wsdl.PortType portType = fieldBinding.getPortType(); 323 if (portType!= null) { 324 List operations = portType.getOperations(); 325 if (operations != null) { 326 if (operations.size() == 1) { 327 javax.wsdl.Operation o = (javax.wsdl.Operation)operations.get(0); 328 operationName = o.getName(); 329 if (o.getInput() != null) 330 input = o.getInput().getName(); 331 if (o.getOutput() != null) 332 output = o.getOutput().getName(); 333 } 334 else 335 throw new WSIFException("Port Type definition contains multiple operations."); 336 } 337 } 338 } 339 340 if (operationName == null) 341 throw new WSIFException("Unable to locate Operation"); 342 343 WSIFResponse response = new WSIFResponse(serviceName); 344 response.setPortName(fieldPort.getName()); 345 response.setOperationName(operationName); 346 response.setInputName(input); 347 response.setOutputName(output); 348 Trc.exit(response); 349 return response; 350 } 351 352 public void formatResponse(WSIFResponse resp, javax.jms.Message out) 353 throws WSIFException { 354 Trc.entry(this, resp, out); 355 Binding binding = fieldPort.getBinding(); 356 BindingOperation bop = 357 binding.getBindingOperation( 358 resp.getOperationName(), 359 resp.getInputName(), 360 resp.getOutputName()); 361 javax.wsdl.Message outMessage = bop.getOperation().getOutput().getMessage(); 362 363 org.apache.wsif.providers.jms.JMSMessage fhMsg = 364 new org.apache.wsif.providers.jms.JMSMessage( 365 fieldDefinition, 366 binding, 367 outMessage, 368 getOutputParts(bop)); 369 370 copyTo(resp.getOutgoingMessage(), fhMsg); 371 fhMsg.write(out); 372 373 Trc.exit(); 374 } 375 376 379 public WSIFResponse unformatResponse(javax.jms.Message out) 380 throws WSIFException { 381 382 Trc.entry(this, out); 383 WSIFResponse resp = createResponse(out); 385 386 Binding binding = fieldPort.getBinding(); 387 BindingOperation bop = 388 binding.getBindingOperation( 389 resp.getOperationName(), 390 resp.getInputName(), 391 resp.getOutputName()); 392 393 if (unformatResponseFault(resp, out, binding, bop)) { 394 Trc.exit(resp); 395 return resp; 396 } 397 398 Operation op = bop.getOperation(); 399 javax.wsdl.Output output = op.getOutput(); 400 if ( output != null ) { 401 javax.wsdl.Message outMessage = output.getMessage(); 402 403 org.apache.wsif.providers.jms.JMSMessage fhMsg = 404 new org.apache.wsif.providers.jms.JMSMessage( 405 fieldDefinition, 406 binding, 407 outMessage, 408 getOutputParts(bop)); 409 410 fhMsg.read(out); 411 412 resp.setOutgoingMessage(fhMsg); 413 } 414 415 Trc.exit(resp); 416 return resp; 417 } 418 419 423 private boolean unformatResponseFault( 424 WSIFResponse resp, 425 javax.jms.Message out, 426 Binding binding, 427 BindingOperation bop) 428 throws WSIFException { 429 430 Trc.entry(this, resp, out, binding, bop); 431 432 Map bndFs = bop.getBindingFaults(); 434 if (bndFs != null && !bndFs.isEmpty()) { 435 Iterator itBndFNames = bndFs.keySet().iterator(); 436 while (itBndFNames.hasNext()) { 437 String bndFName = (String ) itBndFNames.next(); 438 BindingFault bndF = (BindingFault) bndFs.get(bndFName); 439 440 List bndFElems = bndF.getExtensibilityElements(); 441 if (bndFElems == null || bndFElems.isEmpty()) 442 continue; 443 444 Iterator itBndFElems = bndFElems.iterator(); 445 while (itBndFElems.hasNext()) { 446 Object bndFElem = itBndFElems.next(); 447 448 if (bndFElem instanceof JMSFaultIndicator) { 451 JMSFaultIndicator indic = (JMSFaultIndicator) bndFElem; 452 453 if (matchesFaultIndicator(indic, out)) { 456 WSIFMessage msg = 457 unformatFaultMessage( 458 out, 459 binding, 460 bop, 461 bndFName, 462 bndF); 463 464 resp.setOutgoingMessage(msg); 465 resp.setIsFault(true); 466 lastBindingFault = bndF; 467 Trc.exit(true); 468 return true; 469 } 470 } 471 } 472 } 473 } 474 Trc.exit(false); 475 return false; 476 } 477 478 490 private boolean matchesFaultIndicator( 491 JMSFaultIndicator indic, 492 javax.jms.Message out) { 493 Trc.entry(this, indic, out); 494 495 String type = indic.getType(); 497 if (type == null || !"property".equals(type)) { 498 Trc.exit(false); 499 return false; 500 } 501 502 List fProps = indic.getJMSFaultProperties(); 503 if (fProps == null || fProps.isEmpty()) { 504 Trc.exit(false); 505 return false; 506 } 507 508 Iterator itFProps = fProps.iterator(); 509 while (itFProps.hasNext()) { 510 JMSFaultProperty fProp = (JMSFaultProperty) itFProps.next(); 511 String propName = fProp.getName(); 512 if (propName == null 513 || fProp.getType() == null 514 || fProp.getValue() == null) 515 continue; 516 517 Object propValue = null; 518 try { 519 if (!out.propertyExists(propName)) 520 continue; 521 522 propValue = out.getObjectProperty(propName); 523 } catch (JMSException je) { 524 Trc.ignoredException(je); 525 continue; 526 } 527 528 if (propValue != null 529 && fProp.getValue().equals(propValue.toString())) { 530 Trc.exit(true); 531 return true; 532 } 533 } 534 535 Trc.exit(false); 536 return false; 537 } 538 539 private WSIFMessage unformatFaultMessage( 540 javax.jms.Message out, 541 Binding binding, 542 BindingOperation bop, 543 String bndFName, 544 BindingFault bndF) 545 throws WSIFException { 546 Trc.entry(this, out, binding, bop, bndFName, bndF); 547 548 Operation op = bop.getOperation(); 549 Fault fault = op.getFault(bndFName); 550 if (fault == null) 551 throw new WSIFException( 552 "No fault " 553 + bndFName 554 + " found in operation " 555 + op.getName()); 556 557 JMSMessage fhMsg = 558 new JMSMessage( 559 fieldDefinition, 560 binding, 561 fault.getMessage(), 562 getFaultParts(bndF)); 563 564 fhMsg.read(out); 565 Trc.exit(fhMsg); 566 return fhMsg; 567 568 } 569 570 void copyTo( 571 WSIFMessage source, 572 WSIFMessage target) 573 throws WSIFException { 574 if (source == null || target == null) 575 return; 576 for (Iterator i = source.getPartNames(); i.hasNext();) { 577 String partName = i.next().toString(); 578 target.setObjectPart(partName, source.getObjectPart(partName)); 579 } 580 } 581 582 private List getInputParts( 583 BindingOperation bindingOperation) { 584 if (bindingOperation.getBindingInput() != null) { 585 Iterator inputIterator = 586 bindingOperation.getBindingInput().getExtensibilityElements().iterator(); 587 588 while (inputIterator.hasNext()) { 589 ExtensibilityElement ele = 590 (ExtensibilityElement) inputIterator.next(); 591 if (ele instanceof JMSInput) { 592 return ((JMSInput) ele).getParts(); 593 } 594 } 595 } 596 return null; 597 } 598 599 private List getOutputParts( 600 BindingOperation bindingOperation) { 601 if (bindingOperation.getBindingOutput() != null) { 602 Iterator outputIterator = 603 bindingOperation.getBindingOutput().getExtensibilityElements().iterator(); 604 605 while (outputIterator.hasNext()) { 606 ExtensibilityElement ele = 607 (ExtensibilityElement) outputIterator.next(); 608 if (ele instanceof JMSOutput) { 609 return ((JMSOutput) ele).getParts(); 610 } 611 } 612 } 613 return null; 614 } 615 616 private List getFaultParts(BindingFault bindingFault) { 617 Trc.entry(this, bindingFault); 618 619 List list = null; 620 if (bindingFault != null) { 621 Iterator it = bindingFault.getExtensibilityElements().iterator(); 622 while (it.hasNext()) { 623 Object ele = it.next(); 624 if (ele instanceof JMSFault) { 625 list = ((JMSFault) ele).getParts(); 626 break; 627 } 628 } 629 } 630 Trc.exit(list); 631 return list; 632 } 633 634 private String getOperationName() { 635 return reqOpName; 636 } 637 638 private void setOperationName(String s) { 639 reqOpName = s; 640 } 641 642 private String getInputName() { 643 return reqInputName; 644 } 645 646 private void setInputName(String s) { 647 reqInputName = s; 648 } 649 650 private String getOutputName() { 651 return reqOutputName; 652 } 653 654 private void setOutputName(String s) { 655 reqOutputName = s; 656 } 657 658 public BindingFault getLastBindingFault() { 659 Trc.entry(this); 660 Trc.exit(lastBindingFault); 661 return lastBindingFault; 662 } 663 664 } | Popular Tags |