]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/DomainObject/oilsResponse.pm
LP#1612771: bundling and chunking
[OpenSRF.git] / src / perl / lib / OpenSRF / DomainObject / oilsResponse.pm
1 package OpenSRF::DomainObject::oilsResponse;
2 use vars qw/@EXPORT_OK %EXPORT_TAGS/;
3 use Exporter;
4 use OpenSRF::Utils::JSON;
5 use base qw/Exporter/;
6 use OpenSRF::Utils::Logger qw/:level/;
7
8 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfResponse', name => 'OpenSRF::DomainObject::oilsResponse', type => 'hash' );
9
10 BEGIN {
11 @EXPORT_OK = qw/STATUS_CONTINUE STATUS_OK STATUS_ACCEPTED
12                                         STATUS_BADREQUEST STATUS_UNAUTHORIZED STATUS_FORBIDDEN
13                                         STATUS_NOTFOUND STATUS_NOTALLOWED STATUS_TIMEOUT
14                                         STATUS_INTERNALSERVERERROR STATUS_NOTIMPLEMENTED
15                                         STATUS_VERSIONNOTSUPPORTED STATUS_REDIRECTED 
16                                         STATUS_EXPFAILED STATUS_COMPLETE/;
17
18 %EXPORT_TAGS = (
19         status => [ qw/STATUS_CONTINUE STATUS_OK STATUS_ACCEPTED
20                                         STATUS_BADREQUEST STATUS_UNAUTHORIZED STATUS_FORBIDDEN
21                                         STATUS_NOTFOUND STATUS_NOTALLOWED STATUS_TIMEOUT
22                                         STATUS_INTERNALSERVERERROR STATUS_NOTIMPLEMENTED
23                                         STATUS_VERSIONNOTSUPPORTED STATUS_REDIRECTED 
24                                         STATUS_EXPFAILED STATUS_COMPLETE/ ],
25 );
26
27 }
28
29 =head1 NAME
30
31 OpenSRF::DomainObject::oilsResponse
32
33 =head1 SYNOPSIS
34
35 use OpenSRF::DomainObject::oilsResponse qw/:status/;
36
37 my $resp = OpenSRF::DomainObject::oilsResponse->new;
38
39 $resp->status( 'a status message' );
40
41 $resp->statusCode( STATUS_CONTINUE );
42
43 $client->respond( $resp );
44
45 =head1 ABSTRACT
46
47 OpenSRF::DomainObject::oilsResponse implements the base class for all Application
48 layer messages send between the client and server.
49
50 =cut
51
52 sub STATUS_CONTINUE             { return 100 }
53
54 sub STATUS_OK                   { return 200 }
55 sub STATUS_ACCEPTED             { return 202 }
56 sub STATUS_COMPLETE             { return 205 }
57
58 sub STATUS_PARTIAL              { return 206 }
59 sub STATUS_NOCONTENT    { return 204 }
60
61 sub STATUS_REDIRECTED   { return 307 }
62
63 sub STATUS_BADREQUEST   { return 400 }
64 sub STATUS_UNAUTHORIZED { return 401 }
65 sub STATUS_FORBIDDEN            { return 403 }
66 sub STATUS_NOTFOUND             { return 404 }
67 sub STATUS_NOTALLOWED   { return 405 }
68 sub STATUS_TIMEOUT              { return 408 }
69 sub STATUS_EXPFAILED            { return 417 }
70
71 sub STATUS_INTERNALSERVERERROR  { return 500 }
72 sub STATUS_NOTIMPLEMENTED                       { return 501 }
73 sub STATUS_VERSIONNOTSUPPORTED  { return 505 }
74
75 my $log = 'OpenSRF::Utils::Logger';
76
77 sub toString {
78         my $self = shift;
79         return OpenSRF::Utils::JSON->perl2JSON($self);
80 }
81
82 sub new {
83         my $class = shift;
84         $class = ref($class) || $class;
85
86         my $default_status = eval "\$${class}::status";
87         my $default_statusCode = eval "\$${class}::statusCode";
88
89         my %args = (    status => $default_status,
90                         statusCode => $default_statusCode,
91                         @_ );
92
93         return bless( \%args => $class );
94 }
95
96 sub status {
97         my $self = shift;
98         my $val = shift;
99         $self->{status} = $val if (defined $val);
100         return $self->{status};
101 }
102
103 sub statusCode {
104         my $self = shift;
105         my $val = shift;
106         $self->{statusCode} = $val if (defined $val);
107         return $self->{statusCode};
108 }
109
110 #-------------------------------------------------------------------------------
111
112 package OpenSRF::DomainObject::oilsStatus;
113 use OpenSRF::DomainObject::oilsResponse qw/:status/;
114 use base 'OpenSRF::DomainObject::oilsResponse';
115 use vars qw/$status $statusCode/;
116 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfStatus', name => 'OpenSRF::DomainObject::oilsStatus', type => 'hash' );
117
118 =head1 NAME
119
120 OpenSRF::DomainObject::oilsException
121
122 =head1 SYNOPSIS
123
124 use OpenSRF::DomainObject::oilsResponse;
125
126 ...
127
128 # something happens.
129
130 $client->status( OpenSRF::DomainObject::oilsStatus->new );
131
132 =head1 ABSTRACT
133
134 The base class for Status messages sent between client and server.  This
135 is implemented on top of the C<OpenSRF::DomainObject::oilsResponse> class, and 
136 sets the default B<status> to C<Status> and B<statusCode> to C<STATUS_OK>.
137
138 =cut
139
140 $status = 'Status';
141 $statusCode = STATUS_OK;
142
143 #-------------------------------------------------------------------------------
144
145 package OpenSRF::DomainObject::oilsConnectStatus;
146 use OpenSRF::DomainObject::oilsResponse qw/:status/;
147 use base 'OpenSRF::DomainObject::oilsStatus';
148 use vars qw/$status $statusCode/;
149 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfConnectStatus', name => 'OpenSRF::DomainObject::oilsConnectStatus', type => 'hash' );
150
151 =head1 NAME
152
153 OpenSRF::DomainObject::oilsConnectStatus
154
155 =head1 SYNOPSIS
156
157 use OpenSRF::DomainObject::oilsResponse;
158
159 ...
160
161 # something happens.
162
163 $client->status( new OpenSRF::DomainObject::oilsConnectStatus );
164
165 =head1 ABSTRACT
166
167 The class for Stati relating to the connection status of a session.  This
168 is implemented on top of the C<OpenSRF::DomainObject::oilsStatus> class, and 
169 sets the default B<status> to C<Connection Successful> and B<statusCode> to C<STATUS_OK>.
170
171 =head1 SEE ALSO
172
173 B<OpenSRF::DomainObject::oilsStatus>
174
175 =cut
176
177 $status = 'Connection Successful';
178 $statusCode = STATUS_OK;
179
180 #-------------------------------------------------------------------------------
181
182 package OpenSRF::DomainObject::oilsContinueStatus;
183 use OpenSRF::DomainObject::oilsResponse qw/:status/;
184 use base 'OpenSRF::DomainObject::oilsStatus';
185 use vars qw/$status $statusCode/;
186 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfContinueStatus', name => 'OpenSRF::DomainObject::oilsContinueStatus', type => 'hash' );
187
188 =head1 NAME
189
190 OpenSRF::DomainObject::oilsContinueStatus
191
192 =head1 SYNOPSIS
193
194 use OpenSRF::DomainObject::oilsResponse;
195
196 ...
197
198 # something happens.
199
200 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
201
202 =head1 ABSTRACT
203
204 Implements the STATUS_CONTINUE message, informing the client that it should
205 continue to wait for a response to its request.
206
207 =head1 SEE ALSO
208
209 B<OpenSRF::DomainObject::oilsStatus>
210
211 =cut
212
213 $status = 'Please hold.  Creating response...';
214 $statusCode = STATUS_CONTINUE;
215
216 1;
217
218 #-------------------------------------------------------------------------------
219
220 package OpenSRF::DomainObject::oilsResult;
221 use OpenSRF::DomainObject::oilsResponse qw/:status/;
222 use base 'OpenSRF::DomainObject::oilsResponse';
223 use vars qw/$status $statusCode/;
224 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfResult', name => 'OpenSRF::DomainObject::oilsResult', type => 'hash' );
225
226
227 $status = 'OK';
228 $statusCode = STATUS_OK;
229
230 =head1 NAME
231
232 OpenSRF::DomainObject::oilsResult
233
234 =head1 SYNOPSIS
235
236 use OpenSRF::DomainObject::oilsResponse;
237
238  .... do stuff, create $object ...
239
240 my $res = OpenSRF::DomainObject::oilsResult->new;
241
242 $res->content($object)
243
244 $session->respond( $res );
245
246 =head1 ABSTRACT
247
248 This is the base class for encapuslating RESULT messages send from the server
249 to a client.  It is a subclass of B<OpenSRF::DomainObject::oilsResponse>, and
250 sets B<status> to C<OK> and B<statusCode> to C<STATUS_OK>.
251
252 =head1 METHODS
253
254 =head2 OpenSRF::DomainObject::oilsMessage->content( [$new_content] )
255
256 =over 4
257
258 Sets or gets the content of the response.  This should be exactly one object
259 of (sub)type domainObject or domainObjectCollection.
260
261 =back
262
263 =cut
264
265 sub content {
266         my $self = shift;
267         my $val = shift;
268
269         $self->{content} = $val if (defined $val);
270         return $self->{content};
271 }
272
273 =head1 SEE ALSO
274
275 B<OpenSRF::DomainObject::oilsResponse>
276
277 =cut
278
279 1;
280
281 #-------------------------------------------------------------------------------
282
283 package OpenSRF::DomainObject::oilsResult::Partial;
284 use OpenSRF::DomainObject::oilsResponse qw/:status/;
285 use base 'OpenSRF::DomainObject::oilsResult';
286 use vars qw/$status $statusCode/;
287 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfResult', name => 'OpenSRF::DomainObject::oilsResult::Partial', type => 'hash' );
288
289
290 $status = 'Partial Response';
291 $statusCode = STATUS_PARTIAL;
292
293 =head1 NAME
294
295 OpenSRF::DomainObject::oilsResult::Partial
296
297 =head1 SYNOPSIS
298
299 This class is used internally to break apart large OpenSRF messages into small
300 chunks, to reduce the maximum possible stanza size when sending a message over
301 XMPP.
302
303 =cut
304
305 sub content {
306         my $self = shift;
307         my $val = shift;
308
309         $self->{content} = $val if (defined $val);
310         return $self->{content};
311 }
312
313 =head1 SEE ALSO
314
315 B<OpenSRF::DomainObject::oilsResponse>
316
317 =cut
318
319 1;
320
321 #-------------------------------------------------------------------------------
322
323 package OpenSRF::DomainObject::oilsResult::PartialComplete;
324 use OpenSRF::DomainObject::oilsResponse qw/:status/;
325 use base 'OpenSRF::DomainObject::oilsResult';
326 use vars qw/$status $statusCode/;
327 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfResult', name => 'OpenSRF::DomainObject::oilsResult::Partial', type => 'hash' );
328
329
330 $status = 'Partial Response Finalized';
331 $statusCode = STATUS_NOCONTENT;
332
333 =head1 NAME
334
335 OpenSRF::DomainObject::oilsResult::Partial
336
337 =head1 SYNOPSIS
338
339 This class is used internally to mark the end of a stream of small partial
340 OpenSRF messages of type OpenSRF::DomainObject::oilsResult::Partial.
341
342 =cut
343
344 sub content {
345         my $self = shift;
346         my $val = shift;
347
348         $self->{content} = $val if (defined $val);
349         return $self->{content};
350 }
351
352 =head1 SEE ALSO
353
354 B<OpenSRF::DomainObject::oilsResponse>
355
356 =cut
357
358 1;
359
360 #-------------------------------------------------------------------------------
361
362 package OpenSRF::DomainObject::oilsException;
363 use OpenSRF::DomainObject::oilsResponse qw/:status/;
364 use OpenSRF::EX;
365 use base qw/OpenSRF::EX OpenSRF::DomainObject::oilsResponse/;
366 use vars qw/$status $statusCode/;
367 use Error;
368 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfException', name => 'OpenSRF::DomainObject::oilsException', type => 'hash' );
369
370 sub message {
371         my $self = shift;
372         return '<' . $self->statusCode . '>  ' . $self->status;
373 }
374
375 sub new {
376         my $class = shift;
377         return $class->OpenSRF::DomainObject::oilsResponse::new( @_ );
378 }
379
380
381 =head1 NAME
382
383 OpenSRF::DomainObject::oilsException
384
385 =head1 SYNOPSIS
386
387 use OpenSRF::DomainObject::oilsResponse;
388
389 ...
390
391 # something breaks.
392
393 $client->send( 'ERROR', OpenSRF::DomainObject::oilsException->new( status => "ARRRRRRG!" ) );
394
395 =head1 ABSTRACT
396
397 The base class for Exception messages sent between client and server.  This
398 is implemented on top of the C<OpenSRF::DomainObject::oilsResponse> class, and 
399 sets the default B<status> to C<Exception occurred> and B<statusCode> to C<STATUS_BADREQUEST>.
400
401 =cut
402
403 $status = 'Exception occurred';
404 $statusCode = STATUS_INTERNALSERVERERROR;
405
406 #-------------------------------------------------------------------------------
407
408 package OpenSRF::DomainObject::oilsConnectException;
409 use OpenSRF::DomainObject::oilsResponse qw/:status/;
410 use OpenSRF::EX;
411 use base qw/OpenSRF::DomainObject::oilsException OpenSRF::EX::ERROR/;
412 use vars qw/$status $statusCode/;
413 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfConnectException', name => 'OpenSRF::DomainObject::oilsConnectException', type => 'hash' );
414
415 =head1 NAME
416
417 OpenSRF::DomainObject::oilsConnectException
418
419 =head1 SYNOPSIS
420
421 use OpenSRF::DomainObject::oilsResponse;
422
423 ...
424
425 # something breaks while connecting.
426
427 $client->send( 'ERROR', new OpenSRF::DomainObject::oilsConnectException );
428
429 =head1 ABSTRACT
430
431 The class for Exceptions that occur durring the B<CONNECT> phase of a session.  This
432 is implemented on top of the C<OpenSRF::DomainObject::oilsException> class, and 
433 sets the default B<status> to C<Connect Request Failed> and B<statusCode> to C<STATUS_FORBIDDEN>.
434
435 =head1 SEE ALSO
436
437 B<OpenSRF::DomainObject::oilsException>
438
439 =cut
440
441
442 $status = 'Connect Request Failed';
443 $statusCode = STATUS_FORBIDDEN;
444
445 #-------------------------------------------------------------------------------
446
447 package OpenSRF::DomainObject::oilsMethodException;
448 use OpenSRF::DomainObject::oilsResponse qw/:status/;
449 use base 'OpenSRF::DomainObject::oilsException';
450 use vars qw/$status $statusCode/;
451 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfMethodException', name => 'OpenSRF::DomainObject::oilsMethodException', type => 'hash' );
452
453 =head1 NAME
454
455 OpenSRF::DomainObject::oilsMethodException
456
457 =head1 SYNOPSIS
458
459 use OpenSRF::DomainObject::oilsResponse;
460
461 ...
462
463 # something breaks while looking up or starting
464 # a method call.
465
466 $client->send( 'ERROR', new OpenSRF::DomainObject::oilsMethodException );
467
468 =head1 ABSTRACT
469
470 The class for Exceptions that occur during the B<CONNECT> phase of a session.  This
471 is implemented on top of the C<OpenSRF::DomainObject::oilsException> class, and 
472 sets the default B<status> to C<Connect Request Failed> and B<statusCode> to C<STATUS_NOTFOUND>.
473
474 =head1 SEE ALSO
475
476 B<OpenSRF::DomainObject::oilsException>
477
478 =cut
479
480
481 $status = 'A server error occurred during method execution';
482 $statusCode = STATUS_INTERNALSERVERERROR;
483
484 # -------------------------------------------
485
486 package OpenSRF::DomainObject::oilsServerError;
487 use OpenSRF::DomainObject::oilsResponse qw/:status/;
488 use base 'OpenSRF::DomainObject::oilsException';
489 use vars qw/$status $statusCode/;
490 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfServerError', name => 'OpenSRF::DomainObject::oilsServerError', type => 'hash' );
491
492 $status = 'Internal Server Error';
493 $statusCode = STATUS_INTERNALSERVERERROR;
494
495 # -------------------------------------------
496
497 package OpenSRF::DomainObject::oilsBrokenSession;
498 use OpenSRF::DomainObject::oilsResponse qw/:status/;
499 use OpenSRF::EX;
500 use base qw/OpenSRF::DomainObject::oilsException OpenSRF::EX::ERROR/;
501 use vars qw/$status $statusCode/;
502 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfBrokenSession', name => 'OpenSRF::DomainObject::oilsBrokenSession', type => 'hash' );
503 $status = "Request on Disconnected Session";
504 $statusCode = STATUS_EXPFAILED;
505
506 #-------------------------------------------------------------------------------
507
508 package OpenSRF::DomainObject::oilsXMLParseError;
509 use OpenSRF::DomainObject::oilsResponse qw/:status/;
510 use OpenSRF::EX;
511 use base qw/OpenSRF::DomainObject::oilsException OpenSRF::EX::ERROR/;
512 use vars qw/$status $statusCode/;
513 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfXMLParseError', name => 'OpenSRF::DomainObject::oilsXMLParseError', type => 'hash' );
514 $status = "XML Parse Error";
515 $statusCode = STATUS_EXPFAILED;
516
517 #-------------------------------------------------------------------------------
518
519 package OpenSRF::DomainObject::oilsAuthException;
520 use OpenSRF::DomainObject::oilsResponse qw/:status/;
521 use OpenSRF::EX;
522 use base qw/OpenSRF::DomainObject::oilsException OpenSRF::EX::ERROR/;
523 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfAuthException', name => 'OpenSRF::DomainObject::oilsAuthException', type => 'hash' );
524 use vars qw/$status $statusCode/;
525 $status = "Authentication Failure";
526 $statusCode = STATUS_FORBIDDEN;
527
528 1;