package examples.locale;

import com.vtence.molecule.WebServer;
import com.vtence.molecule.middlewares.Locales;

import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;

public class LocaleNegotiationExample {

    private final String[] supportedLanguages;

    public LocaleNegotiationExample(String... supportedLanguages) {
        this.supportedLanguages = supportedLanguages;
    }

    public void run(WebServer server) throws IOException {
        // Knowing what languages we support, figure out the best locale to use for each incoming request
        server.add(new Locales(supportedLanguages))
              .start((request, response) -> {
                  Locale locale = request.attribute(Locale.class);
                  response.contentType("text/plain");
                  response.done(
                          "You asked for: " + request.header("accept-language") + "\n" +
                          "We support: " + Arrays.asList(supportedLanguages) + "\n" +
                          "Our default is: " + Locale.getDefault().toLanguageTag() + "\n" +
                          "The best match is: " + locale.toLanguageTag() + "\n"
                  );
              });
    }

    public static void main(String[] args) throws IOException {
        LocaleNegotiationExample example = new LocaleNegotiationExample("en", "en_US", "fr");
        // Run the default web server
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri());
    }
}
@@ -189,96 +189,575 @@
 e;%0A%0A
-public class LocaleNegotiationExample %7B%0A%0A    private final String%5B%5D supportedLanguages;%0A
+/**%0A * %3Cp%3E%0A *     This example demonstrates locale detection from the HTTP %3Ccode%3EAccept-Language%3C/code%3E header.%0A * %3C/p%3E%0A * %3Cp%3E%0A *     We use the %7B@link Locales%7D middleware to detect the user agent locale and figure out the best%0A *     possible locale knowing the application supported locales.%0A * %3C/p%3E%0A *%0A */%0Apublic class LocaleNegotiationExample %7B%0A%0A    private final String%5B%5D supportedLanguages;%0A%0A    /**%0A     * Constructs this example with the list of locales we support%0A     *%0A     * @param supportedLanguages the list of languages the application supports%0A     */
 %0A   
@@ -1145,16 +1145,98 @@
 e) -%3E %7B%0A
+                  // The best possible locale is available as a request attribute%0A
         
@@ -1294,16 +1294,68 @@
 class);%0A
+                  // Set the response to plain text%0A
         
@@ -1433,16 +1433,71 @@
 e.done(%0A
+                          // What the client asked for%0A
         
@@ -1569,32 +1569,88 @@
 uage%22) + %22%5Cn%22 +%0A
+                          // All the locales we support%0A
                 
@@ -1711,32 +1711,92 @@
 uages) + %22%5Cn%22 +%0A
+                          // The application default locale%0A
                 
@@ -1863,32 +1863,147 @@
 eTag() + %22%5Cn%22 +%0A
+                          // The best locale for this request, depending on supported locales and requested locale%0A
                 
@@ -2168,32 +2168,110 @@
 s IOException %7B%0A
+        // We run the example with support for English, US English and French%0A
         LocaleNe
@@ -2535,8 +2535,9 @@
 %0A    %7D%0A%7D
+%0A

Add javadoc to the LocaleNegotiation example