Search in sources :

Example 31 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class PresetInvoker method invoke.

@Override
public void invoke(ActionHelper helper, ActionRunner chains, Annotation anno) throws HongsException {
    Preset ann = (Preset) anno;
    String conf = ann.conf();
    String form = ann.form();
    String[] deft = ann.deft();
    String[] defs = ann.defs();
    // 默认参数可完全由外部指定
    if (deft == null || deft.length == 0) {
        Map req = helper.getRequestData();
        Set<String> uzed = Synt.toTerms(req.get(Cnst.AB_KEY));
        Set<String> used = new LinkedHashSet();
        if (null != uzed && !uzed.isEmpty()) {
            for (String item : uzed) {
                /*
                    // 2021/04/17
                    // 废弃返回数据模式
                    // 规避内部不确定性
                    // 总是返回原始对象
                    if (item.equals("_str_")) {
                        Core.getInstance().put(Cnst.STRING_MODE, true );
                    } else
                    if (item.equals("_obj_")) {
                        Core.getInstance().put(Cnst.STRING_MODE, false);
                    } else
                    */
                if (item.startsWith("_") == false && !item.startsWith(".") && !item.startsWith(":") && !item.startsWith("!")) {
                    used.add("." + item);
                }
            }
            deft = used.toArray(new String[0]);
        }
    }
    // 识别路径
    if (form.length() == 0) {
        form = chains.getEntity();
    }
    if (conf.length() == 0) {
        conf = chains.getModule();
    // 照顾 Module Action 的配置规则. 2018/7/7 改为完全由外部预判
    // if (FormSet.hasConfFile(conf+"/"+form)) {
    // conf = conf+"/"+form ;
    // }
    }
    // 补充参数
    try {
        Map req;
        PresetHelper pre;
        req = helper.getRequestData();
        pre = new PresetHelper();
        pre.addItemsByForm(conf, form, deft, defs);
        pre.preset(req, helper);
    } catch (HongsException ex) {
        int ec = ex.getErrno();
        if (ec != 910 && ec != 911 && ec != 913) {
            // 非枚举缺失
            throw ex;
        }
    }
    chains.doAction();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HongsException(io.github.ihongs.HongsException) PresetHelper(io.github.ihongs.action.PresetHelper) Map(java.util.Map)

Example 32 with HongsException

use of io.github.ihongs.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();
    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) {
            Object hd = chains.getHandle();
            Object id = dat.get(Cnst.ID_KEY);
            type = /**
             */
            "update".equals(hd) || (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 的配置规则. 2018/7/7 改为完全由外部预判
    // 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);
        Map vls = ver.verify(dat, upd, prp);
        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);
        }
        // 记录可能引起错误的原因
        if (0 != Core.DEBUG) {
            Throwable tr = err.getCause();
            if (null != tr) {
                CoreLogger.error(tr);
            }
        }
        return;
    } catch (HongsException ex) {
        int ec = ex.getErrno();
        if (ec != 910 && ec != 911 && ec != 912) {
            // 非表单缺失
            throw ex;
        }
    }
    chains.doAction();
}
Also used : FormSet(io.github.ihongs.action.FormSet) Set(java.util.Set) VerifyHelper(io.github.ihongs.action.VerifyHelper) HongsException(io.github.ihongs.HongsException) Wrongs(io.github.ihongs.util.verify.Wrongs) Map(java.util.Map)

Example 33 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class VarsFilter method doFilter.

