Search in sources :

Example 66 with HongsException

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

the class SpreadInvoker method invoke.

@Override
public void invoke(ActionHelper helper, ActionRunner chains, Annotation anno) throws HongsException {
    Spread ann = (Spread) anno;
    String conf = ann.conf();
    String form = ann.form();
    byte mode = ann.mode();
    if (mode == -1) {
        Set ab = Synt.toTerms(helper.getRequestData().get(Cnst.AB_KEY));
        if (ab != null) {
            if (ab.contains("_fork")) {
                mode = 1;
            }
        }
    }
    // 执行动作
    chains.doAction();
    Map rsp = helper.getResponseData();
    if (01 != mode) {
        return;
    }
    // 识别路径
    if (form.length() == 0) {
        form = chains.getEntity();
    }
    if (conf.length() == 0) {
        conf = chains.getModule();
        // 照顾 Module Action 的配置规则
        if (FormSet.hasConfFile(conf + "/" + form)) {
            conf = conf + "/" + form;
        }
    }
    // 填充数据
    try {
        Map data = (Map) helper.getAttribute("form:" + conf + "." + form);
        if (data == null) {
            data = FormSet.getInstance(conf).getForm(form);
        }
        SpreadHelper sup;
        sup = new SpreadHelper().addItemsByForm(conf, data);
        sup.spread(rsp);
    } catch (HongsException ex) {
        int ec = ex.getErrno();
        if (ec != 0x10e8 && ec != 0x10e9 && ec != 0x10ea) {
            throw ex;
        }
    }
    // 返回数据
    helper.reply(rsp);
}
Also used : Set(java.util.Set) FormSet(app.hongs.action.FormSet) HongsException(app.hongs.HongsException) SpreadHelper(app.hongs.action.SpreadHelper) Map(java.util.Map)

Example 67 with HongsException

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

the class VerifyInvoker method invoke.

@Override
public void invoke(ActionHelper helper, ActionRunner chains, Annotation anno) throws HongsException {
    Verify ann = (Verify) anno;
    String conf = ann.conf();
    String form = ann.form();
    byte mode = ann.mode();
    byte type = ann.type();
    byte trim = ann.trim();
    // 准备数据
    Map<String, Object> dat = helper.getRequestData();
    Object id = dat.get(Cnst.ID_KEY);
    String at = chains.getAction();
    if (mode == -1) {
        Set ab = Synt.toTerms(helper.getRequestData().get(Cnst.AB_KEY));
        if (ab != null) {
            if (ab.contains(".errs")) {
                mode = 1;
            } else if (ab.contains("!errs")) {
                mode = 2;
            }
        }
    }
    if (type == -1) {
        Boolean up = Synt.asBool(helper.getAttribute(Cnst.UPDATE_MODE));
        if (up == null) {
            type = at.endsWith("/update") || (null != id && !"".equals(id)) ? (byte) 1 : (byte) 0;
        } else {
            type = up ? (byte) 1 : (byte) 0;
        }
    }
    boolean prp = mode <= 0;
    boolean upd = type == 1;
    boolean cln = trim == 1;
    // 识别路径
    if (form.length() == 0) {
        form = chains.getEntity();
    }
    if (conf.length() == 0) {
        conf = chains.getModule();
        // 照顾 Module Action 的配置规则
        if (FormSet.hasConfFile(conf + "/" + form)) {
            conf = conf + "/" + form;
        }
    }
    // 执行校验
    try {
        Map data = (Map) helper.getAttribute("form:" + conf + "." + form);
        if (data == null) {
            data = FormSet.getInstance(conf).getForm(form);
        }
        VerifyHelper ver = new VerifyHelper();
        ver.addRulesByForm(conf, form, data);
        ver.isPrompt(prp);
        ver.isUpdate(upd);
        Map vls = ver.verify(dat);
        if (cln)
            dat.clear();
        dat.putAll(vls);
    } catch (Wrongs err) {
        dat = err.toReply(prp ? 0 : mode);
        helper.reply(dat);
        // Servlet 环境下设置状态码为 400 (错误的请求)
        if (helper.getResponse() != null) {
            helper.getResponse().setStatus(SC_BAD_REQUEST);
        }
        return;
    } catch (HongsException ex) {
        int ec = ex.getErrno();
        if (ec != 0x10e8 && ec != 0x10e9 && ec != 0x10ea) {
            throw ex;
        }
    }
    chains.doAction();
}
Also used : VerifyHelper(app.hongs.action.VerifyHelper) Set(java.util.Set) FormSet(app.hongs.action.FormSet) HongsException(app.hongs.HongsException) Wrongs(app.hongs.util.verify.Wrongs) Map(java.util.Map)

