-
Notifications
You must be signed in to change notification settings - Fork 302
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 구현하기 - 1단계] 쥬니(전정준) 미션 제출합니다. #421
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.reflections.Reflections; | ||
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; | ||
|
||
public class AnnotationHandlerMapping { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class); | ||
private static final int REQUEST_INDEX = 0; | ||
private static final int RESPONSE_INDEX = 1; | ||
|
||
private final Object[] basePackage; | ||
private final Map<HandlerKey, HandlerExecution> handlerExecutions; | ||
|
@@ -20,10 +32,73 @@ public AnnotationHandlerMapping(final Object... basePackage) { | |
} | ||
|
||
public void initialize() { | ||
Set<Class<?>> controllerClasses = findClassesAnnotatedController(); | ||
|
||
initializeHandlerExecutions(controllerClasses); | ||
|
||
log.info("Initialized AnnotationHandlerMapping!"); | ||
} | ||
|
||
private Set<Class<?>> findClassesAnnotatedController() { | ||
Reflections reflections = new Reflections(basePackage); | ||
|
||
return reflections.getTypesAnnotatedWith(Controller.class); | ||
} | ||
|
||
private void initializeHandlerExecutions(Set<Class<?>> controllerClasses) { | ||
controllerClasses.forEach(controlerClass -> { | ||
List<Method> methods = extractMethods(controlerClass); | ||
|
||
initializeHandlerExecutionsByMethods(methods); | ||
}); | ||
} | ||
|
||
private List<Method> extractMethods(Class<?> controllerClasses) { | ||
return Arrays.stream(controllerClasses.getDeclaredMethods()) | ||
.filter(method -> method.isAnnotationPresent(RequestMapping.class)) | ||
.filter(method -> method.getReturnType().equals(ModelAndView.class)) | ||
.filter(method -> validateParameterType(method.getParameterTypes())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private boolean validateParameterType(Class<?>[] parameters) { | ||
return parameters[REQUEST_INDEX].equals(HttpServletRequest.class) && | ||
parameters[RESPONSE_INDEX].equals(HttpServletResponse.class); | ||
} | ||
Comment on lines
+64
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인자 검증까지 해주셨네요 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
private void initializeHandlerExecutionsByMethods(List<Method> methods) { | ||
methods.forEach(this::setupHandlerExecutions); | ||
} | ||
|
||
private void setupHandlerExecutions(Method method) { | ||
RequestMapping requestMappingAnnotation = method.getAnnotation(RequestMapping.class); | ||
String url = requestMappingAnnotation.value(); | ||
RequestMethod[] requestMethods = requestMappingAnnotation.method(); | ||
|
||
try { | ||
for (RequestMethod requestMethod : requestMethods) { | ||
HandlerKey handlerKey = new HandlerKey(url, requestMethod); | ||
Object classInstance = extractClassInstance(method); | ||
HandlerExecution handlerExecution = new HandlerExecution(method, classInstance); | ||
|
||
handlerExecutions.put(handlerKey, handlerExecution); | ||
} | ||
} catch (Exception e) { | ||
log.error("ERROR : {}", e.getMessage()); | ||
} | ||
Comment on lines
+78
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외를 처리하는 것도 하나의 행위라고 생각합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
private Object extractClassInstance(Method method) throws Exception { | ||
Class<?> declaringClassForMethod = method.getDeclaringClass(); | ||
|
||
return declaringClassForMethod.getDeclaredConstructor().newInstance(); | ||
} | ||
|
||
public Object getHandler(final HttpServletRequest request) { | ||
return null; | ||
String uri = request.getRequestURI(); | ||
RequestMethod method = RequestMethod.valueOf(request.getMethod()); | ||
|
||
return handlerExecutions.get(new HandlerKey(uri, method)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
package servlet.com.example; | ||
|
||
import jakarta.servlet.*; | ||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.annotation.WebFilter; | ||
|
||
import java.io.IOException; | ||
|
||
@WebFilter("/*") | ||
public class CharacterEncodingFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
request.getServletContext().log("doFilter() 호출"); | ||
response.setCharacterEncoding("UTF-8"); | ||
chain.doFilter(request, response); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
스트림을 사용해서 깔끔하게 작성해주셨네요.
덕분에 '무엇을 하고 싶은지?'가 정확하게 보여요!