View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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 org.jboss.netty.example.http.upload;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.channel.ChannelHandlerContext;
20  import org.jboss.netty.channel.ExceptionEvent;
21  import org.jboss.netty.channel.MessageEvent;
22  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
23  import org.jboss.netty.handler.codec.http2.HttpChunk;
24  import org.jboss.netty.handler.codec.http2.HttpResponse;
25  import org.jboss.netty.util.CharsetUtil;
26  
27  /**
28   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
29   * @author Andy Taylor (andy.taylor@jboss.org)
30   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
31   * @author <a href="http://openr66.free.fr/">Frederic Bregier</a>
32   *
33   * @version $Rev: 612 $, $Date: 2010-11-11 19:35:43 +0100 (jeu., 11 nov. 2010) $
34   */
35  public class HttpResponseHandler extends SimpleChannelUpstreamHandler {
36  
37      private volatile boolean readingChunks;
38  
39  	@Override
40      public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
41          if (!readingChunks) {
42              HttpResponse response = (HttpResponse) e.getMessage();
43  
44              System.out.println("STATUS: " + response.getStatus());
45              System.out.println("VERSION: " + response.getProtocolVersion());
46              System.out.println();
47  
48              if (!response.getHeaderNames().isEmpty()) {
49                  for (String name: response.getHeaderNames()) {
50                      for (String value: response.getHeaders(name)) {
51                          System.out.println("HEADER: " + name + " = " + value);
52                      }
53                  }
54                  System.out.println();
55              }
56  
57              if (response.getStatus().getCode() == 200 && response.isChunked()) {
58                  readingChunks = true;
59                  System.out.println("CHUNKED CONTENT {");
60              } else {
61                  ChannelBuffer content = response.getContent();
62                  if (content.readable()) {
63                      System.out.println("CONTENT {");
64                      System.out.println(content.toString(CharsetUtil.UTF_8));
65                      System.out.println("} END OF CONTENT");
66                  }
67              }
68          } else {
69              HttpChunk chunk = (HttpChunk) e.getMessage();
70              if (chunk.isLast()) {
71                  readingChunks = false;
72                  System.out.println("} END OF CHUNKED CONTENT");
73              } else {
74                  System.out.print(chunk.getContent().toString(CharsetUtil.UTF_8));
75                  System.out.flush();
76              }
77          }
78      }
79  
80      @Override
81      public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
82              throws Exception {
83          e.getCause().printStackTrace();
84          e.getChannel().close();
85      }
86  }