-
-
Notifications
You must be signed in to change notification settings - Fork 768
refactor: Make sure we only write W3C payload into create session command #1537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bd9e076
refactor: Make sure we only write W3C payload into create session ocm…
mykola-mokhnach 0445b43
Add override
mykola-mokhnach 5cda73f
Fix getting the method
mykola-mokhnach f612e4b
Patch capability names if needed
mykola-mokhnach 4090d0c
Fix checkstyle
mykola-mokhnach 1573425
Fix linting
mykola-mokhnach 9aeb72d
Write into alwaysMatch
mykola-mokhnach b68d728
Handle W3C properly
mykola-mokhnach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/io/appium/java_client/remote/AppiumNewSessionCommandPayload.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.appium.java_client.remote; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import org.openqa.selenium.Capabilities; | ||
import org.openqa.selenium.internal.Require; | ||
import org.openqa.selenium.remote.AcceptedW3CCapabilityKeys; | ||
import org.openqa.selenium.remote.CommandPayload; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.Map; | ||
|
||
import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX; | ||
import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION; | ||
|
||
public class AppiumNewSessionCommandPayload extends CommandPayload { | ||
private static final AcceptedW3CCapabilityKeys ACCEPTED_W3C_PATTERNS = new AcceptedW3CCapabilityKeys(); | ||
|
||
/** | ||
* Appends "appium:" prefix to all non-prefixed non-standard capabilities. | ||
* | ||
* @param possiblyInvalidCapabilities user-provided capabilities mapping. | ||
* @return Fixed capabilities mapping. | ||
*/ | ||
private static Map<String, Object> makeW3CSafe(Capabilities possiblyInvalidCapabilities) { | ||
Require.nonNull("Capabilities", possiblyInvalidCapabilities); | ||
|
||
return possiblyInvalidCapabilities.asMap().entrySet().stream() | ||
.map((entry) -> ACCEPTED_W3C_PATTERNS.test(entry.getKey()) | ||
? entry | ||
: new AbstractMap.SimpleEntry<>( | ||
String.format("%s%s", APPIUM_PREFIX, entry.getKey()), entry.getValue())) | ||
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
/** | ||
* Overrides the default new session behavior to | ||
* only handle W3C capabilities. | ||
* | ||
* @param capabilities User-provided capabilities. | ||
*/ | ||
public AppiumNewSessionCommandPayload(Capabilities capabilities) { | ||
super(NEW_SESSION, ImmutableMap.of( | ||
"capabilities", ImmutableSet.of(makeW3CSafe(capabilities)), | ||
"desiredCapabilities", capabilities | ||
)); | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
src/main/java/io/appium/java_client/remote/AppiumProtocolHandshake.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.appium.java_client.remote; | ||
|
||
import com.google.common.io.CountingOutputStream; | ||
import com.google.common.io.FileBackedOutputStream; | ||
import org.openqa.selenium.Capabilities; | ||
import org.openqa.selenium.ImmutableCapabilities; | ||
import org.openqa.selenium.SessionNotCreatedException; | ||
import org.openqa.selenium.WebDriverException; | ||
import org.openqa.selenium.internal.Either; | ||
import org.openqa.selenium.json.Json; | ||
import org.openqa.selenium.json.JsonOutput; | ||
import org.openqa.selenium.remote.Command; | ||
import org.openqa.selenium.remote.NewSessionPayload; | ||
import org.openqa.selenium.remote.ProtocolHandshake; | ||
import org.openqa.selenium.remote.http.HttpHandler; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
public class AppiumProtocolHandshake extends ProtocolHandshake { | ||
private static void writeJsonPayload(NewSessionPayload srcPayload, Appendable destination) { | ||
try (JsonOutput json = new Json().newOutput(destination)) { | ||
json.beginObject(); | ||
|
||
json.name("capabilities"); | ||
json.beginObject(); | ||
|
||
json.name("firstMatch"); | ||
json.beginArray(); | ||
json.beginObject(); | ||
json.endObject(); | ||
json.endArray(); | ||
|
||
json.name("alwaysMatch"); | ||
try { | ||
Method getW3CMethod = NewSessionPayload.class.getDeclaredMethod("getW3C"); | ||
getW3CMethod.setAccessible(true); | ||
//noinspection unchecked | ||
((Stream<Map<String, Object>>) getW3CMethod.invoke(srcPayload)) | ||
.findFirst() | ||
.map(json::write) | ||
.orElseGet(() -> { | ||
json.beginObject(); | ||
json.endObject(); | ||
return null; | ||
}); | ||
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { | ||
throw new WebDriverException(e); | ||
} | ||
|
||
json.endObject(); // Close "capabilities" object | ||
|
||
try { | ||
Method writeMetaDataMethod = NewSessionPayload.class.getDeclaredMethod( | ||
"writeMetaData", JsonOutput.class); | ||
writeMetaDataMethod.setAccessible(true); | ||
writeMetaDataMethod.invoke(srcPayload, json); | ||
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { | ||
throw new WebDriverException(e); | ||
} | ||
|
||
json.endObject(); | ||
} | ||
} | ||
|
||
@Override | ||
public Result createSession(HttpHandler client, Command command) throws IOException { | ||
//noinspection unchecked | ||
Capabilities desired = ((Set<Map<String, Object>>) command.getParameters().get("capabilities")) | ||
.stream() | ||
.findAny() | ||
.map(ImmutableCapabilities::new) | ||
.orElseGet(ImmutableCapabilities::new); | ||
try (NewSessionPayload payload = NewSessionPayload.create(desired)) { | ||
Either<SessionNotCreatedException, Result> result = createSession(client, payload); | ||
if (result.isRight()) { | ||
return result.right(); | ||
} | ||
throw result.left(); | ||
} | ||
} | ||
|
||
@Override | ||
public Either<SessionNotCreatedException, Result> createSession( | ||
HttpHandler client, NewSessionPayload payload) throws IOException { | ||
int threshold = (int) Math.min(Runtime.getRuntime().freeMemory() / 10, Integer.MAX_VALUE); | ||
FileBackedOutputStream os = new FileBackedOutputStream(threshold); | ||
|
||
try (CountingOutputStream counter = new CountingOutputStream(os); | ||
Writer writer = new OutputStreamWriter(counter, UTF_8)) { | ||
writeJsonPayload(payload, writer); | ||
|
||
try (InputStream rawIn = os.asByteSource().openBufferedStream(); | ||
BufferedInputStream contentStream = new BufferedInputStream(rawIn)) { | ||
Method createSessionMethod = ProtocolHandshake.class.getDeclaredMethod("createSession", | ||
HttpHandler.class, InputStream.class, long.class); | ||
createSessionMethod.setAccessible(true); | ||
//noinspection unchecked | ||
return (Either<SessionNotCreatedException, Result>) createSessionMethod.invoke( | ||
this, client, contentStream, counter.getCount() | ||
); | ||
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { | ||
throw new WebDriverException(e); | ||
} | ||
} finally { | ||
os.reset(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems I'm too late
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np, I could add it in the next PR