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 d0bc3be79c
commit 26c20c91bd
2 changed files with 10 additions and 11 deletions

View File

@ -25,6 +25,7 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.internal.StringUtil;
import javax.net.ssl.SSLEngine;
import java.util.List;
@ -84,7 +85,15 @@ public abstract class SpdyOrHttpChooser extends ByteToMessageDecoder {
* {@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
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.ChannelInboundHandler;
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
* the correct handlers for the selected protocol.
@ -36,14 +34,6 @@ public class SpdyOrHttpHandler extends SpdyOrHttpChooser {
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
protected ChannelInboundHandler createHttpRequestHandlerForHttp() {
return new SpdyServerHandler();