1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.common.util;
21
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import jakarta.xml.soap.MessageFactory;
25 import java.io.ByteArrayInputStream;
26 import java.io.InputStream;
27
28 public class SOAPUtil {
29 public static final String SAMPLE_SOAP_MSG =
30 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
31 + "<SOAP-ENV:Envelope "
32 + "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
33 + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
34 + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
35 + "<SOAP-ENV:Body>"
36 + "<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">"
37 + "<value xmlns=\"\">15</value>"
38 + "</add>"
39 + "</SOAP-ENV:Body>"
40 + "</SOAP-ENV:Envelope>";
41
42 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(SOAPUtil.class);
43
44 private static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
45 private static MessageFactory saajFactory = null;
46
47 static {
48 factory.setNamespaceAware(true);
49 try {
50 saajFactory = MessageFactory.newInstance();
51 } catch (Exception e) {
52 LOG.debug("can't create SAAJ MessageFactory", e);
53 }
54 }
55
56
57
58
59 public static org.w3c.dom.Document toSOAPPart(String xml) throws Exception {
60 try (InputStream in = new ByteArrayInputStream(xml.getBytes())) {
61 DocumentBuilder builder = factory.newDocumentBuilder();
62 return builder.parse(in);
63 }
64 }
65
66
67
68
69 public static org.w3c.dom.Document toSOAPPart(InputStream in) throws Exception {
70 DocumentBuilder builder = factory.newDocumentBuilder();
71 return builder.parse(in);
72 }
73
74
75
76
77
78 public static jakarta.xml.soap.SOAPPart toSAAJSOAPPart(String xml) throws Exception {
79 try (InputStream in = new ByteArrayInputStream(xml.getBytes())) {
80 return saajFactory.createMessage(null, in).getSOAPPart();
81 }
82 }
83
84 }