-
Notifications
You must be signed in to change notification settings - Fork 8
/
message_builders.go
306 lines (262 loc) · 8.95 KB
/
message_builders.go
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package gobayeux
import (
"strconv"
"strings"
)
// HandshakeRequestBuilder provides a way to safely and confidently create
// handshake requests to /meta/handshake.
//
// See also: https://docs.cometd.org/current/reference/#_handshake_request
type HandshakeRequestBuilder struct {
// Required fields
version string
supportedConnectionTypes []string
// Optional fields
minimumVersion string
}
// NewHandshakeRequestBuilder provides an easy way to build a Message that can
// be sent as a Handshake Request as documented in
// https://docs.cometd.org/current/reference/#_handshake_request
func NewHandshakeRequestBuilder() *HandshakeRequestBuilder {
return &HandshakeRequestBuilder{
supportedConnectionTypes: make([]string, 0),
}
}
// AddSupportedConnectionType accepts a string and will add it to the list of
// supported connection types for the /meta/handshake request. It validates
// the connection type. You're encouraged to use one of the constants created
// for these different connection types.
// This will de-duplicate connection types and returns an error if an invalid
// connection type was provided.
func (b *HandshakeRequestBuilder) AddSupportedConnectionType(connectionType string) error {
switch connectionType {
case ConnectionTypeCallbackPolling, ConnectionTypeLongPolling, ConnectionTypeIFrame:
for _, ct := range b.supportedConnectionTypes {
if ct == connectionType {
return nil
}
}
b.supportedConnectionTypes = append(b.supportedConnectionTypes, connectionType)
default:
return BadConnectionTypeError{connectionType}
}
return nil
}
// AddVersion accepts the version of the Bayeux protocol that the client
// supports.
func (b *HandshakeRequestBuilder) AddVersion(version string) error {
if err := validateVersion(version); err != nil {
return err
}
b.version = version
return nil
}
// AddMinimumVersion adds the minimum supported version
func (b *HandshakeRequestBuilder) AddMinimumVersion(version string) error {
if err := validateVersion(version); err != nil {
return err
}
b.minimumVersion = version
return nil
}
// Build generates the final Message to be sent as a Handshake Request
func (b *HandshakeRequestBuilder) Build() ([]Message, error) {
if len(b.supportedConnectionTypes) < 1 {
return nil, ErrNoSupportedConnectionTypes
}
if len(b.version) == 0 {
return nil, ErrNoVersion
}
m := Message{
Channel: MetaHandshake,
Version: b.version,
SupportedConnectionTypes: b.supportedConnectionTypes,
}
if len(b.minimumVersion) > 0 {
m.MinimumVersion = b.minimumVersion
}
// TODO After we've added methods for id, ext, and minimumVersion, update
// those values in the struct here as well
return []Message{m}, nil
}
// ConnectRequestBuilder provides a way to safely build a Message that can be
// sent as a /meta/connect request as documented in
// https://docs.cometd.org/current/reference/#_connect_request
type ConnectRequestBuilder struct {
clientID string
connectionType string
}
// NewConnectRequestBuilder initializes a ConnectRequestBuilder as an easy way
// to build a Message that can be sent as a /meta/connect request.
//
// See also: https://docs.cometd.org/current/reference/#_connect_request
func NewConnectRequestBuilder() *ConnectRequestBuilder {
return &ConnectRequestBuilder{}
}
// AddClientID adds the previously provided clientId to the request
func (b *ConnectRequestBuilder) AddClientID(clientID string) {
b.clientID = clientID
}
// AddConnectionType adds the connection type used by the client for the
// purposes of this connection to the request
func (b *ConnectRequestBuilder) AddConnectionType(connectionType string) error {
switch connectionType {
case ConnectionTypeCallbackPolling, ConnectionTypeLongPolling, ConnectionTypeIFrame:
b.connectionType = connectionType
default:
return BadConnectionTypeError{connectionType}
}
return nil
}
// TODO Add methods for id and ext
// Build generates the final Message to be sent as a Connect Request
func (b *ConnectRequestBuilder) Build() ([]Message, error) {
if b.clientID == "" {
return nil, ErrMissingClientID
}
if b.connectionType == "" {
return nil, ErrMissingConnectionType
}
m := Message{
Channel: MetaConnect,
ClientID: b.clientID,
ConnectionType: b.connectionType,
}
// TODO After we've added methods for id and ext, update
// those values in the struct here as well
return []Message{m}, nil
}
// SubscribeRequestBuilder provides an easy way to build a /meta/subscribe
// request per the specification in
// https://docs.cometd.org/current/reference/#_subscribe_request
type SubscribeRequestBuilder struct {
clientID string
subscription []Channel
}
// NewSubscribeRequestBuilder initializes a SubscribeRequestBuilder as an easy
// way to build a Message that can be sent as a /meta/subscribe request. See
// also https://docs.cometd.org/current/reference/#_subscribe_request
func NewSubscribeRequestBuilder() *SubscribeRequestBuilder {
return &SubscribeRequestBuilder{subscription: make([]Channel, 0)}
}
// AddClientID adds the previously provided clientId to the request
func (b *SubscribeRequestBuilder) AddClientID(clientID string) {
b.clientID = clientID
}
// AddSubscription adds a given channel to the list of subscriptions being
// sent in a /meta/subscribe request
func (b *SubscribeRequestBuilder) AddSubscription(c Channel) error {
if !c.IsValid() {
return InvalidChannelError{c}
}
for _, s := range b.subscription {
if s == c {
return nil
}
}
b.subscription = append(b.subscription, c)
return nil
}
// Build generates the final Message to be sent as a Subscribe Request
func (b *SubscribeRequestBuilder) Build() ([]Message, error) {
if b.clientID == "" {
return nil, ErrMissingClientID
}
if len(b.subscription) < 1 {
return nil, EmptySliceError("subscriptions")
}
ms := make([]Message, len(b.subscription))
for i := range b.subscription {
ms[i] = Message{
Channel: MetaSubscribe,
ClientID: b.clientID,
Subscription: b.subscription[i],
}
}
// TODO Add the ext and id fields once we're able to handle them with the
// builder
return ms, nil
}
// UnsubscribeRequestBuilder provides an easy way to build a /meta/unsubscribe
// request per the specification in
// https://docs.cometd.org/current/reference/#_unsubscribe_request
type UnsubscribeRequestBuilder struct {
clientID string
subscription []Channel
}
// NewUnsubscribeRequestBuilder initializes a SubscribeRequestBuilder as an easy
// way to build a Message that can be sent as a /meta/subscribe request. See
// also https://docs.cometd.org/current/reference/#_unsubscribe_request
func NewUnsubscribeRequestBuilder() *UnsubscribeRequestBuilder {
return &UnsubscribeRequestBuilder{subscription: make([]Channel, 0)}
}
// AddClientID adds the previously provided clientId to the request
func (b *UnsubscribeRequestBuilder) AddClientID(clientID string) {
b.clientID = clientID
}
// AddSubscription adds a given channel to the list of subscriptions being
// sent in a /meta/unsubscribe request
func (b *UnsubscribeRequestBuilder) AddSubscription(c Channel) error {
if !c.IsValid() {
return InvalidChannelError{c}
}
for _, s := range b.subscription {
if s == c {
return nil
}
}
b.subscription = append(b.subscription, c)
return nil
}
// Build generates the final Message to be sent as a Unsubscribe Request
func (b *UnsubscribeRequestBuilder) Build() ([]Message, error) {
if b.clientID == "" {
return nil, ErrMissingClientID
}
if len(b.subscription) < 1 {
return nil, EmptySliceError("subscriptions")
}
ms := make([]Message, len(b.subscription))
for i := range b.subscription {
ms[i] = Message{
Channel: MetaUnsubscribe,
ClientID: b.clientID,
Subscription: b.subscription[i],
}
}
// TODO Add the ext and id fields once we're able to handle them with the
// builder
return ms, nil
}
// DisconnectRequestBuilder provides an easy way to build a /meta/disconnect
// request per the specification in
// https://docs.cometd.org/current/reference/#_bayeux_meta_disconnect
type DisconnectRequestBuilder struct {
clientID string
}
// NewDisconnectRequestBuilder initializes a DisconnectRequestBuilder as an
// easy way to build a Message that can be sent as a /meta/disconnect request.
func NewDisconnectRequestBuilder() *DisconnectRequestBuilder {
return &DisconnectRequestBuilder{}
}
// AddClientID adds the previously provided clientId to the request
func (b *DisconnectRequestBuilder) AddClientID(clientID string) {
b.clientID = clientID
}
// Build generates the final Message to be sent as a Disconnect Request
func (b *DisconnectRequestBuilder) Build() ([]Message, error) {
if b.clientID == "" {
return nil, ErrMissingClientID
}
return []Message{{Channel: MetaDisconnect, ClientID: b.clientID}}, nil
}
func validateVersion(version string) error {
if len(version) < 1 {
return BadConnectionVersionError{version}
}
pieces := strings.SplitN(version, ".", 2)
if _, err := strconv.Atoi(pieces[0]); err != nil {
return BadConnectionVersionError{version}
}
return nil
}