You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by gr...@apache.org on 2013/06/13 13:57:16 UTC

svn commit: r1492633 - /struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java

Author: grobmeier
Date: Thu Jun 13 11:57:16 2013
New Revision: 1492633

URL: http://svn.apache.org/r1492633
Log:
some refactorings for better readability

Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java?rev=1492633&r1=1492632&r2=1492633&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java (original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java Thu Jun 13 11:57:16 2013
@@ -125,6 +125,7 @@ public class I18nInterceptor extends Abs
         Map<String, Object> params = invocation.getInvocationContext().getParameters();
 
         boolean storeInSession = true;
+
         Object requestedLocale = findLocaleParameter(params, parameterName);
         if (requestedLocale == null) {
             requestedLocale = findLocaleParameter(params, requestOnlyParameterName);
@@ -133,41 +134,24 @@ public class I18nInterceptor extends Abs
             }
         }
 
+        Locale locale = getLocaleFromParam(requestedLocale);
+
         //save it in session
         Map<String, Object> session = invocation.getInvocationContext().getSession();
 
-        Locale locale = null;
-        if (requestedLocale != null) {
-            locale = (requestedLocale instanceof Locale) ?
-                    (Locale) requestedLocale : LocalizedTextUtil.localeFromString(requestedLocale.toString(), null);
-            if (locale != null && LOG.isDebugEnabled()) {
-                LOG.debug("applied request locale=#0", locale);
-            }
-        }
         if (session != null) {
             synchronized (session) {
                 if (locale == null) {
                     storeInSession = false;
-                    // check session for saved locale
-                    Object sessionLocale = session.get(attributeName);
-                    if (sessionLocale != null && sessionLocale instanceof Locale) {
-                        locale = (Locale) sessionLocale;
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("applied session locale=#0", locale);
-                        }
-                    } else {
-                        // no overriding locale definition found, stay with current invocation (=browser) locale
-                        locale = invocation.getInvocationContext().getLocale();
-                        if (locale != null && LOG.isDebugEnabled()) {
-                            LOG.debug("applied invocation context locale=#0", locale);
-                        }
-                    }
+                    locale = readStoredLocale(invocation, session);
                 }
+
                 if (storeInSession) {
                     session.put(attributeName, locale);
                 }
             }
         }
+
         saveLocale(invocation, locale);
 
         if (LOG.isDebugEnabled()) {
@@ -183,6 +167,54 @@ public class I18nInterceptor extends Abs
         return result;
     }
 
