EXTCDI-287 store noScript decision in a Cookie

This makes the handling for a user much easier if he has
* an EAR with multiple WebApps (he previously had to 
  disable it for each webapp)
* a user got session timeouts and visits again later 


git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/cdi/trunk@1338308 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/jee-modules/jsf-module/api/pom.xml b/jee-modules/jsf-module/api/pom.xml
index 1e1f831..a18e503 100644
--- a/jee-modules/jsf-module/api/pom.xml
+++ b/jee-modules/jsf-module/api/pom.xml
@@ -39,10 +39,16 @@
         </dependency>
 
         <dependency>
-           <groupId>org.apache.geronimo.specs</groupId>
-           <artifactId>geronimo-el_1.0_spec</artifactId>
-           <scope>provided</scope>
-       </dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-el_1.0_spec</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-servlet_2.5_spec</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
     </dependencies>
 
     <build>
diff --git a/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jsf/api/config/ClientConfig.java b/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jsf/api/config/ClientConfig.java
index 35f88f1..b716be5 100644
--- a/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jsf/api/config/ClientConfig.java
+++ b/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jsf/api/config/ClientConfig.java
@@ -27,6 +27,9 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Serializable;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import java.util.Map;
 
 /**
@@ -44,7 +47,10 @@
     private static final long serialVersionUID = 581351549574404793L;
     public static final String BODY_ATTRIBUTES_PLACEHOLDER = "$$bodyAttributes$$";
 
-    private boolean javaScriptEnabled = true;
+    /** We will set a cookie with this very name if a noscript link got clicked by the user */
+    public static final String COOKIE_NAME_NOSCRIPT_ENABLED = "mfNoScriptEnabled";
+
+    private volatile Boolean javaScriptEnabled = null;
 
     protected String windowHandlerHtml;
 
@@ -62,7 +68,42 @@
      */
     public boolean isJavaScriptEnabled()
     {
-        return this.javaScriptEnabled;
+        if (javaScriptEnabled == null)
+        {
+            synchronized(this)
+            {
+                // double lock checking idiom on volatile variable works since java5
+                if (javaScriptEnabled == null)
+                {
+                    // no info means that it is default -> true
+                    javaScriptEnabled = Boolean.TRUE;
+
+                    FacesContext facesContext = FacesContext.getCurrentInstance();
+                    if (facesContext != null)
+                    {
+                        Object r = facesContext.getExternalContext().getRequest();
+                        if (r instanceof HttpServletRequest)
+                        {
+                            HttpServletRequest request = (HttpServletRequest) r;
+                            Cookie[] cookies = request.getCookies();
+
+                            if (cookies != null && cookies.length > 0)
+                            {
+                                for(Cookie cookie : cookies)
+                                {
+                                    if (cookie.getName().equalsIgnoreCase(COOKIE_NAME_NOSCRIPT_ENABLED))
+                                    {
+                                        javaScriptEnabled = Boolean.parseBoolean(cookie.getValue());
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+        }
+        return javaScriptEnabled;
     }
 
     /**
@@ -74,6 +115,20 @@
     public void setJavaScriptEnabled(boolean javaScriptEnabled)
     {
         this.javaScriptEnabled = javaScriptEnabled;
+
+        // and now also store this information inside a cookie!
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        if (facesContext != null)
+        {
+            Object r = facesContext.getExternalContext().getResponse();
+            if (r instanceof HttpServletResponse)
+            {
+                Cookie cookie = new Cookie(COOKIE_NAME_NOSCRIPT_ENABLED, "" + javaScriptEnabled);
+                cookie.setPath("/"); // for all the server
+                HttpServletResponse response = (HttpServletResponse) r;
+                response.addCookie(cookie);
+            }
+        }
     }
 
     /**