Add Security Header info to Java Code generated from WSDL

10,047

I found a sample code that add a token string to a soap security header. Here is the header form of the code below :

<TicketHeader>
    <Ticket>OD01096347CCA</Ticket>
</TicketHeader>

The method to add the header to the message :

// Security token
String token;
// MyService and MySoapService are stubs generated from WSDL
MyService service = new MyService();
MyServiceSoap ep = service.getMyServiceSoap();

Binding binding = ((BindingProvider) ep).getBinding();
List handlers = binding.getHandlerChain();
handlers.add(new MySOAPHandler(token));
binding.setHandlerChain(handlers);

code of MySoapHandler :

public class MySOAPHandler implements SOAPHandler {

    private String token;

    public DHSOAPHandler(String token) {
        this.token = token;
    }
    public boolean handleMessage(SOAPMessageContext messageContext) {
        SOAPMessage
        msg = messageContext.getMessage();
        if ((Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)){
            try {
                SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
                SOAPHeader header = envelope.addHeader();
                SOAPElement el = header.addHeaderElement(envelope.createName("TicketHeader",
                                    "", "http://ws.service.com/"));
                el = el.addChildElement(envelope.createName("Ticket", "", "http://ws.service.com/"));
                el.setValue(token);
                msg.saveChanges();
            }
            catch (SOAPException    e) {
                return false;
            }
        }
        return true;
    }

    public boolean handleFault(SOAPMessageContext messageContext) {
        return true;
    } 

    public void close(MessageContext messageContext){
    }
    // I'm not quite sure about what should this function do, but I guess something like this...
    public Set getHeaders(){
        Set headers = new HashSet();
        headers.add(new QName("https://ws.service.com/", "TicketHeader"));
        return headers;
    }
}
Share:
10,047
Shane
Author by

Shane

Updated on June 05, 2022

Comments

  • Shane
    Shane almost 2 years

    I used the Netbeans Web Service wizard to generate Java code given a WSDL. If I drag the web service method into a class, then it creates some Java code to call that web service (ex: SubmitApplication). I can see how to populate objects to send info to that web service, but the service also requires a security header with username/password.

    There is a generated class called SecurityHeader that contains the username/password attributes. I can create this object with a valid username/password, but I cannot see how to pass that object or add it to the SubmitApplication call. How can the SecurityHeader be added to the SubmitApplication call?

    Here is an example of what the SOAP request should look like:

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Header>
            <SecurityHeader xmlns="http://schemas.turss.com/BDS/1.0/">
                <CreateTime>6/8/2012 8:32:59 PM</CreateTime>
                <Owner>Sample_Owner</Owner>
                <HashKey>Sample_Hash_Key</HashKey>
            </SecurityHeader>
        </soap:Header>
        <soap:Body>
            <SubmitApplication xmlns="http://schemas.turss.com/BDS/1.0/">
                <newSearch>
                    <CurrentApplicant xmlns="http://schemas.turss.com/BDS/1.0/proxy">
                        <FirstName>Bob</FirstName>
                        <MiddleName />
                        <LastName>Smith</LastName>
                        <Suffix />
                        <BirthDate>1970-10-20T00:00:00</BirthDate>
                        <SSN />
                        <Address />
                        <City />
                        <State />
                        <PostalCode />
                    </CurrentApplicant>
                    <PermissiblePurpose xmlns="http://schemas.turss.com/BDS/1.0/proxy">TenantScreening</PermissiblePurpose>
                </newSearch>
            </SubmitApplication>
        </soap:Body>
    </soap:Envelope>