Search in sources :

Example 1 with Controller

use of org.axe.annotation.ioc.Controller in project Axe by DongyuCai.

the class ControllerHelper method init.

@Override
public void init() {
    synchronized (this) {
        ACTION_LIST = new LinkedList<>();
        ACTION_MAP = new HashMap<>();
        //获取所有的 Controller 类
        Set<Class<?>> controllerClassSet = ClassHelper.getControllerClassSet();
        if (CollectionUtil.isNotEmpty(controllerClassSet)) {
            for (Class<?> controllerClass : controllerClassSet) {
                String basePath = controllerClass.getAnnotation(Controller.class).basePath();
                Method[] methods = controllerClass.getDeclaredMethods();
                if (ArrayUtil.isNotEmpty(methods)) {
                    for (Method method : methods) {
                        //判断方法是否带有 Action 注解
                        if (method.isAnnotationPresent(Request.class)) {
                            Request action = method.getAnnotation(Request.class);
                            String mappingPath = basePath + "/" + action.value();
                            String requestMethod = action.method().REQUEST_METHOD;
                            //检查mappingPath是否合规
                            if (!RequestUtil.checkMappingPath(mappingPath)) {
                                throw new RuntimeException("invalid @Request.value [" + mappingPath + "] of action: " + method);
                            }
                            //检查actionMethod是否合规
                            try {
                                RequestUtil.checkActionMethod(method);
                            } catch (Exception e) {
                                throw new RuntimeException("invalid Controler method : " + e.getMessage());
                            }
                            //格式化
                            mappingPath = RequestUtil.formatUrl(mappingPath);
                            String[] nodeNames = mappingPath.split("/");
                            String nodeName = "";
                            String[] subNodeNameLineAry = {};
                            if (nodeNames.length > 0) {
                                nodeName = nodeNames[0];
                                subNodeNameLineAry = new String[nodeNames.length - 1];
                                for (int i = 0; i < subNodeNameLineAry.length; i++) {
                                    subNodeNameLineAry[i] = nodeNames[i + 1];
                                }
                            }
                            generateActionMap(nodeName, ACTION_MAP, controllerClass, method, subNodeNameLineAry, requestMethod, mappingPath);
                        }
                    }
                }
            }
        }
    }
}
Also used : Request(org.axe.annotation.mvc.Request) Method(java.lang.reflect.Method) Controller(org.axe.annotation.ioc.Controller)

Example 2 with Controller

use of org.axe.annotation.ioc.Controller in project Axe by DongyuCai.

the class HomeController method controller.

