1616
1717package io .appium .java_client .remote ;
1818
19- import static com .google .common .base .Preconditions .checkNotNull ;
20- import static com .google .common .base .Throwables .getRootCause ;
2119import static com .google .common .base .Throwables .throwIfUnchecked ;
22- import static org .openqa .selenium .remote .DriverCommand .GET_ALL_SESSIONS ;
23- import static org .openqa .selenium .remote .DriverCommand .NEW_SESSION ;
24- import static org .openqa .selenium .remote .DriverCommand .QUIT ;
2520
26- import io .appium .java_client .AppiumCommandInfo ;
27- import org .openqa .selenium .NoSuchSessionException ;
28- import org .openqa .selenium .SessionNotCreatedException ;
29- import org .openqa .selenium .UnsupportedCommandException ;
21+ import com .google .common .base .Throwables ;
22+
3023import org .openqa .selenium .WebDriverException ;
3124import org .openqa .selenium .remote .Command ;
32- import org .openqa .selenium .remote .CommandCodec ;
33- import org .openqa .selenium .remote .CommandExecutor ;
34- import org .openqa .selenium .remote .Dialect ;
25+ import org .openqa .selenium .remote .CommandInfo ;
3526import org .openqa .selenium .remote .DriverCommand ;
36- import org .openqa .selenium .remote .HttpSessionId ;
27+ import org .openqa .selenium .remote .HttpCommandExecutor ;
3728import org .openqa .selenium .remote .Response ;
38- import org .openqa .selenium .remote .ResponseCodec ;
3929import org .openqa .selenium .remote .http .HttpClient ;
40- import org .openqa .selenium .remote .http .HttpRequest ;
41- import org .openqa .selenium .remote .http .HttpResponse ;
4230import org .openqa .selenium .remote .internal .ApacheHttpClient ;
4331import org .openqa .selenium .remote .service .DriverService ;
4432
4735import java .net .URL ;
4836import java .util .Map ;
4937
50- public class AppiumCommandExecutor implements CommandExecutor {
38+ public class AppiumCommandExecutor extends HttpCommandExecutor {
5139
52- private final URL remoteServer ;
53- private final HttpClient client ;
54- private final Map <String , AppiumCommandInfo > additionalCommands ;
55- private CommandCodec <HttpRequest > commandCodec ;
56- private ResponseCodec <HttpResponse > responseCodec ;
57- private DriverService service ;
40+ private final DriverService service ;
5841
59- /**
60- * Cretes an instance that sends requests and receives responses.
61- *
62- * @param additionalCommands is the mapped command repository
63- * @param addressOfRemoteServer is the url to connect to the Appium remote/local server
64- * @param httpClientFactory is the http client factory
65- */
66- public AppiumCommandExecutor (Map <String , AppiumCommandInfo > additionalCommands ,
67- URL addressOfRemoteServer , HttpClient .Factory httpClientFactory ) {
68- checkNotNull (addressOfRemoteServer );
69- remoteServer = addressOfRemoteServer ;
70- this .additionalCommands = additionalCommands ;
71- this .client = httpClientFactory .createClient (remoteServer );
42+ public AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands ,
43+ URL addressOfRemoteServer , HttpClient .Factory httpClientFactory ) {
44+ super (additionalCommands , addressOfRemoteServer , httpClientFactory );
45+ service = null ;
7246 }
7347
74- public AppiumCommandExecutor (Map <String , AppiumCommandInfo > additionalCommands , DriverService service ,
75- HttpClient .Factory httpClientFactory ) {
76- this (additionalCommands , service .getUrl (), httpClientFactory );
48+ public AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands , DriverService service ,
49+ HttpClient .Factory httpClientFactory ) {
50+ super (additionalCommands , service .getUrl (), httpClientFactory );
7751 this .service = service ;
7852 }
7953
80- public AppiumCommandExecutor (Map <String , AppiumCommandInfo > additionalCommands ,
81- URL addressOfRemoteServer ) {
54+ public AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands ,
55+ URL addressOfRemoteServer ) {
8256 this (additionalCommands , addressOfRemoteServer , new ApacheHttpClient .Factory ());
8357 }
8458
85- public AppiumCommandExecutor (Map <String , AppiumCommandInfo > additionalCommands ,
86- DriverService service ) {
59+ public AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands ,
60+ DriverService service ) {
8761 this (additionalCommands , service , new ApacheHttpClient .Factory ());
8862 }
8963
90- public URL getAddressOfRemoteServer () {
91- return remoteServer ;
92- }
93-
94- private Response doExecute (Command command ) throws IOException , WebDriverException {
95- if (command .getSessionId () == null ) {
96- if (QUIT .equals (command .getName ())) {
97- return new Response ();
98- }
99- if (!GET_ALL_SESSIONS .equals (command .getName ())
100- && !NEW_SESSION .equals (command .getName ())) {
101- throw new NoSuchSessionException (
102- "Session ID is null. Using WebDriver after calling quit()?" );
103- }
104- }
105-
106- if (NEW_SESSION .equals (command .getName ())) {
107- if (commandCodec != null ) {
108- throw new SessionNotCreatedException ("Session already exists" );
109- }
110- AppiumProtocolHandShake handshake = new AppiumProtocolHandShake ();
111- AppiumProtocolHandShake .Result result = handshake .createSession (client , command );
112- Dialect dialect = result .getDialect ();
113- commandCodec = dialect .getCommandCodec ();
114-
115- additionalCommands .forEach ((key , value ) -> {
116- checkNotNull (key );
117- checkNotNull (value );
118- commandCodec .defineCommand (key , value .getMethod (), value .getUrl ());
119- } );
120-
121- responseCodec = dialect .getResponseCodec ();
122- return result .createResponse ();
123- }
124-
125- if (commandCodec == null || responseCodec == null ) {
126- throw new WebDriverException (
127- "No command or response codec has been defined. Unable to proceed" );
128- }
129-
130- HttpRequest httpRequest = commandCodec .encode (command );
131- try {
132- HttpResponse httpResponse = client .execute (httpRequest , true );
133-
134- Response response = responseCodec .decode (httpResponse );
135- if (response .getSessionId () == null ) {
136- if (httpResponse .getTargetHost () != null ) {
137- response .setSessionId (HttpSessionId .getSessionId (httpResponse .getTargetHost ()));
138- } else {
139- response .setSessionId (command .getSessionId ().toString ());
140- }
141- }
142- if (QUIT .equals (command .getName ())) {
143- client .close ();
144- }
145- return response ;
146- } catch (UnsupportedCommandException e ) {
147- if (e .getMessage () == null || "" .equals (e .getMessage ())) {
148- throw new UnsupportedOperationException (
149- "No information from server. Command name was: " + command .getName (),
150- e .getCause ());
151- }
152- throw e ;
153- }
154- }
155-
15664 @ Override public Response execute (Command command ) throws IOException , WebDriverException {
15765 if (DriverCommand .NEW_SESSION .equals (command .getName ()) && service != null ) {
15866 service .start ();
15967 }
16068
16169 try {
162- return doExecute (command );
70+ return super . execute (command );
16371 } catch (Throwable t ) {
164- Throwable rootCause = getRootCause (t );
72+ Throwable rootCause = Throwables . getRootCause (t );
16573 if (rootCause instanceof ConnectException
166- && rootCause .getMessage ().contains ("Connection refused" )
167- && service != null ) {
74+ && rootCause .getMessage ().contains ("Connection refused" )
75+ && service != null ) {
16876 if (service .isRunning ()) {
16977 throw new WebDriverException ("The session is closed!" , t );
17078 }
@@ -182,4 +90,4 @@ private Response doExecute(Command command) throws IOException, WebDriverExcepti
18290 }
18391 }
18492
185- }
93+ }
0 commit comments