File Coverage

blib/lib/Net/DRI/Protocol.pm
Criterion Covered Total %
statement 145 184 78.8
branch 29 70 41.4
condition 30 90 33.3
subroutine 27 38 71.0
pod 1 13 7.6
total 232 395 58.7


line stmt bran cond sub pod time code
1             ## Domain Registry Interface, Protocol superclass
2             ##
3             ## Copyright (c) 2005-2011,2013-2014,2016 Patrick Mevzek . All rights reserved.
4             ##
5             ## This file is part of Net::DRI
6             ##
7             ## Net::DRI is free software; you can redistribute it and/or modify
8             ## it under the terms of the GNU General Public License as published by
9             ## the Free Software Foundation; either version 2 of the License, or
10             ## (at your option) any later version.
11             ##
12             ## See the LICENSE file that comes with this distribution for more details.
13             ####################################################################################################
14              
15             package Net::DRI::Protocol;
16              
17 68     68   1804 use strict;
  68         72  
  68         1540  
18 68     68   880 use warnings;
  68         68  
  68         1464  
19              
20 68     68   183 use base qw(Class::Accessor::Chained::Fast Net::DRI::BaseClass);
  68         63  
  68         6214  
21             __PACKAGE__->mk_accessors(qw(name version commands message default_parameters logging));
22              
23 68     68   1768 use DateTime;
  68         142322  
  68         1141  
24 68     68   215 use DateTime::Duration;
  68         72  
  68         1051  
25 68     68   33824 use DateTime::Format::ISO8601;
  68         2883263  
  68         2805  
26 68     68   505 use DateTime::Format::Strptime;
  68         95  
  68         316  
27              
28 68     68   3066 use Net::DRI::Exception;
  68         78  
  68         1211  
29 68     68   810 use Net::DRI::Util;
  68         85  
  68         1172  
30 68     68   34480 use Net::DRI::Data::Changes;
  68         119  
  68         1908  
31 68     68   25440 use Net::DRI::Data::Contact;
  68         159  
  68         502  
32 68     68   34671 use Net::DRI::Data::ContactSet;
  68         124  
  68         1712  
33 68     68   28519 use Net::DRI::Data::Hosts;
  68         124  
  68         292  
34 68     68   30576 use Net::DRI::Data::StatusList;
  68         118  
  68         1680  
35 68     68   29104 use Net::DRI::Data::LanguageTag;
  68         113  
  68         95326  
