Example #1
0
  /**
   * Converts a given {@link Document} to an AXIOM {@link org.apache.axiom.soap.SOAPEnvelope}.
   *
   * @param document the document to be converted
   * @return the converted envelope
   * @throws IllegalArgumentException in case of errors
   * @see org.apache.rampart.util.Axis2Util.getSOAPEnvelopeFromDOMDocument(Document, boolean)
   */
  public static SOAPEnvelope toEnvelope(Document document) {
    try {
      DOMImplementation implementation = document.getImplementation();
      Assert.isInstanceOf(DOMImplementationLS.class, implementation);

      DOMImplementationLS loadSaveImplementation = (DOMImplementationLS) implementation;
      LSOutput output = loadSaveImplementation.createLSOutput();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      output.setByteStream(bos);

      LSSerializer serializer = loadSaveImplementation.createLSSerializer();
      serializer.write(document, output);

      ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

      XMLInputFactory inputFactory = StAXUtils.getXMLInputFactory();

      StAXSOAPModelBuilder stAXSOAPModelBuilder =
          new StAXSOAPModelBuilder(inputFactory.createXMLStreamReader(bis), null);
      SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();

      // Necessary to build a correct Axiom tree, see SWS-483
      envelope.serialize(new NullOutputStream());

      return envelope;
    } catch (Exception ex) {
      IllegalArgumentException iaex =
          new IllegalArgumentException("Error in converting Document to SOAP Envelope");
      iaex.initCause(ex);
      throw iaex;
    }
  }