Document doc = readDocument("wsse-request-clean.xml");
WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();
SoapMessage msg = new SoapMessage(new MessageImpl());
Exchange ex = new ExchangeImpl();
ex.setInMessage(msg);
SOAPMessage saajMsg = MessageFactory.newInstance().createMessage();
SOAPPart part = saajMsg.getSOAPPart();
part.setContent(new DOMSource(doc));
saajMsg.saveChanges();
msg.setContent(SOAPMessage.class, saajMsg);
msg.put(
WSHandlerConstants.ACTION,
WSHandlerConstants.USERNAME_TOKEN + " " + WSHandlerConstants.ENCRYPT
);
msg.put(WSHandlerConstants.ENC_PROP_FILE, "META-INF/cxf/outsecurity.properties");
msg.put(WSHandlerConstants.USER, "alice");
msg.put("password", "alicePassword");
msg.put(WSHandlerConstants.ENCRYPTION_USER, "myalias");
msg.put(
WSHandlerConstants.ENCRYPTION_PARTS,
"{Content}{" + WSConstants.WSSE_NS + "}UsernameToken"
);
handler.handleMessage(msg);
doc = part;
assertValid("//wsse:Security", doc);
byte[] docbytes = getMessageBytes(doc);
XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setIgnoringComments(false);
dbf.setIgnoringElementContentWhitespace(true);
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setEntityResolver(new NullResolver());
doc = StaxUtils.read(db, reader, false);
WSS4JInInterceptor inHandler = new WSS4JInInterceptor();
SoapMessage inmsg = new SoapMessage(new MessageImpl());
ex.setInMessage(inmsg);
inmsg.setContent(SOAPMessage.class, saajMsg);
inHandler.setProperty(
WSHandlerConstants.ACTION,
WSHandlerConstants.USERNAME_TOKEN + " " + WSHandlerConstants.ENCRYPT
);
inHandler.setProperty(WSHandlerConstants.DEC_PROP_FILE, "META-INF/cxf/insecurity.properties");
inHandler.setProperty(
WSHandlerConstants.PW_CALLBACK_CLASS,
"org.apache.cxf.ws.security.wss4j.TestPwdCallback"
);
inHandler.handleMessage(inmsg);
//
// Check that the EncryptedData is no longer there
//
assertInvalid("//s:Body/xenc:EncryptedData", saajMsg.getSOAPPart());
//
// There should be exactly 1 (WSS4J) HandlerResult
//
final java.util.List<WSHandlerResult> handlerResults =
(java.util.List<WSHandlerResult>) inmsg.get(WSHandlerConstants.RECV_RESULTS);
assertNotNull(handlerResults);
assertSame(handlerResults.size(), 1);
//
// This should contain exactly 2 protection results
//
final java.util.List<Object> protectionResults =
(java.util.List<Object>) handlerResults.get(0).getResults();
assertNotNull(protectionResults);
assertSame(protectionResults.size(), 2);
WSSecurityEngineResult wsResult1 = (WSSecurityEngineResult)protectionResults.get(0);
WSSecurityEngineResult wsResult2 = (WSSecurityEngineResult)protectionResults.get(1);
final Principal p1 = (Principal)wsResult1.get(WSSecurityEngineResult.TAG_PRINCIPAL);
final Principal p2 = (Principal)wsResult2.get(WSSecurityEngineResult.TAG_PRINCIPAL);
assertTrue(p1 instanceof WSUsernameTokenPrincipal || p2 instanceof WSUsernameTokenPrincipal);
Principal utPrincipal = p1 instanceof WSUsernameTokenPrincipal ? p1 : p2;
Principal secContextPrincipal = (Principal)inmsg.get(WSS4JInInterceptor.PRINCIPAL_RESULT);
assertSame(secContextPrincipal, utPrincipal);
}