[ACCEPTED]-How do I add a SOAP Header using Java JAX-WS-jax-ws

Accepted answer
Score: 19

Thanks Nuno,

Just as soon as I work out how 4 to log in properly to stackoverflow.com 3 I'll do the right thing with your reply.

In 2 the mean time here's the code I ended up 1 with:

FooService service = new FooService();
service.setHandlerResolver(new HandlerResolver() {
    public List<Handler> getHandlerChain(PortInfo portInfo) {
        List<Handler> handlerList = new ArrayList<Handler>();
        handlerList.add(new RGBSOAPHandler());
        return handlerList;
FooPort port = service.getFooPort();
FooPayload payload = new FooPayload();
payload.setAlias("The Hat");


class RGBSOAPHandler implements SOAPHandler<SOAPMessageContext> {

    public Set<QName> getHeaders() {
        return new TreeSet();

    public boolean handleMessage(SOAPMessageContext context) {
        Boolean outboundProperty = 
            (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outboundProperty.booleanValue()) {
            SOAPMessage message = context.getMessage();
            try {
                SOAPEnvelope envelope = context.getMessage()
                SOAPFactory factory = SOAPFactory.newInstance();
                String prefix = "X";
                String uri = "http://...wsssecurity...";
                SOAPElement securityElem = 
                SOAPElement tokenElem = 
                SOAPHeader header = envelope.addHeader();

            } catch (Exception e) {
                System.out.println("Exception in handler: " + e);
        } else {
            // inbound
        return true;

    public boolean handleFault(SOAPMessageContext context) {
        throw new UnsupportedOperationException("Not supported yet.");

    public void close(MessageContext context) {
Score: 1

you might want to look at handlers and handler 6 chains.- I recently had to add a cookie 5 to a given Webservice call and that was 4 how i did it, just created a handler that 3 intercepted the initial call and injected 2 the cookie, you can also manipulate the 1 call headers with a Pivot Handler

Score: 0

for add Soap header, if you implement the 7 WS on the web application server, the Was 6 will add security part at header , after 5 you have configure as per WS-SECURITY standard 4 , such as web-policy etc. I don't understand 3 why need add yourself except the encrypted 2 content part , such as encrypted password 1 etc

More Related questions