Search in sources :

Example 1 with Action

use of com.duangframework.mvc.core.Action in project duangframework by tcrct.

the class RouteHelper method duang.

public static void duang() {
    Map<Class<?>, Object> controllerMap = BeanUtils.getAllBeanMaps().get(CONTROLLER_ENDWITH_NAME);
    if (ToolsKit.isEmpty(controllerMap)) {
        throw new EmptyNullException("mvc controller is null");
    }
    Set<String> excludedMethodName = ObjectKit.buildExcludedMethodName();
    Method[] baseControllerMethods = BaseController.class.getMethods();
    for (Method method : baseControllerMethods) {
        excludedMethodName.add(method.getName());
    }
    // 遍历所有Controller对象,取出Mapping注解,生成路由集合
    for (Iterator<Map.Entry<Class<?>, Object>> it = controllerMap.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<Class<?>, Object> entry = it.next();
        Class<?> controllerClass = entry.getKey();
        Mapping controllerMapping = controllerClass.getAnnotation(Mapping.class);
        // String controllerKey = ToolsKit.isEmpty(controllerMapping) ? buildMappingKey(controllerClass.getSimpleName()) : controllerMapping.value().toLowerCase();
        String controllerKey = buildMappingKey(controllerMapping, controllerClass.getSimpleName());
        for (Method method : controllerClass.getMethods()) {
            Mapping methodMapping = method.getAnnotation(Mapping.class);
            String methodName = method.getName();
            // Object类公用 方法名并且没有参数的方法
            if (!excludedMethodName.contains(methodName) && method.getParameterTypes().length == 0) {
                Action action = buildAction(controllerClass, controllerKey, methodMapping, method);
                String actionKey = action.getActionKey();
                if (actionKey.contains("{") && actionKey.contains("}")) {
                    action.setRestfulKey(actionKey);
                    restfulActionMapping.put(actionKey, action);
                }
                if (!ToolsKit.isExist(actionKey, actionMapping)) {
                    actionMapping.put(actionKey, action);
                }
            }
        }
    }
    List<String> keyList = getAllActionKeys();
    logger.warn("**************** Controller Mapper Key ****************");
    for (String key : keyList) {
        logger.warn(key);
    }
    if (!actionMapping.isEmpty()) {
        InstanceFactory.setActionMapping(actionMapping);
    }
    if (!restfulActionMapping.isEmpty()) {
        InstanceFactory.setRestfulActionMapping(restfulActionMapping);
    }
    logger.warn("RouteHelper Success...");
}
Also used : Action(com.duangframework.mvc.core.Action) Mapping(com.duangframework.core.annotation.mvc.Mapping) Method(java.lang.reflect.Method) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 2 with Action

use of com.duangframework.mvc.core.Action in project duangframework by tcrct.

the class ActionHandle method execute.

@Override
public void execute(String target, IRequest request, IResponse response) throws Exception {
    // 请求的URL中如果包含有.  则全部当作是静态文件的请求处理,直接返回
    if (target.contains(".")) {
        return;
    }
    // 分号后的字符截断
    if (target.contains(";")) {
        target = target.substring(0, target.indexOf(";"));
    }
    // 暂不支持根目录请求
    if ("/".equals(target)) {
        return;
    }
    Action action = InstanceFactory.getActionMapping().get(target);
    if (null == action) {
        action = getRestfulActionMapping(request, target);
        if (null == action) {
            throw new DuangMvcException("action is null or access denied");
        }
    }
    Class<?> controllerClass = action.getControllerClass();
    BaseController controller = null;
    // 是否单例
    if (action.isSingleton()) {
        controller = (BaseController) BeanUtils.getBean(action.getControllerClass(), controllerClass);
    } else {
        // 如果不是设置为单例模式的话就每次请求都创建一个新的Controller对象
        controller = ClassUtils.newInstance(controllerClass);
        // 还要重新执行Ioc注入
        IocHelper.ioc(controller.getClass());
    }
    // 传入request, response到Controller
    controller.init(request, response);
    // 取出方法对象
    Method method = action.getMethod();
    // 取消类型安全检测(可提高反射性能)
    method.setAccessible(true);
    // 反射执行方法
    method.invoke(controller, NULL_ARGS);
    // 返回结果
    controller.getRender().setContext(request, response).render();
}
Also used : Action(com.duangframework.mvc.core.Action) BaseController(com.duangframework.mvc.core.BaseController) Method(java.lang.reflect.Method) DuangMvcException(com.duangframework.core.exceptions.DuangMvcException)

Example 3 with Action

use of com.duangframework.mvc.core.Action in project duangframework by tcrct.

the class ActionHandle method getRestfulActionMapping.

/**
 * 根据restful风格URI,取出对应的Action
 * @param request          请求对象
 * @param target            请求URI
 * @return
 */
private Action getRestfulActionMapping(IRequest request, String target) {
    Map<String, String> paramMap = new HashMap<>();
    Action action = null;
    for (Iterator<Map.Entry<String, Action>> iterator = InstanceFactory.getRestfulActionMapping().entrySet().iterator(); iterator.hasNext(); ) {
        int index = 0;
        paramMap.clear();
        Map.Entry<String, Action> entry = iterator.next();
        String key = entry.getKey();
        // action对象里的URI根据/分裂成数组
        String[] actionKeyArray = key.split("\\/");
        // 请求URI根据/分裂成数组
        String[] targetKeyArray = target.split("\\/");
        // 长度不等则直接退出本次遍历
        if (actionKeyArray.length != targetKeyArray.length) {
            continue;
        }
        int actionKeyLen = actionKeyArray.length;
        for (int i = 0; i < actionKeyLen; i++) {
            if (actionKeyArray[i].equals(targetKeyArray[i])) {
                index++;
            } else if (actionKeyArray[i].startsWith("{") && actionKeyArray[i].endsWith("}")) {
                // 去掉{}后,得出请求name
                String paramName = actionKeyArray[i].substring(1, actionKeyArray[i].length() - 1);
                // 设置到Map里
                paramMap.put(paramName, targetKeyArray[i]);
                index++;
            }
        }
        // 如果匹配的长度一致,则设置这个target对应的Action对象并退出循环
        if (index == actionKeyLen) {
            action = entry.getValue();
            break;
        }
    }
    // 设置到request里
    if (ToolsKit.isNotEmpty(action) && !paramMap.isEmpty()) {
        for (Iterator<Map.Entry<String, String>> iterator = paramMap.entrySet().iterator(); iterator.hasNext(); ) {
            Map.Entry<String, String> entry = iterator.next();
            request.setAttribute(entry.getKey(), entry.getValue());
        }
    }
    return action;
}
Also used : Action(com.duangframework.mvc.core.Action) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Action (com.duangframework.mvc.core.Action)3 Method (java.lang.reflect.Method)2 Mapping (com.duangframework.core.annotation.mvc.Mapping)1 DuangMvcException (com.duangframework.core.exceptions.DuangMvcException)1 EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)1 BaseController (com.duangframework.mvc.core.BaseController)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1