* Test encrypting a custom SOAP header and the SOAP body
*/
@org.junit.Test
public void testSOAPHeaderAndBody() throws Exception {
Document doc = SOAPUtil.toSOAPPart(SOAPMSG);
SOAPConstants soapConstants =
WSSecurityUtil.getSOAPConstants(doc.getDocumentElement());
WSSecEncrypt encrypt = new WSSecEncrypt();
encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
encrypt.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
WSSecHeader secHeader = new WSSecHeader();
secHeader.insertSecurityHeader(doc);
List<WSEncryptionPart> parts = new ArrayList<WSEncryptionPart>();
WSEncryptionPart encP =
new WSEncryptionPart(
soapConstants.getBodyQName().getLocalPart(), // define the body
soapConstants.getEnvelopeURI(),
"");
parts.add(encP);
WSEncryptionPart encP2 =
new WSEncryptionPart(
"foobar",
"urn:foo.bar",
"");
parts.add(encP2);
encrypt.setParts(parts);
Document encryptedDoc = encrypt.build(doc, crypto, secHeader);
if (LOG.isDebugEnabled()) {
String outputString =
XMLUtils.PrettyDocumentToString(encryptedDoc);
LOG.debug(outputString);
}
List<WSSecurityEngineResult> results = verify(encryptedDoc);
QName fooName = new QName("urn:foo.bar", "foobar");
QName bodyName = new QName(soapConstants.getEnvelopeURI(), "Body");
WSSecurityUtil.checkAllElementsProtected(results, WSConstants.ENCR, new QName[]{fooName});
WSSecurityUtil.checkAllElementsProtected(results, WSConstants.ENCR, new QName[]{bodyName});
WSSecurityUtil.checkAllElementsProtected(
results,
WSConstants.ENCR,
new QName[]{bodyName, fooName}
);
WSSecurityUtil.checkAllElementsProtected(
results,
WSConstants.ENCR,
new QName[]{fooName, bodyName}
);
try {
WSSecurityUtil.checkAllElementsProtected(
results,
WSConstants.SIGN,
new QName[]{fooName, bodyName}
);
fail("Failure expected on a wrong action");
} catch (WSSecurityException ex) {
// expected
}
try {
QName headerName = new QName(soapConstants.getEnvelopeURI(), "Header");
WSSecurityUtil.checkAllElementsProtected(
results,
WSConstants.ENCR,
new QName[]{fooName, bodyName, headerName}
);