// Path: src/main/java/org/vaadin/spring/sample/security/account/JdbcAccountRepository.java
// @Repository
// public class JdbcAccountRepository implements AccountRepository {
// 
// 	private final JdbcTemplate jdbcTemplate;
// 
// 	private final PasswordEncoder passwordEncoder;
// 
// 	@Autowired
// 	public JdbcAccountRepository(JdbcTemplate jdbcTemplate, PasswordEncoder passwordEncoder) {
// 		this.jdbcTemplate = jdbcTemplate;
// 		this.passwordEncoder = passwordEncoder;
// 	}
// 
// 	@Transactional
// 	public void createAccount(Account user) throws UsernameAlreadyInUseException {
// 		try {
// 			jdbcTemplate.update(
// 					"insert into Account (firstName, lastName, username, password, role) values (?, ?, ?, ?, ?)",
// 					user.getFirstName(), user.getLastName(), user.getUsername(),
// 					passwordEncoder.encode(user.getPassword()), user.getRole());
// 		} catch (DuplicateKeyException e) {
// 			throw new UsernameAlreadyInUseException(user.getUsername());
// 		}
// 	}
// 
// 	public Account findAccountByUsername(String username) {
// 		return jdbcTemplate.queryForObject("select username, password, firstName, lastName, role from Account where username = ?",
// 				new RowMapper<Account>() {
// 					public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
// 						return new Account(rs.getString("username"), rs.getString("password"), rs.getString("firstName"), rs
// 								.getString("lastName"), rs.getString("role"));
// 					}
// 				}, username);
// 	}
// 
// }
// 
// Path: src/main/java/org/vaadin/spring/sample/security/ui/security/HttpResponseFactory.java
// public class HttpResponseFactory implements FactoryBean<HttpServletResponse>, ApplicationContextAware {
// 
// 	private ApplicationContext applicationContext;
// 	
// 	@Override
// 	public HttpServletResponse getObject() throws Exception {
// 		HttpResponseFilter httpResponseFilter = applicationContext.getBean(HttpResponseFilter.class);	
// 		return httpResponseFilter.getHttpServletReponse();
// 	}
// 
// 	@Override
// 	public Class<?> getObjectType() {	
// 		return HttpServletResponse.class;
// 	}
// 
// 	@Override
// 	public boolean isSingleton() {		
// 		return false;
// 	}
// 
// 	@Override
// 	public void setApplicationContext(ApplicationContext applicationContext)
// 			throws BeansException {
// 		this.applicationContext = applicationContext;
// 		
// 	}
// 
// }
// 
// Path: src/main/java/org/vaadin/spring/sample/security/ui/security/HttpResponseFilter.java
// public class HttpResponseFilter implements Filter {
// 
// 	private ThreadLocal<HttpServletResponse> responses = new ThreadLocal<HttpServletResponse>();
// 	
// 	@Override
// 	public void init(FilterConfig filterConfig) throws ServletException {
// 		
// 	}
// 
// 	@Override
// 	public void doFilter(ServletRequest request, ServletResponse response,
// 			FilterChain chain) throws IOException, ServletException {
// 		
// 		HttpServletResponse r = (HttpServletResponse) response;
// 		responses.set(r);
// 		chain.doFilter(request, response);
// 		responses.remove();		
// 	}
// 	
// 	public HttpServletResponse getHttpServletReponse() {
// 		return responses.get();
// 	}
// 
// 	@Override
// 	public void destroy() {		
// 		
// 	}
// 
// }
// 
// Path: src/main/java/org/vaadin/spring/sample/security/ui/security/SpringApplicationContext.java
// public class SpringApplicationContext implements ApplicationContextAware {
// 	
// 	private static ApplicationContext ctx;
// 
// 	@Override
// 	public void setApplicationContext(ApplicationContext applicationContext)
// 			throws BeansException {
// 		ctx = applicationContext;		
// 	}
// 	
// 	public static ApplicationContext getApplicationContext() {
// 		return ctx;
// 	}
// 	
// 	public static EventBus getEventBus() {
// 		return ctx.getBean(EventBus.class);
// 	}
// 
// }


import javax.sql.DataSource;
import org.eclipse.jetty.servlets.GzipFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.vaadin.spring.sample.security.account.JdbcAccountRepository;
import org.vaadin.spring.sample.security.ui.security.HttpResponseFactory;
import org.vaadin.spring.sample.security.ui.security.HttpResponseFilter;
import org.vaadin.spring.sample.security.ui.security.SpringApplicationContext;
import org.vaadin.spring.servlet.SpringAwareVaadinServlet;
package org.vaadin.spring.sample.security;



