Search in sources :

Example 1 with Session

use of com.blade.mvc.http.Session in project tale by otale.

the class TaleUtils method getLoginUser.

/**
 * 返回当前登录用户
 */
public static Users getLoginUser() {
    Session session = com.blade.mvc.WebContext.request().session();
    if (null == session) {
        return null;
    }
    Users user = session.attribute(TaleConst.LOGIN_SESSION_KEY);
    return user;
}
Also used : Users(com.tale.model.entity.Users) Session(com.blade.mvc.http.Session)

Example 2 with Session

use of com.blade.mvc.http.Session in project blade by biezhi.

the class SessionHandler method createSession.

public Session createSession(Request request) {
    Session session = getSession(request);
    long now = Instant.now().getEpochSecond();
    if (null == session) {
        long expired = now + timeout;
        session = ReflectKit.newInstance(blade.sessionType());
        session.id(UUID.UU32());
        session.created(now);
        session.expired(expired);
        sessionManager.createSession(session);
        return session;
    } else {
        if (session.expired() < now) {
            sessionManager.destorySession(session);
        } else {
            // renewal
            long expired = now + timeout;
            session.expired(expired);
        }
    }
    return session;
}
Also used : Session(com.blade.mvc.http.Session)

Example 3 with Session

use of com.blade.mvc.http.Session in project blade by biezhi.

the class DefaultEngine method render.

@Override
public void render(ModelAndView modelAndView, Writer writer) throws TemplateException {
    String view = modelAndView.getView();
    String body;
    String viewPath;
    if (Const.CLASSPATH.endsWith(PATH_SEPARATOR)) {
        viewPath = Const.CLASSPATH + TEMPLATE_PATH + PATH_SEPARATOR + view;
    } else {
        viewPath = Const.CLASSPATH + PATH_SEPARATOR + TEMPLATE_PATH + PATH_SEPARATOR + view;
    }
    try {
        if (view.startsWith("jar:")) {
            String jarPath = view.substring(4);
            InputStream input = DefaultEngine.class.getResourceAsStream(jarPath);
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            body = IOKit.readToString(reader);
        } else {
            if (BladeKit.runtimeIsJAR()) {
                viewPath = PATH_SEPARATOR + TEMPLATE_PATH + PATH_SEPARATOR + view;
                InputStream in = getClass().getResourceAsStream(viewPath);
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                body = IOKit.readToString(reader);
            } else {
                body = IOKit.readToString(viewPath);
            }
        }
        Request request = WebContext.request();
        Map<String, Object> attributes = new HashMap<>();
        Map<String, Object> reqAttrs = request.attributes();
        attributes.putAll(reqAttrs);
        attributes.putAll(modelAndView.getModel());
        Session session = request.session();
        if (null != session) {
            attributes.putAll(session.attributes());
        }
        String result = BladeTemplate.template(body, attributes).fmt();
        writer.write(result);
    } catch (Exception e) {
        log.warn("View path is: {}", viewPath);
        throw new TemplateException(e);
    } finally {
        IOKit.closeQuietly(writer);
    }
}
Also used : HashMap(java.util.HashMap) TemplateException(com.blade.exception.TemplateException) Request(com.blade.mvc.http.Request) TemplateException(com.blade.exception.TemplateException) Session(com.blade.mvc.http.Session)

Aggregations

Session (com.blade.mvc.http.Session)3 TemplateException (com.blade.exception.TemplateException)1 Request (com.blade.mvc.http.Request)1 Users (com.tale.model.entity.Users)1 HashMap (java.util.HashMap)1