@Override public boolean isReadable() { return mode.isReadable(); }
@Override public boolean isSequential() { return mode.isSequential(); }
@Override public boolean isWritable() { return mode.isWritable(); }
@Override public synchronized void record(Request request, Response response) { if (!mode.isWritable()) { throw new IllegalStateException("the tape is not writable"); } RecordedInteraction interaction = new RecordedInteraction(new Date(), recordRequest(request), recordResponse(response)); if (mode.isSequential()) { interactions.add(interaction.toYaml()); } else { int position = findMatch(request); if (position >= 0) { interactions.set(position, interaction.toYaml()); } else { interactions.add(interaction.toYaml()); } } }
@Override public Response play(final Request request) { if (!mode.isReadable()) { throw new IllegalStateException("the tape is not readable"); } if (mode.isSequential()) { Integer nextIndex = orderedIndex.getAndIncrement(); RecordedInteraction nextInteraction = interactions.get(nextIndex).toImmutable(); if (nextInteraction == null) { throw new IllegalStateException(String.format("No recording found at position %s", nextIndex)); } if (!matchRule.isMatch(request, nextInteraction.request())) { throw new IllegalStateException(String.format("Request %s does not match recorded " + "request" + " %s", stringify(request), stringify(nextInteraction.request()))); } return nextInteraction.response(); } else { int position = findMatch(request); if (position < 0) { throw new IllegalStateException("no matching recording found"); } else { return interactions.get(position).toImmutable().response(); } } }
public Optional<TapeMode> toOptional() { if (this.equals(TapeMode.UNDEFINED)) { return Optional.absent(); } else { return Optional.of(this); } } }
public void start(String tapeName, TapeMode mode) { start(tapeName, mode.toOptional(), Optional.<MatchRule>absent()); }
public Builder withProperties(Properties properties) { if (properties.containsKey("okreplay.tapeRoot")) { tapeRoot(new File(properties.getProperty("okreplay.tapeRoot"))); } if (properties.containsKey("okreplay.defaultMode")) { defaultMode(TapeMode.valueOf(properties.getProperty("okreplay.defaultMode"))); } if (properties.containsKey("okreplay.defaultMatchRules")) { String property = properties.getProperty("okreplay.defaultMatchRules"); List<MatchRule> rules = new ArrayList<>(); for (String s : Arrays.asList(property.split(","))) { rules.add(MatchRules.valueOf(s)); } defaultMatchRule(ComposedMatchRule.of(rules)); } if (properties.containsKey("okreplay.ignoreHosts")) { ignoreHosts(Arrays.asList(properties.getProperty("okreplay.ignoreHosts").split(","))); } if (properties.containsKey("okreplay.ignoreLocalhost")) { ignoreLocalhost(Boolean.valueOf(properties.getProperty("okreplay.ignoreLocalhost"))); } if (properties.containsKey("okreplay.sslEnabled")) { sslEnabled(TypedProperties.getBoolean(properties, "okreplay.sslEnabled")); } return this; }
@Override public synchronized void record(Request request, Response response) { if (!mode.isWritable()) { throw new IllegalStateException("the tape is not writable"); } RecordedInteraction interaction = new RecordedInteraction(new Date(), recordRequest(request), recordResponse(response)); if (mode.isSequential()) { interactions.add(interaction.toYaml()); } else { int position = findMatch(request); if (position >= 0) { interactions.set(position, interaction.toYaml()); } else { interactions.add(interaction.toYaml()); } } }
@Override public Response play(final Request request) { if (!mode.isReadable()) { throw new IllegalStateException("the tape is not readable"); } if (mode.isSequential()) { Integer nextIndex = orderedIndex.getAndIncrement(); RecordedInteraction nextInteraction = interactions.get(nextIndex).toImmutable(); if (nextInteraction == null) { throw new IllegalStateException(String.format("No recording found at position %s", nextIndex)); } if (!matchRule.isMatch(request, nextInteraction.request())) { throw new IllegalStateException(String.format("Request %s does not match recorded " + "request" + " %s", stringify(request), stringify(nextInteraction.request()))); } return nextInteraction.response(); } else { int position = findMatch(request); if (position < 0) { throw new IllegalStateException("no matching recording found"); } else { return interactions.get(position).toImmutable().response(); } } }
public Optional<TapeMode> toOptional() { if (this.equals(TapeMode.UNDEFINED)) { return Optional.absent(); } else { return Optional.of(this); } } }
public void start(String tapeName, TapeMode mode) { start(tapeName, mode.toOptional(), Optional.<MatchRule>absent()); }
public Builder withProperties(Properties properties) { if (properties.containsKey("okreplay.tapeRoot")) { tapeRoot(new File(properties.getProperty("okreplay.tapeRoot"))); } if (properties.containsKey("okreplay.defaultMode")) { defaultMode(TapeMode.valueOf(properties.getProperty("okreplay.defaultMode"))); } if (properties.containsKey("okreplay.defaultMatchRules")) { String property = properties.getProperty("okreplay.defaultMatchRules"); List<MatchRule> rules = new ArrayList<>(); for (String s : Arrays.asList(property.split(","))) { rules.add(MatchRules.valueOf(s)); } defaultMatchRule(ComposedMatchRule.of(rules)); } if (properties.containsKey("okreplay.ignoreHosts")) { ignoreHosts(Arrays.asList(properties.getProperty("okreplay.ignoreHosts").split(","))); } if (properties.containsKey("okreplay.ignoreLocalhost")) { ignoreLocalhost(Boolean.valueOf(properties.getProperty("okreplay.ignoreLocalhost"))); } if (properties.containsKey("okreplay.sslEnabled")) { sslEnabled(TypedProperties.getBoolean(properties, "okreplay.sslEnabled")); } return this; }
@Override public boolean isReadable() { return mode.isReadable(); }
@Override public boolean isWritable() { return mode.isWritable(); }
@Override public boolean isSequential() { return mode.isSequential(); }