package examples.cookies;

import com.vtence.molecule.WebServer;
import com.vtence.molecule.http.Cookie;
import com.vtence.molecule.lib.CookieJar;
import com.vtence.molecule.middlewares.Cookies;
import com.vtence.molecule.routing.DynamicRoutes;

import java.io.IOException;

public class CookiesExample {

    public void run(WebServer server) throws IOException {
        // Enable cookies
        server.add(new Cookies())
              .start(new DynamicRoutes() {{

                  get("/").to((request, response) -> {
                      CookieJar cookies = CookieJar.get(request);
                      // Read client cookie
                      Cookie customer = cookies.get("customer");
                      response.done("Welcome, " + valueOf(customer));
                  });

                  get("/weapon").to((request, response) -> {
                      CookieJar cookies = CookieJar.get(request);
                      // Send back a cookie to the client
                      cookies.add("weapon", "rocket launcher").path("/ammo");
                      response.done();
                  });

                  get("/ammo").to((request, response) -> {
                      CookieJar cookies = CookieJar.get(request);
                      // Refresh client cookies max age
                      cookies.add("ammo", "riding rocket").path("/ammo").maxAge(30);
                      response.done();
                  });

                  get("/backfire").to((request, response) -> {
                      CookieJar cookies = CookieJar.get(request);
                      // Expire client cookie
                      cookies.discard("weapon").path("/ammo");
                      response.done();
                  });
              }});
    }

    private String valueOf(Cookie cookie) {
        return cookie != null ? cookie.value() : null;
    }

    public static void main(String[] args) throws IOException {
        CookiesExample example = new CookiesExample();
        // Run the default web server
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri());
    }
}
@@ -272,202 +272,1179 @@
 n;%0A%0A
-public class CookiesExample %7B%0A%0A    public void run(WebServer server) throws IOException %7B%0A        // Enable cookies%0A        server.add(new Cookies())%0A              .start(new DynamicRoutes() %7B%7B%0A
+/**%0A * %3Cp%3E%0A *     In this example we demonstrate reading and writing cookies. We use an example adapted from%0A *     the RFC 2695 (HTTP State Management mechanism - section 4).%0A * %3C/p%3E%0A * %3Cp%3E%0A *     We use the %7B@link Cookies%7D middleware to enable cookies support. This middleware creates a %7B@link CookieJar%7D%0A *     and makes it available as a request attribute.%0A *     %3Cbr%3E%0A *     The cookie jar is initially filled with client cookies.%0A *     After response processing, only new, modified or expired cookies in the jar are written back to the client.%0A * %3C/p%3E%0A */%0Apublic class CookiesExample %7B%0A%0A    public void run(WebServer server) throws IOException %7B%0A        // Enable cookies with the Cookies middleware. It creates a CookieJar, populates it with client cookies%0A        // and makes it available a request attribute.%0A        server.add(new Cookies())%0A              // We create a simple set of routes to demonstrate various operations on cookies.%0A              .start(new DynamicRoutes() %7B%7B%0A%0A                  // A GET to / will read the 'customer' client cookie%0A                  // This will not write any cookie back to client, since we're only reading from the jar
 %0A   
@@ -1591,22 +1591,26 @@
 // Read 
-client
+'customer'
  cookie%0A
@@ -1759,32 +1759,234 @@
            %7D);%0A%0A
+                  // A GET to /weapon will send back the 'weapon' cookie for path /ammo to Will E. Coyote,%0A                  // our client. This cookie if valid for the duration of the browser session.%0A
                 
@@ -2125,40 +2125,125 @@
  // 
-Send back a cookie to the client
+Will E. Coyote acquires a rocket launcher%0A                      // Send back a 'weapon' session cookie for path /ammo
 %0A   
@@ -2371,32 +2371,207 @@
            %7D);%0A%0A
+                  // A GET to /ammo sends a 'ammo' cookie for path /ammo which is set to expire after 30s.%0A                  // It the cookie already exists, it is refreshed.%0A
                 
@@ -2708,38 +2708,122 @@
  // 
-Refresh client cookies
+Will. E. Coyote purchases a riding rocket%0A                      // Set or refresh the 'ammo' cookie with a
  max age
 %0A   
@@ -2818,16 +2818,23 @@
  max age
+ of 30s
 %0A       
@@ -2965,32 +2965,111 @@
            %7D);%0A%0A
+                  // A GET to /backfire will expire any existing 'ammo' cookie%0A
                 
@@ -3210,21 +3210,85 @@
  // 
-Expire client
+Riding rocket backfires and explodes%0A                      // Expire 'weapon'
  coo
@@ -3853,8 +3853,9 @@
 %0A    %7D%0A%7D
+%0A

Add javadoc to our cookies example