Search in sources :

Example 1 with HongsError

use of app.hongs.HongsError in project HongsCORE by ihongs.

the class Dict method splitKeys.

/**
 * 拆分键
 *
 * <pre>
 * 可以将 a.b[c]:d.:e[:f][] 解析为 a b c :d :e :f null
 * 参考自 Javascript 和 PHP 的语法,
 * Javascript 方括号里面用的是变量,
 * 而 PHP 对象键并不支持点符号写法.
 * </pre>
 *
 * @param path
 * @return
 */
public static Object[] splitKeys(String path) {
    if (path == null) {
        throw new NullPointerException("`path` can not be null");
    }
    /**
     * 原代码为
     * <code>
     *  path.replaceAll("([^\\.\\]])!", "$1.!")
     *      .replace("[", ".")
     *      .replace("]", "" )
     *      .split("\\.")
     * </code>
     * 然后遍历将空串换成 null 空值,
     * 无法处理 [] 里面有 .! 的情况;
     * 采用下面的程序可规避此种问题,
     * 且无需正则而仅做一次遍历即可.
     * 2018/3/9 已将 '!' 换为 ':'.
     */
    List lst = new ArrayList();
    int len = path.length();
    int beg = 0;
    int end = 0;
    char pnt;
    boolean fkh = false;
    while (end < len) {
        pnt = path.charAt(end);
        switch(pnt) {
            case ':':
                if (fkh) {
                    // [] 内可以用 :
                    break;
                }
                if (beg != end) {
                    lst.add(path.substring(beg, end));
                    beg = end;
                }
                break;
            case '.':
                if (fkh) {
                    // [] 内可以用 .
                    break;
                }
                if (beg != end) {
                    lst.add(path.substring(beg, end));
                } else if (beg == 0) {
                    lst.add("");
                } else if (']' != path.charAt(beg - 1)) {
                    // 规避 a[b].c. 中的 ].
                    lst.add(null);
                }
                beg = end + 1;
                break;
            case '[':
                if (fkh) {
                    throw new HongsError(0x46, "Syntax error at " + end + " in " + path);
                }
                if (beg != end) {
                    lst.add(path.substring(beg, end));
                } else if (beg == 0) {
                    lst.add("");
                } else if (']' != path.charAt(beg - 1)) {
                    // 规避 a[b][c] 中的 ][
                    throw new HongsError(0x46, "Syntax error at " + end + " in " + path);
                }
                beg = end + 1;
                fkh = true;
                break;
            case ']':
                if (!fkh) {
                    throw new HongsError(0x46, "Syntax error at " + end + " in " + path);
                }
                if (beg != end) {
                    lst.add(path.substring(beg, end));
                } else if (beg != 0) {
                    lst.add(null);
                } else
                    // if ('[' != path.charAt(beg - 1)) { // 这种情况其实并不存在
                    throw new HongsError(0x46, "Syntax error at " + end + " in " + path);
                // }
                beg = end + 1;
                fkh = false;
                break;
        }
        end++;
    }
    if (beg == 0 && end == 0) {
        lst.add(path);
    } else if (beg != len) {
        lst.add(path.substring(beg));
    } else if ('.' == path.charAt(-1 + len)) {
        // 点结尾列表.
        lst.add(null);
    }
    return lst.toArray();
}
Also used : HongsError(app.hongs.HongsError) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with HongsError

use of app.hongs.HongsError in project HongsCORE by ihongs.

the class ActionHelper method getRequestData.

/**
 * 获取请求数据
 *
 * 不同于 request.getParameterMap,
 * 该方法会将带"[]"的拆成子List, 将带"[xxx]"的拆成子Map, 并逐级递归.
 * 也可以解析用"." 连接的参数, 如 a.b.c 与上面的 a[b][c] 是同样效果.
 * 故请务必注意参数中的"."和"[]"符号.
 * 如果Content-Type为"multipart/form-data", 则使用 apache-common-fileupload 先将文件存入临时目录.
 *
 * @return 请求数据
 */
public Map<String, Object> getRequestData() {
    if (this.request != null && this.requestData == null) {
        String ct = this.request.getContentType();
        if (ct == null) {
            ct = "application/x-www-form-urlencode";
        } else {
            ct = ct.split(";", 2)[0];
        }
        try {
            Map ad, rd;
            // 可用属性传递
            ad = this.getRequestAttr();
            if (ct.startsWith("multipart/")) {
                // 处理上传文件
                rd = this.getRequestPart();
            } else if (ct.endsWith("/json")) {
                // 处理JSON数据
                rd = this.getRequestJson();
            } else {
                rd = null;
            }
            requestData = parseParam(request.getParameterMap());
            // 深度整合数据
            if (rd != null) {
                Dict.putAll(requestData, rd);
            }
            if (ad != null) {
                Dict.putAll(requestData, ad);
            }
        } catch (HongsError er) {
            throw new HongsExpedient(0x1100, er);
        } finally {
            // 防止解析故障后再调用又出错
            if (this.requestData == null) {
                this.requestData = new HashMap();
            }
        }
    }
    return this.requestData;
}
Also used : HongsError(app.hongs.HongsError) HashMap(java.util.HashMap) HongsExpedient(app.hongs.HongsExpedient) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with HongsError

use of app.hongs.HongsError in project HongsCORE by ihongs.

the class ActsAction method service.

