org.openqa.selenium.grid.sessionmap.SessionMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grid Show documentation
Show all versions of grid 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.grid.sessionmap;
import static org.openqa.selenium.grid.web.Routes.combine;
import static org.openqa.selenium.grid.web.Routes.delete;
import static org.openqa.selenium.grid.web.Routes.post;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.HandlerNotFoundException;
import org.openqa.selenium.grid.web.Routes;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import java.io.IOException;
import java.net.URI;
import java.util.Optional;
import java.util.function.Predicate;
/**
* Provides a stable API for looking up where on the Grid a particular webdriver instance is
* running.
*
* This class responds to the following URLs:
*
*
* Verb
* URL Template
* Meaning
*
*
* DELETE
* /se/grid/session/{sessionId}
* Removes a {@link URI} from the session map. Calling this method more than once for the same
* {@link SessionId} will not throw an error.
*
*
* GET
* /se/grid/session/{sessionId}
* Retrieves the {@link URI} associated the {@link SessionId}, or throws a
* {@link org.openqa.selenium.NoSuchSessionException} should the session not be present.
*
*
* POST
* /se/grid/session/{sessionId}
* Registers the session with session map. In theory, the session map never expires a session
* from its mappings, but realistically, sessions may end up being removed for many reasons.
*
*
*
*/
public abstract class SessionMap implements Predicate, CommandHandler {
private final Routes routes;
public abstract boolean add(Session session);
public abstract Session get(SessionId id) throws NoSuchSessionException;
public abstract void remove(SessionId id);
public SessionMap() {
Json json = new Json();
routes = combine(
post("/se/grid/session").using(() -> new AddToSessionMap(json, this)),
Routes.get("/se/grid/session/{sessionId}")
.using((params) -> new GetFromSessionMap(json, this, new SessionId(params.get("sessionId")))),
delete("/se/grid/session/{sessionId}")
.using((params) -> new RemoveFromSession(this, new SessionId(params.get("sessionId")))))
.build();
}
@Override
public boolean test(HttpRequest req) {
return routes.match(req).isPresent();
}
@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
Optional handler = routes.match(req);
if (!handler.isPresent()) {
throw new HandlerNotFoundException(req);
}
handler.get().execute(req, resp);
}
}