You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2022/03/09 14:39:30 UTC

[myfaces-tobago] branch tobago-5.x updated: feat: Make tobago.theme cookie configurable

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch tobago-5.x
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/tobago-5.x by this push:
     new b585472  feat: Make tobago.theme cookie configurable
b585472 is described below

commit b5854724f06206deef986a70970ad51f00b6d2a7
Author: Udo Schnurpfeil <ud...@irian.eu>
AuthorDate: Wed Mar 9 15:34:26 2022 +0100

    feat: Make tobago.theme cookie configurable
    
    issue: TOBAGO-2118
---
 .../org/apache/myfaces/tobago/config/TobagoConfig.java   | 15 +++++++++++++++
 .../org/apache/myfaces/tobago/context/TobagoContext.java |  5 +++--
 .../tobago/internal/config/TobagoConfigFragment.java     |  9 +++++++++
 .../tobago/internal/config/TobagoConfigMerger.java       |  5 +++++
 .../tobago/internal/config/TobagoConfigParser.java       |  6 ++++++
 .../tobago/internal/renderkit/renderer/PageRenderer.java |  3 ++-
 .../apache/myfaces/tobago/internal/util/CookieUtils.java |  6 +++---
 .../apache/myfaces/tobago/config/tobago-config-5.1.xsd   |  7 +++++++
 .../internal/config/TobagoConfigParserUnitTest.java      | 16 ++++++++++++++++
 tobago-core/src/test/resources/tobago-config-5.1.xml     |  1 +
 .../src/main/webapp/WEB-INF/tobago-config.xml            |  1 +
 11 files changed, 68 insertions(+), 6 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/config/TobagoConfig.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/config/TobagoConfig.java