@Request(value = "/controller", method = RequestMethod.GET)
public void controller(@RequestParam("token") String token, HttpServletRequest request, HttpServletResponse response) {
    String contextPath = request.getContextPath();
    StringBuilder html = new StringBuilder();
    html.append("<!DOCTYPE html>");
    html.append("<html>");
    html.append("<head>");
    html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
    html.append("<title>axe controller</title>");
    html.append("</head>");
    html.append("<body>");
    html.append("<table width=\"100%\">");
    html.append("<tr><td align=\"right\">");
    if (ConfigHelper.getAxeSignIn()) {
        html.append("<a style=\"font-size: 15px;color: #AE0000\" href=\"" + contextPath + "/axe/sign-out?token=" + token + "\"><b>退出</b></a>");
    }
    html.append("&nbsp;<a style=\"font-size: 15px;color: #AE0000\" href=\"" + contextPath + "/axe?token=" + token + "\"><b>首页</b></a>");
    html.append("</td></tr>");
    Set<Class<?>> controllerCLassSet = ClassHelper.getControllerClassSet();
    html.append("<tr><td align=\"center\"><font size=\"28\">Controller set x" + controllerCLassSet.size() + "</font></td></tr>");
    html.append("");
    html.append("<tr><td><table cellspacing=\"0px\"><tr><td style=\"background-color: #AE0000\">");
    html.append("&nbsp;<font color=\"white\"><b>Controller</b></font>&nbsp;");
    html.append("</td></tr></table></td></tr>");
    html.append("");
    html.append("<tr><td height=\"2px\" style=\"background-color: #AE0000\"></td></tr>");
    html.append("<tr><td>");
    html.append("<table width=\"100%\">");
    html.append("<tr style=\"background-color: #F0F0F0;\">");
    html.append("<td align=\"left\">&nbsp;</td>");
    html.append("<td align=\"left\"><b>Title</b></td>");
    html.append("<td align=\"left\"><b>BasePath</b></td>");
    html.append("<td align=\"left\"><b>Class</b></td>");
    html.append("<td align=\"left\"><b>Action</b></td>");
    html.append("</tr>");
    Map<String, List<Class<?>>> controllerClassMap = new HashMap<>();
    for (Class<?> controllerClass : controllerCLassSet) {
        Controller controller = controllerClass.getAnnotation(Controller.class);
        String basePath = controller.basePath();
        List<Class<?>> controllerClassList = new ArrayList<>();
        if (controllerClassMap.containsKey(basePath)) {
            controllerClassList = controllerClassMap.get(basePath);
        } else {
            controllerClassMap.put(basePath, controllerClassList);
        }
        controllerClassList.add(controllerClass);
    }
    List<String> basePathList = StringUtil.sortStringSet(controllerClassMap.keySet());
    int id = 1;
    for (String basePath : basePathList) {
        List<Class<?>> controllerClassList = controllerClassMap.get(basePath);
        for (Class<?> controllerClass : controllerClassList) {
            int actionCount = 0;
            Method[] methodAry = controllerClass.getDeclaredMethods();
            for (Method method : methodAry) {
                if (method.isAnnotationPresent(Request.class)) {
                    actionCount++;
                }
            }
            Controller controller = controllerClass.getAnnotation(Controller.class);
            html.append("<tr>");
            html.append("<td align=\"left\">" + (id++) + "</td>");
            html.append("<td align=\"left\">" + controller.title() + "</td>");
            html.append("<td align=\"left\">" + basePath + "</td>");
            html.append("<td align=\"left\">" + controllerClass.getName() + "</td>");
            String basePathHashCode = null;
            int code = basePath.hashCode();
            if (code < 0) {
                basePathHashCode = "_" + Math.abs(code);
            } else {
                basePathHashCode = String.valueOf(code);
            }
            html.append("<td align=\"left\">x<a href=\"" + contextPath + "/axe/controller-" + basePathHashCode + "/action?token=" + token + "\">" + actionCount + "</a></td>");
            html.append("</tr>");
        }
    }
    html.append("</table>");
    html.append("</td></tr>");
    html.append("</table>");
    html.append("</body>");
    html.append("</html>");
    printHtml(response, html.toString());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) RequestMethod(org.axe.constant.RequestMethod) Controller(org.axe.annotation.ioc.Controller) ArrayList(java.util.ArrayList) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.axe.annotation.mvc.Request)

Example 3 with Controller

use of org.axe.annotation.ioc.Controller in project Axe by DongyuCai.

the class HomeController method action.

