<commit_before>package org.gbif.ws.server.guice;

import org.gbif.ws.json.JacksonJsonContextResolver;
import org.gbif.ws.util.PropertiesUtil;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Nullable;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.servlet.GuiceServletContextListener;
import com.sun.jersey.spi.container.ContainerRequestFilter;
import com.sun.jersey.spi.container.ContainerResponseFilter;

/**
 * A basic servlet listener for all our webservices.
 * Note that the request filters will be ordered as follows if provided:
 * <ol>
 * <li>The default filters will always be first</li>
 * <li>The authentication filter will be next if enabled (installAuth=true)</li>
 * <li>The provided filters will be last in the given order</li>
 * </ol>
 */
public abstract class GbifServletListener extends GuiceServletContextListener {

  private final Properties properties;
  private final String resourcePackages;
  private final boolean installAuth;
  private final List<Class<? extends ContainerResponseFilter>> responseFilters = Lists.newArrayList();
  private final List<Class<? extends ContainerRequestFilter>> requestFilters = Lists.newArrayList();

  private Injector injector;

  /**
   * @param properties The properties to load
   * @param resourcePackages packages to scan for jersey resources,
   *        for example "org.gbif.registry.ws,org.gbif.registry.common.ws"
   * @param installAuthenticationFilters if true installs support for authentication. Requires an implementation of
   *        UserService to be installed by one of the additional modules.
   */
  protected GbifServletListener(Properties properties, String resourcePackages, boolean installAuthenticationFilters) {
    this(properties, resourcePackages, installAuthenticationFilters, null);
  }

  /**
   * @param properties The properties to load
   * @param resourcePackages packages to scan for jersey resources,
   *        for example "org.gbif.registry.ws,org.gbif.registry.common.ws"
   * @param installAuthenticationFilters if true installs support for authentication. Requires an implementation of
   *        UserService to be installed by one of the additional modules.
   * @param responseFilters A list of response filters that are applied to the requests.
   */
  protected GbifServletListener(Properties properties, String resourcePackages, boolean installAuthenticationFilters,
    @Nullable List<Class<? extends ContainerResponseFilter>> responseFilters) {
    this(properties, resourcePackages, installAuthenticationFilters, responseFilters, null);
  }

  /**
   * @param properties The properties to load
   * @param resourcePackages packages to scan for jersey resources,
   *        for example "org.gbif.registry.ws,org.gbif.registry.common.ws"
   * @param installAuthenticationFilters if true installs support for authentication. Requires an implementation of
   *        UserService to be installed by one of the additional modules.
   * @param responseFilters A list of response filters that are applied to the requests.
   * @param requestFilters A list of request filters that are applied to the requests. Note: read constructor
   *        documentation on {@link WsJerseyModule}
   */
  protected GbifServletListener(Properties properties, String resourcePackages, boolean installAuthenticationFilters,
    @Nullable List<Class<? extends ContainerResponseFilter>> responseFilters,
    @Nullable List<Class<? extends ContainerRequestFilter>> requestFilters) {
    this.properties = properties;
    this.resourcePackages = resourcePackages;
    this.installAuth = installAuthenticationFilters;
    if (responseFilters != null) {
      this.responseFilters.addAll(responseFilters);
    }
    if (requestFilters != null) {
      this.requestFilters.addAll(requestFilters);
    }
  }

  /**
   * @param propertyFileName Fully qualified filename that must be on the classpath
   * @param resourcePackages packages to scan for jersey resources,
   *        for example "org.gbif.registry.ws,org.gbif.registry.common.ws"
   * @param installAuthenticationFilters if true installs support for authentication. Requires an implementation of
   *        UserService to be installed by one of the additional modules.
   * @param responseFilters A list of response filters that are applied to the requests.
   */
  protected GbifServletListener(String propertyFileName, String resourcePackages, boolean installAuthenticationFilters,
    @Nullable List<Class<? extends ContainerResponseFilter>> responseFilters) {
    this(PropertiesUtil.readFromClasspath(propertyFileName), resourcePackages, installAuthenticationFilters,
      responseFilters);
  }<commit_msg>new method 'readProperties': handles the checked exception that can be thrown when reading a file.<commit_after>package org.gbif.ws.server.guice;

import org.gbif.ws.json.JacksonJsonContextResolver;
import org.gbif.ws.util.PropertiesUtil;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Nullable;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.servlet.GuiceServletContextListener;
import com.sun.jersey.spi.container.ContainerRequestFilter;
import com.sun.jersey.spi.container.ContainerResponseFilter;

/**
 * A basic servlet listener for all our webservices.
 * Note that the request filters will be ordered as follows if provided:
 * <ol>
 * <li>The default filters will always be first</li>
 * <li>The authentication filter will be next if enabled (installAuth=true)</li>
 * <li>The provided filters will be last in the given order</li>
 * </ol>
 */
public abstract class GbifServletListener extends GuiceServletContextListener {

