// Path: opentipbot-persistence/src/main/java/opentipbot/persistence/repository/OpenTipBotUserRepository.java
// public interface OpenTipBotUserRepository extends JpaRepository<OpenTipBotUser, Long> {
//     OpenTipBotUser findByTwitterIdentifier(String twitterIdentifier);
//     OpenTipBotUser findByUserName(String UserName);
// }
// 
// Path: opentipbot-webapp/src/main/java/opentipbot/web/security/UserInterceptor.java
// public final class UserInterceptor extends HandlerInterceptorAdapter {
// 
// 	private final UsersConnectionRepository connectionRepository;
// 
//     private final OpenTipBotUserRepository opentipbotUserRepository;
// 	
// 	private final UserCookieGenerator userCookieGenerator = new UserCookieGenerator();
// 
// 	public UserInterceptor(UsersConnectionRepository connectionRepository, OpenTipBotUserRepository opentipbotUserRepository) {
// 		this.connectionRepository = connectionRepository;
//         this.opentipbotUserRepository = opentipbotUserRepository;
// 	}
// 	
// 	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 		rememberUser(request, response);
// 		handleSignOut(request, response);			
// 		if (SecurityContext.userSignedIn() || requestForNotProtected(request)) {
// 			return true;
// 		} else {
// 			return requireSignIn(request, response);
// 		}
// 	}
// 	
// 	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 		SecurityContext.remove();
// 	}
// 
// 	// internal helpers
// 
// 	private void rememberUser(HttpServletRequest request, HttpServletResponse response) {
// 		String userId = userCookieGenerator.readCookieValue(request);
// 		if (userId == null) {
// 			return;
// 		}
// 		if (!userNotFound(userId)) {
// 			userCookieGenerator.removeCookie(response);
// 			return;
// 		}
//         OpenTipBotUser opentipbotUser= opentipbotUserRepository.findOne(Long.parseLong(userId));
//         if (opentipbotUser == null)
//             return;
// 		SecurityContext.setCurrentUser(opentipbotUser);
// 	}
// 
// 	private void handleSignOut(HttpServletRequest request, HttpServletResponse response) {
// 		if (SecurityContext.userSignedIn() && request.getServletPath().startsWith("/signout")) {
// 			connectionRepository.createConnectionRepository(SecurityContext.getCurrentUser().getId().toString()).removeConnections("twitter");
// 			userCookieGenerator.removeCookie(response);
// 			SecurityContext.remove();			
// 		}
// 	}
// 		
// 	private boolean requestForNotProtected(HttpServletRequest request) {
// 		return request.getServletPath().startsWith("/signin") || request.getServletPath().startsWith("/documentation")
//                 || request.getServletPath().startsWith("/about");
// 	}
// 	
// 	private boolean requireSignIn(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 		new RedirectView("/signin", true).render(null, request, response);
// 		return false;
// 	}
// 
// 	private boolean userNotFound(String userId) {
// 		// doesn't bother checking a local user database: simply checks if the userId is connected to Twitter
// 		return connectionRepository.createConnectionRepository(userId).findPrimaryConnection(Twitter.class) != null;
// 	}
// 	
// }


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.web.accept.ContentNegotiationManager;
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.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;
import opentipbot.persistence.repository.OpenTipBotUserRepository;
import opentipbot.web.security.UserInterceptor;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
package opentipbot.web.config;



/**
 * @author Petri Kainulainen
 */
@Configuration
@ComponentScan(basePackages = {
        "opentipbot.web.controller"
})
@EnableWebMvc
public class WebAppContext extends WebMvcConfigurerAdapter {

    @Autowired
// Path: opentipbot-persistence/src/main/java/opentipbot/persistence/repository/OpenTipBotUserRepository.java
// public interface OpenTipBotUserRepository extends JpaRepository<OpenTipBotUser, Long> {
//     OpenTipBotUser findByTwitterIdentifier(String twitterIdentifier);
//     OpenTipBotUser findByUserName(String UserName);
// }
// 
// Path: opentipbot-webapp/src/main/java/opentipbot/web/security/UserInterceptor.java
// public final class UserInterceptor extends HandlerInterceptorAdapter {
// 
// 	private final UsersConnectionRepository connectionRepository;
// 
//     private final OpenTipBotUserRepository opentipbotUserRepository;
// 	
// 	private final UserCookieGenerator userCookieGenerator = new UserCookieGenerator();
// 
// 	public UserInterceptor(UsersConnectionRepository connectionRepository, OpenTipBotUserRepository opentipbotUserRepository) {
// 		this.connectionRepository = connectionRepository;
//         this.opentipbotUserRepository = opentipbotUserRepository;
// 	}
// 	
// 	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 		rememberUser(request, response);
// 		handleSignOut(request, response);			
// 		if (SecurityContext.userSignedIn() || requestForNotProtected(request)) {
// 			return true;
// 		} else {
// 			return requireSignIn(request, response);
// 		}
// 	}
// 	
// 	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 		SecurityContext.remove();
// 	}
// 
// 	// internal helpers
// 
// 	private void rememberUser(HttpServletRequest request, HttpServletResponse response) {
// 		String userId = userCookieGenerator.readCookieValue(request);
// 		if (userId == null) {
// 			return;
// 		}
// 		if (!userNotFound(userId)) {
// 			userCookieGenerator.removeCookie(response);
// 			return;
// 		}
//         OpenTipBotUser opentipbotUser= opentipbotUserRepository.findOne(Long.parseLong(userId));
//         if (opentipbotUser == null)
//             return;
// 		SecurityContext.setCurrentUser(opentipbotUser);
// 	}
// 
// 	private void handleSignOut(HttpServletRequest request, HttpServletResponse response) {
// 		if (SecurityContext.userSignedIn() && request.getServletPath().startsWith("/signout")) {
// 			connectionRepository.createConnectionRepository(SecurityContext.getCurrentUser().getId().toString()).removeConnections("twitter");
// 			userCookieGenerator.removeCookie(response);
// 			SecurityContext.remove();			
// 		}
// 	}
// 		
// 	private boolean requestForNotProtected(HttpServletRequest request) {
// 		return request.getServletPath().startsWith("/signin") || request.getServletPath().startsWith("/documentation")
//                 || request.getServletPath().startsWith("/about");
// 	}
// 	
// 	private boolean requireSignIn(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 		new RedirectView("/signin", true).render(null, request, response);
// 		return false;
// 	}
// 
// 	private boolean userNotFound(String userId) {
// 		// doesn't bother checking a local user database: simply checks if the userId is connected to Twitter
// 		return connectionRepository.createConnectionRepository(userId).findPrimaryConnection(Twitter.class) != null;
// 	}
// 	
// }


// Path: opentipbot-webapp/src/main/java/opentipbot/web/config/WebAppContext.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.web.accept.ContentNegotiationManager;
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.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;
import opentipbot.persistence.repository.OpenTipBotUserRepository;
import opentipbot.web.security.UserInterceptor;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

package opentipbot.web.config;



/**
 * @author Petri Kainulainen
 */
@Configuration
@ComponentScan(basePackages = {
        "opentipbot.web.controller"
})
@EnableWebMvc
public class WebAppContext extends WebMvcConfigurerAdapter {

    @Autowired
    OpenTipBotUserRepository opentipbotUserRepository;