Add ApplicationProtocolConfig.DISABLED

Motivation:

When ALPN/NPN is disabled, a user has to instantiate a new
ApplicationProtocolConfig with meaningless parameters.

Modifications:

- Add ApplicationProtocolConfig.DISABLED, the singleton instance
- Reject the constructor calls with Protocol.NONE, which doesn't make
  much sense because a user should use DISABLED instead.

Result:

More user-friendly API when ALPN/NPN is not needed by a user.
This commit is contained in:
Trustin Lee 2014-10-31 14:09:46 +09:00
parent a6a42d2f19
commit 3ddac6adff
2 changed files with 26 additions and 9 deletions

View File

@ -15,18 +15,23 @@
*/ */
package io.netty.handler.ssl; package io.netty.handler.ssl;
import static io.netty.handler.ssl.ApplicationProtocolUtil.toList; import javax.net.ssl.SSLEngine;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import javax.net.ssl.SSLEngine; import static io.netty.handler.ssl.ApplicationProtocolUtil.*;
import static io.netty.util.internal.ObjectUtil.*;
/** /**
* Provides an {@link SSLEngine} agnostic way to configure a {@link ApplicationProtocolNegotiator}. * Provides an {@link SSLEngine} agnostic way to configure a {@link ApplicationProtocolNegotiator}.
*/ */
public final class ApplicationProtocolConfig { public final class ApplicationProtocolConfig {
/**
* The configuration that disables application protocol negotiation.
*/
public static final ApplicationProtocolConfig DISABLED = new ApplicationProtocolConfig();
private final List<String> supportedProtocols; private final List<String> supportedProtocols;
private final Protocol protocol; private final Protocol protocol;
private final SelectorFailureBehavior selectorBehavior; private final SelectorFailureBehavior selectorBehavior;
@ -63,12 +68,27 @@ public final class ApplicationProtocolConfig {
* @param selectedBehavior How the peer being notified of the selected protocol should behave. * @param selectedBehavior How the peer being notified of the selected protocol should behave.
* @param supportedProtocols The order of iteration determines the preference of support for protocols. * @param supportedProtocols The order of iteration determines the preference of support for protocols.
*/ */
private ApplicationProtocolConfig(Protocol protocol, SelectorFailureBehavior selectorBehavior, private ApplicationProtocolConfig(
Protocol protocol, SelectorFailureBehavior selectorBehavior,
SelectedListenerFailureBehavior selectedBehavior, List<String> supportedProtocols) { SelectedListenerFailureBehavior selectedBehavior, List<String> supportedProtocols) {
this.supportedProtocols = Collections.unmodifiableList(checkNotNull(supportedProtocols, "supportedProtocols")); this.supportedProtocols = Collections.unmodifiableList(checkNotNull(supportedProtocols, "supportedProtocols"));
this.protocol = checkNotNull(protocol, "protocol"); this.protocol = checkNotNull(protocol, "protocol");
this.selectorBehavior = checkNotNull(selectorBehavior, "selectorBehavior"); this.selectorBehavior = checkNotNull(selectorBehavior, "selectorBehavior");
this.selectedBehavior = checkNotNull(selectedBehavior, "selectedBehavior"); this.selectedBehavior = checkNotNull(selectedBehavior, "selectedBehavior");
if (protocol == Protocol.NONE) {
throw new IllegalArgumentException("protocol (" + Protocol.NONE + ") must not be " + Protocol.NONE + '.');
}
}
/**
* A special constructor that is used to instantiate {@link #DISABLED}.
*/
private ApplicationProtocolConfig() {
supportedProtocols = Collections.emptyList();
protocol = Protocol.NONE;
selectorBehavior = SelectorFailureBehavior.CHOOSE_MY_LAST_PROTOCOL;
selectedBehavior = SelectedListenerFailureBehavior.ACCEPT;
} }
/** /**

View File

@ -31,7 +31,6 @@ import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.TrustManagerFactory;
import java.io.File; import java.io.File;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -602,9 +601,7 @@ public abstract class SslContext {
static ApplicationProtocolConfig toApplicationProtocolConfig(Iterable<String> nextProtocols) { static ApplicationProtocolConfig toApplicationProtocolConfig(Iterable<String> nextProtocols) {
ApplicationProtocolConfig apn; ApplicationProtocolConfig apn;
if (nextProtocols == null) { if (nextProtocols == null) {
apn = new ApplicationProtocolConfig( apn = ApplicationProtocolConfig.DISABLED;
Protocol.NONE, SelectorFailureBehavior.CHOOSE_MY_LAST_PROTOCOL,
SelectedListenerFailureBehavior.ACCEPT, Collections.<String>emptyList());
} else { } else {
apn = new ApplicationProtocolConfig( apn = new ApplicationProtocolConfig(
Protocol.NPN_AND_ALPN, SelectorFailureBehavior.CHOOSE_MY_LAST_PROTOCOL, Protocol.NPN_AND_ALPN, SelectorFailureBehavior.CHOOSE_MY_LAST_PROTOCOL,