Search in sources :

Example 6 with HongsExpedient

use of app.hongs.HongsExpedient 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 7 with HongsExpedient

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

the class Capts method captcha.

public static Capts captcha(int h, String b, String f, String e) {
    if (h < 24 || h > 96) {
        throw new HongsExpedient(0x1100, "h must be 24~96 (px)");
    }
    // 获取配置
    CoreConfig cc = CoreConfig.getInstance();
    String ff = cc.getProperty("core.capts.font.file", "!Capts.ttf");
    String cs = cc.getProperty("core.capts.code.dict", "1234567890");
    int cn = cc.getProperty("core.capts.code.count", 4);
    int mn = cc.getProperty("core.capts.mask.count", 8);
    float sr = cc.getProperty("core.capts.size.ratio", 0.40f);
    float fr = cc.getProperty("core.capts.font.ratio", 0.80f);
    float mr = cc.getProperty("core.capts.mend.ratio", 0.10f);
    float xr = cc.getProperty("core.capts.mask.ratio", 0.05f);
    int w = (int) ((float) h * sr * (cn + 1));
    char[] cd = cs.toCharArray();
    Color bc = "".equals(b) ? new Color(0xffffff) : new Color(Integer.parseInt(b, 16));
    Color fc = "".equals(f) ? new Color(0x000000) : new Color(Integer.parseInt(f, 16));
    // 构建实例
    Capts vc = new Capts();
    vc.setSize(w, h);
    vc.setCodeCount(cn);
    vc.setMaskCount(mn);
    vc.setFontRatio(fr);
    vc.setMendRatio(mr);
    vc.setMaskRatio(xr);
    vc.setBackColor(bc);
    vc.setFontColor(fc);
    vc.setFontFile(ff);
    vc.setCodeDict(cd);
    return vc;
}
Also used : CoreConfig(app.hongs.CoreConfig) HongsExpedient(app.hongs.HongsExpedient) Color(java.awt.Color)

Example 8 with HongsExpedient

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

the class Data method getFields.

/**
 * 获取字段
 * 当表单不在管理区域时
 * 会用当前表单覆盖管理表单
 * 配置文件不存在则抛出异常 0x1104
 * @return
 */
@Override
public Map getFields() {
    try {
        return super.getFields();
    } catch (NullPointerException ex) {
    // Nothing todo
    }
    Map fields, fieldx;
    /**
     * 字段以 centra/data 的字段为基础
     * 但可在 centre/data 重设部分字段
     *
     * 配置文件不得放在资源包里面
     * 此处会校验表单文件是否存在
     */
    try {
        if (!new File(Core.CONF_PATH + "/" + conf + Cnst.FORM_EXT + ".xml").exists()) {
            throw new HongsExpedient(0x1104, "Data form conf '" + conf + "' is not exists").setLocalizedOptions(conf);
        }
        fields = FormSet.getInstance(conf).getForm(form);
        if (!comf.equals(conf)) {
            if (!new File(Core.CONF_PATH + "/" + comf + Cnst.FORM_EXT + ".xml").exists()) {
                throw new HongsExpedient(0x1104, "Data form conf '" + comf + "' is not exists").setLocalizedOptions(comf);
            }
            fieldx = FormSet.getInstance(comf).getForm(form);
            // 补充上额外的字段设置
            fieldx = new HashMap(fieldx);
            fieldx.putAll(fields);
            fields = fieldx;
        }
    } catch (HongsException ex) {
        throw ex.toExpedient();
    }
    setFields(fields);
    return fields;
}
Also used : HashMap(java.util.HashMap) HongsException(app.hongs.HongsException) HongsExpedient(app.hongs.HongsExpedient) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 9 with HongsExpedient

use of app.hongs.HongsExpedient 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)

Example 10 with HongsExpedient

use of app.hongs.HongsExpedient 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();
    try {
        // 下划线开头的为内部资源, 不直接对外开放
        if (ent.startsWith("_")) {
            throw new HongsException(0x1100, "Unsupported Request!");
        }
        // 判断是否禁用了当前动作, 忽略表单不存在
        Map fa = FormSet.getInstance(mod).getForm(ent);
        Set ca = Synt.toSet(Dict.getDepth(fa, "@", "callable"));
        if (ca != null && !ca.contains(act)) {
            throw new HongsException(0x1100, "Unsupported Request.");
        }
    } catch (HongsException | HongsExpedient ex) {
        int ec = ex.getErrno();
        if (ec != 0x10e8 && ec != 0x10ea) {
            throw ex;
        }
    }
}
Also used : Set(java.util.Set) FormSet(app.hongs.action.FormSet) HongsException(app.hongs.HongsException) HongsExpedient(app.hongs.HongsExpedient) Map(java.util.Map)

Aggregations

HongsExpedient (app.hongs.HongsExpedient)11 HongsException (app.hongs.HongsException)7 Map (java.util.Map)7 HashMap (java.util.HashMap)5 Core (app.hongs.Core)4 HongsError (app.hongs.HongsError)4 CoreConfig (app.hongs.CoreConfig)3 ActionHelper (app.hongs.action.ActionHelper)2 FormSet (app.hongs.action.FormSet)2 NaviMap (app.hongs.action.NaviMap)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ServletException (javax.servlet.ServletException)2 Part (javax.servlet.http.Part)2 CoreLocale (app.hongs.CoreLocale)1 ActionRunner (app.hongs.action.ActionRunner)1 Color (java.awt.Color)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1