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.handler.codec.oneone;
    17
    18  import static org.jboss.netty.channel.Channels.*;
    19
    20  import org.jboss.netty.buffer.ChannelBuffers;
    21  import org.jboss.netty.channel.Channel;
    22  import org.jboss.netty.channel.ChannelDownstreamHandler;
    23  import org.jboss.netty.channel.ChannelEvent;
    24  import org.jboss.netty.channel.ChannelHandlerContext;
    25  import org.jboss.netty.channel.ChannelPipeline;
    26  import org.jboss.netty.channel.MessageEvent;
    27  import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
    28  import org.jboss.netty.handler.codec.frame.Delimiters;
    29
    30  /**
    31   * Transforms a write request into another write request.  A typical setup for
    32   * TCP/IP would be:
    33   * <pre>
    34   * {@link ChannelPipeline} pipeline = ...;
    35   *
    36   * // Decoders
    37   * pipeline.addLast("frameDecoder", new {@link DelimiterBasedFrameDecoder}(80, {@link Delimiters#nulDelimiter()}));
    38   * pipeline.addLast("customDecoder", new {@link OneToOneDecoder}() { ... });
    39   *
    40   * // Encoder
    41   * pipeline.addLast("customEncoder", new {@link OneToOneEncoder}() { ... });
    42   * </pre>
    43   *
    44   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    45   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    46   *
    47   * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (목, 12 6월 2008) $
    48   *
    49   * @apiviz.landmark
    50   */
    51  public abstract class OneToOneEncoder implements ChannelDownstreamHandler {
    52
    53      protected OneToOneEncoder() {
    54          super();
    55      }
    56
    57      public void handleDownstream(
    58              ChannelHandlerContext ctx, ChannelEvent evt) throws Exception {
    59          if (!(evt instanceof MessageEvent)) {
    60              ctx.sendDownstream(evt);
    61              return;
    62          }
    63
    64          MessageEvent e = (MessageEvent) evt;
    65          Object originalMessage = e.getMessage();
    66          Object encodedMessage = encode(ctx, e.getChannel(), originalMessage);
    67          if (originalMessage == encodedMessage) {
    68              ctx.sendDownstream(evt);
    69          } else if (encodedMessage != null) {
    70              write(ctx, e.getFuture(), encodedMessage, e.getRemoteAddress());
    71          }
    72      }
    73
    74      /**
    75       * Transforms the specified message into another message and return the
    76       * transformed message.  Note that you can not return {@code null}, unlike
    77       * you can in {@link OneToOneDecoder#decode(ChannelHandlerContext, Channel, Object)};
    78       * you must return something, at least {@link ChannelBuffers#EMPTY_BUFFER}.
    79       */
    80      protected abstract Object encode(
    81              ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception;
    82  }