org.openqa.selenium.remote.http.Route Maven / Gradle / Ivy
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.remote.http;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.json.Json;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static org.openqa.selenium.remote.http.Contents.utf8String;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
import static org.openqa.selenium.remote.http.HttpMethod.POST;
import static org.openqa.selenium.remote.http.UrlPath.ROUTE_PREFIX_KEY;
public abstract class Route implements HttpHandler, Routable {
private static final Json JSON = new Json();
public HttpHandler fallbackTo(Supplier handler) {
Objects.requireNonNull(handler, "Handler to use must be set.");
return req -> {
if (matches(req)) {
return Route.this.execute(req);
}
return Objects.requireNonNull(handler.get(), "Handler to use must be set.").execute(req);
};
}
@Override
public final HttpResponse execute(HttpRequest req) {
if (!matches(req)) {
return new HttpResponse()
.setStatus(HTTP_NOT_FOUND)
.setContent(utf8String(JSON.toJson(ImmutableMap.of(
"value", ImmutableMap.of(
"error", "unknown command",
"message", "Unable to find handler for " + req,
"stacktrace", "")))));
}
HttpResponse res = handle(req);
if (res != null) {
return res;
}
return new HttpResponse()
.setStatus(HTTP_INTERNAL_ERROR)
.addHeader("WebDriver-Error", "unsupported operation")
.addHeader("Selenium-Route", "NULL_RES")
.setContent(utf8String(JSON.toJson(ImmutableMap.of(
"value", ImmutableMap.of(
"error", "unsupported operation",
"message", String.format("Found handler for %s, but nothing was returned", req),
"stacktrace", "")))));
}
protected abstract HttpResponse handle(HttpRequest req);
public static PredicatedConfig matching(Predicate predicate) {
Objects.requireNonNull(predicate, "Predicate to use must be set.");
return new PredicatedConfig(predicate);
}
public static TemplatizedRouteConfig delete(String template) {
Objects.requireNonNull(template, "URL template to use must be set.");
UrlTemplate urlTemplate = new UrlTemplate(template);
return new TemplatizedRouteConfig(
new MatchesHttpMethod(DELETE).and(new MatchesTemplate(urlTemplate)),
urlTemplate);
}
public static TemplatizedRouteConfig get(String template) {
Objects.requireNonNull(template, "URL template to use must be set.");
UrlTemplate urlTemplate = new UrlTemplate(template);
return new TemplatizedRouteConfig(
new MatchesHttpMethod(GET).and(new MatchesTemplate(urlTemplate)),
urlTemplate);
}
public static TemplatizedRouteConfig post(String template) {
Objects.requireNonNull(template, "URL template to use must be set.");
UrlTemplate urlTemplate = new UrlTemplate(template);
return new TemplatizedRouteConfig(
new MatchesHttpMethod(POST).and(new MatchesTemplate(urlTemplate)),
urlTemplate);
}
public static NestedRouteConfig prefix(String prefix) {
Objects.requireNonNull(prefix, "Prefix to use must be set.");
checkArgument(!prefix.isEmpty(), "Prefix to use must not be of 0 length");
return new NestedRouteConfig(prefix);
}
public static Route combine(Routable first, Routable... others) {
Objects.requireNonNull(first, "At least one route must be set.");
return new CombinedRoute(Stream.concat(Stream.of(first), Stream.of(others)));
}
public static Route combine(Iterable routes) {
Objects.requireNonNull(routes, "At least one route must be set.");
return new CombinedRoute(StreamSupport.stream(routes.spliterator(), false));
}
public static class TemplatizedRouteConfig {
private final Predicate predicate;
private final UrlTemplate template;
private TemplatizedRouteConfig(Predicate predicate, UrlTemplate template) {
this.predicate = Objects.requireNonNull(predicate);
this.template = Objects.requireNonNull(template);
}
public Route to(Supplier handler) {
Objects.requireNonNull(handler, "Handler supplier must be set.");
return to(params -> handler.get());
}
public Route to(Function © 2015 - 2025 Weber Informatics LLC | Privacy Policy