]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/DomainObject/oilsResponse.pm
method prettyJSON doesnt exist anymore
[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_REDIRECTED   { return 307 }
59
60 sub STATUS_BADREQUEST   { return 400 }
61 sub STATUS_UNAUTHORIZED { return 401 }
62 sub STATUS_FORBIDDEN            { return 403 }
63 sub STATUS_NOTFOUND             { return 404 }
64 sub STATUS_NOTALLOWED   { return 405 }
65 sub STATUS_TIMEOUT              { return 408 }
66 sub STATUS_EXPFAILED            { return 417 }
67
68 sub STATUS_INTERNALSERVERERROR  { return 500 }
69 sub STATUS_NOTIMPLEMENTED                       { return 501 }
70 sub STATUS_VERSIONNOTSUPPORTED  { return 505 }
71
72 my $log = 'OpenSRF::Utils::Logger';
73
74 sub toString {
75         my $self = shift;
76         return OpenSRF::Utils::JSON->perl2JSON($self);
77 }
78
79 sub new {
80         my $class = shift;
81         $class = ref($class) || $class;
82
83         my $default_status = eval "\$${class}::status";
84         my $default_statusCode = eval "\$${class}::statusCode";
85
86         my %args = (    status => $default_status,
87                         statusCode => $default_statusCode,
88                         @_ );
89
90         return bless( \%args => $class );
91 }
92
93 sub status {
94         my $self = shift;
95         my $val = shift;
96         $self->{status} = $val if (defined $val);
97         return $self->{status};
98 }
99
100 sub statusCode {
101         my $self = shift;
102         my $val = shift;
103         $self->{statusCode} = $val if (defined $val);
104         return $self->{statusCode};
105 }
106
107 #-------------------------------------------------------------------------------
108
109 package OpenSRF::DomainObject::oilsStatus;
110 use OpenSRF::DomainObject::oilsResponse qw/:status/;
111 use base 'OpenSRF::DomainObject::oilsResponse';
112 use vars qw/$status $statusCode/;
113 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfStatus', name => 'OpenSRF::DomainObject::oilsStatus', type => 'hash' );
114
115 =head1 NAME
116
117 OpenSRF::DomainObject::oilsException
118
119 =head1 SYNOPSIS
120
121 use OpenSRF::DomainObject::oilsResponse;
122
123 ...
124
125 # something happens.
126
127 $client->status( OpenSRF::DomainObject::oilsStatus->new );
128
129 =head1 ABSTRACT
130
131 The base class for Status messages sent between client and server.  This
132 is implemented on top of the C<OpenSRF::DomainObject::oilsResponse> class, and 
133 sets the default B<status> to C<Status> and B<statusCode> to C<STATUS_OK>.
134
135 =cut
136
137 $status = 'Status';
138 $statusCode = STATUS_OK;
139
140 #-------------------------------------------------------------------------------
141
142 package OpenSRF::DomainObject::oilsConnectStatus;
143 use OpenSRF::DomainObject::oilsResponse qw/:status/;
144 use base 'OpenSRF::DomainObject::oilsStatus';
145 use vars qw/$status $statusCode/;
146 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfConnectStatus', name => 'OpenSRF::DomainObject::oilsConnectStatus', type => 'hash' );
147
148 =head1 NAME
149
150 OpenSRF::DomainObject::oilsConnectStatus
151
152 =head1 SYNOPSIS
153
154 use OpenSRF::DomainObject::oilsResponse;
155
156 ...
157
158 # something happens.
159
160 $client->status( new OpenSRF::DomainObject::oilsConnectStatus );
161
162 =head1 ABSTRACT
163
164 The class for Stati relating to the connection status of a session.  This
165 is implemented on top of the C<OpenSRF::DomainObject::oilsStatus> class, and 
166 sets the default B<status> to C<Connection Successful> and B<statusCode> to C<STATUS_OK>.
167
168 =head1 SEE ALSO
169
170 B<OpenSRF::DomainObject::oilsStatus>
171
172 =cut
173
174 $status = 'Connection Successful';
175 $statusCode = STATUS_OK;
176
177 #-------------------------------------------------------------------------------
178
179 package OpenSRF::DomainObject::oilsContinueStatus;
180 use OpenSRF::DomainObject::oilsResponse qw/:status/;
181 use base 'OpenSRF::DomainObject::oilsStatus';
182 use vars qw/$status $statusCode/;
183 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfContinueStatus', name => 'OpenSRF::DomainObject::oilsContinueStatus', type => 'hash' );
184
185 =head1 NAME
186
187 OpenSRF::DomainObject::oilsContinueStatus
188
189 =head1 SYNOPSIS
190
191 use OpenSRF::DomainObject::oilsResponse;
192
193 ...
194
195 # something happens.
196
197 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
198
199 =head1 ABSTRACT
200
201 Implements the STATUS_CONTINUE message, informing the client that it should
202 continue to wait for a response to its request.
203
204 =head1 SEE ALSO
205
206 B<OpenSRF::DomainObject::oilsStatus>
207
208 =cut
209
210 $status = 'Please hold.  Creating response...';
211 $statusCode = STATUS_CONTINUE;
212
213 1;
214
215 #-------------------------------------------------------------------------------
216
217 package OpenSRF::DomainObject::oilsResult;
218 use OpenSRF::DomainObject::oilsResponse qw/:status/;
219 use base 'OpenSRF::DomainObject::oilsResponse';
220 use vars qw/$status $statusCode/;
221 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfResult', name => 'OpenSRF::DomainObject::oilsResult', type => 'hash' );
222
223
224 $status = 'OK';
225 $statusCode = STATUS_OK;
226
227 =head1 NAME
228
229 OpenSRF::DomainObject::oilsResult
230
231 =head1 SYNOPSIS
232
233 use OpenSRF::DomainObject::oilsResponse;
234
235  .... do stuff, create $object ...
236
237 my $res = OpenSRF::DomainObject::oilsResult->new;
238
239 $res->content($object)
240
241 $session->respond( $res );
242
243 =head1 ABSTRACT
244
245 This is the base class for encapuslating RESULT messages send from the server
246 to a client.  It is a subclass of B<OpenSRF::DomainObject::oilsResponse>, and
247 sets B<status> to C<OK> and B<statusCode> to C<STATUS_OK>.
248
249 =head1 METHODS
250
251 =head2 OpenSRF::DomainObject::oilsMessage->content( [$new_content] )
252
253 =over 4
254
255 Sets or gets the content of the response.  This should be exactly one object
256 of (sub)type domainObject or domainObjectCollection.
257
258 =back
259
260 =cut
261
262 sub content {
263         my $self = shift;
264         my $val = shift;
265
266         $self->{content} = $val if (defined $val);
267         return $self->{content};
268 }
269
270 =head1 SEE ALSO
271
272 B<OpenSRF::DomainObject::oilsResponse>
273
274 =cut
275
276 1;
277
278 #-------------------------------------------------------------------------------
279
280 package OpenSRF::DomainObject::oilsException;
281 use OpenSRF::DomainObject::oilsResponse qw/:status/;
282 use OpenSRF::EX;
283 use base qw/OpenSRF::EX OpenSRF::DomainObject::oilsResponse/;
284 use vars qw/$status $statusCode/;
285 use Error;
286 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfException', name => 'OpenSRF::DomainObject::oilsException', type => 'hash' );
287
288 sub message {
289         my $self = shift;
290         return '<' . $self->statusCode . '>  ' . $self->status;
291 }
292
293 sub new {
294         my $class = shift;
295         return $class->OpenSRF::DomainObject::oilsResponse::new( @_ );
296 }
297
298
299 =head1 NAME
300
301 OpenSRF::DomainObject::oilsException
302
303 =head1 SYNOPSIS
304
305 use OpenSRF::DomainObject::oilsResponse;
306
307 ...
308
309 # something breaks.
310
311 $client->send( 'ERROR', OpenSRF::DomainObject::oilsException->new( status => "ARRRRRRG!" ) );
312
313 =head1 ABSTRACT
314
315 The base class for Exception messages sent between client and server.  This
316 is implemented on top of the C<OpenSRF::DomainObject::oilsResponse> class, and 
317 sets the default B<status> to C<Exception occurred> and B<statusCode> to C<STATUS_BADREQUEST>.
318
319 =cut
320
321 $status = 'Exception occurred';
322 $statusCode = STATUS_INTERNALSERVERERROR;
323
324 #-------------------------------------------------------------------------------
325
326 package OpenSRF::DomainObject::oilsConnectException;
327 use OpenSRF::DomainObject::oilsResponse qw/:status/;
328 use OpenSRF::EX;
329 use base qw/OpenSRF::DomainObject::oilsException OpenSRF::EX::ERROR/;
330 use vars qw/$status $statusCode/;
331 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfConnectException', name => 'OpenSRF::DomainObject::oilsConnectException', type => 'hash' );
332
333 =head1 NAME
334
335 OpenSRF::DomainObject::oilsConnectException
336
337 =head1 SYNOPSIS
338
339 use OpenSRF::DomainObject::oilsResponse;
340
341 ...
342
343 # something breaks while connecting.
344
345 $client->send( 'ERROR', new OpenSRF::DomainObject::oilsConnectException );
346
347 =head1 ABSTRACT
348
349 The class for Exceptions that occur durring the B<CONNECT> phase of a session.  This
350 is implemented on top of the C<OpenSRF::DomainObject::oilsException> class, and 
351 sets the default B<status> to C<Connect Request Failed> and B<statusCode> to C<STATUS_FORBIDDEN>.
352
353 =head1 SEE ALSO
354
355 B<OpenSRF::DomainObject::oilsException>
356
357 =cut
358
359
360 $status = 'Connect Request Failed';
361 $statusCode = STATUS_FORBIDDEN;
362
363 #-------------------------------------------------------------------------------
364
365 package OpenSRF::DomainObject::oilsMethodException;
366 use OpenSRF::DomainObject::oilsResponse qw/:status/;
367 use base 'OpenSRF::DomainObject::oilsException';
368 use vars qw/$status $statusCode/;
369 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfMethodException', name => 'OpenSRF::DomainObject::oilsMethodException', type => 'hash' );
370
371 =head1 NAME
372
373 OpenSRF::DomainObject::oilsMethodException
374
375 =head1 SYNOPSIS
376
377 use OpenSRF::DomainObject::oilsResponse;
378
379 ...
380
381 # something breaks while looking up or starting
382 # a method call.
383
384 $client->send( 'ERROR', new OpenSRF::DomainObject::oilsMethodException );
385
386 =head1 ABSTRACT
387
388 The class for Exceptions that occur during the B<CONNECT> phase of a session.  This
389 is implemented on top of the C<OpenSRF::DomainObject::oilsException> class, and 
390 sets the default B<status> to C<Connect Request Failed> and B<statusCode> to C<STATUS_NOTFOUND>.
391
392 =head1 SEE ALSO
393
394 B<OpenSRF::DomainObject::oilsException>
395
396 =cut
397
398
399 $status = 'A server error occurred during method execution';
400 $statusCode = STATUS_INTERNALSERVERERROR;
401
402 # -------------------------------------------
403
404 package OpenSRF::DomainObject::oilsServerError;
405 use OpenSRF::DomainObject::oilsResponse qw/:status/;
406 use base 'OpenSRF::DomainObject::oilsException';
407 use vars qw/$status $statusCode/;
408 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfServerError', name => 'OpenSRF::DomainObject::oilsServerError', type => 'hash' );
409
410 $status = 'Internal Server Error';
411 $statusCode = STATUS_INTERNALSERVERERROR;
412
413 # -------------------------------------------
414
415 package OpenSRF::DomainObject::oilsBrokenSession;
416 use OpenSRF::DomainObject::oilsResponse qw/:status/;
417 use OpenSRF::EX;
418 use base qw/OpenSRF::DomainObject::oilsException OpenSRF::EX::ERROR/;
419 use vars qw/$status $statusCode/;
420 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfBrokenSession', name => 'OpenSRF::DomainObject::oilsBrokenSession', type => 'hash' );
421 $status = "Request on Disconnected Session";
422 $statusCode = STATUS_EXPFAILED;
423
424 #-------------------------------------------------------------------------------
425
426 package OpenSRF::DomainObject::oilsXMLParseError;
427 use OpenSRF::DomainObject::oilsResponse qw/:status/;
428 use OpenSRF::EX;
429 use base qw/OpenSRF::DomainObject::oilsException OpenSRF::EX::ERROR/;
430 use vars qw/$status $statusCode/;
431 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfXMLParseError', name => 'OpenSRF::DomainObject::oilsXMLParseError', type => 'hash' );
432 $status = "XML Parse Error";
433 $statusCode = STATUS_EXPFAILED;
434
435 #-------------------------------------------------------------------------------
436
437 package OpenSRF::DomainObject::oilsAuthException;
438 use OpenSRF::DomainObject::oilsResponse qw/:status/;
439 use OpenSRF::EX;
440 use base qw/OpenSRF::DomainObject::oilsException OpenSRF::EX::ERROR/;
441 OpenSRF::Utils::JSON->register_class_hint( hint => 'osrfAuthException', name => 'OpenSRF::DomainObject::oilsAuthException', type => 'hash' );
442 use vars qw/$status $statusCode/;
443 $status = "Authentication Failure";
444 $statusCode = STATUS_FORBIDDEN;
445
446 1;