@WithMockUser @Test void testSetLocaleAuthenticated() { HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); Locale locale = GERMAN; String username = "user"; User user = mock(User.class); when(userService.getUser(username)).thenReturn(user); httpLocaleResolver.setLocale(httpServletRequest, httpServletResponse, locale); verify(user).setLanguageCode(locale.getLanguage()); verify(userService).update(user); }
@Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { String username = SecurityUtils.getCurrentUsername(); if (username == null) { throw new UnsupportedOperationException("Cannot change language if not logged in"); } String languageCode = locale.getLanguage(); if (!LanguageService.hasLanguageCode(languageCode)) { throw new UnsupportedOperationException("Cannot set language to unsupported languageCode"); } User user = userService.getUser(username); if (user == null) { throw new UnknownUserException(username); } if (!Objects.equal(user.getLanguageCode(), languageCode)) { user.setLanguageCode(locale.getLanguage()); userService.update(user); } }
HttpLocaleResolver implements LocaleResolver { @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { String username = SecurityUtils.getCurrentUsername(); if (username == null) { throw new UnsupportedOperationException("Cannot change language if not logged in"); } String languageCode = locale.getLanguage(); if (!LanguageService.hasLanguageCode(languageCode)) { throw new UnsupportedOperationException("Cannot set language to unsupported languageCode"); } User user = userService.getUser(username); if (user == null) { throw new UnknownUserException(username); } if (!Objects.equal(user.getLanguageCode(), languageCode)) { user.setLanguageCode(locale.getLanguage()); userService.update(user); } } }
HttpLocaleResolver implements LocaleResolver { @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { String username = SecurityUtils.getCurrentUsername(); if (username == null) { throw new UnsupportedOperationException("Cannot change language if not logged in"); } String languageCode = locale.getLanguage(); if (!LanguageService.hasLanguageCode(languageCode)) { throw new UnsupportedOperationException("Cannot set language to unsupported languageCode"); } User user = userService.getUser(username); if (user == null) { throw new UnknownUserException(username); } if (!Objects.equal(user.getLanguageCode(), languageCode)) { user.setLanguageCode(locale.getLanguage()); userService.update(user); } } HttpLocaleResolver(
      UserLocaleResolver userLocaleResolver,
      FallbackLocaleSupplier fallbackLocaleSupplier,
      UserService userService); }
HttpLocaleResolver implements LocaleResolver { @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { String username = SecurityUtils.getCurrentUsername(); if (username == null) { throw new UnsupportedOperationException("Cannot change language if not logged in"); } String languageCode = locale.getLanguage(); if (!LanguageService.hasLanguageCode(languageCode)) { throw new UnsupportedOperationException("Cannot set language to unsupported languageCode"); } User user = userService.getUser(username); if (user == null) { throw new UnknownUserException(username); } if (!Objects.equal(user.getLanguageCode(), languageCode)) { user.setLanguageCode(locale.getLanguage()); userService.update(user); } } HttpLocaleResolver(
      UserLocaleResolver userLocaleResolver,
      FallbackLocaleSupplier fallbackLocaleSupplier,
      UserService userService); @Override Locale resolveLocale(HttpServletRequest request); @Override void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale); }
HttpLocaleResolver implements LocaleResolver { @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { String username = SecurityUtils.getCurrentUsername(); if (username == null) { throw new UnsupportedOperationException("Cannot change language if not logged in"); } String languageCode = locale.getLanguage(); if (!LanguageService.hasLanguageCode(languageCode)) { throw new UnsupportedOperationException("Cannot set language to unsupported languageCode"); } User user = userService.getUser(username); if (user == null) { throw new UnknownUserException(username); } if (!Objects.equal(user.getLanguageCode(), languageCode)) { user.setLanguageCode(locale.getLanguage()); userService.update(user); } } HttpLocaleResolver(
      UserLocaleResolver userLocaleResolver,
      FallbackLocaleSupplier fallbackLocaleSupplier,
      UserService userService); @Override Locale resolveLocale(HttpServletRequest request); @Override void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale);  }