Permalink
Browse files

Final check + refactoring + javadoc

  • Loading branch information...
1 parent 19dabcf commit 9562f5841809dda99e98fdac2b9f3c56fe3649e6 Koen Serneels committed Apr 29, 2012
Showing with 1,119 additions and 661 deletions.
  1. +0 −90 ...ndixA-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/AuthenticationController.java
  2. +0 −31 appendixA-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/AuthenticationForm.java
  3. +0 −105 appendixA-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/OrderController.java
  4. +66 −17 ...rc/main/java/com/apress/prospringmvc/bookstore/web/config/BookstoreWebApplicationInitializer.java
  5. +11 −8 ...kstore/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebMvcContextConfiguration.java
  6. +2 −1 ...store/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebflowContextConfiguration.java
  7. +26 −0 ...tore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/AuthenticationController.java
  8. +37 −0 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/BookDetailController.java
  9. +4 −3 ...bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/{ → controller}/MainController.java
  10. +12 −5 ...ore/src/main/java/com/apress/prospringmvc/bookstore/web/{ → controller}/ManageBookController.java
  11. +10 −1 ...bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/{ → controller}/ManageBookForm.java
  12. +9 −1 ...store/src/main/java/com/apress/prospringmvc/bookstore/web/{ → controller}/ManageCategoryForm.java
  13. +113 −0 ...ixA-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/OrderController.java
  14. +3 −2 ...dixA-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/{ → controller}/OrderForm.java
  15. +7 −0 ...src/main/java/com/apress/prospringmvc/bookstore/web/interceptor/CommonDataHandlerInterceptor.java
  16. +2 −0 ...n/java/com/apress/prospringmvc/bookstore/web/security/BookstoreSecurityFlowExecutionListener.java
  17. +8 −0 ...-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/security/BookstoreUserDetails.java
  18. +34 −16 ...ore/src/main/java/com/apress/prospringmvc/bookstore/web/security/BookstoreUserDetailsService.java
  19. +9 −1 .../com/apress/prospringmvc/bookstore/web/security/CloudFoundryHttpServletRequestWrappingFilter.java
  20. +7 −0 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/security/SecurityContextSupport.java
  21. +2 −0 appendixA-bookstore/src/main/resources/spring/spring-security.xml
  22. +2 −1 appendixA-bookstore/src/main/resources/spring/webflow-config.xml
  23. +2 −2 appendixA-bookstore/src/main/webapp/WEB-INF/templates/footer.jsp
  24. +16 −8 appendixA-bookstore/src/main/webapp/WEB-INF/templates/header.jsp
  25. +10 −7 appendixA-bookstore/src/main/webapp/WEB-INF/templates/template.jsp
  26. +4 −0 appendixA-bookstore/src/main/webapp/WEB-INF/tiles/tiles-configuration.xml
  27. +4 −3 appendixA-bookstore/src/main/webapp/WEB-INF/view/public/authentication/login.jsp
  28. +13 −0 appendixA-bookstore/src/main/webapp/WEB-INF/view/public/book/detail.jsp
  29. +7 −14 appendixA-bookstore/src/main/webapp/WEB-INF/view/public/createOrders/createOrders-flow.xml
  30. +4 −8 appendixA-bookstore/src/main/webapp/WEB-INF/view/secured/ordersOverview/orderDetail.jsp
  31. +2 −0 appendixA-bookstore/src/main/webapp/WEB-INF/view/secured/ordersOverview/ordersOverview-flow.xml
  32. +3 −3 appendixA-bookstore/src/main/webapp/WEB-INF/view/secured/placeOrders/placeOrders-flow.xml
  33. +2 −1 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/Account.java
  34. +2 −1 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/Address.java
  35. +6 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/Book.java
  36. +1 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/BookSearchCriteria.java
  37. +2 −1 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/Cart.java
  38. +6 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/Category.java
  39. +6 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/Order.java
  40. +2 −1 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/OrderDetail.java
  41. +6 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/Permission.java
  42. +6 −1 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/Role.java
  43. +2 −1 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/support/AccountBuilder.java
  44. +7 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/support/BookBuilder.java
  45. +18 −11 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/support/CategoryBuilder.java
  46. +2 −1 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/support/EntityBuilder.java
  47. +2 −1 ...store-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/support/InitialDataSetup.java
  48. +2 −1 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/domain/support/OrderBuilder.java
  49. +5 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/formatter/DateFormat.java
  50. +5 −0 ...c/main/java/com/apress/prospringmvc/bookstore/formatter/DateFormatAnnotationFormatterFactory.java
  51. +5 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/formatter/DateFormatter.java
  52. +7 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/repository/AccountRepository.java
  53. +8 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/repository/BookRepository.java
  54. +8 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/repository/CategoryRepository.java
  55. +7 −0 ...store-shared/src/main/java/com/apress/prospringmvc/bookstore/repository/JpaAccountRepository.java
  56. +1 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/repository/JpaBookRepository.java
  57. +7 −0 ...tore-shared/src/main/java/com/apress/prospringmvc/bookstore/repository/JpaCategoryRepository.java
  58. +1 −1 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/repository/JpaOrderRepository.java
  59. +7 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/repository/OrderRepository.java
  60. +2 −2 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/service/AccountService.java
  61. +6 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/service/AccountServiceImpl.java
  62. +15 −8 ...store-shared/src/main/java/com/apress/prospringmvc/bookstore/service/AuthenticationException.java
  63. +6 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/service/BookstoreServiceImpl.java
  64. +7 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/service/CategoryService.java
  65. +6 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/service/CategoryServiceImpl.java
  66. +7 −0 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/validation/AccountValidator.java
  67. +24 −17 bookstore-shared/src/main/java/com/apress/prospringmvc/bookstore/validation/OrderValidator.java
  68. +10 −0 ...-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/AuthenticationSessionListener.java
  69. +27 −13 ...rc/main/java/com/apress/prospringmvc/bookstore/web/config/BookstoreWebApplicationInitializer.java
  70. +3 −2 ...kstore/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebMvcContextConfiguration.java
  71. +2 −1 ...store/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebflowContextConfiguration.java
  72. +18 −17 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/BookDetailController.java
  73. +3 −2 ...er10-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/MainController.java
  74. +2 −1 ...r10-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/OrderController.java
  75. +2 −1 chapter10-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/OrderForm.java
  76. +7 −0 ...src/main/java/com/apress/prospringmvc/bookstore/web/interceptor/CommonDataHandlerInterceptor.java
  77. +1 −1 chapter10-bookstore/src/main/webapp/WEB-INF/templates/template.jsp
  78. +26 −13 ...rc/main/java/com/apress/prospringmvc/bookstore/web/config/BookstoreWebApplicationInitializer.java
  79. +3 −2 ...kstore/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebMvcContextConfiguration.java
  80. +2 −0 ...store/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebflowContextConfiguration.java
  81. +2 −1 ...tore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/AuthenticationController.java
  82. +2 −1 ...-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/AuthenticationForm.java
  83. +2 −2 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/BookDetailController.java
  84. +3 −2 ...er11-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/MainController.java
  85. +2 −1 ...r11-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/OrderController.java
  86. +2 −1 chapter11-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/OrderForm.java
  87. +7 −0 ...src/main/java/com/apress/prospringmvc/bookstore/web/interceptor/CommonDataHandlerInterceptor.java
  88. +1 −1 chapter11-bookstore/src/main/webapp/WEB-INF/templates/template.jsp
  89. +31 −18 ...rc/main/java/com/apress/prospringmvc/bookstore/web/config/BookstoreWebApplicationInitializer.java
  90. +6 −4 ...kstore/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebMvcContextConfiguration.java
  91. +2 −1 ...store/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebflowContextConfiguration.java
  92. +2 −1 ...tore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/AuthenticationController.java
  93. +2 −0 ...-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/AuthenticationForm.java
  94. +2 −2 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/BookDetailController.java
  95. +3 −2 ...er12-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/MainController.java
  96. +2 −1 ...r12-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/OrderController.java
  97. +2 −1 chapter12-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/OrderForm.java
  98. +7 −0 ...src/main/java/com/apress/prospringmvc/bookstore/web/interceptor/CommonDataHandlerInterceptor.java
  99. +1 −1 chapter12-bookstore/src/main/webapp/WEB-INF/templates/template.jsp
  100. +1 −1 chapter12-bookstore/src/main/webapp/WEB-INF/view/createOrders/createOrders-flow.xml
  101. +22 −9 ...rc/main/java/com/apress/prospringmvc/bookstore/web/config/BookstoreWebApplicationInitializer.java
  102. +3 −2 ...kstore/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebMvcContextConfiguration.java
  103. +2 −1 ...store/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebflowContextConfiguration.java
  104. +2 −1 ...tore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/AuthenticationController.java
  105. +2 −2 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/BookDetailController.java
  106. +3 −2 ...er13-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/MainController.java
  107. +11 −4 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/ManageBookController.java
  108. +9 −0 ...er13-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/ManageBookForm.java
  109. +8 −0 ...-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/ManageCategoryForm.java
  110. +2 −1 ...r13-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/OrderController.java
  111. +2 −1 chapter13-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/OrderForm.java
  112. +7 −0 ...src/main/java/com/apress/prospringmvc/bookstore/web/interceptor/CommonDataHandlerInterceptor.java
  113. +2 −0 ...n/java/com/apress/prospringmvc/bookstore/web/security/BookstoreSecurityFlowExecutionListener.java
  114. +8 −0 ...-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/security/BookstoreUserDetails.java
  115. +8 −0 ...ore/src/main/java/com/apress/prospringmvc/bookstore/web/security/BookstoreUserDetailsService.java
  116. +7 −0 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/security/SecurityContextSupport.java
  117. +1 −1 chapter13-bookstore/src/main/webapp/WEB-INF/templates/template.jsp
  118. +0 −9 chapter13-bookstore/src/main/webapp/WEB-INF/web.xml
  119. +9 −0 chapter9-bookstore/src/main/java/com/apress/prospringmvc/bookstore/dao/DummyDao.java
  120. +59 −37 ...store/src/main/java/com/apress/prospringmvc/bookstore/web/BookstoreWebApplicationInitializer.java
  121. +119 −118 ...kstore/src/main/java/com/apress/prospringmvc/bookstore/web/config/WebMvcContextConfiguration.java
  122. +6 −0 ...9-bookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/AccountController.java
  123. +2 −2 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/BookDetailController.java
  124. +2 −2 ...ookstore/src/main/java/com/apress/prospringmvc/bookstore/web/controller/BookSearchController.java