@Override
public void doFilter(Core core, ActionHelper hlpr, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse rsp = hlpr.getResponse();
    HttpServletRequest req = hlpr.getRequest();
    /**
     * 跳过内部动作代理, 如 AutoFilter
     */
    if (null != req.getAttribute(Cnst.ACTION_ATTR)) {
        chain.doFilter(req, rsp);
        return;
    }
    String act = ActionDriver.getRecentPath(req);
    if (null != act && !patter.matches(act)) {
        chain.doFilter(req, rsp);
        return;
    }
    /**
     * 上传文件时可能会发生异常
     */
    Map rd;
    try {
        rd = hlpr.getRequestData();
    } catch (Throwable e) {
        if (e instanceof HongsCause) {
            hlpr.fault((HongsCause) e);
        } else {
            hlpr.fault(e.getMessage());
        }
        return;
    }
    if (rn_limit != 0) {
        int rn = Synt.declare(rd.get(Cnst.RN_KEY), Cnst.RN_DEF);
        if (rn < 1 || rn > rn_limit) {
            rsp.setStatus(400);
            hlpr.fault(Cnst.RN_KEY + " must be 1 to " + rn_limit);
            return;
        }
    }
    if (illegals != null && !illegals.isEmpty()) {
        Set ls = new HashSet(illegals);
        Set ks = rd.keySet();
        ls.retainAll(ks);
        if (!ls.isEmpty()) {
            rsp.setStatus(400);
            hlpr.fault("Illegal parameters: " + Syno.concat(",", ls));
            return;
        }
    }
    if (sr_limit != 0 || sr_level != 0) {
        try {
            srCheck(rd, sr_limit, sr_level, 0, 1);
        } catch (HongsException | HongsExemption ex) {
            hlpr.fault(ex);
            return;
        }
    }
    chain.doFilter(req, rsp);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Set(java.util.Set) HashSet(java.util.HashSet) HongsCause(io.github.ihongs.HongsCause) HongsException(io.github.ihongs.HongsException) HttpServletResponse(javax.servlet.http.HttpServletResponse) HongsExemption(io.github.ihongs.HongsExemption) Map(java.util.Map) HashSet(java.util.HashSet)

Example 34 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class FileAction method update.

@Override
@Action("update")
public void update(ActionHelper helper) throws HongsException {
    CoreLocale lang = CoreLocale.getInstance("manage");
    String path = helper.getParameter("path");
    String dist = helper.getParameter("dist");
    String text = helper.getParameter("text");
    File file;
    File dizt;
    if (dist != null && dist.equals(path)) {
        dist = null;
    }
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.required"));
        return;
    }
    path = realPath(path);
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.is.error"));
        return;
    }
    file = new File(path);
    if (!file.exists()) {
        helper.fault(lang.translate("core.manage.file.path.is.not.exist"));
        return;
    }
    if (isDenyFile(file)) {
        helper.fault(lang.translate("core.manage.file.interdicted"));
        return;
    }
    // 改名移动
    if (dist != null) {
        dist = realPath(dist);
        if (dist == null) {
            helper.fault(lang.translate("core.manage.file.path.is.error"));
            return;
        }
        dizt = new File(dist);
        if (dizt.exists()) {
            helper.fault(lang.translate("core.manage.file.dist.is.exist"));
            return;
        }
        if (isDenyFile(file)) {
            helper.fault(lang.translate("core.manage.file.interdicted"));
            return;
        }
        if (!file.renameTo(dizt)) {
            helper.fault(lang.translate("core.manage.file.rename.failed"));
            return;
        }
        file = dizt;
    }
    // 写入文件
    try {
        saveFile(file, text);
    } catch (Exception ex) {
        CoreLogger.error(ex);
        helper.fault(lang.translate("core.manage.file.update.failed"));
        return;
    }
    helper.reply("");
}
Also used : CoreLocale(io.github.ihongs.CoreLocale) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HongsException(io.github.ihongs.HongsException) Action(io.github.ihongs.action.anno.Action) IAction(io.github.ihongs.dh.IAction)

Example 35 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class DBAction method acting.

@Override
public void acting(ActionHelper helper, ActionRunner runner) throws HongsException {
    String act = runner.getHandle();
    String ent = runner.getEntity();
    String mod = runner.getModule();
    if (ent.startsWith("_") || mod.endsWith("/" + ent)) {
        throw new HongsException(404, "Unsupported Request!");
    }
    Map fs = null;
    do {
        try {
            fs = FormSet.getInstance(mod).getForm(ent);
            break;
        } catch (HongsException ex) {
            if (ex.getErrno() != 910 && ex.getErrno() != 912) {
                // 非表单缺失
                throw ex;
            }
        }
        mod = mod + "/" + ent;
        try {
            fs = FormSet.getInstance(mod).getForm(ent);
            runner.setModule(mod);
        } catch (HongsException ex) {
            if (ex.getErrno() != 910 && ex.getErrno() != 912) {
                // 非表单缺失
                throw ex;
            }
        }
    } while (false);
    if (fs == null) {
        return;
    }
    Set ca = Synt.toSet(Dict.get(fs, null, "@", "callable"));
    if (ca != null && !ca.contains(act)) {
        throw new HongsException(405, "Unsupported Request.");
    }
}
Also used : FormSet(io.github.ihongs.action.FormSet) Set(java.util.Set) HongsException(io.github.ihongs.HongsException) Map(java.util.Map)

Aggregations

HongsException (io.github.ihongs.HongsException)138 Map (java.util.Map)77 HashMap (java.util.HashMap)61 LinkedHashMap (java.util.LinkedHashMap)31 IOException (java.io.IOException)29 Set (java.util.Set)26 HashSet (java.util.HashSet)25 ArrayList (java.util.ArrayList)24 List (java.util.List)20 HongsExemption (io.github.ihongs.HongsExemption)14 Action (io.github.ihongs.action.anno.Action)14 LinkedHashSet (java.util.LinkedHashSet)14 SQLException (java.sql.SQLException)13 FormSet (io.github.ihongs.action.FormSet)12 Table (io.github.ihongs.db.Table)12 FileNotFoundException (java.io.FileNotFoundException)11 CoreConfig (io.github.ihongs.CoreConfig)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 Iterator (java.util.Iterator)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9