// Add a custom element of our own creation
tokenRequest.addCustomElementNS("http://testElementNs.testElementNs", "te:TestElement");
// Create a SOAP body and set the XML element of the token request (a <RequestSecurityToken> element)
// as its only child
SOAPBodyElement sbe = new SOAPBodyElement(tokenRequest.getElement());
// Add the body element to the SOAP envelope
env.addBodyElement(sbe);
System.out.println("\n============= Request ==============");
System.out.println(XMLUtils.DocumentToString(env.getAsDocument()));
// This is where we actually invoke the service, sending the request we've constructed
// Assuming we did everything right, it will return to us a SOAP envelope containing the response
SOAPEnvelope response = call.invoke(env);
System.out.println("\n============= Response ==============");
XMLUtils.PrettyElementToStream(response.getAsDOM(), System.out);
// Find the <RequestSecurityTokenResponse> element the SOAP body should contain
SOAPBodyElement responseBody = response.getBodyByName(TrustConstants.WST_NS, TrustConstants.RESPONSE_TAG);
// Construct a Java object from the XML
RequestSecurityTokenResponse tokenResponse = new RequestSecurityTokenResponse(responseBody.getAsDOM());
System.out.println("\n------- RequestSecurityTokenResponse object ------------- \n" + tokenResponse);
}