001package org.lanyonm.playground.web.controller;
002
003import javax.servlet.http.HttpSession;
004
005import org.lanyonm.playground.service.ExceptionService;
006import org.lanyonm.playground.service.UserService;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009import org.springframework.beans.factory.annotation.Autowired;
010import org.springframework.stereotype.Controller;
011import org.springframework.ui.Model;
012import org.springframework.web.bind.annotation.RequestMapping;
013import org.springframework.web.bind.annotation.RequestMethod;
014
015/**
016 * @author lanyonm
017 */
018@Controller
019public class HomeController {
020
021        @Autowired
022        private ExceptionService exceptionService;
023        @Autowired
024        private UserService userService;
025
026        private static final Logger log = LoggerFactory.getLogger(HomeController.class);
027
028        @RequestMapping(value = "/", method = RequestMethod.GET)
029        public void index(Model model) {
030                log.debug("In the HomeController::index");
031                model.addAttribute("example", "Hello World!");
032        }
033
034        @RequestMapping(value = "/users", method = RequestMethod.GET)
035        public void users(Model model) {
036                model.addAttribute("users", userService.getAllUsers());
037        }
038
039        @RequestMapping(value = "/exceptions", method = RequestMethod.GET)
040        public void exceptions(Model model, HttpSession session) {
041                model.addAttribute("exceptionsState", isThrowingExceptions(session));
042        }
043
044        @RequestMapping(value = "/exceptions", method = RequestMethod.POST)
045        public String toggleExceptions(Model model, HttpSession session) {
046                model.addAttribute("exceptionsState", toggleExceptions(session));
047                return "exceptions";
048        }
049
050        /**
051         * keep an <tt>exceptionsToggle</tt> in the user's session
052         *
053         * @param session
054         */
055        private boolean toggleExceptions(HttpSession session) {
056                if (!isThrowingExceptions(session)) {
057                        session.setAttribute("exceptionsToggle", true);
058                        exceptionService.startThrowingExceptions();
059                        return true;
060                }
061                session.setAttribute("exceptionsToggle", false);
062                exceptionService.stopThrowingExceptions();
063                return false;
064        }
065
066        /**
067         *
068         * @param session
069         * @return true if the user's session is currently throwing exceptions
070         */
071        private boolean isThrowingExceptions(HttpSession session) {
072                if (session.getAttribute("exceptionsToggle") == null) {
073                        log.debug("the exceptionToggle was null");
074                        session.setAttribute("exceptionsToggle", false);
075                }
076                return (Boolean) session.getAttribute("exceptionsToggle");
077        }
078}