parts.add(new WSEncryptionPart("Body", "http://schemas.xmlsoap.org/soap/envelope/", "Content"));
parts.add(new WSEncryptionPart("cid:Attachments", "Content"));
builder.setParts(parts);
final String attachmentId = UUID.randomUUID().toString();
final Attachment attachment = new Attachment();
attachment.setMimeType("text/xml");
attachment.addHeaders(getHeaders(attachmentId));
attachment.setId(attachmentId);
attachment.setSourceStream(new ByteArrayInputStream(SOAPUtil.SAMPLE_SOAP_MSG.getBytes("UTF-8")));
AttachmentCallbackHandler attachmentCallbackHandler =
new AttachmentCallbackHandler(Collections.singletonList(attachment));
builder.setAttachmentCallbackHandler(attachmentCallbackHandler);
LOG.info("Before Signing....");
Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader();
secHeader.insertSecurityHeader(doc);
Document signedDoc = builder.build(doc, crypto, secHeader);
if (LOG.isDebugEnabled()) {
LOG.debug("After Signing....");
String outputString = XMLUtils.PrettyDocumentToString(signedDoc);
LOG.debug(outputString);
}
NodeList sigReferences = signedDoc.getElementsByTagNameNS(WSConstants.SIG_NS, "Reference");
Assert.assertEquals(2, sigReferences.getLength());
attachmentCallbackHandler =
new AttachmentCallbackHandler(Collections.singletonList(attachment));
verify(signedDoc, attachmentCallbackHandler);
Assert.assertFalse(attachmentCallbackHandler.getResponseAttachments().isEmpty());
Attachment responseAttachment = attachmentCallbackHandler.getResponseAttachments().get(0);
byte[] attachmentBytes = readInputStream(responseAttachment.getSourceStream());
Assert.assertTrue(Arrays.equals(attachmentBytes, SOAPUtil.SAMPLE_SOAP_MSG.getBytes("UTF-8")));
Assert.assertEquals("text/xml", responseAttachment.getMimeType());
}