Move generic code to HttpOrSpdyChooser to simplify implementations

Motivation:

HttpOrSpdyChooser can be simplified so the user not need to implement getProtocol(...) method.

Modification:

Add implementation for the method. The user can override it if necessary.

Result:

Easier usage of HttpOrSpdyChooser.
This commit is contained in:
Norman Maurer 2014-07-07 09:34:33 +02:00
parent 93a265ed0a
commit 696e355a70
2 changed files with 10 additions and 11 deletions

View File

@ -24,6 +24,7 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder; import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandler;
import io.netty.util.internal.StringUtil;
import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngine;
import java.util.List; import java.util.List;
@ -82,7 +83,15 @@ public abstract class SpdyOrHttpChooser extends ByteToMessageDecoder {
* {@link SelectedProtocol#UNKNOWN}. * {@link SelectedProtocol#UNKNOWN}.
* *
*/ */
protected abstract SelectedProtocol getProtocol(SSLEngine engine); protected SelectedProtocol getProtocol(SSLEngine engine) {
String[] protocol = StringUtil.split(engine.getSession().getProtocol(), ':');
if (protocol.length < 2) {
// Use HTTP/1.1 as default
return SelectedProtocol.HTTP_1_1;
}
SelectedProtocol selectedProtocol = SelectedProtocol.protocol(protocol[1]);
return selectedProtocol;
}
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

View File

@ -18,8 +18,6 @@ package io.netty.example.spdy.server;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.spdy.SpdyOrHttpChooser; import io.netty.handler.codec.spdy.SpdyOrHttpChooser;
import javax.net.ssl.SSLEngine;
/** /**
* Negotiates with the browser if SPDY or HTTP is going to be used. Once decided, the Netty pipeline is setup with * Negotiates with the browser if SPDY or HTTP is going to be used. Once decided, the Netty pipeline is setup with
* the correct handlers for the selected protocol. * the correct handlers for the selected protocol.
@ -36,14 +34,6 @@ public class SpdyOrHttpHandler extends SpdyOrHttpChooser {
super(maxSpdyContentLength, maxHttpContentLength); super(maxSpdyContentLength, maxHttpContentLength);
} }
@Override
protected SelectedProtocol getProtocol(SSLEngine engine) {
String[] protocol = engine.getSession().getProtocol().split(":");
SelectedProtocol selectedProtocol = SelectedProtocol.protocol(protocol[1]);
System.err.println("Selected Protocol is " + selectedProtocol);
return selectedProtocol;
}
@Override @Override
protected ChannelHandler createHttpRequestHandlerForHttp() { protected ChannelHandler createHttpRequestHandlerForHttp() {
return new SpdyServerHandler(); return new SpdyServerHandler();