Example 68 with HongsException

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

the class AuthFilter method init.

@Override
public void init(FilterConfig config) throws ServletException {
    super.init(config);
    String s;
    /**
     * 获取登录超时
     */
    this.exp = Synt.declare(config.getInitParameter("expire-time"), 0L);
    /**
     * 获取权限配置名
     */
    s = config.getInitParameter("config-name");
    if (null != s) {
        this.aut = s;
        try {
            this.siteMap = NaviMap.getInstance(s);
        } catch (HongsException ex) {
            throw new ServletException(ex);
        }
    } else {
        try {
            this.siteMap = NaviMap.getInstance();
        } catch (HongsException ex) {
            throw new ServletException(ex);
        }
    }
    /**
     * 获取首页URL
     */
    s = config.getInitParameter("index-page");
    if (s != null) {
        this.indexPage = Core.BASE_HREF + s;
    }
    /**
     * 获取登录URL
     */
    s = config.getInitParameter("login-page");
    if (s != null) {
        this.loginPage = Core.BASE_HREF + s;
    }
    /**
     * 获取不包含的URL
     */
    this.ignore = new ChoiceHelper(config.getInitParameter("ignore-urls"), config.getInitParameter("attend-urls"));
}
Also used : ServletException(javax.servlet.ServletException) HongsException(app.hongs.HongsException) ChoiceHelper(app.hongs.action.ChoiceHelper)

Example 69 with HongsException

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

the class AuthFilter method doFilter.

