Search in sources :

Example 11 with Core

use of io.github.ihongs.Core in project HongsCORE by ihongs.

the class ActionHelper method newInstance.

/**
 * 新建实例
 * 用于使用 ActionRunner 时快速构建请求对象,
 * 可用以上 setXxxxxData 在构建之后设置参数.
 * @return
 */
public static ActionHelper newInstance() {
    Core core = Core.getInstance();
    String name = ActionHelper.class.getName();
    ActionHelper inst = (ActionHelper) core.get(name);
    if (null != inst) {
        return inst.clone();
    }
    return new ActionHelper(null, null, null, null);
}
Also used : Core(io.github.ihongs.Core)

Example 12 with Core

use of io.github.ihongs.Core in project HongsCORE by ihongs.

the class FormSet method getInstance.

public static FormSet getInstance(String name) throws HongsException {
    Core core = Core.getInstance();
    String code = FormSet.class.getName() + ":" + name;
    FormSet inst = (FormSet) core.get(code);
    if (inst == null) {
        inst = new FormSet(name);
        core.set(code, inst);
    }
    return inst;
}
Also used : Core(io.github.ihongs.Core)

Example 13 with Core

use of io.github.ihongs.Core 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 + ");");
        }
    }
}
Also used : ActionHelper(io.github.ihongs.action.ActionHelper) HongsExemption(io.github.ihongs.HongsExemption) Core(io.github.ihongs.Core)

Example 14 with Core

use of io.github.ihongs.Core 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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionHelper(io.github.ihongs.action.ActionHelper) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) Map(java.util.Map) Core(io.github.ihongs.Core) Action(io.github.ihongs.action.anno.Action)

Example 15 with Core

use of io.github.ihongs.Core 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);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) ActionHelper(io.github.ihongs.action.ActionHelper) PrintWriter(java.io.PrintWriter) Core(io.github.ihongs.Core) Cmdlet(io.github.ihongs.cmdlet.anno.Cmdlet)

Aggregations

Core (io.github.ihongs.Core)19 ActionHelper (io.github.ihongs.action.ActionHelper)6 HashMap (java.util.HashMap)6 HongsExemption (io.github.ihongs.HongsExemption)5 Map (java.util.Map)5 LinkedHashMap (java.util.LinkedHashMap)3 HongsException (io.github.ihongs.HongsException)2 File (java.io.File)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 CoreSerial (io.github.ihongs.CoreSerial)1 ActionRunner (io.github.ihongs.action.ActionRunner)1 NaviMap (io.github.ihongs.action.NaviMap)1 Action (io.github.ihongs.action.anno.Action)1 Cmdlet (io.github.ihongs.cmdlet.anno.Cmdlet)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1