| 40 | import org.openqa.selenium.internal.Require; |
| 41 | |
| 42 | public abstract class Route implements HttpHandler, Routable { |
| 43 | |
| 44 | public HttpHandler fallbackTo(Supplier<HttpHandler> handler) { |
| 45 | Require.nonNull("Handler", handler); |
| 46 | return req -> { |
| 47 | if (matches(req)) { |
| 48 | return Route.this.execute(req); |
| 49 | } |
| 50 | return Require.state("Handler", handler.get()).nonNull().execute(req); |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public final HttpResponse execute(HttpRequest req) { |
| 56 | if (!matches(req)) { |
| 57 | return new HttpResponse() |
| 58 | .setStatus(HTTP_NOT_FOUND) |
| 59 | .setContent( |
| 60 | asJson( |
| 61 | Map.of( |
| 62 | "value", |
| 63 | Map.of( |
| 64 | "error", "unknown command", |
| 65 | "message", "Unable to find handler for " + req, |
| 66 | "stacktrace", "")))); |
| 67 | } |
| 68 | |
| 69 | HttpResponse res = handle(req); |
| 70 | |
| 71 | if (res != null) { |
| 72 | return res; |
| 73 | } |
| 74 | |
| 75 | return new HttpResponse() |
| 76 | .setStatus(HTTP_INTERNAL_ERROR) |
| 77 | .addHeader("WebDriver-Error", "unsupported operation") |
| 78 | .addHeader("Selenium-Route", "NULL_RES") |
| 79 | .setContent( |
| 80 | asJson( |
| 81 | Map.of( |
| 82 | "value", |
| 83 | Map.of( |
| 84 | "error", "unsupported operation", |
| 85 | "message", |
| 86 | String.format("Found handler for %s, but nothing was returned", req), |
| 87 | "stacktrace", "")))); |
| 88 | } |
| 89 | |
| 90 | protected abstract HttpResponse handle(HttpRequest req); |
| 91 | |
| 92 | public static PredicatedConfig matching(Predicate<HttpRequest> predicate) { |
| 93 | return new PredicatedConfig(Require.nonNull("Predicate", predicate)); |
| 94 | } |
| 95 | |
| 96 | public static TemplatizedRouteConfig delete(String template) { |
| 97 | UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL template", template)); |
| 98 | |
| 99 | return new TemplatizedRouteConfig( |
nothing calls this directly
no outgoing calls
no test coverage detected