Package examples.session

Source Code of examples.session.SessionExample

package examples.session;

import com.vtence.molecule.Application;
import com.vtence.molecule.Request;
import com.vtence.molecule.Response;
import com.vtence.molecule.Session;
import com.vtence.molecule.WebServer;
import com.vtence.molecule.middlewares.CookieSessionTracker;
import com.vtence.molecule.routing.DynamicRoutes;
import com.vtence.molecule.session.SecureIdentifierPolicy;
import com.vtence.molecule.session.SessionPool;
import com.vtence.molecule.lib.Clock;
import com.vtence.molecule.lib.SystemClock;

import java.io.IOException;

public class SessionExample {

    private final Clock clock;

    private CookieSessionTracker sessionTracker;

    public SessionExample(Clock clock) {
        this.clock = clock;
    }

    public void expireAfter(int seconds) {
        this.sessionTracker.expireAfter(seconds);
    }

    public void run(WebServer server) throws IOException {
        // Track sessions using a cookie strategy and an in-memory session pool
        sessionTracker = new CookieSessionTracker(new SessionPool(new SecureIdentifierPolicy(), clock));
        server.add(sessionTracker)
              .start(new DynamicRoutes() {{
                         map("/").to(new Application() {
                             public void handle(Request request, Response response) throws Exception {
                                 Session session = Session.get(request);
                                 String username = session.contains("username") ? session.<String>get("username") : "Guest";
                                 response.body("Hello, " + username);
                             }
                         });

                         post("/login").to(new Application() {
                             public void handle(Request request, Response response) throws Exception {
                                 String username = request.parameter("username");
                                 Session session = Session.get(request);
                                 session.put("username", username);
                                 response.redirectTo("/");
                             }
                         });

                         delete("/logout").to(new Application() {
                             public void handle(Request request, Response response) throws Exception {
                                 Session session = Session.get(request);
                                 session.invalidate();
                                 response.redirectTo("/");
                             }
                         });
                     }}
              );
    }

    public static void main(String[] args) throws IOException {
        SessionExample example = new SessionExample(new SystemClock());
        // Run the default web server
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri());
    }
}
TOP

Related Classes of examples.session.SessionExample

TOP
Copyright © 2018 www.massapi.com. 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.