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);
}
}
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;
}
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;
}
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);
// }
}
}
}
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;
}
}
Aggregations