*/
public void testEMBED_SECURITY_TOKEN_REF() throws Exception {
SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
SOAPEnvelope envelope = null;
WSEncryptBody wsEncrypt = new WSEncryptBody();
//Get the message as document
log.info("Before Encryption....");
Document doc = unsignedEnvelope.getAsDocument();
/* Step 1 :: Add a UserNameToken.
* Step 2 :: Add an Id to it.
* Step 3 :: Create a Reference to the UserNameToken.
* Step 4 :: Setting necessary parameters in WSEncryptBody.
* Step 5 :: Encrypt using the using the password of UserNameToken.
*/
//Step 1
String username = "Dimthu";
String password = "Sri Lanka Sri Lanka UOM ";
byte[] key = password.getBytes();
WSSAddUsernameToken builder = new WSSAddUsernameToken("", false);
builder.setPasswordType(WSConstants.PASSWORD_TEXT);
builder.build(doc, username, password);
//Step 2
// I should add wsu:Id here but I am not adding it since
Element usrEle =
(Element) (doc
.getElementsByTagNameNS(WSConstants.WSSE_NS, "UsernameToken")
.item(0));
String idValue = "1234";
usrEle.setAttribute("Id", idValue);
//Step 3 ::
Reference ref = new Reference(WSSConfig.getDefaultWSConfig(), doc);
ref.setURI("#" + idValue);
ref.setValueType("UsernameToken");
SecurityTokenReference secRef =
new SecurityTokenReference(WSSConfig.getDefaultWSConfig(), doc);
secRef.setReference(ref);
// adding the namespace
WSSecurityUtil.setNamespace(
secRef.getElement(),
WSConstants.WSSE_NS,
WSConstants.WSSE_PREFIX);
//Step 4 ::
wsEncrypt.setKeyIdentifierType(WSConstants.EMBED_SECURITY_TOKEN_REF);
wsEncrypt.setSecurityTokenReference(secRef);
wsEncrypt.setKey(key);
//Step 4 :: Encrypting using the key.
Document encDoc = wsEncrypt.build(doc, crypto);
/*
* convert the resulting document into a message first. The toSOAPMessage()
* mehtod performs the necessary c14n call to properly set up the signed
* document and convert it into a SOAP message. After that we extract it