Search in sources :

Example 1 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method testIssue277_2.

@Test
public void testIssue277_2() {
    Context context = Lang.context();
    context.set("math", Maths.class);
    assertEquals(2, El.eval(context, "math.max(1, 2)"));
}
Also used : Context(org.nutz.lang.util.Context) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Example 2 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class AbstractPathView method createContext.

/**
     * 为一次 HTTP 请求,创建一个可以被表达式引擎接受的上下文对象
     * 
     * @param req
     *            HTTP 请求对象
     * @param obj
     *            入口函数的返回值
     * @return 上下文对象
     */
public static Context createContext(HttpServletRequest req, Object obj) {
    Context context = Lang.context();
    // 复制全局的上下文对象
    Object globalContext = Mvcs.getServletContext().getAttribute(Loading.CONTEXT_NAME);
    if (globalContext != null) {
        context.putAll((Context) globalContext);
    }
    // 请求对象的属性列表
    Map<String, Object> req_attr = new HashMap<String, Object>();
    for (Enumeration<String> en = req.getAttributeNames(); en.hasMoreElements(); ) {
        String tem = en.nextElement();
        if (!tem.startsWith("$"))
            req_attr.put(tem, req.getAttribute(tem));
    }
    // 兼容最初的写法
    context.set("a", req_attr);
    context.set("req_attr", req_attr);
    ActionContext ac = Mvcs.getActionContext();
    if (ac != null)
        context.set("pathargs", Mvcs.getActionContext().getPathArgs());
    HttpSession session = Mvcs.getHttpSession(false);
    if (session != null) {
        Map<String, Object> session_attr = new HashMap<String, Object>();
        for (Enumeration<String> en = session.getAttributeNames(); en.hasMoreElements(); ) {
            String tem = en.nextElement();
            session_attr.put(tem, session.getAttribute(tem));
        }
        context.set("session_attr", session_attr);
    }
    // 请求的参数表,需要兼容之前的p.参数, Fix issue 418
    Map<String, String> p = new HashMap<String, String>();
    for (Object o : Lang.enum2collection(req.getParameterNames(), new ArrayList<String>())) {
        String key = (String) o;
        String value = req.getParameter(key);
        p.put(key, value);
        // 以支持直接获取请求参数
        context.set(key, value);
    }
    context.set("p", p);
    Map<String, String> u = new HashMap<String, String>();
    AtMap at = Mvcs.getAtMap();
    if (at != null) {
        for (Object o : at.keys()) {
            String key = (String) o;
            u.put(key, at.get(key));
        }
        context.set("u", u);
    }
    // 加入返回对象
    if (null != obj)
        context.set(ViewProcessor.DEFAULT_ATTRIBUTE, obj);
    return context;
}
Also used : ActionContext(org.nutz.mvc.ActionContext) Context(org.nutz.lang.util.Context) AtMap(org.nutz.mvc.config.AtMap) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) ActionContext(org.nutz.mvc.ActionContext)

Example 3 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class NutLoading method createContext.

protected void createContext(NutConfig config) {
    // 构建一个上下文对象,方便子类获取更多的环境信息
    // 同时,所有 Filter 和 Adaptor 都可以用 ${app.root} 来填充自己
    Context context = Lang.context();
    String appRoot = config.getAppRoot();
    context.set("app.root", appRoot);
    if (log.isDebugEnabled()) {
        log.debugf(">> app.root = %s", appRoot);
    }
    // 载入环境变量
    for (Entry<String, String> entry : System.getenv().entrySet()) context.set("env." + entry.getKey(), entry.getValue());
    // 载入系统变量
    for (Entry<Object, Object> entry : System.getProperties().entrySet()) context.set("sys." + entry.getKey(), entry.getValue());
    if (log.isTraceEnabled()) {
        log.tracef(">>\nCONTEXT %s", Json.toJson(context, JsonFormat.nice()));
    }
    config.getServletContext().setAttribute(Loading.CONTEXT_NAME, context);
}
Also used : Context(org.nutz.lang.util.Context)

Example 4 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class SegmentsTest method test_simple_replace.

@Test
public void test_simple_replace() {
    String ptn = "1${A}2${B}3${C}4";
    Context context = Lang.context();
    context.set("B", "haha");
    String str = Segments.replace(ptn, context);
    assertEquals("1${A}2haha3${C}4", str);
}
Also used : Context(org.nutz.lang.util.Context) Test(org.junit.Test)

Example 5 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method test_uu32_uu64.

@Test
public void test_uu32_uu64() {
    Context ctx = Lang.context();
    El el = new El("uuid()");
    assertEquals(32, el.eval(ctx).toString().length());
    el = new El("uuid(32)");
    assertTrue(26 >= el.eval(ctx).toString().length());
    el = new El("uuid(64)");
    assertTrue(23 >= el.eval(ctx).toString().length());
}
Also used : Context(org.nutz.lang.util.Context) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Aggregations

Context (org.nutz.lang.util.Context)40 Test (org.junit.Test)29 SimpleSpeedTest (org.nutz.el.speed.SimpleSpeedTest)24 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)3 ServletContext (javax.servlet.ServletContext)3 El (org.nutz.el.El)3 Map (java.util.Map)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BigDecimal (java.math.BigDecimal)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 Ignore (org.junit.Ignore)1 ElException (org.nutz.el.ElException)1 Issue293 (org.nutz.el.issue.Issue293)1 Issue303 (org.nutz.el.issue.Issue303)1