1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| package org.simplespring.mvc.processor.impl;
import lombok.extern.slf4j.Slf4j; import org.simplespring.core.BeanContainer; import org.simplespring.mvc.RequestProcessorChain; import org.simplespring.mvc.annotation.RequestMapping; import org.simplespring.mvc.annotation.RequestParam; import org.simplespring.mvc.annotation.ResponseBody; import org.simplespring.mvc.processor.RequestProcessor; import org.simplespring.mvc.render.JsonResultRender; import org.simplespring.mvc.render.ResourceNotFoundResultRender; import org.simplespring.mvc.render.ResultRender; import org.simplespring.mvc.render.ViewResultRender; import org.simplespring.mvc.type.ControllerMethod; import org.simplespring.mvc.type.RequestPathInfo; import org.simplespring.util.ConverterUtil; import org.simplespring.util.ValidationUtil; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.*; import java.util.concurrent.ConcurrentHashMap;
@Slf4j public class ControllerRequestProcessor implements RequestProcessor { private BeanContainer beanContainer; private Map<RequestPathInfo, ControllerMethod> pathControllerMethodMap = new ConcurrentHashMap<>();
public ControllerRequestProcessor() { this.beanContainer = BeanContainer.getInstance(); Set<Class<?>> requestMappingSet = beanContainer.getClassesByAnnotation(RequestMapping.class); initPathControllerMethodMap(requestMappingSet); }
private void initPathControllerMethodMap(Set<Class<?>> requestMappingSet) { if (ValidationUtil.isEmpty(requestMappingSet)) { return; } for (Class<?> requestMappingClass : requestMappingSet) { RequestMapping requestMapping = requestMappingClass.getAnnotation(RequestMapping.class); String basePath = requestMapping.value(); if (!basePath.startsWith("/")) { basePath = "/" + basePath; } Method[] methods = requestMappingClass.getDeclaredMethods(); if (ValidationUtil.isEmpty(methods)) { continue; } for (Method method : methods) { if (method.isAnnotationPresent(RequestMapping.class)) { RequestMapping methodRequest = method.getAnnotation(RequestMapping.class); String methodPath = methodRequest.value(); if (!methodPath.startsWith("/")) { methodPath = "/" + basePath; } String url = basePath + methodPath; Map<String, Class<?>> methodParams = new HashMap<>(); Parameter[] parameters = method.getParameters(); if (!ValidationUtil.isEmpty(parameters)) { for (Parameter parameter : parameters) { RequestParam param = parameter.getAnnotation(RequestParam.class); if (param == null) { throw new RuntimeException("The parameter must have @RequestParam"); } methodParams.put(param.value(), parameter.getType()); } } String httpMethod = String.valueOf(methodRequest.method()); RequestPathInfo requestPathInfo = new RequestPathInfo(httpMethod, url); if (this.pathControllerMethodMap.containsKey(requestPathInfo)) { log.warn("duplicate url:{} registration,current class {} method{} will override the former one", requestPathInfo.getHttpPath(), requestMappingClass.getName(), method.getName()); } ControllerMethod controllerMethod = new ControllerMethod(requestMappingClass, method, methodParams); this.pathControllerMethodMap.put(requestPathInfo, controllerMethod); } } }
}
@Override public boolean process(RequestProcessorChain requestProcessorChain) throws Exception { String method = requestProcessorChain.getRequestMethod(); String path = requestProcessorChain.getRequestPath(); ControllerMethod controllerMethod = this.pathControllerMethodMap.get(new RequestPathInfo(method, path)); if (controllerMethod == null) { requestProcessorChain.setResultRender(new ResourceNotFoundResultRender(method, path)); return false; } Object result = invokeControllerMethod(controllerMethod, requestProcessorChain.getRequest()); setResultRender(result, controllerMethod, requestProcessorChain); return true; }
private void setResultRender(Object result, ControllerMethod controllerMethod, RequestProcessorChain requestProcessorChain) { if (result == null) { return; } ResultRender resultRender; boolean isJson = controllerMethod.getInvokeMethod().isAnnotationPresent(ResponseBody.class); if (isJson) { resultRender = new JsonResultRender(result); } else { resultRender = new ViewResultRender(result); } requestProcessorChain.setResultRender(resultRender); }
private Object invokeControllerMethod(ControllerMethod controllerMethod, HttpServletRequest request) { Map<String, String> requestParamMap = new HashMap<>(); Map<String, String[]> parameterMap = request.getParameterMap(); for (Map.Entry<String, String[]> parameter : parameterMap.entrySet() ) { if (!ValidationUtil.isEmpty(parameter.getValue())) { requestParamMap.put(parameter.getKey(), parameter.getValue()[0]); } } List<Object> methodParams = new ArrayList<>(); Map<String, Class<?>> methodParamMap = controllerMethod.getMethodParameters(); for (String paramName : methodParamMap.keySet()) { Class<?> type = methodParamMap.get(paramName); String requestValue = requestParamMap.get(paramName); Object value; if (requestValue == null) { value = ConverterUtil.primitiveNull(type); } else { value = ConverterUtil.convert(type, requestValue); } methodParams.add(value); } Object controller = beanContainer.getBean(controllerMethod.getControllerClass()); Method invokeMethod = controllerMethod.getInvokeMethod(); invokeMethod.setAccessible(true); Object result; try { if (methodParams.size() == 0) { result = invokeMethod.invoke(controller); } else { result = invokeMethod.invoke(controller, methodParams.toArray()); } } catch (InvocationTargetException e) { throw new RuntimeException(e.getTargetException()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } return result; } }
|