View Javadoc
1   /*
2    * Copyright 2020 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    *   https://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.example.stomp.websocket;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
21  import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
22  import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
23  import io.netty.handler.codec.http.websocketx.WebSocketFrame;
24  import io.netty.handler.codec.stomp.LastStompContentSubframe;
25  import io.netty.handler.codec.stomp.StompContentSubframe;
26  import io.netty.handler.codec.stomp.StompFrame;
27  import io.netty.handler.codec.stomp.StompHeaders;
28  import io.netty.handler.codec.stomp.StompHeadersSubframe;
29  import io.netty.handler.codec.stomp.StompSubframe;
30  import io.netty.handler.codec.stomp.StompSubframeEncoder;
31  
32  import java.util.List;
33  
34  public class StompWebSocketFrameEncoder extends StompSubframeEncoder {
35  
36      @Override
37      public void encode(ChannelHandlerContext ctx, StompSubframe msg, List<Object> out) throws Exception {
38          super.encode(ctx, msg, out);
39      }
40  
41      @Override
42      protected WebSocketFrame convertFullFrame(StompFrame original, ByteBuf encoded) {
43          if (isTextFrame(original)) {
44              return new TextWebSocketFrame(encoded);
45          }
46  
47          return new BinaryWebSocketFrame(encoded);
48      }
49  
50      @Override
51      protected WebSocketFrame convertHeadersSubFrame(StompHeadersSubframe original, ByteBuf encoded) {
52          if (isTextFrame(original)) {
53              return new TextWebSocketFrame(false, 0, encoded);
54          }
55  
56          return new BinaryWebSocketFrame(false, 0, encoded);
57      }
58  
59      @Override
60      protected WebSocketFrame convertContentSubFrame(StompContentSubframe original, ByteBuf encoded) {
61          if (original instanceof LastStompContentSubframe) {
62              return new ContinuationWebSocketFrame(true, 0, encoded);
63          }
64  
65          return new ContinuationWebSocketFrame(false, 0, encoded);
66      }
67  
68      private static boolean isTextFrame(StompHeadersSubframe headersSubframe) {
69          String contentType = headersSubframe.headers().getAsString(StompHeaders.CONTENT_TYPE);
70          return contentType != null && (contentType.startsWith("text") || contentType.startsWith("application/json"));
71      }
72  }