Source:
private Response doExecute(Command command) throws IOException, WebDriverException { 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"); } AppiumProtocolHandShake handshake = new AppiumProtocolHandShake(); AppiumProtocolHandShake.Result result = handshake.createSession(client, command); Dialect dialect = result.getDialect(); commandCodec = dialect.getCommandCodec(); for (Map.Entry<String, AppiumCommandInfo> entry : additionalCommands.entrySet()) { String key = entry.getKey(); AppiumCommandInfo value = entry.getValue(); checkNotNull(key); checkNotNull(entry.getValue()); commandCodec.defineCommand(key, value.getMethod(), value.getUrl()); } responseCodec = dialect.getResponseCodec(); 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 { HttpResponse httpResponse = client.execute(httpRequest, true); Response response = responseCodec.decode(httpResponse); if (response.getSessionId() == null) { if (httpResponse.getTargetHost() != null) { response.setSessionId(HttpSessionId.getSessionId(httpResponse.getTargetHost())); } else { 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; } }

Target:
private Response doExecute(Command command) throws IOException, WebDriverException { 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"); } AppiumProtocolHandShake handshake = new AppiumProtocolHandShake(); AppiumProtocolHandShake.Result result = handshake.createSession(client, command); Dialect dialect = result.getDialect(); commandCodec = dialect.getCommandCodec(); additionalCommands.forEach((key, value) -> { checkNotNull(key); checkNotNull(value); commandCodec.defineCommand(key, value.getMethod(), value.getUrl()); } ); responseCodec = dialect.getResponseCodec(); 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 { HttpResponse httpResponse = client.execute(httpRequest, true); Response response = responseCodec.decode(httpResponse); if (response.getSessionId() == null) { if (httpResponse.getTargetHost() != null) { response.setSessionId(HttpSessionId.getSessionId(httpResponse.getTargetHost())); } else { 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; } }