1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36 public class RtspEncoder extends HttpObjectEncoder<HttpMessage> {
37
38
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 }