index 0e48a95..0a74f32 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/config/TobagoConfig.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/config/TobagoConfig.java
@@ -55,6 +55,7 @@ public class TobagoConfig {
   private Theme defaultTheme;
   private String defaultThemeName;
   private Map<String, ThemeImpl> availableThemes;
+  private boolean themeCookie;
   private boolean createSessionSecret;
   private boolean checkSessionSecret;
   private boolean preventFrameAttacks;
@@ -104,6 +105,7 @@ public class TobagoConfig {
     supportedThemeNames = new ArrayList<>();
     supportedThemes = new ArrayList<>();
     availableThemes = new HashMap<>();
+    themeCookie = true;
     createSessionSecret = true;
     checkSessionSecret = true;
     preventFrameAttacks = true;
@@ -124,6 +126,9 @@ public class TobagoConfig {
       initDefaultValidatorInfo();
       lock();
 //todo?        servletContext.setAttribute(TobagoConfig.TOBAGO_CONFIG, this);
+      if (LOG.isInfoEnabled()) {
+        LOG.info(this.toString());
+      }
     } catch (final Exception e) {
       final String error = "Tobago can't be initialized! Application will not run correctly!";
       LOG.error(error, e);
@@ -216,6 +221,14 @@ public class TobagoConfig {
     return availableThemes;
   }
 
+  public boolean isThemeCookie() {
+    return themeCookie;
+  }
+
+  public void setThemeCookie(boolean themeCookie) {
+    this.themeCookie = themeCookie;
+  }
+
   public boolean isCreateSessionSecret() {
     return createSessionSecret;
   }
@@ -339,6 +352,8 @@ public class TobagoConfig {
     builder.append(defaultTheme != null ? defaultTheme.getName() : null);
     builder.append(", \navailableThemes=");
     builder.append(availableThemes.keySet());
+    builder.append(", \nthemeCookie=");
+    builder.append(themeCookie);
     builder.append(", \ncreateSessionSecret=");
     builder.append(createSessionSecret);
     builder.append(", \ncheckSessionSecret=");
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/TobagoContext.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/TobagoContext.java
index 6fa2858..fc3a971 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/TobagoContext.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/TobagoContext.java
@@ -90,13 +90,14 @@ public class TobagoContext implements Serializable {
 
       final String themeName;
       final Object request = externalContext.getRequest();
-      if (request instanceof HttpServletRequest) {
+      final TobagoConfig tobagoConfig = getTobagoConfig();
+      if (request instanceof HttpServletRequest && tobagoConfig.isThemeCookie()) {
         themeName = CookieUtils.getThemeNameFromCookie((HttpServletRequest) request);
       } else {
         themeName = null;
       }
 
-      theme = getTobagoConfig().getTheme(themeName);
+      theme = tobagoConfig.getTheme(themeName);
       if (LOG.isDebugEnabled()) {
         LOG.debug("theme='{}'", theme.getName());
       }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigFragment.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigFragment.java
index c8fd2c8..e7a8f63 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigFragment.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigFragment.java
@@ -36,6 +36,7 @@ public class TobagoConfigFragment {
 
   private final List<String> supportedThemeNames;
   private String defaultThemeName;
+  private Boolean themeCookie;
   private Boolean createSessionSecret;
   private Boolean checkSessionSecret;
   private Boolean preventFrameAttacks;
@@ -106,6 +107,14 @@ public class TobagoConfigFragment {
     return themeDefinitions;
   }
 
+  public Boolean getThemeCookie() {
+    return themeCookie;
+  }
+
+  public void setThemeCookie(final String themeCookie) {
+    this.themeCookie = Boolean.valueOf(themeCookie);
+  }
+
   public Boolean getCreateSessionSecret() {
     return createSessionSecret;
   }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMerger.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMerger.java
index 79e54a2..c2f29b4 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMerger.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigMerger.java
@@ -72,6 +72,11 @@ public class TobagoConfigMerger {
         tobagoConfig.addSupportedThemeName(supported);
       }
 
+      // theme cookie
+      if (fragment.getThemeCookie() != null) {
+        tobagoConfig.setThemeCookie(fragment.getThemeCookie());
+      }
+
       // session secret
       if (fragment.getCreateSessionSecret() != null) {
         tobagoConfig.setCreateSessionSecret(fragment.getCreateSessionSecret());
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
index 67a2103..6c711ec 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
@@ -58,6 +58,7 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
   private static final int THEME_CONFIG = 1930630086;
   private static final int DEFAULT_THEME = -114431171;
   private static final int SUPPORTED_THEME = -822303766;
+  private static final int THEME_COOKIE = 1930664680;
   private static final int CREATE_SESSION_SECRET = 413906616;
   private static final int CHECK_SESSION_SECRET = 275994924;
   private static final int PREVENT_FRAME_ATTACKS = 270456726;
@@ -288,6 +289,7 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
       case THEME_CONFIG:
       case DEFAULT_THEME:
       case SUPPORTED_THEME:
+      case THEME_COOKIE:
       case SUPPORTED_MARKUP:
       case MARKUP:
       case CREATE_SESSION_SECRET:
@@ -369,6 +371,10 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
         tobagoConfig.addSupportedThemeName(text);
         break;
 
+      case THEME_COOKIE:
+        tobagoConfig.setThemeCookie(text);
+        break;
+
       case CREATE_SESSION_SECRET:
         tobagoConfig.setCreateSessionSecret(text);
         break;
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PageRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PageRenderer.java
index 052b7ef..c108680 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PageRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/PageRenderer.java
@@ -141,7 +141,8 @@ public class PageRenderer<T extends AbstractUIPage> extends RendererBase<T> {
     }
 
     final Theme theme = tobagoContext.getTheme();
-    if (response instanceof HttpServletResponse && request instanceof HttpServletRequest) {
+    if (response instanceof HttpServletResponse && request instanceof HttpServletRequest
+      && tobagoConfig.isThemeCookie()) {
       CookieUtils.setThemeNameToCookie((HttpServletRequest) request, (HttpServletResponse) response, theme.getName());
     }
 
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/CookieUtils.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/CookieUtils.java
index f2b041b..bee602a 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/CookieUtils.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/CookieUtils.java
@@ -91,7 +91,7 @@ public class CookieUtils {
             }
             cookie.setMaxAge(ONE_YEAR_IN_SECONDS);
           }
-          cookie.setSecure(true);
+          cookie.setSecure(request.isSecure());
           response.addCookie(cookie);
         }
       }
@@ -100,7 +100,7 @@ public class CookieUtils {
       final Cookie cookie = new Cookie(THEME_PARAMETER, themeName);
       cookie.setPath(path);
       cookie.setMaxAge(ONE_YEAR_IN_SECONDS);
-      cookie.setSecure(true);
+      cookie.setSecure(request.isSecure());
       response.addCookie(cookie);
     }
   }
@@ -114,7 +114,7 @@ public class CookieUtils {
         if (THEME_PARAMETER.equals(cookie.getName())) {
           cookie.setMaxAge(0);
           cookie.setValue(null);
-          cookie.setSecure(true);
+          cookie.setSecure(request.isSecure());
           response.addCookie(cookie);
         }
       }
diff --git a/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.1.xsd b/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.1.xsd
index ecff774..6b45ea9 100644
--- a/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.1.xsd
+++ b/tobago-core/src/main/resources/org/apache/myfaces/tobago/config/tobago-config-5.1.xsd
@@ -232,6 +232,13 @@
           </xs:documentation>
         </xs:annotation>
       </xs:element>
+      <xs:element name="theme-cookie" type="xs:boolean" minOccurs="0" maxOccurs="1" default="true">
+        <xs:annotation>
+          <xs:documentation>
+            Use a cookie to set the active theme in the browser, to recognize the active theme at next visit.
+          </xs:documentation>
+        </xs:annotation>
+      </xs:element>
     </xs:sequence>
   </xs:complexType>
 
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParserUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParserUnitTest.java
index 24071b5..296adff 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParserUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParserUnitTest.java
@@ -181,4 +181,20 @@ public class TobagoConfigParserUnitTest {
     }
   }
 
+  @Test
+  public void testThemeCookieUndefined() throws Exception {
+    final URL url = getClass().getClassLoader().getResource("tobago-config-5.0.xml");
+    final TobagoConfigParser parser = new TobagoConfigParser();
+    final TobagoConfigFragment fragment = parser.parse(url);
+    Assertions.assertNull(fragment.getThemeCookie());
+  }
+
+  @Test
+  public void testThemeCookieFalse() throws Exception {
+    final URL url = getClass().getClassLoader().getResource("tobago-config-5.1.xml");
+    final TobagoConfigParser parser = new TobagoConfigParser();
+    final TobagoConfigFragment fragment = parser.parse(url);
+    Assertions.assertFalse(fragment.getThemeCookie());
+  }
+
 }
diff --git a/tobago-core/src/test/resources/tobago-config-5.1.xml b/tobago-core/src/test/resources/tobago-config-5.1.xml
index 29275f7..8f61e22 100644
--- a/tobago-core/src/test/resources/tobago-config-5.1.xml
+++ b/tobago-core/src/test/resources/tobago-config-5.1.xml
@@ -38,6 +38,7 @@
   <theme-config>
     <default-theme>my-theme-1</default-theme>
     <supported-theme>my-theme-2</supported-theme>
+    <theme-cookie>false</theme-cookie>
   </theme-config>
 
   <create-session-secret>false</create-session-secret>
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tobago-config.xml b/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tobago-config.xml
index 7ce43c6..982e0cc 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tobago-config.xml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tobago-config.xml
@@ -42,6 +42,7 @@
     <supported-theme>speyside</supported-theme>
     <supported-theme>roxborough</supported-theme>
     <supported-theme>charlotteville</supported-theme>
+    <theme-cookie>false</theme-cookie>
   </theme-config>
 
   <!-- currently you need to switch this check off for quarkus -->