I’m trying to consume a web service created in .NET which needs SOAP authentication. The part that you would be most interested is this:
<s:element name="SoapAuthenticationHeader" type="tns:SoapAuthenticationHeader" />
<s:complexType name="SoapAuthenticationHeader">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
</s:sequence>
<s:anyAttribute />
</s:complexType>
I’m able to successfully consume the web service in Netbeans. But I cannot use it because in the auto generated stub/methods, I cannot put the username and password for authentication.
Netbeans auto generated stub includes the following for:
JAX-WS
try { // Call Web Service Operation
org.tempuri.TeleCast service = new org.tempuri.TeleCast();
org.tempuri.TeleCastSoap port = service.getTeleCastSoap();
// TODO initialize WS operation arguments here
int campaignid = 0;
java.lang.String to = "";
java.lang.String from = "";
java.lang.String subject = "";
java.lang.String body = "";
java.lang.String uniqueid = "";
// TODO process result here
boolean result = port.queueRealTimeEmail(campaignid, to, from, subject, body, uniqueid);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
JAX-RPC
try { // This code block invokes the TeleCastSoap:queueRealTimeEmail operation on web service
telecastclient.TeleCast teleCast = new telecastclient.TeleCast_Impl();
telecastclient.TeleCastSoap teleCastSoap = teleCast.getTeleCastSoap();
teleCastSoap.queueRealTimeEmail(/* TODO enter operation arguments*/);
} catch(javax.xml.rpc.ServiceException ex) {
java.util.logging.Logger.getLogger(telecastclient.TeleCast.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch(java.rmi.RemoteException ex) {
java.util.logging.Logger.getLogger(telecastclient.TeleCast.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch(Exception ex) {
java.util.logging.Logger.getLogger(telecastclient.TeleCast.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
At the least when I use JAX-WS method, I get a class called SoapAuthenticationHeader in which I can set the username and password. But I don’t know how to pass that object of the SoapAuthenticationHeader before or while making the web service call to perform various operations.
org.tempuri.SoapAuthenticationHeader auth = new SoapAuthenticationHeader();
auth.setUsername("username");
auth.setPassword("password");
I do not have this option in JAX-RPC method.
Any inputs on this would be greatly appreciated. Thanks in advance for your time.
Looks like there is no elegant way to do this from within the default Netbeans IDE.
I was trying the same with Eclipse and in all my searches, I was lead to this Apache Axis2 – Code Generator Wizard Guide for Eclipse Plug-in for Eclipse (I still can’t post links as I’m a newbie).
On using the above plugin (some sort of eclipse add-on of WSDL2Java), it generated the stubs along with proper SoapAuthenticationHeader objects which I can pass along for validation.
So my code now is:
It works just fine.
Now this leads me to a couple of more questions:
the axis2 plugin, my code snippet,
is it the right way to do it?
Sometimes I get this feeling that I
just put a hack together to get the
job done and I’m always interested
in finding the right way to do
things so I can do it better next
time.
generation wizard with eclipse, I
noticed it had an option for Axis
runtime and not Axis2. So now my
question is; With older versions of
Axis or with pre-built tools of the
IDE, consuming web services with
special authentication headers just
can’t be done? Is it more of an architectural limitation?
And thanks again for your time.
Great board this one, SO! Has saved my hide numerous times. So it is my turn to give back to the community now.