|
| 1 | +package org.nlpcn.jcoder.util; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.lang.reflect.Method; |
| 6 | +import java.lang.reflect.Modifier; |
| 7 | +import java.net.URL; |
| 8 | +import java.util.Enumeration; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import javax.servlet.FilterChain; |
| 14 | +import javax.servlet.FilterConfig; |
| 15 | +import javax.servlet.ServletException; |
| 16 | +import javax.servlet.ServletRequest; |
| 17 | +import javax.servlet.ServletResponse; |
| 18 | +import javax.servlet.http.HttpServletRequest; |
| 19 | +import javax.servlet.http.HttpServletResponse; |
| 20 | + |
| 21 | +import org.nlpcn.commons.lang.util.StringUtil; |
| 22 | +import org.nlpcn.jcoder.run.annotation.DefaultExecute; |
| 23 | +import org.nlpcn.jcoder.run.annotation.Execute; |
| 24 | +import org.nlpcn.jcoder.run.java.ClassUtil; |
| 25 | +import org.nlpcn.jcoder.run.java.DynamicEngine; |
| 26 | +import org.nlpcn.jcoder.run.mvc.processor.ApiAdaptorProcessor; |
| 27 | +import org.nlpcn.jcoder.run.mvc.processor.ApiCrossOriginProcessor; |
| 28 | +import org.nlpcn.jcoder.run.mvc.view.JsonView; |
| 29 | +import org.nlpcn.jcoder.util.StaticValue; |
| 30 | +import org.nutz.ioc.loader.annotation.Inject; |
| 31 | +import org.nutz.lang.Mirror; |
| 32 | +import org.nutz.mvc.ActionContext; |
| 33 | +import org.nutz.mvc.ActionInfo; |
| 34 | +import org.nutz.mvc.Mvcs; |
| 35 | +import org.nutz.mvc.NutFilter; |
| 36 | +import org.nutz.resource.Scans; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | +public class TestingFilter extends NutFilter { |
| 41 | + |
| 42 | + private static final Logger LOG = LoggerFactory.getLogger(TestingFilter.class); |
| 43 | + |
| 44 | + private static Map<String,Method> methods = null ; |
| 45 | + |
| 46 | + public static void init(String... packages) throws IOException { |
| 47 | + Map<String,Method> tempMethods = new HashMap<>() ; |
| 48 | + for (String pk : packages) { |
| 49 | + List<Class<?>> list = Scans.me().scanPackage(pk); |
| 50 | + |
| 51 | + list.forEach(cla -> { |
| 52 | + for (Method method : cla.getMethods()) { |
| 53 | + String name = cla.getSimpleName() ; |
| 54 | + if (method.getAnnotationsByType(Execute.class) != null || method.getAnnotationsByType(DefaultExecute.class) != null) { |
| 55 | + |
| 56 | + tempMethods.put(cla.getName().substring(beginIndex, endIndex), value) |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void init(FilterConfig conf) throws ServletException { |
| 65 | + sc = conf.getServletContext(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { |
| 70 | + |
| 71 | + HttpServletRequest request = (HttpServletRequest) req; |
| 72 | + HttpServletResponse response = (HttpServletResponse) resp; |
| 73 | + |
| 74 | + Mvcs.setIoc(StaticValue.getUserIoc()); // reset ioc |
| 75 | + |
| 76 | + Mvcs.setServletContext(sc); |
| 77 | + ActionContext ac = new ActionContext(); |
| 78 | + try { |
| 79 | + Mvcs.set("testing_filter", request, response); |
| 80 | + |
| 81 | + ac.setRequest(request).setResponse(response).setServletContext(request.getServletContext()); |
| 82 | + Mvcs.setActionContext(ac); |
| 83 | + |
| 84 | + new ApiCrossOriginProcessor().process(ac); |
| 85 | + |
| 86 | + Class<?> clz = Class.forName("cn.com.infcn.mobile.api.LoginAction"); |
| 87 | + |
| 88 | + ac.setModule(clz.newInstance()); |
| 89 | + |
| 90 | + Map<String, Method> methodMap = new HashMap<>(); |
| 91 | + for (Method method : clz.getMethods()) { |
| 92 | + if (!Modifier.isPublic(method.getModifiers()) || method.isBridge() || method.getDeclaringClass() != clz) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + if (Mirror.getAnnotationDeep(method, DefaultExecute.class) != null || Mirror.getAnnotationDeep(method, Execute.class) != null) { |
| 97 | + |
| 98 | + if (methodMap.containsKey(method.getName())) { |
| 99 | + throw new RuntimeException("method name repeated: " + method.getName()); |
| 100 | + } |
| 101 | + |
| 102 | + methodMap.put(method.getName(), method); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Method method = methodMap.get("login"); |
| 107 | + |
| 108 | + if (method == null) { |
| 109 | + throw new RuntimeException("not found method : "); |
| 110 | + } |
| 111 | + |
| 112 | + ac.setMethod(methodMap.get("login")); |
| 113 | + |
| 114 | + ActionInfo actionInfo = new ActionInfo(); |
| 115 | + |
| 116 | + actionInfo.setMethod(ac.getMethod()); |
| 117 | + actionInfo.setModuleType(clz); |
| 118 | + |
| 119 | + ApiAdaptorProcessor apiAdaptorProcessor = new ApiAdaptorProcessor(); |
| 120 | + apiAdaptorProcessor.init(Mvcs.getNutConfig(), actionInfo); |
| 121 | + apiAdaptorProcessor.process(ac); |
| 122 | + |
| 123 | + Mirror<?> mirror = Mirror.me(clz); |
| 124 | + |
| 125 | + for (Field field : mirror.getFields()) { |
| 126 | + Inject inject = field.getAnnotation(Inject.class); |
| 127 | + if (inject != null) { |
| 128 | + field.setAccessible(true); |
| 129 | + if (field.getType().equals(org.apache.log4j.Logger.class)) { |
| 130 | + LOG.warn("org.apache.log4j.Logger Deprecated please use org.slf4j.Logger by LoggerFactory"); |
| 131 | + mirror.setValue(ac.getModule(), field, org.apache.log4j.Logger.getLogger(ac.getModule().getClass())); |
| 132 | + } else if (field.getType().equals(org.slf4j.Logger.class)) { |
| 133 | + mirror.setValue(ac.getModule(), field, LoggerFactory.getLogger(ac.getModule().getClass())); |
| 134 | + } else { |
| 135 | + mirror.setValue(ac.getModule(), field, |
| 136 | + StaticValue.getUserIoc().get(field.getType(), StringUtil.isBlank(inject.value()) ? field.getName() : inject.value())); |
| 137 | + } |
| 138 | + field.setAccessible(false); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + Object invoke = ac.getMethod().invoke(ac.getModule(), ac.getMethodArgs()); |
| 143 | + |
| 144 | + new JsonView().render(request, response, invoke); |
| 145 | + } catch (Throwable e) { |
| 146 | + e.printStackTrace(); |
| 147 | + try { |
| 148 | + new JsonView().render(request, response, e); |
| 149 | + } catch (Throwable e1) { |
| 150 | + // TODO Auto-generated catch block |
| 151 | + e1.printStackTrace(); |
| 152 | + } |
| 153 | + } finally { |
| 154 | + Mvcs.set(null, null, null); |
| 155 | + Mvcs.ctx().removeReqCtx(); |
| 156 | + Mvcs.setServletContext(null); |
| 157 | + if (request.getSession(false) != null && request.getSession(false).getAttribute("user") == null) { //if session is empty |
| 158 | + request.getSession().invalidate(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void destroy() { |
| 166 | + System.out.println("destroy"); |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments