<commit_before>package pdp.xacml;

import org.apache.openaz.xacml.api.pip.PIPFinder;
import org.apache.openaz.xacml.pdp.policy.Policy;
import org.apache.openaz.xacml.pdp.policy.PolicyDef;
import org.apache.openaz.xacml.pdp.policy.PolicyFinder;
import org.apache.openaz.xacml.pdp.policy.dom.DOMPolicyDef;
import org.apache.openaz.xacml.pdp.std.StdEvaluationContextFactory;
import org.apache.openaz.xacml.pdp.std.StdPolicyFinder;
import org.apache.openaz.xacml.std.StdStatusCode;
import org.apache.openaz.xacml.std.dom.DOMStructureException;
import org.apache.openaz.xacml.util.XACMLProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pdp.PdpPolicyRepository;
import pdp.xacml.teams.VootClient;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import static java.util.stream.Collectors.toCollection;
import static java.util.stream.StreamSupport.stream;

public class OpenConextEvaluationContextFactory extends StdEvaluationContextFactory {

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

  private PdpPolicyRepository pdpPolicyRepository;
  private boolean cachePolicies;
  private VootClient vootClient;

  public OpenConextEvaluationContextFactory() throws IOException {
    this.cachePolicies = Boolean.valueOf(XACMLProperties.getProperties().getProperty("openconext.pdp.cachePolicies", "true"));
  }

  public void setPdpPolicyRepository(PdpPolicyRepository pdpPolicyRepository) {
    this.pdpPolicyRepository = pdpPolicyRepository;
    setPolicyFinder(loadPolicyFinder());
  }

  @Override
  protected PolicyFinder getPolicyFinder() {
    if (cachePolicies) {
      return super.getPolicyFinder();
    } else {
      return loadPolicyFinder();
    }
  }

  private PolicyFinder loadPolicyFinder() {
    Collection<PolicyDef> rootPolicies =
        stream(pdpPolicyRepository.findAll().spliterator(), false).map(policy -> convertToPolicyDef(policy.getPolicyXml())).collect(toCollection(ArrayList::new));
    LOG.info("(Re)-loaded {} policies from the database", rootPolicies.size());
    return new StdPolicyFinder(rootPolicies, null);
  }


  private PolicyDef convertToPolicyDef(String policyXml) {
    try {
      return DOMPolicyDef.load(new ByteArrayInputStream(policyXml.replaceFirst("\n", "").getBytes()));
    } catch (DOMStructureException e) {
      LOG.error("Error loading policy from " + policyXml, e);
      return new Policy(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, e.getMessage());
    }
  }


  public void setVootClient(VootClient vootClient) {
    this.vootClient = vootClient;
    PIPFinder pipFinder;
    setPIPFinder(loadPIPFinder(vootClient));
  }

  private PIPFinder loadPIPFinder(VootClient vootClient) {
    OpenConextConfigurableEngineFinder pipFinder = new OpenConextConfigurableEngineFinder(vootClient);
    try {
      pipFinder.configure(XACMLProperties.getProperties());
      return pipFinder;
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }
}<commit_msg>Boy scout remove unused assigns<commit_after>package pdp.xacml;

import org.apache.openaz.xacml.api.pip.PIPFinder;
import org.apache.openaz.xacml.pdp.policy.Policy;
import org.apache.openaz.xacml.pdp.policy.PolicyDef;
import org.apache.openaz.xacml.pdp.policy.PolicyFinder;
import org.apache.openaz.xacml.pdp.policy.dom.DOMPolicyDef;
import org.apache.openaz.xacml.pdp.std.StdEvaluationContextFactory;
import org.apache.openaz.xacml.pdp.std.StdPolicyFinder;
import org.apache.openaz.xacml.std.StdStatusCode;
import org.apache.openaz.xacml.std.dom.DOMStructureException;
import org.apache.openaz.xacml.util.XACMLProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pdp.PdpPolicyRepository;
import pdp.xacml.teams.VootClient;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import static java.util.stream.Collectors.toCollection;
import static java.util.stream.StreamSupport.stream;

public class OpenConextEvaluationContextFactory extends StdEvaluationContextFactory {

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

  private PdpPolicyRepository pdpPolicyRepository;
  private boolean cachePolicies;

  public OpenConextEvaluationContextFactory() throws IOException {
    this.cachePolicies = Boolean.valueOf(XACMLProperties.getProperties().getProperty("openconext.pdp.cachePolicies", "true"));
  }

  public void setPdpPolicyRepository(PdpPolicyRepository pdpPolicyRepository) {
    this.pdpPolicyRepository = pdpPolicyRepository;
    setPolicyFinder(loadPolicyFinder());
  }

  @Override
  protected PolicyFinder getPolicyFinder() {
    if (cachePolicies) {
      return super.getPolicyFinder();
    } else {
      return loadPolicyFinder();
    }
  }

  private PolicyFinder loadPolicyFinder() {
    Collection<PolicyDef> rootPolicies =
        stream(pdpPolicyRepository.findAll().spliterator(), false).map(policy -> convertToPolicyDef(policy.getPolicyXml())).collect(toCollection(ArrayList::new));
    LOG.info("(Re)-loaded {} policies from the database", rootPolicies.size());
    return new StdPolicyFinder(rootPolicies, null);
  }


  private PolicyDef convertToPolicyDef(String policyXml) {
    try {
      return DOMPolicyDef.load(new ByteArrayInputStream(policyXml.replaceFirst("\n", "").getBytes()));
    } catch (DOMStructureException e) {
      LOG.error("Error loading policy from " + policyXml, e);
      return new Policy(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, e.getMessage());
    }
  }

  public void setVootClient(VootClient vootClient) {
    setPIPFinder(loadPIPFinder(vootClient));
  }

  private PIPFinder loadPIPFinder(VootClient vootClient) {
    OpenConextConfigurableEngineFinder pipFinder = new OpenConextConfigurableEngineFinder(vootClient);
    try {
      pipFinder.configure(XACMLProperties.getProperties());
      return pipFinder;
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }
}
{'fraction_non_alphanumeric': 0.10044532409698169, 'fraction_numerical': 0.0006597394029358403, 'mean_word_length': 5.283937823834197, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2369146', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1883, 'n_words': 307}