Skip to content

Commit

Permalink
Split FindDirective#proxyAddress into resolvedProxyAddress() and unre…
Browse files Browse the repository at this point in the history
…solvedProxyAddress()
  • Loading branch information
jbaldassari committed Sep 11, 2023
1 parent e2b5b57 commit 2df1f3e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 37 deletions.
81 changes: 47 additions & 34 deletions src/main/java/com/mabl/net/proxy/FindProxyDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ public ConnectionType connectionType() {
return connectionType;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FindProxyDirective that = (FindProxyDirective) o;
return connectionType == that.connectionType && Objects.equals(proxyHostAndPort, that.proxyHostAndPort);
}

@Override
public int hashCode() {
return Objects.hash(connectionType, proxyHostAndPort);
}

/**
* Tests whether this directive has connection type {@link ConnectionType#DIRECT}.
*
Expand All @@ -67,34 +80,13 @@ public boolean isProxy() {
return !isDirect();
}

/**
* Get the proxy address associated with this directive.
* <p>
* Note that the {@link InetSocketAddress} returned by this method is created via
* {@link InetSocketAddress#createUnresolved(String, int)}. It may be necessary to create a new
* {@link InetSocketAddress} in order to resolve the hostname before use.
* <p>
* For example:
* <pre>new InetSocketAddress(address.getHostString(), address.getPort());</pre>
*
* @return the proxy address, or null if the connection type is {@link ConnectionType#DIRECT}.
*/
public InetSocketAddress proxyAddress() {
return proxyHostAndPort.map(hostAndPort -> {
final String[] hostPortParts = hostAndPort.split(HOST_PORT_DELIMITER);
final String host = hostPortParts[0];
final int port = Integer.parseInt(hostPortParts[1]);
return InetSocketAddress.createUnresolved(host, port);
}).orElse(null);
}

/**
* Gets the proxy host component of the directive, e.g. "192.168.1.1"
*
* @return the proxy host for this directive, or null if the connection type is {@link ConnectionType#DIRECT}.
*/
public String proxyHost() {
return Optional.ofNullable(proxyAddress()).map(InetSocketAddress::getHostString)
return Optional.ofNullable(unresolvedProxyAddress()).map(InetSocketAddress::getHostString)
.orElse(null);
}

Expand All @@ -104,7 +96,7 @@ public String proxyHost() {
* @return the proxy port for this directive, or null if the connection type is {@link ConnectionType#DIRECT}.
*/
public Integer proxyPort() {
return Optional.ofNullable(proxyAddress()).map(InetSocketAddress::getPort)
return Optional.ofNullable(unresolvedProxyAddress()).map(InetSocketAddress::getPort)
.orElse(null);
}

Expand All @@ -117,24 +109,45 @@ public String proxyHostAndPort() {
return proxyHostAndPort.orElse(null);
}

/**
* Get the proxy address associated with this directive.
* <p>
* Note that if the proxy host is a hostname, it will be resolved to an IP address by this method.
* To create an unresolved {@link InetSocketAddress}, use {@link #unresolvedProxyAddress()} instead.
*
* @return the proxy address, or null if the connection type is {@link ConnectionType#DIRECT}.
* @see #unresolvedProxyAddress()
*/
public InetSocketAddress resolvedProxyAddress() {
return Optional.ofNullable(unresolvedProxyAddress())
.map(unresolved -> new InetSocketAddress(unresolved.getHostString(), unresolved.getPort()))
.orElse(null);
}

@Override
public String toString() {
final StringBuilder builder = new StringBuilder(connectionType.name());
proxyHostAndPort.ifPresent(proxy -> builder.append(" ").append(proxy));
return builder.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FindProxyDirective that = (FindProxyDirective) o;
return connectionType == that.connectionType && Objects.equals(proxyHostAndPort, that.proxyHostAndPort);
}

@Override
public int hashCode() {
return Objects.hash(connectionType, proxyHostAndPort);
/**
* Get the proxy address associated with this directive.
* <p>
* Note that the {@link InetSocketAddress} returned by this method is created via
* {@link InetSocketAddress#createUnresolved(String, int)}. To create a resolved {@link InetSocketAddress}
* use {@link #resolvedProxyAddress()} instead.
*
* @return the proxy address, or null if the connection type is {@link ConnectionType#DIRECT}.
* @see #resolvedProxyAddress()
*/
public InetSocketAddress unresolvedProxyAddress() {
return proxyHostAndPort.map(hostAndPort -> {
final String[] hostPortParts = hostAndPort.split(HOST_PORT_DELIMITER);
final String host = hostPortParts[0];
final int port = Integer.parseInt(hostPortParts[1]);
return InetSocketAddress.createUnresolved(host, port);
}).orElse(null);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/com/mabl/net/proxy/FindProxyDirectiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public void proxy() throws Exception {
assertEquals("10.0.0.1:8080", directive.proxyHostAndPort());
assertEquals("10.0.0.1", directive.proxyHost());
assertEquals(new Integer(8080), directive.proxyPort());
assertEquals(InetSocketAddress.createUnresolved("10.0.0.1", 8080), directive.proxyAddress());
assertEquals("10.0.0.1", directive.proxyAddress().getHostString());
assertEquals(8080, directive.proxyAddress().getPort());
assertEquals(InetSocketAddress.createUnresolved("10.0.0.1", 8080), directive.unresolvedProxyAddress());
assertEquals(new InetSocketAddress("10.0.0.1", 8080), directive.resolvedProxyAddress());
assertEquals("10.0.0.1", directive.unresolvedProxyAddress().getHostString());
assertEquals(8080, directive.unresolvedProxyAddress().getPort());
assertEquals("PROXY 10.0.0.1:8080", directive.toString());
assertEquals(directive, FindProxyDirective.parse(" PROXY 10.0.0.1:8080 "));
}
Expand Down

0 comments on commit 2df1f3e

Please sign in to comment.