@Test
public void testMultipleAttachmentCompleteEncryption() throws Exception {
final String attachment1Id = UUID.randomUUID().toString();
final Attachment[] attachment = new Attachment[2];
attachment[0] = new Attachment();
attachment[0].setMimeType("text/xml");
attachment[0].addHeaders(getHeaders(attachment1Id));
attachment[0].setId(attachment1Id);
attachment[0].setSourceStream(new ByteArrayInputStream(SOAPUtil.SAMPLE_SOAP_MSG.getBytes("UTF-8")));
final String attachment2Id = UUID.randomUUID().toString();
attachment[1] = new Attachment();
attachment[1].setMimeType("text/plain");
attachment[1].addHeaders(getHeaders(attachment2Id));
attachment[1].setId(attachment2Id);
attachment[1].setSourceStream(new ByteArrayInputStream(SOAPUtil.SAMPLE_SOAP_MSG.getBytes("UTF-8")));
AttachmentCallbackHandler attachmentCallbackHandler =
new AttachmentCallbackHandler(Arrays.asList(attachment));
List<Attachment> encryptedAttachments = attachmentCallbackHandler.getResponseAttachments();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
{
WSSSecurityProperties securityProperties = new WSSSecurityProperties();
List<WSSConstants.Action> actions = new ArrayList<WSSConstants.Action>();
actions.add(WSSConstants.ENCRYPT);
securityProperties.setActions(actions);
securityProperties.loadEncryptionKeystore(this.getClass().getClassLoader().getResource("transmitter.jks"), "default".toCharArray());
securityProperties.setEncryptionUser("receiver");
securityProperties.addEncryptionPart(new SecurePart(new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"), SecurePart.Modifier.Content));
securityProperties.addEncryptionPart(new SecurePart("cid:Attachments", SecurePart.Modifier.Element));
securityProperties.setAttachmentCallbackHandler(attachmentCallbackHandler);
OutboundWSSec wsSecOut = WSSec.getOutboundWSSec(securityProperties);
XMLStreamWriter xmlStreamWriter = wsSecOut.processOutMessage(baos, "UTF-8", new ArrayList<SecurityEvent>());
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml"));
XmlReaderToWriter.writeAll(xmlStreamReader, xmlStreamWriter);
xmlStreamWriter.close();
}
attachmentCallbackHandler = new AttachmentCallbackHandler(encryptedAttachments);
{
WSSSecurityProperties securityProperties = new WSSSecurityProperties();
securityProperties.loadDecryptionKeystore(this.getClass().getClassLoader().getResource("receiver.jks"), "default".toCharArray());
securityProperties.setCallbackHandler(new CallbackHandlerImpl());
securityProperties.setAttachmentCallbackHandler(attachmentCallbackHandler);
InboundWSSec wsSecIn = WSSec.getInboundWSSec(securityProperties);
XMLStreamReader xmlStreamReader = wsSecIn.processInMessage(xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray())));
StAX2DOM.readDoc(documentBuilderFactory.newDocumentBuilder(), xmlStreamReader);
}
Assert.assertFalse(attachmentCallbackHandler.getResponseAttachments().isEmpty());
Attachment responseAttachment = attachmentCallbackHandler.getResponseAttachments().get(0);
byte[] attachment1Bytes = readInputStream(responseAttachment.getSourceStream());
Assert.assertTrue(Arrays.equals(attachment1Bytes, SOAPUtil.SAMPLE_SOAP_MSG.getBytes("UTF-8")));
Assert.assertEquals("text/xml", responseAttachment.getMimeType());
Map<String, String> att1Headers = responseAttachment.getHeaders();
Assert.assertEquals(6, att1Headers.size());
responseAttachment = attachmentCallbackHandler.getResponseAttachments().get(1);
byte[] attachment2Bytes = readInputStream(responseAttachment.getSourceStream());
Assert.assertTrue(Arrays.equals(attachment2Bytes, SOAPUtil.SAMPLE_SOAP_MSG.getBytes("UTF-8")));
Assert.assertEquals("text/plain", responseAttachment.getMimeType());
Map<String, String> att2Headers = responseAttachment.getHeaders();
Assert.assertEquals(6, att2Headers.size());
}