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-remote-driver Show documentation
Show all versions of selenium-remote-driver 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.base.Preconditions.checkArgument;
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 com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
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;
public abstract class Route implements HttpHandler, Predicate {
public HttpHandler fallbackTo(Supplier handler) {
Objects.requireNonNull(handler, "Handler to use must be set.");
return req -> {
if (test(req)) {
return Route.this.apply(req);
}
return Objects.requireNonNull(handler.get(), "Handler to use must be set.").apply(req);
};
}
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(Route first, Route... 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