@Override
public void doFilter(Core core, ActionHelper hlpr, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse rsp = hlpr.getResponse();
    HttpServletRequest req = hlpr.getRequest();
    String act = ActionDriver.getRecentPath(req);
    /**
     * 检查当前动作是否可以忽略
     */
    if (ignore != null && ignore.ignore(act)) {
        chain.doFilter(req, rsp);
        return;
    }
    /**
     * 判断当前用户是否登录超时
     */
    if (exp != 0) {
        long tim = Synt.declare(hlpr.getSessibute(Cnst.STM_SES), 0L);
        long now = System.currentTimeMillis() / 1000;
        if (now - tim < exp) {
            hlpr.setSessibute(Cnst.STM_SES, now);
        } else {
            doFailed(core, hlpr, (byte) 0);
            return;
        }
    }
    /**
     * 调试模式超级管理员无限制
     */
    if (Core.DEBUG > 0) {
        String uid = Synt.declare(hlpr.getSessibute(Cnst.UID_SES), "");
        if (uid != null && uid.equals(Cnst.ADM_UID)) {
            chain.doFilter(req, rsp);
            return;
        }
    }
    // 获取详细会话集合
    Set<String> authset;
    try {
        authset = siteMap.getAuthSet();
    } catch (HongsException e) {
        throw new ServletException(e);
    }
    // 权限动作无前导/
    if (act.startsWith("/")) {
        act = act.substring(1);
    }
    // 附带上协议方法
    String amt = act + "|" + hlpr.getRequest().getMethod();
    if (null == authset) {
        if (null != loginPage) {
            // 没有登录
            doFailed(core, hlpr, (byte) 1);
            return;
        }
        if (siteMap.actions.contains(act)) {
            // 需要权限
            doFailed(core, hlpr, (byte) 3);
            return;
        }
        if (siteMap.actions.contains(amt)) {
            // 需要权限(带方法)
            doFailed(core, hlpr, (byte) 3);
            return;
        }
    } else {
        if (siteMap.actions.contains(act)) {
            if (!authset.contains(act)) {
                // 缺少权限
                doFailed(core, hlpr, (byte) 3);
                return;
            }
        }
        if (siteMap.actions.contains(amt)) {
            if (!authset.contains(amt)) {
                // 缺少权限(带方法)
                doFailed(core, hlpr, (byte) 3);
                return;
            }
        }
        if (siteMap.actions.contains(aut)) {
            if (!authset.contains(aut)) {
                // 禁入区域
                doFailed(core, hlpr, (byte) 2);
                return;
            }
            /**
             * 权限配置中有指定区域的
             * 必须在指定区域再次登录
             * 登录时会将区域写入会话
             */
            if (loginPage != null) {
                Set sae = (Set) hlpr.getSessibute(Cnst.SAE_SES);
                if (sae == null || !sae.contains(aut)) {
                    // 登录区域
                    doFailed(core, hlpr, (byte) 1);
                    return;
                }
            }
        }
    }
    chain.doFilter(req, rsp);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Set(java.util.Set) HongsException(app.hongs.HongsException) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 70 with HongsException

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

the class CmdletRunner method main.

public static void main(String[] args) throws IOException, HongsException {
    args = init(args);
    Core core = Core.getInstance();
    String act = Core.ACTION_NAME.get();
    if (null == act || act.length() == 0) {
        System.err.println("ERROR: Cmdlet name can not be empty.");
        System.exit(2);
        return;
    }
    // 获取方法
    Method method = getCmdlets().get(act);
    if (null == method) {
        System.err.println("ERROR: Cmdlet " + act + " is not exists.");
        System.exit(2);
        return;
    }
    // 执行方法
    try {
        if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
            CmdletHelper.println("Starting...");
        }
        method.invoke(null, new Object[] { args });
        if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
            CmdletHelper.println("Finished!!!");
        }
    } catch (IllegalAccessException ex) {
        CoreLogger.error("Illegal access for method '" + method.getClass().getName() + "." + method.getName() + "(String[]).");
        System.exit(3);
    } catch (IllegalArgumentException ex) {
        CoreLogger.error("Illegal params for method '" + method.getClass().getName() + "." + method.getName() + "(String[]).");
        System.exit(3);
    } catch (InvocationTargetException ex) {
        Throwable ta = ex.getCause();
        if (0 < Core.DEBUG) {
            CoreLogger.error(ta);
            return;
        }
        /**
         * 构建错误消息
         */
        String error = ta.getLocalizedMessage();
        if (!(ta instanceof HongsException) && !(ta instanceof HongsExpedient) && !(ta instanceof HongsError)) {
            CoreLocale lang = Core.getInstance(CoreLocale.class);
            if (error == null || error.length() == 0) {
                error = lang.translate("core.error.unkwn", ta.getClass().getName());
            } else {
                error = lang.translate("core.error.label", ta.getClass().getName()) + ": " + error;
            }
        }
        CoreLogger.error(error);
        System.exit(4);
    } finally {
        try {
            core.close();
        } catch (Throwable er) {
            CoreLogger.error(er);
            System.exit(5);
        }
        /**
         * 输出总的运行时间
         * 并清除参数及核心
         */
        if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
            CmdletHelper.println("Total exec time: " + (Tool.humanTime(System.currentTimeMillis() - Core.STARTS_TIME)));
        }
    }
}
Also used : CoreLocale(app.hongs.CoreLocale) HongsError(app.hongs.HongsError) HongsException(app.hongs.HongsException) HongsExpedient(app.hongs.HongsExpedient) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Core(app.hongs.Core)

Aggregations

HongsException (app.hongs.HongsException)89 Map (java.util.Map)42 HashMap (java.util.HashMap)34 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)15 LinkedHashMap (java.util.LinkedHashMap)15 Set (java.util.Set)15 List (java.util.List)13 File (java.io.File)11 SQLException (java.sql.SQLException)10 FileNotFoundException (java.io.FileNotFoundException)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 PreparedStatement (java.sql.PreparedStatement)8 Iterator (java.util.Iterator)8 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 HongsExpedient (app.hongs.HongsExpedient)7 FormSet (app.hongs.action.FormSet)7 Table (app.hongs.db.Table)7