01: /**
02: * Copyright (c) 2006 Red Hat, Inc. All rights reserved.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17: * USA
18: *
19: * Component of: Red Hat Application Server
20: *
21: * Initial Developers: Greg Lapouchnian
22: * Patrick Smith
23: *
24: */package olstore.controller;
25:
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28: import javax.servlet.http.HttpSession;
29:
30: import olstore.domain.logic.OlstoreFacade;
31: import olstore.domain.manager.CartManager;
32:
33: import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
34:
35: /**
36: * An interceptor class used to ensure a cart object is attached to the session.
37: */
38: public class ShoppingCartInterceptor extends HandlerInterceptorAdapter {
39:
40: // The olstore bean instance that is provided via Spring.
41: private OlstoreFacade olstore;
42:
43: /**
44: * The Spring injection setter method for the olstore bean instance.
45: * @param olstore the bean instance that is provided via spring.
46: */
47: public void setOlstore(OlstoreFacade olstore) {
48: this .olstore = olstore;
49: }
50:
51: /**
52: * A method that is invoked before passing control on.
53: *
54: * @param request the HttpServletRequest.
55: * @param response the HttpServletResponse.
56: * @param handler the handler object being used.
57: */
58: public boolean preHandle(HttpServletRequest request,
59: HttpServletResponse response, Object handler)
60: throws Exception {
61: HttpSession session = request.getSession(true);
62:
63: if (request.getRemoteUser() != null
64: || request.getUserPrincipal() != null) {
65: if (session.getAttribute("cart") == null) {
66: CartManager cart = olstore.getShoppingCart(request
67: .getRemoteUser());
68: session.setAttribute("cart", cart);
69: }
70: }
71: return true;
72: }
73: }
|