HelloServerHandler.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package hello;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.buffer.Unpooled;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6. import java.util.concurrent.ScheduledFuture;
  7. import java.util.concurrent.TimeUnit;
  8. import com.fasterxml.jackson.databind.*;
  9. import io.netty.channel.Channel;
  10. import io.netty.channel.ChannelFuture;
  11. import io.netty.channel.ChannelFutureListener;
  12. import io.netty.channel.ChannelHandlerContext;
  13. import io.netty.channel.SimpleChannelInboundHandler;
  14. import io.netty.handler.codec.http.DefaultFullHttpResponse;
  15. import io.netty.handler.codec.http.FullHttpResponse;
  16. import io.netty.handler.codec.http.HttpHeaders;
  17. import io.netty.handler.codec.http.HttpRequest;
  18. import io.netty.handler.codec.http.HttpResponseStatus;
  19. import io.netty.handler.codec.http.HttpVersion;
  20. import io.netty.util.CharsetUtil;
  21. public class HelloServerHandler extends SimpleChannelInboundHandler<Object> {
  22. private final SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
  23. private CharSequence date;
  24. private static final ObjectMapper MAPPER = new ObjectMapper();
  25. private final ByteBuf buffer = Unpooled.directBuffer().writeBytes("Hello, World!".getBytes(CharsetUtil.UTF_8));
  26. private final CharSequence contentLength = HttpHeaders.newEntity(String.valueOf(buffer.readableBytes()));
  27. private static final CharSequence TYPE_PLAIN = HttpHeaders.newEntity("text/plain; charset=UTF-8");
  28. private static final CharSequence TYPE_JSON = HttpHeaders.newEntity("application/json; charset=UTF-8");
  29. private static final CharSequence SERVER_NAME = HttpHeaders.newEntity("Netty");
  30. private static final CharSequence CONTENT_TYPE_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.CONTENT_TYPE);
  31. private static final CharSequence DATE_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.DATE);
  32. private static final CharSequence CONTENT_LENGTH_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.CONTENT_LENGTH);
  33. private static final CharSequence SERVER_ENTITY = HttpHeaders.newEntity(HttpHeaders.Names.SERVER);
  34. @Override
  35. public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
  36. if (msg instanceof HttpRequest) {
  37. HttpRequest request = (HttpRequest) msg;
  38. String uri = request.getUri();
  39. switch (uri) {
  40. case "/plaintext":
  41. writeResponse(ctx, request, buffer.duplicate().retain(), TYPE_PLAIN, contentLength);
  42. return;
  43. case "/json":
  44. byte[] json = MAPPER.writeValueAsBytes(Collections.singletonMap("message", "Hello, World!"));
  45. writeResponse(ctx, request, Unpooled.wrappedBuffer(json), TYPE_JSON,
  46. String.valueOf(json.length));
  47. return;
  48. }
  49. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, Unpooled.EMPTY_BUFFER, false);
  50. ctx.write(response).addListener(ChannelFutureListener.CLOSE);
  51. }
  52. }
  53. private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf,
  54. CharSequence contentType, CharSequence contentLength) {
  55. // Decide whether to close the connection or not.
  56. boolean keepAlive = HttpHeaders.isKeepAlive(request);
  57. // Build the response object.
  58. FullHttpResponse response = new DefaultFullHttpResponse(
  59. HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf, false);
  60. HttpHeaders headers = response.headers();
  61. headers.set(CONTENT_TYPE_ENTITY, contentType);
  62. headers.set(SERVER_ENTITY, SERVER_NAME);
  63. headers.set(DATE_ENTITY, date);
  64. if (keepAlive) {
  65. headers.set(CONTENT_LENGTH_ENTITY, contentLength);
  66. }
  67. // Close the non-keep-alive connection after the write operation is done.
  68. if (!keepAlive) {
  69. ctx.write(response).addListener(ChannelFutureListener.CLOSE);
  70. } else {
  71. ctx.write(response, ctx.voidPromise());
  72. }
  73. }
  74. @Override
  75. public void exceptionCaught(
  76. ChannelHandlerContext ctx, Throwable cause) throws Exception {
  77. ctx.close();
  78. }
  79. @Override
  80. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
  81. ctx.flush();
  82. }
  83. @Override
  84. public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
  85. super.handlerRemoved(ctx);
  86. buffer.release();
  87. }
  88. @Override
  89. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  90. date = HttpHeaders.newEntity(format.format(new Date()));
  91. Channel channel = ctx.channel();
  92. final ScheduledFuture<?> future = channel.eventLoop().scheduleWithFixedDelay(new Runnable() {
  93. @Override
  94. public void run() {
  95. date = HttpHeaders.newEntity(format.format(new Date()));
  96. }
  97. }, 1000, 1000, TimeUnit.MILLISECONDS);
  98. channel.closeFuture().addListener(new ChannelFutureListener() {
  99. @Override
  100. public void operationComplete(ChannelFuture channelFuture) throws Exception {
  101. future.cancel(false);
  102. }
  103. });
  104. }
  105. }