View Javadoc
1   /*
2    * Copyright 2014 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    *   http://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  package io.netty.example.spdy.server;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.channel.ChannelFutureListener;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.channel.SimpleChannelInboundHandler;
23  import io.netty.handler.codec.http.DefaultFullHttpResponse;
24  import io.netty.handler.codec.http.FullHttpResponse;
25  import io.netty.handler.codec.http.HttpHeaderUtil;
26  import io.netty.handler.codec.http.HttpHeaderValues;
27  import io.netty.handler.codec.http.HttpRequest;
28  import io.netty.util.CharsetUtil;
29  
30  import java.util.Date;
31  
32  import static io.netty.handler.codec.http.HttpHeaderNames.*;
33  import static io.netty.handler.codec.http.HttpResponseStatus.*;
34  import static io.netty.handler.codec.http.HttpVersion.*;
35  
36  /**
37   * HTTP handler that responds with a "Hello World"
38   */
39  public class SpdyServerHandler extends SimpleChannelInboundHandler<Object> {
40  
41      @Override
42      public void messageReceived(ChannelHandlerContext ctx, Object msg) {
43          if (msg instanceof HttpRequest) {
44              HttpRequest req = (HttpRequest) msg;
45  
46              if (HttpHeaderUtil.is100ContinueExpected(req)) {
47                  ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
48              }
49              boolean keepAlive = HttpHeaderUtil.isKeepAlive(req);
50  
51              ByteBuf content = Unpooled.copiedBuffer("Hello World " + new Date(), CharsetUtil.UTF_8);
52  
53              FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
54              response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
55              response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
56  
57              if (!keepAlive) {
58                  ctx.write(response).addListener(ChannelFutureListener.CLOSE);
59              } else {
60                  response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
61                  ctx.write(response);
62              }
63          }
64      }
65  
66      @Override
67      public void channelReadComplete(ChannelHandlerContext ctx) {
68          ctx.flush();
69      }
70  
71      @Override
72      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
73          cause.printStackTrace();
74          ctx.close();
75      }
76  }