Skip to content

Commit

Permalink
refactor: DispatcherServlet 에 어노테이션 기반 핸들러 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
jundonghyuk committed Sep 12, 2023
1 parent 650508e commit 6e633a7
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions app/src/main/java/com/techcourse/DispatcherServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,32 @@
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import webmvc.org.springframework.web.servlet.view.JspView;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.asis.ControllerHandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecutionHandlerAdapter;

public class DispatcherServlet extends HttpServlet {

private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class);

private ManualHandlerMapping manualHandlerMapping;
private HandlerMappingRegistry handlerMappingRegistry;
private HandlerAdapterRegistry handlerAdapterRegistry;

public DispatcherServlet() {
}

@Override
public void init() {
manualHandlerMapping = new ManualHandlerMapping();
manualHandlerMapping.initialize();
handlerMappingRegistry
.addHandlerMapping(new ManualHandlerMapping())
.addHandlerMapping(new AnnotationHandlerMapping());
handlerAdapterRegistry
.addHandlerAdapter(new ControllerHandlerAdapter())
.addHandlerAdapter(new HandlerExecutionHandlerAdapter());
handlerMappingRegistry.initialize();
}

@Override
Expand All @@ -30,22 +40,19 @@ protected void service(final HttpServletRequest request, final HttpServletRespon
log.debug("Method : {}, Request URI : {}", request.getMethod(), requestURI);

try {
final var controller = manualHandlerMapping.getHandler(requestURI);
final var viewName = controller.execute(request, response);
move(viewName, request, response);
Object handler = handlerMappingRegistry.getHandler(request)
.orElseThrow(() -> new IllegalArgumentException("There is not matched handler"));
HandlerAdapter handlerAdapter = handlerAdapterRegistry.getHandlerAdapter(handler)
.orElseThrow(() -> new IllegalArgumentException("Not Supported Handler"));
ModelAndView modelAndView = handlerAdapter.handle(request, response, handler);
render(modelAndView, request, response);
} catch (Throwable e) {
log.error("Exception : {}", e.getMessage(), e);
throw new ServletException(e.getMessage());
}
}

private void move(final String viewName, final HttpServletRequest request, final 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);
private void render(ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response) {
// TODO: 3단계?
}
}

0 comments on commit 6e633a7

Please sign in to comment.