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