@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application  {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);

	}
	
	@Bean
	public SpringAwareVaadinServlet springAwareVaadinServlet() {
		return new CustomVaadinServlet();
	}
	
	@Bean
	public SpringApplicationContext springApplicationContext() {
		return new SpringApplicationContext();
	}
	
	@Bean
    @ConditionalOnMissingBean(RequestContextListener.class)
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }
	
	/**
	 * Allow injection of HttpServletResponse
	 * 
	 * @return
	 */
	@Bean
	public HttpResponseFilter httpResponseFilter() {
		return new HttpResponseFilter(); 		
	}
	
	/**
	 * Allow injection of HttpServletResponse
	 * 
	 * @return
	 */
	@Bean
	public HttpResponseFactory httpResponseFactory() {
// Path: src/main/java/org/vaadin/spring/sample/security/account/JdbcAccountRepository.java
// @Repository
// public class JdbcAccountRepository implements AccountRepository {
// 
// 	private final JdbcTemplate jdbcTemplate;
// 
// 	private final PasswordEncoder passwordEncoder;
// 
// 	@Autowired
// 	public JdbcAccountRepository(JdbcTemplate jdbcTemplate, PasswordEncoder passwordEncoder) {
// 		this.jdbcTemplate = jdbcTemplate;
// 		this.passwordEncoder = passwordEncoder;
// 	}
// 
// 	@Transactional
// 	public void createAccount(Account user) throws UsernameAlreadyInUseException {
// 		try {
// 			jdbcTemplate.update(
// 					"insert into Account (firstName, lastName, username, password, role) values (?, ?, ?, ?, ?)",
// 					user.getFirstName(), user.getLastName(), user.getUsername(),
// 					passwordEncoder.encode(user.getPassword()), user.getRole());
// 		} catch (DuplicateKeyException e) {
// 			throw new UsernameAlreadyInUseException(user.getUsername());
// 		}
// 	}
// 
// 	public Account findAccountByUsername(String username) {
// 		return jdbcTemplate.queryForObject("select username, password, firstName, lastName, role from Account where username = ?",
// 				new RowMapper<Account>() {
// 					public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
// 						return new Account(rs.getString("username"), rs.getString("password"), rs.getString("firstName"), rs
// 								.getString("lastName"), rs.getString("role"));
// 					}
// 				}, username);
// 	}
// 
// }
// 
// Path: src/main/java/org/vaadin/spring/sample/security/ui/security/HttpResponseFactory.java
// public class HttpResponseFactory implements FactoryBean<HttpServletResponse>, ApplicationContextAware {
// 
// 	private ApplicationContext applicationContext;
// 	
// 	@Override
// 	public HttpServletResponse getObject() throws Exception {
// 		HttpResponseFilter httpResponseFilter = applicationContext.getBean(HttpResponseFilter.class);	
// 		return httpResponseFilter.getHttpServletReponse();
// 	}
// 
// 	@Override
// 	public Class<?> getObjectType() {	
// 		return HttpServletResponse.class;
// 	}
// 
// 	@Override
// 	public boolean isSingleton() {		
// 		return false;
// 	}
// 
// 	@Override
// 	public void setApplicationContext(ApplicationContext applicationContext)
// 			throws BeansException {
// 		this.applicationContext = applicationContext;
// 		
// 	}
// 
// }
// 
// Path: src/main/java/org/vaadin/spring/sample/security/ui/security/HttpResponseFilter.java
// public class HttpResponseFilter implements Filter {
// 
// 	private ThreadLocal<HttpServletResponse> responses = new ThreadLocal<HttpServletResponse>();
// 	
// 	@Override
// 	public void init(FilterConfig filterConfig) throws ServletException {
// 		
// 	}
// 
// 	@Override
// 	public void doFilter(ServletRequest request, ServletResponse response,
// 			FilterChain chain) throws IOException, ServletException {
// 		
// 		HttpServletResponse r = (HttpServletResponse) response;
// 		responses.set(r);
// 		chain.doFilter(request, response);
// 		responses.remove();		
// 	}
// 	
// 	public HttpServletResponse getHttpServletReponse() {
// 		return responses.get();
// 	}
// 
// 	@Override
// 	public void destroy() {		
// 		
// 	}
// 
// }
// 
// Path: src/main/java/org/vaadin/spring/sample/security/ui/security/SpringApplicationContext.java
// public class SpringApplicationContext implements ApplicationContextAware {
// 	
// 	private static ApplicationContext ctx;
// 
// 	@Override
// 	public void setApplicationContext(ApplicationContext applicationContext)
// 			throws BeansException {
// 		ctx = applicationContext;		
// 	}
// 	
// 	public static ApplicationContext getApplicationContext() {
// 		return ctx;
// 	}
// 	
// 	public static EventBus getEventBus() {
// 		return ctx.getBean(EventBus.class);
// 	}
// 
// }


// Path: src/main/java/org/vaadin/spring/sample/security/Application.java
import javax.sql.DataSource;
import org.eclipse.jetty.servlets.GzipFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.vaadin.spring.sample.security.account.JdbcAccountRepository;
import org.vaadin.spring.sample.security.ui.security.HttpResponseFactory;
import org.vaadin.spring.sample.security.ui.security.HttpResponseFilter;
import org.vaadin.spring.sample.security.ui.security.SpringApplicationContext;
import org.vaadin.spring.servlet.SpringAwareVaadinServlet;

package org.vaadin.spring.sample.security;



@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application  {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);

	}
	
	@Bean
	public SpringAwareVaadinServlet springAwareVaadinServlet() {
		return new CustomVaadinServlet();
	}
	
	@Bean
	public SpringApplicationContext springApplicationContext() {
		return new SpringApplicationContext();
	}
	
	@Bean
    @ConditionalOnMissingBean(RequestContextListener.class)
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }
	
	/**
	 * Allow injection of HttpServletResponse
	 * 
	 * @return
	 */
	@Bean
	public HttpResponseFilter httpResponseFilter() {
		return new HttpResponseFilter(); 		
	}
	
	/**
	 * Allow injection of HttpServletResponse
	 * 
	 * @return
	 */
	@Bean
	public HttpResponseFactory httpResponseFactory() {
		return new HttpResponseFactory();