Search in sources :

Example 1 with Core

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

the class DB method getInstance.

// ** 构造工厂 **/
/**
 * 获取指定数据库对象
 * <b>注意:</b>
 * <p>
 * 数据库配置中有指定 dbClass, 请务必添加 getInstance:
 * </p>
 * <pre>
 *    public static XxDB getInstance()
 *       throws HongsException
 *    {
 *       return new XxDB();
 *    }
 * </pre>
 * @param name
 * @return 指定DB对象
 * @throws io.github.ihongs.HongsException
 */
public static DB getInstance(String name) throws HongsException {
    String cn = DB.class.getName() + ":" + name;
    Core core = Core.getInstance();
    DB db = (DB) core.get(cn);
    if (db != null) {
        return db;
    }
    DBConfig cc = new DBConfig(name);
    if (cc.dbClass != null && cc.dbClass.length() != 0) {
        return (DB) Core.getInstance(cc.dbClass);
    } else {
        db = new DB(cc);
        core.set(cn, db);
        return db;
    }
}
Also used : Core(io.github.ihongs.Core)

Example 2 with Core

use of io.github.ihongs.Core 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.got(ActionHelper.class);
    Core.THREAD_CORE.set(core);
    if (act == null || act.length() == 0) {
        helper.fault(new HongsException(404, "Action URI can not be empty."));
        return;
    }
    // 去掉根和扩展名
    int pos = act.lastIndexOf('.');
    if (pos != -1) {
        act = act.substring(1, pos);
    } else {
        act = act.substring(1);
    }
    // 获取并执行动作
    try {
        new ActionRunner(helper, act).doAction();
    } catch (HongsException e) {
        helper.fault(e);
    } catch (HongsExemption e) {
        helper.fault(e);
    } catch (RuntimeException e) {
        helper.fault(new HongsException(500, e));
    }
}
Also used : ActionRunner(io.github.ihongs.action.ActionRunner) HongsException(io.github.ihongs.HongsException) ActionHelper(io.github.ihongs.action.ActionHelper) HongsExemption(io.github.ihongs.HongsExemption) Core(io.github.ihongs.Core)

Example 3 with Core

use of io.github.ihongs.Core 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 {
    /*
    // 2020/05/14  通过配置和用户的修改时间来判断是否能有变化
    // 受是否登录、不同用户等影响, 权限经常变化, 必须禁止缓存
    rsp.setHeader("Expires", "0");
    rsp.addHeader("Pragma" , "no-cache");
    rsp.setHeader("Cache-Control", "no-cache");
    */
    Core core = ActionDriver.getActualCore(req);
    ActionHelper helper = core.got(ActionHelper.class);
    String name = req.getPathInfo();
    if (name == null || name.length() == 0) {
        helper.error(400, "Path info required");
        return;
    }
    int p = name.lastIndexOf('.');
    if (p < 0) {
        helper.error(400, "File type required");
        return;
    }
    String type = name.substring(1 + p);
    name = name.substring(1, p);
    if (!"js".equals(type) && !"json".equals(type)) {
        helper.error(400, "Wrong file type: " + type);
        return;
    }
    String s;
    try {
        NaviMap sitemap = NaviMap.getInstance(name);
        Set<String> roleset = sitemap.getRoleSet();
        Set<String> authset;
        // 没有设置 rsname 的不公开
        if (null == sitemap.session) {
            helper.error(403, "Auth data for '" + name + "' is not open to the public");
            return;
        }
        // HTTP 304 缓存策略
        if (roleset instanceof CoreSerial.Mtimes) {
            CoreSerial.Mtimes rolemod = (CoreSerial.Mtimes) roleset;
            long l = Math.max(sitemap.dataModified(), rolemod.dataModified());
            long m = helper.getRequest().getDateHeader("If-Modified-Since");
            if (l != 0) {
                // HTTP 时间精确到秒
                l = l / 1000;
                m = m / 1000;
                if (m >= l) {
                    helper.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    return;
                } else {
                    helper.getResponse().setHeader("Cache-Control", "no-cache");
                    helper.getResponse().setDateHeader("Last-Modified", l * 1000);
                }
            }
        }
        Map<String, Boolean> datamap = new HashMap();
        if (null == roleset)
            authset = new HashSet();
        else
            authset = sitemap.getRoleAuths(roleset.toArray(new String[] {}));
        for (String act : sitemap.actions) {
            datamap.put(act, authset.contains(act));
        }
        s = Dawn.toString(datamap);
    } catch (IllegalArgumentException ex) {
        helper.error(500, ex.getMessage());
        return;
    } catch (HongsException | HongsExemption ex) {
        helper.error(404, ex.getMessage());
        return;
    }
    // 输出权限信息
    if ("json".equals(type)) {
        helper.write("application/json", s);
    } else {
        String c = req.getParameter("callback");
        if (c != null && !c.isEmpty()) {
            if (!c.matches("^[a-zA-Z_\\$][a-zA-Z0-9_]*$")) {
                helper.error(400, "Illegal callback function name!");
                return;
            }
            helper.write("text/javascript", c + "(" + s + ");");
        } else {
            c = "self.HsAUTH=Object.assign(self.HsAUTH||{}";
            helper.write("text/javascript", c + "," + s + ");");
        }
    }
}
Also used : HashMap(java.util.HashMap) HongsExemption(io.github.ihongs.HongsExemption) NaviMap(io.github.ihongs.action.NaviMap) CoreSerial(io.github.ihongs.CoreSerial) HongsException(io.github.ihongs.HongsException) ActionHelper(io.github.ihongs.action.ActionHelper) Core(io.github.ihongs.Core) HashSet(java.util.HashSet)

