1
2
3
4
5
6
7
8 package it.imolinfo.jbi4corba.jbi.cxf;
9
10 import com.sun.tools.internal.xjc.runtime.JAXBContextFactory;
11 import it.imolinfo.jbi4corba.Logger;
12 import it.imolinfo.jbi4corba.LoggerFactory;
13 import it.imolinfo.jbi4corba.jbi.Messages;
14
15 import java.io.OutputStream;
16 import java.lang.reflect.Array;
17 import java.util.Collection;
18 import java.util.List;
19
20 import javax.xml.bind.JAXBContext;
21 import javax.xml.bind.JAXBElement;
22 import javax.xml.bind.JAXBException;
23 import javax.xml.bind.Marshaller;
24 import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
25 import javax.xml.namespace.QName;
26 import javax.xml.stream.XMLEventWriter;
27 import javax.xml.stream.XMLStreamWriter;
28
29 import org.apache.cxf.common.logging.LogUtils;
30 import org.apache.cxf.interceptor.Fault;
31 import org.apache.cxf.service.model.MessagePartInfo;
32 import org.w3c.dom.Node;
33
34
35
36
37
38
39
40
41 public final class OutMarshallerHelper {
42
43
44
45
46 private static final transient Logger LOG = LoggerFactory.getLogger(OutMarshallerHelper.class);
47
48 @SuppressWarnings("unused")
49 private static final Messages MESSAGES =
50 Messages.getMessages(OutMarshallerHelper.class);
51
52 private static final java.util.logging.Logger CXFLOG = LogUtils.getLogger(Jbi4CorbaBareOutInterceptor.class);
53
54
55
56
57
58
59
60
61
62 public static void writeObject(Marshaller u, Object source, Object mObj)
63 throws Fault, JAXBException {
64 if (source instanceof XMLStreamWriter) {
65 u.marshal(mObj, (XMLStreamWriter) source);
66 } else if (source instanceof OutputStream) {
67 u.marshal(mObj, (OutputStream) source);
68 } else if (source instanceof Node) {
69 u.marshal(mObj, (Node) source);
70 } else if (source instanceof XMLEventWriter) {
71 u.marshal(mObj, (XMLEventWriter) source);
72 } else {
73 throw new Fault(new org.apache.cxf.common.i18n.Message(
74 "UNKNOWN_SOURCE", CXFLOG, source.getClass().getName()));
75 }
76 }
77
78
79
80
81
82
83
84
85
86
87 @SuppressWarnings("unchecked")
88 public static void marshal(Marshaller marshaller, Object elValue,
89 MessagePartInfo part, Object source) throws Fault {
90
91 try {
92
93
94 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
95 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
96 } catch (javax.xml.bind.PropertyException e) {
97
98 LOG.debug(e.getMessage());
99 }
100
101 Class<?> cls = null;
102 if (part != null) {
103 cls = part.getTypeClass();
104 }
105
106 if (cls == null) {
107 cls = null != elValue ? elValue.getClass() : null;
108 }
109
110 if (cls != null && cls.isArray() && elValue instanceof Collection) {
111 Collection<?> col = (Collection<?>) elValue;
112 elValue = col.toArray((Object[]) Array.newInstance(cls.getComponentType(), col.size()));
113 }
114
115 try {
116 Object mObj = elValue;
117 QName elName = null;
118 if (part != null) {
119 elName = part.getConcreteName();
120 }
121
122 if (null != elName) {
123
124 if (part != null) {
125
126
127
128
129
130
131
132
133
134
135
136
137
138 if (mObj == null) {
139 Class clazz = part.getTypeClass();
140 LOG.debug("null value deducting type class: "+clazz.toString());
141
142
143
144
145
146
147
148
149 OutMarshallerHelper.writeObject(marshaller, source, new JAXBElement(elName, Object.class, null));
150
151 return;
152 }
153
154
155 if (mObj.getClass().isArray()) {
156
157
158 Object objArray;
159 if (mObj instanceof List) {
160 List l = (List) mObj;
161 objArray = l.toArray(new Object[l.size()]);
162 cls = null;
163 } else {
164 objArray = mObj;
165 cls = objArray.getClass().getComponentType();
166 }
167 int len = Array.getLength(objArray);
168 for (int x = 0; x < len; x++) {
169 Object o = Array.get(objArray, x);
170 OutMarshallerHelper.writeObject(marshaller, source,
171 new JAXBElement(elName, cls == null ? o.getClass() : cls, o));
172 }
173 } else {
174 OutMarshallerHelper.writeObject(marshaller, source, new JAXBElement(elName,
175 cls, mObj));
176 }
177 } else if (byte[].class == cls && part.getTypeQName() != null && part.getTypeQName().getLocalPart().equals(
178 "hexBinary")) {
179 mObj = new HexBinaryAdapter().marshal((byte[]) mObj);
180 OutMarshallerHelper.writeObject(marshaller, source, new JAXBElement(elName,
181 String.class, mObj));
182 } else if (mObj instanceof JAXBElement) {
183 OutMarshallerHelper.writeObject(marshaller, source, mObj);
184 } else {
185 OutMarshallerHelper.writeObject(marshaller, source, new JAXBElement(elName,
186 cls, mObj));
187 }
188 } else {
189 OutMarshallerHelper.writeObject(marshaller, source, mObj);
190 }
191 } catch (Fault ex) {
192 ex.printStackTrace();
193 throw (Fault) ex.fillInStackTrace();
194 } catch (Exception ex) {
195 ex.printStackTrace();
196 if (ex instanceof javax.xml.bind.MarshalException) {
197 javax.xml.bind.MarshalException marshalEx = (javax.xml.bind.MarshalException) ex;
198 org.apache.cxf.common.i18n.Message faultMessage = new org.apache.cxf.common.i18n.Message("MARSHAL_ERROR", CXFLOG, marshalEx.getLinkedException().getMessage());
199 throw new Fault(faultMessage, ex);
200 } else {
201 throw new Fault(new org.apache.cxf.common.i18n.Message("MARSHAL_ERROR", CXFLOG, ex.getMessage()), ex);
202 }
203 }
204 }
205 }