@Request(value = "/action", method = RequestMethod.GET)
public void action(@RequestParam("token") String token, HttpServletRequest request, HttpServletResponse response, String basePathHashCode) {
    String contextPath = request.getContextPath();
    StringBuilder html = new StringBuilder();
    html.append("<!DOCTYPE html>");
    html.append("<html>");
    html.append("<head>");
    html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
    html.append("<title>axe action</title>");
    html.append("</head>");
    html.append("<body>");
    html.append("<table width=\"100%\">");
    html.append("<tr><td align=\"right\">");
    if (ConfigHelper.getAxeSignIn()) {
        html.append("<a style=\"font-size: 15px;color: #AE0000\" href=\"" + contextPath + "/axe/sign-out?token=" + token + "\"><b>退出</b></a>");
    }
    html.append("&nbsp;<a style=\"font-size: 15px;color: #AE0000\" href=\"" + contextPath + "/axe?token=" + token + "\"><b>首页</b></a>");
    html.append("</td></tr>");
    List<Handler> handlerList = ControllerHelper.getActionList();
    String basePath = "";
    if (basePathHashCode != null) {
        Map<Class<?>, String> hashCodeMap = new HashMap<>();
        List<Handler> controllerHandlerList = new ArrayList<>();
        for (Handler handler : handlerList) {
            Class<?> controller = handler.getControllerClass();
            String hashCode = null;
            if (hashCodeMap.containsKey(controller)) {
                hashCode = hashCodeMap.get(controller);
            } else {
                basePath = controller.getAnnotation(Controller.class).basePath();
                int code = basePath.hashCode();
                if (code < 0) {
                    hashCode = "_" + Math.abs(code);
                } else {
                    hashCode = String.valueOf(code);
                }
                hashCodeMap.put(controller, hashCode);
            }
            if (hashCode.equals(basePathHashCode)) {
                controllerHandlerList.add(handler);
            }
        }
        handlerList = controllerHandlerList;
    }
    basePath = StringUtil.isEmpty(basePath) ? "" : basePath + " - ";
    html.append("<tr><td align=\"center\"><font size=\"28\">" + basePath + "Action list x" + handlerList.size() + "</font></td></tr>");
    html.append("");
    html.append("<tr><td><table cellspacing=\"0px\"><tr><td style=\"background-color: #AE0000\">");
    html.append("&nbsp;<font color=\"white\"><b>Action</b></font>&nbsp;");
    html.append("</td></tr></table></td></tr>");
    html.append("");
    html.append("<tr><td height=\"2px\" style=\"background-color: #AE0000\"></td></tr>");
    html.append("<tr><td>");
    html.append("<table width=\"100%\">");
    html.append("<tr style=\"background-color: #F0F0F0;\">");
    html.append("<td align=\"left\">&nbsp;</td>");
    html.append("<td align=\"left\"><b>Title</b></td>");
    html.append("<td align=\"left\"><b>Mapping</b></td>");
    html.append("<td align=\"left\"><b>Request-Method</b></td>");
    html.append("<td align=\"left\"><b>Controller-Class</b></td>");
    html.append("<td align=\"left\"><b>Action-Method</b></td>");
    html.append("<td align=\"left\"><b>Filter</b></td>");
    html.append("</tr>");
    Map<String, List<Handler>> handlerMap = new HashMap<>();
    for (Handler handler : handlerList) {
        String mappingPath = handler.getMappingPath();
        List<Handler> action = new ArrayList<>();
        if (handlerMap.containsKey(mappingPath)) {
            action = handlerMap.get(mappingPath);
        } else {
            handlerMap.put(mappingPath, action);
        }
        action.add(handler);
    }
    List<String> mappingPathList = StringUtil.sortStringSet(handlerMap.keySet());
    int id = 1;
    for (String mappingPath : mappingPathList) {
        List<Handler> action = handlerMap.get(mappingPath);
        for (Handler handler : action) {
            Controller controller = handler.getControllerClass().getAnnotation(Controller.class);
            int code = controller.basePath().hashCode();
            String hashCode = null;
            if (code < 0) {
                hashCode = "_" + Math.abs(code);
            } else {
                hashCode = String.valueOf(code);
            }
            html.append("<tr>");
            html.append("<td align=\"left\">" + (id++) + "</td>");
            html.append("<td align=\"left\">" + controller.title() + "." + handler.getActionMethod().getAnnotation(Request.class).title() + "</td>");
            html.append("<td align=\"left\">" + mappingPath + "</td>");
            html.append("<td align=\"left\">" + handler.getRequestMethod() + "</td>");
            html.append("<td align=\"left\"><a href=\"" + contextPath + "/axe/controller-" + hashCode + "/action?token=" + token + "\">" + handler.getControllerClass().getName() + "</a></td>");
            hashCode = null;
            code = mappingPath.hashCode();
            if (code < 0) {
                hashCode = "_" + Math.abs(code);
            } else {
                hashCode = String.valueOf(code);
            }
            html.append("<td align=\"left\"><a href=\"" + contextPath + "/axe/action/" + hashCode + "?token=" + token + "\">" + handler.getActionMethod().getName() + "</a></td>");
            html.append("<td align=\"left\">x" + handler.getFilterList().size() + "</td>");
            html.append("</tr>");
        }
    }
    html.append("</table>");
    html.append("</td></tr>");
    html.append("</table>");
    html.append("</body>");
    html.append("</html>");
    printHtml(response, html.toString());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Handler(org.axe.bean.mvc.Handler) Controller(org.axe.annotation.ioc.Controller) ArrayList(java.util.ArrayList) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.axe.annotation.mvc.Request)

