|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * See the NOTICE file distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.appium.java_client.remote; |
| 18 | + |
| 19 | +import com.google.common.io.CountingOutputStream; |
| 20 | +import com.google.common.io.FileBackedOutputStream; |
| 21 | +import org.openqa.selenium.Capabilities; |
| 22 | +import org.openqa.selenium.ImmutableCapabilities; |
| 23 | +import org.openqa.selenium.SessionNotCreatedException; |
| 24 | +import org.openqa.selenium.WebDriverException; |
| 25 | +import org.openqa.selenium.internal.Either; |
| 26 | +import org.openqa.selenium.json.Json; |
| 27 | +import org.openqa.selenium.json.JsonOutput; |
| 28 | +import org.openqa.selenium.remote.Command; |
| 29 | +import org.openqa.selenium.remote.NewSessionPayload; |
| 30 | +import org.openqa.selenium.remote.ProtocolHandshake; |
| 31 | +import org.openqa.selenium.remote.http.HttpHandler; |
| 32 | + |
| 33 | +import java.io.BufferedInputStream; |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.InputStream; |
| 36 | +import java.io.OutputStreamWriter; |
| 37 | +import java.io.Writer; |
| 38 | +import java.lang.reflect.InvocationTargetException; |
| 39 | +import java.lang.reflect.Method; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.Set; |
| 42 | +import java.util.stream.Stream; |
| 43 | + |
| 44 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 45 | + |
| 46 | +@SuppressWarnings("UnstableApiUsage") |
| 47 | +public class AppiumProtocolHandshake extends ProtocolHandshake { |
| 48 | + private static void writeJsonPayload(NewSessionPayload srcPayload, Appendable destination) { |
| 49 | + try (JsonOutput json = new Json().newOutput(destination)) { |
| 50 | + json.beginObject(); |
| 51 | + |
| 52 | + json.name("capabilities"); |
| 53 | + json.beginObject(); |
| 54 | + |
| 55 | + json.name("firstMatch"); |
| 56 | + json.beginArray(); |
| 57 | + json.beginObject(); |
| 58 | + json.endObject(); |
| 59 | + json.endArray(); |
| 60 | + |
| 61 | + json.name("alwaysMatch"); |
| 62 | + try { |
| 63 | + Method getW3CMethod = NewSessionPayload.class.getDeclaredMethod("getW3C"); |
| 64 | + getW3CMethod.setAccessible(true); |
| 65 | + //noinspection unchecked |
| 66 | + ((Stream<Map<String, Object>>) getW3CMethod.invoke(srcPayload)) |
| 67 | + .findFirst() |
| 68 | + .map(json::write) |
| 69 | + .orElseGet(() -> { |
| 70 | + json.beginObject(); |
| 71 | + json.endObject(); |
| 72 | + return null; |
| 73 | + }); |
| 74 | + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
| 75 | + throw new WebDriverException(e); |
| 76 | + } |
| 77 | + |
| 78 | + json.endObject(); // Close "capabilities" object |
| 79 | + |
| 80 | + try { |
| 81 | + Method writeMetaDataMethod = NewSessionPayload.class.getDeclaredMethod( |
| 82 | + "writeMetaData", JsonOutput.class); |
| 83 | + writeMetaDataMethod.setAccessible(true); |
| 84 | + writeMetaDataMethod.invoke(srcPayload, json); |
| 85 | + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
| 86 | + throw new WebDriverException(e); |
| 87 | + } |
| 88 | + |
| 89 | + json.endObject(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Result createSession(HttpHandler client, Command command) throws IOException { |
| 95 | + //noinspection unchecked |
| 96 | + Capabilities desired = ((Set<Map<String, Object>>) command.getParameters().get("capabilities")) |
| 97 | + .stream() |
| 98 | + .findAny() |
| 99 | + .map(ImmutableCapabilities::new) |
| 100 | + .orElseGet(ImmutableCapabilities::new); |
| 101 | + try (NewSessionPayload payload = NewSessionPayload.create(desired)) { |
| 102 | + Either<SessionNotCreatedException, Result> result = createSession(client, payload); |
| 103 | + if (result.isRight()) { |
| 104 | + return result.right(); |
| 105 | + } |
| 106 | + throw result.left(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Either<SessionNotCreatedException, Result> createSession( |
| 112 | + HttpHandler client, NewSessionPayload payload) throws IOException { |
| 113 | + int threshold = (int) Math.min(Runtime.getRuntime().freeMemory() / 10, Integer.MAX_VALUE); |
| 114 | + FileBackedOutputStream os = new FileBackedOutputStream(threshold); |
| 115 | + |
| 116 | + try (CountingOutputStream counter = new CountingOutputStream(os); |
| 117 | + Writer writer = new OutputStreamWriter(counter, UTF_8)) { |
| 118 | + writeJsonPayload(payload, writer); |
| 119 | + |
| 120 | + try (InputStream rawIn = os.asByteSource().openBufferedStream(); |
| 121 | + BufferedInputStream contentStream = new BufferedInputStream(rawIn)) { |
| 122 | + Method createSessionMethod = ProtocolHandshake.class.getDeclaredMethod("createSession", |
| 123 | + HttpHandler.class, InputStream.class, long.class); |
| 124 | + createSessionMethod.setAccessible(true); |
| 125 | + //noinspection unchecked |
| 126 | + return (Either<SessionNotCreatedException, Result>) createSessionMethod.invoke( |
| 127 | + this, client, contentStream, counter.getCount() |
| 128 | + ); |
| 129 | + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
| 130 | + throw new WebDriverException(e); |
| 131 | + } |
| 132 | + } finally { |
| 133 | + os.reset(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments