Writing a jboss - webservices client using python suds
I was experimenting with writing a secured web services client in python and I was surprised how easy it was using python suds.
First write your webservice in java:
@Remote
public interface TestService {
public String echo(String s);
public int add(int a, int b);
}
and the implementation:
@WebService
@Stateless
public class TestServiceImpl implements TestService {
public String echo(String s) {
return s;
}
public int add(int a, int b) {
return a+b;
}
}
Expose it using a webapp and configure security using web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>project-name-war</display-name> <description></description> <servlet> <servlet-name>WS</servlet-name> <servlet-class>net.ahlawat.TestServiceImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>WS</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <security-constraint> <web-resource-collection> <web-resource-name>web service</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>user</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> </login-config> <security-role> <description>Known users of the Foobar service</description> <role-name>user</role-name> </security-role> </web-app>
and configure it to a jboss login module using jboss-web.xml:
<?xml version="1.0" encoding="UTF-8"?> <jboss-web> <security-domain>java:/jaas/tws</security-domain> </jboss-web>
Basically that is it - you can then code up your python clinet like this:
from suds.client import Client
url = "https://127.0.0.1:8080/webServiceTest-war?wsdl"
client = Client(url, username="user", password="userPassword2010!")
#print client
result = client.service.add(1,2)
print result
result = client.service.echo("Hello world")
print result
And not surprisingly the result:
C:\cygwin\home\deepti\python-suds-0.4>python client.py 3 Hello world C:\cygwin\home\deepti\python-suds-0.4>
Its amazing how easy this was. I think this is where you think programming in java sucks - putting out a simple client using basic auth would have taken 5 pages of code. Suds is an excellent library to write a quick web service client. Great tool to have in your programmers arsenal.




