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.
This commit is contained in:
Leonardo Freitas Gomes 2014-01-05 15:53:52 +01:00 committed by Norman Maurer
parent 2c3f02c453
commit 3ce35c710a

View File

@ -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<Object> 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();