  private final Properties properties;
  private final String resourcePackages;
  private final boolean installAuth;
  private final List<Class<? extends ContainerResponseFilter>> responseFilters = Lists.newArrayList();
  private final List<Class<? extends ContainerRequestFilter>> requestFilters = Lists.newArrayList();

  private Injector injector;

  /**
   * @param properties The properties to load
   * @param resourcePackages packages to scan for jersey resources,
   *        for example "org.gbif.registry.ws,org.gbif.registry.common.ws"
   * @param installAuthenticationFilters if true installs support for authentication. Requires an implementation of
   *        UserService to be installed by one of the additional modules.
   */
  protected GbifServletListener(Properties properties, String resourcePackages, boolean installAuthenticationFilters) {
    this(properties, resourcePackages, installAuthenticationFilters, null);
  }

  /**
   * @param properties The properties to load
   * @param resourcePackages packages to scan for jersey resources,
   *        for example "org.gbif.registry.ws,org.gbif.registry.common.ws"
   * @param installAuthenticationFilters if true installs support for authentication. Requires an implementation of
   *        UserService to be installed by one of the additional modules.
   * @param responseFilters A list of response filters that are applied to the requests.
   */
  protected GbifServletListener(Properties properties, String resourcePackages, boolean installAuthenticationFilters,
    @Nullable List<Class<? extends ContainerResponseFilter>> responseFilters) {
    this(properties, resourcePackages, installAuthenticationFilters, responseFilters, null);
  }

  /**
   * @param properties The properties to load
   * @param resourcePackages packages to scan for jersey resources,
   *        for example "org.gbif.registry.ws,org.gbif.registry.common.ws"
   * @param installAuthenticationFilters if true installs support for authentication. Requires an implementation of
   *        UserService to be installed by one of the additional modules.
   * @param responseFilters A list of response filters that are applied to the requests.
   * @param requestFilters A list of request filters that are applied to the requests. Note: read constructor
   *        documentation on {@link WsJerseyModule}
   */
  protected GbifServletListener(Properties properties, String resourcePackages, boolean installAuthenticationFilters,
    @Nullable List<Class<? extends ContainerResponseFilter>> responseFilters,
    @Nullable List<Class<? extends ContainerRequestFilter>> requestFilters) {
    this.properties = properties;
    this.resourcePackages = resourcePackages;
    this.installAuth = installAuthenticationFilters;
    if (responseFilters != null) {
      this.responseFilters.addAll(responseFilters);
    }
    if (requestFilters != null) {
      this.requestFilters.addAll(requestFilters);
    }
  }

  /**
   * @param propertyFileName Fully qualified filename that must be on the classpath
   * @param resourcePackages packages to scan for jersey resources,
   *        for example "org.gbif.registry.ws,org.gbif.registry.common.ws"
   * @param installAuthenticationFilters if true installs support for authentication. Requires an implementation of
   *        UserService to be installed by one of the additional modules.
   * @param responseFilters A list of response filters that are applied to the requests.
   */
  protected GbifServletListener(String propertyFileName, String resourcePackages, boolean installAuthenticationFilters,
    @Nullable List<Class<? extends ContainerResponseFilter>> responseFilters) {
    this(readProperties(propertyFileName), resourcePackages, installAuthenticationFilters,
      responseFilters);
  }

  /**
   * Hides the checked exception that can be thrown when reading a file.
   */
  private static Properties readProperties(String propertyFileName) {
    Properties properties = null;
    try {
      properties = PropertiesUtil.readFromClasspath(propertyFileName);
    } catch (IOException e) {
      new IllegalArgumentException("Error reading properties file", e);
    }
    return properties;
  }
{'fraction_non_alphanumeric': 0.07333984375, 'fraction_numerical': 0.0, 'mean_word_length': 4.565760869565217, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '3139292', 'n_tokens_mistral': 2504, 'n_tokens_neox': 2537, 'n_words': 866}