I found a walk-around. envelope.getBody().insertSiblingBefore(headerElm);
This will add the header OMElement into the envelope. However, the Header added is only considered as a child element of envelope, not a SOAPHeader. Thus if I call envelope.getHeader(), I will get exception. Regards, Xinjun On 4/5/06, Xinjun Chen <[EMAIL PROTECTED]> wrote: > I am using the following function to add UsernameToken to my security > header. But the processed envelope get some problems. > 1. The Header is placed after the body. How can I insert SOAPHeader > before SOAPBody? > 2. The security header defines the xmlns:xsd and xmlns:xsi again, > which have already been defined in <SOAP-ENV:Envelope>. > > Could you give me some advice on how to solve the above two problems? > > Attached: > > 1) The function used for adding username token to the envelope: > > public void addUsernameTokens(UsernameToken token, final SOAPEnvelope > envelope) throws Exception { > Document domDoc = Axis2Util.getDocumentFromSOAPEnvelope(envelope); > WSSAddUsernameToken builder = new WSSAddUsernameToken("", false); > if (token.isDigested()) { > builder.setPasswordType(WSConstants.PASSWORD_DIGEST); > } else { > builder.setPasswordTyp(WSConstants.PASSWORD_TEXT); > } > > builder.build(domDoc, token.getUsername(), token.getPassword()); > Element header = (Element) > (domDoc.getElementsByTagName("wsse:Security").item(0)); > OMElement headerElm = (OMElement) (Axis2Util.toOM(header)); > SOAPFactory factory = Axis2Util.getSOAPFactory(envelope); > factory.createSOAPHeader(envelope); > envelope.getHeader().addChild(headerElm); > } > > 2): The original soap envelope: > > <?xml version='1.0' encoding='utf-8'?><SOAP-ENV:Envelope > xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xmlns:xsd="http://www.w3.org/2001/XMLSchema"> > <SOAP-ENV:Body> > <sayHello xmlns="http://services/helloworld"> > <value>Hello world!</value> > </sayHello> > </SOAP-ENV:Body> > </SOAP-ENV:Envelope> > > 3): The processed soap envelope: > > <?xml version='1.0' encoding='utf-8'?><SOAP-ENV:Envelope > xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xmlns:xsd="http://www.w3.org/2001/XMLSchema"> > <SOAP-ENV:Body> > <sayHello xmlns="http://services/helloworld"> > <value>Hello world!</value> > </sayHello> > </SOAP-ENV:Body> > <SOAP-ENV:Header><wsse:Security > xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xmlns:xsd="http://www.w3.org/2001/XMLSchema"> > <wsse:UsernameToken><wsse:Username>xinjun</wsse:Username><wsse:Password > Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password></wsse:UsernameToken></wsse:Security></SOAP-ENV:Header></SOAP-ENV:Envelope> >