...28import org.openqa.selenium.remote.server.commandhandler.BeginSession;29import org.openqa.selenium.remote.server.commandhandler.GetAllSessions;30import org.openqa.selenium.remote.server.commandhandler.GetLogTypes;31import org.openqa.selenium.remote.server.commandhandler.GetLogsOfType;32import org.openqa.selenium.grid.web.NoHandler;33import org.openqa.selenium.remote.server.commandhandler.NoSessionHandler;34import org.openqa.selenium.remote.server.commandhandler.Status;35import org.openqa.selenium.remote.server.commandhandler.UploadFile;36import java.util.List;37import java.util.Map;38import java.util.Objects;39import java.util.Optional;40import java.util.function.Function;41import javax.servlet.http.HttpServletRequest;42class AllHandlers {43 private final Json json;44 private final ActiveSessions allSessions;45 private final Map<HttpMethod, ImmutableList<Function<String, CommandHandler>>> additionalHandlers;46 public AllHandlers(NewSessionPipeline pipeline, ActiveSessions allSessions) {47 this.allSessions = Objects.requireNonNull(allSessions);48 this.json = new Json();49 this.additionalHandlers = ImmutableMap.of(50 HttpMethod.DELETE, ImmutableList.of(),51 HttpMethod.GET, ImmutableList.of(52 handler("/session/{sessionId}/log/types",53 params -> new GetLogTypes(json, allSessions.get(new SessionId(params.get("sessionId"))))),54 handler("/sessions", params -> new GetAllSessions(allSessions, json)),55 handler("/status", params -> new Status(json))56 ),57 HttpMethod.POST, ImmutableList.of(58 handler("/session", params -> new BeginSession(pipeline, allSessions, json)),59 handler("/session/{sessionId}/file",60 params -> new UploadFile(json, allSessions.get(new SessionId(params.get("sessionId"))))),61 handler("/session/{sessionId}/log",62 params -> new GetLogsOfType(json, allSessions.get(new SessionId(params.get("sessionId"))))),63 handler("/session/{sessionId}/se/file",64 params -> new UploadFile(json, allSessions.get(new SessionId(params.get("sessionId")))))65 ));66 }67 public CommandHandler match(HttpServletRequest req) {68 String path = Strings.isNullOrEmpty(req.getPathInfo()) ? "/" : req.getPathInfo();69 Optional<? extends CommandHandler> additionalHandler = additionalHandlers.get(HttpMethod.valueOf(req.getMethod()))70 .stream()71 .map(bundle -> bundle.apply(req.getPathInfo()))72 .filter(Objects::nonNull)73 .findFirst();74 if (additionalHandler.isPresent()) {75 return additionalHandler.get();76 }77 // All commands that take a session id expect that as the path fragment immediately after "/session".78 SessionId id = null;79 List<String> fragments = Splitter.on('/').limit(4).splitToList(path);80 if (fragments.size() > 2) {81 if ("session".equals(fragments.get(1))) {82 id = new SessionId(fragments.get(2));83 }84 }85 if (id != null) {86 ActiveSession session = allSessions.get(id);87 if (session == null) {88 return new NoSessionHandler(json, id);89 }90 return session;91 }92 return new NoHandler(json);93 }94 private <H extends CommandHandler> Function<String, CommandHandler> handler(95 String template,96 Function<Map<String, String>, H> handlerGenerator) {97 UrlTemplate urlTemplate = new UrlTemplate(template);98 return path -> {99 UrlTemplate.Match match = urlTemplate.match(path);100 if (match == null) {101 return null;102 }103 return handlerGenerator.apply(match.getParameters());104 };105 }106}...