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