Search in sources :

Example 1 with Controller

use of com.jfinal.core.Controller in project jfinal by jfinal.

the class JFinalSession method intercept.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void intercept(Invocation inv) {
    inv.invoke();
    Controller c = inv.getController();
    if (c.getRender() instanceof com.jfinal.render.JsonRender) {
        return;
    }
    HttpSession hs = c.getSession(createSession);
    if (hs != null) {
        Map session = new JFinalSession(hs);
        for (Enumeration<String> names = hs.getAttributeNames(); names.hasMoreElements(); ) {
            String name = names.nextElement();
            session.put(name, hs.getAttribute(name));
        }
        c.setAttr("session", session);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) Controller(com.jfinal.core.Controller) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Controller

use of com.jfinal.core.Controller in project jfinal by jfinal.

the class CacheInterceptor method intercept.

public final void intercept(Invocation inv) {
    Controller controller = inv.getController();
    String cacheName = buildCacheName(inv, controller);
    String cacheKey = buildCacheKey(inv, controller);
    Map<String, Object> cacheData = CacheKit.get(cacheName, cacheKey);
    if (cacheData == null) {
        Lock lock = getLock(cacheName);
        // prevent cache snowslide
        lock.lock();
        try {
            cacheData = CacheKit.get(cacheName, cacheKey);
            if (cacheData == null) {
                inv.invoke();
                cacheAction(cacheName, cacheKey, controller);
                return;
            }
        } finally {
            lock.unlock();
        }
    }
    useCacheDataAndRender(cacheData, controller);
}
Also used : Controller(com.jfinal.core.Controller) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 3 with Controller

use of com.jfinal.core.Controller in project jfinal by jfinal.

the class I18nInterceptor method intercept.

/**
	 * 1: use the locale from request para if exists. change the locale write to the cookie
	 * 2: use the locale from cookie para if exists.
	 * 3: use the default locale
	 * 4: use setAttr(resName, resObject) pass Res object to the view.
	 */
public void intercept(Invocation inv) {
    Controller c = inv.getController();
    String localeParaName = getLocaleParaName();
    String locale = c.getPara(localeParaName);
    if (StrKit.notBlank(locale)) {
        // change locale, write cookie
        c.setCookie(localeParaName, locale, Const.DEFAULT_I18N_MAX_AGE_OF_COOKIE);
    } else {
        // get locale from cookie and use the default locale if it is null
        locale = c.getCookie(localeParaName);
        if (StrKit.isBlank(locale))
            locale = I18n.defaultLocale;
    }
    inv.invoke();
    if (isSwitchView) {
        switchView(locale, c);
    } else {
        Res res = I18n.use(getBaseName(), locale);
        c.setAttr(getResName(), res);
    }
}
Also used : Controller(com.jfinal.core.Controller)

Example 4 with Controller

use of com.jfinal.core.Controller in project jfinal by jfinal.

the class Restful method intercept.

/**
	 * add  edit 无需处理
	 * 
	 * GET		/user			--->	index
	 * GET		/user/id		--->	show  
	 * POST		/user			--->	save	
	 * PUT		/user/id		--->	update
	 * DELECT	/user/id		--->	delete
	 */
public void intercept(Invocation inv) {
    // 阻止 JFinal 原有规则 action 请求
    Controller controller = inv.getController();
    Boolean isRestfulForward = controller.getAttr(isRestfulForwardKey);
    String methodName = inv.getMethodName();
    if (set.contains(methodName) && isRestfulForward == null) {
        inv.getController().renderError(404);
        return;
    }
    if (isRestfulForward != null && isRestfulForward) {
        inv.invoke();
        return;
    }
    String controllerKey = inv.getControllerKey();
    String method = controller.getRequest().getMethod().toUpperCase();
    String urlPara = controller.getPara();
    if ("GET".equals(method)) {
        if (urlPara != null && !"edit".equals(methodName)) {
            controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
            controller.forwardAction(controllerKey + "/show/" + urlPara);
            return;
        }
    } else if ("POST".equals(method)) {
        controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
        controller.forwardAction(controllerKey + "/save");
        return;
    } else if ("PUT".equals(method)) {
        controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
        controller.forwardAction(controllerKey + "/update/" + urlPara);
        return;
    } else if ("DELETE".equals(method)) {
        controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
        controller.forwardAction(controllerKey + "/delete/" + urlPara);
        return;
    }
    inv.invoke();
}
Also used : Controller(com.jfinal.core.Controller)

Aggregations

Controller (com.jfinal.core.Controller)4 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 HttpSession (javax.servlet.http.HttpSession)1