org.openqa.selenium.grid.node.CapabilityResponseEncoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grid Show documentation
Show all versions of grid Show documentation
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. 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 org.openqa.selenium.grid.node;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.ErrorCodes;
import org.openqa.selenium.remote.SessionId;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
public class CapabilityResponseEncoder {
private static final Json JSON = new Json();
private static final ResponseEncoder, byte[]> JWP_ENCODER =
new Encoder(Dialect.OSS);
private static final ResponseEncoder, byte[]> W3C_ENCODER =
new Encoder(Dialect.W3C);
private CapabilityResponseEncoder() {
// Utility class
}
public static ResponseEncoder, byte[]> getEncoder(Dialect dialect) {
switch (dialect) {
case OSS:
return JWP_ENCODER;
case W3C:
return W3C_ENCODER;
default:
throw new IllegalArgumentException("Unrecognised dialect: " + dialect);
}
}
public interface ResponseEncoder extends Function, BiFunction {
@Override
default ResponseEncoder andThen(Function after) {
return new ResponseEncoder() {
@Override
public V apply(final T t, final U u) {
return after.apply(ResponseEncoder.this.apply(t, u));
}
@Override
public V apply(final T t) {
return after.apply(ResponseEncoder.this.apply(t));
}
};
}
}
private static class Encoder implements ResponseEncoder, byte[]> {
private final Dialect dialect;
private Encoder(Dialect dialect) {
this.dialect = Objects.requireNonNull(dialect);
}
@Override
public byte[] apply(Session session, Map metadata) {
Objects.requireNonNull(session);
Objects.requireNonNull(metadata);
return encodeAsResponse(dialect, session.getId(), session.getCapabilities(), metadata);
}
@Override
public byte[] apply(Session session) {
return apply(session, ImmutableMap.of());
}
/**
* Create a UTF-8 encoded response for a given dialect for use with the New Session command.
*/
private static byte[] encodeAsResponse(
Dialect dialect,
SessionId id,
Capabilities capabilities,
Map metadata) {
Map toEncode;
switch (dialect) {
case OSS:
toEncode = encodeJsonWireProtocol(id, capabilities, metadata);
break;
case W3C:
toEncode = encodeW3C(id, capabilities, metadata);
break;
default:
throw new IllegalArgumentException("Unknown dialect: " + dialect);
}
return JSON.toJson(toEncode).getBytes(UTF_8);
}
private static Map encodeW3C(
SessionId id,
Capabilities capabilities,
Map metadata) {
return ImmutableMap.builder()
.putAll(metadata)
.put("value", ImmutableMap.of(
"sessionId", id,
"capabilities", capabilities))
.build();
}
private static Map encodeJsonWireProtocol(
SessionId id,
Capabilities capabilities,
Map metadata) {
return ImmutableMap.builder()
.putAll(metadata)
.put("status", ErrorCodes.SUCCESS)
.put("sessionId", id)
.put("value", capabilities)
.build();
}
}
}