View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * Convert an SOAP Envelope as a String to a org.w3c.dom.Document.
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       * Convert an SOAP Envelope as an InputStream to a org.w3c.dom.Document.
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       * Convert an SOAP Envelope as a String to a jakarta.xml.soap.SOAPPart.
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  }