-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathBackendMessageDecoder.java
210 lines (184 loc) · 7.07 KB
/
BackendMessageDecoder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.r2dbc.postgresql.message.backend;
import io.netty.buffer.ByteBuf;
import io.r2dbc.postgresql.util.Assert;
import reactor.core.publisher.Flux;
/**
* A decoder that reads {@link ByteBuf}s and returns a {@link Flux} of decoded {@link BackendMessage}s.
*/
public final class BackendMessageDecoder {
/**
* Decode a {@link ByteBuf} into a {@link BackendMessage}.
*
* @param envelope the {@link ByteBuf} to decode
* @return a {@link BackendMessage}.
*/
public static BackendMessage decode(ByteBuf envelope) {
Assert.requireNonNull(envelope, "in must not be null");
MessageType messageType = MessageType.valueOf(envelope.readByte());
envelope.skipBytes(4);
return decodeBody(envelope, messageType);
}
private static BackendMessage decodeBody(ByteBuf body, MessageType messageType) {
switch (messageType) {
case AUTHENTICATION:
return decodeAuthentication(body);
case BACKEND_KEY_DATA:
return BackendKeyData.decode(body);
case BIND_COMPLETE:
return BindComplete.INSTANCE;
case CLOSE_COMPLETE:
return CloseComplete.INSTANCE;
case COMMAND_COMPLETE:
return CommandComplete.decode(body);
case COPY_DATA:
return CopyData.decode(body);
case COPY_DONE:
return CopyDone.INSTANCE;
case COPY_BOTH_RESPONSE:
return CopyBothResponse.decode(body);
case COPY_IN_RESPONSE:
return CopyInResponse.decode(body);
case COPY_OUT_RESPONSE:
return CopyOutResponse.decode(body);
case DATA_ROW:
return DataRow.decode(body);
case EMPTY_QUERY_RESPONSE:
return EmptyQueryResponse.INSTANCE;
case ERROR_RESPONSE:
return ErrorResponse.decode(body);
case FUNCTION_CALL_RESPONSE:
return FunctionCallResponse.decode(body);
case NO_DATA:
return NoData.INSTANCE;
case NOTICE_RESPONSE:
return NoticeResponse.decode(body);
case NOTIFICATION_RESPONSE:
return NotificationResponse.decode(body);
case PARAMETER_DESCRIPTION:
return ParameterDescription.decode(body);
case PARAMETER_STATUS:
return ParameterStatus.decode(body);
case PARSE_COMPLETE:
return ParseComplete.INSTANCE;
case PORTAL_SUSPENDED:
return PortalSuspended.INSTANCE;
case READY_FOR_QUERY:
return ReadyForQuery.decode(body);
case ROW_DESCRIPTION:
return RowDescription.decode(body);
default:
throw new IllegalArgumentException(String.format("%s is not a supported message type", messageType));
}
}
private static BackendMessage decodeAuthentication(ByteBuf in) {
AuthenticationType authenticationType = AuthenticationType.valueOf(in.readInt());
switch (authenticationType) {
case OK:
return AuthenticationOk.INSTANCE;
case KERBEROS_V5:
return AuthenticationKerberosV5.INSTANCE;
case CLEARTEXT_PASSWORD:
return AuthenticationCleartextPassword.INSTANCE;
case GSS:
return AuthenticationGSS.INSTANCE;
case GSS_CONTINUE:
return AuthenticationGSSContinue.decode(in);
case MD5_PASSWORD:
return AuthenticationMD5Password.decode(in);
case SCMC_CREDENTIAL:
return AuthenticationSCMCredential.INSTANCE;
case SASL:
return AuthenticationSASL.decode(in);
case SASL_CONTINUE:
return AuthenticationSASLContinue.decode(in);
case SASL_FINAL:
return AuthenticationSASLFinal.decode(in);
case SSPI:
return AuthenticationSSPI.INSTANCE;
default:
throw new IllegalArgumentException(String.format("%s is not a supported authentication type", authenticationType));
}
}
private enum AuthenticationType {
OK(0),
KERBEROS_V5(2),
CLEARTEXT_PASSWORD(3),
GSS(7),
GSS_CONTINUE(8),
MD5_PASSWORD(5),
SCMC_CREDENTIAL(6),
SASL(10),
SASL_CONTINUE(11),
SASL_FINAL(12),
SSPI(9);
private final int discriminator;
AuthenticationType(int discriminator) {
this.discriminator = discriminator;
}
static AuthenticationType valueOf(int i) {
for (AuthenticationType authType : values()) {
if (authType.discriminator == i) {
return authType;
}
}
throw new IllegalArgumentException(String.format("%d is not a valid authentication type", i));
}
}
private enum MessageType {
AUTHENTICATION('R'),
BACKEND_KEY_DATA('K'),
BIND_COMPLETE('2'),
CLOSE_COMPLETE('3'),
COMMAND_COMPLETE('C'),
COPY_BOTH_RESPONSE('W'),
COPY_DATA('d'),
COPY_DONE('c'),
COPY_IN_RESPONSE('G'),
COPY_OUT_RESPONSE('H'),
DATA_ROW('D'),
EMPTY_QUERY_RESPONSE('I'),
ERROR_RESPONSE('E'),
FUNCTION_CALL_RESPONSE('V'),
NO_DATA('n'),
NOTICE_RESPONSE('N'),
NOTIFICATION_RESPONSE('A'),
PARAMETER_DESCRIPTION('t'),
PARAMETER_STATUS('S'),
PARSE_COMPLETE('1'),
PORTAL_SUSPENDED('s'),
READY_FOR_QUERY('Z'),
ROW_DESCRIPTION('T');
private static final MessageType CACHE[] = new MessageType[Math.abs(Byte.MIN_VALUE) + Byte.MAX_VALUE];
static {
for (MessageType messageType : values()) {
CACHE[Math.abs(Byte.MIN_VALUE) + (byte) messageType.discriminator] = messageType;
}
}
MessageType(char discriminator) {
this.discriminator = discriminator;
}
private final char discriminator;
static MessageType valueOf(byte b) {
MessageType messageType = CACHE[Math.abs(Byte.MIN_VALUE) + b];
if (messageType == null) {
throw new IllegalArgumentException(String.format("%c is not a valid message type", b));
}
return messageType;
}
}
}