1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.testsuite_jpms.main;
18
19 import io.netty.buffer.Unpooled;
20 import io.netty.channel.ChannelFuture;
21 import io.netty.channel.ChannelFutureListener;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.channel.SimpleChannelInboundHandler;
24 import io.netty.handler.codec.http.HttpObject;
25 import io.netty.handler.codec.http.HttpRequest;
26 import io.netty.handler.codec.http.HttpUtil;
27 import io.netty.handler.codec.http.FullHttpResponse;
28 import io.netty.handler.codec.http.DefaultFullHttpResponse;
29 import io.netty.handler.ssl.SslHandler;
30
31 import java.nio.charset.StandardCharsets;
32 import java.util.stream.Collectors;
33
34 import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
35 import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
36 import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
37 import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
38 import static io.netty.handler.codec.http.HttpHeaderValues.CLOSE;
39 import static io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN;
40 import static io.netty.handler.codec.http.HttpResponseStatus.OK;
41
42 public class HttpHelloWorldServerHandler extends SimpleChannelInboundHandler<HttpObject> {
43
44 @Override
45 public void channelReadComplete(ChannelHandlerContext ctx) {
46 ctx.flush();
47 }
48
49 @Override
50 public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
51 if (msg instanceof HttpRequest) {
52 HttpRequest req = (HttpRequest) msg;
53
54 String modulesInfo = ModuleLayer.boot().modules().stream()
55 .map(module -> "- " + module.getName() + " " +
56 (module.getDescriptor().isAutomatic() ? "(automatic)" : ""))
57 .collect(Collectors.joining("\r\n", "Boot layer:\r\n", "\r\n"));
58
59 String channelType = ctx.channel().getClass().getName();
60
61 String sslEngineInfo = "";
62 SslHandler sslHandler = ctx.pipeline().get(SslHandler.class);
63 if (sslHandler != null) {
64 sslEngineInfo = "SSL Engine: " + sslHandler.engine().getClass().getName() + "\r\n";
65 }
66
67 String hello = "Hello World\r\n" +
68 "Transport: " + channelType + "\r\n" +
69 sslEngineInfo +
70 modulesInfo;
71
72 boolean keepAlive = HttpUtil.isKeepAlive(req);
73 FullHttpResponse response = new DefaultFullHttpResponse(
74 req.protocolVersion(), OK,
75 Unpooled.copiedBuffer(hello, StandardCharsets.UTF_8));
76 response.headers()
77 .set(CONTENT_TYPE, TEXT_PLAIN)
78 .setInt(CONTENT_LENGTH, response.content().readableBytes());
79
80 if (keepAlive) {
81 if (!req.protocolVersion().isKeepAliveDefault()) {
82 response.headers().set(CONNECTION, KEEP_ALIVE);
83 }
84 } else {
85
86 response.headers().set(CONNECTION, CLOSE);
87 }
88
89 ChannelFuture f = ctx.write(response);
90
91 if (!keepAlive) {
92 f.addListener(ChannelFutureListener.CLOSE);
93 }
94 }
95 }
96
97 @Override
98 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
99 cause.printStackTrace();
100 ctx.close();
101 }
102 }