01: package org.objectweb.celtix.bus.jaxws.io;
02:
03: import java.io.ByteArrayInputStream;
04: import java.io.ByteArrayOutputStream;
05: import java.io.InputStream;
06:
07: import javax.xml.bind.JAXBContext;
08: import javax.xml.bind.Unmarshaller;
09: 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
44: .getSupportedFormats()[0])) {
45: obj = new DOMSource();
46: ((DOMSource) obj).setNode(doc);
47: } else if (SAXSource.class.isAssignableFrom(callback
48: .getSupportedFormats()[0])) {
49: InputSource inputSource = new InputSource(
50: getSOAPBodyStream(doc));
51: obj = new SAXSource(inputSource);
52: } else if (StreamSource.class.isAssignableFrom(callback
53: .getSupportedFormats()[0])) {
54: obj = new StreamSource(getSOAPBodyStream(doc));
55: } else if (Object.class.isAssignableFrom(callback
56: .getSupportedFormats()[0])) {
57: JAXBContext context = callback.getJAXBContext();
58: Unmarshaller u = context.createUnmarshaller();
59: return u.unmarshal(doc);
60: }
61: } catch (Exception se) {
62: se.printStackTrace();
63: }
64: return obj;
65: }
66:
67: public Object read(QName name, int idx, T input) {
68: //Complete
69: return null;
70: }
71:
72: public void readWrapper(ObjectMessageContext objCtx,
73: boolean isOutBound, T input) {
74: //Complete
75: }
76:
77: private InputStream getSOAPBodyStream(Document doc)
78: throws Exception {
79: System
80: .setProperty(DOMImplementationRegistry.PROPERTY,
81: "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
82: DOMImplementationRegistry registry = DOMImplementationRegistry
83: .newInstance();
84: DOMImplementationLS impl = (DOMImplementationLS) registry
85: .getDOMImplementation("LS");
86: LSOutput output = impl.createLSOutput();
87: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
88: output.setByteStream(byteArrayOutputStream);
89: LSSerializer writer = impl.createLSSerializer();
90: writer.write(doc, output);
91: byte[] buf = byteArrayOutputStream.toByteArray();
92: return new ByteArrayInputStream(buf);
93: }
94:
95: }
|