Package com.vtence.molecule

Examples of com.vtence.molecule.Session


    public int size() {
        return sessions.size();
    }

    public Session load(String id) {
        Session session = sessions.get(id);
        if (session == null || !validate(session)) return null;
        Session data = makeSession(id, session);
        listener.sessionLoaded(id);
        return data;
    }
View Full Code Here


    }

    public String save(Session data) {
        if (data.invalid()) throw new IllegalStateException("Session invalidated");
        String sid = sessionId(data);
        Session session = makeSession(sid, data);
        Date now = clock.now();
        session.updatedAt(now);
        sessions.put(sid, session);
        if (sid.equals(data.id())) {
            listener.sessionSaved(sid);
        } else {
            session.createdAt(now);
            listener.sessionCreated(sid);
        }
        return sid;
    }
View Full Code Here

    private String sessionId(Session data) {
        return data.exists() && contains(data.id()) ? data.id() : policy.generateId();
    }

    private Session makeSession(String sid, Session data) {
        Session session = new Session(sid);
        session.merge(data);
        session.maxAge(data.maxAge());
        session.updatedAt(data.updatedAt());
        session.createdAt(data.createdAt());
        return session;
    }
View Full Code Here

        this.expireAfter = seconds;
        return this;
    }

    public void handle(Request request, Response response) throws Exception {
        Session session = prepareSession(request);
        try {
            forward(request, response);
            commitSession(request, response, session);
        } finally {
            Session.unset(request);
View Full Code Here

            Session.unset(request);
        }
    }

    private Session prepareSession(Request request) {
        Session session = acquireSession(request);
        session.maxAge(expireAfter);
        Session.set(request, session);
        return session;
    }
View Full Code Here

        return session;
    }

    private Session acquireSession(Request request) {
        String id = sessionId(request);
        if (id == null) return new Session();
        Session session = store.load(id);
        return session != null ? session : new Session();
    }
View Full Code Here

    MockResponse response = new MockResponse();

    @Before public void
    stubSessionStore() {
        context.checking(new Expectations() {{
            allowing(store).load("existing"); will(returnValue(new Session("existing")));
            allowing(store).load("expired"); will(returnValue(null));
        }});
    }
View Full Code Here

    @Test public void
    tracksExistingSessionsUsingACookieAndSavesSessionIfModified() throws Exception {
        tracker.connectTo(incrementCounter());

        Session clientSession = store.load("existing");
        clientSession.put("counter", 1);
        context.checking(new Expectations() {{
            oneOf(store).save(with(sessionWithId("existing")));
            will(returnValue("existing"));
        }});
View Full Code Here

    }

    private Application echoSessionId() {
        return new Application() {
            public void handle(Request request, Response response) throws Exception {
                Session session = Session.get(request);
                response.body("Session: " + session.id());
            }
        };
    }
View Full Code Here

    }

    private Application incrementCounter() {
        return new Application() {
            public void handle(Request request, Response response) throws Exception {
                Session session = Session.get(request);
                Integer counter = session.contains("counter") ? session.<Integer>get("counter") : 0;
                session.put("counter", counter++);
                response.body("Counter: " + counter);
            }
        };
    }
View Full Code Here

TOP

Related Classes of com.vtence.molecule.Session

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.