From 3ce35c710a5fc9a8416436b4bce09c98e7ed45b2 Mon Sep 17 00:00:00 2001 From: Leonardo Freitas Gomes Date: Sun, 5 Jan 2014 15:53:52 +0100 Subject: [PATCH] Improves SelectedProtocol enum to avoid boilerplate on subclasses NPN server providers return a String version of the negotiated protocol and the getProtocolByName method allows to easily get an instance of the SelectedProtocol enum and avoid the need for a switch statement in each subclass to match the String against the enum value. --- .../handler/codec/spdy/SpdyOrHttpChooser.java | 57 ++++++++++++++----- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java index 717182b5e9..6a0fe6c899 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java @@ -26,9 +26,10 @@ import io.netty.handler.codec.http.HttpRequestDecoder; import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.ssl.SslHandler; -import javax.net.ssl.SSLEngine; import java.util.List; +import javax.net.ssl.SSLEngine; + /** * {@link ChannelInboundHandler} which is responsible to setup the {@link ChannelPipeline} either for * HTTP or SPDY. This offers an easy way for users to support both at the same time while not care to @@ -39,11 +40,38 @@ public abstract class SpdyOrHttpChooser extends ByteToMessageDecoder { // TODO: Replace with generic NPN handler public enum SelectedProtocol { - SPDY_3, - SPDY_3_1, - HTTP_1_1, - HTTP_1_0, - UNKNOWN + SPDY_3("spdy/3"), + SPDY_3_1("spdy/3.1"), + HTTP_1_1("http/1.1"), + HTTP_1_0("http/1.0"), + UNKNOWN("Unknown"); + + private String name; + + private SelectedProtocol(String defaultName) { + this.name = defaultName; + } + + public String getName() { + return this.name; + } + + /** + * Get an instance of this enum based on the protocol name returned by the NPN server provider + * + * @param name + * the protocol name + * @return the SelectedProtocol instance + */ + public static SelectedProtocol getProtocolByName(String name) { + + for (SelectedProtocol protocol : SelectedProtocol.values()) { + if (protocol.getName().equals(name)) { + return protocol; + } + } + return UNKNOWN; + } } private final int maxSpdyContentLength; @@ -55,8 +83,8 @@ public abstract class SpdyOrHttpChooser extends ByteToMessageDecoder { } /** - * Return the {@link SelectedProtocol} for the {@link SSLEngine}. If its not known yet implementations - * MUST return {@link SelectedProtocol#UNKNOWN}. + * Return the {@link SelectedProtocol} for the {@link SSLEngine}. If its not known yet implementations MUST return + * {@link SelectedProtocol#UNKNOWN}. * */ protected abstract SelectedProtocol getProtocol(SSLEngine engine); @@ -64,14 +92,17 @@ public abstract class SpdyOrHttpChooser extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { if (initPipeline(ctx)) { - // When we reached here we can remove this handler as its now clear what protocol we want to use - // from this point on. This will also take care of forward all messages. + // When we reached here we can remove this handler as its now clear + // what protocol we want to use + // from this point on. This will also take care of forward all + // messages. ctx.pipeline().remove(this); } } private boolean initPipeline(ChannelHandlerContext ctx) { - // Get the SslHandler from the ChannelPipeline so we can obtain the SslEngine from it. + // Get the SslHandler from the ChannelPipeline so we can obtain the + // SslEngine from it. SslHandler handler = ctx.pipeline().get(SslHandler.class); if (handler == null) { // SslHandler is needed by SPDY by design. @@ -136,8 +167,8 @@ public abstract class SpdyOrHttpChooser extends ByteToMessageDecoder { * when the {@link SelectedProtocol} was {@link SelectedProtocol#SPDY_3} or * {@link SelectedProtocol#SPDY_3_1}. * - * By default this getMethod will just delecate to {@link #createHttpRequestHandlerForHttp()}, but - * sub-classes may override this to change the behaviour. + * By default this getMethod will just delecate to {@link #createHttpRequestHandlerForHttp()}, but sub-classes may + * override this to change the behaviour. */ protected ChannelInboundHandler createHttpRequestHandlerForSpdy() { return createHttpRequestHandlerForHttp();