From 555c6383b930418ed4ef86a0916eadb3e04b7961 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Fri, 9 Feb 2024 23:35:26 -0800 Subject: [PATCH] Fix location resolver. --- .../config/common/CommonConfig.java | 34 ++++++++++- .../config/rest/InterceptorsConfig.java | 8 +-- .../filter/UserLocaleInterceptor.java | 57 ------------------- wise-api/src/main/resources/application.yml | 0 .../resources/spring/wisemapping-messages.xml | 15 ----- 5 files changed, 35 insertions(+), 79 deletions(-) delete mode 100644 wise-api/src/main/java/com/wisemapping/filter/UserLocaleInterceptor.java mode change 100755 => 100644 wise-api/src/main/resources/application.yml delete mode 100755 wise-api/src/main/resources/spring/wisemapping-messages.xml diff --git a/wise-api/src/main/java/com/wisemapping/config/common/CommonConfig.java b/wise-api/src/main/java/com/wisemapping/config/common/CommonConfig.java index f945cbdf..43f06c96 100644 --- a/wise-api/src/main/java/com/wisemapping/config/common/CommonConfig.java +++ b/wise-api/src/main/java/com/wisemapping/config/common/CommonConfig.java @@ -1,16 +1,48 @@ package com.wisemapping.config.common; import com.wisemapping.dao.LabelManagerImpl; +import com.wisemapping.model.User; import com.wisemapping.security.AuthenticationProvider; +import com.wisemapping.security.Utils; import com.wisemapping.service.MindmapServiceImpl; import com.wisemapping.util.VelocityEngineUtils; +import jakarta.servlet.http.HttpServletRequest; +import org.jetbrains.annotations.Nullable; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; -import org.springframework.context.annotation.ImportResource; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; + +import java.util.Locale; @ComponentScan(basePackageClasses = {AuthenticationProvider.class, MindmapServiceImpl.class, LabelManagerImpl.class, VelocityEngineUtils.class}) @Import({JPAConfig.class, SecurityConfig.class}) @EnableAutoConfiguration public class CommonConfig { + @Bean + public LocaleResolver localeResolver() { + final LocaleResolver localeResolver = new AcceptHeaderLocaleResolver() { + @Override + public Locale resolveLocale(@Nullable HttpServletRequest request) { + final User user = Utils.getUser(); + Locale result; + String locale = user.getLocale(); + if (user != null && locale != null) { + final String locales[] = locale.split("_"); + Locale.Builder builder = new Locale.Builder().setLanguage(locales[0]); + if (locales.length > 1) { + builder.setVariant(locales[1]); + } + result = builder.build(); + } else { + result = super.resolveLocale(request); + } + return result; + } + }; + return localeResolver; + } } + diff --git a/wise-api/src/main/java/com/wisemapping/config/rest/InterceptorsConfig.java b/wise-api/src/main/java/com/wisemapping/config/rest/InterceptorsConfig.java index ac1c9784..e521b7ea 100644 --- a/wise-api/src/main/java/com/wisemapping/config/rest/InterceptorsConfig.java +++ b/wise-api/src/main/java/com/wisemapping/config/rest/InterceptorsConfig.java @@ -18,7 +18,6 @@ package com.wisemapping.config.rest; import com.wisemapping.filter.RequestPropertiesInterceptor; -import com.wisemapping.filter.UserLocaleInterceptor; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; @@ -27,17 +26,14 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration -@ComponentScan(basePackageClasses = UserLocaleInterceptor.class) +@ComponentScan(basePackageClasses = RequestPropertiesInterceptor.class) public class InterceptorsConfig implements WebMvcConfigurer { - @Autowired - private UserLocaleInterceptor userLocaleInterceptor; - @Autowired private RequestPropertiesInterceptor requestPropertiesInterceptor; @Override public void addInterceptors(@NotNull final InterceptorRegistry registry) { - registry.addInterceptor(userLocaleInterceptor); registry.addInterceptor(requestPropertiesInterceptor); } + } \ No newline at end of file diff --git a/wise-api/src/main/java/com/wisemapping/filter/UserLocaleInterceptor.java b/wise-api/src/main/java/com/wisemapping/filter/UserLocaleInterceptor.java deleted file mode 100644 index 6b4abe1f..00000000 --- a/wise-api/src/main/java/com/wisemapping/filter/UserLocaleInterceptor.java +++ /dev/null @@ -1,57 +0,0 @@ -/* -* Copyright [2022] [wisemapping] -* -* Licensed under WiseMapping Public License, Version 1.0 (the "License"). -* It is basically the Apache License, Version 2.0 (the "License") plus the -* "powered by wisemapping" text requirement on every single page; -* you may not use this file except in compliance with the License. -* You may obtain a copy of the license at -* -* http://www.wisemapping.org/license -* -* 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.wisemapping.filter; - -import com.wisemapping.model.User; -import com.wisemapping.security.Utils; -import org.jetbrains.annotations.NotNull; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.i18n.SessionLocaleResolver; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import jakarta.servlet.http.HttpSession; -import java.util.Locale; -@Component -public class UserLocaleInterceptor implements HandlerInterceptor { - - @Override - public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, Object object) throws Exception { - - final HttpSession session = request.getSession(false); - User user = Utils.getUser(false); - - if (user != null && session != null) { - String userLocale = user.getLocale(); - final Locale sessionLocale = (Locale) session.getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME); - if ((userLocale != null) && ((sessionLocale == null) || (!userLocale.equals(sessionLocale.toString())))) { - Locale locale; - if (userLocale.contains("_")) { - final String[] spit = userLocale.split("_"); - locale = new Locale(spit[0], spit[1]); - } else { - locale = new Locale(userLocale); - } - session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale); - } - } - return true; - } -} diff --git a/wise-api/src/main/resources/application.yml b/wise-api/src/main/resources/application.yml old mode 100755 new mode 100644 diff --git a/wise-api/src/main/resources/spring/wisemapping-messages.xml b/wise-api/src/main/resources/spring/wisemapping-messages.xml deleted file mode 100755 index 7cebddbe..00000000 --- a/wise-api/src/main/resources/spring/wisemapping-messages.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - messages - - - -