View Javadoc
1   /*
2    * Copyright 2024 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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                  // Tell the client we're going to close the connection.
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 }