Added SWS client

This commit is contained in:
Arjen Poutsma
2007-06-14 01:21:18 +00:00
parent 1be5b4a086
commit 91a39a2289
6 changed files with 201 additions and 4 deletions

View File

@@ -2,7 +2,8 @@ SPRING WEB SERVICES
Client table of contents
---------------------------------------------------
* axis1 - A client for the airline service written in Java using JAX-RPC (Axis 1).
* cs - A client for the airline service written in C#.
* jax-ws - A client for the airline service written in Java using JAX-WS.
* saaj - Two clients that are written in Java and use SAAJ.
* axis1 - A client for the airline service written in Java using JAX-RPC (Axis 1).
* cs - A client for the airline service written in C#.
* jax-ws - A client for the airline service written in Java using JAX-WS.
* saaj - Two clients that are written in Java and use SAAJ.
* spring-ws - A client for the airline service written in Java using Spring-WS using XMLBeans marshalling.

View File

@@ -0,0 +1,62 @@
<?xml version="1.0"?>
<project name="spring-ws-airline-sample-spring-ws-client" default="build" xmlns:artifact="urn:maven-artifact-ant">
<property name="bin.dir" value="bin"/>
<property name="src.dir" value="src"/>
<property name="gen.dir" value="gen"/>
<property name="schema" value="../../src/main/webapp/WEB-INF/xsd/airline.xsd"/>
<target name="init">
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
<classpath>
<pathelement location="${basedir}/../../../maven-artifact-ant-2.0.4-dep.jar"/>
</classpath>
</typedef>
<artifact:dependencies pathId="classpath">
<dependency groupId="org.springframework.ws" artifactId="spring-ws-core" version="1.0-rc2-SNAPSHOT"/>
<dependency groupId="com.sun.xml.messaging.saaj" artifactId="saaj-impl" version="1.3"/>
<dependency groupId="commons-httpclient" artifactId="commons-httpclient" version="3.0.1"/>
<dependency groupId="commons-httpclient" artifactId="commons-httpclient" version="3.0.1"/>
<dependency groupId="xmlbeans" artifactId="xbean" version="2.2.0"/>
</artifact:dependencies>
</target>
<target name="generate" depends="init">
<mkdir dir="${gen.dir}"/>
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean" classpathref="classpath"/>
<xmlbean schema="${schema}" srcgendir="${gen.dir}" classgendir="${bin.dir}" classpathref="classpath"/>
</target>
<target name="build" depends="init,generate">
<mkdir dir="${bin.dir}"/>
<javac destdir="${bin.dir}">
<src path="${src.dir}"/>
<src path="${gen.dir}"/>
<classpath refid="classpath"/>
</javac>
<copy todir="${bin.dir}">
<fileset dir="${src.dir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="${bin.dir}"/>
<delete dir="${gen.dir}"/>
</target>
<target name="run" depends="echo"/>
<target name="echo" depends="build">
<java classname="org.springframework.ws.samples.airline.client.sws.AirlineClient"
fork="true"
failonerror="true">
<classpath refid="classpath"/>
<classpath location="${bin.dir}"/>
</java>
</target>
</project>

View File

@@ -0,0 +1,11 @@
SPRING WEB SERVICES
This directory contains a Java client for the Airline Web Service that uses
the Spring-WS WebServiceTemplate in combination with XMLBeans marshalling.
The client can be run from the provided ant file, by calling "ant run".
SAJA Client Sample table of contents
---------------------------------------------------
* src - The source files for the client
* build.xml - Ant build file with a 'build' and a 'run' target

View File