36              
37             =pod
38              
39             =head1 NAME
40              
41             Net::DRI::Protocol - Superclass of all Net::DRI Protocols
42              
43             =head1 DESCRIPTION
44              
45             Please see the README file for details.
46              
47             =head1 SUPPORT
48              
49             For now, support questions should be sent to:
50              
51             Enetdri@dotandco.comE
52              
53             Please also see the SUPPORT file in the distribution.
54              
55             =head1 SEE ALSO
56              
57             Ehttp://www.dotandco.com/services/software/Net-DRI/E
58              
59             =head1 AUTHOR
60              
61             Patrick Mevzek, Enetdri@dotandco.comE
62              
63             =head1 COPYRIGHT
64              
65             Copyright (c) 2005-2011,2013-2014,2016 Patrick Mevzek .
66             All rights reserved.
67              
68             This program is free software; you can redistribute it and/or modify
69             it under the terms of the GNU General Public License as published by
70             the Free Software Foundation; either version 2 of the License, or
71             (at your option) any later version.
72              
73             See the LICENSE file that comes with this distribution for more details.
74              
75             =cut
76              
77             ####################################################################################################
78              
79             sub new
80             {
81 2     2 1 22 my ($class,$ctx)=@_;
82              
83             my $self={ capabilities=> {},
84 0     0   0 factories => { datetime => sub { return DateTime->new(@_); },
85 0     0   0 duration => sub { return DateTime::Duration->new(@_); },
86 3     3   16 changes => sub { return Net::DRI::Data::Changes->new(@_); },
87 0     0   0 contact => sub { return Net::DRI::Data::Contact->new(); },
88 0     0   0 contactset => sub { return Net::DRI::Data::ContactSet->new(@_); },
89 8     8   21 hosts => sub { return Net::DRI::Data::Hosts->new(@_); },
90 0     0   0 status => sub { return Net::DRI::Data::StatusList->new(@_); },
91 0     0   0 language_tag=> sub { return Net::DRI::Data::LanguageTag->new(@_); },
92             },
93             logging => $ctx->{registry}->logging(),
94             logging_ctx => { registry => $ctx->{registry}->name(), profile => $ctx->{profile}, transport_class => $ctx->{transport_class} },
95 2         50 };
96              
97 2         18 bless($self,$class);
98 2         13 $self->message(undef);
99 2         39 $self->default_parameters({});
100              
101 2         20 $self->log_setup_channel($class,'protocol',$self->{logging_ctx});
102 2         6 $self->log_output('debug','core',sprintf('Added profile %s for registry %s',$class,$ctx->{registry}->name()));
103 2         5 return $self;
104             }
105              
106             sub log_output
107             {
108 5     5 0 24 my ($self,$level,$type,$data1)=@_;
109 5 100 100     24 $self->{logging_ctx}->{protocol}=$self->name().'/'.$self->version() if (! exists $self->{logging_ctx}->{protocol} && defined $self->name());
110 5 50       41 return $self->logging()->output($level,$type,ref $data1 ? +{ %{$self->{logging_ctx}}, %$data1 } : $data1);
  0         0  
111             }
112              
113              
114             sub parse_iso8601
115             {
116 0     0 0 0 my ($self,$d)=@_;
117 0 0       0 $self->{iso8601_parser}=DateTime::Format::ISO8601->new() unless exists $self->{iso8601_parser};
118 0         0 return $self->{iso8601_parser}->parse_datetime($d);
119             }
120              
121             sub build_strptime_parser
122             {
123 0     0 0 0 my ($self,@args)=@_;
124 0         0 my $key=join("\x{001E}",@args);
125 0 0       0 $self->{strptime_parser}->{$key}=DateTime::Format::Strptime->new(@args) unless exists $self->{strptime_parser}->{$key};
126 0         0 return $self->{strptime_parser}->{$key};
127             }
128              
129             sub create_local_object
130             {
131 31     31 0 42 my ($self,$what,@args)=@_;
132 31 50 33     100 return unless defined $self && defined $what;
133 31         47 my $fn=$self->factories();
134 31 50 33     225 Net::DRI::Exception::err_assert('invalid factory name "'.$what.'"') unless defined $fn && ref $fn eq 'HASH' && exists $fn->{$what} && ref $fn->{$what} eq 'CODE';
      33        
      33        
135 31         68 return $fn->{$what}->(@args);
136             }
137              
138             ## This should not be called multiple times for a given Protocol class (as it will erase the loaded_modules slot)
139             sub _load
140             {
141 1     1   2 my ($self,@classes)=@_;
142 1         4 my $etype='protocol/'.$self->name();
143 1         8 my $version=$self->version();
144              
145 1         5 my %skip = map { substr($_,1) => 1 } grep { /^-/ } @classes;
  0         0  
  3         6  
146              
147 1         2 my (%c,%done,@done);
148 1         2 foreach my $class (@classes)
149             {
150 3 50 33     20 next if exists $done{$class} || $class=~m/^-/ || exists $skip{$class};
      33        
151 3         14 $self->log_output('debug','core',sprintf('Loading class "%s"',$class));
152 3         9 Net::DRI::Util::load_module($class,$etype);
153 3 50       43 Net::DRI::Exception::method_not_implemented('register_commands',$class) unless $class->can('register_commands');
154 3         25 my $rh=$class->register_commands($version);
155 3         7 $self->{commands_by_class}->{$class}=$rh;
156 3         13 Net::DRI::Util::hash_merge(\%c,$rh); ## { object type => { action type => [ build action, parse action ]+ } }
157 3 50       24 if ($class->can('capabilities_add'))
158             {
159 0         0 my @a=$class->capabilities_add();
160 0 0       0 if (ref($a[0]))
161             {
162 0         0 foreach my $a (@a) { $self->capabilities(@$a); }
  0         0  
163             } else
164             {
165 0         0 $self->capabilities(@a);
166             }
167             }
168 3 50       14 $class->setup($self,$version) if $class->can('setup');
169 3         5 $done{$class}=1;
170 3         6 push @done,$class;
171             }
172              
173 1         2 $self->{loaded_modules}=\@done;
174 1         7 $self->commands(\%c);
175 1         10 return;
176             }
177              
178             ## has_module + find_action_in_class should instead better be based on some ID, like the XML namespace in EPP,
179             ## instead of the Perl module names
180             sub has_module
181             {
182 0     0 0 0 my ($self,$mod)=@_;
183 0 0 0     0 return 0 unless defined $mod && length $mod;
184 0 0       0 return (grep { $_ eq $mod } @{$self->{loaded_modules}})? 1 : 0;
  0         0  
  0         0  
185             }
186              
187             sub find_action_in_class
188             {
189 0     0 0 0 my ($self,$class,$otype,$oaction)=@_;
190 0 0 0     0 return unless defined $class && length $class && exists $self->{commands_by_class}->{$class} && exists $self->{commands_by_class}->{$class}->{$otype} && exists $self->{commands_by_class}->{$class}->{$otype}->{$oaction};
      0        
      0        
      0        
191 0 0       0 return wantarray ? @{$self->{commands_by_class}->{$class}->{$otype}->{$oaction}} : $self->{commands_by_class}->{$class}->{$otype}->{$oaction}->[0];
  0         0  
192             }
193              
194             sub _load_commands
195             {
196 20     20   18 my ($self,$otype,$oaction)=@_;
197              
198 20         43 my $etype='protocol/'.$self->name();
199 20 50 33     213 Net::DRI::Exception->die(1,$etype,7,'Object type and/or action not defined') unless (defined $otype && length $otype && defined $oaction && length $oaction);
      33        
      33        
200 20         42 my $h=$self->commands();
201 20 50       87 Net::DRI::Exception->die(1,$etype,8,'No actions defined for object of type <'.$otype.'>') unless exists($h->{$otype});
202 20 50       38 Net::DRI::Exception->die(1,$etype,9,'No action name <'.$oaction.'> defined for object of type <'.$otype.'> in '.ref($self)) unless exists($h->{$otype}->{$oaction});
203 20         26 return $h;
204             }
205              
206             sub has_action
207             {
208 0     0 0 0 my ($self,$otype,$oaction)=@_;
209 0 0       0 return eval { my $h=$self->_load_commands($otype,$oaction); 1; } ? 1 : 0;
  0         0  
  0         0  
210             }
211              
212             sub action
213             {
214 10     10 0 19 my ($self,$otype,$oaction,$trid,@params)=@_;
215 10         20 my $h=$self->_load_commands($otype,$oaction);
216              
217             ## Create a new message from scratch and loop through all functions registered for given action & type
218 10         17 my $msg=$self->create_local_object('message',$trid,$otype,$oaction);
219 10 50 33     83 Net::DRI::Exception->die(0,'protocol',1,'Unsuccessfull message creation') unless ($msg && ref $msg && $msg->isa('Net::DRI::Protocol::Message'));
      33        
220 10         22 $self->message($msg); ## store it for later use (in loop below)
221              
222 10         41 foreach my $t (@{$h->{$otype}->{$oaction}})
  10         20  
223             {
224 10         10 my $pf=$t->[0];
225 10 50 33     35 next unless (defined($pf) && (ref($pf) eq 'CODE'));
226 10         27 $pf->($self,@params);
227             }
228              
229 10         24 $self->message(undef); ## needed ? useful ?
230 10         74 return $msg;
231             }
232              
233             sub reaction
234             {
235 10     10 0 14 my ($self,$otype,$oaction,$dr,$sent,$oname,$trid)=@_;
236 10         17 my $h=$self->_load_commands($otype,$oaction);
237 10         18 my $msg=$self->create_local_object('message');
238 10 50 33     70 Net::DRI::Exception->die(0,'protocol',1,'Unsuccessfull message creation') unless ($msg && ref($msg) && $msg->isa('Net::DRI::Protocol::Message'));
      33        
239              
240 10         9 my %info;
241             ## TODO is $sent needed here really ? if not remove from API above also !
242 10         25 $msg->parse($dr,\%info,$otype,$oaction,$sent); ## will trigger an Exception by itself if problem ## TODO : add later the whole LocalStorage stuff done when sending ? (instead of otype/oaction/message sent)
243 10         23 $self->message($msg); ## store it for later use (in loop below)
244 10 0 66     85 $info{$otype}->{$oname}->{name}=$oname if ($otype eq 'domain' || $otype eq 'host' || $otype eq 'nsgroup' || $otype eq 'keygroup'); ## TODO : abstract this ?
      33        
      33        
245              
246 10 0 33     20 if (exists $h->{message} && exists $h->{message}->{result})
247             {
248 0         0 foreach my $t (@{$h->{message}->{result}})
  0         0  
249             {
250 0         0 my $pf=$t->[1];
251 0 0 0     0 next unless (defined $pf && ref $pf eq 'CODE');
252 0         0 $pf->($self,$otype,$oaction,$oname,\%info);
253             }
254             }
255              
256 10         6 foreach my $t (@{$h->{$otype}->{$oaction}})
  10         21  
257             {
258 10         13 my $pf=$t->[1];
259 10 100 66     34 next unless (defined $pf && ref $pf eq 'CODE');
260 6         17 $pf->($self,$otype,$oaction,$oname,\%info);
261             }
262              
263 10         27 my $rc=$msg->result_status();
264 10 50       19 if (defined $rc)
265             {
266 10 50       24 $rc->_set_trid([ $trid ]) unless $rc->trid(); ## if not done inside Protocol::*::Message::result_status, make sure we save at least our transaction id
267 10         21 foreach my $v1 (values %info)
268             {
269 10 50 33     53 next unless ref $v1 eq 'HASH' && keys %$v1;
270 10         6 foreach my $v2 (values %{$v1})
  10         17  
271             {
272 10 50 33     40 next unless ref $v2 eq 'HASH' && keys %$v2; ## yes, this can happen, with must_reconnect for example
273 10 50       15 next if exists $v2->{result_status};
274 10         20 $v2->{result_status}=$rc;
275             }
276             }
277             }
278 10         29 $self->message(undef); ## needed ? useful ?
279              
280 10         55 $info{session}->{exchange}->{result_from_cache}=0;
281 10         19 $info{session}->{exchange}->{protocol}=$self->nameversion();
282 10         78 $info{session}->{exchange}->{trid}=$trid;
283 10         51 return ($rc,\%info);
284             }
285              
286             sub nameversion
287             {
288 14     14 0 833 my $self=shift;
289 14         30 return $self->name().'/'.$self->version();
290             }
291              
292             sub factories
293             {
294 33     33 0 33 my ($self,$object,$code)=@_;
295 33 100 66     68 if (defined $object && defined $code)
296             {
297 2         3 $self->{factories}->{$object}=$code;
298 2         5 return $self;
299             }
300 31         36 return $self->{factories};
301             }
302              
303             sub capabilities
304             {
305 12     12 0 1062 my ($self,$action,$object,$cap)=@_;
306 12 100 66     30 if (defined($action) && defined($object))
307             {
308 4 100       16 $self->{capabilities}->{$action}={} unless exists($self->{capabilities}->{$action});
309 4 50       5 if (defined($cap))
310             {
311 4         7 $self->{capabilities}->{$action}->{$object}=$cap;
312             } else
313             {
314 0         0 delete($self->{capabilities}->{$action}->{$object});
315             }
316             }
317 12         24 return $self->{capabilities};
318             }
319              
320             ####################################################################################################
321             1;