package examples.filtering;

import com.vtence.molecule.Middleware;
import com.vtence.molecule.Request;
import com.vtence.molecule.Response;
import com.vtence.molecule.WebServer;
import com.vtence.molecule.middlewares.AbstractMiddleware;
import com.vtence.molecule.routing.DynamicRoutes;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.vtence.molecule.http.HttpStatus.UNAUTHORIZED;

public class FilteringExample {

    private final Map<String, String> users = new HashMap<>();

    public FilteringExample() {
        users.put("admin", "admin");
    }

    public void run(WebServer server) throws IOException {
        // An an authentication filter that checks against a map of authorized users
        Middleware authenticate = new AbstractMiddleware() {
            public void handle(Request request, Response response) throws Exception {
                String user = request.parameter("username");
                String password = request.parameter("password");

                String token = users.get(user);
                if (password != null && password.equals(token)) {
                    request.attribute("user", user);
                    // Carry on with the processing chain
                    forward(request, response);
                } else {
                    // Halt request processing
                    response.status(UNAUTHORIZED)
                            .done("Get away!");
                }
            }
        };

        // All requests to /private/... go through the authentication filter
        server.filter("/private", authenticate)
              .start(new DynamicRoutes() {{
                  // This route is private thus requires authentication
                  get("/private/area").to((request, response) ->
                          response.done("Hello, " + request.attribute("user") + "!"));

                  // This route is public
                  get("/hello").to((request, response) ->
                          response.done("Welcome, Guest!"));
              }});
    }

    public static void main(String[] args) throws IOException {
        FilteringExample example = new FilteringExample();
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri() + "/private/area?username=admin&password=admin");
    }
}
@@ -428,134 +428,483 @@
 D;%0A%0A
-public class FilteringExample %7B%0A%0A    private final Map%3CString, String%3E users = new HashMap%3C%3E();%0A%0A    public FilteringExample()
+/**%0A * %3Cp%3E%0A *     This example shows how to filter access to specific resources. We use a custom authentication middleware%0A *     to make sure only authenticated users can access resources under %3Ccode%3E/private%3C/code%3E.%0A * %3C/p%3E%0A */%0Apublic class FilteringExample %7B%0A%0A    /**%0A     * Our user database is a simple hash.%0A     */%0A    private final Map%3CString, String%3E users = new HashMap%3C%3E();%0A%0A    // Let's populate our users base with a single user %22admin%22 with password %22admin%22%0A   
  %7B%0A 
@@ -938,32 +938,71 @@
 admin%22);%0A    %7D%0A%0A
+    public FilteringExample() %7B%0A    %7D%0A%0A
     public void 
@@ -1059,13 +1059,29 @@
  // 
-An an
+We implement a simple
  aut
@@ -1092,22 +1092,26 @@
 ication 
-filter
+middleware
  that ch
@@ -1149,16 +1149,130 @@
 d users%0A
+        // If credentials match, we allow access to the requested resource. If not, we send a 401 - Unauthorized.%0A
         
@@ -1402,32 +1402,113 @@
 ows Exception %7B%0A
+                // We read the username and password from the request parameters%0A
                 
@@ -1728,24 +1728,115 @@
 s(token)) %7B%0A
+                    // Credentials match, store the current user as a request attribute...%0A
             
@@ -1899,17 +1899,26 @@
      // 
-C
+... then c
 arry on 
@@ -2420,14 +2420,18 @@
 vate
+,
  thus 
+it 
 requ
@@ -2644,16 +2644,39 @@
 s public
+, anybody can access it
 %0A       
@@ -3133,8 +3133,9 @@
 %0A    %7D%0A%7D
+%0A

Add javadoc to the filtering example