Skip to content

Commit 39de154

Browse files
committed
SWS-217
1 parent 957c62e commit 39de154

File tree

12 files changed

+101
-23
lines changed

12 files changed

+101
-23
lines changed

samples/airline/src/main/java/org/springframework/ws/samples/airline/dao/jpa/JpaFlightDao.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020
import javax.persistence.EntityManager;
21+
import javax.persistence.NoResultException;
2122
import javax.persistence.PersistenceContext;
2223
import javax.persistence.Query;
2324

@@ -39,7 +40,7 @@ public List<Flight> findFlights(String fromAirportCode,
3940
String toAirportCode,
4041
Interval interval,
4142
ServiceClass serviceClass) throws DataAccessException {
42-
Query query = entityManager.createQuery("FROM Flight f WHERE f.from.code = :from " +
43+
Query query = entityManager.createQuery("SELECT f FROM Flight f WHERE f.from.code = :from " +
4344
"AND f.to.code = :to AND f.departureTime >= :start AND f.departureTime <= :end AND " +
4445
"f.serviceClass = :class");
4546
query.setParameter("from", fromAirportCode);
@@ -56,10 +57,15 @@ public Flight getFlight(Long id) {
5657

5758
public Flight getFlight(String flightNumber, DateTime departureTime) {
5859
Query query = entityManager
59-
.createQuery("FROM Flight f WHERE f.number = :number AND f.departureTime = :departureTime");
60+
.createQuery("SELECT f FROM Flight f WHERE f.number = :number AND f.departureTime = :departureTime");
6061
query.setParameter("number", flightNumber);
6162
query.setParameter("departureTime", departureTime);
62-
return (Flight) query.getSingleResult();
63+
try {
64+
return (Flight) query.getSingleResult();
65+
}
66+
catch (NoResultException e) {
67+
return null;
68+
}
6369
}
6470

6571
public Flight update(Flight flight) {

samples/airline/src/main/java/org/springframework/ws/samples/airline/dao/jpa/JpaFrequentFlyerDao.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.ws.samples.airline.dao.jpa;
1818

1919
import javax.persistence.EntityManager;
20+
import javax.persistence.NoResultException;
2021
import javax.persistence.PersistenceContext;
2122
import javax.persistence.Query;
2223

@@ -32,9 +33,14 @@ public class JpaFrequentFlyerDao implements FrequentFlyerDao {
3233
private EntityManager entityManager;
3334

3435
public FrequentFlyer get(String username) throws DataAccessException {
35-
Query query = entityManager.createQuery("FROM FrequentFlyer f WHERE f.username = :username");
36+
Query query = entityManager.createQuery("SELECT f FROM FrequentFlyer f WHERE f.username = :username");
3637
query.setParameter("username", username);
37-
return (FrequentFlyer) query.getSingleResult();
38+
try {
39+
return (FrequentFlyer) query.getSingleResult();
40+
}
41+
catch (NoResultException e) {
42+
return null;
43+
}
3844
}
3945

4046
}

samples/airline/src/main/java/org/springframework/ws/samples/airline/security/AcegiFrequentFlyerSecurityService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.transaction.annotation.Transactional;
2727
import org.springframework.ws.samples.airline.dao.FrequentFlyerDao;
2828
import org.springframework.ws.samples.airline.domain.FrequentFlyer;
29+
import org.springframework.ws.samples.airline.service.NoSuchFrequentFlyerException;
2930

3031
/**
3132
* Implementation of the <code>FrequentFlyerSecurityService</code> that uses Acegi.
@@ -59,8 +60,14 @@ public FrequentFlyer getCurrentlyAuthenticatedFrequentFlyer() {
5960
}
6061

6162
@Transactional
62-
public FrequentFlyer getFrequentFlyer(String username) {
63-
return frequentFlyerDao.get(username);
63+
public FrequentFlyer getFrequentFlyer(String username) throws NoSuchFrequentFlyerException {
64+
FrequentFlyer frequentFlyer = frequentFlyerDao.get(username);
65+
if (frequentFlyer != null) {
66+
return frequentFlyer;
67+
}
68+
else {
69+
throw new NoSuchFrequentFlyerException(username);
70+
}
6471
}
6572

6673
@Transactional

samples/airline/src/main/java/org/springframework/ws/samples/airline/security/FrequentFlyerSecurityService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.ws.samples.airline.security;
1818

1919
import org.springframework.ws.samples.airline.domain.FrequentFlyer;
20+
import org.springframework.ws.samples.airline.service.NoSuchFrequentFlyerException;
2021

2122
/**
2223
* Defines the business logic for handling frequent flyers.
@@ -30,8 +31,9 @@ public interface FrequentFlyerSecurityService {
3031
*
3132
* @param username the username
3233
* @return the frequent flyer with the given username, or <code>null</code> if not found
34+
* @throws NoSuchFrequentFlyerException when the frequent flyer cannot be found
3335
*/
34-
FrequentFlyer getFrequentFlyer(String username);
36+
FrequentFlyer getFrequentFlyer(String username) throws NoSuchFrequentFlyerException;
3537

3638
/**
3739
* Returns the <code>FrequentFlyer</code> that is currently logged in.

samples/airline/src/main/java/org/springframework/ws/samples/airline/security/StubFrequentFlyerSecurityService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.ws.samples.airline.security;
1818

1919
import org.springframework.ws.samples.airline.domain.FrequentFlyer;
20+
import org.springframework.ws.samples.airline.service.NoSuchFrequentFlyerException;
2021

2122
/**
2223
* Stub implementation of <code>FrequentFlyerSecurityService</code>. This implementation is used by default by {@link
@@ -34,12 +35,12 @@ public StubFrequentFlyerSecurityService() {
3435
john.setMiles(10);
3536
}
3637

37-
public FrequentFlyer getFrequentFlyer(String username) {
38+
public FrequentFlyer getFrequentFlyer(String username) throws NoSuchFrequentFlyerException {
3839
if (john.getUsername().equals(username)) {
3940
return john;
4041
}
4142
else {
42-
return null;
43+
throw new NoSuchFrequentFlyerException(username);
4344
}
4445
}
4546

samples/airline/src/main/java/org/springframework/ws/samples/airline/service/AirlineService.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,17 @@ List<Flight> getFlights(String fromAirportCode,
6767
* @param passengers the list of passengers for the flight to book. Can be either {@link Passenger} objects with
6868
* a first and last name, or {@link FrequentFlyer} objects with a username.
6969
* @return the created ticket
70-
* @throws NoSuchFlightException if a flight with the specified flight number and departure time does not exist
71-
* @throws NoSeatAvailableException if not enough seats are available for the flight
70+
* @throws NoSuchFlightException if a flight with the specified flight number and departure time does not
71+
* exist
72+
* @throws NoSeatAvailableException if not enough seats are available for the flight
73+
* @throws NoSuchFrequentFlyerException if a specified {@link FrequentFlyer} cannot be found
7274
* @see org.springframework.ws.samples.airline.domain.Passenger
7375
* @see org.springframework.ws.samples.airline.domain.FrequentFlyer
7476
*/
75-
@Transactional(rollbackFor = {NoSuchFlightException.class, NoSeatAvailableException.class})
77+
@Transactional(readOnly = false,
78+
rollbackFor = {NoSuchFlightException.class, NoSeatAvailableException.class, NoSuchFrequentFlyerException.class})
7679
Ticket bookFlight(String flightNumber, DateTime departureTime, List<Passenger> passengers)
77-
throws NoSuchFlightException, NoSeatAvailableException;
80+
throws NoSuchFlightException, NoSeatAvailableException, NoSuchFrequentFlyerException;
7881

7982
/**
8083
* Returns the amount of frequent flyer award miles for the currently logged in frequent flyer.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2006 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.samples.airline.service;
18+
19+
import org.springframework.ws.soap.server.endpoint.annotation.FaultCode;
20+
import org.springframework.ws.soap.server.endpoint.annotation.SoapFault;
21+
22+
/**
23+
* Exception thrown when a specified frequent flyer cannot be found.
24+
*
25+
* @author Rossen Stoyanchev
26+
* @author Arjen Poutsma
27+
*/
28+
@SoapFault(faultCode = FaultCode.CLIENT)
29+
public class NoSuchFrequentFlyerException extends Exception {
30+
31+
private String username;
32+
33+
public NoSuchFrequentFlyerException(String username) {
34+
super("No frequent flyer with name [" + username + "]");
35+
this.username = username;
36+
}
37+
38+
public String getusername() {
39+
return username;
40+
}
41+
}

samples/airline/src/main/java/org/springframework/ws/samples/airline/service/impl/AirlineServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.ws.samples.airline.service.AirlineService;
3535
import org.springframework.ws.samples.airline.service.NoSeatAvailableException;
3636
import org.springframework.ws.samples.airline.service.NoSuchFlightException;
37+
import org.springframework.ws.samples.airline.service.NoSuchFrequentFlyerException;
3738

3839
/**
3940
* Default implementation of the <code>AirlineService</code> interface.
@@ -60,7 +61,7 @@ public void setFrequentFlyerSecurityService(FrequentFlyerSecurityService frequen
6061
}
6162

6263
public Ticket bookFlight(String flightNumber, DateTime departureTime, List<Passenger> passengers)
63-
throws NoSuchFlightException, NoSeatAvailableException {
64+
throws NoSuchFlightException, NoSeatAvailableException, NoSuchFrequentFlyerException {
6465
Assert.notEmpty(passengers, "No passengers given");
6566
if (logger.isDebugEnabled()) {
6667
logger.debug("Booking flight '" + flightNumber + "' on '" + departureTime + "' for " + passengers);

samples/airline/src/main/java/org/springframework/ws/samples/airline/ws/MarshallingAirlineEndpoint.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.ws.samples.airline.service.AirlineService;
4343
import org.springframework.ws.samples.airline.service.NoSeatAvailableException;
4444
import org.springframework.ws.samples.airline.service.NoSuchFlightException;
45+
import org.springframework.ws.samples.airline.service.NoSuchFrequentFlyerException;
4546
import org.springframework.ws.server.endpoint.annotation.Endpoint;
4647
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
4748

@@ -107,8 +108,8 @@ private List<Flight> getSchemaFlights(String from,
107108
* @return the JAXB2 representation of a <code>&lt;BookFlightResponse&gt;</code>
108109
*/
109110
@PayloadRoot(localPart = BOOK_FLIGHT_REQUEST, namespace = NAMESPACE)
110-
public JAXBElement<Ticket> bookFlight(BookFlightRequest request)
111-
throws NoSeatAvailableException, DatatypeConfigurationException, NoSuchFlightException {
111+
public JAXBElement<Ticket> bookFlight(BookFlightRequest request) throws NoSeatAvailableException,
112+
DatatypeConfigurationException, NoSuchFlightException, NoSuchFrequentFlyerException {
112113
if (logger.isDebugEnabled()) {
113114
logger.debug("Received BookingFlightRequest '" + request.getFlightNumber() + "' on '" +
114115
request.getDepartureTime() + "' for " + request.getPassengers().getPassengerOrUsername());
@@ -121,8 +122,8 @@ public JAXBElement<Ticket> bookFlight(BookFlightRequest request)
121122
/** Converts between the domain and schema types. */
122123
private Ticket bookSchemaFlight(String flightNumber,
123124
XMLGregorianCalendar xmlDepartureTime,
124-
List<Object> passengerOrUsernameList)
125-
throws NoSeatAvailableException, NoSuchFlightException, DatatypeConfigurationException {
125+
List<Object> passengerOrUsernameList) throws NoSeatAvailableException,
126+
NoSuchFlightException, NoSuchFrequentFlyerException, DatatypeConfigurationException {
126127
DateTime departureTime = SchemaConversionUtils.toDateTime(xmlDepartureTime);
127128
List<Passenger> passengers = new ArrayList<Passenger>(passengerOrUsernameList.size());
128129
for (Iterator<Object> iterator = passengerOrUsernameList.iterator(); iterator.hasNext();) {
@@ -143,4 +144,4 @@ else if (passengerOrUsername instanceof String) {
143144
return SchemaConversionUtils.toSchemaType(domainTicket);
144145
}
145146

146-
}
147+
}

samples/airline/src/main/java/org/springframework/ws/samples/airline/ws/XPathAirlineEndpoint.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.ws.samples.airline.service.AirlineService;
4343
import org.springframework.ws.samples.airline.service.NoSeatAvailableException;
4444
import org.springframework.ws.samples.airline.service.NoSuchFlightException;
45+
import org.springframework.ws.samples.airline.service.NoSuchFrequentFlyerException;
4546
import org.springframework.ws.server.endpoint.annotation.Endpoint;
4647
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
4748
import org.springframework.ws.server.endpoint.annotation.XPathParam;
@@ -119,8 +120,9 @@ public Source getFlights(@XPathParam("//tns:from")String from,
119120
public Source bookFlight(@XPathParam("//tns:flightNumber")String flightNumber,
120121
@XPathParam("//tns:departureTime")String departureTimeString,
121122
@XPathParam("//tns:passengers/tns:passenger")NodeList passengerNodes,
122-
@XPathParam("//tns:passengers/tns:username")NodeList frequentFlyerNodes)
123-
throws NoSeatAvailableException, NoSuchFlightException, DatatypeConfigurationException, JAXBException {
123+
@XPathParam("//tns:passengers/tns:username")NodeList frequentFlyerNodes) throws
124+
NoSeatAvailableException, NoSuchFlightException, NoSuchFrequentFlyerException,
125+
DatatypeConfigurationException, JAXBException {
124126
if (logger.isDebugEnabled()) {
125127
logger.debug("Received BookingFlightRequest '" + flightNumber + "' on '" + departureTimeString + "' for " +
126128
passengerNodes.getLength() + " passengers and " + frequentFlyerNodes.getLength() +

0 commit comments

Comments
 (0)