You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2018/07/30 19:55:17 UTC

svn commit: r1837087 - in /turbine/core/trunk/src: java/org/apache/turbine/ java/org/apache/turbine/pipeline/ java/org/apache/turbine/services/rundata/ java/org/apache/turbine/services/velocity/ java/org/apache/turbine/util/ test/org/apache/turbine/ te...

Author: tv
Date: Mon Jul 30 19:55:17 2018
New Revision: 1837087

URL: http://svn.apache.org/viewvc?rev=1837087&view=rev
Log:
Move all charset and locale related methods to org.apache.turbine.util.LocaleUtils
Deprecate Turbine.getDefaultInputEncoding()
Allow overriding the output encoding

Added:
    turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java   (with props)
Modified:
    turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java
    turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java
    turbine/core/trunk/src/java/org/apache/turbine/services/rundata/TurbineRunDataService.java
    turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java
    turbine/core/trunk/src/test/org/apache/turbine/TurbineTest.java
    turbine/core/trunk/src/test/org/apache/turbine/services/rundata/DefaultTurbineRunDataTest.java

Modified: turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java?rev=1837087&r1=1837086&r2=1837087&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java Mon Jul 30 19:55:17 2018
@@ -239,6 +239,9 @@ public interface TurbineConstants
 	/** Default value for Charset property */
 	String LOCALE_DEFAULT_CHARSET_DEFAULT = "ISO-8859-1";
 
+    /** Override Charset property */
+    String LOCALE_OVERRIDE_CHARSET_KEY = "locale.override.charset";
+
 	/** If this value is set as applicationRoot, then the webContext is used
 	 * as application root
 	 */

Modified: turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java?rev=1837087&r1=1837086&r2=1837087&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java Mon Jul 30 19:55:17 2018
@@ -29,14 +29,14 @@ import javax.servlet.http.HttpServletReq
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.turbine.Turbine;
-import org.apache.turbine.TurbineConstants;
-import org.apache.turbine.annotation.TurbineConfiguration;
-import org.apache.turbine.util.RunData;
+import org.apache.turbine.util.LocaleUtils;
 import org.apache.turbine.util.TurbineException;
 
 /**
  * Set default encoding of the request. The default behavior is to respond
- * with the charset that was requested.
+ * with the charset that was requested. If the configuration sets a property named
+ * "locale.override.charset", the output encoding will always be set to its value,
+ * no matter what the input encoding is.
  *
  * This valve must be situated in the pipeline before any access to the
  * {@link org.apache.fulcrum.parser.ParameterParser} to take effect.
@@ -48,9 +48,6 @@ public class DefaultSetEncodingValve
 {
     private static final Log log = LogFactory.getLog(DefaultSetEncodingValve.class);
 
-    @TurbineConfiguration( TurbineConstants.PARAMETER_ENCODING_KEY )
-    protected String defaultEncoding = TurbineConstants.PARAMETER_ENCODING_DEFAULT;
-
     /**
      * @see org.apache.turbine.pipeline.Valve#invoke(PipelineData, ValveContext)
      */
