Search in sources :

Example 6 with CoreConfig

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

the class MoreAction method exec.

@Action("exec")
public void exec(ActionHelper helper) throws HongsException {
    CoreConfig cnf = CoreConfig.getInstance();
    HttpServletRequest req = helper.getRequest();
    HttpServletResponse rsp = helper.getResponse();
    // 许可及IP白名单
    boolean sw = cnf.getProperty("core.exec.more.enable", false);
    String ia = cnf.getProperty("core.exec.more.allows");
    String ip = ActionDriver.getClientAddr(req);
    Set ias = Synt.toTerms(ia);
    if (ias == null || ias.isEmpty()) {
        ias = new HashSet();
        ias.add("::1");
        ias.add("127.0.0.1");
        ias.add("0:0:0:0:0:0:0:1");
    }
    if (!sw) {
        throw new HongsException(400, "Illegal request!");
    }
    if (!ias.contains(ip)) {
        throw new HongsException(400, "Illegal request.");
    }
    Map map = helper.getRequestData();
    String act = Core.ACTION_NAME.get();
    String cmd = (String) map.get("cmd");
    try {
        exec(helper, cmd, req, rsp);
    } finally {
        Core.ACTION_NAME.set(act);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashSet(java.util.HashSet) Set(java.util.Set) CoreConfig(io.github.ihongs.CoreConfig) HongsException(io.github.ihongs.HongsException) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Action(io.github.ihongs.action.anno.Action)

Example 7 with CoreConfig

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

the class CmdletRunner method init.

/**
 * 命令启动初始化
 * @param args
 * @return
 */
public static String[] init(String[] args) {
    Map<String, Object> opts;
    opts = CmdletHelper.getOpts(args, "DEBUG:i", "COREPATH:s", "CONFPATH:s", "DATAPATH:s", "BASEPATH:s", "BASEHREF:s", "LANGUAGE:s", "TIMEZONE:s", "!U", "!A");
    args = (String[]) opts.get("");
    Core.THREAD_CORE.set(Core.GLOBAL_CORE);
    Core.ACTION_TIME.set(Core.STARTS_TIME);
    Core.ACTION_NAME.set(args.length != 0 ? args[0] : null);
    /**
     * 核心属性配置 *
     */
    Core.ENVIR = 0;
    Core.DEBUG = Synt.declare(opts.get("DEBUG"), (byte) 0);
    Core.CORE_PATH = Synt.declare(opts.get("COREPATH"), System.getProperty("user.dir"));
    Core.CORE_PATH = new File(Core.CORE_PATH).getAbsolutePath();
    Core.CONF_PATH = Synt.declare(opts.get("CONFPATH"), Core.CORE_PATH + File.separator + "etc");
    Core.DATA_PATH = Synt.declare(opts.get("DATAPATH"), Core.CORE_PATH + File.separator + "var");
    Core.BASE_PATH = Synt.declare(opts.get("BASEPATH"), Core.CORE_PATH + File.separator + "web");
    // 如果 web 目录不存在, 则视为在 WEB-INF 下
    File bp = new File(Core.BASE_PATH);
    if (!bp.exists()) {
        Core.BASE_PATH = bp.getParent();
    }
    /**
     * 系统属性配置 *
     */
    CoreConfig cnf = CoreConfig.getInstance("defines");
    Core.SERVER_ID = cnf.getProperty("server.id", "0");
    Map m = new HashMap();
    m.put("SERVER_ID", Core.SERVER_ID);
    m.put("BASE_PATH", Core.BASE_PATH);
    m.put("CORE_PATH", Core.CORE_PATH);
    m.put("CONF_PATH", Core.CONF_PATH);
    m.put("DATA_PATH", Core.DATA_PATH);
    // 启动系统属性
    for (Map.Entry et : cnf.entrySet()) {
        String k = (String) et.getKey();
        String v = (String) et.getValue();
        if (k.startsWith("envir.")) {
            k = k.substring(6);
            v = Syno.inject(v, m);
            System.setProperty(k, v);
        }
    }
    if (4 == (4 & Core.DEBUG)) {
        // 调试系统属性
        for (Map.Entry et : cnf.entrySet()) {
            String k = (String) et.getKey();
            String v = (String) et.getValue();
            if (k.startsWith("debug.")) {
                k = k.substring(6);
                v = Syno.inject(v, m);
                System.setProperty(k, v);
            }
        }
    }
    /**
     * 提取网址前缀 *
     */
    String su = Synt.defoult((String) opts.get("BASEHREF"), System.getProperty("serv.url"));
    if (null != su) {
        Pattern pattern = Pattern.compile("^\\w+://[^/]+");
        Matcher matcher = pattern.matcher(su);
        if (matcher.find()) {
            Core.SERV_PATH = su.substring(0 + matcher.end());
            Core.SERV_HREF = su.substring(0, matcher.end());
        } else {
            Core.SERV_PATH = su;
            Core.SERV_HREF = "";
        }
    } else {
        Core.SERV_PATH = "";
        Core.SERV_HREF = "";
    }
    /**
     * 实例属性配置 *
     */
    cnf = CoreConfig.getInstance("default");
    String zone = null;
    if (opts.containsKey("TIMEZONE")) {
        zone = (String) opts.get("TIMEZONE");
    }
    if (zone == null || zone.length() == 0) {
        if (cnf.getProperty("core.timezone.probing", false)) {
            zone = TimeZone.getDefault().getID();
        } else {
            zone = cnf.getProperty("core.timezone.default", Cnst.ZONE_DEF);
        }
    }
    Core.ACTION_ZONE.set(zone);
    String lang = null;
    if (opts.containsKey("LANGUAGE")) {
        lang = (String) opts.get("LANGUAGE");
    }
    if (lang == null || lang.length() == 0) {
        if (cnf.getProperty("core.language.probing", false)) {
            /**
             * 获取系统默认的区域
             * 仅保留 语言[_地区]
             */
            lang = Locale.getDefault().toString();
            int pos = lang.indexOf('_');
            if (pos > 0) {
                pos = lang.indexOf('_', pos + 1);
                if (pos > 0) {
                    lang = lang.substring(0, pos);
                }
            }
            lang = CoreLocale.getAcceptLanguage(lang);
            if (lang == null) {
                lang = cnf.getProperty("core.language.default", Cnst.LANG_DEF);
            }
        } else {
            lang = cnf.getProperty("core.language.default", Cnst.ZONE_DEF);
        }
    } else {
        /**
         * 检查语言参数设置
         */
        String leng;
        leng = lang;
        lang = CoreLocale.getAcceptLanguage(lang);
        if (lang == null) {
            CoreLogger.error("ERROR: Unsupported language: " + leng + ".");
            System.exit(1);
        }
    }
    Core.ACTION_LANG.set(lang);
    /**
     * 初始化动作助手, 可复用动作组件 *
     */
    ActionHelper hlpr = new ActionHelper(null, null, null, null);
    Core.getInstance().set(ActionHelper.class.getName(), hlpr);
    hlpr.updateOutput(System.out, new PrintWriter(System.out));
    return args;
}
Also used : Pattern(java.util.regex.Pattern) CoreConfig(io.github.ihongs.CoreConfig) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ActionHelper(io.github.ihongs.action.ActionHelper) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 8 with CoreConfig

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

the class LuceneRecord method getWriter.

public IndexWriter getWriter() throws HongsException {
    if (writer == null || writer.isOpen() == false) {
        String path = getDbPath();
        try {
            /**
             * 2021/04/18
             * 为在一个库里存多个表
             * 不再在开始预设分析器
             * 改为存字段时直接写入 TokenStream
             */
            CoreConfig cc = CoreConfig.getInstance();
            IndexWriterConfig iwc = new IndexWriterConfig();
            // IndexWriterConfig iwc = new IndexWriterConfig(getAnalyzer());
            iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
            iwc.setMaxBufferedDocs(cc.getProperty("core.lucene.max.buf.docs", -1));
            iwc.setRAMBufferSizeMB(cc.getProperty("core.lucene.ram.buf.size", 16D));
            Directory dir = FSDirectory.open(Paths.get(path));
            writer = new IndexWriter(dir, iwc);
        } catch (IOException x) {
            throw new HongsException(x);
        }
        if (4 == (4 & Core.DEBUG)) {
            CoreLogger.trace("Start the lucene writer for " + getDbName());
        }
    }
    return writer;
}
Also used : CoreConfig(io.github.ihongs.CoreConfig) IndexWriter(org.apache.lucene.index.IndexWriter) HongsException(io.github.ihongs.HongsException) IOException(java.io.IOException) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 9 with CoreConfig

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

the class ActionDriver method doLaunch.

private void doLaunch(Core core, ActionHelper hlpr, HttpServletRequest req) throws ServletException {
    Core.ACTION_TIME.set(System.currentTimeMillis());
    Core.ACTION_NAME.set(getOriginPath(req).substring(1));
    // 外部没有指定网站域名则在首次请求时进行设置(非线程安全)
    if (Core.SERV_HREF == null || Core.SERV_HREF.isEmpty()) {
        Core.SERV_HREF = Core.SERVER_HREF.get();
    }
    CoreConfig conf = core.got(CoreConfig.class);
    Core.ACTION_LANG.set(conf.getProperty("core.language.default", Cnst.LANG_DEF));
    if (conf.getProperty("core.language.probing", false)) {
        /**
         * 语言可以记录到Session/Cookies里
         */
        String sess = conf.getProperty("core.language.session", Cnst.LANG_KEY);
        String lang = (String) hlpr.getSessibute(sess);
        if (lang == null || lang.length() == 0) {
            lang = (String) hlpr.getCookibute(sess);
            if (lang == null || lang.length() == 0) {
                lang = req.getHeader("Accept-Language");
            }
        }
        /**
         * 检查是否是支持的语言
         */
        if (lang != null) {
            lang = CoreLocale.getAcceptLanguage(lang);
            if (lang != null) {
                Core.ACTION_LANG.set(lang);
            }
        }
    }
    Core.ACTION_ZONE.set(conf.getProperty("core.timezone.default", Cnst.ZONE_DEF));
    if (conf.getProperty("core.timezone.probing", false)) {
        /**
         * 时区可以记录到Session/Cookies里
         */
        String sess = conf.getProperty("core.timezone.session", Cnst.ZONE_KEY);
        String zone = (String) hlpr.getSessibute(sess);
        if (zone == null || zone.length() == 0) {
            zone = (String) hlpr.getCookibute(sess);
            if (zone == null || zone.length() == 0) {
                zone = req.getHeader(/**/
                "X-Timezone");
            }
        }
        /**
         * 过滤一下避免错误时区
         */
        if (zone != null) {
            zone = TimeZone.getTimeZone(zone).getID();
            // if (zone != null) {
            Core.ACTION_ZONE.set(zone);
        // }
        }
    }
}
Also used : CoreConfig(io.github.ihongs.CoreConfig)

Example 10 with CoreConfig

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

the class Pagelet method inAjax.

private boolean inAjax(HttpServletRequest req) {
    // JSONP
    String c = Cnst.CB_KEY;
    c = req.getParameter(c);
    if (c != null && !c.isEmpty()) {
        return true;
    }
    CoreConfig cnf = CoreConfig.getInstance("default");
    c = cnf.getProperty("core.callback", "callback");
    c = req.getParameter(c);
    if (c != null && !c.isEmpty()) {
        return true;
    }
    // Accept
    String a = req.getHeader("Accept");
    if (a != null) {
        if (IS_JSON.matcher(a).find()) {
            return true;
        }
        if (IS_HTML.matcher(a).find()) {
            return false;
        }
    }
    // Ajax
    String x = req.getHeader("X-Requested-With");
    if (x != null && !x.isEmpty()) {
        return true;
    } else {
        return false;
    }
}
Also used : CoreConfig(io.github.ihongs.CoreConfig)

Aggregations

CoreConfig (io.github.ihongs.CoreConfig)23 Map (java.util.Map)12 HashMap (java.util.HashMap)11 HongsException (io.github.ihongs.HongsException)10 Action (io.github.ihongs.action.anno.Action)9 CommitSuccess (io.github.ihongs.action.anno.CommitSuccess)7 IOException (java.io.IOException)4 HongsExemption (io.github.ihongs.HongsExemption)3 Verify (io.github.ihongs.action.anno.Verify)3 File (java.io.File)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ActionHelper (io.github.ihongs.action.ActionHelper)1 Cmdlet (io.github.ihongs.cmdlet.anno.Cmdlet)1 DB (io.github.ihongs.db.DB)1 Table (io.github.ihongs.db.Table)1