All Downloads are FREE. Search and download functionalities are using the official Maven repository.

walkman.WalkmanConfig Maven / Gradle / Ivy

package walkman;

import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

/**
 * The configuration used by walkman.
 *
 * `WalkmanConfig` instances are created with a builder. For example:
 *
 * [source,java]
 * ----
 * WalkmanConfig configuration = new WalkmanConfig.Builder()
 * .tapeRoot(tapeRoot)
 * .ignoreLocalhost(true)
 * .build();
 * ----
 *
 * @see Builder
 */
@SuppressWarnings("WeakerAccess")
public class WalkmanConfig {
  public static final String DEFAULT_TAPE_ROOT = "src/test/resources/walkman/tapes";
  public static final TapeMode DEFAULT_MODE = TapeMode.READ_ONLY;
  public static final MatchRule DEFAULT_MATCH_RULE = ComposedMatchRule.of(MatchRules.method,
      MatchRules.uri);

  private final TapeRoot tapeRoot;
  private final TapeMode defaultMode;
  private final ImmutableCollection ignoreHosts;
  private final boolean ignoreLocalhost;
  private final MatchRule defaultMatchRule;
  private final boolean sslEnabled;
  private final WalkmanInterceptor interceptor;

  protected WalkmanConfig(Builder builder) {
    this.tapeRoot = builder.tapeRoot;
    this.defaultMode = builder.defaultMode;
    this.defaultMatchRule = builder.defaultMatchRule;
    this.ignoreHosts = builder.ignoreHosts;
    this.ignoreLocalhost = builder.ignoreLocalhost;
    this.sslEnabled = builder.sslEnabled;
    this.interceptor = builder.interceptor;
  }

  /**
   * The base directory where tape files are stored.
   */
  public TapeRoot getTapeRoot() {
    return tapeRoot;
  }

  /**
   * The default mode for an inserted tape.
   */
  public TapeMode getDefaultMode() {
    return defaultMode;
  }

  public MatchRule getDefaultMatchRule() {
    return defaultMatchRule;
  }

  /**
   * Hosts that are ignored by walkman. Any connections made will be allowed to proceed
   * normally and not be intercepted.
   */
  public Collection getIgnoreHosts() {
    if (isIgnoreLocalhost()) {
      return new ImmutableSet.Builder()
          .addAll(ignoreHosts)
          .addAll(Network.getLocalAddresses())
          .build();
    } else {
      return ignoreHosts;
    }
  }

  /**
   * If `true` then all connections to localhost addresses are ignored.
   *
   * This is equivalent to including the following in the collection returned by {@link
   * #getIgnoreHosts()}: * `"localhost"` * `"127.0.0.1"` * `InetAddress.getLocalHost()
   * .getHostName()`
   * * `InetAddress.getLocalHost().getHostAddress()`
   */
  public boolean isIgnoreLocalhost() {
    return ignoreLocalhost;
  }

  public WalkmanInterceptor interceptor() {
    return interceptor;
  }

  /**
   * If set to true add support for proxying SSL (disable certificate
   * checking).
   */
  public boolean isSslEnabled() {
    return sslEnabled;
  }

  /**
   * Called by the `Recorder` instance so that the configuration can add listeners.
   *
   * You should **not** call this method yourself.
   */
  public void registerListeners(Collection listeners) {
    listeners.add(new ProxyServer(this, interceptor));
  }

  public static class Builder {
    TapeRoot tapeRoot = new DefaultTapeRoot(new File(WalkmanConfig.DEFAULT_TAPE_ROOT));
    TapeMode defaultMode = WalkmanConfig.DEFAULT_MODE;
    MatchRule defaultMatchRule = WalkmanConfig.DEFAULT_MATCH_RULE;
    ImmutableCollection ignoreHosts = ImmutableList.of();
    boolean ignoreLocalhost;
    boolean sslEnabled;
    WalkmanInterceptor interceptor;

    public Builder() {
      try {
        URL propertiesFile = WalkmanConfig.class.getResource("/walkman.properties");
        if (propertiesFile != null) {
          Properties properties = new Properties();
          properties.load(propertiesFile.openStream());
          withProperties(properties);
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }

    public Builder withProperties(Properties properties) {
      if (properties.containsKey("walkman.tapeRoot")) {
        tapeRoot(new File(properties.getProperty("walkman.tapeRoot")));
      }

      if (properties.containsKey("walkman.defaultMode")) {
        defaultMode(TapeMode.valueOf(properties.getProperty("walkman.defaultMode")));
      }

      if (properties.containsKey("walkman.defaultMatchRules")) {
        List rules = Lists.transform(Splitter.on(",").splitToList(properties
            .getProperty("walkman.defaultMatchRules")), new Function() {
          @Override public MatchRule apply(String input) {
            return MatchRules.valueOf(input);
          }
        });
        defaultMatchRule(ComposedMatchRule.of(rules));
      }

      if (properties.containsKey("walkman.ignoreHosts")) {
        ignoreHosts(Splitter.on(",").splitToList(properties.getProperty("walkman.ignoreHosts")));
      }

      if (properties.containsKey("walkman.ignoreLocalhost")) {
        ignoreLocalhost(Boolean.valueOf(properties.getProperty("walkman.ignoreLocalhost")));
      }

      if (properties.containsKey("walkman.sslEnabled")) {
        sslEnabled(TypedProperties.getBoolean(properties, "walkman.sslEnabled"));
      }

      return this;
    }

    public Builder tapeRoot(File tapeRoot) {
      return tapeRoot(new DefaultTapeRoot(tapeRoot));
    }

    public Builder tapeRoot(TapeRoot tapeRoot) {
      this.tapeRoot = tapeRoot;
      return this;
    }

    public Builder defaultMode(TapeMode defaultMode) {
      this.defaultMode = defaultMode;
      return this;
    }

    public Builder interceptor(WalkmanInterceptor interceptor) {
      this.interceptor = interceptor;
      return this;
    }

    public Builder defaultMatchRule(MatchRule defaultMatchRule) {
      this.defaultMatchRule = defaultMatchRule;
      return this;
    }

    public Builder defaultMatchRules(MatchRule... defaultMatchRules) {
      this.defaultMatchRule = ComposedMatchRule.of(defaultMatchRules);
      return this;
    }

    public Builder ignoreHosts(Iterable ignoreHosts) {
      this.ignoreHosts = ImmutableList.copyOf(ignoreHosts);
      return this;
    }

    public Builder ignoreLocalhost(boolean ignoreLocalhost) {
      this.ignoreLocalhost = ignoreLocalhost;
      return this;
    }

    public Builder sslEnabled(boolean sslEnabled) {
      this.sslEnabled = sslEnabled;
      return this;
    }

    public WalkmanConfig build() {
      return new WalkmanConfig(this);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy