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 * 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.protobuf;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.channel.Channel;
20 import org.jboss.netty.channel.ChannelHandler.Sharable;
21 import org.jboss.netty.channel.ChannelHandlerContext;
22 import org.jboss.netty.channel.ChannelPipeline;
23 import org.jboss.netty.channel.MessageEvent;
24 import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
25 import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
26 import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
27
28 import com.google.protobuf.Message;
29 import com.google.protobuf.MessageLite;
30
31 /**
32 * Encodes the requested <a href="http://code.google.com/p/protobuf/">Google
33 * Protocol Buffers</a> {@link Message} and {@link MessageLite} into a
34 * {@link ChannelBuffer}. A typical setup for TCP/IP would be:
35 * <pre>
36 * {@link ChannelPipeline} pipeline = ...;
37 *
38 * // Decoders
39 * pipeline.addLast("frameDecoder",
40 * new {@link LengthFieldBasedFrameDecoder}(1048576, 0, 4, 0, 4));
41 * pipeline.addLast("protobufDecoder",
42 * new {@link ProtobufDecoder}(MyMessage.getDefaultInstance()));
43 *
44 * // Encoder
45 * pipeline.addLast("frameEncoder", new {@link LengthFieldPrepender}(4));
46 * pipeline.addLast("protobufEncoder", new {@link ProtobufEncoder}());
47 * </pre>
48 * and then you can use a {@code MyMessage} instead of a {@link ChannelBuffer}
49 * as a message:
50 * <pre>
51 * void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageEvent} e) {
52 * MyMessage req = (MyMessage) e.getMessage();
53 * MyMessage res = MyMessage.newBuilder().setText(
54 * "Did you say '" + req.getText() + "'?").build();
55 * ch.write(res);
56 * }
57 * </pre>
58 *
59 * @apiviz.landmark
60 */
61 @Sharable
62 public class ProtobufEncoder extends OneToOneEncoder {
63
64 @Override
65 protected Object encode(
66 ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
67 if (msg instanceof MessageLite) {
68 byte[] array = ((MessageLite) msg).toByteArray();
69 return ctx.getChannel().getConfig().getBufferFactory().getBuffer(array, 0, array.length);
70 }
71 if (msg instanceof MessageLite.Builder) {
72 byte[] array = ((MessageLite.Builder) msg).build().toByteArray();
73 return ctx.getChannel().getConfig().getBufferFactory().getBuffer(array, 0, array.length);
74 }
75 return msg;
76 }
77 }