package de.ivu.elect.jee.faces.context;

import java.util.concurrent.atomic.AtomicBoolean;

import javax.faces.context.ExternalContext;
import javax.faces.context.ExternalContextWrapper;

import org.primefaces.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ElectExternalContext extends ExternalContextWrapper {

  public static final AtomicBoolean CSP_ENABLED = new AtomicBoolean(true);

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

  private final ExternalContext delegate;

  public ElectExternalContext(ExternalContext externalContext) {
    delegate = externalContext;
  }

  @Override
  public ExternalContext getWrapped() {
    return delegate;
  }

  @Override
  public String getInitParameter(String name) {
    if (Constants.ContextParams.CSP.equals(name)) {
      boolean csp = CSP_ENABLED.get();
      LOG.info("CSP enabled: " + csp);
      return Boolean.toString(csp);
    }
    return super.getInitParameter(name);
  }

}