diff --git a/pom.xml b/pom.xml index 8bb4f42..dd0a887 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ org.apache.sling.i18n - 2.6.3-SNAPSHOT + 2.7.0-SNAPSHOT Apache Sling I18N Support Support for creating Java I18N ResourceBundles from repository resources. @@ -124,6 +124,12 @@ javax.servlet-api provided + + jakarta.servlet + jakarta.servlet-api + 5.0.0 + provided + org.osgi diff --git a/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java b/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java index 4d0ec95..73d5657 100644 --- a/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java +++ b/src/main/java/org/apache/sling/i18n/DefaultLocaleResolver.java @@ -23,8 +23,6 @@ import java.util.List; import java.util.Locale; -import javax.servlet.http.HttpServletRequest; - import org.apache.sling.api.SlingHttpServletRequest; /** @@ -41,18 +39,32 @@ public class DefaultLocaleResolver implements LocaleResolver, RequestLocaleResol * List. */ public List resolveLocale(final SlingHttpServletRequest request) { - return this.resolveLocale((HttpServletRequest)request); + return this.resolveLocale((javax.servlet.http.HttpServletRequest)request); } /** * @see org.apache.sling.i18n.RequestLocaleResolver#resolveLocale(javax.servlet.http.HttpServletRequest) */ - public List resolveLocale(final HttpServletRequest request) { + public List resolveLocale(final javax.servlet.http.HttpServletRequest request) { Enumeration locales = request.getLocales(); - ArrayList localeList = new ArrayList(); + ArrayList localeList = new ArrayList<>(); while (locales.hasMoreElements()) { localeList.add((Locale) locales.nextElement()); } return localeList; } + + /** + * @see org.apache.sling.i18n.RequestLocaleResolver#resolveLocale(jakarta.servlet.http.HttpServletRequest) + */ + @Override + public List resolveLocale(jakarta.servlet.http.HttpServletRequest request) { + Enumeration locales = request.getLocales(); + ArrayList localeList = new ArrayList<>(); + while (locales.hasMoreElements()) { + localeList.add((Locale) locales.nextElement()); + } + return localeList; + } + } diff --git a/src/main/java/org/apache/sling/i18n/RequestLocaleResolver.java b/src/main/java/org/apache/sling/i18n/RequestLocaleResolver.java index 75b1205..e58e658 100644 --- a/src/main/java/org/apache/sling/i18n/RequestLocaleResolver.java +++ b/src/main/java/org/apache/sling/i18n/RequestLocaleResolver.java @@ -21,8 +21,6 @@ import java.util.List; import java.util.Locale; -import javax.servlet.http.HttpServletRequest; - /** * The RequestLocaleResolver service interface may be implemented by a * service registered under this name to allow the resolution of the request @@ -51,6 +49,26 @@ public interface RequestLocaleResolver { * @return The list of Locales to use for internationalization * of request processing */ - List resolveLocale(HttpServletRequest request); + List resolveLocale(javax.servlet.http.HttpServletRequest request); + + /** + * Return a non-null but possibly empty list of + * Locale instances to consider for localization of the current + * request. The list returned is assumed to be ordered by preference where + * the first entry is the preferred Locale and the last entry is + * the least preferred Locale. + *

+ * Returning an empty list is equivalent to returning a singleton list whose + * single entry is the {@link ResourceBundleProvider#getDefaultLocale()}. + * + * @param request The HttpServletRequest providing hints + * and information for the Locale resolution. + * @return The list of Locales to use for internationalization + * of request processing + * @since 2.3 + */ + default List resolveLocale(jakarta.servlet.http.HttpServletRequest request) { + throw new UnsupportedOperationException(); + } } diff --git a/src/main/java/org/apache/sling/i18n/package-info.java b/src/main/java/org/apache/sling/i18n/package-info.java index c729ae7..f286a8c 100644 --- a/src/main/java/org/apache/sling/i18n/package-info.java +++ b/src/main/java/org/apache/sling/i18n/package-info.java @@ -17,7 +17,7 @@ * under the License. */ -@org.osgi.annotation.versioning.Version("2.2.1") +@org.osgi.annotation.versioning.Version("2.3.0") package org.apache.sling.i18n; diff --git a/src/test/java/org/apache/sling/i18n/DefaultLocaleResolverTest.java b/src/test/java/org/apache/sling/i18n/DefaultLocaleResolverTest.java new file mode 100644 index 0000000..1bf14e6 --- /dev/null +++ b/src/test/java/org/apache/sling/i18n/DefaultLocaleResolverTest.java @@ -0,0 +1,77 @@ +/* + * 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 org.apache.sling.i18n; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.junit.Test; +import org.mockito.Mockito; + +/** + * + */ +public class DefaultLocaleResolverTest { + + private DefaultLocaleResolver localeResolver = new DefaultLocaleResolver(); + + /** + * Test method for {@link org.apache.sling.i18n.DefaultLocaleResolver#resolveLocale(org.apache.sling.api.SlingHttpServletRequest)}. + */ + @Test + public void testResolveLocaleSlingHttpServletRequest() { + SlingHttpServletRequest req = Mockito.mock(SlingHttpServletRequest.class); + Mockito.doReturn(Collections.enumeration(Arrays.asList(Locale.ENGLISH, Locale.GERMAN))) + .when(req).getLocales(); + List locales = localeResolver.resolveLocale(req); + assertNotNull(locales); + assertEquals(2, locales.size()); + } + + /** + * Test method for {@link org.apache.sling.i18n.DefaultLocaleResolver#resolveLocale(javax.servlet.http.HttpServletRequest)}. + */ + @Test + public void testResolveLocaleHttpServletRequest() { + javax.servlet.http.HttpServletRequest req = Mockito.mock(javax.servlet.http.HttpServletRequest.class); + Mockito.doReturn(Collections.enumeration(Arrays.asList(Locale.ENGLISH, Locale.GERMAN))) + .when(req).getLocales(); + List locales = localeResolver.resolveLocale(req); + assertNotNull(locales); + assertEquals(2, locales.size()); + } + + /** + * Test method for {@link org.apache.sling.i18n.DefaultLocaleResolver#resolveLocale(jakarta.servlet.http.HttpServletRequest)}. + */ + @Test + public void testResolveLocaleHttpServletRequest1() { + jakarta.servlet.http.HttpServletRequest req = Mockito.mock(jakarta.servlet.http.HttpServletRequest.class); + Mockito.doReturn(Collections.enumeration(Arrays.asList(Locale.ENGLISH, Locale.GERMAN))) + .when(req).getLocales(); + List locales = localeResolver.resolveLocale(req); + assertNotNull(locales); + assertEquals(2, locales.size()); + } + +} diff --git a/src/test/java/org/apache/sling/i18n/RequestLocaleResolverTest.java b/src/test/java/org/apache/sling/i18n/RequestLocaleResolverTest.java new file mode 100644 index 0000000..d9bdd47 --- /dev/null +++ b/src/test/java/org/apache/sling/i18n/RequestLocaleResolverTest.java @@ -0,0 +1,48 @@ +/* + * 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 org.apache.sling.i18n; + +import static org.junit.Assert.assertThrows; + +import java.util.List; +import java.util.Locale; + +import org.junit.Test; +import org.mockito.Mockito; + +/** + * + */ +public class RequestLocaleResolverTest { + + /** + * Test method for {@link org.apache.sling.i18n.RequestLocaleResolver#resolveLocale(jakarta.servlet.http.HttpServletRequest)}. + */ + @Test + public void testResolveLocaleHttpServletRequest() { + RequestLocaleResolver requestLocaleResolver = new RequestLocaleResolver() { + @Override + public List resolveLocale(javax.servlet.http.HttpServletRequest request) { + throw new UnsupportedOperationException(); + } + }; + + jakarta.servlet.http.HttpServletRequest jakartaReq = Mockito.mock(jakarta.servlet.http.HttpServletRequest.class); + assertThrows(UnsupportedOperationException.class, () -> requestLocaleResolver.resolveLocale(jakartaReq)); + } + +}