diff --git a/jhipster-framework/src/main/java/tech/jhipster/config/locale/AngularCookieLocaleResolver.java b/jhipster-framework/src/main/java/tech/jhipster/config/locale/AngularCookieLocaleResolver.java deleted file mode 100644 index 21ff862f8..000000000 --- a/jhipster-framework/src/main/java/tech/jhipster/config/locale/AngularCookieLocaleResolver.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2016-2023 the original author or authors from the JHipster project. - * - * This file is part of the JHipster project, see https://www.jhipster.tech/ - * for more information. - * - * Licensed 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 tech.jhipster.config.locale; - -import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.i18n.LocaleContext; -import org.springframework.context.i18n.TimeZoneAwareLocaleContext; -import org.springframework.util.StringUtils; -import org.springframework.web.servlet.i18n.CookieLocaleResolver; -import org.springframework.web.util.WebUtils; - -import java.util.Locale; -import java.util.TimeZone; - -/** - * Angular cookie saved the locale with a double quote (%22en%22). So the default - * CookieLocaleResolver#StringUtils.parseLocaleString(localePart) - * is not able to parse the locale. - *

- * This class will check if a double quote has been added, if so it will remove it. - */ -public class AngularCookieLocaleResolver extends CookieLocaleResolver { - - private final Logger logger = LoggerFactory.getLogger(AngularCookieLocaleResolver.class); - - /** - * Constant QUOTE="%22" - */ - public static final String QUOTE = "%22"; - - public AngularCookieLocaleResolver() { - super(); - } - - public AngularCookieLocaleResolver(String cookieName) { - super(cookieName); - } - - /** - * {@inheritDoc} - */ - @Override - public Locale resolveLocale(HttpServletRequest request) { - parseAngularCookieIfNecessary(request); - return (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME); - } - - /** - * {@inheritDoc} - */ - @Override - public LocaleContext resolveLocaleContext(HttpServletRequest request) { - parseAngularCookieIfNecessary(request); - return new TimeZoneAwareLocaleContext() { - @Override - public Locale getLocale() { - return (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME); - } - - @Override - public TimeZone getTimeZone() { - return (TimeZone) request.getAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME); - } - }; - } - - private void parseAngularCookieIfNecessary(HttpServletRequest request) { - if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) { - // Retrieve and parse cookie value. - Cookie cookie = WebUtils.getCookie(request, DEFAULT_COOKIE_NAME); - Locale locale = null; - TimeZone timeZone = null; - if (cookie != null) { - String value = cookie.getValue(); - - // Remove the double quote - value = StringUtils.replace(value, QUOTE, ""); - - String localePart = value; - String timeZonePart = null; - int spaceIndex = localePart.indexOf(' '); - if (spaceIndex != -1) { - localePart = value.substring(0, spaceIndex); - timeZonePart = value.substring(spaceIndex + 1); - } - locale = !"-".equals(localePart) ? StringUtils.parseLocaleString(localePart.replace('-', '_')) : null; - if (timeZonePart != null) { - timeZone = StringUtils.parseTimeZoneString(timeZonePart); - } - if (logger.isTraceEnabled()) { - logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale + - "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : "")); - } - } - request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, - locale != null ? locale : determineDefaultLocale(request)); - - request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME, - timeZone != null ? timeZone : determineDefaultTimeZone(request)); - } - } - - String quote(String string) { - return QUOTE + string + QUOTE; - } -} diff --git a/jhipster-framework/src/main/java/tech/jhipster/config/locale/package-info.java b/jhipster-framework/src/main/java/tech/jhipster/config/locale/package-info.java deleted file mode 100644 index d32c52354..000000000 --- a/jhipster-framework/src/main/java/tech/jhipster/config/locale/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Locale specific code. - */ -package tech.jhipster.config.locale; diff --git a/jhipster-framework/src/test/java/tech/jhipster/config/locale/AngularCookieLocaleResolverTest.java b/jhipster-framework/src/test/java/tech/jhipster/config/locale/AngularCookieLocaleResolverTest.java deleted file mode 100644 index 8a225cf30..000000000 --- a/jhipster-framework/src/test/java/tech/jhipster/config/locale/AngularCookieLocaleResolverTest.java +++ /dev/null @@ -1,270 +0,0 @@ -/* - * Copyright 2016-2023 the original author or authors from the JHipster project. - * - * This file is part of the JHipster project, see https://www.jhipster.tech/ - * for more information. - * - * Licensed 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 tech.jhipster.config.locale; - -import tech.jhipster.test.LogbackRecorder; -import tech.jhipster.test.LogbackRecorder.Event; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.MockitoAnnotations; -import org.springframework.context.i18n.LocaleContext; -import org.springframework.context.i18n.TimeZoneAwareLocaleContext; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; - -import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.time.ZoneId; -import java.util.List; -import java.util.Locale; -import java.util.TimeZone; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.springframework.web.servlet.i18n.CookieLocaleResolver.DEFAULT_COOKIE_NAME; -import static org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME; -import static org.springframework.web.servlet.i18n.CookieLocaleResolver.TIME_ZONE_REQUEST_ATTRIBUTE_NAME; - -class AngularCookieLocaleResolverTest { - - private static final Locale LOCALE_DEFAULT = Locale.UK; - private static final Locale LOCALE_CUSTOM = Locale.FRANCE; - private static final TimeZone TIMEZONE_CUSTOM = TimeZone.getTimeZone(ZoneId.of("GMT")); - private static final TimeZone TIMEZONE_DEFAULT = TimeZone.getTimeZone(ZoneId.of("GMT+01:00")); - - private HttpServletRequest request; - private HttpServletResponse response; - private AngularCookieLocaleResolver resolver; - private LogbackRecorder recorder; - - @Captor - private ArgumentCaptor captor; - - @BeforeEach - void setup() { - MockitoAnnotations.openMocks(this); - - request = spy(new MockHttpServletRequest()); - - response = spy(MockHttpServletResponse.class); - resolver = new AngularCookieLocaleResolver(); - resolver.setDefaultLocale(LOCALE_DEFAULT); - resolver.setDefaultTimeZone(TIMEZONE_DEFAULT); - - recorder = LogbackRecorder.forClass(resolver.getClass()).reset().capture("DEBUG"); - } - - @AfterEach - void teardown() { - recorder.release(); - } - - @Test - void testDefaults() { - when(request.getCookies()).thenReturn(new Cookie[]{}); - - LocaleContext context = resolver.resolveLocaleContext(request); - - assertThat(context).isNotNull(); - assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class); - assertThat(((TimeZoneAwareLocaleContext) context).getLocale()).isEqualTo(LOCALE_DEFAULT); - assertThat(((TimeZoneAwareLocaleContext) context).getTimeZone()).isEqualTo(TIMEZONE_DEFAULT); - - List events = recorder.play(); - assertThat(events).isEmpty(); - } - - @Test - void testPresets() { - when(request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME)).thenReturn(LOCALE_DEFAULT); - when(request.getAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME)).thenReturn(TIMEZONE_DEFAULT); - - LocaleContext context = resolver.resolveLocaleContext(request); - - assertThat(context).isNotNull(); - assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class); - Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale(); - TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone(); - - assertThat(locale).isNotNull(); - assertThat(locale).isEqualTo(LOCALE_DEFAULT); - assertThat(zone).isEqualTo(TIMEZONE_DEFAULT); - - List events = recorder.play(); - assertThat(events).isEmpty(); - } - - @Test - void testLocale() { - String value = LOCALE_CUSTOM.toString(); - Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value); - when(request.getCookies()).thenReturn(new Cookie[]{cookie}); - - Locale locale = resolver.resolveLocale(request); - - assertThat(locale).isNotNull(); - assertThat(locale).isEqualTo(LOCALE_CUSTOM); - - List events = recorder.play(); - assertThat(events).isEmpty(); - } - - @Test - void testCookieLocaleWithQuotes() { - String value = resolver.quote(LOCALE_CUSTOM.toString()); - Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value); - when(request.getCookies()).thenReturn(new Cookie[]{cookie}); - - Locale locale = resolver.resolveLocale(request); - - assertThat(locale).isNotNull(); - assertThat(locale).isEqualTo(LOCALE_CUSTOM); - - List events = recorder.play(); - assertThat(events).isEmpty(); - } - - @Test - void testTimeZone() { - String value = "- " + TIMEZONE_CUSTOM.getID(); - Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value); - when(request.getCookies()).thenReturn(new Cookie[]{cookie}); - - LocaleContext context = resolver.resolveLocaleContext(request); - - assertThat(context).isNotNull(); - assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class); - Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale(); - TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone(); - assertThat(locale).isEqualTo(LOCALE_DEFAULT); - assertThat(zone).isEqualTo(TIMEZONE_CUSTOM); - - List events = recorder.play(); - assertThat(events).isEmpty(); - } - - @Test - void testTimeZoneWithQuotes() { - String value = resolver.quote("- " + TIMEZONE_CUSTOM.getID()); - Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value); - when(request.getCookies()).thenReturn(new Cookie[]{cookie}); - - LocaleContext context = resolver.resolveLocaleContext(request); - - assertThat(context).isNotNull(); - assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class); - Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale(); - TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone(); - assertThat(locale).isEqualTo(LOCALE_DEFAULT); - assertThat(zone).isEqualTo(TIMEZONE_CUSTOM); - - List events = recorder.play(); - assertThat(events).isEmpty(); - } - - @Test - void testLocaleAndTimeZone() { - String value = LOCALE_CUSTOM + " " + TIMEZONE_CUSTOM.getID(); - Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value); - when(request.getCookies()).thenReturn(new Cookie[]{cookie}); - - LocaleContext context = resolver.resolveLocaleContext(request); - - assertThat(context).isNotNull(); - assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class); - Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale(); - TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone(); - assertThat(locale).isEqualTo(LOCALE_CUSTOM); - assertThat(zone).isEqualTo(TIMEZONE_CUSTOM); - - List events = recorder.play(); - assertThat(events).isEmpty(); - } - - @Test - void testLocaleAndTimeZoneWithQuotes() { - String value = resolver.quote(LOCALE_CUSTOM.toString() + " " + TIMEZONE_CUSTOM.getID()); - Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value); - when(request.getCookies()).thenReturn(new Cookie[]{cookie}); - - LocaleContext context = resolver.resolveLocaleContext(request); - - assertThat(context).isNotNull(); - assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class); - Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale(); - TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone(); - assertThat(locale).isEqualTo(LOCALE_CUSTOM); - assertThat(zone).isEqualTo(TIMEZONE_CUSTOM); - - List events = recorder.play(); - assertThat(events).isEmpty(); - } - - @Test - void testTraceLogLocale() { - recorder.release(); - recorder.capture("TRACE"); - - String value = LOCALE_CUSTOM.toString(); - Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value); - when(request.getCookies()).thenReturn(new Cookie[]{cookie}); - - Locale locale = resolver.resolveLocale(request); - - List events = recorder.play(); - assertThat(events).hasSize(1); - - Event event = events.get(0); - assertThat(event.getLevel()).isEqualTo("TRACE"); - assertThat(event.getMessage()).isEqualTo("Parsed cookie value [" + value + "] into locale '" + locale + "'"); - assertThat(event.getThrown()).isNull(); - } - - @Test - void testTraceLogLocaleAndTimeZone() { - recorder.release(); - recorder.capture("TRACE"); - - String value = LOCALE_CUSTOM + " " + TIMEZONE_CUSTOM.getID(); - Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value); - when(request.getCookies()).thenReturn(new Cookie[]{cookie}); - - LocaleContext context = resolver.resolveLocaleContext(request); - - assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class); - Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale(); - TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone(); - - List events = recorder.play(); - assertThat(events).hasSize(1); - - Event event = events.get(0); - assertThat(event.getLevel()).isEqualTo("TRACE"); - assertThat(event.getMessage()).isEqualTo("Parsed cookie value [" + value + "] into locale '" + locale + "' " + - "and time zone '" + zone.getID() + "'"); - assertThat(event.getThrown()).isNull(); - } -}