diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageEncoder.java b/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageEncoder.java index 45a1f58548..468558438d 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageEncoder.java @@ -21,7 +21,6 @@ */ package org.jboss.netty.handler.codec.http; -import static org.jboss.netty.channel.Channels.*; import static org.jboss.netty.handler.codec.http.HttpCodecUtil.*; import java.util.List; @@ -29,10 +28,10 @@ import java.util.Set; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipelineCoverage; -import org.jboss.netty.channel.MessageEvent; -import org.jboss.netty.channel.SimpleChannelHandler; +import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; /** * encodes an http message @@ -43,23 +42,23 @@ import org.jboss.netty.channel.SimpleChannelHandler; * @version $Rev$, $Date$ */ @ChannelPipelineCoverage("one") -public abstract class HttpMessageEncoder extends SimpleChannelHandler { +public abstract class HttpMessageEncoder extends OneToOneEncoder { + @Override - public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception { - if (!(e.getMessage() instanceof HttpMessage)) { - ctx.sendDownstream(e); - return; + protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { + if (!(msg instanceof HttpMessage)) { + return msg; } - HttpMessage request = (HttpMessage) e.getMessage(); + HttpMessage request = (HttpMessage) msg; ChannelBuffer buf = ChannelBuffers.dynamicBuffer( - e.getChannel().getConfig().getBufferFactory()); + channel.getConfig().getBufferFactory()); encodeInitialLine(buf, request); encodeHeaders(buf, request); buf.writeBytes(CRLF); if (request.getContent() != null) { buf.writeBytes(request.getContent()); } - write(ctx, e.getChannel(), e.getFuture(), buf, e.getRemoteAddress()); + return buf; } /** diff --git a/src/main/java/org/jboss/netty/handler/codec/oneone/OneToOneDecoder.java b/src/main/java/org/jboss/netty/handler/codec/oneone/OneToOneDecoder.java new file mode 100644 index 0000000000..603ee6cdb8 --- /dev/null +++ b/src/main/java/org/jboss/netty/handler/codec/oneone/OneToOneDecoder.java @@ -0,0 +1,85 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.handler.codec.oneone; + +import static org.jboss.netty.channel.Channels.*; + +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelEvent; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.channel.ChannelUpstreamHandler; +import org.jboss.netty.channel.MessageEvent; +import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder; +import org.jboss.netty.handler.codec.frame.Delimiters; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +/** + * Transforms a received message into another message. Please note that this + * decoder must be used with a proper {@link FrameDecoder} such as + * {@link DelimiterBasedFrameDecoder} if you are using a stream-based transport + * such as TCP/IP. A typical decoder setup would be: + *
+ * {@link ChannelPipeline} pipeline = ...; + * + * // Decoders + * pipeline.addLast("frameDecoder", new {@link DelimiterBasedFrameDecoder}(80, {@link Delimiters#nulDelimiter()})); + * pipeline.addLast("customDecoder", new {@link OneToOneDecoder}() { ... }); + * + * // Encoder + * pipeline.addLast("customEncoder", new {@link OneToOneEncoder}() { ... }); + *+ * + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * + * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (목, 12 6월 2008) $ + * + * @apiviz.landmark + */ +public abstract class OneToOneDecoder implements ChannelUpstreamHandler { + + /** + * Creates a new instance with the current system character set. + */ + protected OneToOneDecoder() { + super(); + } + + public void handleUpstream( + ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { + if (!(evt instanceof MessageEvent)) { + ctx.sendUpstream(evt); + return; + } + + MessageEvent e = (MessageEvent) evt; + fireMessageReceived( + ctx, e.getChannel(), + decode(ctx, e.getChannel(), e.getMessage()), + e.getRemoteAddress()); + } + + protected abstract Object decode( + ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception; +} diff --git a/src/main/java/org/jboss/netty/handler/codec/oneone/OneToOneEncoder.java b/src/main/java/org/jboss/netty/handler/codec/oneone/OneToOneEncoder.java new file mode 100644 index 0000000000..dced91e78d --- /dev/null +++ b/src/main/java/org/jboss/netty/handler/codec/oneone/OneToOneEncoder.java @@ -0,0 +1,78 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.handler.codec.oneone; + +import static org.jboss.netty.channel.Channels.*; + +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelDownstreamHandler; +import org.jboss.netty.channel.ChannelEvent; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.channel.MessageEvent; +import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder; +import org.jboss.netty.handler.codec.frame.Delimiters; + +/** + * Transforms a received message into another message. A typical decoder setup + * would be: + *
+ * {@link ChannelPipeline} pipeline = ...; + * + * // Decoders + * pipeline.addLast("frameDecoder", new {@link DelimiterBasedFrameDecoder}(80, {@link Delimiters#nulDelimiter()})); + * pipeline.addLast("customDecoder", new {@link OneToOneDecoder}() { ... }); + * + * // Encoder + * pipeline.addLast("customEncoder", new {@link OneToOneEncoder}() { ... }); + *+ * + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * + * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (목, 12 6월 2008) $ + * + * @apiviz.landmark + */ +public abstract class OneToOneEncoder implements ChannelDownstreamHandler { + + protected OneToOneEncoder() { + super(); + } + + public void handleDownstream( + ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { + if (!(evt instanceof MessageEvent)) { + ctx.sendDownstream(evt); + return; + } + + MessageEvent e = (MessageEvent) evt; + write(ctx, e.getChannel(), e.getFuture(), + encode(ctx, e.getChannel(), e.getMessage()), + e.getRemoteAddress()); + } + + protected abstract Object encode( + ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception; +} diff --git a/src/main/java/org/jboss/netty/handler/codec/oneone/package-info.java b/src/main/java/org/jboss/netty/handler/codec/oneone/package-info.java new file mode 100644 index 0000000000..f2cafac183 --- /dev/null +++ b/src/main/java/org/jboss/netty/handler/codec/oneone/package-info.java @@ -0,0 +1,28 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +/** + * Simplistic abstract classes which help implement encoder and decoder that + * transform an object into another object and vice versa. + */ +package org.jboss.netty.handler.codec.oneone; \ No newline at end of file diff --git a/src/main/java/org/jboss/netty/handler/codec/serialization/CompatibleObjectEncoder.java b/src/main/java/org/jboss/netty/handler/codec/serialization/CompatibleObjectEncoder.java index 835dac5696..437e06b838 100644 --- a/src/main/java/org/jboss/netty/handler/codec/serialization/CompatibleObjectEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/serialization/CompatibleObjectEncoder.java @@ -22,8 +22,6 @@ */ package org.jboss.netty.handler.codec.serialization; -import static org.jboss.netty.channel.Channels.*; - import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; @@ -33,11 +31,10 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBufferFactory; import org.jboss.netty.buffer.ChannelBufferOutputStream; import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.ChannelDownstreamHandler; -import org.jboss.netty.channel.ChannelEvent; +import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipelineCoverage; -import org.jboss.netty.channel.MessageEvent; +import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; /** * An encoder which serializes a Java object into a {@link ChannelBuffer} @@ -52,7 +49,7 @@ import org.jboss.netty.channel.MessageEvent; * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (목, 12 6월 2008) $ */ @ChannelPipelineCoverage("one") -public class CompatibleObjectEncoder implements ChannelDownstreamHandler { +public class CompatibleObjectEncoder extends OneToOneEncoder { private final AtomicReference