Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MVC 구현하기 - 3단계] 이오(이지우) 미션 제출합니다. #572

Merged
merged 8 commits into from
Sep 25, 2023
2 changes: 1 addition & 1 deletion app/src/main/java/com/techcourse/TomcatStarter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.techcourse;

import com.techcourse.servlet.UncheckedServletException;
import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.scan.StandardJarScanner;
import webmvc.org.springframework.web.servlet.mvc.asis.servlet.UncheckedServletException;

public class TomcatStarter {

Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/techcourse/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 HomeController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) {
return new ModelAndView(new JspView("/index.jsp"));
}
}
45 changes: 37 additions & 8 deletions app/src/main/java/com/techcourse/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,65 @@

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(path = "/login")
public class LoginController {

private static final Logger log = LoggerFactory.getLogger(LoginController.class);
private static final String REDIRECT_INDEX = "redirect:/index.jsp";
private static final String REDIRECT_401 = "redirect:/401.jsp";

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
@RequestMapping(method = RequestMethod.POST)
public ModelAndView doLogin(final HttpServletRequest req, final HttpServletResponse res) {
final String view = findViewName(req);
final var modelAndView = new ModelAndView(new JspView(view));
modelAndView.addObject("id", req.getAttribute("id"));

return modelAndView;
}

@RequestMapping(value = "/view", method = RequestMethod.GET)
iamjooon2 marked this conversation as resolved.
Show resolved Hide resolved
public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) {
final String loginView = UserSession.getUserFrom(req.getSession())
.map(user -> {
log.info("logged in {}", user.getAccount());
return REDIRECT_INDEX;
})
.orElse("/login.jsp");
final var modelAndView = new ModelAndView(new JspView(loginView));
modelAndView.addObject("id", req.getAttribute("id"));

return modelAndView;
}

public String findViewName(final HttpServletRequest req) {
if (UserSession.isLoggedIn(req.getSession())) {
return "redirect:/index.jsp";
return REDIRECT_INDEX;
}

return InMemoryUserRepository.findByAccount(req.getParameter("account"))
.map(user -> {
log.info("User : {}", user);
return login(req, user);
})
.orElse("redirect:/401.jsp");
.orElse(REDIRECT_401);
}

private String login(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 REDIRECT_INDEX;
}
return "redirect:/401.jsp";
return REDIRECT_401;
}
}

This file was deleted.

20 changes: 15 additions & 5 deletions app/src/main/java/com/techcourse/controller/LogoutController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
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(path = "/logout")
public class LogoutController {

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView logout(final HttpServletRequest req, final HttpServletResponse res) {
final var session = req.getSession();
session.removeAttribute(UserSession.SESSION_KEY);
return "redirect:/";

final String view = "redirect:/";
final var modelAndView = new ModelAndView(new JspView(view));
modelAndView.addObject("id", req.getAttribute("id"));

return modelAndView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class RegisterController {

@RequestMapping(method = RequestMethod.POST)
public ModelAndView save(final HttpServletRequest req, final HttpServletResponse res) {
final String view = saveUser(req, res);
final String view = saveUser(req);
final var modelAndView = new ModelAndView(new JspView(view));
modelAndView.addObject("id", req.getAttribute("id"));

Expand All @@ -30,13 +30,16 @@ public ModelAndView show(final HttpServletRequest req, final HttpServletResponse
return modelAndView;
}

public String saveUser(final HttpServletRequest req, final HttpServletResponse res) {
public String saveUser(final HttpServletRequest req) {
final var user = new User(2,
req.getParameter("account"),
req.getParameter("password"),
req.getParameter("email"));
InMemoryUserRepository.save(user);

final var session = req.getSession();
session.setAttribute(UserSession.SESSION_KEY, user);

return "redirect:/index.jsp";
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/com/techcourse/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -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.http.exception.PageNotFoundException;
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(path = "/api")
public class UserController {

private static final Logger log = LoggerFactory.getLogger(UserController.class);

@RequestMapping(value = "/user", method = RequestMethod.GET)
public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) {
final String account = req.getParameter("account");
log.debug("user id : {}", account);

final ModelAndView modelAndView = new ModelAndView(new JsonView());
final User user = InMemoryUserRepository.findByAccount(account)
.orElseThrow(PageNotFoundException::new);

modelAndView.addObject("user", user);
return modelAndView;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.WebApplicationInitializer;
import webmvc.org.springframework.web.servlet.mvc.asis.servlet.DispatcherServlet;
iamjooon2 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Base class for {@link WebApplicationInitializer} implementations that register a {@link DispatcherServlet} in the
Expand Down
40 changes: 0 additions & 40 deletions app/src/main/java/com/techcourse/servlet/ManualHandlerMapping.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.techcourse.support.web.filter;

import jakarta.servlet.*;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;

@WebFilter("/*")
Expand All @@ -15,7 +19,7 @@ public void init(final FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, FilterChain chain)
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding(DEFAULT_ENCODING);
response.setCharacterEncoding(DEFAULT_ENCODING);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.techcourse.support.web.filter;

import jakarta.servlet.*;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@WebFilter("/*")
public class ResourceFilter implements Filter {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package web.org.springframework.http.exception;

public class PageNotFoundException extends HttpException {

public PageNotFoundException() {
this.statusCode = 404;
this.message = "요청하신 페이지를 찾을 수 없습니다 :";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package webmvc.org.springframework.web.servlet;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -19,15 +21,15 @@ public ModelAndView addObject(final String attributeName, final Object attribute
return this;
}

public void render(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
view.render(model, request, response);
}

public Object getObject(final String attributeName) {
return model.get(attributeName);
}

public Map<String, Object> getModel() {
return Collections.unmodifiableMap(model);
}

public View getView() {
return view;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

public interface View {
void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception;

String getName();
}

This file was deleted.

Loading