@@ -66,7 +63,7 @@ public class DefaultSetEncodingValve
 
         if (requestEncoding == null)
         {
-            requestEncoding = defaultEncoding;
+            requestEncoding = LocaleUtils.getDefaultInputEncoding();
 
             if (log.isDebugEnabled())
             {
@@ -84,8 +81,13 @@ public class DefaultSetEncodingValve
         }
 
         // Copy encoding charset to RunData to set a reasonable default for the response
-        RunData data = getRunData(pipelineData);
-        data.setCharSet(requestEncoding);
+        String outputEncoding = LocaleUtils.getOverrideCharSet();
+        if (outputEncoding == null)
+        {
+            outputEncoding = requestEncoding;
+        }
+
+        getRunData(pipelineData).setCharSet(outputEncoding);
 
         // Pass control to the next Valve in the Pipeline
         context.invokeNext(pipelineData);

Modified: turbine/core/trunk/src/java/org/apache/turbine/services/rundata/TurbineRunDataService.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/services/rundata/TurbineRunDataService.java?rev=1837087&r1=1837086&r2=1837087&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/services/rundata/TurbineRunDataService.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/services/rundata/TurbineRunDataService.java Mon Jul 30 19:55:17 2018
@@ -284,18 +284,10 @@ public class TurbineRunDataService
         {
             throw new TurbineException("RunData configuration '" + key + "' is illegal caused a pool exception", pe);
         }
-        catch (ClassNotFoundException x)
+        catch (ClassNotFoundException | ClassCastException | InstantiationException x)
         {
             throw new TurbineException("RunData configuration '" + key + "' is illegal", x);
         }
-        catch (ClassCastException x)
-        {
-            throw new TurbineException("RunData configuration '" + key + "' is illegal", x);
-        }
-        catch (InstantiationException e)
-        {
-            throw new TurbineException("RunData configuration '" + key + "' is illegal", e);
-        }
 
         // Set the request and response.
         data.get(Turbine.class).put(HttpServletRequest.class, req);

Modified: turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java?rev=1837087&r1=1837086&r2=1837087&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java Mon Jul 30 19:55:17 2018
@@ -34,12 +34,12 @@ import org.apache.commons.lang.StringUti
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.turbine.Turbine;
-import org.apache.turbine.TurbineConstants;
 import org.apache.turbine.pipeline.PipelineData;
 import org.apache.turbine.services.InitializationException;
 import org.apache.turbine.services.TurbineServices;
 import org.apache.turbine.services.pull.PullService;
 import org.apache.turbine.services.template.BaseTemplateEngineService;
+import org.apache.turbine.util.LocaleUtils;
 import org.apache.turbine.util.RunData;
 import org.apache.turbine.util.TurbineException;
 import org.apache.velocity.VelocityContext;
@@ -144,11 +144,13 @@ public class TurbineVelocityService
             // Register with the template service.
             registerConfiguration(VelocityService.VELOCITY_EXTENSION);
 
-            String turbineInputEncoding = Turbine.getConfiguration().getString(
-                    TurbineConstants.PARAMETER_ENCODING_KEY,
-                    TurbineConstants.PARAMETER_ENCODING_DEFAULT);
+            defaultInputEncoding = getConfiguration().getString("input.encoding", LocaleUtils.getDefaultInputEncoding());
 
-            defaultInputEncoding = getConfiguration().getString("input.encoding", turbineInputEncoding);
+            String outputEncoding = LocaleUtils.getOverrideCharSet();
+            if (outputEncoding == null)
+            {
+                outputEncoding = defaultInputEncoding;
+            }
             defaultOutputEncoding = getConfiguration().getString("output.encoding", defaultInputEncoding);
 
             setInit(true);

Added: turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java?rev=1837087&view=auto
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java (added)
+++ turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java Mon Jul 30 19:55:17 2018
@@ -0,0 +1,140 @@
+package org.apache.turbine.util;
+
+import java.util.Locale;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.fulcrum.mimetype.MimeTypeService;
+import org.apache.turbine.Turbine;
+import org.apache.turbine.TurbineConstants;
+import org.apache.turbine.services.ServiceManager;
+import org.apache.turbine.services.TurbineServices;
+
+/**
+ * This class provides utilities for handling locales and charsets
+ *
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ */
+public class LocaleUtils
+{
+    /** Logging */
+    private static Log log = LogFactory.getLog(LocaleUtils.class);
+
+    /** The default locale. */
+    private static Locale defaultLocale = null;
+
+    /** The default charset. */
+    private static String defaultCharSet = null;
+
+    /**
+     * Returns the default input encoding for the servlet.
+     *
+     * @return the default input encoding.
+     */
+    public static String getDefaultInputEncoding()
+    {
+        // Get the default input defaultEncoding
+        String inputEncoding = Turbine.getConfiguration()
+                .getString(TurbineConstants.PARAMETER_ENCODING_KEY,
+                        TurbineConstants.PARAMETER_ENCODING_DEFAULT);
+
+        if (log.isDebugEnabled())
+        {
+            log.debug("Input Encoding has been set to " + inputEncoding);
+        }
+
+        return inputEncoding;
+    }
+
+    /**
+     * Gets the default locale defined by properties named "locale.default.lang"
+     * and "locale.default.country".
+     *
+     * This changed from earlier Turbine versions that you can rely on
+     * getDefaultLocale() to never return null.
+     *
+     * @return A Locale object.
+     */
+    public static Locale getDefaultLocale()
+    {
+        if (defaultLocale == null)
+        {
+            /* Get the default locale and cache it in a static variable. */
+            String lang = Turbine.getConfiguration()
+                    .getString(TurbineConstants.LOCALE_DEFAULT_LANGUAGE_KEY,
+                            TurbineConstants.LOCALE_DEFAULT_LANGUAGE_DEFAULT);
+
+            String country = Turbine.getConfiguration()
+                    .getString(TurbineConstants.LOCALE_DEFAULT_COUNTRY_KEY,
+                            TurbineConstants.LOCALE_DEFAULT_COUNTRY_DEFAULT);
+
+            // We ensure that lang and country is never null
+            defaultLocale = new Locale(lang, country);
+        }
+
+        return defaultLocale;
+    }
+
+    /**
+     * Gets the default charset defined by a property named
+     * "locale.default.charset"
+     *
+     * @return the name of the default charset or null.
+     */
+    public static String getDefaultCharSet()
+    {
+        if (defaultCharSet == null)
+        {
+            /* Get the default charset and cache it in a static variable. */
+            defaultCharSet = Turbine.getConfiguration()
+                    .getString(TurbineConstants.LOCALE_DEFAULT_CHARSET_KEY,
+                            TurbineConstants.LOCALE_DEFAULT_CHARSET_DEFAULT);
+            log.debug("defaultCharSet = " + defaultCharSet + " (From Properties)");
+        }
+
+        String charset = defaultCharSet;
+
+        if (StringUtils.isEmpty(charset)) // can happen if set explicitly in the configuration
+        {
+            log.debug("Default charset is empty!");
+            /* Default charset isn't specified, get the locale specific one. */
+            Locale locale = getDefaultLocale();
+            log.debug("Locale is " + locale);
+
+            if (!locale.equals(Locale.US))
+            {
+                log.debug("We don't have US Locale!");
+                ServiceManager serviceManager = TurbineServices.getInstance();
+                MimeTypeService mimeTypeService = null;
+                try
+                {
+                    mimeTypeService = (MimeTypeService) serviceManager.getService(MimeTypeService.ROLE);
+                }
+                catch (Exception e)
+                {
+                    throw new RuntimeException(e);
+                }
+                charset = mimeTypeService.getCharSet(locale);
+
+                log.debug("Charset now " + charset);
+            }
+        }
+
+        log.debug("Returning default Charset of " + charset);
+        return charset;
+    }
+
+    /**
+     * Gets the charset defined by a property named "locale.override.charset"
+     * This property has no default. If it exists, the output charset is always
+     * set to its value
+     *
+     * @return the name of the override charset or null.
+     */
+    public static String getOverrideCharSet()
+    {
+        return Turbine.getConfiguration()
+                .getString(TurbineConstants.LOCALE_OVERRIDE_CHARSET_KEY);
+    }
+}

Propchange: turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: turbine/core/trunk/src/test/org/apache/turbine/TurbineTest.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/test/org/apache/turbine/TurbineTest.java?rev=1837087&r1=1837086&r2=1837087&view=diff
==============================================================================
--- turbine/core/trunk/src/test/org/apache/turbine/TurbineTest.java (original)
+++ turbine/core/trunk/src/test/org/apache/turbine/TurbineTest.java Mon Jul 30 19:55:17 2018
@@ -27,6 +27,8 @@ import javax.servlet.http.HttpServletRes
 
 import org.apache.turbine.test.BaseTestCase;
 import org.apache.turbine.util.TurbineConfig;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
 
@@ -40,14 +42,28 @@ import org.mockito.Mockito;
  */
 public class TurbineTest extends BaseTestCase
 {
+    private TurbineConfig tc = null;
 
-    @Test
-    public void testTurbineAndFirstGet() throws Exception
+    @Before
+    public void setUp() throws Exception
     {
-        TurbineConfig tc = new TurbineConfig(".",
+        tc = new TurbineConfig(".",
                 "/conf/test/CompleteTurbineResources.properties");
         tc.initialize();
+    }
+
+    @After
+    public void tearDown() throws Exception
+    {
+        if (tc != null)
+        {
+            tc.dispose();
+        }
+    }
 
+    @Test
+    public void testTurbineAndFirstGet() throws Exception
+    {
         assertNotNull(Turbine.getDefaultServerData());
         assertEquals("", Turbine.getServerName());
         assertEquals("80", Turbine.getServerPort());
@@ -61,31 +77,23 @@ public class TurbineTest extends BaseTes
 
         assertEquals("8080", Turbine.getServerPort());
         t.destroy();
-        tc.dispose();
     }
 
     @Test
     public void testDefaultInputEncoding() throws Exception
     {
-        TurbineConfig tc = new TurbineConfig(".",
-                "/conf/test/CompleteTurbineResources.properties");
-        tc.initialize();
         Turbine t = tc.getTurbine();
         assertNotNull(t.getDefaultInputEncoding());
         assertEquals(TurbineConstants.PARAMETER_ENCODING_DEFAULT, t.getDefaultInputEncoding());
         t.destroy();
-        tc.dispose();
     }
 
     @Test
     public void testNonDefaultEncoding()
     {
-        TurbineConfig tc = new TurbineConfig(".",
-                "/conf/test/CompleteTurbineResourcesWithEncoding.properties");
-        tc.initialize();
         Turbine t = tc.getTurbine();
+        t.getConfiguration().setProperty(TurbineConstants.PARAMETER_ENCODING_KEY, "UTF-8");
         assertNotNull(t.getDefaultInputEncoding());
         assertEquals("UTF-8", t.getDefaultInputEncoding());
-        tc.dispose();
     }
 }

Modified: turbine/core/trunk/src/test/org/apache/turbine/services/rundata/DefaultTurbineRunDataTest.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/test/org/apache/turbine/services/rundata/DefaultTurbineRunDataTest.java?rev=1837087&r1=1837086&r2=1837087&view=diff
==============================================================================
--- turbine/core/trunk/src/test/org/apache/turbine/services/rundata/DefaultTurbineRunDataTest.java (original)
+++ turbine/core/trunk/src/test/org/apache/turbine/services/rundata/DefaultTurbineRunDataTest.java Mon Jul 30 19:55:17 2018
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertEqu
 import org.apache.turbine.Turbine;
 import org.apache.turbine.TurbineConstants;
 import org.apache.turbine.test.BaseTestCase;
+import org.apache.turbine.util.LocaleUtils;
 import org.apache.turbine.util.TurbineConfig;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -43,10 +44,7 @@ public class DefaultTurbineRunDataTest e
 		Turbine.getConfiguration().setProperty(
 			  TurbineConstants.LOCALE_DEFAULT_COUNTRY_KEY,
 			  "UK");
-         DefaultTurbineRunData runData =
-            new DefaultTurbineRunData();
-        assertEquals("ISO-8859-1", runData.getDefaultCharSet());
-
+        assertEquals("ISO-8859-1", LocaleUtils.getDefaultCharSet());
     }
 
     @BeforeClass
@@ -58,6 +56,7 @@ public class DefaultTurbineRunDataTest e
                 "/conf/test/TestFulcrumComponents.properties");
         tc.initialize();
     }
+
     @AfterClass
     public static void tearDown() throws Exception
     {