package de.ivu.elect.business.system.control;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.konfiguration.entity.ElectKonfigurationWerte;
import de.ivu.elect.business.konfiguration.entity.KonfigurationLoader;
import de.ivu.elect.jee.faces.HttpSecurityHeader;
import de.ivu.elect.jee.faces.context.ElectExternalContext;
import de.ivu.elect.jee.presentation.renderkit.ElectRenderKit;
import de.ivu.elect.jee.startup.PostStartup;

public class PrimefacesCspPostStartup implements PostStartup {

  private static final Logger LOG = LoggerFactory.getLogger(PrimefacesCspPostStartup.class);

  @Inject
  private transient KonfigurationLoader konfigurationLoader;

  @Override
  public void postStartup() {
    if (isPrimefacesCspAktivieren()) {
      ElectRenderKit.ENABLED.set(true);
      ElectExternalContext.CSP_ENABLED.set(true);
      HttpSecurityHeader.disableCSP(); // eigenes CSP-Handling deakticieren
      LOG.info("ElectRenderKit/CSP aktiviert");
    } else {
      ElectRenderKit.ENABLED.set(false);
      ElectExternalContext.CSP_ENABLED.set(false);
      LOG.info("ElectRenderKit/CSP deaktiviert");
    }
  }

  private boolean isPrimefacesCspAktivieren() {
    return konfigurationLoader.resolveBooleanValue(ElectKonfigurationWerte.PRIMEFACES_CSP_AKTIVIEREN);
  }
}
