diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java deleted file mode 100644 index a6e6625f9b..0000000000 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.techcourse; - -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServlet; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.util.Optional; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import web.org.springframework.http.HttpStatus; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; -import webmvc.org.springframework.web.servlet.view.JspView; - -public class DispatcherServlet extends HttpServlet { - - private static final long serialVersionUID = 1L; - private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); - - private HandlerMappings handlerMappings; - - public DispatcherServlet() { - } - - @Override - public void init() { - handlerMappings = new HandlerMappings(); - handlerMappings.addHandlerMapping(new AnnotationHandlerMapping("com.techcourse.controller")); - handlerMappings.addHandlerMapping(new ManualHandlerMapping()); - } - - @Override - protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { - log.debug("Method : {}, Request URI : {}", request.getMethod(), request.getRequestURI()); - - try { - Optional execution = handlerMappings.getHandler(request); - - if (execution.isEmpty()) { - response.setStatus(HttpStatus.NOT_FOUND.getValue()); - return; - } - - ModelAndView view = execution.get().handle(request, response); - move(view.getViewName(), request, response); - } catch (Throwable e) { - log.error("Exception : {}", e.getMessage(), e); - throw new ServletException(e.getMessage()); - } - } - - private void move( - String viewName, - HttpServletRequest request, - HttpServletResponse response - ) throws Exception { - if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { - response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); - return; - } - - final var requestDispatcher = request.getRequestDispatcher(viewName); - requestDispatcher.forward(request, response); - } - -} diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index 6e814cdd25..c58f02ad6c 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -3,6 +3,7 @@ import jakarta.servlet.ServletContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import web.org.springframework.web.DispatcherServlet; import web.org.springframework.web.WebApplicationInitializer; /** diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java deleted file mode 100644 index ba33db7263..0000000000 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.techcourse; - -import com.techcourse.controller.LoginController; -import com.techcourse.controller.LoginViewController; -import com.techcourse.controller.LogoutController; -import com.techcourse.controller.RegisterController; -import com.techcourse.controller.RegisterViewController; -import jakarta.servlet.http.HttpServletRequest; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; -import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; - -public class ManualHandlerMapping implements HandlerMapping { - - private static final Logger log = LoggerFactory.getLogger(ManualHandlerMapping.class); - private static final String IMPLEMENT_METHOD_NAME = "execute"; - - private final Map controllers = new HashMap<>(); - - @Override - public void initialize() { - controllers.put("/", new ForwardController("/index.jsp")); - controllers.put("/login", new LoginController()); - controllers.put("/login/view", new LoginViewController()); - controllers.put("/logout", new LogoutController()); - controllers.put("/register/view", new RegisterViewController()); - controllers.put("/register", new RegisterController()); - - log.info("Initialized Manual Handler Mapping!"); - controllers.keySet() - .forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); - } - - @Override - public HandlerExecution getHandler(HttpServletRequest request) { - log.debug("Request Mapping Uri : {}", request.getRequestURI()); - Controller controller = controllers.getOrDefault(request.getRequestURI(), null); - - if (Objects.isNull(controller)) { - return null; - } - - Method execution = Arrays.stream(controller.getClass() - .getMethods()) - .filter(method -> method.getName().equals(IMPLEMENT_METHOD_NAME)) - .findFirst() - .orElseThrow(() -> new IllegalStateException("Execute 메서드가 구현되어 있지 않습니다.")); - - return new HandlerExecution(controller, execution); - } - -} diff --git a/app/src/main/java/com/techcourse/controller/IndexController.java b/app/src/main/java/com/techcourse/controller/IndexController.java new file mode 100644 index 0000000000..dbc3685182 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/IndexController.java @@ -0,0 +1,19 @@ +package com.techcourse.controller; + +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 IndexController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView index(HttpServletRequest request, HttpServletResponse response) { + return new ModelAndView(new JspView("/index.jsp")); + } + +} diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 0428fe109e..3a3f17d790 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -2,36 +2,52 @@ 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 webmvc.org.springframework.web.servlet.mvc.asis.Controller; 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; -public class LoginController implements Controller { +@Controller +public class LoginController { private static final Logger log = LoggerFactory.getLogger(LoginController.class); - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + @RequestMapping(value = "/login", method = RequestMethod.POST) + public ModelAndView login(final HttpServletRequest req, final HttpServletResponse res) throws Exception { if (UserSession.isLoggedIn(req.getSession())) { - return "redirect:/index.jsp"; + return new ModelAndView(new JspView("redirect:/index.jsp")); } return InMemoryUserRepository.findByAccount(req.getParameter("account")) .map(user -> { log.info("User : {}", user); - return login(req, user); + return redirectAfterCheckAccount(req, user); }) - .orElse("redirect:/401.jsp"); + .orElse(new ModelAndView(new JspView("redirect:/401.jsp"))); } - private String login(final HttpServletRequest request, final User user) { + private ModelAndView redirectAfterCheckAccount(final HttpServletRequest request, final User user) { if (user.checkPassword(request.getParameter("password"))) { final var session = request.getSession(); session.setAttribute(UserSession.SESSION_KEY, user); - return "redirect:/index.jsp"; + return new ModelAndView(new JspView("redirect:/index.jsp")); } - return "redirect:/401.jsp"; + return new ModelAndView(new JspView("redirect:/401.jsp")); } + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public ModelAndView loginView(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + 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"))); + } + } diff --git a/app/src/main/java/com/techcourse/controller/LoginViewController.java b/app/src/main/java/com/techcourse/controller/LoginViewController.java deleted file mode 100644 index 86ec26cdce..0000000000 --- a/app/src/main/java/com/techcourse/controller/LoginViewController.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.techcourse.controller; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; - -public class LoginViewController implements Controller { - - private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - return UserSession.getUserFrom(req.getSession()) - .map(user -> { - log.info("logged in {}", user.getAccount()); - return "redirect:/index.jsp"; - }) - .orElse("/login.jsp"); - } -} diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java index 4642fd9450..f44208de5a 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -1,15 +1,21 @@ package com.techcourse.controller; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; +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; -public class LogoutController implements Controller { +@Controller +public class LogoutController { - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public ModelAndView logout(final HttpServletRequest req, final HttpServletResponse res) throws Exception { final var session = req.getSession(); session.removeAttribute(UserSession.SESSION_KEY); - return "redirect:/"; + return new ModelAndView(new JspView("redirect:/")); } + } diff --git a/app/src/main/java/com/techcourse/controller/RegisterAnnotationController.java b/app/src/main/java/com/techcourse/controller/RegisterAnnotationController.java deleted file mode 100644 index a1a311d665..0000000000 --- a/app/src/main/java/com/techcourse/controller/RegisterAnnotationController.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.techcourse.controller; - -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 RegisterAnnotationController { - - @RequestMapping(value = "/register", method = RequestMethod.POST) - public ModelAndView save(HttpServletRequest req, HttpServletResponse res) { - User user = new User( - 2, - req.getParameter("account"), - req.getParameter("password"), - req.getParameter("email") - ); - - InMemoryUserRepository.save(user); - - return new ModelAndView( - new JspView("redirect:/index.jsp") - ); - } - - @RequestMapping(value = "/register", method = RequestMethod.GET) - public ModelAndView show(HttpServletRequest req, HttpServletResponse res) { - return new ModelAndView( - new JspView("/register.jsp") - ); - } - -} diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index ec0ac5c0be..dcc2f49f7b 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -2,14 +2,19 @@ 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 webmvc.org.springframework.web.servlet.mvc.asis.Controller; +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; -public class RegisterController implements Controller { +@Controller +public class RegisterController { - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + @RequestMapping(value = "/register", method = RequestMethod.POST) + public ModelAndView save(HttpServletRequest req, HttpServletResponse res) { User user = new User( 2, req.getParameter("account"), @@ -18,7 +23,17 @@ public String execute(final HttpServletRequest req, final HttpServletResponse re ); InMemoryUserRepository.save(user); - return "redirect:/index.jsp"; + + return new ModelAndView( + new JspView("redirect:/index.jsp") + ); + } + + @RequestMapping(value = "/register", method = RequestMethod.GET) + public ModelAndView show(HttpServletRequest req, HttpServletResponse res) { + return new ModelAndView( + new JspView("/register.jsp") + ); } } diff --git a/app/src/main/java/com/techcourse/controller/RegisterViewController.java b/app/src/main/java/com/techcourse/controller/RegisterViewController.java deleted file mode 100644 index 328e984d75..0000000000 --- a/app/src/main/java/com/techcourse/controller/RegisterViewController.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.techcourse.controller; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; - -public class RegisterViewController implements Controller { - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - return "/register.jsp"; - } - -} diff --git a/app/src/main/java/com/techcourse/controller/UserController.java b/app/src/main/java/com/techcourse/controller/UserController.java new file mode 100644 index 0000000000..419025db56 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -0,0 +1,33 @@ +package com.techcourse.controller; + +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.JsonView; + +@Controller +public class UserController { + + private static final Logger log = LoggerFactory.getLogger(UserController.class); + + @RequestMapping(value = "/api/user", method = RequestMethod.GET) + public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { + String account = request.getParameter("account"); + log.debug("user id : {}", account); + + ModelAndView modelAndView = new ModelAndView(new JsonView()); + User user = InMemoryUserRepository.findByAccount(account) + .orElseThrow(); + + modelAndView.addObject("user", user); + return modelAndView; + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/techcourse/domain/User.java b/app/src/main/java/com/techcourse/domain/User.java index beb0919b7e..fd7d45dde8 100644 --- a/app/src/main/java/com/techcourse/domain/User.java +++ b/app/src/main/java/com/techcourse/domain/User.java @@ -22,6 +22,18 @@ public String getAccount() { return account; } + public long getId() { + return id; + } + + public String getPassword() { + return password; + } + + public String getEmail() { + return email; + } + @Override public String toString() { return "User{" + diff --git a/app/src/main/webapp/index.html b/app/src/main/webapp/index.html index 95df361069..1ca98f1be2 100644 --- a/app/src/main/webapp/index.html +++ b/app/src/main/webapp/index.html @@ -19,7 +19,7 @@ diff --git a/app/src/main/webapp/index.jsp b/app/src/main/webapp/index.jsp index 142ee3d104..4ee84d2926 100644 --- a/app/src/main/webapp/index.jsp +++ b/app/src/main/webapp/index.jsp @@ -26,7 +26,7 @@ <% } else { %> <% } %> diff --git a/app/src/main/webapp/register.jsp b/app/src/main/webapp/register.jsp index 7e68995a56..b42e5606be 100644 --- a/app/src/main/webapp/register.jsp +++ b/app/src/main/webapp/register.jsp @@ -36,7 +36,7 @@ diff --git a/mvc/src/main/java/web/org/springframework/web/DispatcherServlet.java b/mvc/src/main/java/web/org/springframework/web/DispatcherServlet.java new file mode 100644 index 0000000000..2ecbdb7af1 --- /dev/null +++ b/mvc/src/main/java/web/org/springframework/web/DispatcherServlet.java @@ -0,0 +1,49 @@ +package web.org.springframework.web; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapters; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappings; + +public class DispatcherServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); + + private HandlerMappings handlerMappings; + private HandlerAdapters handlerAdapters; + + public DispatcherServlet() { + } + + @Override + public void init() { + handlerMappings = new HandlerMappings(); + handlerMappings.addHandlerMapping(new AnnotationHandlerMapping("com.techcourse.controller")); + + handlerAdapters = new HandlerAdapters(); + handlerAdapters.addHandlerAdapter(new AnnotationHandlerAdapter()); + } + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException { + log.debug("Method : {}, Request URI : {}", request.getMethod(), request.getRequestURI()); + + try { + Object object = handlerMappings.getHandler(request); + ModelAndView view = handlerAdapters.handle(object, request, response); + view.getView().render(view.getModel(), request, response); + } catch (Throwable e) { + log.error("Exception : {}", e.getMessage(), e); + throw new ServletException(e.getMessage()); + } + } + +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java index e6ce362282..0b2a0f4a45 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java @@ -31,7 +31,4 @@ public View getView() { return view; } - public String getViewName() { - return view.getViewName(); - } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java index 0c15d553f9..e2638282c5 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java @@ -9,6 +9,4 @@ public interface View { void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception; - String getViewName(); - } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java deleted file mode 100644 index a1420222df..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java +++ /dev/null @@ -1,10 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.asis; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - -public interface Controller { - - String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception; - -} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java deleted file mode 100644 index cd8f1ef371..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java +++ /dev/null @@ -1,20 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.asis; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - -import java.util.Objects; - -public class ForwardController implements Controller { - - private final String path; - - public ForwardController(final String path) { - this.path = Objects.requireNonNull(path); - } - - @Override - public String execute(final HttpServletRequest request, final HttpServletResponse response) { - return path; - } -} 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..5d0303647b --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java @@ -0,0 +1,25 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; + +public class AnnotationHandlerAdapter implements HandlerAdapter { + + @Override + public boolean isSupport(Object object) { + return object instanceof HandlerExecution; + } + + @Override + public ModelAndView handle( + Object object, + HttpServletRequest request, + HttpServletResponse response + ) { + HandlerExecution execution = (HandlerExecution) object; + + return execution.handle(request, response); + } + +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java index cf9b16597e..31378c0f13 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java @@ -68,15 +68,14 @@ private void putHandlerExecutionByAnnotation(Object controller, Method method) { } } - @Override - public HandlerExecution getHandler(final HttpServletRequest request) { + public Object getHandler(HttpServletRequest request) { HandlerKey key = new HandlerKey( request.getRequestURI(), RequestMethod.from(request.getMethod()) ); - return handlerExecutions.getOrDefault(key, null); + return handlerExecutions.get(key); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java new file mode 100644 index 0000000000..915a4e8540 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java @@ -0,0 +1,13 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; + +public interface HandlerAdapter { + + boolean isSupport(Object object); + + ModelAndView handle(Object object, HttpServletRequest request, HttpServletResponse response); + +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.java new file mode 100644 index 0000000000..6c835f4dff --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.java @@ -0,0 +1,38 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import webmvc.org.springframework.web.servlet.ModelAndView; + +public class HandlerAdapters { + + private final List handlerAdapters; + + public HandlerAdapters(List handlerAdapters) { + this.handlerAdapters = new ArrayList<>(handlerAdapters); + } + + public HandlerAdapters() { + this(Collections.emptyList()); + } + + public void addHandlerAdapter(HandlerAdapter handlerAdapter) { + handlerAdapters.add(handlerAdapter); + } + + public ModelAndView handle( + Object object, + HttpServletRequest request, + HttpServletResponse response + ) { + return handlerAdapters.stream() + .filter(handlerAdapter -> handlerAdapter.isSupport(object)) + .map(handlerAdapter -> handlerAdapter.handle(object, request, response)) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + } + +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java index edc68d2fd2..766b894510 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java @@ -4,7 +4,6 @@ import jakarta.servlet.http.HttpServletResponse; import java.lang.reflect.Method; import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.view.JspView; public class HandlerExecution { @@ -16,16 +15,12 @@ public HandlerExecution(Object instance, Method method) { this.method = method; } - public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception { - Object resultOfHandle = method.invoke(instance, request, response); - - if (resultOfHandle instanceof ModelAndView) { - return (ModelAndView) resultOfHandle; + public ModelAndView handle(HttpServletRequest request, HttpServletResponse response) { + try { + return (ModelAndView) method.invoke(instance, request, response); + } catch (Exception exception) { + throw new IllegalArgumentException(exception); } - - return new ModelAndView( - new JspView((String) resultOfHandle) - ); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java index 36e3b5f4ed..fbd88a7a65 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java @@ -6,6 +6,6 @@ public interface HandlerMapping { void initialize(); - HandlerExecution getHandler(HttpServletRequest request); + Object getHandler(HttpServletRequest request); } diff --git a/app/src/main/java/com/techcourse/HandlerMappings.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java similarity index 72% rename from app/src/main/java/com/techcourse/HandlerMappings.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java index d1cb22556a..cd156b1670 100644 --- a/app/src/main/java/com/techcourse/HandlerMappings.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java @@ -1,13 +1,10 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc.tobe; import jakarta.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.Optional; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; public class HandlerMappings { @@ -26,11 +23,12 @@ public void addHandlerMapping(HandlerMapping handlerMapping) { handlerMappings.add(handlerMapping); } - public Optional getHandler(HttpServletRequest request) { + public Object getHandler(HttpServletRequest request) { return handlerMappings.stream() .map(handlerMapping -> handlerMapping.getHandler(request)) .filter(Objects::nonNull) - .findFirst(); + .findFirst() + .orElseThrow(IllegalArgumentException::new); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java index 191c9fcd6c..fa659870e7 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java @@ -1,20 +1,36 @@ package webmvc.org.springframework.web.servlet.view; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.View; - import java.util.Map; +import web.org.springframework.http.MediaType; +import webmvc.org.springframework.web.servlet.View; public class JsonView implements View { + private final ObjectMapper objectMapper = new ObjectMapper(); + @Override - public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { + public void render( + Map model, + HttpServletRequest request, + HttpServletResponse response + ) throws Exception { + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + String responseBody = modelToJson(model); + response.getWriter().write(responseBody); } - @Override - public String getViewName() { - return ""; + private String modelToJson(final Map model) throws Exception { + if (model.size() == 1) { + Object next = model.values() + .iterator() + .next(); + return objectMapper.writeValueAsString(next); + } + + return objectMapper.writeValueAsString(model); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java index 8e1923ee49..0a470f8fdb 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java @@ -1,5 +1,6 @@ package webmvc.org.springframework.web.servlet.view; +import jakarta.servlet.RequestDispatcher; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.util.Map; @@ -19,20 +20,31 @@ public JspView(final String viewName) { } @Override - public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { - // todo + public void render( + Map model, + HttpServletRequest request, + HttpServletResponse response + ) throws Exception { + if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { + response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); + return; + } + + renderView(model, request, response); + } + private void renderView( + Map model, + HttpServletRequest request, + HttpServletResponse response + ) throws Exception { model.keySet().forEach(key -> { log.debug("attribute name : {}, value : {}", key, model.get(key)); request.setAttribute(key, model.get(key)); }); - // todo - } - - @Override - public String getViewName() { - return viewName; + RequestDispatcher requestDispatcher = request.getRequestDispatcher(viewName); + requestDispatcher.forward(request, response); } } diff --git a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java index 8e4472cdbc..e7e04b8f6e 100644 --- a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java +++ b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java @@ -4,6 +4,7 @@ import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import webmvc.org.springframework.web.servlet.ModelAndView; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -12,11 +13,13 @@ class AnnotationHandlerMappingTest { private AnnotationHandlerMapping handlerMapping; + private AnnotationHandlerAdapter annotationHandlerAdapter; @BeforeEach void setUp() { handlerMapping = new AnnotationHandlerMapping("samples"); handlerMapping.initialize(); + annotationHandlerAdapter = new AnnotationHandlerAdapter(); } @Test @@ -28,8 +31,8 @@ void get() throws Exception { when(request.getRequestURI()).thenReturn("/get-test"); when(request.getMethod()).thenReturn("GET"); - final var handlerExecution = handlerMapping.getHandler(request); - final var modelAndView = handlerExecution.handle(request, response); + final var handler = handlerMapping.getHandler(request); + final var modelAndView = annotationHandlerAdapter.handle(handler, request, response); assertThat(modelAndView.getObject("id")).isEqualTo("gugu"); } @@ -43,8 +46,8 @@ void post() throws Exception { when(request.getRequestURI()).thenReturn("/post-test"); when(request.getMethod()).thenReturn("POST"); - final var handlerExecution = handlerMapping.getHandler(request); - final var modelAndView = handlerExecution.handle(request, response); + final var handler = handlerMapping.getHandler(request); + final var modelAndView = annotationHandlerAdapter.handle(handler, request, response); assertThat(modelAndView.getObject("id")).isEqualTo("gugu"); }