use of io.github.ihongs.action.ActionHelper in project HongsCORE by ihongs.
the class TitlesHelper method addForks.
/**
* 通过调用关联动作来补全名称
* @param ls
* @param fc
* @param fn
* @throws HongsException
*/
protected void addForks(List<Object[]> ls, Map fc, String fn) throws HongsException {
String at = (String) fc.get("data-at");
String vk = (String) fc.get("data-vk");
String tk = (String) fc.get("data-tk");
if (at == null || at.isEmpty()) {
String c = (String) fc.get("conf");
String f = (String) fc.get("form");
at = c + "/" + f + "/search";
}
if (vk == null || vk.isEmpty()) {
vk = Cnst.ID_KEY;
}
if (tk == null || tk.isEmpty()) {
tk = "name";
}
Object[] vk2 = Dict.splitKeys(vk);
Object[] tk2 = Dict.splitKeys(tk);
// 映射关系
Map<Object, Object[]> lm = new HashMap();
for (Object[] lx : ls) {
lm.put(lx[0], lx);
}
Set li = lm.keySet();
// 查询结构
Map cd = new HashMap();
Map rd = new HashMap();
Set rb = new HashSet();
rd.put(Cnst.RB_KEY, rb);
rd.put(Cnst.RN_KEY, 1024);
rb.add(vk);
rb.add(tk);
// 获取结果
ActionHelper ah = ActionHelper.newInstance();
ah.setContextData(cd);
ah.setRequestData(rd);
ActionRunner ar = ActionRunner.newInstance(ah, at);
/**
* Lucene 单个条件的数量无法超过 1024
* 故需拆成片段
* 分批进行查询
*/
List l = new ArrayList(li);
int k = l.size();
int j = 0, i = 0;
while (j < k) {
j = i + 1024;
if (j > k) {
j = k;
}
rd.put(Cnst.ID_KEY, l.subList(i, j));
/**/
i = j;
ar.doInvoke();
// 整合数据
Map sd = ah.getResponseData();
List<Map> lz = (List) sd.get("list");
if (lz != null)
for (Map ro : lz) {
String lv = Dict.getValue(ro, "", vk2);
String lt = Dict.getValue(ro, "", tk2);
Object[] lx = lm.get(lv);
if (null != lx) {
lx[1] = lt;
}
}
}
}
use of io.github.ihongs.action.ActionHelper 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.action.ActionHelper in project HongsCORE by ihongs.
the class MoreAction method more.
@Action("__main__")
public void more(ActionHelper helper) {
helper.reply("");
HttpServletRequest req = helper.getRequest();
HttpServletResponse rsp = helper.getResponse();
Map re0 = helper.getRequestData();
Map rs0 = helper.getResponseData();
Core core = Core.getInstance();
Wrap wrap = new Wrap(helper);
String act = null;
try {
act = Core.ACTION_NAME.get();
core.put(ActionHelper.class.getName(), wrap);
more(wrap, null, req, rsp, re0, rs0, null, 0);
} finally {
Core.ACTION_NAME.set(act);
core.put(ActionHelper.class.getName(), helper);
}
helper.reply(rs0);
}
use of io.github.ihongs.action.ActionHelper in project HongsCORE by ihongs.
the class Access method call.
@Cmdlet("call")
public static void call(String[] args) throws HongsException {
Map<String, Object> opts;
opts = CmdletHelper.getOpts(args, "request:s", "context:s", "session:s", "cookies:s", "!A");
args = (String[]) opts.get("");
if (args.length == 0) {
CmdletHelper.ERR.get().println("Usage: ACTION_NAME [--request DATA] [--cookies DATA] [--session DATA] [--context DATA]\r\n\t" + "DATA can be JSON or URL search string.");
return;
}
// 请求参数
ActionHelper helper = new ActionHelper(data((String) opts.get("request")), data((String) opts.get("context")), data((String) opts.get("session")), data((String) opts.get("cookies")));
// 输出管道
PrintStream ps = CmdletHelper.OUT.get();
PrintWriter pw = new PrintWriter(ps);
helper.updateOutput(ps, pw);
// 将新动作助手对象放入全局以便跨层读取
String cn = ActionHelper.class.getName();
Core co = Core.getInstance();
Object ah = co.get(cn);
try {
co.set(cn, helper);
ActionRunner.newInstance(helper, args[0]).doActing();
helper.flush();
ps.println();
} finally {
if (null != ah) {
co.set(cn, ah);
} else {
co.unset(cn);
}
}
}
use of io.github.ihongs.action.ActionHelper in project HongsCORE by ihongs.
the class ApisAction method service.
@Override
protected void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
String act = ActionDriver.getRecentPath(req);
if (act == null || act.length() == 0) {
rsp.sendError(HttpServletResponse.SC_NOT_FOUND, "URI can not be empty");
return;
}
int dot;
dot = act.lastIndexOf(".");
act = act.subSequence(0, dot) + Cnst.ACT_EXT;
ActionHelper hlpr = ActionDriver.getActualCore(req).got(ActionHelper.class);
Map reqs = hlpr.getRequestData();
Object _mod = reqs.remove(modeKey);
Object _dat = reqs.remove(dataKey);
// 数据转换策略
Set mode = null;
if (_mod != null) {
try {
mode = trnsConv(_mod);
} catch (ClassCastException e) {
hlpr.error(400, "Can not parse value for " + modeKey);
return;
}
}
// 请求数据封装
Map data = null;
if (_dat != null) {
try {
data = trnsData(_dat);
} catch (ClassCastException e) {
hlpr.error(400, "Can not parse value for " + dataKey);
return;
}
}
// 额外请求数据
if (data != null) {
reqs.putAll(data);
}
// 转发动作处理, 获取响应数据
req.getRequestDispatcher(act).include(req, rsp);
Map resp = hlpr.getResponseData();
if (resp == null) {
return;
}
// 整理响应数据
if (mode != null) {
if (!mode.isEmpty()) {
convData(resp, mode);
}
if (mode.contains("wrap")) {
wrapData(resp);
}
if (mode.contains("scok")) {
rsp.setStatus(HttpServletResponse.SC_OK);
}
}
}
Aggregations