Let SniHandler accept Mapping as well as DominaNameMapping

Related: #4470 #4473

Motivation:

A user might want to:

- implement dynamic mapping from hostname to SslContext
- server large number of domain names whose SslContext can be
  initialized lazily and destroyed when unused

Modifications:

- Let SniHandler accept Mapping<String, SslContext> as well as
  DomainNameMapping
- Make the default constructor of SslContext so that a user can create
  his or her own SslContext wrapper

Result:

Flexibility
This commit is contained in:
Trustin Lee 2015-12-17 13:55:10 +09:00
parent 904e70a4d4
commit b62c5290ed
2 changed files with 21 additions and 7 deletions

View File

@ -21,6 +21,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.DomainNameMapping; import io.netty.util.DomainNameMapping;
import io.netty.util.Mapping;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
@ -40,28 +41,38 @@ public class SniHandler extends ByteToMessageDecoder {
private static final InternalLogger logger = private static final InternalLogger logger =
InternalLoggerFactory.getInstance(SniHandler.class); InternalLoggerFactory.getInstance(SniHandler.class);
private final DomainNameMapping<SslContext> mapping; private final Mapping<Object, SslContext> mapping;
private boolean handshaken; private boolean handshaken;
private volatile String hostname; private volatile String hostname;
private volatile SslContext selectedContext; private volatile SslContext selectedContext;
/** /**
* Create a SNI detection handler with configured {@link SslContext} * Creates a SNI detection handler with configured {@link SslContext}
* maintained by {@link DomainNameMapping} * maintained by {@link Mapping}
* *
* @param mapping the mapping of domain name to {@link SslContext} * @param mapping the mapping of domain name to {@link SslContext}
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public SniHandler(DomainNameMapping<? extends SslContext> mapping) { public SniHandler(Mapping<? super String, ? extends SslContext> mapping) {
if (mapping == null) { if (mapping == null) {
throw new NullPointerException("mapping"); throw new NullPointerException("mapping");
} }
this.mapping = (DomainNameMapping<SslContext>) mapping; this.mapping = (Mapping<Object, SslContext>) mapping;
handshaken = false; handshaken = false;
} }
/**
* Creates a SNI detection handler with configured {@link SslContext}
* maintained by {@link DomainNameMapping}
*
* @param mapping the mapping of domain name to {@link SslContext}
*/
public SniHandler(DomainNameMapping<? extends SslContext> mapping) {
this((Mapping<String, ? extends SslContext>) mapping);
}
/** /**
* @return the selected hostname * @return the selected hostname
*/ */
@ -70,7 +81,7 @@ public class SniHandler extends ByteToMessageDecoder {
} }
/** /**
* @return the selected sslcontext * @return the selected {@link SslContext}
*/ */
public SslContext sslContext() { public SslContext sslContext() {
return selectedContext; return selectedContext;

View File

@ -760,7 +760,10 @@ public abstract class SslContext {
return apn; return apn;
} }
SslContext() { } /**
* Creates a new instance.
*/
protected SslContext() { }
/** /**
* Returns {@code true} if and only if this context is for server-side. * Returns {@code true} if and only if this context is for server-side.