Commit 62cd9827 authored by igoshin's avatar igoshin Committed by Matija Obreza
Browse files

Java based configuration

parent 869a7164
......@@ -499,7 +499,17 @@
<version>${jetty.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>${jetty.version}</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.data</groupId>-->
<!--<artifactId>spring-data-jpa</artifactId>-->
<!--<version>1.5.1.RELEASE</version>-->
<!--</dependency>-->
</dependencies>
<build>
<plugins>
......
......@@ -26,18 +26,6 @@ import org.springframework.stereotype.Component;
@Component("props")
public class ApplicationProps implements InitializingBean {
@Value("${paginator.default.pageSize}")
private int defaulPaginatorPageSize;
@Value("${paginator.default.maxPage}")
private int defaulPaginatorMaxPage;
@Value("${paginator.default.fastStep}")
private int defaulPaginatorFastStep;
@Value("${default.admin.email}")
private String defaultAdminEmail;
@Value("${tileserver.cdn}")
private String tileserverCdn;
......@@ -47,26 +35,6 @@ public class ApplicationProps implements InitializingBean {
@Value("${base.url}")
private String baseUrl;
public int getDefaulPaginatorPageSize() {
return defaulPaginatorPageSize;
}
public int getDefaulPaginatorMaxPage() {
return defaulPaginatorMaxPage;
}
public int getDefaulPaginatorFastStep() {
return defaulPaginatorFastStep;
}
public String getDefaultAdminEmail() {
return defaultAdminEmail;
}
public void setDefaultAdminEmail(String defaultAdminEmail) {
this.defaultAdminEmail = defaultAdminEmail;
}
public String getTileserverCdn() {
return tileserverCdn;
}
......
......@@ -16,10 +16,6 @@
package org.genesys2.server.service.impl;
import java.util.Arrays;
import javax.mail.internet.MimeMessage;
import org.genesys2.server.service.EMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -31,6 +27,9 @@ import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
@Service
public class EMailServiceImpl implements EMailService {
......
package org.genesys2.spring.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
@Configuration
@Import({
SpringCommonConfig.class,
SpringAclConfig.class,
SpringSchedulerConfig.class,
SpringDataBaseConfig.class,
SpringMailConfig.class,
SpringSecurityOauthConfig.class,
SpringCacheConfig.class})
@ImportResource({"classpath:/spring/spring-security.xml"})
public class ApplicationConfig {
}
package org.genesys2.spring.config;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.genesys2.server.cache.HazelcastAclCache;
import org.genesys2.server.security.Slf4jAuditLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.acls.AclPermissionEvaluator;
import org.springframework.security.acls.domain.AclAuthorizationStrategyImpl;
import org.springframework.security.acls.domain.DefaultPermissionGrantingStrategy;
import org.springframework.security.acls.jdbc.BasicLookupStrategy;
import org.springframework.security.acls.jdbc.JdbcMutableAclService;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@Configuration
public class SpringAclConfig {
@Autowired
private DataSource dataSource;
@Autowired
private CacheManager cacheManager;
@Bean(name = "permissionEvaluator")
public AclPermissionEvaluator aclPermissionEvaluator() {
return new AclPermissionEvaluator(aclService());
}
@Bean
public JdbcMutableAclService aclService() {
return new JdbcMutableAclService(dataSource, lookupStrategy(), aclCache());
}
@Bean
public AclAuthorizationStrategyImpl aclAuthorizationStrategy() {
return new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ADMINISTRATOR"));
}
@Bean
public Slf4jAuditLogger slf4jAuditLogger() {
return new Slf4jAuditLogger();
}
@Bean
public DefaultPermissionGrantingStrategy permissionGrantingStrategy() {
return new DefaultPermissionGrantingStrategy(slf4jAuditLogger());
}
@Bean
public HazelcastAclCache aclCache() {
return new HazelcastAclCache(aclCacheCache(), permissionGrantingStrategy(), aclAuthorizationStrategy());
}
@Bean
public Cache aclCacheCache() {
return cacheManager.getCache("aclcache");
}
@Bean
public BasicLookupStrategy lookupStrategy() {
return new BasicLookupStrategy(dataSource, aclCache(), aclAuthorizationStrategy(), permissionGrantingStrategy());
}
}
package org.genesys2.spring.config;
import com.hazelcast.web.WebFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import java.util.Properties;
@Configuration
@EnableCaching
@ImportResource("classpath:/spring/spring-cache.xml")
public class SpringCacheConfig {
@Value("${base.cookie-domain}")
private String cookieDomain;
@Value("${base.cookie-secure}")
private String cookieSecure;
@Value("${base.cookie-http-only}")
private String cookieHttpOnly;
@Bean
public WebFilter hazelcastWebFilter() {
return new WebFilter(filterProps());
}
private Properties filterProps() {
Properties properties = new Properties();
properties.setProperty("use-client", "false");
properties.setProperty("map-name", "jetty-sessions");
properties.setProperty("sticky-session", "false");
properties.setProperty("cookie-name", "hz-session-id");
properties.setProperty("cookie-domain", cookieDomain);
properties.setProperty("cookie-secure", cookieSecure);
properties.setProperty("cookie-http-only", cookieHttpOnly);
properties.setProperty("debug", "true");
properties.setProperty("instance-name", "genesys");
properties.setProperty("shutdown-on-destroy", "true");
return properties;
}
}
package org.genesys2.spring.config;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.genesys2.spring.validation.oval.spring.SpringOvalValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import org.springframework.security.acls.AclPermissionEvaluator;
import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Validator;
import javax.persistence.Entity;
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"org.genesys2.spring", "org.genesys2.server"},
excludeFilters = {
@ComponentScan.Filter(value = Controller.class, type = FilterType.ANNOTATION),
@ComponentScan.Filter(value = Configuration.class, type = FilterType.ANNOTATION)},
includeFilters = @ComponentScan.Filter(value = Entity.class, type = FilterType.ANNOTATION))
@Configuration
public class SpringCommonConfig {
@Autowired
private AclPermissionEvaluator aclPermissionEvaluator;
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setLocation(new ClassPathResource("application.properties"));
propertyPlaceholderConfigurer.setLocation(new ClassPathResource("spring/spring.properties"));
return propertyPlaceholderConfigurer;
}
@Scope("singleton")
@Bean
public Validator validator() {
SpringOvalValidator validator = new SpringOvalValidator();
validator.setValidator(new net.sf.oval.Validator());
return validator;
}
@Bean
public Jackson2ObjectMapperFactoryBean objectMapper() {
Jackson2ObjectMapperFactoryBean mapperFactoryBean = new Jackson2ObjectMapperFactoryBean();
mapperFactoryBean.setFeaturesToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapperFactoryBean;
}
@Bean
public DefaultWebSecurityExpressionHandler webExpressionHandler() {
DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
handler.setPermissionEvaluator(aclPermissionEvaluator);
return handler;
}
}
package org.genesys2.spring.config;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.util.Properties;
@EnableJpaRepositories(
basePackages = {"org.genesys2.server.persistence.domain", "org.genesys2.server.persistence.acl"},
entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager",
repositoryImplementationPostfix = "CustomImpl")
//@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableTransactionManagement
@Configuration
@ImportResource("classpath:/spring/spring-db.xml")
public class SpringDataBaseConfig {
@Value("${db.url}")
private String dbUrl;
@Value("${db.driverClassName}")
private String dbDriverClassName;
@Value("${db.username}")
private String dbUsername;
@Value("${db.password}")
private String dbPassword;
@Value("${db.showSql}")
private boolean dbShowSql;
@Value("${db.dialect}")
private String dbDialect;
@Value("${db.hbm2ddl}")
private String dbHbm2ddl;
@Value("${lucene.indexDir}")
private String luceneIndexDir;
@Bean
public DataSource dataSource() {
DataSource dataSource = new DataSource();
dataSource.setUrl(dbUrl);
dataSource.setDriverClassName(dbDriverClassName);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(true);
return dataSource;
}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPersistenceUnitName("spring-jpa");
entityManager.setJpaVendorAdapter(hibernateJpaVendorAdapter());
entityManager.setJpaProperties(jpaProperties());
entityManager.setPackagesToScan(
"org.genesys2.server.model.acl",
"org.genesys2.server.model.impl",
"org.genesys2.server.model.genesys",
"org.genesys2.server.model.oauth");
return entityManager;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setDataSource(dataSource());
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
private Properties jpaProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", dbDialect);
properties.setProperty("hibernate.connection.charSet", "utf8");
properties.setProperty("hibernate.connection.autocommit", "false");
properties.setProperty("hibernate.hbm2ddl.auto", dbHbm2ddl);
properties.setProperty("hibernate.search.default.indexBase", luceneIndexDir);
properties.setProperty("hibernate.search.default.exclusive_index_use", "false");
properties.setProperty("hibernate.search.default.​worker.execution", "async");
return properties;
}
private HibernateJpaVendorAdapter hibernateJpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(dbShowSql);
return jpaVendorAdapter;
}
}
package org.genesys2.spring.config;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
import java.util.Properties;
@Configuration
public class SpringMailConfig {
@Value("${mail.host}")
private String host;
@Value("${mail.port}")
private Integer port;
@Value("${mail.user.name}")
private String userName;
@Value("${mail.user.password}")
private String pwd;
@Value("${mail.smtp.ssl.enable}")
private String sslEnable;
@Value("${mail.smtp.starttls.enable}")
private String smtpStarttlsEnable;
@Value("${mail.smtp.auth}")
private String smtpAuth;
@Value("${mail.transport.protocol}")
private String protocol;
@Bean
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(host);
sender.setPort(port);
sender.setUsername(userName);
sender.setPassword(pwd);
sender.setJavaMailProperties(javaMailProperties());
return sender;
}
@Bean
public VelocityEngine velocityEngine() {
VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
velocityEngineFactoryBean.setVelocityProperties(velocityProperties());
return velocityEngineFactoryBean.getObject();
}
private Properties javaMailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.smtp.ssl.enable", sslEnable);
properties.setProperty("mail.smtp.starttls.enable", smtpStarttlsEnable);
properties.setProperty("mail.smtp.auth", smtpAuth);
properties.setProperty("mail.transport.protocol", protocol);
return properties;
}
private Properties velocityProperties() {
Properties properties = new Properties();
properties.setProperty("resource.loader", "class");
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
properties.setProperty("velocimacro.permissions.allow.inline.local.scope", "false");
properties.setProperty("runtime.log.logsystem.log4j.logger", "velocity");
properties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.Log4JLogChute");
return properties;
}
}
package org.genesys2.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@EnableAsync
@EnableScheduling
@Configuration
public class SpringSchedulerConfig {
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setCorePoolSize(5);
pool.setMaxPoolSize(16);
return pool;
}
}
package org.genesys2.spring.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:/spring/spring-security-oauth.xml")
public class SpringSecurityOauthConfig {
}
package org.genesys2.spring.config;
import org.genesys2.spring.AddStuffInterceptor;
import org.genesys2.spring.BetterSessionLocaleResolver;
import org.genesys2.spring.validation.oval.spring.SpringOvalValidator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.Validator;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.theme.CookieThemeResolver;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import java.util.HashSet;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"org.genesys2.server.servlet.filter", "org.genesys2.server.servlet.controller"})
@Configuration
public class SpringServletConfig extends WebMvcConfigurerAdapter {
@Value("${theme.defaultThemeName}")
private String defaultThemeName;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/html/**").addResourceLocations("/html/");
}
@Bean
public ViewResolver viewResolver() {
org.springframework.web.servlet.view.InternalResourceViewResolver resolver = new org.springframework.web.servlet.view.InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
resolver.setExposedContextBeanNames(new String[]{"props", "jspHelper"});
resolver.setRedirectHttp10Compatible(false);
resolver.setRequestContextAttribute("requestContext");
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
ThemeChangeInterceptor themeChangeInterceptor = new ThemeChangeInterceptor();
themeChangeInterceptor.setParamName("theme");
registry.addInterceptor(localeChangeInterceptor);
registry.addInterceptor(themeChangeInterceptor);
registry.addInterceptor(new AddStuffInterceptor());