-
Notifications
You must be signed in to change notification settings - Fork 2
/
responses.st
503 lines (418 loc) · 14.4 KB
/
responses.st
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
Namespace current: Shampoo [
Object subclass: Response [
<category: 'Shampoo-Responses'>
<comment: 'I am an abstract class. My subclasses represent various types
of responses and hold all the required information. They also know how to
serialize themselves into a network-transfer-friendly format.'>
| id |
Response class >> id: anInteger [
<category: 'instance creation'>
^(self new)
id: anInteger;
yourself
]
id: anInteger [
<category: 'private'>
id := anInteger
]
type [
<category: 'private'>
^self shouldNotImplement
]
do: aBlock [
"Evaluate a one-argument block, pass self to it.
This method is actually a kludge and it will be probably
removed later"
<category: 'collection'>
^aBlock value: self
]
asXML [
<category: 'marshalling'>
^(ShampooXML.ShNode tagname: 'response')
addAttribute: 'id' value: id asString;
addAttribute: 'type' value: self type;
yourself
]
]
Response subclass: ServerInfoResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances hold information about the server (image). Actually,
the server can send me any time, even without an appropriate request'>
type [
<category: 'private'>
^'Info'
]
asXML [
<category: 'marshalling'>
^(super asXML)
addNode: (ShampooXML.ShText text: Smalltalk version);
yourself
]
]
Response subclass: OperationalResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances are used to hold a success/failure status of
an operation. An operation is usually a result of a request execution.'>
| success message |
OperationalResponse class >> success: anInteger [
<category: 'instance creation'>
^(OperationalResponse id: anInteger)
success: true;
yourself
]
OperationalResponse class >> failure: anInteger [
<category: 'instance creation'>
^(OperationalResponse id: anInteger)
success: false;
yourself
]
OperationalResponse class >> failure: anInteger with: anError [
<category: 'instance creation'>
^self failure: anInteger text: anError messageText
]
OperationalResponse class >> failure: anInteger text: aString [
<category: 'instance creation'>
^(OperationalResponse id: anInteger)
success: false;
message: aString;
yourself
]
statusString [
<category: 'accessors'>
^success ifTrue: ['success'] ifFalse: ['failure']
]
success: aBoolean [
<category: 'private'>
success := aBoolean
]
message: aString [
<category: 'private'>
message := aString
]
type [
<category: 'private'>
^'OperationalResponse'
]
asXML [
<category: 'marshalling'>
| r |
r := super asXML.
r addAttribute: 'status' value: self statusString.
message ifNotNil:
[r addNode: (ShampooXML.ShText text: message)].
^r
]
]
Decorator subclass: CollectionResponseDecorator [
<category: 'Shampoo-Responses'>
<comment: 'I am a decorator, a Smalltalk alternative to the multiple
inheritance. My instances are used to add some additional state and behavior
dynamically to the existing objects. The additions introduced by me could not
be added somewhere in the root of the class (sub)hierarchy, some classes in
the hierarchy should behave like me, and some should not. That''s why I am
here.'>
| items |
items [
<category: 'accessors'>
^items ifNil: [items := Dictionary new]
]
itemsAt: itemTypeString put: anArray [
<category: 'accessors'>
self items at: itemTypeString put: anArray copy
]
asXML [
<category: 'marshalling'>
| root |
root := self underlyingObject asXML.
self items keysAndValuesDo:
[:key :values | values do: [:each |
root addNode: ((ShampooXML.ShNode tagname: key asString)
addNode: (ShampooXML.ShText text: each);
yourself)]].
^root
]
do: aBlock [
"CollectionResponseDecorator usually wraps Response objects.
Response objects partly understand the collection protocol,
especially a #do: message. This wrapper does not proxy this
message to the underlying object."
<category: 'decorating'>
^aBlock value: self
]
]
Response subclass: NamespacesResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances represent a list of namespaces in the image'>
NamespacesResponse class >> id: anInteger names: anArray [
<category: 'instance creation'>
^(CollectionResponseDecorator on: (self id: anInteger))
itemsAt: 'namespace' put: anArray;
yourself
]
type [
<category: 'private'>
^'Namespaces'
]
]
Response subclass: ClassesResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances represent a lists of classes in a concrete
namespace.'>
ClassesResponse class >> id: anInteger names: anArray [
<category: 'instance creation'>
^(CollectionResponseDecorator on: (self id: anInteger))
itemsAt: 'class' put: anArray;
yourself
]
type [
<category: 'private'>
^'Classes'
]
]
Response subclass: ClassInfoResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances hold some generic information about a concrete
class in the concrete namespace. See more in my subclasses.'>
| class attrs |
ClassInfoResponse class >> id: anInteger class: aClass [
<category: 'instance creation'>
^self id: anInteger class: aClass attrs: nil
]
ClassInfoResponse class >> id: anInteger class: aClass attrs: anArray [
<category: 'instance creation'>
^(self id: anInteger)
class: aClass attrs: anArray
yourself
]
class: aClass attrs: anArray [
<category: 'private'>
class := aClass.
anArray isNil ifFalse: [attrs := Dictionary from: anArray].
]
asXML [
<category: 'marshalling'>
| r |
r := super asXML.
r addAttribute: 'class' value: class.
attrs ifNotNil:
[:dict |
dict keysAndValuesDo:
[:key :value | r addAttribute: key value: value]].
^r
]
]
ClassInfoResponse subclass: ClassResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances hold information about a class and its state.'>
ClassResponse class >>
id: anInteger class: aClass superclass: aSuperClass
instvars: anInstVars classvars: aClassVars poolvars: aPoolDicts
category: aString [
<category: 'instance creation'>
| obj |
obj := self
id: anInteger
class: aClass
attrs: {'superclass' -> aSuperClass.
'category' -> aString}.
^(CollectionResponseDecorator on: obj)
itemsAt: 'instvar' put: anInstVars;
itemsAt: 'classvar' put: aClassVars;
itemsAt: 'poolvar' put: aPoolDicts;
yourself
]
type [
<category: 'private'>
^'Class'
]
]
ClassInfoResponse subclass: MethodCategoriesResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances hold a lists of method categories of a concrete class'>
MethodCategoriesResponse class >> id: anInteger class: aClass categories: anArray [
<category: 'instance creation'>
^(CollectionResponseDecorator on: (self id: anInteger class: aClass))
itemsAt: 'category' put: anArray;
yourself
]
type [
<category: 'private'>
^'Categories'
]
]
ClassInfoResponse subclass: MethodsResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances hold a lists of methods in a concrete category
of the concrete class'>
MethodsResponse class >> id: anInteger class: aClass methods: anArray [
<category: 'instance creation'>
^(CollectionResponseDecorator on: (self id: anInteger class: aClass))
itemsAt: 'method' put: anArray;
yourself
]
type [
<category: 'private'>
^'Methods'
]
]
ClassInfoResponse subclass: MethodResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances hold a source code of a concrete method in a
concrete class'>
| methodName methodSource |
MethodResponse class >> id: anInteger class: aClass method: aNameString source: aSourceString [
<category: 'instance creation'>
^(self id: anInteger class: aClass)
method: aNameString methodSource: aSourceString;
yourself
]
method: aNameString methodSource: aSourceString [
<category: 'private'>
methodName := aNameString.
methodSource := PrettyPrinter prettifyMethod: aSourceString
]
type [
<category: 'private'>
^'MethodSource'
]
asXML [
<category: 'marshalling'>
^(super asXML)
addAttribute: 'method' value: methodName;
addNode: (ShampooXML.ShText text: methodSource);
yourself
]
]
Response subclass: PrintItResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances hold an output of a PrintIt request'>
| value |
PrintItResponse class >> id: anInteger value: anObject [
<category: 'instance creation'>
^(self id: anInteger)
value: anObject;
yourself
]
value: anObject [
<category: 'private'>
value := anObject
]
value [
<category: 'accessors'>
^value
]
type [
<category: 'private'>
^'PrintIt'
]
asXML [
<category: 'marshalling'>
^(super asXML)
addNode: (ShampooXML.ShText text: value printString);
yourself
]
]
Response subclass: EchoResponse [
<category: 'Shampoo-Responses'>
<comment: 'My instances hold Transcript outputs. My instances are sent
to all clients when the Transcript have something to print out, there is no
appropriate request.'>
| text |
EchoResponse class >> id: anInteger text: aString [
<category: 'instance creation'>
^(self id: anInteger)
text: aString;
yourself
]
text: aString [
<category: 'private'>
text := aString
]
type [
<category: 'private'>
^'Echo'
]
asXML [
<category: 'marshalling'>
^(super asXML)
addNode: (ShampooXML.ShText text: text);
yourself
]
]
Response subclass: MagicResponse [
<category: 'Shampoo-Responses'>
<comment: 'I am not actually a response, I am like a request that
goes from the server to a client. My instances hold a magic number used by
a client for password encryption during the authentication.'>
| number |
MagicResponse class >> number: anInteger [
<category: 'instance creation'>
^(self id: 0)
number: anInteger;
yourself
]
number: anInteger [
<category: 'private'>
number := anInteger
]
type [
<category: 'private'>
^'Magic'
]
asXML [
<category: 'marshalling'>
^(super asXML)
addNode: (ShampooXML.ShText text: number printString);
yourself
]
]
Response subclass: FileOutResponse [
| isLast xml |
FileOutResponse class >>
id: anInteger class: aClassName source: aString [
<category: 'instance creation'>
^(self id: anInteger)
class: aClassName source: aString;
yourself
]
FileOutResponse class >>
id: anInteger category: aCategory source: aString [
<category: 'instance creation'>
^(self id: anInteger)
category: aCategory source: aString;
yourself
]
class: aClassName source: aString [
<category: 'private'>
xml := super asXML.
xml addAttribute: 'class' value: aClassName;
addNode: (ShampooXML.ShText text: aString)
]
category: aCategory source: aString [
<category: 'private'>
xml := super asXML.
xml addAttribute: 'category' value: aCategory;
addNode: (ShampooXML.ShText text: aString)
]
isLast [
<category: 'accessors'>
^isLast ifNil: [isLast := false]
]
isLast: aBoolean [
<category: 'accessors'>
isLast := aBoolean
]
type [
<category: 'private'>
^'FileOut'
]
asXML [
<category: 'marshalling'>
| r |
r := xml copy.
self isLast ifTrue:
[r addAttribute: 'last' value: 'true'].
^r
]
]
]