-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
815f2dd
commit e7abad4
Showing
3 changed files
with
70 additions
and
18 deletions.
There are no files selected for viewing
65 changes: 51 additions & 14 deletions
65
...c/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.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,29 +1,66 @@ | ||
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 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 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; | ||
|
||
public class AnnotationHandlerMapping { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class); | ||
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class); | ||
|
||
private final Object[] basePackage; | ||
private final Map<HandlerKey, HandlerExecution> handlerExecutions; | ||
private final Object[] basePackages; | ||
private final Map<HandlerKey, HandlerExecution> handlerExecutions; | ||
|
||
public AnnotationHandlerMapping(final Object... basePackage) { | ||
this.basePackage = basePackage; | ||
this.handlerExecutions = new HashMap<>(); | ||
} | ||
public AnnotationHandlerMapping(final Object... basePackages) { | ||
this.basePackages = basePackages; | ||
this.handlerExecutions = new HashMap<>(); | ||
} | ||
|
||
public void initialize() | ||
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { | ||
log.info("Initialized AnnotationHandlerMapping!"); | ||
|
||
final Reflections reflections = new Reflections(basePackages); | ||
|
||
public void initialize() { | ||
log.info("Initialized AnnotationHandlerMapping!"); | ||
final Set<Class<?>> controllerAnnotationClasses = | ||
reflections.getTypesAnnotatedWith(Controller.class); | ||
|
||
for (Class<?> controllerAnnotationClass : controllerAnnotationClasses) { | ||
final Object instance = controllerAnnotationClass.getDeclaredConstructor().newInstance(); | ||
final Method[] methods = controllerAnnotationClass.getDeclaredMethods(); | ||
|
||
Arrays.stream(methods) | ||
.filter(method -> method.isAnnotationPresent(RequestMapping.class)) | ||
.forEach(method -> putHandlerExecutions(method, instance)); | ||
} | ||
} | ||
|
||
private void putHandlerExecutions(final Method method, final Object target) { | ||
final RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); | ||
|
||
for (final RequestMethod requestMethod : requestMapping.method()) { | ||
final HandlerKey handlerKey = new HandlerKey(requestMapping.value(), requestMethod); | ||
|
||
public Object getHandler(final HttpServletRequest request) { | ||
return null; | ||
handlerExecutions.put(handlerKey, new HandlerExecution(method, target)); | ||
} | ||
} | ||
|
||
public Object getHandler(final HttpServletRequest request) { | ||
final String requestURI = request.getRequestURI(); | ||
final String method = request.getMethod(); | ||
|
||
final HandlerKey handlerKey = new HandlerKey(requestURI, RequestMethod.find(method)); | ||
|
||
return handlerExecutions.get(handlerKey); | ||
} | ||
} |
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