use of io.github.ihongs.HongsCause in project HongsCORE by ihongs.
the class Pagelet method doAction.
@Override
public void doAction(Core core, ActionHelper ah) throws ServletException, IOException {
HttpServletRequest req = ah.getRequest();
HttpServletResponse rsp = ah.getResponse();
/**
* 错误页但要的是 JSON
* 则不必构建页面 HTML
*/
if (req.getDispatcherType() == DispatcherType.ERROR) {
Integer ern = (Integer) req.getAttribute("javax.servlet.error.status_code");
if (ern != null && inAjax(req)) {
Object err = req.getAttribute("javax.servlet.error.exception");
Object msg = req.getAttribute("javax.servlet.error.message");
if (err != null) {
Throwable ex = (Throwable) err;
Throwable ax = ex.getCause();
if (ex instanceof HongsCause) {
ah.fault((HongsCause) ex);
} else if (ax instanceof HongsCause) {
ah.fault((HongsCause) ax);
} else {
ah.fault(new HongsException(ern, (String) msg, ax != null ? ax : ex));
}
} else {
Map dat = new HashMap();
dat.put("ok", false);
dat.put("ern", "Er" + ern);
dat.put("msg", /**/
msg);
ah.reply(dat);
rsp.setStatus(ern);
}
return;
}
}
try {
this._jspService(req, rsp);
} catch (ServletException ex) {
Throwable ax = ex.getCause();
if (ax == null) {
ax = ex;
}
String er = ax.getLocalizedMessage();
int eo = ax instanceof HongsCause ? ((HongsCause) ax).getState() : 0;
if (eo >= 600 || eo < 400) {
eo = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
req.setAttribute("javax.servlet.error.status_code", eo);
req.setAttribute("javax.servlet.error.message", er);
req.setAttribute("javax.servlet.error.exception", ax);
req.setAttribute("javax.servlet.error.exception_type", ax.getClass().getName());
rsp.sendError(eo, er);
} catch (RuntimeException ax) {
String er = ax.getLocalizedMessage();
int eo = ax instanceof HongsCause ? ((HongsCause) ax).getState() : 0;
if (eo >= 600 || eo < 400) {
eo = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
req.setAttribute("javax.servlet.error.status_code", eo);
req.setAttribute("javax.servlet.error.message", er);
req.setAttribute("javax.servlet.error.exception", ax);
req.setAttribute("javax.servlet.error.exception_type", ax.getClass().getName());
rsp.sendError(eo, er);
}
}
use of io.github.ihongs.HongsCause in project HongsCORE by ihongs.
the class VarsFilter method doFilter.
@Override
public void doFilter(Core core, ActionHelper hlpr, FilterChain chain) throws IOException, ServletException {
HttpServletResponse rsp = hlpr.getResponse();
HttpServletRequest req = hlpr.getRequest();
/**
* 跳过内部动作代理, 如 AutoFilter
*/
if (null != req.getAttribute(Cnst.ACTION_ATTR)) {
chain.doFilter(req, rsp);
return;
}
String act = ActionDriver.getRecentPath(req);
if (null != act && !patter.matches(act)) {
chain.doFilter(req, rsp);
return;
}
/**
* 上传文件时可能会发生异常
*/
Map rd;
try {
rd = hlpr.getRequestData();
} catch (Throwable e) {
if (e instanceof HongsCause) {
hlpr.fault((HongsCause) e);
} else {
hlpr.fault(e.getMessage());
}
return;
}
if (rn_limit != 0) {
int rn = Synt.declare(rd.get(Cnst.RN_KEY), Cnst.RN_DEF);
if (rn < 1 || rn > rn_limit) {
rsp.setStatus(400);
hlpr.fault(Cnst.RN_KEY + " must be 1 to " + rn_limit);
return;
}
}
if (illegals != null && !illegals.isEmpty()) {
Set ls = new HashSet(illegals);
Set ks = rd.keySet();
ls.retainAll(ks);
if (!ls.isEmpty()) {
rsp.setStatus(400);
hlpr.fault("Illegal parameters: " + Syno.concat(",", ls));
return;
}
}
if (sr_limit != 0 || sr_level != 0) {
try {
srCheck(rd, sr_limit, sr_level, 0, 1);
} catch (HongsException | HongsExemption ex) {
hlpr.fault(ex);
return;
}
}
chain.doFilter(req, rsp);
}
use of io.github.ihongs.HongsCause in project HongsCORE by ihongs.
the class VersFilter method doFilter.
@Override
public void doFilter(Core core, ActionHelper hlpr, FilterChain chain) throws IOException, ServletException {
HttpServletResponse rsp = hlpr.getResponse();
HttpServletRequest req = hlpr.getRequest();
if (level == 0) {
chain.doFilter(req, rsp);
return;
}
/**
* 跳过内部动作代理, 如 AutoFilter
*/
if (null != req.getAttribute(Cnst.ACTION_ATTR)) {
chain.doFilter(req, rsp);
return;
}
String act = ActionDriver.getRecentPath(req);
if (null != act && !urlPatter.matches(act)) {
chain.doFilter(req, rsp);
return;
}
String ref = /* Referer */
getRefersPath(req);
if (null != ref && !refPatter.matches(ref)) {
chain.doFilter(req, rsp);
return;
}
/**
* 上传文件时可能会发生异常
*/
Map rd;
try {
rd = hlpr.getRequestData();
} catch (Throwable e) {
if (e instanceof HongsCause) {
hlpr.fault((HongsCause) e);
} else {
hlpr.fault(e.getMessage());
}
return;
}
int rf = 0;
int nf = 0;
if (1 == (1 & level)) {
/**
* 非 JSON 已在 Dict 中兼容
*/
String ct = req.getContentType();
if (ct != null) {
ct = ct.split(";", 2)[0];
if (ct.endsWith("/json")) {
doChange(rd);
}
}
}
if (4 == (4 & level)) {
if (act != null) {
int p = act.lastIndexOf(".");
if (p > 0) {
String c = act.substring(0, p);
if (c.endsWith("/acount") || c.endsWith("/amount")) {
if (rd.containsKey(Cnst.OB_KEY) == false) {
rd.put(Cnst.OB_KEY, Synt.setOf("!"));
}
}
}
}
}
if (2 == (2 & level)) {
Set ab = Synt.toTerms(rd.get(Cnst.AB_KEY));
if (ab != null) {
rd.put(Cnst.AB_KEY, ab);
if (rd.containsKey(Cnst.ID_KEY) && (ab.contains("!enum") || ab.contains("!menu") || ab.contains("!info"))) {
rd.put(Cnst.ID_KEY, "");
}
if (ab.contains("!info")) {
ab.add(".info");
}
if (ab.contains(".form")) {
ab.add(".fall");
}
if (ab.contains(".enfo")) {
rf = -1;
} else if (ab.contains("!enum") || ab.contains(".enum")) {
ab.add(".enfo");
rf = 1;
} else if (ab.contains("!menu") || ab.contains(".menu")) {
ab.add(".enfo");
rf = 2;
}
if (ab.contains(".id")) {
nf += 1;
}
if (ab.contains(".rn")) {
nf += 2;
}
}
}
chain.doFilter(req, rsp);
if (2 == (2 & level)) {
Map sd = hlpr.getResponseData();
if (sd != null) {
if (sd.containsKey("enfo")) {
switch(rf) {
case 1:
sd.put("enum", sd.remove("enfo"));
break;
case 2:
sd.put("menu", sd.remove("enfo"));
break;
case 0:
sd.put("info", sd.remove("enfo"));
break;
}
}
if (1 != (1 & nf) && !sd.containsKey("info") && sd.containsKey(Cnst.ID_KEY)) {
Object id = sd.get(Cnst.ID_KEY);
rd.put(Cnst.ID_KEY, id);
sd.put("info", rd);
}
if (2 != (2 & nf) && !sd.containsKey("size") && sd.containsKey(Cnst.RN_KEY)) {
Object ct = sd.get(Cnst.RN_KEY);
sd.put("size", ct);
}
}
}
}
use of io.github.ihongs.HongsCause in project HongsCORE by ihongs.
the class Proclet method doAction.
@Override
public void doAction(Core core, ActionHelper ah) throws ServletException, IOException {
HttpServletRequest req = ah.getRequest();
HttpServletResponse rsp = ah.getResponse();
try {
this._jspService(new Request(req), rsp);
} catch (ServletException ex) {
Throwable ax = ex.getCause();
if (ax == null) {
ax = ex;
}
if (ax instanceof HongsCause) {
ah.fault((HongsCause) ax);
} else {
ah.fault(new HongsException(ax));
}
} catch (RuntimeException ax) {
if (ax instanceof HongsCause) {
ah.fault((HongsCause) ax);
} else {
ah.fault(new HongsException(ax));
}
}
}
use of io.github.ihongs.HongsCause in project HongsCORE by ihongs.
the class MoreAction method eval.
private void eval(ActionHelper helper, String act, HttpServletRequest req, HttpServletResponse rsp) {
// 重设路径
act = act + Cnst.ACT_EXT;
Core.ACTION_NAME.set(act);
helper.setAttribute(Cnst.ACTION_ATTR, null);
helper.setAttribute(Cnst.ORIGIN_ATTR, null);
try {
req.getRequestDispatcher("/" + act).include(req, rsp);
} catch (ServletException | IOException ex) {
if (ex.getCause() instanceof HongsCause) {
HongsCause ez = (HongsCause) ex.getCause();
String en = Integer.toHexString(ez.getErrno());
Map map = new HashMap();
map.put("ok", false);
map.put("ern", "Ex" + en);
map.put("err", ez.getMessage());
map.put("msg", ez.getLocalizedMessage());
helper.reply(map);
} else {
Map map = new HashMap();
map.put("ok", false);
map.put("ern", "Er500");
map.put("err", ex.getMessage());
map.put("msg", ex.getLocalizedMessage());
helper.reply(map);
}
}
}
Aggregations