18
18
19
19
import static com .google .common .base .Preconditions .checkNotNull ;
20
20
import static com .google .common .base .Throwables .throwIfUnchecked ;
21
- import static java .lang .String .format ;
22
- import static java .nio .charset .StandardCharsets .UTF_8 ;
23
21
import static java .util .Optional .ofNullable ;
24
- import static java .util .logging .Logger .getLogger ;
25
22
import static org .openqa .selenium .remote .DriverCommand .NEW_SESSION ;
26
23
27
24
import com .google .common .base .Supplier ;
28
25
import com .google .common .base .Throwables ;
29
26
30
- import com .google .common .io .CountingOutputStream ;
31
- import com .google .common .io .FileBackedOutputStream ;
32
-
33
- import io .appium .java_client .internal .Config ;
34
- import org .openqa .selenium .Capabilities ;
35
- import org .openqa .selenium .ImmutableCapabilities ;
36
27
import org .openqa .selenium .SessionNotCreatedException ;
37
28
import org .openqa .selenium .WebDriverException ;
38
29
import org .openqa .selenium .remote .Command ;
45
36
import org .openqa .selenium .remote .Response ;
46
37
import org .openqa .selenium .remote .ResponseCodec ;
47
38
import org .openqa .selenium .remote .codec .w3c .W3CHttpCommandCodec ;
48
- import org .openqa .selenium .remote .http .Filter ;
49
39
import org .openqa .selenium .remote .http .HttpClient ;
50
- import org .openqa .selenium .remote .http .HttpHandler ;
51
40
import org .openqa .selenium .remote .http .HttpRequest ;
52
41
import org .openqa .selenium .remote .http .HttpResponse ;
53
- import org .openqa .selenium .remote .http .WebSocket ;
54
- import org .openqa .selenium .remote .http .WebSocket .Listener ;
55
42
import org .openqa .selenium .remote .service .DriverService ;
56
43
57
- import java .io .BufferedInputStream ;
58
44
import java .io .IOException ;
59
- import java .io .InputStream ;
60
- import java .io .OutputStreamWriter ;
61
- import java .io .UncheckedIOException ;
62
- import java .io .Writer ;
63
45
import java .lang .reflect .Field ;
64
- import java .lang .reflect .InvocationTargetException ;
65
- import java .lang .reflect .Method ;
66
46
import java .net .ConnectException ;
67
47
import java .net .URL ;
68
48
import java .util .Map ;
@@ -95,7 +75,6 @@ public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
95
75
this (additionalCommands , null , checkNotNull (addressOfRemoteServer ), httpClientFactory );
96
76
}
97
77
98
-
99
78
public AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands ,
100
79
URL addressOfRemoteServer ) {
101
80
this (additionalCommands , addressOfRemoteServer , HttpClient .Factory .createDefault ());
@@ -165,65 +144,13 @@ private Response createSession(Command command) throws IOException {
165
144
if (getCommandCodec () != null ) {
166
145
throw new SessionNotCreatedException ("Session already exists" );
167
146
}
168
- ProtocolHandshake handshake = new ProtocolHandshake () {
169
- @ SuppressWarnings ("unchecked" )
170
- public Result createSession (HttpClient client , Command command ) throws IOException {
171
- Capabilities desiredCapabilities = (Capabilities ) command .getParameters ().get ("desiredCapabilities" );
172
- Capabilities desired = desiredCapabilities == null ? new ImmutableCapabilities () : desiredCapabilities ;
173
-
174
- //the number of bytes before the stream should switch to buffering to a file
175
- int threshold = (int ) Math .min (Runtime .getRuntime ().freeMemory () / 10 , Integer .MAX_VALUE );
176
- FileBackedOutputStream os = new FileBackedOutputStream (threshold );
177
- try {
178
-
179
- CountingOutputStream counter = new CountingOutputStream (os );
180
- Writer writer = new OutputStreamWriter (counter , UTF_8 );
181
- NewAppiumSessionPayload payload = NewAppiumSessionPayload .create (desired );
182
- payload .writeTo (writer );
183
-
184
- try (InputStream rawIn = os .asByteSource ().openBufferedStream ();
185
- BufferedInputStream contentStream = new BufferedInputStream (rawIn )) {
186
-
187
- Method createSessionMethod = this .getClass ().getSuperclass ()
188
- .getDeclaredMethod ("createSession" , HttpClient .class , InputStream .class , long .class );
189
- createSessionMethod .setAccessible (true );
190
147
191
- Optional <Result > result = (Optional <Result >) createSessionMethod .invoke (this ,
192
- client .with (httpHandler -> req -> {
193
- req .setHeader (IDEMPOTENCY_KEY_HEADER , UUID .randomUUID ().toString ().toLowerCase ());
194
- return httpHandler .execute (req );
195
- }), contentStream , counter .getCount ());
196
-
197
- return result .map (result1 -> {
198
- Result toReturn = result .get ();
199
- getLogger (ProtocolHandshake .class .getName ())
200
- .info (format ("Detected dialect: %s" , toReturn .getDialect ()));
201
- return toReturn ;
202
- }).orElseThrow (() -> new SessionNotCreatedException (
203
- format ("Unable to create a new remote session. Desired capabilities = %s" , desired )));
204
- } catch (NoSuchMethodException | IllegalAccessException e ) {
205
- throw new SessionNotCreatedException (format ("Unable to create a new remote session. "
206
- + "Make sure your project dependencies config does not override "
207
- + "Selenium API version %s used by java-client library." ,
208
- Config .main ().getValue ("selenium.version" , String .class )), e );
209
- } catch (InvocationTargetException e ) {
210
- String message = "Unable to create a new remote session." ;
211
- if (e .getCause () != null ) {
212
- if (e .getCause () instanceof WebDriverException ) {
213
- message += " Please check the server log for more details." ;
214
- }
215
- message += format (" Original error: %s" , e .getCause ().getMessage ());
216
- }
217
- throw new SessionNotCreatedException (message , e );
218
- }
219
- } finally {
220
- os .reset ();
221
- }
222
- }
223
- };
224
-
225
- ProtocolHandshake .Result result = handshake
226
- .createSession (getClient (), command );
148
+ ProtocolHandshake .Result result = new ProtocolHandshake ().createSession (
149
+ getClient ().with ((httpHandler ) -> (req ) -> {
150
+ req .setHeader (IDEMPOTENCY_KEY_HEADER , UUID .randomUUID ().toString ().toLowerCase ());
151
+ return httpHandler .execute (req );
152
+ }), command
153
+ );
227
154
Dialect dialect = result .getDialect ();
228
155
setCommandCodec (dialect .getCommandCodec ());
229
156
getAdditionalCommands ().forEach (this ::defineCommand );
@@ -241,9 +168,9 @@ public Response execute(Command command) throws WebDriverException {
241
168
throw new WebDriverException (e .getMessage (), e );
242
169
}
243
170
});
244
- }
245
- if ( getAdditionalCommands ().containsKey (command .getName ())) {
246
- super . defineCommand ( command . getName (), getAdditionalCommands (). get ( command . getName ()));
171
+ if ( getAdditionalCommands (). containsKey ( command . getName ())) {
172
+ super . defineCommand ( command . getName (), getAdditionalCommands ().get (command .getName ()));
173
+ }
247
174
}
248
175
249
176
Response response ;
0 commit comments