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.channel.Channel; 21 import org.jboss.netty.channel.ChannelEvent; 22 import org.jboss.netty.channel.ChannelHandlerContext; 23 import org.jboss.netty.channel.ChannelPipeline; 24 import org.jboss.netty.channel.ChannelUpstreamHandler; 25 import org.jboss.netty.channel.MessageEvent; 26 import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder; 27 import org.jboss.netty.handler.codec.frame.Delimiters; 28 import org.jboss.netty.handler.codec.frame.FrameDecoder; 29 30 /** 31 * Transforms a received message into another message. Please note that this 32 * decoder must be used with a proper {@link FrameDecoder} such as 33 * {@link DelimiterBasedFrameDecoder} or you must implement proper framing 34 * mechanism by yourself if you are using a stream-based transport such as 35 * TCP/IP. A typical setup for TCP/IP would be: 36 * <pre> 37 * {@link ChannelPipeline} pipeline = ...; 38 * 39 * // Decoders 40 * pipeline.addLast("frameDecoder", new {@link DelimiterBasedFrameDecoder}(80, {@link Delimiters#nulDelimiter()})); 41 * pipeline.addLast("customDecoder", new {@link OneToOneDecoder}() { ... }); 42 * 43 * // Encoder 44 * pipeline.addLast("customEncoder", new {@link OneToOneEncoder}() { ... }); 45 * </pre> 46 * 47 * @author <a href="http://www.jboss.org/netty/">The Netty Project</a> 48 * @author <a href="http://gleamynode.net/">Trustin Lee</a> 49 * 50 * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (목, 12 6월 2008) $ 51 * 52 * @apiviz.landmark 53 */ 54 public abstract class OneToOneDecoder implements ChannelUpstreamHandler { 55 56 /** 57 * Creates a new instance with the current system character set. 58 */ 59 protected OneToOneDecoder() { 60 super(); 61 } 62 63 public void handleUpstream( 64 ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { 65 if (!(evt instanceof MessageEvent)) { 66 ctx.sendUpstream(evt); 67 return; 68 } 69 70 MessageEvent e = (MessageEvent) evt; 71 Object originalMessage = e.getMessage(); 72 Object decodedMessage = decode(ctx, e.getChannel(), originalMessage); 73 if (originalMessage == decodedMessage) { 74 ctx.sendUpstream(evt); 75 } else if (decodedMessage != null) { 76 fireMessageReceived(ctx, decodedMessage, e.getRemoteAddress()); 77 } 78 } 79 80 /** 81 * Transforms the specified received message into another message and return 82 * the transformed message. Return {@code null} if the received message 83 * is supposed to be discarded. 84 */ 85 protected abstract Object decode( 86 ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception; 87 }