package examples.middleware;

import com.vtence.molecule.*;
import com.vtence.molecule.middlewares.AbstractMiddleware;

import java.io.IOException;

public class CustomMiddlewareExample {

    public void run(WebServer server) throws IOException {

        // An example of performing some work before handling control to the next or application
        Middleware getFirefox = new AbstractMiddleware() {
            public void handle(Request request, Response response) throws Exception {
                // Tell IE users to get Firefox
                String userAgent = request.header("User-Agent");
                if (userAgent != null && userAgent.contains("MSIE")) {
                    response.redirectTo("http://www.mozilla.org").done();
                } else {
                    // Hand over control to next application in the stack
                    forward(request, response);
                }
            }
        };

        // An example of performing additional work after getting control back
        // (there's already a middleware for that, btw)
        Middleware contentLengthHeader = new AbstractMiddleware() {
            public void handle(Request request, Response response) throws Exception {
                forward(request, response).whenSuccessful(resp -> {
                    // Set content length header on the response
                    resp.contentLength(resp.size());
                });
            }
        };


        // A simple hello world application
        Application helloWorld = (request, response) ->
                response.contentType("text/html").done("<html><body>Hello, World</body></html>");

        // Deploy middlewares first, followed by our application
        server.add(getFirefox)
              .add(contentLengthHeader)
              .start(helloWorld);
    }

    public static void main(String[] args) throws IOException {
        CustomMiddlewareExample example = new CustomMiddlewareExample();
        // Run the default web server
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri());
    }
}
@@ -54,99 +54,872 @@
 ule.
-*;%0Aimport com.vtence.molecule.middlewares.AbstractMiddleware;%0A%0Aimport java.io.IOException;%0A
+Application;%0Aimport com.vtence.molecule.Middleware;%0Aimport com.vtence.molecule.Request;%0Aimport com.vtence.molecule.Response;%0Aimport com.vtence.molecule.WebServer;%0Aimport com.vtence.molecule.middlewares.AbstractMiddleware;%0A%0Aimport java.io.IOException;%0A%0A/**%0A * %3Cp%3E%0A *     This example demonstrates how to write custom middlewares to add to the processing pipeline. %3Cbr%3E%0A *     We demonstrate both performing some work before handling control to processing pipeline%0A *     as well as performing additional work once the processing pipeline completes.%0A * %3C/p%3E%0A * %3Cp%3E%0A *      Our first middleware reads the %3Ccode%3EUser-Agent%3C/code%3E HTTP header value and%0A *      redirects IE users to the Mozilla web site.%0A *      %3Cbr%3E%0A*       The second second middleware calculates the response%0A *      size to add a %3Ccode%3EContent-Length%3C/code%3E HTTP header to the response.%0A * %3C/p%3E%0A */
 %0Apub
@@ -1014,17 +1014,16 @@
 ption %7B%0A
-%0A
         
@@ -1021,37 +1021,38 @@
 %0A        // 
-An example of
+To demonstrate
  performing 
@@ -1096,27 +1096,116 @@
 the 
-next or application
+processing pipeline,%0A        // we read the User-Agent header and redirect IE users to the Mozilla web site.
 %0A   
@@ -1526,24 +1526,154 @@
 (%22MSIE%22)) %7B%0A
+                    // Short-circuit the processing pipeline and redirect our user%0A                    // to the Mozilla web site%0A
             
@@ -1940,21 +1940,22 @@
  // 
-An example of
+To demonstrate
  per
@@ -2004,16 +2004,43 @@
 rol back
+, we set the Content-Length
 %0A       
@@ -2047,51 +2047,130 @@
  // 
-(
+header on 
 the
+ 
 re
-'s already a middleware for that, btw
+sponse%0A        // (for a more capable version of this middleware, check the ContentLengthHeader middleware
 )%0A  
@@ -2313,32 +2313,162 @@
 ows Exception %7B%0A
+                // Forward the request for processing, the perform additional work when the response%0A                // completes%0A
                 
@@ -2546,16 +2546,20 @@
  // Set 
+the 
 content 
@@ -3388,8 +3388,9 @@
 %0A    %7D%0A%7D
+%0A

Add javadoc to the CustomMiddleware example