From 9b44df148d871472369212ead64afe67466a7538 Mon Sep 17 00:00:00 2001 From: sandip singh Date: Tue, 5 Nov 2019 21:08:56 +0530 Subject: [PATCH] BAEL-3283 - Refactored the code as per review comments. --- .../com/baeldung/config/DataSourceConfig.java | 8 +-- .../com/baeldung/config/ThemeMVCConfig.java | 8 --- .../resolver/UserPreferenceThemeResolver.java | 58 +++++++------------ .../src/main/resources/db/sql/insert-data.sql | 2 +- 4 files changed, 25 insertions(+), 51 deletions(-) diff --git a/spring-mvc-themes/src/main/java/com/baeldung/config/DataSourceConfig.java b/spring-mvc-themes/src/main/java/com/baeldung/config/DataSourceConfig.java index ea74a8176e..ff47bd7047 100644 --- a/spring-mvc-themes/src/main/java/com/baeldung/config/DataSourceConfig.java +++ b/spring-mvc-themes/src/main/java/com/baeldung/config/DataSourceConfig.java @@ -6,7 +6,6 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.orm.jpa.JpaTransactionManager; -import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; @@ -29,16 +28,13 @@ public class DataSourceConfig { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan("com.baeldung.domain"); - JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); - em.setJpaVendorAdapter(vendorAdapter); + em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); return em; } @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { - JpaTransactionManager transactionManager = new JpaTransactionManager(); - transactionManager.setEntityManagerFactory(emf); - return transactionManager; + return new JpaTransactionManager(emf); } @Bean diff --git a/spring-mvc-themes/src/main/java/com/baeldung/config/ThemeMVCConfig.java b/spring-mvc-themes/src/main/java/com/baeldung/config/ThemeMVCConfig.java index 35d513fad0..86f6f54195 100644 --- a/spring-mvc-themes/src/main/java/com/baeldung/config/ThemeMVCConfig.java +++ b/spring-mvc-themes/src/main/java/com/baeldung/config/ThemeMVCConfig.java @@ -39,20 +39,12 @@ public class ThemeMVCConfig implements WebMvcConfigurer { } @Bean -// @Profile("cookie") public ThemeResolver themeResolver() { UserPreferenceThemeResolver themeResolver = new UserPreferenceThemeResolver(); themeResolver.setDefaultThemeName("light"); return themeResolver; } -// @Bean -// @Profile("database") -// public ThemeResolver databaseThemeResolver() { -// UserPreferenceThemeResolver themeResolver = new UserPreferenceThemeResolver(); -// themeResolver.setDefaultThemeName("light"); -// return themeResolver; -// } @Override public void configureViewResolvers(ViewResolverRegistry resolverRegistry) { diff --git a/spring-mvc-themes/src/main/java/com/baeldung/theme/resolver/UserPreferenceThemeResolver.java b/spring-mvc-themes/src/main/java/com/baeldung/theme/resolver/UserPreferenceThemeResolver.java index f4a3a7971e..4c59734d41 100644 --- a/spring-mvc-themes/src/main/java/com/baeldung/theme/resolver/UserPreferenceThemeResolver.java +++ b/spring-mvc-themes/src/main/java/com/baeldung/theme/resolver/UserPreferenceThemeResolver.java @@ -11,6 +11,7 @@ import org.springframework.web.servlet.ThemeResolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.Optional; public class UserPreferenceThemeResolver implements ThemeResolver { @@ -34,55 +35,40 @@ public class UserPreferenceThemeResolver implements ThemeResolver { @Override public String resolveThemeName(HttpServletRequest request) { - String themeName = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME); - if (themeName != null) { - return themeName; - } - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - - if (authentication != null && authentication.isAuthenticated() && isNotAnonymous(authentication)) { - User user = (User) authentication.getPrincipal(); - UserPreference userPreference = userPreferenceRepository.findById(user.getUsername()).orElse(null); - - if (userPreference != null) { - themeName = userPreference.getTheme(); - } - } - - // Fall back to default theme. - if (themeName == null) { - themeName = getDefaultThemeName(); - } + String themeName = findThemeFromRequest(request).orElse(findUserPreferredTheme().orElse(getDefaultThemeName())); request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName); return themeName; } - private boolean isNotAnonymous(Authentication authentication) { - return !isAnonymous(authentication); + private Optional findUserPreferredTheme() { + Authentication authentication = SecurityContextHolder.getContext() + .getAuthentication(); + UserPreference userPreference = getUserPreference(authentication).orElse(new UserPreference()); + return Optional.ofNullable(userPreference.getTheme()); } - private boolean isAnonymous(Authentication authentication) { - return authentication != null && authentication.getPrincipal() instanceof String && "anonymousUser".equals(authentication.getPrincipal()); + private Optional findThemeFromRequest(HttpServletRequest request) { + return Optional.ofNullable((String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME)); + } + + private Optional getUserPreference(Authentication authentication) { + return isAuthenticated(authentication) ? userPreferenceRepository.findById(((User) authentication.getPrincipal()).getUsername()) : Optional.empty(); + } + + private boolean isAuthenticated(Authentication authentication) { + return authentication != null && authentication.isAuthenticated(); } @Override public void setThemeName(HttpServletRequest request, HttpServletResponse response, String theme) { - - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (authentication != null && authentication.isAuthenticated() && isNotAnonymous(authentication)) { + Authentication authentication = SecurityContextHolder.getContext() + .getAuthentication(); + if (isAuthenticated(authentication)) { request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, theme); - User user = (User) authentication.getPrincipal(); - - UserPreference userPreference = userPreferenceRepository.findById(user.getUsername()).orElse(new UserPreference()); - - userPreference.setUsername(user.getUsername()); + UserPreference userPreference = getUserPreference(authentication).orElse(new UserPreference()); + userPreference.setUsername(((User) authentication.getPrincipal()).getUsername()); userPreference.setTheme(StringUtils.hasText(theme) ? theme : null); userPreferenceRepository.save(userPreference); - - } - - if (!StringUtils.hasText(theme)) { - request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, getDefaultThemeName()); } } } diff --git a/spring-mvc-themes/src/main/resources/db/sql/insert-data.sql b/spring-mvc-themes/src/main/resources/db/sql/insert-data.sql index 310dcd00c7..7ddf684d9a 100644 --- a/spring-mvc-themes/src/main/resources/db/sql/insert-data.sql +++ b/spring-mvc-themes/src/main/resources/db/sql/insert-data.sql @@ -1,6 +1,6 @@ insert into users values('john', '$2a$10$cjcbIX/aLe12PpGZ.vQfweLiB7K1QTC5enTk3oD0deCMdtj2Sx.im', 1); insert into users values('admin', '$2a$10$cjcbIX/aLe12PpGZ.vQfweLiB7K1QTC5enTk3oD0deCMdtj2Sx.im', 1); -insert into authorities values('sandip', 'USER'); +insert into authorities values('john', 'USER'); insert into authorities values('admin', 'USER'); insert into authorities values('admin', 'ADMIN'); \ No newline at end of file