/**
 * 服务方法
 * Servlet Mapping: *.act<br/>
 * 注意: 不支持请求URI的路径中含有"."(句点), 且必须区分大小写;
 * 其目的是为了防止产生多种形式的请求路径, 影响动作过滤, 产生安全隐患.
 *
 * @param req
 * @param rsp
 * @throws javax.servlet.ServletException
 */
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException {
    String act = ActionDriver.getRecentPath(req);
    Core core = ActionDriver.getActualCore(req);
    ActionHelper helper = core.get(ActionHelper.class);
    Core.THREAD_CORE.set(core);
    if (act == null || act.length() == 0) {
        senderr(helper, 0x1104, null, "Action URI can not be empty.", "");
        return;
    }
    // 去掉根和扩展名
    act = act.substring(1);
    int pos = act.lastIndexOf('.');
    if (pos != -1)
        act = act.substring(0, pos);
    // 获取并执行动作
    try {
        ActionRunner runner = new ActionRunner(helper, act);
        runner.doAction();
    } catch (ClassCastException ex) {
        // 类型转换失败按 400 错误处理
        senderr(helper, new HongsException(0x1100, ex));
    } catch (HongsException ex) {
        senderr(helper, ex);
    } catch (HongsExpedient ex) {
        senderr(helper, ex);
    } catch (HongsError ex) {
        senderr(helper, ex);
    }
}
Also used : ActionRunner(app.hongs.action.ActionRunner) HongsError(app.hongs.HongsError) HongsException(app.hongs.HongsException) ActionHelper(app.hongs.action.ActionHelper) HongsExpedient(app.hongs.HongsExpedient) Core(app.hongs.Core)

Example 4 with HongsError

use of app.hongs.HongsError in project HongsCORE by ihongs.

the class AuthAction method service.

/**
 * 服务方法
 * 判断配置和消息有没有生成, 如果没有则生成; 消息按客户语言存放
 * @param req
 * @param rsp
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    // 受是否登录、不同用户等影响, 权限经常变化,必须禁止缓存
    rsp.setHeader("Expires", "0");
    rsp.addHeader("Pragma", "no-cache");
    rsp.setHeader("Cache-Control", "no-cache");
    Core core = ActionDriver.getActualCore(req);
    ActionHelper helper = core.get(ActionHelper.class);
    String name = req.getPathInfo();
    if (name == null || name.length() == 0) {
        helper.error400("Path info required");
        return;
    }
    int p = name.lastIndexOf('.');
    if (p < 0) {
        helper.error400("File type required");
        return;
    }
    String type = name.substring(1 + p);
    name = name.substring(1, p);
    if (!"js".equals(type) && !"json".equals(type)) {
        helper.error400("Wrong file type: " + type);
        return;
    }
    String s;
    try {
        NaviMap sitemap = NaviMap.getInstance(name);
        Set<String> authset = sitemap.getAuthSet();
        // 没有设置 rsname 的不公开
        if (null == sitemap.session) {
            helper.error404("Auth data for '" + name + "' is not open to the public");
            return;
        }
        Map<String, Boolean> datamap = new HashMap();
        if (null == authset)
            authset = new HashSet();
        for (String act : sitemap.actions) {
            datamap.put(act, authset.contains(act));
        }
        s = Data.toString(datamap);
    } catch (HongsException | HongsExpedient | HongsError ex) {
        if (ex.getErrno() == 0x10e0) {
            helper.error404(ex.getMessage());
        } else {
            helper.error500(ex.getMessage());
        }
        return;
    }
    // 输出权限信息
    if ("json".equals(type)) {
        helper.print(s, "application/json");
    } else {
        String c = req.getParameter("callback");
        if (c != null && c.length() != 0) {
            if (!c.matches("^[a-zA-Z_\\$][a-zA-Z0-9_]*$")) {
                helper.error400("Illegal callback function name!");
                return;
            }
            helper.print("function " + c + "() { return " + s + "; }", "text/javascript");
        } else {
            helper.print("if(!self.HsAUTH)self.HsAUTH={};Object.assign(self.HsAUTH," + s + ");", "text/javascript");
        }
    }
}
Also used : HongsError(app.hongs.HongsError) HashMap(java.util.HashMap) HongsExpedient(app.hongs.HongsExpedient) NaviMap(app.hongs.action.NaviMap) HongsException(app.hongs.HongsException) ActionHelper(app.hongs.action.ActionHelper) Core(app.hongs.Core) HashSet(java.util.HashSet)

Example 5 with HongsError

use of app.hongs.HongsError in project HongsCORE by ihongs.

the class AutoFilter method getlays.

private Set<String> getlays() {
    if (null != layset) {
        return layset;
    }
    File dir = new File(Core.BASE_PATH + layout);
    if (!dir.exists()) {
        throw new HongsError(0x3f, "Auto layout '" + layout.substring(1) + "' is not exists");
    }
    if (!dir.isDirectory()) {
        throw new HongsError(0x3f, "Auto layout '" + layout.substring(1) + "' is not a directory");
    }
    /**
     * 这里不需要管层级的深度
     * 下面是按越深越先加入的
     */
    layset = new LinkedHashSet();
    // cstmap = new LinkedHashMap();
    // cxtmap = new LinkedHashMap();
    // 递归获取目录下所有文件
    getlays(layset, dir, "/");
    return layset;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HongsError(app.hongs.HongsError) File(java.io.File)

Aggregations

HongsError (app.hongs.HongsError)12 Core (app.hongs.Core)5 HongsExpedient (app.hongs.HongsExpedient)4 ActionHelper (app.hongs.action.ActionHelper)4 HongsException (app.hongs.HongsException)3 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Method (java.lang.reflect.Method)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 List (java.util.List)2 CoreLocale (app.hongs.CoreLocale)1 ActionRunner (app.hongs.action.ActionRunner)1 NaviMap (app.hongs.action.NaviMap)1 Action (app.hongs.action.anno.Action)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1