|
16 | 16 |
|
17 | 17 | package io.appium.java_client.remote; |
18 | 18 |
|
19 | | -import org.openqa.selenium.Capabilities; |
20 | | -import org.openqa.selenium.ImmutableCapabilities; |
21 | | -import org.openqa.selenium.SessionNotCreatedException; |
22 | | -import org.openqa.selenium.WebDriverException; |
23 | | -import org.openqa.selenium.internal.Either; |
24 | | -import org.openqa.selenium.json.Json; |
25 | | -import org.openqa.selenium.json.JsonOutput; |
26 | | -import org.openqa.selenium.remote.Command; |
27 | | -import org.openqa.selenium.remote.NewSessionPayload; |
28 | 19 | import org.openqa.selenium.remote.ProtocolHandshake; |
29 | | -import org.openqa.selenium.remote.http.Contents; |
30 | | -import org.openqa.selenium.remote.http.HttpHandler; |
31 | 20 |
|
32 | | -import java.io.IOException; |
33 | | -import java.io.StringWriter; |
34 | | -import java.lang.reflect.InvocationTargetException; |
35 | | -import java.lang.reflect.Method; |
36 | | -import java.util.Map; |
37 | | -import java.util.Set; |
38 | | -import java.util.stream.Stream; |
39 | | - |
40 | | -@SuppressWarnings("UnstableApiUsage") |
| 21 | +/** |
| 22 | + * This class is deprecated and should be removed. |
| 23 | + * |
| 24 | + * @deprecated Use ProtocolHandshake instead. |
| 25 | + */ |
| 26 | +@Deprecated |
41 | 27 | public class AppiumProtocolHandshake extends ProtocolHandshake { |
42 | | - private static void writeJsonPayload(NewSessionPayload srcPayload, Appendable destination) { |
43 | | - try (JsonOutput json = new Json().newOutput(destination)) { |
44 | | - json.beginObject(); |
45 | | - |
46 | | - json.name("capabilities"); |
47 | | - json.beginObject(); |
48 | | - |
49 | | - json.name("firstMatch"); |
50 | | - json.beginArray(); |
51 | | - json.beginObject(); |
52 | | - json.endObject(); |
53 | | - json.endArray(); |
54 | | - |
55 | | - json.name("alwaysMatch"); |
56 | | - try { |
57 | | - Method getW3CMethod = NewSessionPayload.class.getDeclaredMethod("getW3C"); |
58 | | - getW3CMethod.setAccessible(true); |
59 | | - //noinspection unchecked,resource |
60 | | - ((Stream<Map<String, Object>>) getW3CMethod.invoke(srcPayload)) |
61 | | - .findFirst() |
62 | | - .map(json::write) |
63 | | - .orElseGet(() -> { |
64 | | - json.beginObject(); |
65 | | - json.endObject(); |
66 | | - return null; |
67 | | - }); |
68 | | - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
69 | | - throw new WebDriverException(e); |
70 | | - } |
71 | | - |
72 | | - json.endObject(); // Close "capabilities" object |
73 | | - |
74 | | - try { |
75 | | - Method writeMetaDataMethod = NewSessionPayload.class.getDeclaredMethod( |
76 | | - "writeMetaData", JsonOutput.class); |
77 | | - writeMetaDataMethod.setAccessible(true); |
78 | | - writeMetaDataMethod.invoke(srcPayload, json); |
79 | | - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
80 | | - throw new WebDriverException(e); |
81 | | - } |
82 | | - |
83 | | - json.endObject(); |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - @Override |
88 | | - public Result createSession(HttpHandler client, Command command) throws IOException { |
89 | | - //noinspection unchecked |
90 | | - Capabilities desired = ((Set<Map<String, Object>>) command.getParameters().get("capabilities")) |
91 | | - .stream() |
92 | | - .findAny() |
93 | | - .map(ImmutableCapabilities::new) |
94 | | - .orElseGet(ImmutableCapabilities::new); |
95 | | - try (NewSessionPayload payload = NewSessionPayload.create(desired)) { |
96 | | - Either<SessionNotCreatedException, Result> result = createSession(client, payload); |
97 | | - if (result.isRight()) { |
98 | | - return result.right(); |
99 | | - } |
100 | | - throw result.left(); |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - @Override |
105 | | - public Either<SessionNotCreatedException, Result> createSession(HttpHandler client, NewSessionPayload payload) { |
106 | | - |
107 | | - StringWriter stringWriter = new StringWriter(); |
108 | | - writeJsonPayload(payload, stringWriter); |
109 | | - |
110 | | - try { |
111 | | - Method createSessionMethod = ProtocolHandshake.class.getDeclaredMethod( |
112 | | - "createSession", HttpHandler.class, Contents.Supplier.class |
113 | | - ); |
114 | | - createSessionMethod.setAccessible(true); |
115 | | - //noinspection unchecked |
116 | | - return (Either<SessionNotCreatedException, Result>) createSessionMethod.invoke( |
117 | | - this, client, Contents.utf8String(stringWriter.toString()) |
118 | | - ); |
119 | | - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
120 | | - throw new WebDriverException(e); |
121 | | - } |
122 | | - } |
123 | 28 | } |
0 commit comments