mirror of
https://github.com/apache/struts.git
synced 2026-08-31 19:35:40 +00:00
WW-4791 Stops using DefaultLocalizedTextProvider#localeFromString
This commit is contained in:
@@ -15,9 +15,10 @@
|
||||
*/
|
||||
package com.opensymphony.xwork2;
|
||||
|
||||
import com.opensymphony.xwork2.util.DefaultLocalizedTextProvider;
|
||||
import org.apache.commons.lang3.LocaleUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.message.ParameterizedMessage;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
@@ -42,7 +43,13 @@ public class DefaultLocaleProvider implements LocaleProvider {
|
||||
|
||||
@Override
|
||||
public boolean isValidLocaleString(String localeStr) {
|
||||
return isValidLocale(DefaultLocalizedTextProvider.localeFromString(localeStr, getLocale()));
|
||||
Locale locale = null;
|
||||
try {
|
||||
locale = LocaleUtils.toLocale(localeStr);
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.warn(new ParameterizedMessage("Cannot convert [{}] to proper locale", localeStr, e));
|
||||
}
|
||||
return isValidLocale(locale);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -152,7 +152,10 @@ public class DefaultLocalizedTextProvider implements LocalizedTextProvider {
|
||||
* @param localeStr The locale String to parse.
|
||||
* @param defaultLocale The locale to use if localeStr is <tt>null</tt>.
|
||||
* @return requested Locale
|
||||
*
|
||||
* @deprecated please use {@link org.apache.commons.lang3.LocaleUtils#toLocale(String)}
|
||||
*/
|
||||
@Deprecated
|
||||
public static Locale localeFromString(String localeStr, Locale defaultLocale) {
|
||||
if ((localeStr == null) || (localeStr.trim().length() == 0) || ("_".equals(localeStr))) {
|
||||
if (defaultLocale != null) {
|
||||
|
||||
@@ -39,10 +39,12 @@ import com.opensymphony.xwork2.util.location.LocatableProperties;
|
||||
import com.opensymphony.xwork2.util.location.Location;
|
||||
import com.opensymphony.xwork2.util.location.LocationUtils;
|
||||
import com.opensymphony.xwork2.util.profiling.UtilTimerStack;
|
||||
import org.apache.commons.lang3.LocaleUtils;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.message.ParameterizedMessage;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.StrutsException;
|
||||
@@ -672,14 +674,7 @@ public class Dispatcher {
|
||||
extraContext.put(ActionContext.SESSION, sessionMap);
|
||||
extraContext.put(ActionContext.APPLICATION, applicationMap);
|
||||
|
||||
Locale locale;
|
||||
if (defaultLocale != null) {
|
||||
locale = DefaultLocalizedTextProvider.localeFromString(defaultLocale, request.getLocale());
|
||||
} else {
|
||||
locale = request.getLocale();
|
||||
}
|
||||
|
||||
extraContext.put(ActionContext.LOCALE, locale);
|
||||
extraContext.put(ActionContext.LOCALE, getLocale(request));
|
||||
|
||||
extraContext.put(StrutsStatics.HTTP_REQUEST, request);
|
||||
extraContext.put(StrutsStatics.HTTP_RESPONSE, response);
|
||||
@@ -697,6 +692,22 @@ public class Dispatcher {
|
||||
return extraContext;
|
||||
}
|
||||
|
||||
protected Locale getLocale(HttpServletRequest request) {
|
||||
Locale locale;
|
||||
if (defaultLocale != null) {
|
||||
try {
|
||||
locale = LocaleUtils.toLocale(defaultLocale);
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.warn(new ParameterizedMessage("Cannot convert 'struts.locale' = [{}] to proper locale, defaulting to request locale [{}]",
|
||||
defaultLocale, request.getLocale()), e);
|
||||
locale = request.getLocale();
|
||||
}
|
||||
} else {
|
||||
locale = request.getLocale();
|
||||
}
|
||||
return locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the path to save uploaded files to (this is configurable).
|
||||
*
|
||||
@@ -754,10 +765,7 @@ public class Dispatcher {
|
||||
encoding = "UTF-8";
|
||||
}
|
||||
|
||||
Locale locale = null;
|
||||
if (defaultLocale != null) {
|
||||
locale = DefaultLocalizedTextProvider.localeFromString(defaultLocale, request.getLocale());
|
||||
}
|
||||
Locale locale = getLocale(request);
|
||||
|
||||
if (encoding != null) {
|
||||
applyEncoding(request, encoding);
|
||||
|
||||
@@ -25,7 +25,7 @@ import com.opensymphony.xwork2.LocaleProvider;
|
||||
import com.opensymphony.xwork2.LocaleProviderFactory;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
||||
import com.opensymphony.xwork2.util.DefaultLocalizedTextProvider;
|
||||
import org.apache.commons.lang.LocaleUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.message.ParameterizedMessage;
|
||||
@@ -166,7 +166,9 @@ public class I18nInterceptor extends AbstractInterceptor {
|
||||
} else {
|
||||
String localeStr = requestedLocale.toString();
|
||||
if (localeProvider.isValidLocaleString(localeStr)) {
|
||||
locale = DefaultLocalizedTextProvider.localeFromString(requestedLocale.toString(), null);
|
||||
locale = LocaleUtils.toLocale(localeStr);
|
||||
} else {
|
||||
locale = localeProvider.getLocale();
|
||||
}
|
||||
}
|
||||
if (locale != null) {
|
||||
|
||||
+4
-6
@@ -26,6 +26,7 @@ import com.opensymphony.xwork2.inject.ContainerBuilder;
|
||||
import com.opensymphony.xwork2.util.DefaultLocalizedTextProvider;
|
||||
import com.opensymphony.xwork2.util.location.LocatableProperties;
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.commons.lang3.LocaleUtils;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
|
||||
import java.util.Locale;
|
||||
@@ -41,7 +42,7 @@ public class PropertiesConfigurationProviderTest extends TestCase {
|
||||
|
||||
ContainerBuilder builder = new ContainerBuilder();
|
||||
builder.constant("foo", "bar");
|
||||
builder.constant("struts.locale", "DE_de");
|
||||
builder.constant("struts.locale", "de_DE");
|
||||
|
||||
PropertiesConfigurationProvider prov = new PropertiesConfigurationProvider();
|
||||
prov.register(builder, new LocatableProperties());
|
||||
@@ -49,7 +50,7 @@ public class PropertiesConfigurationProviderTest extends TestCase {
|
||||
Container container = builder.create(true);
|
||||
|
||||
String localeStr = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
|
||||
Locale locale = DefaultLocalizedTextProvider.localeFromString(localeStr, Locale.FRANCE);
|
||||
Locale locale = LocaleUtils.toLocale(localeStr);
|
||||
|
||||
assertNotNull(locale);
|
||||
assertEquals("DE", locale.getCountry());
|
||||
@@ -68,11 +69,8 @@ public class PropertiesConfigurationProviderTest extends TestCase {
|
||||
Container container = builder.create(true);
|
||||
|
||||
String localeStr = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
|
||||
Locale locale = DefaultLocalizedTextProvider.localeFromString(localeStr, Locale.getDefault());
|
||||
|
||||
assertNotNull(locale);
|
||||
Locale vmLocale = Locale.getDefault();
|
||||
assertEquals(locale, vmLocale);
|
||||
assertNull(localeStr);
|
||||
}
|
||||
|
||||
public void testDefaultSettings() throws Exception {
|
||||
|
||||
@@ -102,7 +102,6 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
Mock mock = new Mock(HttpServletRequest.class);
|
||||
mock.expectAndReturn("getCharacterEncoding", "utf-8");
|
||||
mock.expectAndReturn("getHeader", "X-Requested-With", "");
|
||||
mock.expectAndReturn("getLocale", Locale.getDefault());
|
||||
mock.expectAndReturn("getCharacterEncoding", "utf-8");
|
||||
HttpServletRequest req = (HttpServletRequest) mock.proxy();
|
||||
HttpServletResponse res = new MockHttpServletResponse();
|
||||
|
||||
@@ -90,19 +90,8 @@ public class I18nInterceptorTest extends TestCase {
|
||||
assertEquals(denmark, mai.getInvocationContext().getLocale()); // should create a locale object
|
||||
}
|
||||
|
||||
public void testCountryOnlyLocale() throws Exception {
|
||||
prepare(I18nInterceptor.DEFAULT_PARAMETER, "NL");
|
||||
interceptor.intercept(mai);
|
||||
|
||||
assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined()); // should have been removed
|
||||
|
||||
Locale denmark = new Locale("NL");
|
||||
assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
|
||||
assertEquals(denmark, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
|
||||
}
|
||||
|
||||
public void testLanguageOnlyLocale() throws Exception {
|
||||
prepare(I18nInterceptor.DEFAULT_PARAMETER, "da_");
|
||||
prepare(I18nInterceptor.DEFAULT_PARAMETER, "da");
|
||||
interceptor.intercept(mai);
|
||||
|
||||
assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined()); // should have been removed
|
||||
|
||||
+20
-9
@@ -26,10 +26,11 @@ import com.opensymphony.xwork2.ActionProxy;
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.util.DefaultLocalizedTextProvider;
|
||||
import org.apache.commons.lang3.LocaleUtils;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.message.ParameterizedMessage;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.StrutsException;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
@@ -393,14 +394,7 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
|
||||
extraContext.put(ActionContext.SESSION, sessionMap);
|
||||
extraContext.put(ActionContext.APPLICATION, applicationMap);
|
||||
|
||||
String defaultLocale = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
|
||||
Locale locale;
|
||||
if (defaultLocale != null) {
|
||||
locale = DefaultLocalizedTextProvider.localeFromString(defaultLocale, request.getLocale());
|
||||
} else {
|
||||
locale = request.getLocale();
|
||||
}
|
||||
extraContext.put(ActionContext.LOCALE, locale);
|
||||
extraContext.put(ActionContext.LOCALE, getLocale(request));
|
||||
|
||||
extraContext.put(StrutsStatics.STRUTS_PORTLET_CONTEXT, getPortletContext());
|
||||
extraContext.put(REQUEST, request);
|
||||
@@ -424,6 +418,23 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
|
||||
return extraContext;
|
||||
}
|
||||
|
||||
protected Locale getLocale(PortletRequest request) {
|
||||
String defaultLocale = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
|
||||
Locale locale;
|
||||
if (defaultLocale != null) {
|
||||
try {
|
||||
locale = LocaleUtils.toLocale(defaultLocale);
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.warn(new ParameterizedMessage("Cannot convert 'struts.locale' = [{}] to proper locale, defaulting to request locale [{}]",
|
||||
defaultLocale, request.getLocale()), e);
|
||||
locale = request.getLocale();
|
||||
}
|
||||
} else {
|
||||
locale = request.getLocale();
|
||||
}
|
||||
return locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the action and executes it. This method first creates the action
|
||||
* context from the given parameters then loads an <tt>ActionProxy</tt>
|
||||
|
||||
Reference in New Issue
Block a user