public void start(OkReplayConfig configuration, Tape tape) { this.configuration = configuration; this.tape = Optional.fromNullable(tape); isRunning = true; }
@Override public Charset getCharset() { String header = header(CONTENT_TYPE); if (isNullOrEmpty(header)) { // TODO: this isn't valid for non-text data – this method should return Optional<String> return UTF_8; } else { return Optional.fromNullable(MediaType.parse(header).charset()).or(UTF_8); } }
@Override public okhttp3.Response intercept(Chain chain) throws IOException { okhttp3.Request request = chain.request(); if (isRunning && !isHostIgnored(request)) { if (!tape.isPresent()) { return buildResponse(request, 403, "No tape"); } else { //noinspection ConstantConditions Tape tape = this.tape.get(); Request recordedRequest = OkHttpRequestAdapter.adapt(request); if (tape.isReadable() && tape.seek(recordedRequest)) { return replayResponse(request, tape, recordedRequest); } else { LOG.warning(String.format("no matching request found on tape '%s' for request %s %s", tape.getName(), request.method(), request.url().toString())); if (tape.getMode() == TapeMode.READ_ONLY_QUIET) { return buildResponse(request, 404, "No matching response"); } // If the tape isn't writeable, abandon this request. This prevents us from // talking to the server for non-mutable tapes. if (!tape.isWritable()) { throwTapeNotWritable(request.method() + " " + request.url().toString()); } // Continue the request and attempt to write the response to the tape. return recordResponse(request, tape, recordedRequest, chain.proceed(request)); } } } else { return chain.proceed(request); } }
/** * Starts the Recorder, inserting a tape with the specified parameters. * * @param tapeName the name of the tape. * @param mode the tape mode. If not supplied the default mode from the configuration is * used. * @param matchRule the rules used to match recordings on the tape. If not supplied a default is * used. * @throws IllegalStateException if the Recorder is already started. */ @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public void start(String tapeName, Optional<TapeMode> mode, Optional<MatchRule> matchRule) { if (tape != null) { throw new IllegalStateException("start called when Recorder is already started"); } tape = getTapeLoader().loadTape(tapeName); tape.setMode(mode.or(configuration.getDefaultMode())); tape.setMatchRule(matchRule.or(configuration.getDefaultMatchRule())); configuration.interceptor().start(configuration, tape); for (RecorderListener listener : listeners) { listener.onRecorderStart(tape); } }
@Override public Charset getCharset() { String header = header(CONTENT_TYPE); if (isNullOrEmpty(header)) { // TODO: this isn't valid for non-text data – this method should return Optional<String> return UTF_8; } else { return Optional.fromNullable(MediaType.parse(header).charset()).or(UTF_8); } }
@Override public okhttp3.Response intercept(Chain chain) throws IOException { okhttp3.Request request = chain.request(); if (isRunning && !isHostIgnored(request)) { if (!tape.isPresent()) { return new okhttp3.Response.Builder() // } else { Tape tape = this.tape.get(); Request recordedRequest = OkHttpRequestAdapter.adapt(request); if (tape.isReadable() && tape.seek(recordedRequest)) {
/** * Starts the Recorder, inserting a tape with the specified parameters. * * @param tapeName the name of the tape. * @param mode the tape mode. If not supplied the default mode from the configuration is * used. * @param matchRule the rules used to match recordings on the tape. If not supplied a default is * used. * @throws IllegalStateException if the Recorder is already started. */ @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public void start(String tapeName, Optional<TapeMode> mode, Optional<MatchRule> matchRule) { if (tape != null) { throw new IllegalStateException("start called when Recorder is already started"); } tape = getTapeLoader().loadTape(tapeName); tape.setMode(mode.or(configuration.getDefaultMode())); tape.setMatchRule(matchRule.or(configuration.getDefaultMatchRule())); configuration.interceptor().start(configuration, tape); for (RecorderListener listener : listeners) { listener.onRecorderStart(tape); } }
public void start(OkReplayConfig configuration, Tape tape) { this.configuration = configuration; this.tape = Optional.fromNullable(tape); isRunning = true; }
/** * If {@code nullableReference} is non-null, returns an {@code Optional} instance containing that * reference; otherwise returns {@link Optional#absent}. * <p> * <p><b>Comparison to {@code java.util.Optional}:</b> this method is equivalent to Java 8's * {@code Optional.ofNullable}. */ static <T> Optional<T> fromNullable(@Nullable T nullableReference) { return (nullableReference == null) ? Optional.<T>absent() : new Present<T>(nullableReference); }
/** * If {@code nullableReference} is non-null, returns an {@code Optional} instance containing that * reference; otherwise returns {@link Optional#absent}. * <p> * <p><b>Comparison to {@code java.util.Optional}:</b> this method is equivalent to Java 8's * {@code Optional.ofNullable}. */ static <T> Optional<T> fromNullable(@Nullable T nullableReference) { return (nullableReference == null) ? Optional.<T>absent() : new Present<T>(nullableReference); }
@Override <V> Optional<V> transform(Function<? super T, V> function) { checkNotNull(function); return Optional.absent(); }
@Override <V> Optional<V> transform(Function<? super T, V> function) { checkNotNull(function); return Optional.absent(); }
public void start(String tapeName, TapeMode mode) { start(tapeName, mode.toOptional(), Optional.<MatchRule>absent()); }
public void start(String tapeName, TapeMode mode) { start(tapeName, mode.toOptional(), Optional.<MatchRule>absent()); }