@SuppressWarnings("unchecked")
protected Envelope buildSOAPMessage(SAMLMessageContext samlMsgCtx, SAMLObject samlMessage) {
Envelope envelope = null;
if (samlMsgCtx.getOutboundMessage() != null && samlMsgCtx.getOutboundMessage() instanceof Envelope) {
envelope = (Envelope) samlMsgCtx.getOutboundMessage();
Body body = envelope.getBody();
if (body == null) {
XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
SOAPObjectBuilder<Body> bodyBuilder = (SOAPObjectBuilder<Body>) builderFactory
.getBuilder(Body.DEFAULT_ELEMENT_NAME);
body = bodyBuilder.buildObject();
envelope.setBody(body);
} else if (!body.getUnknownXMLObjects().isEmpty()) {
log.warn("Supplied SOAP Envelope Body was not empty. Existing contents will be removed.");
body.getUnknownXMLObjects().clear();
}
body.getUnknownXMLObjects().add(samlMessage);
} else {
envelope = buildSOAPMessage(samlMessage);
samlMsgCtx.setOutboundMessage(envelope);
}
return envelope;