use of io.github.ihongs.HongsException 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 + ");");
}
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class AuthTag method doStartTag.
@Override
public int doStartTag() throws JspException {
try {
NaviMap nav = NaviMap.getInstance(this.cnf);
this.ebb = (this.act == null || nav.chkAuth(this.act)) && (this.rol == null || nav.chkRole(this.rol)) && (this.men == null || nav.chkMenu(this.men));
} catch (HongsException ex) {
throw new JspException(ex);
}
if (this.not) {
this.ebb = !this.ebb;
}
if (this.ebb) {
return BodyTagSupport.EVAL_BODY_BUFFERED;
} else {
return BodyTagSupport.SKIP_BODY;
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class JAction method acting.
@Override
public void acting(ActionHelper helper, ActionRunner runner) throws HongsException {
String act = runner.getHandle();
String ent = runner.getEntity();
String mod = runner.getModule();
if (ent.startsWith("_") || mod.endsWith("/" + ent)) {
throw new HongsException(404, "Unsupported Request!");
}
Map fs = null;
do {
try {
fs = FormSet.getInstance(mod).getForm(ent);
break;
} catch (HongsException ex) {
if (ex.getErrno() != 910 && ex.getErrno() != 912) {
// 非表单缺失
throw ex;
}
}
mod = mod + "/" + ent;
try {
fs = FormSet.getInstance(mod).getForm(ent);
runner.setModule(mod);
} catch (HongsException ex) {
if (ex.getErrno() != 910 && ex.getErrno() != 912) {
// 非表单缺失
throw ex;
}
}
} while (false);
if (fs == null) {
return;
}
Set ca = Synt.toSet(Dict.get(fs, null, "@", "callable"));
if (ca != null && !ca.contains(act)) {
throw new HongsException(405, "Unsupported Request.");
}
}
use of io.github.ihongs.HongsException 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.HongsException in project HongsCORE by ihongs.
the class Access method exec.
@Cmdlet("exec")
public static void exec(String[] args) throws HongsException {
if (args.length == 0) {
CmdletHelper.ERR.get().println("Usage: CMDLET_NAME [ARG_0] [ARG_1] ...");
return;
}
// 请求参数
Map rep = new HashMap();
rep.put("cmd", args[0]);
if (args.length > 1) {
rep.put("args", Arrays.copyOfRange(args, 1, args.length));
}
String req = Dawn.toString(rep, true);
// 命令接口
String url = Core.SERV_HREF + Core.SERV_PATH + "/common/more/exec" + Cnst.ACT_EXT;
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(0);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json,text/html,*/*;q=0.8");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("X-Requested-With", CoreConfig.getInstance().getProperty("core.powered.by"));
String ln;
PrintStream ps;
PrintWriter pw;
BufferedReader br;
pw = new PrintWriter(conn.getOutputStream());
pw.print(req);
pw.flush();
pw.close();
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
ps = CmdletHelper.OUT.get();
while ((ln = br.readLine()) != null) {
ps.print(ln);
}
ps.println();
} catch (UnsupportedEncodingException ex) {
throw new HongsException(1111, ex);
} catch (MalformedURLException ex) {
throw new HongsException(1111, ex);
} catch (IOException ex) {
throw new HongsException(1110, ex);
}
}
Aggregations