1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.remote.http;18import java.io.UncheckedIOException;19import java.util.Objects;20import java.util.function.Function;21/**22 * Can be wrapped around an {@link HttpHandler} in order to either modify incoming23 * {@link HttpRequest}s or outgoing {@link HttpResponse}s using the well-known "Filter" pattern.24 * This is very similar to the Servlet spec's {@link javax.servlet.Filter}, but takes advantage of25 * lambdas:26 * <pre>{@code27 * Filter filter = next -> {28 * return req -> {29 * req.addHeader("cheese", "brie");30 * HttpResponse res = next.apply(req);31 * res.addHeader("vegetable", "peas");32 * return res;33 * };34 * }35 * }</pre>36 *37 *<p>Because each filter returns an {@link HttpHandler}, it's easy to do processing before, or after38 * each request, as well as short-circuit things if necessary.39 */40@FunctionalInterface41public interface Filter extends Function<HttpHandler, HttpHandler> {42 default Filter andThen(Filter next) {43 Objects.requireNonNull(next, "Next filter must be set.");44 return req -> apply(next.apply(req));45 }46 default HttpHandler andFinally(HttpHandler end) {47 Objects.requireNonNull(end, "HTTP handler must be set.");48 return request -> Filter.this.apply(end).execute(request);49 }50 default Routable andFinally(Routable end) {51 return new Routable() {52 @Override53 public HttpResponse execute(HttpRequest req) throws UncheckedIOException {54 return Filter.this.apply(end).execute(req);55 }56 @Override57 public boolean matches(HttpRequest req) {58 return end.matches(req);59 }60 };61 }62}...