1 22 package org.jboss.test.webservice.message; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.StringWriter ; 26 import java.rmi.RemoteException ; 27 28 import javax.xml.namespace.QName ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.rpc.ServiceException ; 32 import javax.xml.rpc.ServiceFactory ; 33 34 import org.jboss.logging.Logger; 35 import org.jboss.util.xml.DOMUtils; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.NodeList ; 39 40 import com.ibm.wsdl.util.xml.DOM2Writer; 41 42 46 public class MessageJavaBean implements Message 47 { 48 private final Logger log = Logger.getLogger(MessageJavaBean.class); 50 51 53 public Element processElement(Element msg) throws RemoteException 54 { 55 StringWriter swr = new StringWriter (); 56 DOM2Writer.serializeAsXML(msg, swr); 57 log.info("processElement: " + swr); 58 59 try 60 { 61 Element reqEl = (Element )msg; 62 63 QName qname = new QName (NSURI_1, "Order", PREFIX_1); 65 QName elementName = new QName (reqEl.getNamespaceURI(), reqEl.getLocalName(), reqEl.getPrefix()); 66 if (qname.equals(elementName) == false) 67 throw new IllegalArgumentException ("Unexpected element: " + elementName); 68 69 String attrVal = reqEl.getAttribute("attrval"); 71 if ("somevalue".equals(attrVal) == false) 72 throw new IllegalArgumentException ("Unexpected attribute value: " + attrVal); 73 74 if (isWS4EEAvailable() == false) 76 { 77 82 String nsURI_1 = reqEl.getAttribute("xmlns:" + PREFIX_1); 84 if (NSURI_1.equals(nsURI_1) == false) 85 throw new IllegalArgumentException ("Unexpected namespace URI: " + nsURI_1); 86 87 String nsURI_2 = reqEl.getAttribute("xmlns:" + PREFIX_2); 88 if (NSURI_2.equals(nsURI_2) == false) 89 throw new IllegalArgumentException ("Unexpected namespace URI: " + nsURI_2); 90 } 91 92 NodeList nodeList1 = reqEl.getElementsByTagNameNS(NSURI_2, "Customer"); 95 if (nodeList1.getLength() != 1) 96 throw new IllegalArgumentException ("Cannot getElementsByTagNameNS"); 97 98 NodeList nodeList2 = reqEl.getElementsByTagName("Item"); 101 if (nodeList2.getLength() != 1) 102 throw new IllegalArgumentException ("Cannot getElementsByTagName"); 103 104 qname = new QName (NSURI_2, "Customer", PREFIX_2); 106 Element custEl = DOMUtils.getFirstChildElement(reqEl, qname); 107 String elementValue = DOMUtils.getTextContent(custEl); 108 if ("Customer".equals(custEl.getLocalName()) == false || "Kermit".equals(elementValue) == false) 109 throw new IllegalArgumentException ("Unexpected element value: " + elementValue); 110 111 qname = new QName ("Item"); 113 Element itemEl = DOMUtils.getFirstChildElement(reqEl, qname); 114 elementValue = DOMUtils.getTextContent(itemEl); 115 if ("Item".equals(itemEl.getLocalName()) == false || "Ferrari".equals(elementValue) == false) 116 throw new IllegalArgumentException ("Unexpected element value: " + elementValue); 117 118 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 120 docBuilderFactory.setNamespaceAware(true); 121 122 DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); 124 Document doc = builder.parse(new ByteArrayInputStream (response.getBytes())); 125 Element resElement = doc.getDocumentElement(); 126 127 return resElement; 128 } 129 catch (RuntimeException e) 130 { 131 throw e; 132 } 133 catch (Exception e) 134 { 135 throw new RemoteException (e.toString(), e); 136 } 137 } 138 139 private boolean isWS4EEAvailable() 140 { 141 try 142 { 143 ServiceFactory factory = ServiceFactory.newInstance(); 144 if ("org.jboss.webservice.client.ServiceFactoryImpl".equals(factory.getClass().getName())) 145 return true; 146 } 147 catch (ServiceException e) 148 { 149 } 151 return false; 152 } 153 } 154 | Popular Tags |