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