@@ -0,0 +1,5 @@
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.client.sws;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.springWs.samples.airline.schemas.BookFlightRequestDocument;
import org.springframework.springWs.samples.airline.schemas.BookFlightResponseDocument;
import org.springframework.springWs.samples.airline.schemas.Flight;
import org.springframework.springWs.samples.airline.schemas.GetFlightsRequestDocument;
import org.springframework.springWs.samples.airline.schemas.GetFlightsResponseDocument;
import org.springframework.springWs.samples.airline.schemas.Name;
import org.springframework.springWs.samples.airline.schemas.Ticket;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
public class AirlineClient extends WebServiceGatewaySupport {
public void getFlights() {
GetFlightsRequestDocument getFlightsRequestDocument = GetFlightsRequestDocument.Factory.newInstance();
GetFlightsRequestDocument.GetFlightsRequest getFlightsRequest =
getFlightsRequestDocument.addNewGetFlightsRequest();
getFlightsRequest.setFrom("AMS");
getFlightsRequest.setTo("VCE");
Calendar departureDate = Calendar.getInstance();
departureDate.clear();
departureDate.set(2006, Calendar.JANUARY, 31);
getFlightsRequest.setDepartureDate(departureDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Requesting flights on " + dateFormat.format(departureDate.getTime()));
GetFlightsResponseDocument getFlightsResponseDocument =
(GetFlightsResponseDocument) getWebServiceTemplate().marshalSendAndReceive(getFlightsRequestDocument);
GetFlightsResponseDocument.GetFlightsResponse response = getFlightsResponseDocument.getGetFlightsResponse();
System.out.println("Got " + response.sizeOfFlightArray() + " results");
if (response.sizeOfFlightArray() > 0) {
// Book the first flight using John Doe as a frequent flyer
BookFlightRequestDocument bookFlightRequestDocument = BookFlightRequestDocument.Factory.newInstance();
BookFlightRequestDocument.BookFlightRequest bookFlightRequest =
bookFlightRequestDocument.addNewBookFlightRequest();
bookFlightRequest.setFlightNumber(response.getFlightArray(0).getNumber());
bookFlightRequest.setDepartureTime(response.getFlightArray(0).getDepartureTime());
BookFlightRequestDocument.BookFlightRequest.Passengers passengers = bookFlightRequest.addNewPassengers();
passengers.addUsername("john");
BookFlightResponseDocument bookFlightResponseDocument = (BookFlightResponseDocument) getWebServiceTemplate()
.marshalSendAndReceive(bookFlightRequestDocument);
Ticket ticket = bookFlightResponseDocument.getBookFlightResponse();
writeTicket(ticket);
}
}
private static void writeTicket(Ticket ticket) {
System.out.println("Ticket " + ticket.getId());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Ticket issue date:\t" + dateFormat.format(ticket.getIssueDate().getTime()));
for (int i = 0; i < ticket.getPassengers().sizeOfPassengerArray(); i++) {
writeName(ticket.getPassengers().getPassengerArray(i));
}
writeFlight(ticket.getFlight());
}
private static void writeName(Name name) {
System.out.println("Passenger Name:");
System.out.println(name.getFirst() + " " + name.getLast());
System.out.println("------------");
}
private static void writeFlight(Flight flight) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println(dateFormat.format(flight.getDepartureTime().getTime()));
System.out.println(flight.getNumber() + "\t" + flight.getServiceClass());
System.out.println("------------");
System.out.println("Depart:\t" + flight.getFrom().getCode() + "-" + flight.getFrom().getName() + "\t" +
dateFormat.format(flight.getDepartureTime().getTime()));
System.out.println("\t" + flight.getFrom().getCity());
System.out.println("Arrive:\t" + flight.getTo().getCode() + "-" + flight.getTo().getName() + "\t" +
dateFormat.format(flight.getArrivalTime().getTime()));
System.out.println("\t" + flight.getTo().getCity());
}
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml", AirlineClient.class);
AirlineClient airlineClient = (AirlineClient) applicationContext.getBean("airlineClient", AirlineClient.class);
airlineClient.getFlights();
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="airlineClient" class="org.springframework.ws.samples.airline.client.sws.AirlineClient">
<property name="marshaller" ref="marshaller"/>
<property name="unmarshaller" ref="marshaller"/>
<property name="defaultUri" value="http://localhost:8080/airline/services"/>
</bean>
<bean id="marshaller" class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller"/>
</beans>