Skip to content

Commit b1f8bc9

Browse files
fix: Deprecate AppiumProtocolHandshake class (#2173)
1 parent 56dc411 commit b1f8bc9

File tree

4 files changed

+35
-121
lines changed

4 files changed

+35
-121
lines changed

src/main/java/io/appium/java_client/AppiumDriver.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.appium.java_client.internal.ReflectionHelpers;
2121
import io.appium.java_client.internal.SessionHelpers;
2222
import io.appium.java_client.remote.AppiumCommandExecutor;
23-
import io.appium.java_client.remote.AppiumNewSessionCommandPayload;
2423
import io.appium.java_client.remote.AppiumW3CHttpCommandCodec;
2524
import io.appium.java_client.remote.options.BaseOptions;
2625
import io.appium.java_client.service.local.AppiumDriverLocalService;
@@ -50,11 +49,13 @@
5049
import java.util.Collections;
5150
import java.util.HashSet;
5251
import java.util.Map;
52+
import java.util.Optional;
5353
import java.util.Set;
5454

5555
import static com.google.common.base.Strings.isNullOrEmpty;
5656
import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX;
5757
import static io.appium.java_client.remote.options.SupportsAutomationNameOption.AUTOMATION_NAME_OPTION;
58+
import static java.util.Collections.singleton;
5859
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
5960

6061
/**
@@ -265,25 +266,27 @@ public void addCommand(HttpMethod httpMethod, String url, String methodName) {
265266

266267
@Override
267268
protected void startSession(Capabilities capabilities) {
268-
Response response = execute(new AppiumNewSessionCommandPayload(capabilities));
269-
if (response == null) {
270-
throw new SessionNotCreatedException(
271-
"The underlying command executor returned a null response.");
272-
}
273-
274-
Object responseValue = response.getValue();
275-
if (responseValue == null) {
276-
throw new SessionNotCreatedException(
277-
"The underlying command executor returned a response without payload: "
278-
+ response);
279-
}
280-
if (!(responseValue instanceof Map)) {
281-
throw new SessionNotCreatedException(
282-
"The underlying command executor returned a response with a non well formed payload: "
283-
+ response);
284-
}
269+
var response = Optional.ofNullable(
270+
execute(DriverCommand.NEW_SESSION(singleton(capabilities)))
271+
).orElseThrow(() -> new SessionNotCreatedException(
272+
"The underlying command executor returned a null response."
273+
));
274+
275+
var rawCapabilities = Optional.ofNullable(response.getValue())
276+
.map(value -> {
277+
if (!(value instanceof Map)) {
278+
throw new SessionNotCreatedException(String.format(
279+
"The underlying command executor returned a response "
280+
+ "with a non well formed payload: %s", response)
281+
);
282+
}
283+
//noinspection unchecked
284+
return (Map<String, Object>) value;
285+
})
286+
.orElseThrow(() -> new SessionNotCreatedException(
287+
"The underlying command executor returned a response without payload: " + response)
288+
);
285289

286-
@SuppressWarnings("unchecked") Map<String, Object> rawCapabilities = (Map<String, Object>) responseValue;
287290
// TODO: remove this workaround for Selenium API enforcing some legacy capability values in major version
288291
rawCapabilities.remove("platform");
289292
if (rawCapabilities.containsKey(CapabilityType.BROWSER_NAME)

src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private Response createSession(Command command) throws IOException {
173173
throw new SessionNotCreatedException("Session already exists");
174174
}
175175

176-
ProtocolHandshake.Result result = new AppiumProtocolHandshake().createSession(getClient(), command);
176+
var result = new ProtocolHandshake().createSession(getClient(), command);
177177
Dialect dialect = result.getDialect();
178178
if (!(dialect.getCommandCodec() instanceof W3CHttpCommandCodec)) {
179179
throw new SessionNotCreatedException("Only W3C sessions are supported. "

src/main/java/io/appium/java_client/remote/AppiumNewSessionCommandPayload.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727

2828
import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION;
2929

30+
/**
31+
* This class is deprecated and will be removed.
32+
*
33+
* @deprecated Use CommandPayload instead.
34+
*/
35+
@Deprecated
3036
public class AppiumNewSessionCommandPayload extends CommandPayload {
3137
/**
3238
* Appends "appium:" prefix to all non-prefixed non-standard capabilities.

src/main/java/io/appium/java_client/remote/AppiumProtocolHandshake.java

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -16,108 +16,13 @@
1616

1717
package io.appium.java_client.remote;
1818

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;
2819
import org.openqa.selenium.remote.ProtocolHandshake;
29-
import org.openqa.selenium.remote.http.Contents;
30-
import org.openqa.selenium.remote.http.HttpHandler;
3120

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
4127
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-
}
12328
}

0 commit comments

Comments
 (0)