Example 4 with Core

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

the class LuceneRecord method getInstance.

/**
 * 获取实例
 * 存储为 conf/form 表单为 conf.form
 * 表单缺失则尝试获取 conf/form.form
 * 实例生命周期将交由 Core 维护
 * @param conf
 * @param form
 * @return
 * @throws HongsException
 */
public static LuceneRecord getInstance(String conf, String form) throws HongsException {
    String code = LuceneRecord.class.getName() + ":" + conf + "!" + form;
    Core core = Core.getInstance();
    LuceneRecord inst = (LuceneRecord) core.get(code);
    if (inst == null) {
        String path = conf + "/" + form;
        String name = conf + "!" + form;
        Map fxrm = FormSet.getInstance(conf).getForm(form);
        // 表单配置中可指定数据路径
        Map c = (Map) fxrm.get("@");
        if (c != null) {
            String p;
            p = (String) c.get("db-path");
            if (null != p && p.length() != 0) {
                path = p;
            }
            p = (String) c.get("db-name");
            if (null != p && p.length() != 0) {
                name = p;
            }
        }
        // 进一步处理路径中的变量等
        Map m = new HashMap();
        m.put("SERVER_ID", Core.SERVER_ID);
        m.put("CORE_PATH", Core.CORE_PATH);
        m.put("DATA_PATH", Core.DATA_PATH);
        path = Syno.inject(path, m);
        if (!new File(path).isAbsolute())
            path = Core.DATA_PATH + "/lucene/" + path;
        inst = new LuceneRecord(fxrm, path, name);
        core.set(code, inst);
    }
    return inst;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) File(java.io.File) Core(io.github.ihongs.Core)

Example 5 with Core

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

the class ActionDriver method doDriver.

final void doDriver(ServletRequest rep, ServletResponse rsp, final DriverProxy agt) throws ServletException, IOException {
    HttpServletRequest req = (HttpServletRequest) rep;
    HttpServletResponse rsq = (HttpServletResponse) rsp;
    ActionHelper hlpr;
    Core core = (Core) req.getAttribute(Core.class.getName());
    if (core == null) {
        /**
         * 外层调用
         */
        core = Core.getInstance();
        req.setAttribute(Core.class.getName(), core);
        hlpr = new ActionHelper(req, rsq);
        core.set(ActionHelper.class.getName(), hlpr);
        try {
            doLaunch(core, hlpr, req);
            agt.doDriver(core, hlpr);
            doCommit(core, hlpr, req, rsq);
        } catch (IOException ex) {
            // 非调试模式忽略客户端中途断开
            Throwable cx = ex.getCause();
            if (cx != null && cx.getClass().getSimpleName().equalsIgnoreCase("EofException") && 4 != (4 & Core.DEBUG)) {
                return;
            }
            /**
             * 异常抛到这层了就必须记入日志
             * 继续抛出将被登记的错误页处理
             * 下同
             */
            CoreLogger.error(ex);
            throw ex;
        } catch (ServletException ex) {
            CoreLogger.error(ex);
            throw ex;
        } catch (RuntimeException ex) {
            CoreLogger.error(ex);
            throw ex;
        } catch (Error er) {
            CoreLogger.error(er);
            throw er;
        } finally {
            doFinish(core, hlpr, req);
        }
    } else {
        /**
         * 内层调用
         */
        Core.THREAD_CORE.set(core);
        hlpr = core.got(ActionHelper.class);
        hlpr.updateHelper(req, rsq);
        /**/
        agt.doDriver(core, hlpr);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Core(io.github.ihongs.Core)

Aggregations

Core (io.github.ihongs.Core)19 ActionHelper (io.github.ihongs.action.ActionHelper)6 HashMap (java.util.HashMap)6 HongsExemption (io.github.ihongs.HongsExemption)5 Map (java.util.Map)5 LinkedHashMap (java.util.LinkedHashMap)3 HongsException (io.github.ihongs.HongsException)2 File (java.io.File)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 CoreSerial (io.github.ihongs.CoreSerial)1 ActionRunner (io.github.ihongs.action.ActionRunner)1 NaviMap (io.github.ihongs.action.NaviMap)1 Action (io.github.ihongs.action.anno.Action)1 Cmdlet (io.github.ihongs.cmdlet.anno.Cmdlet)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1