-
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단계] 유콩(김유빈) 미션 제출합니다 (#251)
* docs: 2단계 체크리스트 작성 * refactor: controller 패키지 이동 * refactor: mapping 과 adapter 패키지 분리 * feat: ControllerScanner 로 @controller 붙은 객체 조회 * feat: HandlerMappingRegistry 에서 HandlerMapping 처리 * feat: HandlerAdapterRegistry 에서 HandlerAdapter 처리 * test: 요청 및 응답 객체 픽스쳐 생성 * refactor: final 키워드 추가 및 불필요한 선언 삭제 * refactor: BDDMockito 메서드로 변경 * refactor: 테스트에서만 사용하는 생성자 제거 * refactor: getter 가 아닌 메서드 이용하여 render * refactor: 에러메시지 변경
- Loading branch information
Showing
33 changed files
with
450 additions
and
188 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
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
3 changes: 2 additions & 1 deletion
3
...vc/controller/asis/ForwardController.java → ...hcourse/controller/ForwardController.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
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
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
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
2 changes: 1 addition & 1 deletion
2
...tstep/mvc/controller/asis/Controller.java → ...a/nextstep/mvc/controller/Controller.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
41 changes: 41 additions & 0 deletions
41
mvc/src/main/java/nextstep/mvc/controller/ControllerScanner.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,41 @@ | ||
package nextstep.mvc.controller; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.reflections.Reflections; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import nextstep.web.annotation.Controller; | ||
|
||
public class ControllerScanner { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ControllerScanner.class); | ||
|
||
private ControllerScanner() { | ||
} | ||
|
||
public static Map<Class<?>, Object> getControllers(final Object... basePackage) { | ||
final Reflections reflections = new Reflections(basePackage); | ||
final Set<Class<?>> controllers = reflections.getTypesAnnotatedWith(Controller.class); | ||
|
||
return controllers.stream() | ||
.collect(Collectors.toMap( | ||
Function.identity(), | ||
ControllerScanner::initController | ||
)); | ||
} | ||
|
||
private static Object initController(final Class<?> clazz) { | ||
try { | ||
return clazz.getDeclaredConstructors()[0] | ||
.newInstance(); | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
throw new IllegalArgumentException(String.format("생성자가 존재하지 않습니다. [%s]", clazz.getName())); | ||
} | ||
} | ||
} |
81 changes: 0 additions & 81 deletions
81
mvc/src/main/java/nextstep/mvc/controller/tobe/AnnotationHandlerMapping.java
This file was deleted.
Oops, something went wrong.
7 changes: 3 additions & 4 deletions
7
...roller/tobe/AnnotationHandlerAdapter.java → ...dleradapter/AnnotationHandlerAdapter.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
2 changes: 1 addition & 1 deletion
2
...ain/java/nextstep/mvc/HandlerAdapter.java → ...ep/mvc/handleradapter/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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package nextstep.mvc; | ||
package nextstep.mvc.handleradapter; | ||
|
||
import nextstep.mvc.view.ModelAndView; | ||
|
||
|
39 changes: 39 additions & 0 deletions
39
mvc/src/main/java/nextstep/mvc/handleradapter/HandlerAdapterRegistry.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,39 @@ | ||
package nextstep.mvc.handleradapter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import nextstep.mvc.view.ModelAndView; | ||
|
||
public class HandlerAdapterRegistry { | ||
|
||
private final List<HandlerAdapter> handlerAdapters; | ||
|
||
public HandlerAdapterRegistry() { | ||
this.handlerAdapters = new ArrayList<>(); | ||
} | ||
|
||
public void add(final HandlerAdapter handlerAdapter) { | ||
handlerAdapters.add(handlerAdapter); | ||
} | ||
|
||
public ModelAndView getModelAndView(final Object handler, final HttpServletRequest request, final HttpServletResponse response) { | ||
return handlerAdapters.stream() | ||
.filter(handlerAdapter -> handlerAdapter.supports(handler)) | ||
.map(handlerAdapter -> getModelAndView(handler, request, response, handlerAdapter)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException(String.format("적절한 handler 가 존재하지 않습니다. [%s]", handler))); | ||
} | ||
|
||
private ModelAndView getModelAndView(final Object handler, | ||
final HttpServletRequest request, final HttpServletResponse response, final HandlerAdapter handlerAdapter | ||
){ | ||
try { | ||
return handlerAdapter.handle(request, response, handler); | ||
} catch (Exception e) { | ||
throw new IllegalArgumentException(e.getMessage()); | ||
} | ||
} | ||
} |
5 changes: 2 additions & 3 deletions
5
.../com/techcourse/ManualHandlerAdapter.java → .../handleradapter/ManualHandlerAdapter.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
Oops, something went wrong.