HelloServerHandler.java 5.1 KB

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