use of app.hongs.HongsError in project HongsCORE by ihongs.
the class AutoFilter method getacts.
private Set<String> getacts() {
if (null != actset) {
return actset;
}
// 总是通过 search 获取动作 class
// 即使无需 search 也要存在 search 方法才行
Class cls;
try {
cls = ActionRunner.getActions().get(action.substring(1) + "/search").getMclass();
} catch (NullPointerException ex) {
throw new HongsError(0x3e, "Auto action '" + action.substring(1) + "/search' is not exists", ex);
}
cstset = new HashSet();
actset = new TreeSet(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// 对比两个动作路径层级数
// 优先匹配层级更深的动作
int i, c1 = 0, c2 = 0;
i = 0;
while ((i = o1.indexOf('/', i)) != -1) {
i++;
c1++;
}
i = 0;
while ((i = o2.indexOf('/', i)) != -1) {
i++;
c2++;
}
i = Integer.compare(c2, c1);
return i != 0 ? i : 1;
}
});
for (Method mtd : cls.getMethods()) {
Action ann = mtd.getAnnotation(Action.class);
if (null != ann) {
String uri;
if (!"".equals(ann.value())) {
uri = "/" + ann.value();
} else {
uri = "/" + mtd.getName();
}
if (mtd.isAnnotationPresent(CustomReplies.class)) {
cstset.add(uri);
}
actset.add(uri);
}
}
return actset;
}
use of app.hongs.HongsError in project HongsCORE by ihongs.
the class CmdletHelper method getOpts.
// ** 参数相关 **/
/**
* 解析参数
* 本函数遵循 Perl 的 Getopt::Long 解析规则
* @param args
* @param chks
* @return
*/
public static Map<String, Object> getOpts(String[] args, String... chks) {
Map<String, Object[]> chkz = new HashMap();
Map<String, Object> newOpts = new HashMap();
List<String> newArgs = new ArrayList();
Set<String> reqOpts = new LinkedHashSet();
Set<String> errMsgs = new LinkedHashSet();
Pattern p = Pattern.compile("^([\\w\\.\\-\\|]*)(=|:|\\+|\\*)([sifb]|\\/(.+)\\/(i)?( .*)?)$");
Pattern bp = Pattern.compile("^(true|false|yes|no|y|n|1|0)$", Pattern.CASE_INSENSITIVE);
Pattern tp = Pattern.compile("^(true|yes|y|1)$", Pattern.CASE_INSENSITIVE);
Pattern fp = Pattern.compile("^\\d+(\\.\\d+)?$");
Pattern ip = Pattern.compile("^\\d+$");
// 禁止未知参数
boolean ub = false;
// 禁止匿名参数
boolean vb = false;
// 用法说明
String hlp = null;
String pre = "\r\n\t";
for (String chk : chks) {
Matcher m = p.matcher(chk);
if (!m.find()) {
if (chk.equals("!U")) {
ub = true;
continue;
} else if (chk.equals("!A")) {
vb = true;
continue;
} else if (chk.startsWith("?")) {
hlp = chk.substring(1);
continue;
}
// 0号错误
errMsgs.add(GETERRS[0].replace("%chk", chk));
continue;
}
String name = m.group(1);
String sign = m.group(2);
String type = m.group(3);
if ("=".equals(sign) || "+".equals(sign)) {
reqOpts.add(name);
}
if (type.startsWith("/")) {
String reg = m.group(4);
String mod = m.group(5);
String err = m.group(6);
Pattern pat;
if (mod != null) {
pat = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
} else {
pat = Pattern.compile(reg);
}
if (err != null) {
err = err.trim();
} else {
err = GETERRS[7];
}
reg = "/" + reg + "/" + mod;
chkz.put(name, new Object[] { sign.charAt(0), 'r', pat, reg, err });
} else {
chkz.put(name, new Object[] { sign.charAt(0), type.charAt(0) });
}
}
F: for (int i = 0; i < args.length; i++) {
String name = args[i];
if (name.startsWith("--")) {
name = name.substring(2);
if (chkz.containsKey(name)) {
Object[] chk = chkz.get(name);
char sign = (Character) chk[0];
char type = (Character) chk[1];
Pattern rp = null;
String reg = null;
String err = null;
Object val = null;
List vals = null;
if ('r' == type) {
rp = (Pattern) chk[3];
reg = (String) chk[4];
err = (String) chk[5];
}
if ('+' == sign || '*' == sign) {
vals = (List) newOpts.get(name);
if (vals == null) {
vals = new ArrayList();
newOpts.put(name, vals);
}
}
W: while (i < args.length - 1) {
String arg = args[i + 1];
if (arg.startsWith("--")) {
if (i == 0)
break;
else
continue F;
}
if (arg.startsWith("\\")) {
arg = arg.substring(1);
}
i++;
switch(type) {
case 'i':
if (!ip.matcher(arg).matches()) {
errMsgs.add(GETERRS[4].replace("%opt", name));
continue W;
}
val = Long.parseLong(arg);
break;
case 'f':
if (!fp.matcher(arg).matches()) {
errMsgs.add(GETERRS[5].replace("%opt", name));
continue W;
}
val = Double.parseDouble(arg);
break;
case 'b':
if (!bp.matcher(arg).matches()) {
errMsgs.add(GETERRS[6].replace("%opt", name));
continue W;
}
val = tp.matcher(arg).matches();
break;
case 'r':
if (!rp.matcher(arg).matches()) {
errMsgs.add(err.replace("%opt", name).replace("%mat", reg));
continue W;
}
default:
val = arg;
}
if ('+' == sign || '*' == sign) {
vals.add(val);
} else {
if (newOpts.containsKey(name)) {
errMsgs.add(GETERRS[3].replace("%opt", name));
} else {
newOpts.put(name, val);
}
}
continue F;
}
if ('b' == type) {
if ('+' == sign || '*' == sign) {
vals.add(true);
} else {
if (newOpts.containsKey(name)) {
errMsgs.add(GETERRS[3].replace("%opt", name));
} else {
newOpts.put(name, true);
}
}
} else {
errMsgs.add(GETERRS[2].replace("%opt", name));
}
} else if (ub) {
// 7号错误
errMsgs.add(GETERRS[8].replace("%opt", name));
} else {
newArgs.add(args[i]);
}
} else if (vb) {
// 8号错误
errMsgs.add(GETERRS[9]);
} else {
newArgs.add(args[i]);
}
}
for (String name : reqOpts) {
if (!newOpts.containsKey(name)) {
Set<String> err = new LinkedHashSet();
err.add(GETERRS[1].replace("%opt", name));
err.addAll(errMsgs);
errMsgs = err;
}
}
if (!errMsgs.isEmpty()) {
StringBuilder err = new StringBuilder();
for (String msg : errMsgs) {
err.append(pre).append(msg);
}
String msg = err.toString();
String trs = msg;
if (null != hlp) {
trs += pre + hlp.replaceAll("\\n", pre);
}
HongsError er = new HongsError(0x35, msg);
er.setLocalizedOptions(trs);
throw er;
} else if (hlp != null && args.length == 0) {
System.err.println(hlp.replaceAll("\\n", pre));
System.exit(0);
}
// 把剩余的参数放进去
newOpts.put("", newArgs.toArray(new String[0]));
return newOpts;
}
use of app.hongs.HongsError in project HongsCORE by ihongs.
the class ConfAction 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 {
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;
}
/**
* 如果指定配置的数据并没有改变
* 则直接返回 304 Not modified
*/
String m;
m = helper.getRequest().getHeader("If-Modified-Since");
if (m != null && m.equals(ConfAction.MTIMES.get(name))) {
helper.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
/**
* 如果没有配置
* 则调用工厂方法构造 JS 代码
*/
String s;
if (!ConfAction.CACHES.containsKey(name)) {
try {
s = this.makeConf(name);
} catch (HongsError ex) {
helper.error500(ex.getMessage());
return;
}
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
m = sdf.format(new Date());
ConfAction.CACHES.put(name, s);
ConfAction.MTIMES.put(name, m);
} else {
s = ConfAction.CACHES.get(name);
m = ConfAction.MTIMES.get(name);
}
// 标明修改时间
helper.getResponse().setHeader("Last-Modified", m);
// 输出配置信息
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.HsCONF)self.HsCONF={};Object.assign(self.HsCONF," + s + ");", "text/javascript");
}
}
}
use of app.hongs.HongsError in project HongsCORE by ihongs.
the class LangAction method service.
/**
* 服务方法
* 判断配置和消息有没有生成, 如果没有则生成; 消息按客户语言存放
* @param req
* @param rsp
* @throws java.io.IOException
* @throws javax.servlet.ServletException
*/
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException, ServletException {
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;
}
/**
* 如果指定语言的数据并没有改变
* 则直接返回 304 Not modified
*/
String m;
m = helper.getRequest().getHeader("If-Modified-Since");
if (m != null && m.equals(LangAction.MTIMES.get(name))) {
helper.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
/**
* 如果没有语言
* 则调用工厂方法构造 JS 代码
*/
String s;
if (!LangAction.CACHES.containsKey(name)) {
try {
s = this.makeLang(name);
} catch (HongsError ex) {
helper.error500(ex.getMessage());
return;
}
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
m = sdf.format(new Date());
LangAction.CACHES.put(name, s);
LangAction.MTIMES.put(name, m);
} else {
s = LangAction.CACHES.get(name);
m = LangAction.MTIMES.get(name);
}
// 标明修改时间
helper.getResponse().setHeader("Last-Modified", m);
// 输出语言信息
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.HsLANG)self.HsLANG={};Object.assign(self.HsLANG," + s + ");", "text/javascript");
}
}
}
use of app.hongs.HongsError 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)));
}
}
}
Aggregations