public void writePayloadTo(XMLStreamWriter writer) throws XMLStreamException {
    if (payloadLocalName == null) {
      return;
    } // no body
    assert unconsumed();
    XMLStreamReaderToXMLStreamWriter conv = new XMLStreamReaderToXMLStreamWriter();
    while (reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
      String name = reader.getLocalName();
      String nsUri = reader.getNamespaceURI();

      // after previous conv.bridge() call the cursor will be at
      // END_ELEMENT. Check if its not soapenv:Body then move to next
      // ELEMENT
      if (reader.getEventType() == XMLStreamConstants.END_ELEMENT) {
        if (!name.equals("Body") || !nsUri.equals(soapVersion.nsUri)) {
          XMLStreamReaderUtil.nextElementContent(reader);
          if (reader.getEventType() == XMLStreamConstants.END_DOCUMENT) {
            break;
          }
          name = reader.getLocalName();
          nsUri = reader.getNamespaceURI();
        }
      }
      if (name.equals("Body") && nsUri.equals(soapVersion.nsUri)
          || (reader.getEventType() == XMLStreamConstants.END_DOCUMENT)) {
        break;
      }
      conv.bridge(reader, writer);
    }
    reader.close();
    XMLStreamReaderFactory.recycle(reader);
  }
  public void writePayloadTo(
      ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment)
      throws SAXException {
    assert unconsumed();
    try {
      if (payloadLocalName == null) {
        return;
      } // no body

      XMLStreamReaderToContentHandler conv =
          new XMLStreamReaderToContentHandler(reader, contentHandler, true, fragment);

      while (reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
        String name = reader.getLocalName();
        String nsUri = reader.getNamespaceURI();

        // after previous conv.bridge() call the cursor will be at
        // END_ELEMENT. Check if its not soapenv:Body then move to next
        // ELEMENT
        if (reader.getEventType() == XMLStreamConstants.END_ELEMENT) {
          if (!name.equals("Body") || !nsUri.equals(soapVersion.nsUri)) {
            XMLStreamReaderUtil.nextElementContent(reader);
            if (reader.getEventType() == XMLStreamConstants.END_DOCUMENT) {
              break;
            }
            name = reader.getLocalName();
            nsUri = reader.getNamespaceURI();
          }
        }
        if (name.equals("Body") && nsUri.equals(soapVersion.nsUri)
            || (reader.getEventType() == XMLStreamConstants.END_DOCUMENT)) {
          break;
        }

        conv.bridge();
      }
      reader.close();
      XMLStreamReaderFactory.recycle(reader);
    } catch (XMLStreamException e) {
      Location loc = e.getLocation();
      if (loc == null) {
        loc = DummyLocation.INSTANCE;
      }

      SAXParseException x =
          new SAXParseException(
              e.getMessage(),
              loc.getPublicId(),
              loc.getSystemId(),
              loc.getLineNumber(),
              loc.getColumnNumber(),
              e);
      errorHandler.error(x);
    }
  }
  public Message copy() {
    try {
      // copy the payload
      XMLStreamReader clone;
      XMLStreamReader clonedReader;

      if (hasPayload()) {
        assert unconsumed();
        consumedAt = null; // but we don't want to mark it as consumed
        MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer();

        // the boolean value tells the first body part is written.
        // based on this we do the right thing
        StreamReaderBufferCreator c = new StreamReaderBufferCreator(xsb);
        while (reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
          String name = reader.getLocalName();
          String nsUri = reader.getNamespaceURI();
          if (name.equals("Body") && nsUri.equals(soapVersion.nsUri)
              || (reader.getEventType() == XMLStreamConstants.END_DOCUMENT)) {
            break;
          }
          c.create(reader);
        }
        XMLStreamReaderFactory.recycle(reader);

        reader = xsb.readAsXMLStreamReader();
        reader = new VerifiedMessageXMLStreamReader(reader, bodyEnvNs);
        clone = xsb.readAsXMLStreamReader();
        clonedReader = new VerifiedMessageXMLStreamReader(clone, bodyEnvNs);
        // advance to the start tag of the first element
        proceedToRootElement(reader);
        proceedToRootElement(clonedReader);
      } else {
        // it's tempting to use EmptyMessageImpl, but it doesn't presere the infoset
        // of <envelope>,<header>, and <body>, so we need to stick to StreamMessage.
        clone = reader;
        clonedReader = reader;
      }

      return new VerifiedStreamMessage(
          envelopeTag,
          headerTag,
          attachmentSet,
          HeaderList.copy(headers),
          bodyTag,
          clone,
          soapVersion,
          this.bodyEnvNs);
    } catch (XMLStreamException e) {
      throw new WebServiceException("Failed to copy a message", e);
    }
  }
 public <T> T readPayloadAsJAXB(Bridge<T> bridge) throws JAXBException {
   cacheMessage();
   if (!hasPayload()) {
     return null;
   }
   assert unconsumed();
   T r =
       bridge.unmarshal(
           reader, hasAttachments() ? new AttachmentUnmarshallerImpl(getAttachments()) : null);
   XMLStreamReaderUtil.close(reader);
   XMLStreamReaderFactory.recycle(reader);
   return r;
 }
 @Override
 public void consume() {
   assert unconsumed();
   XMLStreamReaderFactory.recycle(reader);
 }