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
Allow direct proxy configuration #435
Changes from 7 commits
d1660bd
3d86ae1
22205d2
4b82941
0acc399
3c089ce
56cd9a8
9bbe491
b15b303
5b7ca0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,45 +36,105 @@ | |
@Style(visibility = Style.ImplementationVisibility.PACKAGE, builder = "new") | ||
public abstract class ProxyConfiguration { | ||
|
||
enum Type { | ||
|
||
/** | ||
* Use a direct connection. This option will bypass any JVM-level configured proxy settings. | ||
*/ | ||
direct, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. other enums in this project are UPPER_CASE, please do the same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These leak into the configuration, which usually doesn't contain upper case values. Glad to change, but that's why I had made this particular enum lowercase There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SslConfiguration uses UPPER_CASE, i think. What are example of lower-case configs? |
||
|
||
/** | ||
* Use an http-proxy specified by {@link ProxyConfiguration#hostAndPort()} and (optionally) | ||
* {@link ProxyConfiguration#credentials()}. | ||
*/ | ||
http; | ||
} | ||
|
||
/** | ||
* The hostname and port of the HTTP/HTTPS Proxy. Recognized formats include those recognized by {@link | ||
* com.google.common.net.HostAndPort}, for instance {@code foo.com:80}, {@code 192.168.3.100:8080}, etc. | ||
*/ | ||
public abstract String hostAndPort(); | ||
@JsonProperty("hostAndPort") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are snake case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the builder specified below. This supports deserializing in snake-case and serializing in camel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you mean deserializing the camel-cased hostAndPort? I don't think that was ever supported because of the annotation |
||
public abstract Optional<String> maybeHostAndPort(); | ||
|
||
/** | ||
* @deprecated Use maybeHostAndPort(). | ||
*/ | ||
@Deprecated | ||
@SuppressWarnings("checkstyle:designforextension") | ||
@JsonIgnore | ||
@Lazy | ||
public String hostAndPort() { | ||
Preconditions.checkState(maybeHostAndPort().isPresent(), "hostAndPort was not configured"); | ||
return maybeHostAndPort().get(); | ||
} | ||
|
||
/** | ||
* Credentials if the proxy needs authentication. | ||
*/ | ||
public abstract Optional<BasicCredentials> credentials(); | ||
|
||
@Value.Default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. document default. |
||
@SuppressWarnings("checkstyle:designforextension") | ||
public Type type() { | ||
return Type.http; | ||
} | ||
|
||
@Value.Check | ||
protected final void check() { | ||
HostAndPort host = HostAndPort.fromString(hostAndPort()); | ||
Preconditions.checkArgument(host.hasPort(), "Given hostname does not contain a port number: " + host); | ||
switch (type()) { | ||
case http: | ||
HostAndPort host = HostAndPort.fromString(maybeHostAndPort().get()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't this NPE when type=http and no host is specified? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch yes |
||
Preconditions.checkArgument(host.hasPort(), | ||
"Given hostname does not contain a port number: " + host); | ||
break; | ||
case direct: | ||
Preconditions.checkArgument(!maybeHostAndPort().isPresent() && !credentials().isPresent(), | ||
"Neither credential nor host-and-port may be configured for direct proxies"); | ||
break; | ||
default: | ||
throw new IllegalStateException("Unrecognized case; this is a library bug"); | ||
} | ||
|
||
if (credentials().isPresent()) { | ||
Preconditions.checkArgument(type() == Type.http, "credentials only valid for http proxies"); | ||
} | ||
} | ||
|
||
@Lazy | ||
@SuppressWarnings("checkstyle:designforextension") | ||
@JsonIgnore | ||
public Proxy toProxy() { | ||
HostAndPort hostAndPort = HostAndPort.fromString(hostAndPort()); | ||
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort())); | ||
switch (type()) { | ||
case http: | ||
HostAndPort hostAndPort = HostAndPort.fromString(maybeHostAndPort().get()); | ||
InetSocketAddress addr = new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort()); | ||
return new Proxy(Proxy.Type.HTTP, addr); | ||
case direct: | ||
return Proxy.NO_PROXY; | ||
default: | ||
throw new IllegalStateException("unrecognized proxy type; this is a library error"); | ||
} | ||
} | ||
|
||
public static ProxyConfiguration of(String hostAndPort) { | ||
return new ProxyConfiguration.Builder().hostAndPort(hostAndPort).build(); | ||
return new ProxyConfiguration.Builder().maybeHostAndPort(hostAndPort).build(); | ||
} | ||
|
||
public static ProxyConfiguration of(String hostAndPort, BasicCredentials credentials) { | ||
return new ProxyConfiguration.Builder().hostAndPort(hostAndPort).credentials(credentials).build(); | ||
return new ProxyConfiguration.Builder().maybeHostAndPort(hostAndPort).credentials(credentials).build(); | ||
} | ||
|
||
public static ProxyConfiguration direct() { | ||
return new ProxyConfiguration.Builder().type(Type.direct).build(); | ||
} | ||
|
||
// TODO(jnewman): #317 - remove kebab-case methods when Jackson 2.7 is picked up | ||
static final class Builder extends ImmutableProxyConfiguration.Builder { | ||
|
||
@JsonProperty("host-and-port") | ||
Builder hostAndPortKebabCase(String hostAndPort) { | ||
return hostAndPort(hostAndPort); | ||
return maybeHostAndPort(hostAndPort); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type: direct |
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.
please update the ServiceConfiguration#proxyConfiguration docs: if absent, uses system proxy configuration.