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 static io.netty.handler.codec.http.HttpConstants.CR;
19  import static io.netty.handler.codec.http.HttpConstants.LF;
20  import static io.netty.handler.codec.http.HttpConstants.SP;
21  import io.netty.buffer.ByteBuf;
22  import io.netty.handler.codec.UnsupportedMessageTypeException;
23  import io.netty.handler.codec.http.HttpContent;
24  import io.netty.handler.codec.http.HttpHeaders;
25  import io.netty.handler.codec.http.HttpMessage;
26  import io.netty.handler.codec.http.HttpObjectEncoder;
27  import io.netty.handler.codec.http.HttpRequest;
28  import io.netty.handler.codec.http.HttpResponse;
29  import io.netty.util.CharsetUtil;
30  import io.netty.util.internal.StringUtil;
31  
32  /**
33   * Encodes an RTSP message represented in {@link HttpMessage} or an {@link HttpContent} into
34   * a {@link ByteBuf}.
35   */
36  public class RtspEncoder extends HttpObjectEncoder<HttpMessage> {
37      /**
38       * Constant for CRLF.
39       */
40      private static final byte[] CRLF = {CR, LF};
41  
42      @Override
43      public boolean acceptOutboundMessage(final Object msg)
44             throws Exception {
45          return super.acceptOutboundMessage(msg) && ((msg instanceof HttpRequest) || (msg instanceof HttpResponse));
46      }
47  
48      @Override
49      protected void encodeInitialLine(final ByteBuf buf, final HttpMessage message)
50             throws Exception {
51          if (message instanceof HttpRequest) {
52              HttpRequest request = (HttpRequest) message;
53              HttpHeaders.encodeAscii(request.getMethod().toString(), buf);
54              buf.writeByte(SP);
55              buf.writeBytes(request.getUri().getBytes(CharsetUtil.UTF_8));
56              buf.writeByte(SP);
57              HttpHeaders.encodeAscii(request.getProtocolVersion().toString(), buf);
58              buf.writeBytes(CRLF);
59          } else if (message instanceof HttpResponse) {
60              HttpResponse response = (HttpResponse) message;
61              HttpHeaders.encodeAscii(response.getProtocolVersion().toString(),
62                                      buf);
63              buf.writeByte(SP);
64              buf.writeBytes(String.valueOf(response.getStatus().code())
65                                   .getBytes(CharsetUtil.US_ASCII));
66              buf.writeByte(SP);
67              HttpHeaders.encodeAscii(String.valueOf(response.getStatus().reasonPhrase()),
68                                      buf);
69              buf.writeBytes(CRLF);
70          } else {
71              throw new UnsupportedMessageTypeException("Unsupported type "
72                                  + StringUtil.simpleClassName(message));
73          }
74      }
75  }