diff --git a/samples/tutorial/src/main/java/com/mycompany/hr/ws/HolidayEndpoint.java b/samples/tutorial/src/main/java/com/mycompany/hr/ws/HolidayEndpoint.java index 5ec48bcd..8442a8a2 100644 --- a/samples/tutorial/src/main/java/com/mycompany/hr/ws/HolidayEndpoint.java +++ b/samples/tutorial/src/main/java/com/mycompany/hr/ws/HolidayEndpoint.java @@ -16,7 +16,9 @@ package com.mycompany.hr.ws; +import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; @@ -25,10 +27,12 @@ import org.springframework.ws.server.endpoint.annotation.PayloadRoot; import org.springframework.ws.server.endpoint.annotation.RequestPayload; import com.mycompany.hr.service.HumanResourceService; -import org.jdom.Element; -import org.jdom.JDOMException; -import org.jdom.Namespace; -import org.jdom.xpath.XPath; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.Namespace; +import org.jdom2.filter.Filters; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; /** * This endpoint handles holiday requests. It uses a combination of JDOM and XPath to extract interesting pieces of XML @@ -41,11 +45,13 @@ public class HolidayEndpoint { private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas"; - private XPath startDateExpression; + private XPathExpression startDateExpression; - private XPath endDateExpression; + private XPathExpression endDateExpression; - private XPath nameExpression; + private XPathExpression firstNameExpression; + + private XPathExpression lastNameExpression; private HumanResourceService humanResourceService; @@ -53,22 +59,30 @@ public class HolidayEndpoint { public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException { this.humanResourceService = humanResourceService; Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI); - startDateExpression = XPath.newInstance("//hr:StartDate"); - startDateExpression.addNamespace(namespace); - endDateExpression = XPath.newInstance("//hr:EndDate"); - endDateExpression.addNamespace(namespace); - nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)"); - nameExpression.addNamespace(namespace); + XPathFactory xPathFactory = XPathFactory.instance(); + startDateExpression = xPathFactory.compile("//hr:StartDate", Filters.element(), null, namespace); + endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null, namespace); + firstNameExpression = xPathFactory.compile("//hr:FirstName", Filters.element(), null, namespace); + lastNameExpression = xPathFactory.compile("//hr:LastName", Filters.element(), null, namespace); } @PayloadRoot(namespace = NAMESPACE_URI, localPart = "HolidayRequest") public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - Date startDate = dateFormat.parse(startDateExpression.valueOf(holidayRequest)); - Date endDate = dateFormat.parse(endDateExpression.valueOf(holidayRequest)); - String name = nameExpression.valueOf(holidayRequest); + Date startDate = parseDate(startDateExpression, holidayRequest); + Date endDate = parseDate(endDateExpression, holidayRequest); + String name = firstNameExpression.evaluateFirst(holidayRequest).getText() + " " + lastNameExpression.evaluateFirst(holidayRequest).getText(); humanResourceService.bookHoliday(startDate, endDate, name); } + private Date parseDate(XPathExpression expression, Element element) throws ParseException { + Element result = expression.evaluateFirst(element); + if (result != null) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + return dateFormat.parse(result.getText()); + } else { + throw new IllegalArgumentException("Could not evaluate [" + expression + "] on [" + element + "]"); + } + } + } diff --git a/samples/tutorial/src/test/java/com/mycompany/hr/ws/HolidayEndpointTest.java b/samples/tutorial/src/test/java/com/mycompany/hr/ws/HolidayEndpointTest.java index 19a327b7..74717908 100644 --- a/samples/tutorial/src/test/java/com/mycompany/hr/ws/HolidayEndpointTest.java +++ b/samples/tutorial/src/test/java/com/mycompany/hr/ws/HolidayEndpointTest.java @@ -20,8 +20,8 @@ import java.io.InputStream; import java.util.Calendar; import com.mycompany.hr.service.HumanResourceService; -import org.jdom.Document; -import org.jdom.input.SAXBuilder; +import org.jdom2.Document; +import org.jdom2.input.SAXBuilder; import org.junit.Before; import org.junit.Test;