org.openqa.selenium.remote.http.Route Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-http Show documentation
Show all versions of selenium-http Show documentation
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
// 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 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.asJson;
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.OPTIONS;
import static org.openqa.selenium.remote.http.HttpMethod.POST;
import static org.openqa.selenium.remote.http.UrlPath.ROUTE_PREFIX_KEY;
import com.google.common.collect.ImmutableMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
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 org.openqa.selenium.internal.Require;
public abstract class Route implements HttpHandler, Routable {
public HttpHandler fallbackTo(Supplier handler) {
Require.nonNull("Handler", handler);
return req -> {
if (matches(req)) {
return Route.this.execute(req);
}
return Require.state("Handler", handler.get()).nonNull().execute(req);
};
}
@Override
public final HttpResponse execute(HttpRequest req) {
if (!matches(req)) {
return new HttpResponse()
.setStatus(HTTP_NOT_FOUND)
.setContent(
asJson(
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(
asJson(
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) {
return new PredicatedConfig(Require.nonNull("Predicate", predicate));
}
public static TemplatizedRouteConfig delete(String template) {
UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL template", template));
return new TemplatizedRouteConfig(
new MatchesHttpMethod(DELETE).and(new MatchesTemplate(urlTemplate)), urlTemplate);
}
public static TemplatizedRouteConfig get(String template) {
UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL template", template));
return new TemplatizedRouteConfig(
new MatchesHttpMethod(GET).and(new MatchesTemplate(urlTemplate)), urlTemplate);
}
public static TemplatizedRouteConfig post(String template) {
UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL template", template));
return new TemplatizedRouteConfig(
new MatchesHttpMethod(POST).and(new MatchesTemplate(urlTemplate)), urlTemplate);
}
public static TemplatizedRouteConfig options(String template) {
UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL template", template));
return new TemplatizedRouteConfig(
new MatchesHttpMethod(OPTIONS).and(new MatchesTemplate(urlTemplate)), urlTemplate);
}
public static NestedRouteConfig prefix(String prefix) {
Require.nonNull("Prefix", prefix);
Require.stateCondition(!prefix.isEmpty(), "Prefix to use must not be of 0 length");
return new NestedRouteConfig(prefix);
}
public static Route combine(Routable first, Routable... others) {
Require.nonNull("At least one route", first);
return new CombinedRoute(Stream.concat(Stream.of(first), Stream.of(others)));
}
public static Route combine(Iterable routes) {
Require.nonNull("At least one route", routes);
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 = Require.nonNull("Predicate", predicate);
this.template = Require.nonNull("URL template", template);
}
public Route to(Supplier handler) {
Require.nonNull("Handler supplier", handler);
return to(params -> handler.get());
}
public Route to(Function