signature.setAttachmentCallbackHandler(new CallbackHandler() {
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
if (callbacks[0] instanceof AttachmentRequestCallback) {
AttachmentRequestCallback attachmentRequestCallback = (AttachmentRequestCallback) callbacks[0];
List<Attachment> attachments = new ArrayList<Attachment>();
attachments.add(attachment[0]);
attachmentRequestCallback.setAttachments(attachments);
} else {
AttachmentResultCallback attachmentResultCallback = (AttachmentResultCallback) callbacks[0];
attachment[0] = attachmentResultCallback.getAttachment();
}
}
});
doc = signature.build(doc, crypto, secHeader);
WSSecEncrypt encrypt = new WSSecEncrypt();
encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
encrypt.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
encrypt.setParts(parts);
encrypt.setAttachmentCallbackHandler(new CallbackHandler() {
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
if (callbacks[0] instanceof AttachmentRequestCallback) {
AttachmentRequestCallback attachmentRequestCallback = (AttachmentRequestCallback) callbacks[0];
List<Attachment> attachments = new ArrayList<Attachment>();
attachments.add(attachment[0]);
attachmentRequestCallback.setAttachments(attachments);
} else {
AttachmentResultCallback attachmentResultCallback = (AttachmentResultCallback) callbacks[0];
attachment[0] = attachmentResultCallback.getAttachment();
}
}
});
Document encryptedDoc = encrypt.build(doc, crypto, secHeader);
if (LOG.isDebugEnabled()) {
String outputString = XMLUtils.PrettyDocumentToString(encryptedDoc);
LOG.debug(outputString);
}
try {
verify(encryptedDoc, new CallbackHandler() {
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
if (callbacks[0] instanceof AttachmentRequestCallback) {
AttachmentRequestCallback attachmentRequestCallback = (AttachmentRequestCallback) callbacks[0];
if (!attachment[0].getId().equals(attachmentRequestCallback.getAttachmentId())) {
throw new RuntimeException("wrong attachment requested");
}
List<Attachment> attachments = new ArrayList<Attachment>();
attachments.add(attachment[0]);
if (attachment[0].getHeaders().size() == 6) {
//signature callback
attachment[0].addHeader(AttachmentUtils.MIME_HEADER_CONTENT_DESCRIPTION, "Kaputt");
}
attachmentRequestCallback.setAttachments(attachments);
} else {
AttachmentResultCallback attachmentResultCallback = (AttachmentResultCallback) callbacks[0];
attachment[0] = attachmentResultCallback.getAttachment();
}
}