Example 4 with Controller

use of org.axe.annotation.ioc.Controller in project Axe by DongyuCai.

the class HomeController method actionDetail.

@Request(value = "/action/{mappingHashCode}", method = RequestMethod.GET)
public void actionDetail(@RequestParam("mappingHashCode") String mappingHashCode, @RequestParam("token") String token, HttpServletRequest request, HttpServletResponse response) {
    String contextPath = request.getContextPath();
    do {
        if (StringUtil.isEmpty(mappingHashCode))
            break;
        StringBuilder html = new StringBuilder();
        html.append("<!DOCTYPE html>");
        html.append("<html>");
        html.append("<head>");
        html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
        html.append("<title>axe action detail</title>");
        html.append("</head>");
        html.append("<body>");
        html.append("<table width=\"100%\">");
        html.append("<tr><td align=\"right\">");
        if (ConfigHelper.getAxeSignIn()) {
            html.append("<a style=\"font-size: 15px;color: #AE0000\" href=\"" + contextPath + "/axe/sign-out?token=" + token + "\"><b>退出</b></a>");
        }
        html.append("&nbsp;<a style=\"font-size: 15px;color: #AE0000\" href=\"" + contextPath + "/axe?token=" + token + "\"><b>首页</b></a>");
        html.append("</td></tr>");
        List<Handler> handlerList = ControllerHelper.getActionList();
        Handler handler = null;
        for (Handler handler_ : handlerList) {
            String hashCode = null;
            int code = handler_.getMappingPath().hashCode();
            if (code < 0) {
                hashCode = "_" + Math.abs(code);
            } else {
                hashCode = String.valueOf(code);
            }
            if (hashCode.equals(mappingHashCode)) {
                handler = handler_;
                break;
            }
        }
        if (handler == null)
            break;
        String basePath = handler.getControllerClass().getAnnotation(Controller.class).basePath();
        html.append("<tr><td align=\"center\"><font size=\"28\">Action Detail - " + handler.getMappingPath() + "</font></td></tr>");
        html.append("");
        html.append("<tr><td><table cellspacing=\"0px\"><tr><td style=\"background-color: #AE0000\">");
        html.append("&nbsp;<font color=\"white\"><b>Action Detail</b></font>&nbsp;");
        html.append("</td></tr></table></td></tr>");
        html.append("<tr><td height=\"2px\" style=\"background-color: #AE0000\"></td></tr>");
        html.append("<tr><td>");
        html.append("<table width=\"100%\">");
        html.append("<tr style=\"background-color: #F0F0F0;\">");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\"><b>属性</b></td>");
        html.append("<td align=\"left\"><b>值</b></td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\">action-title</td>");
        html.append("<td align=\"left\">" + handler.getActionMethod().getAnnotation(Request.class).title() + "</td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\">mapping</td>");
        html.append("<td align=\"left\">" + handler.getMappingPath() + "</td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\">request-method</td>");
        html.append("<td align=\"left\">" + handler.getRequestMethod() + "</td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\">content-type</td>");
        html.append("<td align=\"left\">" + handler.getContentType() + "</td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\">character-encoding</td>");
        html.append("<td align=\"left\">" + handler.getCharacterEncoding() + "</td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\">action-method</td>");
        html.append("<td align=\"left\">" + handler.getActionMethod().toString() + "</td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\">controller-title</td>");
        html.append("<td align=\"left\">" + handler.getControllerClass().getAnnotation(Controller.class).title() + "</td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\">basePath</td>");
        html.append("<td align=\"left\">" + basePath + "</td>");
        html.append("</tr>");
        html.append("<tr>");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\">action-controller</td>");
        html.append("<td align=\"left\">" + handler.getControllerClass().getName() + "</td>");
        html.append("</tr>");
        html.append("</table>");
        html.append("</td></tr><tr><td>&nbsp;</td></tr>");
        html.append("");
        html.append("");
        html.append("<tr><td><table cellspacing=\"0px\"><tr><td style=\"background-color: #AE0000\">");
        html.append("&nbsp;<font color=\"white\"><b>Filter list</b></font>&nbsp;");
        html.append("</td></tr></table></td></tr>");
        html.append("<tr><td height=\"2px\" style=\"background-color: #AE0000\"></td></tr>");
        html.append("<tr><td>");
        html.append("<table width=\"100%\">");
        html.append("<tr style=\"background-color: #F0F0F0;\">");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\"><b>Level</b></td>");
        html.append("<td align=\"left\"><b>Class</b></td>");
        html.append("<td align=\"left\"><b>Mapping</b></td>");
        html.append("<td align=\"left\"><b>NotMapping</b></td>");
        html.append("</tr>");
        List<Filter> filterList = handler.getFilterList();
        int id = 1;
        for (Filter filter : filterList) {
            html.append("<tr>");
            html.append("<td align=\"left\">" + (id++) + "</td>");
            html.append("<td align=\"left\">" + filter.setLevel() + "</td>");
            html.append("<td align=\"left\">" + filter.getClass() + "</td>");
            Pattern mappingPattern = filter.setMapping();
            String mappingRegex = mappingPattern == null ? "" : mappingPattern.toString();
            html.append("<td align=\"left\">" + mappingRegex + "</td>");
            Pattern notMappingPattern = filter.setNotMapping();
            String notMappingRegex = notMappingPattern == null ? "" : notMappingPattern.toString();
            html.append("<td align=\"left\">" + notMappingRegex + "</td>");
            html.append("</tr>");
        }
        html.append("</table>");
        html.append("</td></tr><tr><td>&nbsp;</td></tr>");
        html.append("");
        html.append("");
        html.append("<tr><td><table cellspacing=\"0px\"><tr><td style=\"background-color: #AE0000\">");
        html.append("&nbsp;<font color=\"white\"><b>Interceptor list</b></font>&nbsp;");
        html.append("</td></tr></table></td></tr>");
        html.append("<tr><td height=\"2px\" style=\"background-color: #AE0000\"></td></tr>");
        html.append("<tr><td>");
        html.append("<table width=\"100%\">");
        html.append("<tr style=\"background-color: #F0F0F0;\">");
        html.append("<td align=\"left\">&nbsp;</td>");
        html.append("<td align=\"left\"><b>Class</b></td>");
        html.append("</tr>");
        List<org.axe.interface_.mvc.Interceptor> interceptorList = handler.getInterceptorList();
        id = 1;
        for (org.axe.interface_.mvc.Interceptor interceptor : interceptorList) {
            html.append("<tr>");
            html.append("<td align=\"left\">" + (id++) + "</td>");
            html.append("<td align=\"left\">" + interceptor.getClass() + "</td>");
            html.append("</tr>");
        }
        html.append("</table>");
        html.append("</td></tr><tr><td>&nbsp;</td></tr>");
        html.append("</table>");
        html.append("</body>");
        html.append("</html>");
        printHtml(response, html.toString());
    } while (false);
}
Also used : Pattern(java.util.regex.Pattern) Handler(org.axe.bean.mvc.Handler) Controller(org.axe.annotation.ioc.Controller) Filter(org.axe.interface_.mvc.Filter) Interceptor(org.axe.annotation.mvc.Interceptor) SignInInterceptor(org.axe.home.interceptor.SignInInterceptor) HomeInterceptor(org.axe.home.interceptor.HomeInterceptor) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.axe.annotation.mvc.Request)

Aggregations

Controller (org.axe.annotation.ioc.Controller)4 Request (org.axe.annotation.mvc.Request)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Handler (org.axe.bean.mvc.Handler)2 Pattern (java.util.regex.Pattern)1 Interceptor (org.axe.annotation.mvc.Interceptor)1 RequestMethod (org.axe.constant.RequestMethod)1 HomeInterceptor (org.axe.home.interceptor.HomeInterceptor)1 SignInInterceptor (org.axe.home.interceptor.SignInInterceptor)1 Filter (org.axe.interface_.mvc.Filter)1