1 package org.objectweb.celtix.bus.jaxws.io; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.InputStream ; 6 7 import javax.xml.bind.JAXBContext; 8 import javax.xml.bind.Unmarshaller; 9 import javax.xml.namespace.QName ; 10 import javax.xml.soap.SOAPBody ; 11 import javax.xml.transform.Source ; 12 import javax.xml.transform.dom.DOMSource ; 13 import javax.xml.transform.sax.SAXSource ; 14 import javax.xml.transform.stream.StreamSource ; 15 16 import org.w3c.dom.Document ; 17 import org.w3c.dom.bootstrap.DOMImplementationRegistry ; 18 import org.w3c.dom.ls.DOMImplementationLS ; 19 import org.w3c.dom.ls.LSOutput ; 20 import org.w3c.dom.ls.LSSerializer ; 21 22 import org.xml.sax.InputSource ; 23 24 import org.objectweb.celtix.bindings.DataReader; 25 import org.objectweb.celtix.bus.jaxws.DynamicDataBindingCallback; 26 import org.objectweb.celtix.context.ObjectMessageContext; 27 28 public class SOAPBodyDataReader<T> implements DataReader<T> { 29 30 final DynamicDataBindingCallback callback; 31 32 public SOAPBodyDataReader(DynamicDataBindingCallback cb) { 33 callback = cb; 34 } 35 36 public Object read(int idx, T input) { 37 Source obj = null; 38 SOAPBody src = (SOAPBody )input; 39 try { 40 Document doc = src.extractContentAsDocument(); 41 assert doc != null; 42 43 if (DOMSource .class.isAssignableFrom(callback.getSupportedFormats()[0])) { 44 obj = new DOMSource (); 45 ((DOMSource )obj).setNode(doc); 46 } else if (SAXSource .class.isAssignableFrom(callback.getSupportedFormats()[0])) { 47 InputSource inputSource = new InputSource (getSOAPBodyStream(doc)); 48 obj = new SAXSource (inputSource); 49 } else if (StreamSource .class.isAssignableFrom(callback.getSupportedFormats()[0])) { 50 obj = new StreamSource (getSOAPBodyStream(doc)); 51 } else if (Object .class.isAssignableFrom(callback.getSupportedFormats()[0])) { 52 JAXBContext context = callback.getJAXBContext(); 53 Unmarshaller u = context.createUnmarshaller(); 54 return u.unmarshal(doc); 55 } 56 } catch (Exception se) { 57 se.printStackTrace(); 58 } 59 return obj; 60 } 61 62 public Object read(QName name, int idx, T input) { 63 return null; 65 } 66 67 public void readWrapper(ObjectMessageContext objCtx, boolean isOutBound, T input) { 68 } 70 71 private InputStream getSOAPBodyStream(Document doc) throws Exception { 72 System.setProperty(DOMImplementationRegistry.PROPERTY, 73 "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); 74 DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 75 DOMImplementationLS impl = (DOMImplementationLS )registry.getDOMImplementation("LS"); 76 LSOutput output = impl.createLSOutput(); 77 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (); 78 output.setByteStream(byteArrayOutputStream); 79 LSSerializer writer = impl.createLSSerializer(); 80 writer.write(doc, output); 81 byte[] buf = byteArrayOutputStream.toByteArray(); 82 return new ByteArrayInputStream (buf); 83 } 84 85 } 86 | Popular Tags |