Skip to content

Commit

Permalink
Merge pull request #931 from apache/fix/WW-5422-trimable
Browse files Browse the repository at this point in the history
WW-5422 Fixes support for trimable locale string in request
  • Loading branch information
lukaszlenart committed May 13, 2024
2 parents 195b0e5 + 0a57cac commit 49eda37
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 23 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/com/opensymphony/xwork2/ActionSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public boolean isValidLocale(Locale locale) {
return getLocaleProvider().isValidLocale(locale);
}

@Override
public Locale toLocale(String localeStr) {
return getLocaleProvider().toLocale(localeStr);
}

@Override
public boolean hasKey(String key) {
return getTextProvider().hasKey(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,23 @@ public Locale getLocale() {

@Override
public boolean isValidLocaleString(String localeStr) {
Locale locale = this.toLocale(localeStr);
return isValidLocale(locale);
}

@Override
public boolean isValidLocale(Locale locale) {
return locale != null && LocaleUtils.isAvailableLocale(locale);
}

@Override
public Locale toLocale(String localeStr) {
Locale locale = null;
try {
locale = LocaleUtils.toLocale(StringUtils.trimToNull(localeStr));
} catch (IllegalArgumentException e) {
LOG.warn(new ParameterizedMessage("Cannot convert [{}] to proper locale", localeStr), e);
}
return isValidLocale(locale);
}

@Override
public boolean isValidLocale(Locale locale) {
return LocaleUtils.isAvailableLocale(locale);
return locale;
}
}
16 changes: 16 additions & 0 deletions core/src/main/java/com/opensymphony/xwork2/LocaleProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package com.opensymphony.xwork2;

import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Locale;


Expand Down Expand Up @@ -58,4 +61,17 @@ public interface LocaleProvider {
*/
boolean isValidLocale(Locale locale);

/**
* Tries to convert provided locale string into {@link Locale} or returns null
* @param localeStr a String representing locale, e.g.: en_EN
* @return instance of {@link Locale} or null
* @since Struts 6.5.0
*/
default Locale toLocale(String localeStr) {
try {
return LocaleUtils.toLocale(StringUtils.trimToNull(localeStr));
} catch (IllegalArgumentException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,15 @@ public boolean isValidLocale(Locale locale) {
return localeProvider.isValidLocale(locale);
}

@Override
public Locale toLocale(String localeStr) {
return localeProvider.toLocale(localeStr);
}

public boolean hasKey(String key) {
return textProvider.hasKey(key);
}

public String getText(String aTextName) {
return textProvider.getText(aTextName);
}
Expand Down Expand Up @@ -280,6 +285,11 @@ public boolean isValidLocaleString(String localeStr) {
public boolean isValidLocale(Locale locale) {
return getLocaleProvider().isValidLocale(locale);
}

@Override
public Locale toLocale(String localeStr) {
return getLocaleProvider().toLocale(localeStr);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.TextParseUtil;
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;
Expand Down Expand Up @@ -85,7 +84,7 @@ public void setRequestCookieParameterName(String requestCookieParameterName) {
}

public void setLocaleStorage(String storageName) {
if (storageName == null || "".equals(storageName)) {
if (storageName == null || storageName.isEmpty()) {
this.storage = Storage.ACCEPT_LANGUAGE;
} else {
try {
Expand Down Expand Up @@ -169,27 +168,21 @@ protected LocaleHandler getLocaleHandler(ActionInvocation invocation) {
}

/**
* Creates a Locale object from the request param, which might
* be already a Local or a String
* Creates a Locale object from the request param
*
* @param requestedLocale the parameter from the request
* @return the Locale
* @return instance of {@link Locale} or null
*/
protected Locale getLocaleFromParam(Object requestedLocale) {
protected Locale getLocaleFromParam(String requestedLocale) {
LocaleProvider localeProvider = localeProviderFactory.createLocaleProvider();

Locale locale = null;
if (requestedLocale != null) {
if (requestedLocale instanceof Locale) {
locale = (Locale) requestedLocale;
} else {
String localeStr = requestedLocale.toString();
if (localeProvider.isValidLocaleString(localeStr)) {
locale = LocaleUtils.toLocale(localeStr);
} else {
locale = localeProvider.getLocale();
}
locale = localeProvider.toLocale(requestedLocale);
if (locale == null) {
locale = localeProvider.getLocale();
}

if (locale != null) {
LOG.debug("Found locale: {}", locale);
}
Expand Down Expand Up @@ -285,7 +278,7 @@ protected AcceptLanguageLocaleHandler(ActionInvocation invocation) {
@Override
@SuppressWarnings("rawtypes")
public Locale find() {
if (supportedLocale.size() > 0) {
if (!supportedLocale.isEmpty()) {
Enumeration locales = actionInvocation.getInvocationContext().getServletRequest().getLocales();
while (locales.hasMoreElements()) {
Locale locale = (Locale) locales.nextElement();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* 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.
*/
package com.opensymphony.xwork2;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Locale;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class DefaultLocaleProviderTest {

private DefaultLocaleProvider provider;

@Before
public void setUp() throws Exception {
provider = new DefaultLocaleProvider();
}

@BeforeClass
public static void beforeClass() throws Exception {
ActionContext.of().bind();
}

@AfterClass
public static void afterClass() throws Exception {
ActionContext.clear();
}

@Test
public void getLocale() {
// given
ActionContext.getContext().withLocale(Locale.ITALY);

// when
Locale actual = provider.getLocale();

// then
assertEquals(Locale.ITALY, actual);
}

@Test
public void getLocaleNull() {
// given
ActionContext backup = ActionContext.getContext();
ActionContext.clear();

// when
Locale actual = provider.getLocale();

// then
assertNull(actual);
ActionContext.bind(backup);
}

@Test
public void toLocale() {
// given
ActionContext.getContext().withLocale(Locale.GERMAN);

// when
Locale actual = provider.toLocale("it");

// then
assertEquals(Locale.ITALIAN, actual);
}

@Test
public void toLocaleFull() {
// given
ActionContext.getContext().withLocale(Locale.GERMAN);

// when
Locale actual = provider.toLocale("it_IT");

// then
assertEquals(Locale.ITALY, actual);
}

@Test
public void toLocaleTrimEndOfLine() {
// given
ActionContext.getContext().withLocale(Locale.GERMAN);

// when
Locale actual = provider.toLocale("it_IT\n");

// then
assertEquals(Locale.ITALY, actual);
}

@Test
public void toLocaleTrimEmptySpace() {
// given
ActionContext.getContext().withLocale(Locale.GERMAN);

// when
Locale actual = provider.toLocale(" it_IT ");

// then
assertEquals(Locale.ITALY, actual);
}

@Test
public void isValidLocaleNull() {
// given
ActionContext.getContext().withLocale(Locale.GERMAN);

// when
boolean actual = provider.isValidLocale(null);

// then
assertFalse(actual);
}

@Test
public void isValidLocale() {
// given
ActionContext.getContext().withLocale(Locale.GERMAN);

// when
boolean actual = provider.isValidLocale(Locale.ITALIAN);

// then
assertTrue(actual);
}

@Test
public void isValidLocaleString() {
// given
ActionContext.getContext().withLocale(Locale.GERMAN);

// when
boolean actual = provider.isValidLocaleString("it");

// then
assertTrue(actual);
}

@Test
public void isValidLocaleStringNot() {
// given
ActionContext.getContext().withLocale(Locale.GERMAN);

// when
boolean actual = provider.isValidLocaleString("italy");

// then
assertFalse(actual);
}

}
Loading

0 comments on commit 49eda37

Please sign in to comment.