BAEL-3283 - Refactored the code as per review comments.

This commit is contained in:
sandip singh 2019-11-05 21:08:56 +05:30
parent f6c3f9ebcb
commit 9b44df148d
4 changed files with 25 additions and 51 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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<String> 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<String> findThemeFromRequest(HttpServletRequest request) {
return Optional.ofNullable((String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME));
}
private Optional<UserPreference> 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());
}
}
}

View File

@ -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');