Skip to content

Commit

Permalink
feat: AnnotationController 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
thdwoqor committed Sep 16, 2023
1 parent 80df1a3 commit 5a00194
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/com/techcourse/DispatcherServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/com/techcourse/ManualHandlerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 5a00194

Please sign in to comment.