+    /**
+     * Reads the locale from the session, and if not found from the
+     * current invocation (=browser)
+     *
+     * @param invocation the current invocation
+     * @param session the current session
+     * @return the read locale
+     */
+    private Locale readStoredLocale(ActionInvocation invocation, Map<String, Object> session) {
+        // check session for saved locale
+        Object sessionLocale = session.get(attributeName);
+        if (sessionLocale != null && sessionLocale instanceof Locale) {
+            Locale locale = (Locale) sessionLocale;
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("applied session locale=#0", locale);
+            }
+            return locale;
+        }
+
+        // no overriding locale definition found, stay with current invocation (=browser) locale
+        Locale locale = invocation.getInvocationContext().getLocale();
+        if (locale != null && LOG.isDebugEnabled()) {
+            LOG.debug("applied invocation context locale=#0", locale);
+        }
+
+        return locale;
+    }
+
+    /**
+     * Creates a Locale object from the request param, which might
+     * be already a Local or a String
+     *
+     * @param requestedLocale the parameter from the request
+     * @return the Locale
+     */
+    private Locale getLocaleFromParam(Object requestedLocale) {
+        Locale locale = null;
+        if (requestedLocale != null) {
+            locale = (requestedLocale instanceof Locale) ?
+                    (Locale) requestedLocale :
+                    LocalizedTextUtil.localeFromString(requestedLocale.toString(), null);
+            if (locale != null && LOG.isDebugEnabled()) {
+                LOG.debug("applied request locale=#0", locale);
+            }
+        }
+        return locale;
+    }
+
     private Object findLocaleParameter(Map<String, Object> params, String parameterName) {
         Object requestedLocale = params.remove(parameterName);
         if (requestedLocale != null && requestedLocale.getClass().isArray()



Re: svn commit: r1492633 - /struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java

Posted by Christian Grobmeier <gr...@gmail.com>.
On Thu, Jun 13, 2013 at 2:27 PM, Lukasz Lenart <lu...@apache.org> wrote:
> It is better to make methods protected - easier to extend or restore
> the old behaviour

might make sense. For example: readStoredLocale might be able to read
from cookies first,
when overriding. This might also solve my previously mentioned
problem, if we don't want't to build in native cookie support

I am going to change it to protected


>
> Regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
>
> 2013/6/13  <gr...@apache.org>:
>> Author: grobmeier
>> Date: Thu Jun 13 11:57:16 2013
>> New Revision: 1492633
>>
>> URL: http://svn.apache.org/r1492633
>> Log:
>> some refactorings for better readability
>>
>> Modified:
>>     struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java
>>
>> Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java
>> URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java?rev=1492633&r1=1492632&r2=1492633&view=diff
>> ==============================================================================
>> --- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java (original)
>> +++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java Thu Jun 13 11:57:16 2013
>> @@ -125,6 +125,7 @@ public class I18nInterceptor extends Abs
>>          Map<String, Object> params = invocation.getInvocationContext().getParameters();
>>
>>          boolean storeInSession = true;
>> +
>>          Object requestedLocale = findLocaleParameter(params, parameterName);
>>          if (requestedLocale == null) {
>>              requestedLocale = findLocaleParameter(params, requestOnlyParameterName);
>> @@ -133,41 +134,24 @@ public class I18nInterceptor extends Abs
>>              }
>>          }
>>
>> +        Locale locale = getLocaleFromParam(requestedLocale);
>> +
>>          //save it in session
>>          Map<String, Object> session = invocation.getInvocationContext().getSession();
>>
>> -        Locale locale = null;
>> -        if (requestedLocale != null) {
>> -            locale = (requestedLocale instanceof Locale) ?
>> -                    (Locale) requestedLocale : LocalizedTextUtil.localeFromString(requestedLocale.toString(), null);
>> -            if (locale != null && LOG.isDebugEnabled()) {
>> -                LOG.debug("applied request locale=#0", locale);
>> -            }
>> -        }
>>          if (session != null) {
>>              synchronized (session) {
>>                  if (locale == null) {
>>                      storeInSession = false;
>> -                    // check session for saved locale
>> -                    Object sessionLocale = session.get(attributeName);
>> -                    if (sessionLocale != null && sessionLocale instanceof Locale) {
>> -                        locale = (Locale) sessionLocale;
>> -                        if (LOG.isDebugEnabled()) {
>> -                            LOG.debug("applied session locale=#0", locale);
>> -                        }
>> -                    } else {
>> -                        // no overriding locale definition found, stay with current invocation (=browser) locale
>> -                        locale = invocation.getInvocationContext().getLocale();
>> -                        if (locale != null && LOG.isDebugEnabled()) {
>> -                            LOG.debug("applied invocation context locale=#0", locale);
>> -                        }
>> -                    }
>> +                    locale = readStoredLocale(invocation, session);
>>                  }
>> +
>>                  if (storeInSession) {
>>                      session.put(attributeName, locale);
>>                  }
>>              }
>>          }
>> +
>>          saveLocale(invocation, locale);
>>
>>          if (LOG.isDebugEnabled()) {
>> @@ -183,6 +167,54 @@ public class I18nInterceptor extends Abs
>>          return result;
>>      }
>>
>> +    /**
>> +     * Reads the locale from the session, and if not found from the
>> +     * current invocation (=browser)
>> +     *
>> +     * @param invocation the current invocation
>> +     * @param session the current session
>> +     * @return the read locale
>> +     */
>> +    private Locale readStoredLocale(ActionInvocation invocation, Map<String, Object> session) {
>> +        // check session for saved locale
>> +        Object sessionLocale = session.get(attributeName);
>> +        if (sessionLocale != null && sessionLocale instanceof Locale) {
>> +            Locale locale = (Locale) sessionLocale;
>> +            if (LOG.isDebugEnabled()) {
>> +                LOG.debug("applied session locale=#0", locale);
>> +            }
>> +            return locale;
>> +        }
>> +
>> +        // no overriding locale definition found, stay with current invocation (=browser) locale
>> +        Locale locale = invocation.getInvocationContext().getLocale();
>> +        if (locale != null && LOG.isDebugEnabled()) {
>> +            LOG.debug("applied invocation context locale=#0", locale);
>> +        }
>> +
>> +        return locale;
>> +    }
>> +
>> +    /**
>> +     * Creates a Locale object from the request param, which might
>> +     * be already a Local or a String
>> +     *
>> +     * @param requestedLocale the parameter from the request
>> +     * @return the Locale
>> +     */
>> +    private Locale getLocaleFromParam(Object requestedLocale) {
>> +        Locale locale = null;
>> +        if (requestedLocale != null) {
>> +            locale = (requestedLocale instanceof Locale) ?
>> +                    (Locale) requestedLocale :
>> +                    LocalizedTextUtil.localeFromString(requestedLocale.toString(), null);
>> +            if (locale != null && LOG.isDebugEnabled()) {
>> +                LOG.debug("applied request locale=#0", locale);
>> +            }
>> +        }
>> +        return locale;
>> +    }
>> +
>>      private Object findLocaleParameter(Map<String, Object> params, String parameterName) {
>>          Object requestedLocale = params.remove(parameterName);
>>          if (requestedLocale != null && requestedLocale.getClass().isArray()
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
>



--
http://www.grobmeier.de
https://www.timeandbill.de

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: svn commit: r1492633 - /struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java

Posted by Lukasz Lenart <lu...@apache.org>.
It is better to make methods protected - easier to extend or restore
the old behaviour


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

2013/6/13  <gr...@apache.org>:
> Author: grobmeier
> Date: Thu Jun 13 11:57:16 2013
> New Revision: 1492633
>
> URL: http://svn.apache.org/r1492633
> Log:
> some refactorings for better readability
>
> Modified:
>     struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java
>
> Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java
> URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java?rev=1492633&r1=1492632&r2=1492633&view=diff
> ==============================================================================
> --- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java (original)
> +++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java Thu Jun 13 11:57:16 2013
> @@ -125,6 +125,7 @@ public class I18nInterceptor extends Abs
>          Map<String, Object> params = invocation.getInvocationContext().getParameters();
>
>          boolean storeInSession = true;
> +
>          Object requestedLocale = findLocaleParameter(params, parameterName);
>          if (requestedLocale == null) {
>              requestedLocale = findLocaleParameter(params, requestOnlyParameterName);
> @@ -133,41 +134,24 @@ public class I18nInterceptor extends Abs
>              }
>          }
>
> +        Locale locale = getLocaleFromParam(requestedLocale);
> +
>          //save it in session
>          Map<String, Object> session = invocation.getInvocationContext().getSession();
>
> -        Locale locale = null;
> -        if (requestedLocale != null) {
> -            locale = (requestedLocale instanceof Locale) ?
> -                    (Locale) requestedLocale : LocalizedTextUtil.localeFromString(requestedLocale.toString(), null);
> -            if (locale != null && LOG.isDebugEnabled()) {
> -                LOG.debug("applied request locale=#0", locale);
> -            }
> -        }
>          if (session != null) {
>              synchronized (session) {
>                  if (locale == null) {
>                      storeInSession = false;
> -                    // check session for saved locale
> -                    Object sessionLocale = session.get(attributeName);
> -                    if (sessionLocale != null && sessionLocale instanceof Locale) {
> -                        locale = (Locale) sessionLocale;
> -                        if (LOG.isDebugEnabled()) {
> -                            LOG.debug("applied session locale=#0", locale);
> -                        }
> -                    } else {
> -                        // no overriding locale definition found, stay with current invocation (=browser) locale
> -                        locale = invocation.getInvocationContext().getLocale();
> -                        if (locale != null && LOG.isDebugEnabled()) {
> -                            LOG.debug("applied invocation context locale=#0", locale);
> -                        }
> -                    }
> +                    locale = readStoredLocale(invocation, session);
>                  }
> +
>                  if (storeInSession) {
>                      session.put(attributeName, locale);
>                  }
>              }
>          }
> +
>          saveLocale(invocation, locale);
>
>          if (LOG.isDebugEnabled()) {
> @@ -183,6 +167,54 @@ public class I18nInterceptor extends Abs
>          return result;
>      }
>
> +    /**
> +     * Reads the locale from the session, and if not found from the
> +     * current invocation (=browser)
> +     *
> +     * @param invocation the current invocation
> +     * @param session the current session
> +     * @return the read locale
> +     */
> +    private Locale readStoredLocale(ActionInvocation invocation, Map<String, Object> session) {
> +        // check session for saved locale
> +        Object sessionLocale = session.get(attributeName);
> +        if (sessionLocale != null && sessionLocale instanceof Locale) {
> +            Locale locale = (Locale) sessionLocale;
> +            if (LOG.isDebugEnabled()) {
> +                LOG.debug("applied session locale=#0", locale);
> +            }
> +            return locale;
> +        }
> +
> +        // no overriding locale definition found, stay with current invocation (=browser) locale
> +        Locale locale = invocation.getInvocationContext().getLocale();
> +        if (locale != null && LOG.isDebugEnabled()) {
> +            LOG.debug("applied invocation context locale=#0", locale);
> +        }
> +
> +        return locale;
> +    }
> +
> +    /**
> +     * Creates a Locale object from the request param, which might
> +     * be already a Local or a String
> +     *
> +     * @param requestedLocale the parameter from the request
> +     * @return the Locale
> +     */
> +    private Locale getLocaleFromParam(Object requestedLocale) {
> +        Locale locale = null;
> +        if (requestedLocale != null) {
> +            locale = (requestedLocale instanceof Locale) ?
> +                    (Locale) requestedLocale :
> +                    LocalizedTextUtil.localeFromString(requestedLocale.toString(), null);
> +            if (locale != null && LOG.isDebugEnabled()) {
> +                LOG.debug("applied request locale=#0", locale);
> +            }
> +        }
> +        return locale;
> +    }
> +
>      private Object findLocaleParameter(Map<String, Object> params, String parameterName) {
>          Object requestedLocale = params.remove(parameterName);
>          if (requestedLocale != null && requestedLocale.getClass().isArray()
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org