org.openqa.selenium.remote.ProtocolHandshake Java Examples

The following examples show how to use org.openqa.selenium.remote.ProtocolHandshake. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomHttpCommandExecutor.java    From hifive-pitalium with Apache License 2.0 4 votes vote down vote up
public Response execute(Command command) throws IOException {
	if (command.getSessionId() == null) {
		if (QUIT.equals(command.getName())) {
			return new Response();
		}
		if (!GET_ALL_SESSIONS.equals(command.getName()) && !NEW_SESSION.equals(command.getName())) {
			throw new NoSuchSessionException("Session ID is null. Using WebDriver after calling quit()?");
		}
	}

	if (NEW_SESSION.equals(command.getName())) {
		if (commandCodec != null) {
			throw new SessionNotCreatedException("Session already exists");
		}
		ProtocolHandshake handshake = new ProtocolHandshake();
		log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), true));
		ProtocolHandshake.Result result = handshake.createSession(client, command);
		dialect = result.getDialect();
		commandCodec = dialect.getCommandCodec();
		for (Map.Entry<String, CommandInfo> entry : additionalCommands.entrySet()) {
			defineCommand(entry.getKey(), entry.getValue());
		}
		responseCodec = dialect.getResponseCodec();
		log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), false));
		return result.createResponse();
	}

	if (commandCodec == null || responseCodec == null) {
		throw new WebDriverException("No command or response codec has been defined. Unable to proceed");
	}

	HttpRequest httpRequest = commandCodec.encode(command);
	try {
		log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), true));
		HttpResponse httpResponse = client.execute(httpRequest, true);
		log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), false));

		Response response = responseCodec.decode(httpResponse);
		if (response.getSessionId() == null) {
			if (httpResponse.getTargetHost() != null) {
				response.setSessionId(HttpSessionId.getSessionId(httpResponse.getTargetHost()));
			} else {
				// Spam in the session id from the request
				response.setSessionId(command.getSessionId().toString());
			}
		}
		if (QUIT.equals(command.getName())) {
			client.close();
		}
		return response;
	} catch (UnsupportedCommandException e) {
		if (e.getMessage() == null || "".equals(e.getMessage())) {
			throw new UnsupportedOperationException(
					"No information from server. Command name was: " + command.getName(), e.getCause());
		}
		throw e;
	}
}
 
Example #2
Source File: RemoteSession.java    From selenium with Apache License 2.0 4 votes vote down vote up
protected Optional<ActiveSession> performHandshake(
    Tracer tracer,
    X additionalData,
    URL url,
    Set<Dialect> downstreamDialects,
    Capabilities capabilities) {
  try {
    HttpClient client = HttpClient.Factory.createDefault().createClient(url);

    Command command = new Command(
        null,
        DriverCommand.NEW_SESSION(capabilities));

    ProtocolHandshake.Result result = new ProtocolHandshake().createSession(client, command);

    HttpHandler codec;
    Dialect upstream = result.getDialect();
    Dialect downstream;
    if (downstreamDialects.contains(result.getDialect())) {
      codec = new ReverseProxyHandler(tracer, client);
      downstream = upstream;
    } else {
      downstream = downstreamDialects.isEmpty() ? OSS : downstreamDialects.iterator().next();
      codec = new ProtocolConverter(tracer, client, downstream, upstream);
    }

    Response response = result.createResponse();
    //noinspection unchecked
    Optional<ActiveSession> activeSession = Optional.of(newActiveSession(
        additionalData,
        downstream,
        upstream,
        codec,
        new SessionId(response.getSessionId()),
        (Map<String, Object>) response.getValue()));
    activeSession.ifPresent(session -> log.info("Started new session " + session));
    return activeSession;
  } catch (IOException | IllegalStateException | NullPointerException e) {
    log.log(Level.WARNING, e.getMessage(), e);
    return Optional.empty();
  }
}