**
* Sign a soap message.
* We wish to sign both the body and the attachment
*
*
* @param msg - SOAPMessage object to sign - there is one attachment
present
* @param msgStream - the SOAPMessage represented as an input stream
* @return new Signed SOAPMessage
* @throws Exception
*/
private SOAPMessage signSOAPMessage(SOAPMessage msg, InputStream msgStream) {
Message signedSOAPMsg=null;
Iterator attachmentsIter=null;
FileInputStream attachmentStream = null;
try {
// create an AxisMessage from the SOAPMessage InputStream
// pass false for arg2 as the msgStream contains the ENTIRE message.
Message axisMessage = new Message(msgStream, false,msg.getMimeHeaders());
SOAPEnvelope unsignedEnvelope = axisMessage.getSOAPEnvelope();
Document doc = unsignedEnvelope.getAsDocument();
// WSSignEnvelope signs a SOAP envelope according to the
// WS Specification (X509 profile) and adds the signature data
// to the envelope.
WSSecSignature signer = new WSSecSignature();
String alias = "16c73ab6-b892-458f-abf5-2f875f74882e";
String password = "security";
signer.setUserInfo(alias, password);
// create a vector of WSEncryptPart parts to sign, both the soap body
//and the attachments
SOAPConstants soapConstants =WSSecurityUtil.getSOAPConstants(unsignedEnvelope) ;
Vector parts = new Vector();
// add the body part
String localPart = soapConstants.getBodyQName().getLocalPart();
String envelopeURI = soapConstants.getEnvelopeURI();
WSEncryptionPart body = new WSEncryptionPart(localPart, envelopeURI,
"Content");
parts.add(body);
// how to add the attachment part?????
signer.setParts(parts);
// The "build" method, creates the signed SOAP envelope.
// It takes a SOAP Envelope as a W3C Document and adds
// a WSS Signature header to it. The signed elements
// depend on the signature parts that are specified by
// the WSBaseMessage.setParts(java.util.Vector parts)
// method. By default, SOAP Body is signed.
// The "crypto" parameter is the object that implements
// access to the keystore and handling of certificates.
// A default implementation is included:
// org.apache.ws.security.components.crypto.Merlin
Document signedDoc = signer.build(doc, CryptoFactory.getInstance(),null);
// Convert the signed document into a SOAP message.
signedSOAPMsg = (Message) toSOAPMessage(signedDoc);
} catch (Exception e) {
e.printStackTrace();
}
return signedSOAPMsg;
}