/* * COMSAT * Copyright (c) 2013-2014, Parallel Universe Software Co. All rights reserved. * * This program and the accompanying materials are dual-licensed under * either the terms of the Eclipse Public License v1.0 as published by * the Eclipse Foundation * * or (per the licensee's choosing) * * under the terms of the GNU Lesser General Public License version 3.0 * as published by the Free Software Foundation. */ /* * Based on the corresponding class in Spring Boot Samples. * Copyright the original author Rob Winch. * Released under the ASF 2.0 license. */ package comsat.sample.ui.mvc; import co.paralleluniverse.fibers.Fiber; import co.paralleluniverse.fibers.SuspendExecution; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import comsat.sample.ui.Message; import comsat.sample.ui.MessageRepository; /** * @author Rob Winch */ @Controller @RequestMapping("/") public class MessageController { private final MessageRepository messageRepository; @Autowired public MessageController(MessageRepository messageRepository) { this.messageRepository = messageRepository; } @RequestMapping public ModelAndView list() throws InterruptedException, SuspendExecution { Fiber.sleep(10); Iterable<Message> messages = this.messageRepository.findAll(); return new ModelAndView("messages/list", "messages", messages); } @RequestMapping("{id}") public ModelAndView view(@PathVariable("id") Message message) throws InterruptedException, SuspendExecution { Fiber.sleep(10); return new ModelAndView("messages/view", "message", message); } @RequestMapping(params = "form", method = RequestMethod.GET) public String createForm(@ModelAttribute Message message) throws InterruptedException, SuspendExecution { Fiber.sleep(10); return "messages/form"; } @RequestMapping(method = RequestMethod.POST) public ModelAndView create(@Valid Message message, BindingResult result, RedirectAttributes redirect) throws InterruptedException, SuspendExecution { Fiber.sleep(10); if (result.hasErrors()) { return new ModelAndView("messages/form", "formErrors", result.getAllErrors()); } message = this.messageRepository.save(message); redirect.addFlashAttribute("globalMessage", "Successfully created a new message"); return new ModelAndView("redirect:/{message.id}", "message.id", message.getId()); } @RequestMapping("foo") public String foo() throws InterruptedException, SuspendExecution { Fiber.sleep(10); throw new RuntimeException("Expected exception in controller"); } }