Skip to content

Commit

Permalink
refactor: 리뷰 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
CFalws committed Sep 25, 2023
1 parent 6fb7e8a commit fb31ba3
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.View;
import webmvc.org.springframework.web.servlet.mvc.handlermapping.HandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.handlermapping.HandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecutionAdapter;
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 final List<HandlerMapping> handlerMappings = new ArrayList<>();
private final List<HandlerAdapter> handlerAdapters = new ArrayList<>();
private final HandlerMappings handlerMappings = new HandlerMappings();
private final HandlerAdapters handlerAdapters = new HandlerAdapters();

@Override
public void init() {
Expand All @@ -40,33 +36,21 @@ protected void service(final HttpServletRequest request, final HttpServletRespon
log.debug("Method : {}, Request URI : {}", request.getMethod(), requestURI);

try {
final var handler = findHandler(request);
final var handlerAdapter = findHandlerAdapter(handler);
final var modelAndView = handlerAdapter.handle(handler, request, response);
move(modelAndView, request, response);
final var optionalHandler = handlerMappings.findHandler(request);
if (optionalHandler.isEmpty()) {
move(new ModelAndView(new JspView("/404.jsp")), request, response);
} else {
Handler handler = optionalHandler.get();
final var handlerAdapter = handlerAdapters.findHandlerAdapter(handler);
final var modelAndView = handlerAdapter.handle(handler, request, response);
move(modelAndView, request, response);
}
} catch (Exception e) {
log.error("Exception : {}", e.getMessage(), e);
throw new ServletException(e.getMessage());
}
}

private Object findHandler(HttpServletRequest request) {
for (HandlerMapping handlerMapping : handlerMappings) {
Optional<Object> handler = handlerMapping.getHandler(request);
if (handler.isPresent()) {
return handler.get();
}
}
throw new IllegalArgumentException("handler를 찾을 수 없습니다. uri: " + request.getRequestURI());
}

private HandlerAdapter findHandlerAdapter(Object handler) {
return handlerAdapters.stream()
.filter(handlerAdapter -> handlerAdapter.supports(handler))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("handlerAdapter를 찾을 수 없습니다."));
}

private void move(
final ModelAndView modelAndView,
final HttpServletRequest request,
Expand Down
9 changes: 9 additions & 0 deletions mvc/src/main/java/web/org/springframework/web/Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package web.org.springframework.web;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public interface Handler {

Object handle(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
21 changes: 21 additions & 0 deletions mvc/src/main/java/web/org/springframework/web/HandlerAdapters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package web.org.springframework.web;

import java.util.ArrayList;
import java.util.List;
import webmvc.org.springframework.web.servlet.mvc.handlermapping.HandlerAdapter;

public class HandlerAdapters {

private final List<HandlerAdapter> handlerAdapters = new ArrayList<>();

public void add(HandlerAdapter handlerAdapter) {
handlerAdapters.add(handlerAdapter);
}

public HandlerAdapter findHandlerAdapter(Handler handler) {
return handlerAdapters.stream()
.filter(adapter -> adapter.supports(handler))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("handlerAdapter를 찾을 수 없습니다. handler: " + handler));
}
}
26 changes: 26 additions & 0 deletions mvc/src/main/java/web/org/springframework/web/HandlerMappings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package web.org.springframework.web;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import webmvc.org.springframework.web.servlet.mvc.handlermapping.HandlerMapping;

public class HandlerMappings {

private final List<HandlerMapping> mappings = new ArrayList<>();

void add(HandlerMapping handlerMapping) {
mappings.add(handlerMapping);
}

public Optional<Handler> findHandler(HttpServletRequest request) {
for (HandlerMapping handlerMapping : mappings) {
Optional<Handler> handler = handlerMapping.getHandler(request);
if (handler.isPresent()) {
return handler;
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import web.org.springframework.web.Handler;
import webmvc.org.springframework.web.servlet.ModelAndView;

public interface HandlerAdapter {

boolean supports(Object handler);
boolean supports(Handler handler);

ModelAndView handle(Object handler, HttpServletRequest request, HttpServletResponse response) throws Exception;
ModelAndView handle(Handler handler, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import jakarta.servlet.http.HttpServletRequest;

import java.util.Optional;
import web.org.springframework.web.Handler;

public interface HandlerMapping {

Optional<Object> getHandler(final HttpServletRequest request);
Optional<Handler> getHandler(final HttpServletRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.Handler;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import webmvc.org.springframework.web.servlet.mvc.handlermapping.HandlerMapping;
Expand Down Expand Up @@ -57,7 +58,7 @@ private void putHandlerExecutionOfTypeAnnotatedWithController(Method handler, Ob
}

@Override
public Optional<Object> getHandler(final HttpServletRequest request) {
public Optional<Handler> getHandler(final HttpServletRequest request) {
RequestMethod requestMethod = RequestMethod.valueOf(request.getMethod());
HandlerKey handlerKey = new HandlerKey(request.getRequestURI(), requestMethod);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import web.org.springframework.web.Handler;

public class HandlerExecution {
public class HandlerExecution implements Handler {

private final Method handler;
private final Object instance;
Expand All @@ -14,11 +15,8 @@ public HandlerExecution(Method handler, Object instance) {
this.instance = instance;
}

@Override
public Object handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return handler.invoke(instance, request, response);
}

public Class<?> getReturnType() {
return handler.getReturnType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import web.org.springframework.web.Handler;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.handlermapping.HandlerAdapter;
import webmvc.org.springframework.web.servlet.view.JspView;

public class HandlerExecutionAdapter implements HandlerAdapter {

@Override
public boolean supports(Object handler) {
public boolean supports(Handler handler) {
return handler instanceof HandlerExecution;
}

@Override
public ModelAndView handle(
Object handler,
Handler handler,
HttpServletRequest request,
HttpServletResponse response
) throws Exception {
Object handledResult = ((HandlerExecution) handler).handle(request, response);
Class<?> returnType = ((HandlerExecution) handler).getReturnType();
Object handledResult = handler.handle(request, response);
Class<?> returnType = handledResult.getClass();
if (returnType == ModelAndView.class) {
return (ModelAndView) handledResult;
}
Expand Down

0 comments on commit fb31ba3

Please sign in to comment.