MessageParser.java
11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package th.co.ais.ssbsrfc.message;
import java.io.ByteArrayInputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import ec02.utils.AppLog;
import th.co.ais.ssbsrfc.instance.AdjustmentResponseIns;
import th.co.ais.ssbsrfc.instance.SOAPNamespaceMapper;
public class MessageParser
{
public static String toXml(Object obj) {
String strXml = "";
StringWriter sw = new StringWriter();
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.marshal(obj, sw);
strXml = sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return strXml;
}
public static String toXmlWithoutRoot(Object obj) {
String strXml = "";
StringWriter sw = new StringWriter();
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.marshal(obj, sw);
strXml = sw.toString().replace("<root>", "").replace("</root>", "");
} catch (Exception e) {
e.printStackTrace();
}
return strXml;
}
public static String toXml(Object obj, String rootElement) {
String strXml = "";
StringWriter sw = new StringWriter();
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.marshal(obj, sw);
strXml = sw.toString().replace("root", rootElement);
} catch (Exception e) {
e.printStackTrace();
}
return strXml;
}
@SuppressWarnings("rawtypes")
public static String toXmlWithNamespace(Object obj, Class objClass, Object namespaceMapper) {
String strXml = "";
StringWriter sw = new StringWriter();
try {
JAXBContext jc = JAXBContext.newInstance(objClass);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", namespaceMapper);
marshaller.marshal(obj, sw);
strXml = sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return strXml;
}
@SuppressWarnings("rawtypes")
public static Object fromXml(String strXml, Class objClass) {
Object obj = null;
try {
strXml = removeXmlVersion(strXml);
JAXBContext jc = JAXBContext.newInstance(objClass);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader reader = new StringReader(strXml);
obj = unmarshaller.unmarshal(reader);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
@SuppressWarnings("rawtypes")
public static Object fromXmlWithoutRoot(String strXml, Class objClass) {
Object obj = null;
try {
strXml = "<root>" + removeXmlVersion(strXml) + "</root>";
JAXBContext jc = JAXBContext.newInstance(objClass);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader reader = new StringReader(strXml);
obj = unmarshaller.unmarshal(reader);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static String toSoap(Object obj) {
String message = "";
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage outgoingMessage = messageFactory.createMessage();
SOAPPart soappart = outgoingMessage.getSOAPPart();
SOAPEnvelope envelope = soappart.getEnvelope();
envelope.addNamespaceDeclaration("def", "http://definition.webservices.daa.ema.com/");
envelope.removeNamespaceDeclaration("SOAP-ENV");
envelope.removeNamespaceDeclaration("cbs");
envelope.removeNamespaceDeclaration("arc");
envelope.removeNamespaceDeclaration("ars");
envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.addNamespaceDeclaration("cbs", "http://www.huawei.com/bme/cbsinterface/cbscommon");
envelope.addNamespaceDeclaration("arc", "http://cbs.huawei.com/ar/wsservice/arcommon");
envelope.addNamespaceDeclaration("ars", "http://www.huawei.com/bme/cbsinterface/arservices");
envelope.setPrefix("soapenv");
outgoingMessage.getSOAPHeader().setPrefix("soapenv");
outgoingMessage.getSOAPBody().setPrefix("soapenv");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new SOAPNamespaceMapper());
marshaller.marshal( obj, doc );
SOAPBody body = envelope.getBody();
body.addDocument(doc);
outgoingMessage.saveChanges();
StringWriter sw = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(outgoingMessage.getSOAPPart()), new StreamResult(sw));
message = sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
public static String toSoap(Object obj, String namespacePrefix, String namespaceUrl) {
String message = "";
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage outgoingMessage = messageFactory.createMessage();
SOAPPart soappart = outgoingMessage.getSOAPPart();
SOAPEnvelope envelope = soappart.getEnvelope();
envelope.addNamespaceDeclaration(namespacePrefix, namespaceUrl);
envelope.removeNamespaceDeclaration("SOAP-ENV");
envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix("soapenv");
outgoingMessage.getSOAPHeader().setPrefix("soapenv");
outgoingMessage.getSOAPBody().setPrefix("soapenv");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new SOAPNamespaceMapper() );
marshaller.marshal( obj, doc );
SOAPBody body = envelope.getBody();
body.addDocument(doc);
outgoingMessage.saveChanges();
StringWriter sw = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(outgoingMessage.getSOAPPart()), new StreamResult(sw));
message = sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
@SuppressWarnings("rawtypes")
public static Object fromSoap(String strSoap, Class objClass) {
Object obj = null;
try {
SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(strSoap.getBytes()));
Unmarshaller unmarshaller = JAXBContext.newInstance(objClass).createUnmarshaller();
SOAPBody body = message.getSOAPBody();
if(body != null && !body.getTextContent().trim().equals("")) {
obj = unmarshaller.unmarshal(body.extractContentAsDocument());
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static AdjustmentResponseIns formsoap(String ss) throws XMLStreamException, JAXBException{
Reader reader = new StringReader(ss);
XMLInputFactory factory = XMLInputFactory.newInstance(); // Or newFactory()
XMLStreamReader xsr = factory.createXMLStreamReader(reader);
AppLog.d("IN:"+xsr);
xsr.nextTag();
int i =0;
while(!xsr.getLocalName().equals("ResultHeader")) {
xsr.nextTag();
AppLog.d("I HERE:"+i++);
}
JAXBContext jc = JAXBContext.newInstance(AdjustmentResponseIns.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<AdjustmentResponseIns> jb = unmarshaller.unmarshal(xsr, AdjustmentResponseIns.class);
xsr.close();
AdjustmentResponseIns customer = jb.getValue();
return customer;
}
public static String toJson(Object obj) {
String strJson = "";
try {
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
strJson = gson.toJson(obj, obj.getClass());
} catch (Exception e) {
e.printStackTrace();
}
return strJson;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object fromJson(String strJson, Class objClass) {
Object obj = null;
try {
Gson gson = new Gson();
obj = gson.fromJson(strJson, objClass);
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static JsonObject toJsonObject(Object obj) {
JsonObject jsonObj = null;
try {
Gson gson = new Gson();
JsonElement element = gson.fromJson (MessageParser.toJson(obj), JsonElement.class);
jsonObj = element.getAsJsonObject();
} catch (Exception e) {
e.printStackTrace();
}
return jsonObj;
}
private static String removeXmlVersion(String value)
{
return value.replaceAll("\\<\\?xml(.+?)\\?\\>", "");
}
}