|
Developing Web Services with Open Source by Chris Peltz & Claire Rogers
WSJ Vol 03 Issue 12 - pg.12
Listing 1
public class Weather extends Object {
public static Forecast getWeather(String zip)
throws Exception {
Connection conn = null;
Forecast f = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost/weatherdb",
"hpuser","hppwd");
String select = "select * from zipcode where zipcode = ?";
PreparedStatement stmt = conn.prepareStatement(select);
stmt.setString(1,zip);
ResultsSet rs = stmt.executeQuery();
rs.next();
String city = rs.getString("city");
String state = rs.getString("state");
String date = "2003-04-02";
select = "select * from forecast where zipcode = ? and dt = ?";
stmt = conn.prepareStatement(select);
stmt.setString(1,zip);
stmt.setString(2,date);
rs = stmt.executeQuery();
rs.next();
f = new Forecast(zip,city,state,date, rs.getString("forecast"),
rs.getShort("high"),
rs.getShort("low"), rs.getByte("precip") );
return f;
}
}
Listing 2
public class Forecast {
public Forecast(String z, String c, String s, String d, String f, short h, short l,
byte p) {
zip = z;
city = c;
state = s;
date = d;
forecast = f;
hi = h;
low = l;
precip = p;
}
}
Listing 3
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions ...">
<wsdl:types>
<schema ...>
<import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<complexType name="Forecast">
<sequence>
<element name="zip" nillable="true" type="xsd:string"/>
<element name="city" nillable="true" type="xsd:string"/>
<element name="state" nillable="true" type="xsd:string"/>
<element name="date" nillable="true" type="xsd:string"/>
<element name="forecast" nillable="true" type="xsd:string"/>
<element name="hi" type="xsd:short"/>
<element name="low" type="xsd:short"/>
<element name="precip" type="xsd:byte"/>
</sequence>
</complexType>
</schema>
</wsdl:types>
<wsdl:message name="getWeatherResponse">
<wsdl:part name="getWeatherReturn" type="tns2:Forecast"/>
</wsdl:message>
<wsdl:message name="getWeatherRequest">
<wsdl:part name="in0" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="Weather">
<wsdl:operation name="getWeather" parameterOrder="in0">
<wsdl:input name="getWeatherRequest" message="impl:getWeatherRequest"/>
<wsdl:output name="getWeatherResponse" message="impl:getWeatherResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="weatherSoapBinding" type="impl:Weather">
...
</wsdl:binding>
<wsdl:service name="WeatherService">
<wsdl:port name="weather" binding="impl:weatherSoapBinding">
<wsdlsoap:address location="http://localhost:8080/axis/services/weather"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Listing 4
public weather.ws.Forecast getWeather(java.lang.String in0)
throws java.rmi.RemoteException {
weather.Weather w = new weather.Weather();
weather.Forecast f = w.getWeather(in0);
weather.ws.Forecast f1 = new weather.ws.Forecast();
f1.setZip(f.zip);
f1.setCity(f.city);
f1.setState(f.state);
...
return f1;
}
Listing 5
<service name="weather" provider="java:RPC" style="rpc" use="encoded">
<parameter name="wsdlTargetNamespace" value="urn:weather"/>
<parameter name="wsdlServiceElement" value="WeatherService"/>
<parameter name="wsdlServicePort" value="weather"/>
<parameter name="className" value="weather.ws.WeatherSoapBindingImpl"/>
<parameter name="wsdlPortType" value="Weather"/>
<operation name="getWeather" qname="operNS:getWeather"
xmlns:operNS="urn:weather" returnQName="getWeatherReturn"
returnType="rtns:Forecast" xmlns:rtns="http://weather" >
<parameter name="in0" type="tns:string" xmlns:tns="http://www.w3.org/2001/XMLSchema"/>
</operation>
...
</service>
Listing 6
<target name="wsdl2java" depends="java2wsdl" description="Create Java Bindings">
<axis-wsdl2java
output="${proj.dir}"
serverside="true"
url="${proj.dir}/Weather.wsdl">
</axis-wsdl2java>
</target>
<target name="compilews" depends="wsdl2java" description="Compile Web Services">
<javac srcdir="${ws.dir}"
destdir="${build.classes.dir}"
classpathref="axis.classpath">
</javac>
</target>
<target name="deploy" depends="compilews" description="Deploy WS">
<jar destfile="${proj.dir}/Weather.jar">
<fileset dir="${proj.dir}/weather">
<include name="**/*.class"/>
</fileset>
</jar>
<copy file="${proj.dir}/Weather.jar" todir="${axis.web-inf}"/>
<axis-admin xmlfile="${proj.dir}/weather/ws/deploy.wsdd"/>
</target>
Listing 7
public class WeatherClient {
public static void main(String [] args) {
WeatherService service = new WeatherServiceLocator();
Weather weather = service.getweather();
Forecast forecast = weather.getWeather(args[0]);
System.out.println("Forecast:" + forecast.getForecast());
}
}
Listing 8
# setup protocol
protocol = ProtocolHandler.getProtocol("soap")
body = SOAPBody()
protocol.setBody(body)
# define the location of the web service
protocol.setHost("localhost")
protocol.setPath("axis/servlet/AxisServlet")
protocol.setPort( 8081 )
# Send a request to the getWeather method in the weather web service
body.setTarget("weather")
body.setMethod("getWeather")
body.addParameter( "zip", String, "80538", None )
# create a bean serializer
beanSer = BeanSerializer()
qName = QName("http://weather", "Forecast")
protocol.setMapTypes( Constants.NS_URI_SOAP_ENC, qName, Forecast, beanSer, beanSer )
# print result
for i in range(100):
response = protocol.connect()
print "TIME: ", Long(response.getTotalTime()).toString()
|