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