-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ MVC 구현하기 - 2 단계 ] 필립(양재필) 미션 제출합니다. (#453)
* refactor: 컨트롤러 초기화 메소드 추출 * chore: 컨트롤러 로딩시 로깅 추가 * feat: 어노테이션 기반 컨트롤러 맵핑 기능 추가 * refactor: RegisterController를 어노테이션 기반으로 변경 - 기존 RegisterController를 controller implement기반에서annotation기반으로 변경 - RegisterViewController를 RegisterController로 통합 * refactor: HandlerMapping 추가 - DispatcherServlet에서 각각의 HandlerMapping을 가지지 않도록 HanlderMappings 추가 - AnnotationHanlderMapping, ManualHandlerMapping의 HandlerMapping interface 추가 - HandlerMapping추가 위치 이동 : DispatcheServlet -> DispatcheServletInitalizer * refactor: HandlerAdapter 적용 - ManualControllerHandlerAdatper - 기존 Controller inteface를 위한 어댑터 - Controller 객체를 필드로 가지고있음 - HandlerExcutionAdapter - Annotation 기반으로 생성된 HanlderExcution을 위한 어댑터 - HandlerExcution을 가지고 있음
- Loading branch information
Showing
13 changed files
with
265 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/src/main/java/com/techcourse/ManualControllerHandlerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.Controller; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class ManualControllerHandlerAdapter implements HandlerAdapter { | ||
|
||
private final Controller controller; | ||
|
||
public ManualControllerHandlerAdapter(final Object controller) { | ||
this.controller = (Controller) controller; | ||
} | ||
|
||
@Override | ||
public boolean isSupport(Object handler) { | ||
return handler instanceof Controller; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception { | ||
final String path = controller.execute(request, response); | ||
return new ModelAndView(new JspView(path)); | ||
} | ||
} |
20 changes: 15 additions & 5 deletions
20
app/src/main/java/com/techcourse/ManualHandlerMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
app/src/main/java/com/techcourse/controller/RegisterViewController.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package webmvc.org.springframework.web.servlet.mvc; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
|
||
public interface HandlerAdapter { | ||
|
||
boolean isSupport(Object handler); | ||
|
||
ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception; | ||
} |
48 changes: 48 additions & 0 deletions
48
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package webmvc.org.springframework.web.servlet.mvc; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class HandlerAdapterFactory { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(HandlerExecutionAdapter.class); | ||
|
||
private final Map<Class<?>, Class<? extends HandlerAdapter>> supportedHandlerAdapters; | ||
private final Map<Object, HandlerAdapter> adapterCache; | ||
|
||
public HandlerAdapterFactory() { | ||
this.supportedHandlerAdapters = new HashMap<>(); | ||
this.adapterCache = new ConcurrentHashMap<>(); | ||
} | ||
|
||
public void addAdapterType(final Class<?> handlerType, final Class<? extends HandlerAdapter> adapterType) { | ||
log.info("Add handler adapter - {}", adapterType.getSimpleName()); | ||
supportedHandlerAdapters.put(handlerType, adapterType); | ||
} | ||
|
||
public HandlerAdapter getAdapter(final Object handler) { | ||
if (!adapterCache.containsKey(handler)) { | ||
adapterCache.put(handler, makeHandlerAdapter(handler)); | ||
} | ||
return adapterCache.get(handler); | ||
} | ||
|
||
private HandlerAdapter makeHandlerAdapter(final Object handler) { | ||
final Class<? extends HandlerAdapter> adapterClass = supportedHandlerAdapters.entrySet() | ||
.stream() | ||
.filter(entry -> entry.getKey().isInstance(handler)) | ||
.map(Map.Entry::getValue) | ||
.findAny() | ||
.orElseThrow(() -> new RuntimeException("Could not find supported adapter for " + handler.getClass().getSimpleName())); | ||
|
||
try { | ||
return adapterClass.getDeclaredConstructor(Object.class).newInstance(handler); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerExecutionAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package webmvc.org.springframework.web.servlet.mvc; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; | ||
|
||
public class HandlerExecutionAdapter implements HandlerAdapter { | ||
|
||
private final HandlerExecution handlerExecution; | ||
|
||
public HandlerExecutionAdapter(final Object handlerExecution) { | ||
this.handlerExecution = (HandlerExecution) handlerExecution; | ||
} | ||
|
||
@Override | ||
public boolean isSupport(Object handler) { | ||
return handler instanceof HandlerExecution; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception { | ||
return handlerExecution.handle(request, response); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package webmvc.org.springframework.web.servlet.mvc; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
public interface HandlerMapping { | ||
|
||
void initialize(); | ||
|
||
boolean isSupport(HttpServletRequest request); | ||
|
||
Object getHandler(HttpServletRequest request); | ||
} |
33 changes: 33 additions & 0 deletions
33
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package webmvc.org.springframework.web.servlet.mvc; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class HandlerMappings { | ||
|
||
private final List<HandlerMapping> handlerMappings; | ||
|
||
public HandlerMappings() { | ||
this.handlerMappings = new ArrayList<>(); | ||
} | ||
|
||
public void add(final HandlerMapping handlerMapping) { | ||
handlerMappings.add(handlerMapping); | ||
} | ||
|
||
public void initialize() { | ||
for (final HandlerMapping handlerMapping : handlerMappings) { | ||
handlerMapping.initialize(); | ||
} | ||
} | ||
|
||
public Object getHandler(final HttpServletRequest request) { | ||
final HandlerMapping handlerMapping = handlerMappings.stream() | ||
.filter(h -> h.isSupport(request)) | ||
.findAny() | ||
.orElseThrow(() -> new RuntimeException("Could not find supporting hanlder for " + request.getMethod() + " " + request.getRequestURI())); | ||
return handlerMapping.getHandler(request); | ||
} | ||
} |
Oops, something went wrong.