View Javadoc
1   /*
2    * Copyright 2014 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 io.netty.handler.codec.stomp;
17  
18  import java.util.List;
19  import java.util.Map.Entry;
20  
21  import io.netty.buffer.ByteBuf;
22  import io.netty.channel.ChannelHandlerContext;
23  import io.netty.handler.codec.AsciiHeadersEncoder;
24  import io.netty.handler.codec.AsciiHeadersEncoder.NewlineType;
25  import io.netty.handler.codec.AsciiHeadersEncoder.SeparatorType;
26  import io.netty.handler.codec.MessageToMessageEncoder;
27  import io.netty.util.CharsetUtil;
28  
29  /**
30   * Encodes a {@link StompFrame} or a {@link StompSubframe} into a {@link ByteBuf}.
31   */
32  public class StompSubframeEncoder extends MessageToMessageEncoder<StompSubframe> {
33  
34      @Override
35      protected void encode(ChannelHandlerContext ctx, StompSubframe msg, List<Object> out) throws Exception {
36          if (msg instanceof StompFrame) {
37              StompFrame frame = (StompFrame) msg;
38              ByteBuf frameBuf = encodeFrame(frame, ctx);
39              out.add(frameBuf);
40              ByteBuf contentBuf = encodeContent(frame, ctx);
41              out.add(contentBuf);
42          } else if (msg instanceof StompHeadersSubframe) {
43              StompHeadersSubframe frame = (StompHeadersSubframe) msg;
44              ByteBuf buf = encodeFrame(frame, ctx);
45              out.add(buf);
46          } else if (msg instanceof StompContentSubframe) {
47              StompContentSubframe stompContentSubframe = (StompContentSubframe) msg;
48              ByteBuf buf = encodeContent(stompContentSubframe, ctx);
49              out.add(buf);
50          }
51      }
52  
53      private static ByteBuf encodeContent(StompContentSubframe content, ChannelHandlerContext ctx) {
54          if (content instanceof LastStompContentSubframe) {
55              ByteBuf buf = ctx.alloc().buffer(content.content().readableBytes() + 1);
56              buf.writeBytes(content.content());
57              buf.writeByte(StompConstants.NUL);
58              return buf;
59          } else {
60              return content.content().retain();
61          }
62      }
63  
64      private static ByteBuf encodeFrame(StompHeadersSubframe frame, ChannelHandlerContext ctx) {
65          ByteBuf buf = ctx.alloc().buffer();
66  
67          buf.writeCharSequence(frame.command().toString(), CharsetUtil.US_ASCII);
68          buf.writeByte(StompConstants.LF);
69          AsciiHeadersEncoder headersEncoder = new AsciiHeadersEncoder(buf, SeparatorType.COLON, NewlineType.LF);
70          for (Entry<CharSequence, CharSequence> entry : frame.headers()) {
71              headersEncoder.encode(entry);
72          }
73          buf.writeByte(StompConstants.LF);
74          return buf;
75      }
76  }