package de.ivu.elect.presentation.security.sso;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class SingleSignOnFilter implements Filter {

  @Inject
  private transient SingleSignOnWrapper singleSignOnWrapper;

  private FilterConfig filterConfig;

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    if (singleSignOnWrapper.isSingleSignOnAktiviert()) {
      singleSignOnWrapper.doFilter(request, response, filterChain, filterConfig);
    } else {
      filterChain.doFilter(request, response);
    }
  }

  @Override
  public void destroy() {
    filterConfig = null;
  }
}
