Service/Resource.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Service/Profiles.pm ↑ Summary Apps/Service/Testd.pm »
1 package XAS::Service::Resource;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶use XAS::Factory;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Data::Dumper;
8 use Hash::MultiValue;
9 use parent 'Web::Machine::Resource';
10 use Web::Machine::Util 'create_header';
11  
12 # -------------------------------------------------------------------------
13 # Web::Machine::Resource overrides
14 # ------------------------------------------------------------------------
15  
16 sub init {
17 my $self = shift;
18 my $args = shift;
19  
20 $self->{'tt'} = exists $args->{'template'}
21 ? $args->{'template'}
22 : undef;
23  
24 $self->{'json'} = exists $args->{'json'}
25 ? $args->{'json'}
26 : undef;
27  
28 $self->{'app_name'} = exists $args->{'app_name'}
29 ? $args->{'app_name'}
30 : 'Test App';
31  
32 $self->{'app_description'} = exists $args->{'app_description'}
33 ? $args->{'app_description'}
34 : 'Testing Testing 1 2 3';
35  
36 $self->{'alias'} = exists $args->{'alias'}
37 ? $args->{'alias'}
38 : 'resource';
39  
40 $self->errcode(0);
41 $self->errstr('');
42  
43 $self->{'env'} = XAS::Factory->module('environment');
44 $self->{'log'} = XAS::Factory->module('logger');
45  
46 }
47  
48 sub is_authorized {
49 my $self = shift;
50 my $auth = shift;
51  
52 my $stat = 0;
53  
54 if ($auth) {
55  
56 warn "is_authorized - override this please\n";
57 warn sprintf("username: %s, password: %s\n", $auth->username, $auth->password);
58  
59 $stat = 1;
60  
61 return $stat;
62  
63 }
64  
65 return create_header('WWWAuthenticate' => [ 'Basic' => ( realm => 'XAS Rest' ) ] );
66  
67 }
68  
69 sub options {
70 my $self = shift;
71  
72 my $options;
73 my @accepted;
74 my @provided;
75 my $allowed = $self->allowed_methods;
76  
77 foreach my $hash (@{$self->content_types_accepted}) {
78  
79 my ($key) = keys %$hash;
80 push(@accepted, $key);
81  
82 }
83  
84 foreach my $hash (@{$self->content_types_provided}) {
85  
86 my ($key) = keys %$hash;
87 push(@provided, $key);
88  
89 }
90  
91 $options->{'allow'} = join(',', @$allowed);
92 $options->{'accepted'} = join(',', @accepted);
93 $options->{'provides'} = join(',', @provided);
94  
95 return $options;
96  
97 }
98  
99 sub allowed_methods { [qw[ OPTIONS GET HEAD ]] }
100  
101 sub post_is_create {
102  
103 # uses "content_types_accepted" methods for procssing
104  
105 return 1;
106  
107 }
108  
109 sub content_types_accepted {
110  
111 return [
112 { 'application/json' => 'from_json' },
113 { 'application/x-www-form-urlencoded' => 'from_html' },
114 ];
115  
116 }
117  
118 sub content_types_provided {
119  
120 return [
121 { 'text/html' => 'to_html' },
122 { 'application/hal+json' => 'to_json' },
123 ];
124  
125 }
126  
127 sub charset_provided { return ['UTF-8']; }
128  
129 sub finish_request {
130 my $self = shift;
131 my $metadata = shift;
132  
133 my $alias = $self->alias;
134 my $user = $self->request->user || 'unknown';
135 my $uri = $self->request->uri;
136 my $method = $self->request->method;
137 my $code = $self->response->code;
138 my $path = $uri->path;
139  
140 my $fixup = sub {
141 my $status = shift;
142 my $format = shift;
143 my $data = shift;
144  
145 my $output;
146  
147 if ($format eq 'json') {
148  
149 $output = $self->format_json($data);
150 $self->response->content_type('application/hal+json');
151  
152 } else {
153  
154 $output = $self->format_html($data);
155 $self->response->content_type('text/html');
156  
157 }
158  
159 $self->response->body($output);
160 $self->response->header('Location' => $uri->path);
161 $self->response->status($status);
162  
163 {
164 use bytes;
165 $self->response->header('Content-Length' => length($output));
166 }
167  
168 };
169  
170 $self->log->info(
171 sprintf('%s: %s requested a %s for %s with a status of %s',
172 $alias, $user, $method, $path, $code)
173 );
174  
175 if (defined($metadata->{'exception'})) {
176  
177 my $data;
178 my $ex = $metadata->{'exception'};
179 my $ref = ref($metadata->{'exception'});
180 my $status = $self->errcode || 403;
181 my $type = $self->request->header('accept');
182 my $format = ($type =~ /json/) ? 'json' : 'html';
183  
184 $data->{'_links'} = $self->get_links();
185 $data->{'navigation'} = $self->get_navigation();
186  
187 if (($ref eq 'XAS::Exception') or ($ref eq 'Badger::Exception')) {
188  
189 $data->{'_embedded'}->{'errors'} = [{
190 title => $self->errstr,
191 status => $status,
192 code => $ex->type,
193 detail => $ex->info
194 }];
195  
196 } else {
197  
198 $data->{'_embedded'}->{'errors'} = [{
199 title => $self->errstr,
200 status => $status,
201 code => 'unknown error',
202 detail => sprintf('%s', $ex)
203 }];
204  
205 }
206  
207 $fixup->($status, $format, $data);
208  
209 } elsif ($self->response->status >= 400) {
210  
211 my $data;
212 my $body = join('<br>', @{$self->response->body});
213 my $code = ($self->response->status >= 500) ? 'http internal server error' : 'http client error';
214 my $status = $self->response->status;
215 my $type = $self->request->header('accept');
216 my $format = ($type =~ /json/) ? 'json' : 'html';
217  
218 $data->{'_links'} = $self->get_links();
219 $data->{'navigation'} = $self->get_navigation();
220  
221 $data->{'_embedded'}->{'errors'} = [{
222 title => sprintf('HTTP Error: %s', $self->response->status),
223 status => $self->response->status,
224 code => $code,
225 detail => $body,
226 }];
227  
228 $fixup->($status, $format, $data);
229  
230 }
231  
232 }
233  
234 # -------------------------------------------------------------------------
235 # methods
236 # -------------------------------------------------------------------------
237  
238 sub process_exception {
239 my $self = shift;
240 my $title = shift;
241 my $status = shift;
242  
243 $self->{'errcode'} = $$status;
244 $self->{'errstr'} = $title;
245  
246 }
247  
248 sub process_params {
249 my $self = shift;
250 my $params = shift;
251  
252 return 1;
253  
254 }
255  
256 sub get_navigation {
257 my $self = shift;
258  
259 return [{
260 link => '/',
261 text => 'Root',
262 }];
263  
264 }
265  
266 sub get_links {
267 my $self = shift;
268  
269 return {
270 self => {
271 title => 'Root',
272 href => '/',
273 },
274 };
275  
276 }
277  
278 sub get_response {
279 my $self = shift;
280  
281 my $data;
282  
283 $data->{'_links'} = $self->get_links();
284 $data->{'navigation'} = $self->get_navigation();
285  
286 return $data;
287  
288 }
289  
290 sub json_to_multivalue {
291 my $self = shift;
292 my $json = shift;
293  
294 my $decoded = $self->json->decode($json);
295 my $params = Hash::MultiValue->new();
296  
297 while (my ($key, $value) = each(%$decoded)) {
298  
299 $params->add($key, $value);
300  
301 }
302  
303 return $params;
304  
305 }
306  
307 sub from_json {
308 my $self = shift;
309  
310 # get the post parameters
311  
312 my $content = $self->request->content;
313 my $params = $self->json_to_multivalue($content);
314  
315 return $self->process_params($params);
316  
317 }
318  
319 sub from_html {
320 my $self = shift;
321  
322 # get the post parameters
323  
324 my $params = $self->request->parameters;
325  
326 return $self->process_params($params);
327  
328 }
329  
330 sub to_json {
331 my $self = shift;
332  
333 my $data = $self->get_response();
334 my $json = $self->format_json($data);
335  
336 return $json;
337  
338 }
339  
340 sub to_html {
341 my $self = shift;
342  
343 my $data = $self->get_response();
344 my $html = $self->format_html($data);
345  
346 return $html;
347  
348 }
349  
350 sub format_json {
351 my $self = shift;
352 my $data = shift;
353  
354 delete $data->{'navigation'};
355  
356 return $self->json->encode($data);
357  
358 }
359  
360 sub format_html {
361 my $self = shift;
362 my $data = shift;
363  
364 my $html;
365 my $view = {
366 view => {
367 title => $self->app_name,
368 description => $self->app_description,
369 template => 'dispatcher.tt',
370 data => $data,
371 }
372 };
373  
374 $self->tt->process('wrapper.tt', $view, \$html);
375  
376 return $html;
377  
378 }
379  
380 sub format_body {
381 my $self = shift;
382 my $data = shift;
383  
384 my $body;
385 my $type = $self->request->header('accept');
386 my $format = ($type =~ /json/) ? 'json' : 'html';;
387  
388 if ($format eq 'json') {
389  
390 $body = $self->format_json($data);
391  
392 } else {
393  
394 $body = $self->format_html($data);
395  
396 }
397  
398 return $body;
399  
400 }
401  
402 # -------------------------------------------------------------------------
403 # accessors
404 # -------------------------------------------------------------------------
405  
406 sub app_name {
407 my $self = shift;
408  
409 return $self->{'app_name'};
410  
411 }
412  
413 sub app_description {
414 my $self = shift;
415  
416 return $self->{'app_description'};
417  
418 }
419  
420 sub json {
421 my $self = shift;
422  
423 return $self->{'json'};
424  
425 }
426  
427 sub tt {
428 my $self = shift;
429  
430 return $self->{'tt'};
431  
432 }
433  
434 sub env {
435 my $self = shift;
436  
437 return $self->{'env'};
438  
439 }
440  
441 sub log {
442 my $self = shift;
443  
444 return $self->{'log'};
445  
446 }
447  
448 sub alias {
449 my $self = shift;
450  
451 return $self->{'alias'};
452  
453 }
454  
455 # -------------------------------------------------------------------------
456 # mutators
457 # -------------------------------------------------------------------------
458  
459 sub errcode {
460 my $self = shift;
461 my $code = shift;
462  
463 $self->{'errcode'} = $code if (defined($code));
464  
465 return $self->{'errcode'};
466  
467 }
468  
469 sub errstr {
470 my $self = shift;
471 my $string = shift;
472  
473 $self->{'errstr'} = $string if (defined($string));
474  
475 return $self->{'errstr'};
476  
477 }
478  
479 1;
480  
481 __END__
« Service/Profiles.pm ↑ Summary Apps/Service/Testd.pm »