WebSocketIndexPageHandler Class — netty Architecture
Architecture documentation for the WebSocketIndexPageHandler class in WebSocketIndexPageHandler.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD 8ea989dc_fe6f_0b1b_4077_4a12c09b3008["WebSocketIndexPageHandler"] bb4f4103_99b9_ae0d_cfe8_852a1612e0e3["WebSocketIndexPageHandler.java"] 8ea989dc_fe6f_0b1b_4077_4a12c09b3008 -->|defined in| bb4f4103_99b9_ae0d_cfe8_852a1612e0e3 7461d872_f53c_f8b0_7bb6_0e8e8af04402["WebSocketIndexPageHandler()"] 8ea989dc_fe6f_0b1b_4077_4a12c09b3008 -->|method| 7461d872_f53c_f8b0_7bb6_0e8e8af04402 bed3075c_486b_aa7a_c6e9_bbec68fb8e86["channelRead0()"] 8ea989dc_fe6f_0b1b_4077_4a12c09b3008 -->|method| bed3075c_486b_aa7a_c6e9_bbec68fb8e86 13054ed8_f52d_443c_21f0_3bc43c39ac5d["exceptionCaught()"] 8ea989dc_fe6f_0b1b_4077_4a12c09b3008 -->|method| 13054ed8_f52d_443c_21f0_3bc43c39ac5d 966de156_0f8c_6b06_20a6_e0712003b649["sendHttpResponse()"] 8ea989dc_fe6f_0b1b_4077_4a12c09b3008 -->|method| 966de156_0f8c_6b06_20a6_e0712003b649 067e9bef_ae1e_f5c1_347f_04439f92a7e5["String()"] 8ea989dc_fe6f_0b1b_4077_4a12c09b3008 -->|method| 067e9bef_ae1e_f5c1_347f_04439f92a7e5
Relationship Graph
Source Code
example/src/main/java/io/netty/example/http/websocketx/server/WebSocketIndexPageHandler.java lines 42–118
public class WebSocketIndexPageHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
private final String websocketPath;
public WebSocketIndexPageHandler(String websocketPath) {
this.websocketPath = websocketPath;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
// Handle a bad request.
if (!req.decoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), BAD_REQUEST,
ctx.alloc().buffer(0)));
return;
}
// Handle websocket upgrade request.
if (req.headers().contains(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET, true)) {
ctx.fireChannelRead(req.retain());
return;
}
// Allow only GET methods.
if (!GET.equals(req.method())) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), FORBIDDEN,
ctx.alloc().buffer(0)));
return;
}
// Send the index page
if ("/".equals(req.uri()) || "/index.html".equals(req.uri())) {
String webSocketLocation = getWebSocketLocation(ctx.pipeline(), req, websocketPath);
ByteBuf content = WebSocketServerIndexPage.getContent(webSocketLocation);
FullHttpResponse res = new DefaultFullHttpResponse(req.protocolVersion(), OK, content);
res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
HttpUtil.setContentLength(res, content.readableBytes());
sendHttpResponse(ctx, req, res);
} else {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), NOT_FOUND,
ctx.alloc().buffer(0)));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
HttpResponseStatus responseStatus = res.status();
if (responseStatus.code() != 200) {
ByteBufUtil.writeUtf8(res.content(), responseStatus.toString());
HttpUtil.setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
boolean keepAlive = HttpUtil.isKeepAlive(req) && responseStatus.code() == 200;
HttpUtil.setKeepAlive(res, keepAlive);
ChannelFuture future = ctx.writeAndFlush(res);
if (!keepAlive) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) {
String protocol = "ws";
if (cp.get(SslHandler.class) != null) {
// SSL in use so use Secure WebSockets
protocol = "wss";
}
return protocol + "://" + req.headers().get(HttpHeaderNames.HOST) + path;
}
}
Defined In
Source
Frequently Asked Questions
What is the WebSocketIndexPageHandler class?
WebSocketIndexPageHandler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/http/websocketx/server/WebSocketIndexPageHandler.java.
Where is WebSocketIndexPageHandler defined?
WebSocketIndexPageHandler is defined in example/src/main/java/io/netty/example/http/websocketx/server/WebSocketIndexPageHandler.java at line 42.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free