### Question:
SAAJOutInterceptor extends AbstractSoapInterceptor { public void handleMessage(SoapMessage message) throws Fault { SOAPMessage saaj = message.getContent(SOAPMessage.class); try { if (message.hasHeaders() && saaj != null && saaj.getSOAPPart().getEnvelope().getHeader() == null) { saaj.getSOAPPart().getEnvelope().addHeader(); } } catch (SOAPException e) { throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, message.getVersion().getSender()); } if (saaj == null) { SoapVersion version = message.getVersion(); try { MessageFactory factory = getFactory(message); SOAPMessage soapMessage = factory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); XMLStreamWriter origWriter = (XMLStreamWriter)message.get(ORIGINAL_XML_WRITER); if (origWriter == null) { origWriter = message.getContent(XMLStreamWriter.class); } message.put(ORIGINAL_XML_WRITER, origWriter); W3CDOMStreamWriter writer = new SAAJStreamWriter(soapPart); message.setContent(XMLStreamWriter.class, writer); message.setContent(SOAPMessage.class, soapMessage); message.setContent(Node.class, soapMessage.getSOAPPart()); } catch (SOAPException e) { throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, version.getSender()); } } else if (!message.containsKey(ORIGINAL_XML_WRITER)) { XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class); message.put(ORIGINAL_XML_WRITER, origWriter); XMLStreamWriter dummyWriter = StaxUtils.createXMLStreamWriter(new OutputStream() { public void write(int b) throws IOException { } public void write(byte[] b, int off, int len) throws IOException { } }); message.setContent(XMLStreamWriter.class, dummyWriter); } message.getInterceptorChain().add(SAAJOutEndingInterceptor.INSTANCE); } SAAJOutInterceptor(); synchronized MessageFactory getFactory(SoapMessage message); void handleMessage(SoapMessage message); @Override void handleFault(SoapMessage message); static final String ORIGINAL_XML_WRITER; }### Answer:
@Test public void testHandleHeader() throws Exception { soapMessage = TestUtil.createEmptySoapMessage(Soap11.getInstance(), chain); soapMessage.setContent(OutputStream.class, new ByteArrayOutputStream()); SOAPMessage m = SAAJFactoryResolver.createMessageFactory(soapMessage.getVersion()).createMessage(); InputStream ins = getClass().getResourceAsStream("../test-soap-header.xml"); m.getSOAPPart().setContent(new StreamSource(ins)); Element el = DOMUtils.getFirstElement(m.getSOAPPart().getEnvelope().getHeader()); el = (Element)DOMUtils.getDomElement(el); List<Header> h = soapMessage.getHeaders(); while (el != null) { h.add(new SoapHeader(DOMUtils.getElementQName(el), el)); el = DOMUtils.getNextElement(el); } soapMessage.setContent(SOAPMessage.class, m); W3CDOMStreamWriter writer = new SAAJStreamWriter(m.getSOAPPart()); soapMessage.setContent(XMLStreamWriter.class, writer); soi.handleMessage(soapMessage); }