View Javadoc
1   /*
2    * Copyright 2015 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.handler.codec.rtsp;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufUtil;
20  import io.netty.handler.codec.UnsupportedMessageTypeException;
21  import io.netty.handler.codec.http.HttpContent;
22  import io.netty.handler.codec.http.HttpMessage;
23  import io.netty.handler.codec.http.HttpObjectEncoder;
24  import io.netty.handler.codec.http.HttpRequest;
25  import io.netty.handler.codec.http.HttpResponse;
26  import io.netty.util.CharsetUtil;
27  
28  import static io.netty.handler.codec.http.HttpConstants.*;
29  
30  /**
31   * Encodes an RTSP message represented in {@link HttpMessage} or an {@link HttpContent} into
32   * a {@link ByteBuf}.
33   */
34  public class RtspEncoder extends HttpObjectEncoder<HttpMessage> {
35      private static final int CRLF_SHORT = (CR << 8) | LF;
36  
37      @Override
38      public boolean acceptOutboundMessage(final Object msg)
39             throws Exception {
40          return super.acceptOutboundMessage(msg) && ((msg instanceof HttpRequest) || (msg instanceof HttpResponse));
41      }
42  
43      @Override
44      protected void encodeInitialLine(final ByteBuf buf, final HttpMessage message)
45             throws Exception {
46          if (message instanceof HttpRequest) {
47              HttpRequest request = (HttpRequest) message;
48              ByteBufUtil.copy(request.method().asciiName(), buf);
49              buf.writeByte(SP);
50              buf.writeCharSequence(request.uri(), CharsetUtil.UTF_8);
51              buf.writeByte(SP);
52              buf.writeCharSequence(request.protocolVersion().toString(), CharsetUtil.US_ASCII);
53              ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
54          } else if (message instanceof HttpResponse) {
55              HttpResponse response = (HttpResponse) message;
56              buf.writeCharSequence(response.protocolVersion().toString(), CharsetUtil.US_ASCII);
57              buf.writeByte(SP);
58              ByteBufUtil.copy(response.status().codeAsText(), buf);
59              buf.writeByte(SP);
60              buf.writeCharSequence(response.status().reasonPhrase(), CharsetUtil.US_ASCII);
61              ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
62          } else {
63              throw new UnsupportedMessageTypeException(message, HttpRequest.class, HttpResponse.class);
64          }
65      }
66  }