You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by gk...@apache.org on 2023/09/22 14:20:44 UTC

[turbine-core] branch trunk updated: add rundata based locale setting in session (temp storage), integrated in LocalizationTool + test, update yauaa to 7.22.0

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

gk pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/turbine-core.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 17587806 add rundata based locale setting in session (temp storage), integrated in LocalizationTool + test, update yauaa to 7.22.0
17587806 is described below

commit 17587806ac6b55185fff031e5ac2d96587d272a5
Author: Georg Kallidis <gk...@apache.org>
AuthorDate: Fri Sep 22 16:18:00 2023 +0200

    add rundata based locale setting in session (temp storage), integrated in LocalizationTool + test, update yauaa to 7.22.0
---
 conf/test/fulcrumRundataRoleConfiguration.xml      |  53 +++++++++
 conf/test/rundataTemplateService.properties        |  23 ++++
 pom.xml                                            |   2 +-
 .../services/localization/LocalizationTool.java    |   4 +-
 .../localization/RundataLocalizationInterface.java |  19 ++++
 .../localization/RundataLocalizationService.java   |  57 ++++++++++
 .../turbine/services/localization/BarBundle.java   |  42 +++++++
 .../services/localization/BarBundle_de.java        |  42 +++++++
 .../localization/RundataLocalizationToolTest.java  | 123 +++++++++++++++++++++
 9 files changed, 363 insertions(+), 2 deletions(-)

diff --git a/conf/test/fulcrumRundataRoleConfiguration.xml b/conf/test/fulcrumRundataRoleConfiguration.xml
new file mode 100644
index 00000000..84c1e009
--- /dev/null
+++ b/conf/test/fulcrumRundataRoleConfiguration.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<!--
+	This configuration file for Avalon components is used for testing components with Turbine.
+-->
+
+<role-list>
+
+    <role
+        name="org.apache.fulcrum.localization.LocalizationService"
+        shorthand="localization"
+        default-class="org.apache.turbine.services.localization.RundataLocalizationService"/>
+
+    <role
+        name="org.apache.fulcrum.factory.FactoryService"
+        shorthand="factory"
+        default-class="org.apache.fulcrum.factory.DefaultFactoryService"/>
+
+    <role
+        name="org.apache.fulcrum.pool.PoolService"
+        shorthand="pool"
+        default-class="org.apache.fulcrum.pool.DefaultPoolService"/>
+
+    <role
+        name="org.apache.fulcrum.parser.ParserService"
+        shorthand="parser"
+        default-class="org.apache.fulcrum.parser.DefaultParserService"/>
+
+    <!-- These components belong to the Fulcrum-Security services -->
+    <role
+        name="org.apache.fulcrum.security.SecurityService"
+        shorthand="securityService"
+        default-class="org.apache.fulcrum.security.BaseSecurityService"/>
+  
+</role-list>
+
diff --git a/conf/test/rundataTemplateService.properties b/conf/test/rundataTemplateService.properties
new file mode 100644
index 00000000..993fd569
--- /dev/null
+++ b/conf/test/rundataTemplateService.properties
@@ -0,0 +1,23 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+services.AvalonComponentService.componentRoles = conf/test/fulcrumRundataRoleConfiguration.xml
+
+include=TemplateService.properties
+
+
+
diff --git a/pom.xml b/pom.xml
index 1c8d0a2b..4da78579 100644
--- a/pom.xml
+++ b/pom.xml
@@ -913,7 +913,7 @@
       <groupId>nl.basjes.parse.useragent</groupId>
       <artifactId>yauaa</artifactId>
       <!-- java 11 required for v6.1+ -->
-      <version>7.20.0</version>
+      <version>7.22.0</version>
     </dependency>
     <dependency>
       <groupId>org.apache.fulcrum</groupId>