@@ -1,90 +0,0 @@
-package com.apress.prospringmvc.bookstore.web;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.binding.message.MessageBuilder;
-import org.springframework.binding.message.MessageContext;
-import org.springframework.stereotype.Controller;
-import org.springframework.validation.Errors;
-import org.springframework.web.bind.annotation.ModelAttribute;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.servlet.ModelAndView;
-import org.springframework.webflow.action.EventFactorySupport;
-import org.springframework.webflow.execution.Event;
-import org.springframework.webflow.mvc.servlet.MvcExternalContext;
-
-import com.apress.prospringmvc.bookstore.domain.Account;
-import com.apress.prospringmvc.bookstore.service.AccountService;
-import com.apress.prospringmvc.bookstore.service.AuthenticationException;
-import com.apress.prospringmvc.bookstore.service.BookstoreService;
-
-/**
- * This controller talks to the {@link BookstoreService} to authenticate a user. This controller can be used via Spring MVC
- * (request mapping login.html) or as POJO for example via Web Flow
- *
- * @author Koen Serneels
- */
-
-@Controller
-public class AuthenticationController {
-
- public static final String AUTHENTICATED_ACCOUNT_KEY = "authenticatedAccount";
-
- private static final String LOGIN_FAILED_KEY = "label.login.failed";
-
- @Autowired
- private AccountService accountService;
-
- // ----- Spring MVC logic
-
- @RequestMapping("public/authentication/login.htm")
- public ModelAndView authentication() {
- ModelAndView mov = new ModelAndView();
-
- mov.setViewName("login");
- mov.addObject("authenticationForm", initializeForm());
- return mov;
- }
-
- @RequestMapping(value = "public/authentication/authenticate.htm", method = RequestMethod.POST)
- public ModelAndView authentication(@ModelAttribute
- AuthenticationForm authenticationForm, Errors errors, ModelAndView mov, HttpSession httpSession) {
- try {
- authenticate(authenticationForm, httpSession);
- mov.addObject("authenticationOk", "true");
- mov.addObject("username", authenticationForm.getUsername());
- mov.setViewName("main");
- } catch (AuthenticationException authenticationException) {
- errors.reject(LOGIN_FAILED_KEY);
- mov.setViewName("login");
- }
-
- return mov;
- }
-
- public AuthenticationForm initializeForm() {
- return new AuthenticationForm();
- }
-
- // ---- POJO logic
- public Event authenticate(AuthenticationForm authenticationForm, MvcExternalContext externalContext,
- MessageContext messageContext) {
- try {
- authenticate(authenticationForm, ((HttpServletRequest) externalContext.getNativeRequest()).getSession());
- } catch (AuthenticationException authenticationException) {
- messageContext.addMessage(new MessageBuilder().error().code(LOGIN_FAILED_KEY).build());
- return new EventFactorySupport().error(this);
- }
- return new EventFactorySupport().success(this);
- }
-
- // ---- Helpers
- private void authenticate(AuthenticationForm authenticationForm, HttpSession httpSession)
- throws AuthenticationException {
- Account account = accountService.login(authenticationForm.getUsername(), authenticationForm.getPassword());
- httpSession.setAttribute(AUTHENTICATED_ACCOUNT_KEY, account);
- }
-}
@@ -1,31 +0,0 @@
-package com.apress.prospringmvc.bookstore.web;
-
-import java.io.Serializable;
-
-/**
- * Simple form for holding username and password
- *
- * @author Koen Serneels
- */
-
-public class AuthenticationForm implements Serializable {
-
- private String username;
- private String password;
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-}
@@ -1,105 +0,0 @@
-package com.apress.prospringmvc.bookstore.web;
-
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.List;
-
-import org.apache.commons.lang3.time.DateUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.propertyeditors.CustomDateEditor;
-import org.springframework.binding.message.MessageBuilder;
-import org.springframework.binding.message.MessageContext;
-import org.springframework.stereotype.Component;
-import org.springframework.web.bind.WebDataBinder;
-import org.springframework.web.bind.annotation.InitBinder;
-import org.springframework.webflow.action.EventFactorySupport;
-import org.springframework.webflow.execution.Event;
-
-import com.apress.prospringmvc.bookstore.domain.Book;
-import com.apress.prospringmvc.bookstore.domain.Category;
-import com.apress.prospringmvc.bookstore.domain.Order;
-import com.apress.prospringmvc.bookstore.domain.support.OrderBuilder;
-import com.apress.prospringmvc.bookstore.service.BookstoreService;
-import com.apress.prospringmvc.bookstore.service.CategoryService;
-import com.apress.prospringmvc.bookstore.web.security.SecurityContextSupport;
-
-/**
- * Controller to be used to place and view orders using the {@link BookstoreService}. This controller can be used using
- * Spring MVC (view orders) or by POJO access (for example Web Flow) for placing orders
- *
- * @author Koen Serneels
- */
-
-@Component
-public class OrderController {
- @Autowired
- private BookstoreService bookstoreService;
-
- @Autowired
- private CategoryService categoryService;
-
- public List<Order> retrieveOrders() {
- List<Order> orders = this.bookstoreService.findOrdersForAccount(SecurityContextSupport.getUserDetails()
- .getAccount());
- return orders;
- }
-
- public OrderForm initializeForm() {
- OrderForm orderForm = new OrderForm();
- orderForm.setQuantity(1);
- orderForm.setOrderDate(new Date());
- return orderForm;
- }
-
- public List<Category> initializeSelectableCategories() {
- return this.categoryService.findAll();
- }
-
- public List<Book> initializeSelectableBooks(OrderForm orderForm) {
- return this.bookstoreService.findBooksByCategory(orderForm.getCategory());
- }
-
- public void addBook(OrderForm orderForm) {
- Book book = orderForm.getBook();
- if (orderForm.getBooks().containsKey(book)) {
- orderForm.getBooks().put(book, orderForm.getBooks().get(book) + orderForm.getQuantity());
- } else {
- orderForm.getBooks().put(book, orderForm.getQuantity());
- }
- }
-
- public Long placeOrder(OrderForm orderForm) {
- Order order = new OrderBuilder().addBooks(orderForm.getBooks()).deliveryDate(orderForm.getDeliveryDate())
- .orderDate(orderForm.getOrderDate()).account(SecurityContextSupport.getUserDetails().getAccount())
- .build(true);
- return this.bookstoreService.store(order).getId();
- }
-
- public Event validateDeliveryDate(OrderForm orderForm, MessageContext messageContext) {
- if (orderForm.getDeliveryDate() == null) {
- MessageBuilder errorMessageBuidler = new MessageBuilder().error();
- errorMessageBuidler.source("deliveryDate");
- errorMessageBuidler.code("error.page.selectdeliveryoptions.deliverydate.required");
- messageContext.addMessage(errorMessageBuidler.build());
- return new EventFactorySupport().error(this);
- }
-
- if (orderForm.getDeliveryDate().before(DateUtils.truncate(orderForm.getOrderDate(), Calendar.DAY_OF_MONTH))) {
- MessageBuilder errorMessageBuidler = new MessageBuilder().error();
- errorMessageBuidler.source("deliveryDate");
- errorMessageBuidler.code("error.page.selectdeliveryoptions.deliverydate.in.past");
- messageContext.addMessage(errorMessageBuidler.build());
- return new EventFactorySupport().error(this);
- }
- return new EventFactorySupport().success(this);
- }
-
- @InitBinder
- public void initBinder(WebDataBinder binder) {
- SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
- dateFormat.setLenient(false);
- binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
- }
-
-}
@@ -1,15 +1,21 @@
package com.apress.prospringmvc.bookstore.web.config;
+import java.util.EnumSet;
+
+import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
+import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.security.config.BeanIds;
+import org.springframework.web.SpringServletContainerInitializer;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
+import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;
@@ -18,44 +24,87 @@
import com.apress.prospringmvc.bookstore.config.TestDataContextConfiguration;
/**
- * The main {@link WebApplicationInitializer} which starts up a {@link AnnotationConfigWebApplicationContext}. Resources
- * for this context are retrieved from annotated classes which are annotated using the {@link Configuration}. The
- * classes loaded are mentioned here are stored in the {@link #configurationClasses}
+ * {@link WebApplicationInitializer} that will be called by Spring's {@link SpringServletContainerInitializer} as part
+ * of the JEE {@link ServletContainerInitializer} pattern. This class will be called on application startup and will
+ * configure our JEE and Spring configuration.
+ * <p/>
+ *
+ * It will first initializes our {@link AnnotationConfigWebApplicationContext} with the common {@link Configuration}
+ * classes: {@link InfrastructureContextConfiguration} and {@link TestDataContextConfiguration} using a typical JEE
+ * {@link ContextLoaderListener}.
+ * <p/>
+ *
+ * Next it creates a {@link DispatcherServlet}, being a normal JEE Servlet which will create on its turn a child
+ * {@link AnnotationConfigWebApplicationContext} configured with the Spring MVC {@link Configuration} classes
+ * {@link WebMvcContextConfiguration} and {@link WebflowContextConfiguration}. This Servlet will be registered using
+ * JEE's programmatical API support.
* <p/>
*
- * Finally we also programmatically configure the {@link DispatcherServlet} that listens to /
+ * Finally we also register the Spring {@link DelegatingFilterProxy} filter which will be used by Spring security to add
+ * the security filters.
+ * </p>
*
+ * Note: the {@link OpenEntityManagerInViewFilter} is only enabled for pages served soley via Spring MVC. For pages
+ * being served via WebFlow we configured WebFlow to use the JpaFlowExecutionListener.
+ *
+ * @author Marten Deinum
* @author Koen Serneels
+ *
*/
-
public class BookstoreWebApplicationInitializer implements WebApplicationInitializer {
private static final Class<?>[] configurationClasses = new Class<?>[] { TestDataContextConfiguration.class,
WebMvcContextConfiguration.class, InfrastructureContextConfiguration.class,
WebflowContextConfiguration.class };
+ private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
+
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
+ registerListener(servletContext);
+ registerDispatcherServlet(servletContext);
+ // We are using JpaFlowExecutionListener instead, but we enable it for Spring MVC served pages
+ registerOpenEntityManagerInViewFilter(servletContext);
+ registerSpringSecurityFilterChain(servletContext);
+ }
- // Create the 'root' Spring application context
- AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
- rootContext.register(configurationClasses);
+ private void registerDispatcherServlet(ServletContext servletContext) {
+ AnnotationConfigWebApplicationContext dispatcherContext = createContext(WebMvcContextConfiguration.class);
+ ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME,
+ new DispatcherServlet(dispatcherContext));
+ dispatcher.setLoadOnStartup(1);
+ dispatcher.addMapping("/");
+ }
+ private void registerListener(ServletContext servletContext) {
+ AnnotationConfigWebApplicationContext rootContext = createContext(configurationClasses);
servletContext.addListener(new ContextLoaderListener(rootContext));
+ servletContext.addListener(new RequestContextListener());
+ }
- // Register and map the dispatcher servlet
- ServletRegistration.Dynamic dispatcher = servletContext.addServlet("bookstore", new DispatcherServlet(
- rootContext));
-
- FilterRegistration.Dynamic penEntityManagerInViewFilter = servletContext.addFilter(
- "openEntityManagerInViewFilter", new OpenEntityManagerInViewFilter());
- penEntityManagerInViewFilter.addMappingForUrlPatterns(null, false, "/*");
+ private void registerOpenEntityManagerInViewFilter(ServletContext servletContext) {
+ FilterRegistration.Dynamic registration = servletContext.addFilter("openEntityManagerInView",
+ new OpenEntityManagerInViewFilter());
+ registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false,
+ "*.htm");
+ registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false,
+ "/j_spring_security_check");
+ }
+ private void registerSpringSecurityFilterChain(ServletContext servletContext) {
FilterRegistration.Dynamic springSecurityFilterChain = servletContext.addFilter(
BeanIds.SPRING_SECURITY_FILTER_CHAIN, new DelegatingFilterProxy());
springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*");
+ }
- dispatcher.setLoadOnStartup(1);
- dispatcher.addMapping("/");
+ /**
+ * Factory method to create {@link AnnotationConfigWebApplicationContext} instances.
+ * @param annotatedClasses
+ * @return
+ */
+ private AnnotationConfigWebApplicationContext createContext(final Class<?>... annotatedClasses) {
+ AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
+ context.register(annotatedClasses);
+ return context;
}
}
Oops, something went wrong.

0 comments on commit 9562f58

Please sign in to comment.