HelloServerHandler.java 4.8 KB

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