Using client-side WS-Security in airline Spring-WS client

This commit is contained in:
Arjen Poutsma
2008-02-05 16:54:49 +00:00
parent 0ffb8818f2
commit 3ba48e6648
7 changed files with 214 additions and 10 deletions

View File

@@ -20,11 +20,12 @@
<artifact:dependencies pathId="classpath">
<remoteRepository refid="main"/>
<remoteRepository refid="java.net"/>
<dependency groupId="org.springframework.ws" artifactId="spring-ws-core" version="1.5.0-m1"/>
<dependency groupId="org.springframework.ws" artifactId="spring-ws-core" version="1.5.0-m2-SNAPSHOT"/>
<dependency groupId="org.springframework.ws" artifactId="spring-ws-security" version="1.5.0-m2-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"/>
<dependency groupId="log4j" artifactId="log4j" version="1.2.13"/>
</artifact:dependencies>
</target>
@@ -54,11 +55,19 @@
<delete dir="${gen.dir}"/>
</target>
<target name="run" depends="getFlights"/>
<target name="run" depends="getFlights, getFrequentFlyerMileage"/>
<target name="getFlights" depends="build">
<java classname="org.springframework.ws.samples.airline.client.sws.AirlineClient" fork="true"
failonerror="true">
<java classname="org.springframework.ws.samples.airline.client.sws.GetFlights" fork="true"
failonerror="true">
<classpath refid="classpath"/>
<classpath location="${bin.dir}"/>
</java>
</target>
<target name="getFrequentFlyerMileage" depends="build">
<java classname="org.springframework.ws.samples.airline.client.sws.GetFrequentFlyerMileage" fork="true"
failonerror="true">
<classpath refid="classpath"/>
<classpath location="${bin.dir}"/>
</java>

View File

@@ -1,4 +1,6 @@
log4j.rootLogger=WARN, stdout
log4j.logger.org.springframework.ws=DEBUG
log4j.logger.org.springframework.xml=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

View File

@@ -0,0 +1,111 @@
/*
* 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.messages.BookFlightRequestDocument;
import org.springframework.springWs.samples.airline.schemas.messages.BookFlightResponseDocument;
import org.springframework.springWs.samples.airline.schemas.messages.GetFlightsRequestDocument;
import org.springframework.springWs.samples.airline.schemas.messages.GetFlightsResponseDocument;
import org.springframework.springWs.samples.airline.schemas.types.Flight;
import org.springframework.springWs.samples.airline.schemas.types.Name;
import org.springframework.springWs.samples.airline.schemas.types.Ticket;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
public class GetFlights extends WebServiceGatewaySupport {
public GetFlights(WebServiceMessageFactory messageFactory) {
super(messageFactory);
}
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 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 void writeName(Name name) {
System.out.println("Passenger Name:");
System.out.println(name.getFirst() + " " + name.getLast());
System.out.println("------------");
}
private 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(
"org/springframework/ws/samples/airline/client/sws/applicationContext.xml");
GetFlights getFlights = (GetFlights) applicationContext.getBean("getFlights", GetFlights.class);
getFlights.getFlights();
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2008 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 javax.xml.transform.Result;
import javax.xml.transform.Source;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
public class GetFrequentFlyerMileage extends WebServiceGatewaySupport {
public GetFrequentFlyerMileage(WebServiceMessageFactory messageFactory) {
super(messageFactory);
}
public void getFrequentFlyerMileage() {
Source source = new StringSource(
"<GetFrequentFlyerMileageRequest xmlns=\"http://www.springframework.org/spring-ws/samples/airline/schemas/messages\" />");
Result result = new StringResult();
getWebServiceTemplate().sendSourceAndReceiveToResult(source, result);
System.out.println("result = " + result);
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"org/springframework/ws/samples/airline/client/sws/applicationContext.xml");
GetFrequentFlyerMileage getFrequentFlyerMileage = (GetFrequentFlyerMileage) applicationContext
.getBean("getFrequentFlyerMileage", GetFrequentFlyerMileage.class);
getFrequentFlyerMileage.getFrequentFlyerMileage();
}
}

View File

@@ -2,12 +2,36 @@
<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"/>
<bean id="messagFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>
<bean id="abstractClient" abstract="true">
<constructor-arg ref="messagFactory"/>
<property name="defaultUri" value="http://localhost:8080/airline/services"/>
</bean>
<bean id="marshaller" class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller"/>
<bean id="getFlights" parent="abstractClient" class="org.springframework.ws.samples.airline.client.sws.GetFlights">
<property name="marshaller" ref="marshaller"/>
<property name="unmarshaller" ref="marshaller"/>
</bean>
<bean id="getFrequentFlyerMileage" parent="abstractClient"
class="org.springframework.ws.samples.airline.client.sws.GetFrequentFlyerMileage">
<property name="interceptors" ref="securityInterceptor"/>
</bean>
<bean id="securityInterceptor" class="org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor">
<property name="policyConfiguration"
value="classpath:org/springframework/ws/samples/airline/client/sws/securityPolicy.xml"/>
<property name="callbackHandler">
<bean class="org.springframework.ws.soap.security.xwss.callback.SimpleUsernamePasswordCallbackHandler">
<property name="username" value="john"/>
<property name="password" value="changeme"/>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,3 @@
<xwss:SecurityConfiguration dumpMessages="false" xmlns:xwss="http://java.sun.com/xml/ns/xwss/config">
<xwss:UsernameToken digestPassword="true" useNonce="true"/>
</xwss:SecurityConfiguration>

View File

@@ -19,7 +19,6 @@
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
<version>2.1</version>
<exclusions>
@@ -37,7 +36,7 @@
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.5</version>
<scope>provided</scope>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>