package com.firefly.example.http.hello;

import com.firefly.$;
import com.firefly.codec.http2.model.Cookie;
import com.firefly.server.http2.HTTP2ServerBuilder;
import com.firefly.server.http2.router.handler.session.HTTPSessionConfiguration;
import com.firefly.server.http2.router.handler.session.LocalHTTPSessionHandler;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.Phaser;

/**
 * @author Pengtao Qiu
 */
public class LocalSessionDemo {
    public static void main(String[] args) throws Exception {
        String host = "localhost";
        int port = 8081;
        String uri = "https://" + host + ":" + port;

        int maxGetSession = 3;
        Phaser phaser = new Phaser(1 + maxGetSession + 1);

        HTTP2ServerBuilder httpsServer = $.httpsServer();
        LocalHTTPSessionHandler sessionHandler = new LocalHTTPSessionHandler(new HTTPSessionConfiguration());
        httpsServer.router().path("*").handler(sessionHandler)
                   .router().post("/session/:name")
                   .handler(ctx -> {
                       String name = ctx.getRouterParameter("name");
                       System.out.println("the path param -> " + name);
                       HttpSession session = ctx.getSession(true);
                       session.setAttribute(name, "bar");
                       // 1 second later, the session will expire
                       session.setMaxInactiveInterval(1);
                       ctx.end("create session success");
                   })
                   .router().get("/session/:name")
                   .handler(ctx -> {
                       HttpSession session = ctx.getSession();
                       if (session != null) {
                           ctx.end("session value is " + session.getAttribute("foo"));
                       } else {
                           ctx.end("session is invalid");
                       }
                   })
                   .listen(host, port);

        List<Cookie> c
                = $.httpsClient().post(uri + "/session/foo").submit()
                   .thenApply(res -> {
                       List<Cookie> cookies = res.getCookies();
                       System.out.println(res.getStatus());
                       System.out.println(cookies);
                       System.out.println(res.getStringBody());
                       return cookies;
                   })
                   .thenApply(cookies -> {
                       for (int i = 0; i < maxGetSession; i++) {
                           $.httpsClient().get(uri + "/session/foo").cookies(cookies).submit()
                            .thenAccept(res2 -> {
                                String sessionFoo = res2.getStringBody();
                                System.out.println(sessionFoo);
                                phaser.arrive();
                            });
                       }
                       return cookies;
                   }).get();

        $.thread.sleep(3000L); // the session expired
        $.httpsClient().get(uri + "/session/foo").cookies(c).submit()
         .thenAccept(res -> {
             String sessionFoo = res.getStringBody();
             System.out.println(sessionFoo);
             phaser.arrive();
         });

        phaser.arriveAndAwaitAdvance();
        httpsServer.stop();
        $.httpsClient().stop();
        sessionHandler.stop();
    }
}

@@ -739,17 +739,17 @@
 ssion + 
-1
+2
 );%0A%0A    
@@ -2018,32 +2018,16 @@
 ookie%3E c
-%0A               
  = $.htt
@@ -2084,32 +2084,39 @@
                 
+       
 .thenApply(res -
@@ -2134,32 +2134,39 @@
                 
+  
+     
 List%3CCookie%3E coo
@@ -2186,24 +2186,31 @@
 tCookies();%0A
+       
             
@@ -2272,32 +2272,39 @@
                 
+       
 System.out.print
@@ -2331,32 +2331,39 @@
                 
+     
+  
 System.out.print
@@ -2402,32 +2402,39 @@
                 
+       
 return cookies;%0A
@@ -2444,35 +2444,49 @@
                 
+   
-%7D)%0A
+    %7D)%0A       
                 
@@ -2535,16 +2535,23 @@
         
+ 
+      
 for (int
@@ -2580,24 +2580,31 @@
 ion; i++) %7B%0A
+       
             
@@ -2706,32 +2706,39 @@
                 
+       
 .thenAccept(res2
@@ -2767,32 +2767,39 @@
                 
+    
+   
 String sessionFo
@@ -2816,32 +2816,39 @@
 etStringBody();%0A
+       
                 
@@ -2919,32 +2919,39 @@
                 
+       
 phaser.arrive();
@@ -2943,32 +2943,39 @@
 haser.arrive();%0A
+       
                 
@@ -3005,34 +3005,48 @@
                 
-%7D%0A
+       %7D%0A       
                 
@@ -3091,15 +3091,266 @@
     
-%7D).get(
+       %7D).get();%0A%0A        $.httpsClient().get(uri + %22/resource-not-found%22).submit()%0A         .thenAccept(res -%3E %7B%0A             System.out.println(res.getStatus());%0A             System.out.println(res.getStringBody());%0A             phaser.arrive();%0A         %7D
 );%0A%0A

test default error response handler SPI