use of io.github.ihongs.HongsExemption 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.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 lang = name + "_" + Core.ACTION_LANG.get();
/**
* 如果指定语言的数据并没有改变
* 则直接返回 304 Not modified
*/
long m = helper.getRequest().getDateHeader("If-Modified-Since");
if (LangAction.MTIMES.containsKey(lang) && MTIMES.get(lang) <= m) {
helper.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
/**
* 如果没有语言
* 则调用工厂方法构造 JS 代码
*/
String s;
if (!LangAction.CACHES.containsKey(lang)) {
try {
s = this.makeLang(name);
} catch (HongsExemption ex) {
helper.error(404, ex.getMessage());
return;
}
// HTTP 时间精确到秒
m = System.currentTimeMillis() / 1000L * 1000L;
LangAction.CACHES.put(lang, s);
LangAction.MTIMES.put(lang, m);
} else {
s = LangAction.CACHES.get(lang);
m = LangAction.MTIMES.get(lang);
}
// 标明修改时间
helper.getResponse().setDateHeader("Last-Modified", m);
// 输出语言信息
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.HsLANG=Object.assign(self.HsLANG||{}";
helper.write("text/javascript", c + "," + s + ");");
}
}
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class CmdletRunner method exec.
/**
* 内部执行
* @param args
*/
public static void exec(String[] args) {
String act = null;
int l = args.length;
if (l == 1) {
act = args[0];
args = new String[0];
} else if (l > 1) {
act = args[0];
args = Arrays.copyOfRange(args, 1, l);
}
// 提取动作
if (null == act || act.length() < 1) {
throw new HongsExemption(835, "Cmdlet name can not be empty.");
}
// 获取方法
Method met = getCmdlets().get(act);
if (null == met) {
throw new HongsExemption(835, "Cmdlet " + act + " is not exists.");
}
// 执行方法
try {
met.invoke(null, new Object[] { args });
} catch (IllegalAccessException ex) {
throw new HongsExemption(836, "Illegal access for method " + met.getClass().getName() + "." + met.getName() + "(String[]).", ex);
} catch (IllegalArgumentException ex) {
throw new HongsExemption(836, "Illegal params for method " + met.getClass().getName() + "." + met.getName() + "(String[]).", ex);
} catch (InvocationTargetException ex) {
throw new HongsExemption(837, ex.getCause());
}
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class AuthFilter method doFilter.
@Override
public void doFilter(Core core, ActionHelper hlpr, FilterChain chain) throws IOException, ServletException {
HttpServletResponse rsp = hlpr.getResponse();
HttpServletRequest req = hlpr.getRequest();
String act = ActionDriver.getRecentPath(req);
/**
* 标记当前登录区域, 以便区分不同权限
*/
req.setAttribute(AuthFilter.class.getName() + ":config", aut);
req.setAttribute(AuthFilter.class.getName() + ":expire", exp);
/**
* 检查当前动作是否可以忽略
*/
if (patter != null && !patter.matches(act)) {
chain.doFilter(req, rsp);
return;
}
/**
* 登记的动作权限串无前斜杠
*/
if (act.startsWith("/")) {
act = act.substring(1);
}
/**
* 自动重载导航对象(权限表)
*/
long fmt = siteMap.fileModified();
long dmt = siteMap.dataModified();
if (fmt == 0 || fmt > dmt) {
try {
siteMap.init();
} catch (HongsException e) {
throw new ServletException(e);
}
}
/**
* 判断当前用户是否登录超时
* 未超时且是调试模式
* 对超级管理员无限制
* 自定义 RoleSet 中可抛出 401,403 异常
*/
Set authset = null;
Set actions = siteMap.actions;
long now = System.currentTimeMillis() / 1000;
long ust = Synt.declare(hlpr.getSessibute(Cnst.UST_SES), 0L);
if (exp == 0 || exp > now - ust) {
if (4 == (4 & Core.DEBUG)) {
Object uid = hlpr.getSessibute(Cnst.UID_SES);
if (Cnst.ADM_UID.equals(uid)) {
actions = EMP_SET;
}
}
try {
authset = siteMap.getAuthSet();
} catch (HongsException e) {
int c = e.getState();
if (c >= 401 && c <= 403) {
doFailed(core, hlpr, (byte) (c - 400), e.getLocalizedMessage(), null);
return;
}
throw e.toExemption();
} catch (HongsExemption e) {
int c = e.getState();
if (c >= 401 && c <= 403) {
doFailed(core, hlpr, (byte) (c - 400), e.getLocalizedMessage(), null);
return;
}
throw e.toExemption();
}
ust = 0;
}
if (authset != null) {
if (actions.contains(aut) && !authset.contains(aut)) {
doFailed(core, hlpr, (byte) 2);
return;
}
if (actions.contains(act) && !authset.contains(act)) {
doFailed(core, hlpr, (byte) 3);
return;
}
} else {
if (actions.contains(aut)) {
doFailed(core, hlpr, (byte) (ust > 0 ? 0 : 1));
return;
}
if (actions.contains(act)) {
doFailed(core, hlpr, (byte) (ust > 0 ? 0 : 1));
return;
}
}
chain.doFilter(req, rsp);
}
use of io.github.ihongs.HongsExemption 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.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;
}
/**
* 如果指定配置的数据并没有改变
* 则直接返回 304 Not modified
*/
long m = helper.getRequest().getDateHeader("If-Modified-Since");
if (ConfAction.MTIMES.containsKey(name) && MTIMES.get(name) <= m) {
helper.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
/**
* 如果没有配置
* 则调用工厂方法构造 JS 代码
*/
String s;
if (!ConfAction.CACHES.containsKey(name)) {
try {
s = this.makeConf(name);
} catch (HongsExemption ex) {
helper.error(404, ex.getMessage());
return;
}
// HTTP 时间精确到秒
m = System.currentTimeMillis() / 1000L * 1000L;
ConfAction.CACHES.put(name, s);
ConfAction.MTIMES.put(name, m);
} else {
s = ConfAction.CACHES.get(name);
m = ConfAction.MTIMES.get(name);
}
// 标明修改时间
helper.getResponse().setDateHeader("Last-Modified", m);
// 输出配置信息
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.HsCONF=Object.assign(self.HsCONF||{}";
helper.write("text/javascript", c + "," + s + ");");
}
}
}
Aggregations