View Javadoc
1   /*
2    * Copyright 2012 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    *   https://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.netty5.example.proxy;
17  
18  import io.netty5.channel.Channel;
19  import io.netty5.channel.ChannelHandler;
20  import io.netty5.channel.ChannelHandlerContext;
21  
22  public class HexDumpProxyBackendHandler implements ChannelHandler {
23  
24      private final Channel inboundChannel;
25  
26      public HexDumpProxyBackendHandler(Channel inboundChannel) {
27          this.inboundChannel = inboundChannel;
28      }
29  
30      @Override
31      public void channelActive(ChannelHandlerContext ctx) {
32          ctx.read();
33      }
34  
35      @Override
36      public void channelRead(final ChannelHandlerContext ctx, Object msg) {
37          inboundChannel.writeAndFlush(msg).addListener(inboundChannel, (inbound, future) -> {
38              if (future.isSuccess()) {
39                  ctx.channel().read();
40              } else {
41                  inbound.close();
42              }
43          });
44      }
45  
46      @Override
47      public void channelInactive(ChannelHandlerContext ctx) {
48          HexDumpProxyFrontendHandler.closeOnFlush(inboundChannel);
49      }
50  
51      @Override
52      public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
53          cause.printStackTrace();
54          HexDumpProxyFrontendHandler.closeOnFlush(ctx.channel());
55      }
56  }