diff --git a/gradle.properties b/gradle.properties index ca54b58..ec0a45c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = net-http -version = 3.0.3 +version = 3.0.4 org.gradle.warning.mode = ALL diff --git a/net-http-server-netty-secure/src/main/java/org/xbib/net/http/server/netty/secure/HttpsAddress.java b/net-http-server-netty-secure/src/main/java/org/xbib/net/http/server/netty/secure/HttpsAddress.java index 4480f8e..212bfaf 100644 --- a/net-http-server-netty-secure/src/main/java/org/xbib/net/http/server/netty/secure/HttpsAddress.java +++ b/net-http-server-netty-secure/src/main/java/org/xbib/net/http/server/netty/secure/HttpsAddress.java @@ -93,7 +93,7 @@ public class HttpsAddress extends HttpAddress { return sslContext; } - public static class Builder { + public static class Builder extends HttpAddress.Builder { private static TrustManagerFactory TRUST_MANAGER_FACTORY; @@ -120,14 +120,6 @@ public class HttpsAddress extends HttpAddress { } } - private String host; - - private int port = -1; - - private boolean isSecure = true; - - private HttpVersion httpVersion = HttpVersion.HTTP_1_1; - private TrustManagerFactory trustManagerFactory; private KeyStore trustManagerKeyStore; @@ -154,32 +146,34 @@ public class HttpsAddress extends HttpAddress { private boolean enableOcsp; - private Set hostNames; - - private Builder() { + protected Builder() { this.trustManagerFactory = TRUST_MANAGER_FACTORY; this.sslProvider = OpenSsl.isAvailable() ? SslProvider.OPENSSL : SslProvider.JDK; this.ciphers = OpenSsl.isAvailable() ? DEFAULT_OPENSSL_CIPHERS : DEFAULT_JDK_CIPHERS; this.cipherSuiteFilter = SupportedCipherSuiteFilter.INSTANCE; } + @Override public Builder setHost(String host) { this.host = host; return this; } + @Override public Builder setPort(int port) { this.port = port; return this; } + @Override public Builder setSecure(boolean secure) { this.isSecure = secure; return this; } - public Builder setVersion(HttpVersion httpVersion) { - this.httpVersion = httpVersion; + @Override + public Builder setVersion(HttpVersion version) { + this.version = version; return this; } @@ -313,9 +307,10 @@ public class HttpsAddress extends HttpAddress { return this; } + @Override public HttpsAddress build() throws KeyStoreException, SSLException { Objects.requireNonNull(host); - Objects.requireNonNull(httpVersion); + Objects.requireNonNull(version); Objects.requireNonNull(privateKey); Objects.requireNonNull(certChain); if (certChain.isEmpty()) { @@ -334,14 +329,14 @@ public class HttpsAddress extends HttpAddress { sslContextBuilder.sslContextProvider(sslContextProvider); } if (applicationProtocolConfig == null) { - if (httpVersion.equals(HttpVersion.HTTP_2_0)) { + if (version.equals(HttpVersion.HTTP_2_0)) { // OpenSSL does not support FATAL_ALERT behaviour applicationProtocolConfig = new ApplicationProtocolConfig(ApplicationProtocolConfig.Protocol.ALPN, ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE, ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1); } - if (httpVersion.equals(HttpVersion.HTTP_1_1)) { + if (version.equals(HttpVersion.HTTP_1_1)) { applicationProtocolConfig = new ApplicationProtocolConfig(ApplicationProtocolConfig.Protocol.ALPN, ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE, ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT, @@ -360,7 +355,7 @@ public class HttpsAddress extends HttpAddress { " session timeout = " + sslContext.sessionTimeout() + " cipher suite = " + sslContext.cipherSuites() ); - return new HttpsAddress(host, port, httpVersion, isSecure, hostNames, sslContext); + return new HttpsAddress(host, port, version, isSecure, hostNames, sslContext); } } diff --git a/net-http-server-simple-secure/src/main/java/org/xbib/net/http/server/simple/secure/HttpsAddress.java b/net-http-server-simple-secure/src/main/java/org/xbib/net/http/server/simple/secure/HttpsAddress.java index f8f362f..8eec320 100644 --- a/net-http-server-simple-secure/src/main/java/org/xbib/net/http/server/simple/secure/HttpsAddress.java +++ b/net-http-server-simple-secure/src/main/java/org/xbib/net/http/server/simple/secure/HttpsAddress.java @@ -29,13 +29,9 @@ import java.util.Map; import java.util.Objects; import java.util.ServiceLoader; import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; public class HttpsAddress extends HttpAddress { - private static final Logger logger = Logger.getLogger(HttpsAddress.class.getName()); - private final SSLContext sslContext; public HttpsAddress(String host, Integer port, HttpVersion version, @@ -84,7 +80,7 @@ public class HttpsAddress extends HttpAddress { return sslContext; } - public static class Builder { + public static class Builder extends HttpAddress.Builder { private static TrustManagerFactory TRUST_MANAGER_FACTORY; @@ -99,14 +95,6 @@ public class HttpsAddress extends HttpAddress { } } - private String host; - - private int port = -1; - - private boolean isSecure = true; - - private HttpVersion httpVersion = HttpVersion.HTTP_1_1; - private TrustManagerFactory trustManagerFactory; private KeyStore trustManagerKeyStore; @@ -126,23 +114,27 @@ public class HttpsAddress extends HttpAddress { this.ciphers = DEFAULT_JDK_CIPHERS; } + @Override public Builder setHost(String host) { this.host = host; return this; } + @Override public Builder setPort(int port) { this.port = port; return this; } + @Override public Builder setSecure(boolean secure) { this.isSecure = secure; return this; } - public Builder setVersion(HttpVersion httpVersion) { - this.httpVersion = httpVersion; + @Override + public Builder setVersion(HttpVersion version) { + this.version = version; return this; } @@ -220,7 +212,7 @@ public class HttpsAddress extends HttpAddress { public HttpsAddress build() throws KeyStoreException { Objects.requireNonNull(host); - Objects.requireNonNull(httpVersion); + Objects.requireNonNull(version); Objects.requireNonNull(privateKey); Objects.requireNonNull(certChain); if (certChain.isEmpty()) { @@ -236,7 +228,7 @@ public class HttpsAddress extends HttpAddress { certChain) .withTrustMaterial(trustManagerFactory) .build(); - return new HttpsAddress(host, port, httpVersion, isSecure, hostNames, sslFactory.getSslContext()); + return new HttpsAddress(host, port, version, isSecure, hostNames, sslFactory.getSslContext()); } } diff --git a/net-http/src/main/java/org/xbib/net/http/HttpAddress.java b/net-http/src/main/java/org/xbib/net/http/HttpAddress.java index feb3352..e9fb5f1 100644 --- a/net-http/src/main/java/org/xbib/net/http/HttpAddress.java +++ b/net-http/src/main/java/org/xbib/net/http/HttpAddress.java @@ -1,5 +1,8 @@ package org.xbib.net.http; +import java.security.KeyStoreException; +import java.util.Objects; +import javax.net.ssl.SSLException; import org.xbib.net.Address; import org.xbib.net.NetworkUtils; import org.xbib.net.SocketConfig; @@ -15,21 +18,15 @@ import java.util.Set; */ public class HttpAddress implements Address { - private final String host; - - private final Integer port; - - private final HttpVersion version; - - private final Boolean secure; - - private final Set hostNames; + private final Builder builder; private InetAddress inetAddress; private InetSocketAddress inetSocketAddress; - private SocketConfig socketConfig; + public static Builder builder() { + return new Builder(); + } public static HttpAddress http1(String host) { return new HttpAddress(host, 80, HttpVersion.HTTP_1_1, false); @@ -74,30 +71,36 @@ public class HttpAddress implements Address { public HttpAddress(String host, Integer port, HttpVersion version, boolean secure) { this(host, port, version, secure, Set.of()); } + public HttpAddress(String host, Integer port, HttpVersion version, boolean secure, Set hostNames) { - this.host = host; - this.port = (port == null || port == -1) ? secure ? 443 : 80 : port; - this.version = version; - this.secure = secure; - this.hostNames = hostNames; - this.socketConfig = new SocketConfig(); + this(builder() + .setHost(host) + .setPort((port == null || port == -1) ? secure ? 443 : 80 : port) + .setVersion(version) + .setSecure(secure) + .setHostNames(hostNames) + .setSocketConfig(new SocketConfig())); + } + + public HttpAddress(Builder builder) { + this.builder = builder; } @Override public String getHost() { - return host; + return builder.host; } @Override public Integer getPort() { - return port; + return builder.port; } @Override public InetAddress getInetAddress() throws IOException { if (inetAddress == null) { - this.inetAddress = NetworkUtils.resolveInetAddress(host, null); + this.inetAddress = NetworkUtils.resolveInetAddress(builder.host, null); } return inetAddress; } @@ -106,7 +109,7 @@ public class HttpAddress implements Address { public InetSocketAddress getInetSocketAddress() throws IOException { if (inetSocketAddress == null) { InetAddress inetAddress = getInetAddress(); - this.inetSocketAddress = new InetSocketAddress(inetAddress.getHostAddress(), port); + this.inetSocketAddress = new InetSocketAddress(inetAddress.getHostAddress(), builder.port); } return inetSocketAddress; } @@ -114,60 +117,113 @@ public class HttpAddress implements Address { @Override public URL base() { return isSecure() ? - URL.https().host(host).port(port).build() : - URL.http().host(host).port(port).build(); + URL.https().host(builder.host).port(builder.port).build() : + URL.http().host(builder.host).port(builder.port).build(); } @Override public boolean isSecure() { - return secure; - } - - public void setSocketConfig(SocketConfig socketConfig) { - this.socketConfig = socketConfig; + return builder.isSecure; } @Override public SocketConfig getSocketConfig() { - return socketConfig; + return builder.socketConfig; } public Set getHostNames() { - return hostNames; + return builder.hostNames; } public HttpVersion getVersion() { - return version; + return builder.version; } public String hostAndPort() { - return host + ":" + port; + return builder.host + ":" + builder.port; } public String hostAddressAndPort() throws IOException { - return getInetAddress().getHostAddress() + ":" + port; + return getInetAddress().getHostAddress() + ":" + builder.port; } public String canonicalHostAndPort() throws IOException { - return getInetAddress().getCanonicalHostName() + ":" + port; + return getInetAddress().getCanonicalHostName() + ":" + builder.port; } @Override public String toString() { - return "[" + version + "]" + (secure ? "[SECURE]" : "") + host + ":" + port; + return "[" + builder.version + "]" + (builder.isSecure ? "[SECURE]" : "") + builder.host + ":" + builder.port; } @Override public boolean equals(Object object) { return object instanceof HttpAddress && - host.equals(((HttpAddress) object).host) && - (port != null && port.equals(((HttpAddress) object).port)) && - version.equals(((HttpAddress) object).version) && - secure.equals(((HttpAddress) object).secure); + builder.host.equals(((HttpAddress) object).builder.host) && + (builder.port != null && builder.port.equals(((HttpAddress) object).builder.port)) && + builder.version.equals(((HttpAddress) object).builder.version) && + builder.isSecure.equals(((HttpAddress) object).builder.isSecure); } @Override public int hashCode() { - return host.hashCode() ^ port ^ version.hashCode() ^ secure.hashCode(); + return builder.host.hashCode() ^ builder.port ^ builder.version.hashCode() ^ builder.isSecure.hashCode(); + } + + public static class Builder { + + protected String host; + + protected Integer port; + + protected Boolean isSecure; + + protected HttpVersion version; + + protected Set hostNames; + + protected SocketConfig socketConfig; + + protected Builder() { + this.port = -1; + this.isSecure = false; + this.version = HttpVersion.HTTP_1_1; + } + + public Builder setHost(String host) { + this.host = host; + return this; + } + + public Builder setPort(int port) { + this.port = port; + return this; + } + + public Builder setSecure(boolean secure) { + this.isSecure = secure; + return this; + } + + public Builder setVersion(HttpVersion httpVersion) { + this.version = httpVersion; + return this; + } + + public Builder setHostNames(Set hostNames) { + this.hostNames = hostNames; + return this; + } + + public Builder setSocketConfig(SocketConfig socketConfig) { + this.socketConfig = socketConfig; + return this; + } + + public HttpAddress build() throws KeyStoreException, SSLException { + Objects.requireNonNull(host); + Objects.requireNonNull(version); + return new HttpAddress(this); + } } }