View Javadoc
1   /*
2    * Copyright 2012 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.handler.codec.AsciiString;
20  import io.netty.handler.codec.http.FullHttpResponse;
21  import io.netty.handler.codec.http.HttpResponse;
22  
23  import static io.netty.handler.codec.http.HttpConstants.*;
24  
25  /**
26   * Encodes an RTSP response represented in {@link FullHttpResponse} into
27   * a {@link ByteBuf}.
28   */
29  public class RtspResponseEncoder extends RtspObjectEncoder<HttpResponse> {
30      private static final byte[] CRLF = { CR, LF };
31  
32      @Override
33      public boolean acceptOutboundMessage(Object msg) throws Exception {
34          return msg instanceof FullHttpResponse;
35      }
36  
37      @Override
38      protected void encodeInitialLine(ByteBuf buf, HttpResponse response) throws Exception {
39          AsciiString version = response.protocolVersion().text();
40          buf.writeBytes(version.array(), version.arrayOffset(), version.length());
41          buf.writeByte(SP);
42  
43          AsciiString code = response.status().codeAsText();
44          buf.writeBytes(code.array(), code.arrayOffset(), code.length());
45          buf.writeByte(SP);
46  
47          AsciiString reasonPhrase = response.status().reasonPhrase();
48          buf.writeBytes(reasonPhrase.array(), reasonPhrase.arrayOffset(), reasonPhrase.length());
49          buf.writeBytes(CRLF);
50      }
51  }