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

import org.gbif.utils.HttpUtil;
import org.gbif.ws.client.BaseWsGetClient;
import org.gbif.ws.client.interceptor.HttpErrorResponseInterceptor;
import org.gbif.ws.client.interceptor.PublicMethodMatcher;
import org.gbif.ws.json.JacksonJsonContextResolver;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.PrivateModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.matcher.Matchers;
import com.google.inject.name.Names;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.client.apache4.ApacheHttpClient4;
import com.sun.jersey.client.apache4.ApacheHttpClient4Handler;
import org.apache.http.client.HttpClient;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;

/**
 * Base web service client module.
 * Allows to specify HTTP connection parameters: httpTimeout, maxHttpConnections and maxHttpConnectionsPerRoute.
 * The default values for those parameters are: httpTimeout = 10 seconds, maxHttpConnections = 100 and
 * maxHttpConnectionsPerRoute = 100.
 */
public abstract class GbifWsClientModule extends PrivateModule {


  /**
   * Default names/keys to create an HTTP client connection.
   * The values listed in this class are the expected key in a properties file.
   */
  public static class HttpClientConnParams {

    // Used for each of i) getting a connection from the pool, establishing a connection and the
    // socket timeout
    public static final String HTTP_TIMEOUT = "httpTimeout";
    public static final String MAX_HTTP_CONNECTIONS = "maxHttpConnections";
    public static final String MAX_HTTP_CONNECTIONS_PER_ROUTE = "maxHttpConnectionsPerRoute";
  }

  private final Properties properties;
  private final Set<Package> clientPackages;

  // Default values to establish a client connection
  protected static final int DEFAULT_HTTP_TIMEOUT_MSECS = 10000;
  protected static final int DEFAULT_MAX_HTTP_CONNECTIONS = 100;
  protected static final int DEFAULT_MAX_HTTP_CONNECTIONS_PER_ROUTE = 100;

  protected GbifWsClientModule(Properties properties, Package... clientPackages) {
    this.properties = properties;
    this.clientPackages = Sets.newHashSet(clientPackages);
    this.clientPackages.add(BaseWsGetClient.class.getPackage());
  }

  @Override
  protected final void configure() {
    Names.bindProperties(binder(), properties);

    // configure jsonMixIns
    JacksonJsonContextResolver.addMixIns(getPolymorphicClassMap());

    configureClient();

    // bind common interceptors
    for (Package clientPackage : clientPackages) {
      // order is important!
      bindInterceptor(Matchers.inPackage(clientPackage), new PublicMethodMatcher(), new HttpErrorResponseInterceptor());
    }
  }

  /**
   * Implement this method to configure the clients guice module.
   * You can install modules, bind classes.
   * Dont forget to expose any classes that should be visible to the outside world!
   */
  protected abstract void configureClient();

  /**
   * The Jackson JSON configuration needs to know about how to (de)serialize polymorphic classes.
   * Override this method to pass a map of mixIn classes into the Jackson context resolver.
   *
   * @return the mixIn class map. Defaults to an empty map.
   */
  protected Map<Class<?>, Class<?>> getPolymorphicClassMap() {
    return Maps.newHashMap();
  }
<commit_msg>use same wording in WsClient than ServletListener for MixIns<commit_after>package org.gbif.ws.client.guice;

import org.gbif.utils.HttpUtil;
import org.gbif.ws.client.BaseWsGetClient;
import org.gbif.ws.client.interceptor.HttpErrorResponseInterceptor;
import org.gbif.ws.client.interceptor.PublicMethodMatcher;
import org.gbif.ws.json.JacksonJsonContextResolver;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.PrivateModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.matcher.Matchers;
import com.google.inject.name.Names;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.client.apache4.ApacheHttpClient4;
import com.sun.jersey.client.apache4.ApacheHttpClient4Handler;
import org.apache.http.client.HttpClient;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;

/**
 * Base web service client module.
 * Allows to specify HTTP connection parameters: httpTimeout, maxHttpConnections and maxHttpConnectionsPerRoute.
 * The default values for those parameters are: httpTimeout = 10 seconds, maxHttpConnections = 100 and
 * maxHttpConnectionsPerRoute = 100.
 */
public abstract class GbifWsClientModule extends PrivateModule {


  /**
   * Default names/keys to create an HTTP client connection.
   * The values listed in this class are the expected key in a properties file.
   */
  public static class HttpClientConnParams {

    // Used for each of i) getting a connection from the pool, establishing a connection and the
    // socket timeout
    public static final String HTTP_TIMEOUT = "httpTimeout";
    public static final String MAX_HTTP_CONNECTIONS = "maxHttpConnections";
    public static final String MAX_HTTP_CONNECTIONS_PER_ROUTE = "maxHttpConnectionsPerRoute";
  }

  private final Properties properties;
  private final Set<Package> clientPackages;

  // Default values to establish a client connection
  protected static final int DEFAULT_HTTP_TIMEOUT_MSECS = 10000;
  protected static final int DEFAULT_MAX_HTTP_CONNECTIONS = 100;
  protected static final int DEFAULT_MAX_HTTP_CONNECTIONS_PER_ROUTE = 100;

  protected GbifWsClientModule(Properties properties, Package... clientPackages) {
    this.properties = properties;
    this.clientPackages = Sets.newHashSet(clientPackages);
    this.clientPackages.add(BaseWsGetClient.class.getPackage());
  }

  @Override
  protected final void configure() {
    Names.bindProperties(binder(), properties);

    // configure jsonMixIns
    JacksonJsonContextResolver.addMixIns(getMixIns());

    configureClient();

    // bind common interceptors
    for (Package clientPackage : clientPackages) {
      // order is important!
      bindInterceptor(Matchers.inPackage(clientPackage), new PublicMethodMatcher(), new HttpErrorResponseInterceptor());
    }
  }

  /**
   * Implement this method to configure the clients guice module.
   * You can install modules, bind classes.
   * Dont forget to expose any classes that should be visible to the outside world!
   */
  protected abstract void configureClient();


  /**
   * Override this method to pass a map of mixIn classes into the Jackson context resolver.
   * For example, the Jackson JSON configuration may need to know about how to (de)serialize polymorphic classes.
   *
   * @return the mixIn class map. Defaults to an empty map.
   */
  protected Map<Class<?>, Class<?>> getMixIns() {
    return Maps.newHashMap();
  }

{'fraction_non_alphanumeric': 0.08290782047836603, 'fraction_numerical': 0.006181134103735555, 'mean_word_length': 4.58364591147787, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '7588868', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2047, 'n_words': 599}