// Path: src/main/java/org/broadinstitute/macarthurlab/matchbox/entities/AuthorizedToken.java
// public class AuthorizedToken {
// 	@Id
// 	private String id;
// 	/**
// 	 * Authorization token
// 	 */
// 	private final String token;
// 	/**
// 	 * The center that the token is for
// 	 */
// 	private final String centerName;
// 	/**
// 	 * The primary contact email of the center
// 	 */
// 	private final String primaryContactEmail;
// 	
// 	
// 	/**
// 	 * Default no-argument constructor creates empty object
// 	 */
// 	public AuthorizedToken() {
// 		this.id = "";
// 		this.token = "";
// 		this.centerName = "";
// 		this.primaryContactEmail = "";
// 	}
// 	
// 	
// 	/**
// 	 * @param id	id in database
// 	 * @param token	token
// 	 * @param centerName	name of the center the token is for
// 	 * @param primaryContactEmail	contact email address for this center
// 	 */
// 	public AuthorizedToken(String id, String token, String centerName, String primaryContactEmail) {
// 		this.id = id;
// 		this.token = token;
// 		this.centerName = centerName;
// 		this.primaryContactEmail = primaryContactEmail;
// 	}
// 	
// 	
// 	/**
// 	 * @return the id
// 	 */
// 	public String getId() {
// 		return id;
// 	}
// 	/**
// 	 * @param id the id to set
// 	 */
// 	public void setId(String id) {
// 		this.id = id;
// 	}
// 	/**
// 	 * @return the token
// 	 */
// 	public String getToken() {
// 		return token;
// 	}
// 	/**
// 	 * @return the centerName
// 	 */
// 	public String getCenterName() {
// 		return centerName;
// 	}
// 	/**
// 	 * @return the primaryContactEmail
// 	 */
// 	public String getPrimaryContactEmail() {
// 		return primaryContactEmail;
// 	}
// 	
// 	
// 	
// }


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.broadinstitute.macarthurlab.matchbox.entities.AuthorizedToken;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 	Authentication module
 * 
 * #TODO
 * 1. Add filter to allow ONLY privileged users access to /individual/*
 * branches
 */
package org.broadinstitute.macarthurlab.matchbox.authentication;



@Component
public class AuthenticationFilter implements Filter{
	private static final String X_AUTH_TOKEN_HEADER="X-Auth-Token";
	private static final String ACCEPT_HEADER="Accept";
	private AccessAuthorizedNode accessAuthorizedNode;	
	private final List<String> authorizedTokens;
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private final Map<String,String> tokenToMMECenterMapping;
	

		
	/**
	 * Initializes class
	 */
	public AuthenticationFilter(){
    	String configFile = "file:" + System.getProperty("user.dir") + "/config/config.xml";
    	ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
    	this.accessAuthorizedNode = context.getBean("accessAuthorizedNode", AccessAuthorizedNode.class);

    	Map<String,String> tokenToMMECenterMapping = new HashMap<String,String>();
    	List<String> authorizedTokes=new ArrayList<String>();
// Path: src/main/java/org/broadinstitute/macarthurlab/matchbox/entities/AuthorizedToken.java
// public class AuthorizedToken {
// 	@Id
// 	private String id;
// 	/**
// 	 * Authorization token
// 	 */
// 	private final String token;
// 	/**
// 	 * The center that the token is for
// 	 */
// 	private final String centerName;
// 	/**
// 	 * The primary contact email of the center
// 	 */
// 	private final String primaryContactEmail;
// 	
// 	
// 	/**
// 	 * Default no-argument constructor creates empty object
// 	 */
// 	public AuthorizedToken() {
// 		this.id = "";
// 		this.token = "";
// 		this.centerName = "";
// 		this.primaryContactEmail = "";
// 	}
// 	
// 	
// 	/**
// 	 * @param id	id in database
// 	 * @param token	token
// 	 * @param centerName	name of the center the token is for
// 	 * @param primaryContactEmail	contact email address for this center
// 	 */
// 	public AuthorizedToken(String id, String token, String centerName, String primaryContactEmail) {
// 		this.id = id;
// 		this.token = token;
// 		this.centerName = centerName;
// 		this.primaryContactEmail = primaryContactEmail;
// 	}
// 	
// 	
// 	/**
// 	 * @return the id
// 	 */
// 	public String getId() {
// 		return id;
// 	}
// 	/**
// 	 * @param id the id to set
// 	 */
// 	public void setId(String id) {
// 		this.id = id;
// 	}
// 	/**
// 	 * @return the token
// 	 */
// 	public String getToken() {
// 		return token;
// 	}
// 	/**
// 	 * @return the centerName
// 	 */
// 	public String getCenterName() {
// 		return centerName;
// 	}
// 	/**
// 	 * @return the primaryContactEmail
// 	 */
// 	public String getPrimaryContactEmail() {
// 		return primaryContactEmail;
// 	}
// 	
// 	
// 	
// }


// Path: src/main/java/org/broadinstitute/macarthurlab/matchbox/authentication/AuthenticationFilter.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.broadinstitute.macarthurlab.matchbox.entities.AuthorizedToken;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 	Authentication module
 * 
 * #TODO
 * 1. Add filter to allow ONLY privileged users access to /individual/*
 * branches
 */
package org.broadinstitute.macarthurlab.matchbox.authentication;



@Component
public class AuthenticationFilter implements Filter{
	private static final String X_AUTH_TOKEN_HEADER="X-Auth-Token";
	private static final String ACCEPT_HEADER="Accept";
	private AccessAuthorizedNode accessAuthorizedNode;	
	private final List<String> authorizedTokens;
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private final Map<String,String> tokenToMMECenterMapping;
	

		
	/**
	 * Initializes class
	 */
	public AuthenticationFilter(){
    	String configFile = "file:" + System.getProperty("user.dir") + "/config/config.xml";
    	ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
    	this.accessAuthorizedNode = context.getBean("accessAuthorizedNode", AccessAuthorizedNode.class);

    	Map<String,String> tokenToMMECenterMapping = new HashMap<String,String>();
    	List<String> authorizedTokes=new ArrayList<String>();
    	for(AuthorizedToken authorizeNode  : this.getAccessAuthorizedNode().getAccessAuthorizedNodes()){