Skip to content

Commit

Permalink
feat: AnnotaionHandlerMapping 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
pilyang committed Sep 13, 2023
1 parent 527f223 commit 775beb4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;

import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
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 java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class AnnotationHandlerMapping {

Expand All @@ -21,9 +30,49 @@ public AnnotationHandlerMapping(final Object... basePackage) {

public void initialize() {
log.info("Initialized AnnotationHandlerMapping!");

final Reflections reflections = new Reflections(basePackage);
final Set<Class<?>> controllerClasses = reflections.getTypesAnnotatedWith(Controller.class);

for (final Class<?> controllerClass : controllerClasses) {
try {
final Object controller = controllerClass.getDeclaredConstructor().newInstance();
final Set<Method> handlerMethods = getHandlerMethods(controllerClass);

for (final Method method : handlerMethods) {
putHandler(controller, method);
}
} catch (InstantiationException |
IllegalAccessException |
InvocationTargetException |
NoSuchMethodException e
) {
log.error("Fail Initializing Controller - " + e.getMessage(), e);
}
}
}

private Set<Method> getHandlerMethods(final Class<?> controllerClass) {
return Arrays.stream(controllerClass.getMethods())
.filter(method -> method.isAnnotationPresent(RequestMapping.class))
.collect(Collectors.toUnmodifiableSet());
}

private void putHandler(final Object controller, final Method method) {
final RequestMapping requestMappingInfo = method.getAnnotation(RequestMapping.class);

for (RequestMethod requestMethod : requestMappingInfo.method()) {
final HandlerKey handlerKey = new HandlerKey(requestMappingInfo.value(), requestMethod);
final HandlerExecution handlerExecution = new HandlerExecution(controller, method);
handlerExecutions.put(handlerKey, handlerExecution);
}
}

public Object getHandler(final HttpServletRequest request) {
return null;
return handlerExecutions.get(getHandlerKey(request));
}

private HandlerKey getHandlerKey(final HttpServletRequest request) {
return new HandlerKey(request.getRequestURI(), RequestMethod.valueOf(request.getMethod()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@
import jakarta.servlet.http.HttpServletResponse;
import webmvc.org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Method;

public class HandlerExecution {

private final Object controllerInstance;
private final Method handler;

public HandlerExecution(final Object controllerInstance, final Method handler) {
this.controllerInstance = controllerInstance;
this.handler = handler;
}

public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return null;
return (ModelAndView) handler.invoke(controllerInstance, request, response);
}
}

0 comments on commit 775beb4

Please sign in to comment.