use of io.github.ihongs.HongsException 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.HongsException in project HongsCORE by ihongs.
the class IsUnique method verify.
@Override
public Object verify(Value watch) throws Wrong {
// 跳过空值和空串
Object value = watch.get();
if (FORCE == STAND) {
if (value == null) {
return STAND;
}
if (value.equals("")) {
return STAND;
}
}
Map nd = watch.getCleans();
Object id = nd.get(Cnst.ID_KEY);
String ut = (String) getParam("data-ut");
String uk = (String) getParam("data-uk");
String nk = (String) getParam("__name__");
String ck = (String) getParam("__conf__");
String fk = (String) getParam("__form__");
if (ut == null || ut.isEmpty()) {
ut = ck + "/" + fk + "/search";
}
if (uk == null || uk.isEmpty()) {
uk = nk;
}
// 请求数据
Map cd = new HashMap();
Map rd = new HashMap();
rd.put(Cnst.PN_KEY, 0);
rd.put(Cnst.RN_KEY, 1);
rd.put(Cnst.RB_KEY, Synt.setOf(Cnst.ID_KEY));
// 更新需排除当前记录
if (watch.isUpdate()) {
Map ne = new HashMap();
ne.put(Cnst.NE_REL, id);
rd.put(Cnst.ID_KEY, ne);
}
// 参与唯一约束的字段
Set us = Synt.toTerms(uk);
Set ns = new HashSet(us);
Iterator<String> it = ns.iterator();
while (it.hasNext()) {
String n = it.next();
Object v;
if (nd.containsKey(n)) {
it.remove();
v = nd.get(n);
} else if (nk.equals(n)) {
it.remove();
v = value;
} else {
continue;
}
if (v == null) {
rd.put(n, Synt.mapOf(Cnst.IS_REL, "null"));
} else if (v.equals("")) {
rd.put(n, Synt.mapOf(Cnst.EQ_REL, v));
} else {
rd.put(n, v);
}
}
// 没提供任何值则跳过
if (ns.size() == us.size()) {
return FORCE;
}
// 补充缺的旧的字段值
if (watch.isUpdate() && !ns.isEmpty()) {
Map ud = new HashMap();
ud.put(Cnst.ID_KEY, id);
ud.put(Cnst.RB_KEY, ns);
ud.put(Cnst.RN_KEY, 0);
ActionHelper ah = ActionHelper.newInstance();
ah.setContextData(cd);
ah.setRequestData(ud);
try {
ActionRunner.newInstance(ah, ut).doInvoke();
} catch (HongsException ex) {
throw ex.toExemption();
}
SD: {
Map sd = ah.getResponseData();
if (sd == null) {
break SD;
}
if (sd.containsKey("list")) {
List sl = (List) sd.get("list");
if (sl.isEmpty())
break SD;
sd = (Map) sl.get(00);
} else if (sd.containsKey("info")) {
sd = (Map) sd.get("info");
} else {
break SD;
}
for (Object n : ns) {
Object v = sd.get(n);
if (v == null) {
rd.put(n, Synt.mapOf(Cnst.IS_REL, "null"));
} else if (v.equals("")) {
rd.put(n, Synt.mapOf(Cnst.EQ_REL, v));
} else {
rd.put(n, v);
}
}
}
}
// 执行动作
ActionHelper ah = ActionHelper.newInstance();
ah.setContextData(cd);
ah.setRequestData(rd);
try {
ActionRunner.newInstance(ah, ut).doInvoke();
} catch (HongsException ex) {
throw ex.toExemption();
}
// 对比结果
Map sd = ah.getResponseData();
if (sd == null) {
return FORCE;
}
if (sd.containsKey("list")) {
List list = (List) sd.get("list");
if (list == null || list.isEmpty()) {
return FORCE;
}
} else if (sd.containsKey("info")) {
Map info = (Map) sd.get("info");
if (info == null || info.isEmpty()) {
return FORCE;
}
} else if (sd.containsKey("page")) {
Map page = (Map) sd.get("page");
if (page == null || page.isEmpty()) {
return FORCE;
} else if (page.containsKey("state") && Synt.declare(page.get("pages"), 0) <= 0) {
return FORCE;
} else if (page.containsKey("count") && Synt.declare(page.get("count"), 0) == 0) {
return FORCE;
}
}
throw new Wrong("fore.form.is.not.unique");
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Ordinary method verify.
@Override
public Object verify(Value watch) throws Wrong {
Object value = watch.get();
/**
* 数组和集合要取第一个
* 这样处理就可以适用于 Servlet 的 ParameterMap
*/
if (value instanceof Object[]) {
Object[] a = (Object[]) value;
return a.length > 0 ? a[0] : null;
}
if (value instanceof Collection) {
Collection c = (Collection) value;
Object[] a = c.toArray();
return a.length > 0 ? a[0] : null;
}
/**
* 对象类型的字段要放行
* 至于实际仍是多组取值
* 在这里就没办法分辨了
*/
if (value instanceof Map) {
try {
Object type = getParam("__type__");
if ("json".equals(type) || "object".equals(type)) {
return value;
}
Object kind = FormSet.getInstance().getEnum("__types__").get(type);
if ("form".equals(kind)) {
return value;
}
} catch (HongsException ex) {
throw ex.toExemption();
}
throw new Wrong("fore.form.ordinary");
}
return STAND;
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Remote method buildPost.
/**
* 构建请求实体
*
* 采用键值对的数据结构,
* 当一个键对应多个值时,
* 值可以是数组或者集合.
*
* @param data
* @return
* @throws HongsException
*/
public static HttpEntity buildPost(Map<String, Object> data) throws HongsException {
List<NameValuePair> pair = new ArrayList(data.size());
for (Map.Entry<String, Object> et : data.entrySet()) {
String n = et.getKey();
Object o = et.getValue();
if (o == null) {
// continue;
} else if (o instanceof Object[]) {
// 针对 Servlet 参数格式
for (Object v : (Object[]) o) {
String s = String.valueOf(v);
pair.add(new BasicNameValuePair(n, s));
}
} else if (o instanceof Collection) {
// 针对 WebSocket 的格式
for (Object v : (Collection) o) {
String s = String.valueOf(v);
pair.add(new BasicNameValuePair(n, s));
}
} else {
String s = String.valueOf(o);
pair.add(new BasicNameValuePair(n, s));
}
}
try {
return new UrlEncodedFormEntity(pair, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HongsException(1111, e);
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Link method mergeSQLParams.
/**
* 绑定SQL数据项
* 调用本方法前务必先调用内checkSQLParams
* @param sql
* @param params
* @throws HongsException
*/
public static void mergeSQLParams(StringBuilder sql, List params) throws HongsException {
if (params == null) {
params = new ArrayList();
}
int pos = 0;
int num = 0;
/**
* 填充参数
*/
while ((pos = sql.indexOf("?", pos)) != -1) {
if (num >= params.size()) {
break;
}
/**
* 如果参数是NULL,
* 则直接加入NULL;
* 如果参数是数字,
* 则直接加入数字;
* 如果参数是其他类型,
* 则转换成字符串并加引号.
*/
Object obj = params.get(num);
String str;
if (obj == null) {
str = "NULL";
} else if (obj instanceof Number) {
str = Synt.asString((Number) obj);
} else {
str = quoteValue(obj.toString());
}
sql.replace(pos, pos + 1, str);
pos += str.length() - 1;
num += 1;
}
if (num != params.size()) {
throw new HongsException(1049, "The number of '?' and the number of parameters are inconsistent." + " ?s: " + num + " params: " + params.size() + " SQL: " + sql);
}
}
Aggregations