From 5a00194c8cc9310893b6e3f5d8fb21b32948028f Mon Sep 17 00:00:00 2001 From: thdwoqor Date: Sat, 16 Sep 2023 19:27:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20AnnotationController=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/DispatcherServlet.java | 7 +++ .../com/techcourse/ManualHandlerAdapter.java | 5 +- .../annotation/AnnotationHomeController.java | 20 +++++++ .../annotation/AnnotationLoginController.java | 58 +++++++++++++++++++ .../AnnotationLogoutController.java | 23 ++++++++ .../AnnotationRegisterController.java | 33 +++++++++++ .../mvc/tobe/AnnotationHandlerAdapter.java | 21 +++++++ 7 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/techcourse/controller/annotation/AnnotationHomeController.java create mode 100644 app/src/main/java/com/techcourse/controller/annotation/AnnotationLoginController.java create mode 100644 app/src/main/java/com/techcourse/controller/annotation/AnnotationLogoutController.java create mode 100644 app/src/main/java/com/techcourse/controller/annotation/AnnotationRegisterController.java create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index 8dd44572f5..a777b870e9 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -13,6 +13,8 @@ import webmvc.org.springframework.web.servlet.HandlerMapping; import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.View; +import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; public class DispatcherServlet extends HttpServlet { @@ -34,6 +36,11 @@ public void init() { manualHandlerMapping.initialize(); handlerMappings.add(manualHandlerMapping); handlerAdapters.add(new ManualHandlerAdapter()); + + AnnotationHandlerMapping annotationHandlerMapping = new AnnotationHandlerMapping("com"); + annotationHandlerMapping.initialize(); + handlerMappings.add(annotationHandlerMapping); + handlerAdapters.add(new AnnotationHandlerAdapter()); } @Override diff --git a/app/src/main/java/com/techcourse/ManualHandlerAdapter.java b/app/src/main/java/com/techcourse/ManualHandlerAdapter.java index ddd73ac952..8673b750cd 100644 --- a/app/src/main/java/com/techcourse/ManualHandlerAdapter.java +++ b/app/src/main/java/com/techcourse/ManualHandlerAdapter.java @@ -10,10 +10,7 @@ public class ManualHandlerAdapter implements HandlerAdapter { @Override public boolean support(final Object handler) { - if (handler instanceof Controller) { - return true; - } - return false; + return handler instanceof Controller; } @Override diff --git a/app/src/main/java/com/techcourse/controller/annotation/AnnotationHomeController.java b/app/src/main/java/com/techcourse/controller/annotation/AnnotationHomeController.java new file mode 100644 index 0000000000..f8a4f29e9b --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/annotation/AnnotationHomeController.java @@ -0,0 +1,20 @@ +package com.techcourse.controller.annotation; + +import context.org.springframework.stereotype.Controller; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; + +@Controller +public class AnnotationHomeController { + + private static final String INDEX_JSP = "/index.jsp"; + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView display(HttpServletRequest req, HttpServletResponse res) { + return new ModelAndView(new JspView(INDEX_JSP)); + } +} diff --git a/app/src/main/java/com/techcourse/controller/annotation/AnnotationLoginController.java b/app/src/main/java/com/techcourse/controller/annotation/AnnotationLoginController.java new file mode 100644 index 0000000000..f2fe76b101 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/annotation/AnnotationLoginController.java @@ -0,0 +1,58 @@ +package com.techcourse.controller.annotation; + +import com.techcourse.controller.LoginController; +import com.techcourse.controller.UserSession; +import com.techcourse.domain.User; +import com.techcourse.repository.InMemoryUserRepository; +import context.org.springframework.stereotype.Controller; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; + +@Controller +public class AnnotationLoginController { + private static final Logger log = LoggerFactory.getLogger(LoginController.class); + private static final String REDIRECT_INDEX_JSP = "redirect:/index.jsp"; + private static final String REDIRECT_401_JSP = "redirect:/401.jsp"; + private static final String LOGIN_JSP = "/login.jsp"; + + @RequestMapping(value = "/login/view", method = RequestMethod.GET) + public ModelAndView display(HttpServletRequest req, HttpServletResponse res) { + return UserSession.getUserFrom(req.getSession()) + .map(user -> { + log.info("logged in {}", user.getAccount()); + return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); + }) + .orElse(new ModelAndView(new JspView(LOGIN_JSP))); + } + + @RequestMapping(value = "/login", method = RequestMethod.POST) + public ModelAndView login(HttpServletRequest req, HttpServletResponse res) { + if (UserSession.isLoggedIn(req.getSession())) { + return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); + } + + ModelAndView modelAndView = InMemoryUserRepository.findByAccount(req.getParameter("account")) + .map(user -> { + log.info("User : {}", user); + return checkPassword(req, user); + }) + .orElse(new ModelAndView(new JspView(REDIRECT_401_JSP))); + modelAndView.addObject("id", req.getAttribute("id")); + return modelAndView; + } + + private ModelAndView checkPassword(final HttpServletRequest request, final User user) { + if (user.checkPassword(request.getParameter("password"))) { + final var session = request.getSession(); + session.setAttribute(UserSession.SESSION_KEY, user); + return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); + } + return new ModelAndView(new JspView(REDIRECT_401_JSP)); + } +} diff --git a/app/src/main/java/com/techcourse/controller/annotation/AnnotationLogoutController.java b/app/src/main/java/com/techcourse/controller/annotation/AnnotationLogoutController.java new file mode 100644 index 0000000000..5bcae03bce --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/annotation/AnnotationLogoutController.java @@ -0,0 +1,23 @@ +package com.techcourse.controller.annotation; + +import com.techcourse.controller.UserSession; +import context.org.springframework.stereotype.Controller; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; + +@Controller +public class AnnotationLogoutController { + + private static final String REDIRECT_INDEX_JSP = "redirect:/index.jsp"; + + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public ModelAndView logout(HttpServletRequest req, HttpServletResponse res) { + final var session = req.getSession(); + session.removeAttribute(UserSession.SESSION_KEY); + return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); + } +} diff --git a/app/src/main/java/com/techcourse/controller/annotation/AnnotationRegisterController.java b/app/src/main/java/com/techcourse/controller/annotation/AnnotationRegisterController.java new file mode 100644 index 0000000000..0774b63981 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/annotation/AnnotationRegisterController.java @@ -0,0 +1,33 @@ +package com.techcourse.controller.annotation; + +import com.techcourse.domain.User; +import com.techcourse.repository.InMemoryUserRepository; +import context.org.springframework.stereotype.Controller; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; + +@Controller +public class AnnotationRegisterController { + + private static final String REDIRECT_INDEX_JSP = "redirect:/index.jsp"; + + @RequestMapping(value = "/register/view", method = RequestMethod.GET) + public ModelAndView display(HttpServletRequest req, HttpServletResponse res) { + return new ModelAndView(new JspView("/register.jsp")); + } + + @RequestMapping(value = "/register", method = RequestMethod.POST) + public ModelAndView register(HttpServletRequest req, HttpServletResponse res) { + final var user = new User( + req.getParameter("account"), + req.getParameter("password"), + req.getParameter("email")); + InMemoryUserRepository.save(user); + + return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); + } +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java new file mode 100644 index 0000000000..05fce07e21 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java @@ -0,0 +1,21 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.HandlerAdapter; +import webmvc.org.springframework.web.servlet.ModelAndView; + +public class AnnotationHandlerAdapter implements HandlerAdapter { + + @Override + public boolean support(final Object handler) { + return handler instanceof HandlerExecution; + } + + @Override + public ModelAndView handle(final Object handler, final HttpServletRequest request, + final HttpServletResponse response) + throws Exception { + return ((HandlerExecution) handler).handle(request, response); + } +}