diff --git a/src/java/org/apache/turbine/services/localization/LocalizationTool.java b/src/java/org/apache/turbine/services/localization/LocalizationTool.java
index ee46be9c..6e0955a8 100644
--- a/src/java/org/apache/turbine/services/localization/LocalizationTool.java
+++ b/src/java/org/apache/turbine/services/localization/LocalizationTool.java
@@ -137,7 +137,9 @@ public class LocalizationTool implements ApplicationTool
         {
             // Pull necessary information out of RunData while we have
             // a reference to it.
-            locale = localizationService.getLocale(((RunData) data).getRequest());
+            locale = (localizationService instanceof RundataLocalizationService)?
+                    ((RundataLocalizationService)localizationService).getLocale((RunData) data):
+                    localizationService.getLocale(((RunData) data).getRequest());
         }
     }
 
diff --git a/src/java/org/apache/turbine/services/localization/RundataLocalizationInterface.java b/src/java/org/apache/turbine/services/localization/RundataLocalizationInterface.java
new file mode 100644
index 00000000..552b910f
--- /dev/null
+++ b/src/java/org/apache/turbine/services/localization/RundataLocalizationInterface.java
@@ -0,0 +1,19 @@
+package org.apache.turbine.services.localization;
+
+import java.io.Serializable;
+import java.util.Locale;
+
+import org.apache.fulcrum.localization.LocalizationService;
+import org.apache.turbine.util.RunData;
+
+public interface RundataLocalizationInterface extends LocalizationService, Serializable
+{
+    /**
+     * Get the locale from the session first, then fallback to normal request headers.
+     * 
+     * @param data
+     * @return Current locale based on state.
+     */
+    Locale getLocale(RunData data);
+    
+}
diff --git a/src/java/org/apache/turbine/services/localization/RundataLocalizationService.java b/src/java/org/apache/turbine/services/localization/RundataLocalizationService.java
new file mode 100644
index 00000000..8743e88f
--- /dev/null
+++ b/src/java/org/apache/turbine/services/localization/RundataLocalizationService.java
@@ -0,0 +1,57 @@
+package org.apache.turbine.services.localization;
+
+import java.util.Locale;
+
+import org.apache.fulcrum.localization.DefaultLocalizationService;
+import org.apache.fulcrum.localization.LocalizationService;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.turbine.om.security.User;
+import org.apache.turbine.util.RunData;
+
+/**
+ * 
+ * Instead of reading first the accept-language header in a http request,
+ * instead this method read the user.getTemp("locale")
+ * from the RunData to obtain the language choice by the user
+ * without the browser language rule.
+ * If user.getPerm("language") is not set,
+ *  the "Accept-Language" header is read.
+ * 
+ * Adapted from the Jetspeed-1 implementation of CustomLocalizationService.
+ * 
+ */
+public class RundataLocalizationService extends DefaultLocalizationService implements RundataLocalizationInterface {
+
+    private static final Logger log = LogManager.getLogger(RundataLocalizationService.class); 
+    
+    @Override
+    public Locale getLocale(RunData data) {
+        User user = data.getUser();
+        if (user == null)
+        {
+            return getLocale(data.getRequest().getHeader(LocalizationService.ACCEPT_LANGUAGE));
+        }
+        else
+        {
+            try
+            {
+                Locale locale = (Locale) data.getUser().getTemp("locale");
+                if (locale == null)
+                {
+                    return getLocale(data.getRequest().getHeader(LocalizationService.ACCEPT_LANGUAGE));
+                }
+                else
+                {
+                    log.debug( "retrieved lang from temp(locale):{}", ()-> locale.getLanguage() );
+                    return locale;
+                }
+            }
+            catch (Exception use)
+            {
+                return getLocale(data.getRequest().getHeader(LocalizationService.ACCEPT_LANGUAGE));
+            }
+        }
+    }
+
+}
diff --git a/src/test/org/apache/turbine/services/localization/BarBundle.java b/src/test/org/apache/turbine/services/localization/BarBundle.java
new file mode 100644
index 00000000..f33289dc
--- /dev/null
+++ b/src/test/org/apache/turbine/services/localization/BarBundle.java
@@ -0,0 +1,42 @@
+package org.apache.turbine.services.localization;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ListResourceBundle;
+
+/**
+ * An english resource bundle for use in testing.
+ */
+public class BarBundle extends ListResourceBundle
+{
+    private static final Object[][] CONTENTS =
+    {
+        { "key1", "val1" },
+        { "key2", "val2" },
+        { "key3", "val3" },
+        { "key4", "val4" }
+    };
+
+    @Override
+    protected Object[][] getContents()
+    {
+        return CONTENTS;
+    }
+}
diff --git a/src/test/org/apache/turbine/services/localization/BarBundle_de.java b/src/test/org/apache/turbine/services/localization/BarBundle_de.java
new file mode 100644
index 00000000..56d34abe
--- /dev/null
+++ b/src/test/org/apache/turbine/services/localization/BarBundle_de.java
@@ -0,0 +1,42 @@
+package org.apache.turbine.services.localization;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ListResourceBundle;
+
+/**
+ * An english resource bundle for use in testing.
+ */
+public class BarBundle_de extends ListResourceBundle
+{
+    private static final Object[][] CONTENTS =
+    {
+        { "key1", "wert1" },
+        { "key2", "weet2" },
+        { "key3", "wert3" },
+        { "key4", "wert4" }
+    };
+
+    @Override
+    protected Object[][] getContents()
+    {
+        return CONTENTS;
+    }
+}
diff --git a/src/test/org/apache/turbine/services/localization/RundataLocalizationToolTest.java b/src/test/org/apache/turbine/services/localization/RundataLocalizationToolTest.java
new file mode 100644
index 00000000..4ca27977
--- /dev/null
+++ b/src/test/org/apache/turbine/services/localization/RundataLocalizationToolTest.java
@@ -0,0 +1,123 @@
+package org.apache.turbine.services.localization;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.mockito.Mockito.mock;
+
+import java.util.Locale;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
+import org.apache.fulcrum.testcontainer.BaseUnit5Test;
+import org.apache.turbine.annotation.AnnotationProcessor;
+import org.apache.turbine.annotation.TurbineTool;
+import org.apache.turbine.om.security.DefaultUserImpl;
+import org.apache.turbine.om.security.User;
+import org.apache.turbine.services.TurbineServices;
+import org.apache.turbine.services.rundata.RunDataService;
+import org.apache.turbine.util.RunData;
+import org.apache.turbine.util.TurbineConfig;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit test for Localization Tool. Verifies that localization works the same using the
+ * deprecated Turbine localization service as well as the new Fulcrum Localization
+ * component.
+ *
+ * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
+ * @version $Id$
+ */
+public class RundataLocalizationToolTest extends BaseUnit5Test
+{
+    private static TurbineConfig tc = null;
+    
+    @TurbineTool(LocalizationTool.class)
+    private LocalizationTool lt;
+    
+    @BeforeAll
+    public static void setUp() throws Exception
+    {
+        tc = new TurbineConfig(".", "/conf/test/rundataTemplateService.properties");
+        tc.initialize();
+        
+    }
+
+    @BeforeEach
+    public void initTool() throws Exception
+    {
+        AnnotationProcessor.process(this);   
+        lt.init(getRunData());
+    }
+
+    @Test
+    public void testGet() throws Exception
+    {
+        assertEquals("wert1", lt.get("key1"));
+        assertEquals("wert3", lt.get("key3"));
+    }
+
+    @Test
+    public void testGetLocale() throws Exception
+    {
+        assertNotNull(lt.getLocale());
+        assertEquals("DE", lt.getLocale().getCountry());
+        assertEquals("de", lt.getLocale().getLanguage());
+    }
+
+    @Test
+    public void testInit() throws Exception
+    {
+        assertNotNull(lt.getLocale());
+    }
+
+    private RunData getRunData() throws Exception
+    {
+        RunDataService rds = (RunDataService) TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
+        ServletConfig config = mock(ServletConfig.class);
+        HttpServletRequest request = getMockRequest();
+        HttpServletResponse response = mock(HttpServletResponse.class);
+        RunData runData = rds.getRunData(request, response, config);
+        
+        User user = null;
+        try {
+            user = new DefaultUserImpl(mock(TurbineUser.class));
+            user.setTemp("locale", new Locale("de","DE") );
+            runData.setUser(user);            
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        
+        return runData;
+    }
+
+    @AfterAll
+    public static void destroy() {
+        tc.dispose();
+    }
+}