1 57 58 package org.apache.wsif.providers.jms; 59 60 import java.util.ArrayList ; 61 import java.util.Iterator ; 62 63 import javax.jms.JMSException ; 64 import javax.jms.ObjectMessage ; 65 import javax.jms.TextMessage ; 66 import javax.wsdl.Binding; 67 import javax.wsdl.Definition; 68 import javax.wsdl.Message; 69 70 import org.apache.wsif.WSIFException; 71 import org.apache.wsif.base.WSIFDefaultMessage; 72 import org.apache.wsif.format.jms.JMSFormatHandler; 73 import org.apache.wsif.logging.Trc; 74 import org.apache.wsif.util.WSIFUtils; 75 import org.apache.wsif.wsdl.extensions.format.TypeMapping; 76 import org.apache.wsif.wsdl.extensions.jms.JMSBinding; 77 import org.apache.wsif.wsdl.extensions.jms.JMSConstants; 78 84 public class JMSMessage extends WSIFDefaultMessage { 85 private static final long serialVersionUID = 1L; 86 87 private static String XML_ENCODING = "XML"; 89 private static String JAVA_ENCODING = "Java"; 90 91 static String XML_SCHEMA_1999 = "http://www.w3.org/1999/XMLSchema"; 92 static String XML_SCHEMA_2000_10 = "http://www.w3.org/2000/10/XMLSchema"; 93 static String XML_SCHEMA_2001 = "http://www.w3.org/2001/XMLSchema"; 94 95 static final java.util.HashMap PRIMITIVE_JAVA_MAPPING = 96 new java.util.HashMap (); 97 98 static { 101 PRIMITIVE_JAVA_MAPPING.put("anySimpleType", java.lang.String .class); 102 PRIMITIVE_JAVA_MAPPING.put("anyURI", java.lang.String .class); 103 PRIMITIVE_JAVA_MAPPING.put("base64Binary", byte[].class); 104 PRIMITIVE_JAVA_MAPPING.put("boolean", java.lang.Boolean .class); 105 PRIMITIVE_JAVA_MAPPING.put("byte", java.lang.Byte .class); 106 PRIMITIVE_JAVA_MAPPING.put("date", java.util.GregorianCalendar .class); 107 PRIMITIVE_JAVA_MAPPING.put("dateTime", java.util.Date .class); 108 PRIMITIVE_JAVA_MAPPING.put("decimal", java.math.BigDecimal .class); 109 PRIMITIVE_JAVA_MAPPING.put("double", java.lang.Double .class); 110 PRIMITIVE_JAVA_MAPPING.put("duration", java.lang.String .class); 111 PRIMITIVE_JAVA_MAPPING.put("ENTITIES", java.lang.String .class); 112 PRIMITIVE_JAVA_MAPPING.put("ENTITY", java.lang.String .class); 113 PRIMITIVE_JAVA_MAPPING.put("float", java.lang.Float .class); 114 PRIMITIVE_JAVA_MAPPING.put("gDay", java.lang.String .class); 115 PRIMITIVE_JAVA_MAPPING.put("gMonth", java.lang.String .class); 116 PRIMITIVE_JAVA_MAPPING.put("gMonthDay", java.lang.String .class); 117 PRIMITIVE_JAVA_MAPPING.put("gYear", java.lang.String .class); 118 PRIMITIVE_JAVA_MAPPING.put("gYearMonth", java.lang.String .class); 119 PRIMITIVE_JAVA_MAPPING.put("hexBinary", byte[].class); 120 PRIMITIVE_JAVA_MAPPING.put("ID", java.lang.String .class); 121 PRIMITIVE_JAVA_MAPPING.put("IDREF", java.lang.String .class); 122 PRIMITIVE_JAVA_MAPPING.put("IDREFS", java.lang.String .class); 123 PRIMITIVE_JAVA_MAPPING.put("int", java.lang.Integer .class); 124 PRIMITIVE_JAVA_MAPPING.put("integer", java.math.BigInteger .class); 125 PRIMITIVE_JAVA_MAPPING.put("language", java.lang.String .class); 126 PRIMITIVE_JAVA_MAPPING.put("long", java.lang.Long .class); 127 PRIMITIVE_JAVA_MAPPING.put("Name", java.lang.String .class); 128 PRIMITIVE_JAVA_MAPPING.put("NCName", java.lang.String .class); 129 PRIMITIVE_JAVA_MAPPING.put( 130 "negativeInteger", 131 java.math.BigInteger .class); 132 PRIMITIVE_JAVA_MAPPING.put("NMTOKEN", java.lang.String .class); 133 PRIMITIVE_JAVA_MAPPING.put("NMTOKENS", java.lang.String .class); 134 PRIMITIVE_JAVA_MAPPING.put( 135 "nonNegativeInteger", 136 java.math.BigInteger .class); 137 PRIMITIVE_JAVA_MAPPING.put( 138 "nonPositiveInteger", 139 java.math.BigInteger .class); 140 PRIMITIVE_JAVA_MAPPING.put("normalizedString", java.lang.String .class); 141 PRIMITIVE_JAVA_MAPPING.put("NOTATION", java.lang.String .class); 142 PRIMITIVE_JAVA_MAPPING.put( 143 "positiveInteger", 144 java.math.BigInteger .class); 145 PRIMITIVE_JAVA_MAPPING.put("QName", javax.xml.namespace.QName .class); 146 PRIMITIVE_JAVA_MAPPING.put("short", java.lang.Short .class); 147 PRIMITIVE_JAVA_MAPPING.put("string", java.lang.String .class); 148 PRIMITIVE_JAVA_MAPPING.put("time", java.lang.String .class); 149 PRIMITIVE_JAVA_MAPPING.put("token", java.lang.String .class); 150 PRIMITIVE_JAVA_MAPPING.put("unsignedByte", java.lang.Short .class); 151 PRIMITIVE_JAVA_MAPPING.put("unsignedInt", java.lang.Long .class); 152 PRIMITIVE_JAVA_MAPPING.put("unsignedLong", java.math.BigInteger .class); 153 PRIMITIVE_JAVA_MAPPING.put("unsignedShort", java.lang.Integer .class); 154 } 155 156 private Definition fieldDefinitionModel; 157 private Binding fieldBindingModel; 158 private Message fieldMessageModel; 159 private java.util.List fieldMessageParts; 160 161 164 public JMSMessage( 165 Definition definitionModel, 166 Binding bindingModel, 167 Message messageModel, 168 java.util.List parts) { 169 super(); 170 Trc.entry(this, definitionModel, bindingModel, messageModel, parts); 171 fieldDefinitionModel = definitionModel; 172 fieldBindingModel = bindingModel; 173 fieldMessageModel = messageModel; 174 fieldMessageParts = parts; 175 Trc.exit(); 176 } 177 178 181 public void write(javax.jms.Message message) throws WSIFException { 182 Trc.entry(this, message); 183 184 if (!isCorrectMessageType(message)) 185 throw new WSIFException("Incorrect message type"); 186 187 if (message instanceof javax.jms.TextMessage ) 188 write((javax.jms.TextMessage ) message); 189 else if (message instanceof javax.jms.ObjectMessage ) 190 write((javax.jms.ObjectMessage ) message); 191 else 192 throw new WSIFException( 193 "Unsupported Message Type: " + message.getClass().getName()); 194 Trc.exit(); 195 } 196 197 200 private void write(javax.jms.TextMessage message) throws WSIFException { 201 Trc.entry(this, message); 202 203 if (!XML_ENCODING.equals(getFormatEncoding(fieldBindingModel))) 205 throw new WSIFException("Unable to support non XML encodings in a JMS Text Message"); 206 207 try { 208 ArrayList al = new ArrayList (); 209 for (Iterator i = this.getPartNames(); i.hasNext();) { 210 al.add(i.next()); 211 } 212 String [] partNames = (String []) al.toArray(new String [al.size()]); 213 214 if (partNames.length == 1) { 215 String partName = partNames[0]; 217 218 javax.wsdl.Part partModel = fieldMessageModel.getPart(partName); 219 220 javax.xml.namespace.QName partQName = 221 new javax.xml.namespace.QName ( 222 fieldMessageModel.getQName().getNamespaceURI(), 223 partModel.getName()); 224 225 JMSFormatHandler fh = getFormatHandler(partName); 226 227 if (fh != null) { 228 fh.setPartQName(partQName); 229 fh.setObjectPart(parts.get(partName)); 230 fh.write(message); 231 } else { 232 Object part = parts.get(partName); 234 message.setText(part.toString()); 235 } 236 } else { 237 java.io.ByteArrayOutputStream os = 240 new java.io.ByteArrayOutputStream (); 241 java.io.OutputStreamWriter writer = 242 new java.io.OutputStreamWriter (os); 243 244 org.apache.xml.serialize.OutputFormat format = 245 new org.apache.xml.serialize.OutputFormat(); 246 org.apache.xml.serialize.XMLSerializer serializer = 247 new org.apache.xml.serialize.XMLSerializer(writer, format); 248 249 String namespace = ""; 250 251 serializer.startDocument(); 253 254 serializer.startElement( 256 namespace, 257 fieldMessageModel.getQName().getLocalPart(), 258 "", 259 new org.xml.sax.helpers.AttributesImpl ()); 260 261 for (int i = 0; i < partNames.length; i++) { 263 String partName = partNames[i]; 264 265 serializer.startElement( 267 namespace, 268 partName, 269 "", 270 new org.xml.sax.helpers.AttributesImpl ()); 271 272 javax.wsdl.Part partModel = 273 fieldMessageModel.getPart(partName); 274 javax.xml.namespace.QName partQName = 275 new javax.xml.namespace.QName ( 276 fieldMessageModel.getQName().getNamespaceURI(), 277 partModel.getName()); 278 279 JMSFormatHandler fh = getFormatHandler(partName); 281 282 if (fh != null) { 283 fh.setPartQName(partQName); 284 fh.setObjectPart(parts.get(partName)); 285 286 message.setText(""); 289 fh.write(message); 290 char[] c = message.getText().toCharArray(); 291 serializer.characters(c, 0, c.length); 292 } else { 293 Object part = parts.get(partName); 295 char[] c = part.toString().toCharArray(); 296 serializer.characters(c, 0, c.length); 297 } 298 299 serializer.endElement(partName); 301 } 302 303 serializer.endElement( 305 fieldMessageModel.getQName().getLocalPart()); 306 307 serializer.endDocument(); 309 310 writer.flush(); 311 String msgContents = os.toString(); 312 message.setText(msgContents); 314 } 315 316 } catch (JMSException e) { 317 Trc.exception(e); 318 throw new WSIFException("Error in write.", e); 319 } catch (java.io.IOException e) { 320 Trc.exception(e); 321 throw new WSIFException("Error in write.", e); 322 } catch (org.xml.sax.SAXException e) { 323 Trc.exception(e); 324 throw new WSIFException("Error in write.", e); 325 } 326 Trc.exit(); 327 } 328 329 332 private void write(javax.jms.ObjectMessage message) throws WSIFException { 333 Trc.entry(this, message); 334 335 if (!JAVA_ENCODING.equals(getFormatEncoding(fieldBindingModel))) 337 throw new WSIFException("Unable to support non Java encodings in a JMS Object Message"); 338 339 try { 340 341 ArrayList al = new ArrayList (); 342 for (Iterator i = this.getPartNames(); i.hasNext();) { 343 al.add(i.next()); 344 } 345 String [] partNames = (String []) al.toArray(new String [al.size()]); 346 347 if (partNames.length == 1) { 348 String partName = partNames[0]; 350 351 javax.wsdl.Part partModel = fieldMessageModel.getPart(partName); 352 353 javax.xml.namespace.QName partQName = 354 new javax.xml.namespace.QName ( 355 fieldMessageModel.getQName().getNamespaceURI(), 356 partModel.getName()); 357 358 JMSFormatHandler fh = getFormatHandler(partName); 359 360 if (fh != null) { 361 fh.setPartQName(partQName); 362 fh.setObjectPart(parts.get(partName)); 363 fh.write(message); 364 } else { 365 Object part = parts.get(partName); 367 try { 370 message.setObject((java.io.Serializable ) part); 371 } catch (ClassCastException e) { 372 Trc.exception(e); 373 throw new WSIFException("Unable to serialize a part"); 374 } 375 } 376 } else { 377 java.util.HashMap result = new java.util.HashMap (); 379 for (int i = 0; i < partNames.length; i++) { 380 String partName = partNames[i]; 381 382 javax.wsdl.Part partModel = 383 fieldMessageModel.getPart(partName); 384 385 javax.xml.namespace.QName partQName = 386 new javax.xml.namespace.QName ( 387 fieldMessageModel.getQName().getNamespaceURI(), 388 partModel.getName()); 389 390 JMSFormatHandler fh = getFormatHandler(partName); 391 392 if (fh != null) { 393 fh.setPartQName(partQName); 394 fh.setObjectPart(parts.get(partName)); 395 fh.write(message); 396 result.put(partName, message.getObject()); 397 } else { 398 Object part = parts.get(partName); 400 try { 403 result.put(partName, (java.io.Serializable ) part); 404 } catch (ClassCastException e) { 405 Trc.exception(e); 406 throw new WSIFException("Unable to serialize a part"); 407 } 408 } 409 } 410 message.setObject(result); 411 } 412 413 } catch (JMSException e) { 414 Trc.exception(e); 415 throw new WSIFException("Error in write.", e); 416 } 417 Trc.exit(); 418 } 419 420 423 public void read(javax.jms.Message message) throws WSIFException { 424 Trc.entry(this, message); 425 426 if (!isCorrectMessageType(message)) 427 throw new WSIFException("Incorrect message type"); 428 429 if (message instanceof javax.jms.TextMessage ) 430 read((javax.jms.TextMessage ) message); 431 else if (message instanceof javax.jms.ObjectMessage ) 432 read((javax.jms.ObjectMessage ) message); 433 else 434 throw new WSIFException( 435 "Unsupported Message Type: " + message.getClass().getName()); 436 Trc.exit(); 437 } 438 439 442 private void read(javax.jms.TextMessage message) throws WSIFException { 443 Trc.entry(this, message); 444 445 if (!XML_ENCODING.equals(getFormatEncoding(fieldBindingModel))) 447 throw new WSIFException("Unable to support non XML encodings in a JMS Text Message"); 448 449 boolean wsifFormat = false; 450 451 try { 452 453 javax.xml.parsers.DocumentBuilderFactory factory = 455 javax.xml.parsers.DocumentBuilderFactory.newInstance(); 456 factory.setNamespaceAware(true); 457 factory.setValidating(false); 458 459 javax.xml.parsers.DocumentBuilder builder = 460 factory.newDocumentBuilder(); 461 builder.setErrorHandler(null); 462 463 String text = message.getText(); 464 java.io.ByteArrayInputStream is = 465 new java.io.ByteArrayInputStream (text.getBytes()); 466 org.w3c.dom.Document doc = builder.parse(is); 467 468 if (fieldMessageModel 470 .getQName() 471 .getLocalPart() 472 .equals(doc.getDocumentElement().getLocalName())) { 473 474 wsifFormat = true; 475 476 message.clearBody(); 478 479 for (org.w3c.dom.Node n = 482 doc.getDocumentElement().getFirstChild(); 483 n != null; 484 n = n.getNextSibling()) { 485 if (n.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 486 String partName = n.getLocalName(); 487 488 java.io.ByteArrayOutputStream os = 489 new java.io.ByteArrayOutputStream (); 490 491 org.apache.xml.serialize.OutputFormat format = 492 new org.apache.xml.serialize.OutputFormat(); 493 org.apache.xml.serialize.TextSerializer serializer = 494 new org.apache.xml.serialize.TextSerializer(); 495 serializer.setOutputFormat(format); 496 serializer.setOutputByteStream(os); 497 498 format.setOmitXMLDeclaration(true); 499 501 serializer.serialize((org.w3c.dom.Element ) n); 502 os.flush(); 503 String partText = os.toString(); 505 506 javax.wsdl.Part partModel = 507 fieldMessageModel.getPart(partName); 508 JMSFormatHandler fh = getFormatHandler(partName); 509 510 if (fh != null) { 511 512 fh.setPartQName( 513 new javax.xml.namespace.QName ( 514 fieldMessageModel 515 .getQName() 516 .getNamespaceURI(), 517 partModel.getName())); 518 519 message.setText(partText); 520 fh.read(message); 521 523 setObjectPart(partName, fh.getObjectPart()); 524 525 528 } else { 529 setObjectPart(partName, partText); 531 } 532 } 533 } 534 535 message.setText(text); 537 } 538 } catch (JMSException e) { 539 Trc.exception(e); 540 throw new WSIFException("Error in read.", e); 541 } catch (javax.xml.parsers.ParserConfigurationException e) { 542 Trc.exception(e); 543 throw new WSIFException("Error in read.", e); 544 } catch (Exception e) { 545 Trc.exception(e); 546 } 548 549 try { 550 if (!wsifFormat) { 551 Object [] partNames = 554 fieldMessageParts != null 555 ? fieldMessageParts.toArray() 556 : fieldMessageModel.getParts().keySet().toArray(); 557 558 if (partNames.length != 1) 560 throw new WSIFException( 561 "There should only be one part defined in " 562 + fieldMessageModel.getQName().getLocalPart()); 563 564 String partName = partNames[0].toString(); 565 566 javax.wsdl.Part partModel = fieldMessageModel.getPart(partName); 567 JMSFormatHandler fh = getFormatHandler(partName); 568 569 if (fh != null) { 570 fh.setPartQName( 571 new javax.xml.namespace.QName ( 572 fieldMessageModel.getQName().getNamespaceURI(), 573 partModel.getName())); 574 fh.read(message); 575 576 setObjectPart(partName, fh.getObjectPart()); 577 578 581 } else { 582 setObjectPart(partName, message.getText()); 584 } 585 } 586 } catch (JMSException e) { 587 Trc.exception(e); 588 throw new WSIFException("Error in read.", e); 589 } 590 Trc.exit(); 591 } 592 593 596 private void read(javax.jms.ObjectMessage message) throws WSIFException { 597 Trc.entry(this, message); 598 599 if (!JAVA_ENCODING.equals(getFormatEncoding(fieldBindingModel))) 601 throw new WSIFException("Unable to support non Java encodings in a JMS Object Message"); 602 603 try { 604 Object object = message.getObject(); 605 Object [] partNames = 606 fieldMessageParts != null 607 ? fieldMessageParts.toArray() 608 : fieldMessageModel.getParts().keySet().toArray(); 609 610 if (partNames.length == 0) 612 return; 613 614 if (object instanceof java.util.Map ) { 616 message.clearBody(); 618 619 java.util.Map map = (java.util.Map ) object; 620 621 for (int i = 0; i < partNames.length; i++) { 623 String partName = partNames[i].toString(); 624 625 if (map.containsKey(partName)) { 626 javax.wsdl.Part partModel = 627 fieldMessageModel.getPart(partName); 628 JMSFormatHandler fh = getFormatHandler(partName); 629 630 if (fh != null) { 631 632 fh.setPartQName( 633 new javax.xml.namespace.QName ( 634 fieldMessageModel 635 .getQName() 636 .getNamespaceURI(), 637 partModel.getName())); 638 message.setObject( 640 (java.io.Serializable ) map.get(partName)); 641 fh.read(message); 642 setObjectPart(partName, fh.getObjectPart()); 644 645 648 } else { 649 setObjectPart(partName, map.get(partName)); 651 } 652 } 653 654 } 655 message.setObject((java.io.Serializable ) object); 657 } else { 658 661 if (partNames.length != 1) 663 throw new WSIFException( 664 "There should only be one part defined in " 665 + fieldMessageModel.getQName().getLocalPart() 666 + " or the JMS ObjectMessage should be a Map of " 667 + "all the parts in the message"); 668 669 String partName = partNames[0].toString(); 670 671 javax.wsdl.Part partModel = fieldMessageModel.getPart(partName); 672 JMSFormatHandler fh = getFormatHandler(partName); 673 674 if (fh != null) { 675 fh.setPartQName( 676 new javax.xml.namespace.QName ( 677 fieldMessageModel.getQName().getNamespaceURI(), 678 partModel.getName())); 679 fh.read(message); 680 681 setObjectPart(partName, fh.getObjectPart()); 682 683 686 } else { 687 setObjectPart(partName, message.getObject()); 689 } 690 691 } 692 } catch (JMSException e) { 693 Trc.exception(e); 694 throw new WSIFException("Error in read.", e); 695 } 696 Trc.exit(); 697 } 698 699 private static String getFormatEncoding(Binding bindingModel) { 701 Trc.entry(null, bindingModel); 702 703 java.util.Iterator iterator = 704 bindingModel.getExtensibilityElements().iterator(); 705 706 while (iterator.hasNext()) { 707 javax.wsdl.extensions.ExtensibilityElement ee = 708 (javax.wsdl.extensions.ExtensibilityElement) iterator.next(); 709 if (ee instanceof TypeMapping) { 710 TypeMapping typeMapping = (TypeMapping) ee; 711 String s = typeMapping.getEncoding(); 712 Trc.exit(s); 713 return s; 714 } 715 } 716 717 Trc.exit(null); 718 return null; 719 } 720 721 private JMSFormatHandler getFormatHandler(String partName) { 722 Trc.entry(this, partName); 723 724 javax.wsdl.Part partModel = fieldMessageModel.getPart(partName); 725 JMSFormatHandler fh = null; 726 727 javax.xml.namespace.QName partType = partModel.getTypeName(); 728 if (partType == null) 729 partType = partModel.getElementName(); 730 731 try { 732 if (isSchemaNamespace(partType.getNamespaceURI()) 734 && isXSDPrimitiveType(partType.getLocalPart())) 735 fh = 736 new PrimitiveTypeFormatHandler( 737 (Class ) PRIMITIVE_JAVA_MAPPING.get( 738 partType.getLocalPart().toLowerCase())); 739 else 740 fh = (JMSFormatHandler) 741 WSIFUtils.getFormatHandler( 743 partModel, 744 this.fieldDefinitionModel, 745 this.fieldBindingModel); 746 } catch (java.lang.Exception e) { 747 Trc.exception(e); 748 } 749 750 Trc.exit(fh); 751 return fh; 752 } 753 754 757 static boolean isSchemaNamespace(String namespaceURI) { 758 return XML_SCHEMA_1999.equals(namespaceURI) 759 || XML_SCHEMA_2000_10.equals(namespaceURI) 760 || XML_SCHEMA_2001.equals(namespaceURI); 761 } 762 763 static boolean isXSDPrimitiveType(String type) { 764 Trc.entry(null, type); 765 766 Object [] types = PRIMITIVE_JAVA_MAPPING.keySet().toArray(); 767 for (int i = 0; i < types.length; i++) { 768 if (types[i].toString().equalsIgnoreCase(type)) { 769 Trc.exit(true); 770 return true; 771 } 772 } 773 Trc.exit(false); 774 return false; 775 } 776 777 private int getMessageType() { 778 Trc.entry(this); 779 780 java.util.Iterator iterator = 781 fieldBindingModel.getExtensibilityElements().iterator(); 782 783 while (iterator.hasNext()) { 784 javax.wsdl.extensions.ExtensibilityElement ee = 785 (javax.wsdl.extensions.ExtensibilityElement) iterator.next(); 786 if (ee instanceof JMSBinding) { 787 JMSBinding jmsBinding = (JMSBinding) ee; 788 int type = jmsBinding.getJmsMessageType(); 789 Trc.exit(type); 790 return type; 791 } 792 } 793 794 int type = 795 org 796 .apache 797 .wsif 798 .wsdl 799 .extensions 800 .jms 801 .JMSConstants 802 .MESSAGE_TYPE_NOTSET; 803 Trc.exit(type); 804 return type; 805 } 806 807 private boolean isCorrectMessageType(javax.jms.Message message) { 808 Trc.entry(this, message); 809 int type = getMessageType(); 810 811 boolean result = 812 (message instanceof TextMessage 813 && type == JMSConstants.MESSAGE_TYPE_TEXTMESSAGE) 814 || (message instanceof ObjectMessage 815 && type == JMSConstants.MESSAGE_TYPE_OBJECTMESSAGE) 816 || (message instanceof javax.jms.StreamMessage 817 && type == JMSConstants.MESSAGE_TYPE_STREAMMESSAGE) 818 || (message instanceof javax.jms.BytesMessage 819 && type == JMSConstants.MESSAGE_TYPE_BYTEMESSAGE) 820 || (message instanceof javax.jms.MapMessage 821 && type == JMSConstants.MESSAGE_TYPE_MAPMESSAGE); 822 823 Trc.exit(result); 824 return result; 825 } 826 } | Popular Tags |