FileDocCategorySizeDatePackage
SignedSOAPEnvelope.javaAPI DocApache Axis 1.47921Sat Apr 22 18:56:52 BST 2006samples.security

SignedSOAPEnvelope

public class SignedSOAPEnvelope extends org.apache.axis.message.SOAPEnvelope

Fields Summary
static String
SOAPSECNS
static String
SOAPSECprefix
static String
keystoreType
static String
keystoreFile
static String
keystorePass
static String
privateKeyAlias
static String
privateKeyPass
static String
certificateAlias
private org.apache.axis.MessageContext
msgContext
Constructors Summary
public SignedSOAPEnvelope(org.apache.axis.MessageContext msgContext, org.apache.axis.message.SOAPEnvelope env, String baseURI, String keystoreFile)


     
        org.apache.xml.security.Init.init();
    
        this.msgContext = msgContext;
        init(env, baseURI, keystoreFile);
    
public SignedSOAPEnvelope(org.apache.axis.message.SOAPEnvelope env, String baseURI)

        init(env, baseURI, keystoreFile);
    
Methods Summary
private org.w3c.dom.DocumentgetSOAPEnvelopeAsDocument(org.apache.axis.message.SOAPEnvelope env, org.apache.axis.MessageContext msgContext)

        StringWriter writer = new StringWriter();
        SerializationContext serializeContext = new SerializationContext(writer, msgContext);
        env.output(serializeContext);
        writer.close();

        Reader reader = new StringReader(writer.getBuffer().toString());
        Document doc = XMLUtils.newDocument(new InputSource(reader));
        if (doc == null)
            throw new Exception(
                    Messages.getMessage("noDoc00", writer.getBuffer().toString()));
        return doc;
    
private voidinit(org.apache.axis.message.SOAPEnvelope env, java.lang.String baseURI, java.lang.String keystoreFile)

        try {
            System.out.println("Beginning Client signing...");
            env.addMapping(new Mapping(SOAPSECNS, SOAPSECprefix));
            env.addAttribute(Constants.URI_SOAP11_ENV, "actor", "some-uri");
            env.addAttribute(Constants.URI_SOAP11_ENV, "mustUnderstand", "1");

            SOAPHeaderElement header = 
                new SOAPHeaderElement(XMLUtils.StringToElement(SOAPSECNS,
                                                               "Signature",
                                                               ""));
            env.addHeader(header);

            Document doc = getSOAPEnvelopeAsDocument(env, msgContext);

            KeyStore ks = KeyStore.getInstance(keystoreType);
            FileInputStream fis = new FileInputStream(keystoreFile);

            ks.load(fis, keystorePass.toCharArray());

            PrivateKey privateKey = (PrivateKey) ks.getKey(privateKeyAlias,
                    privateKeyPass.toCharArray());

            Element soapHeaderElement = (Element) ((Element) doc.getFirstChild()).getElementsByTagNameNS("*", "Header").item(0);
            Element soapSignatureElement = (Element) soapHeaderElement.getElementsByTagNameNS("*", "Signature").item(0);

            //Id attribute creation
            Element body = (Element)doc.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Body").item(0);
            body.setAttribute("Id", "Body");

            XMLSignature sig = new XMLSignature(doc, baseURI,
                    XMLSignature.ALGO_ID_SIGNATURE_DSA);

            soapSignatureElement.appendChild(sig.getElement());
            sig.addDocument("#Body");


            X509Certificate cert =
                    (X509Certificate) ks.getCertificate(certificateAlias);


            sig.addKeyInfo(cert);
            sig.addKeyInfo(cert.getPublicKey());
            sig.sign(privateKey);

            Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
            byte[] canonicalMessage = c14n.canonicalizeSubtree(doc);

            InputSource is = new InputSource(new java.io.ByteArrayInputStream(canonicalMessage));
            DeserializationContext dser = null;
            if (msgContext == null) {
                AxisClient tmpEngine = new AxisClient(new NullProvider());
                msgContext = new MessageContext(tmpEngine);
            }
            dser = new DeserializationContext(is, msgContext,
                    Message.REQUEST, this);

            dser.parse();
            System.out.println("Client signing complete.");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }