Skip to content

Commit

Permalink
Add extraHeaders support for java-client (#3406)
Browse files Browse the repository at this point in the history
This adds the ability for construction of a java-client with extra headers set on every request. This is potentially useful for cases where a proxy needs additional information to be able to route a request. It has been plumbed through the java client example utilities. In the future, we may wish to more formally support these use cases through DeephavenTarget / DH URIs.
  • Loading branch information
devinrsmith authored Feb 6, 2023
1 parent 46ef898 commit e133c81
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.grpc.ManagedChannel;
import picocli.CommandLine.Option;

import java.util.Map;

public class ConnectOptions {

public static final String DEFAULT_HOST = "localhost";
Expand Down Expand Up @@ -46,6 +48,9 @@ public static ManagedChannel open(ConnectOptions options) {
@Option(names = {"--ssl"}, description = "The optional ssl config file.", converter = SSLConverter.class)
SSLConfig ssl;

@Option(names = {"--header"})
Map<String, String> headers;

private ClientConfig config() {
final Builder builder = ClientConfig.builder().target(target);
if (userAgent != null) {
Expand All @@ -57,6 +62,9 @@ private ClientConfig config() {
if (ssl != null) {
builder.ssl(ssl);
}
if (headers != null) {
builder.putAllExtraHeaders(headers);
}
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import io.deephaven.ssl.config.TrustJdk;
import io.deephaven.ssl.config.impl.KickstartUtils;
import io.grpc.ManagedChannel;
import io.grpc.Metadata;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.MetadataUtils;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
import nl.altindag.ssl.SSLFactory;
import nl.altindag.ssl.util.NettySslUtils;

import javax.net.ssl.SSLException;
import java.util.Map;

public final class ChannelHelper {

Expand Down Expand Up @@ -62,6 +65,15 @@ public static ManagedChannel channel(ClientConfig clientConfig) {
channelBuilder.usePlaintext();
}
clientConfig.userAgent().ifPresent(channelBuilder::userAgent);
if (!clientConfig.extraHeaders().isEmpty()) {
channelBuilder.intercept(MetadataUtils.newAttachHeadersInterceptor(of(clientConfig.extraHeaders())));
}
return channelBuilder.build();
}

private static Metadata of(Map<String, String> map) {
final Metadata metadata = new Metadata();
map.forEach((k, v) -> metadata.put(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER), v));
return metadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import io.deephaven.annotations.BuildableStyle;
import io.deephaven.ssl.config.SSLConfig;
import io.deephaven.uri.DeephavenTarget;
import org.immutables.value.Value.Check;
import org.immutables.value.Value.Default;
import org.immutables.value.Value.Immutable;

import java.util.Map;
import java.util.Optional;

/**
Expand Down Expand Up @@ -40,6 +40,11 @@ public static Builder builder() {
*/
public abstract Optional<String> userAgent();

/**
* The extra headers.
*/
public abstract Map<String, String> extraHeaders();

/**
* The maximum inbound message size. Defaults to 100MiB.
*/
Expand All @@ -56,6 +61,10 @@ public interface Builder {

Builder userAgent(String userAgent);

Builder putExtraHeaders(String key, String value);

Builder putAllExtraHeaders(Map<String, ? extends String> entries);

Builder maxInboundMessageSize(int maxInboundMessageSize);

ClientConfig build();
Expand Down

0 comments on commit e133c81

Please sign in to comment.