DRI/Transport/HTTP/SOAPLite.pm untested

filter: Statementstmt 0.0% 0 / 162 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 313 CC 30 SCAR 47.1
CC
30
Cov
0%
new
306.0
_send_receive
30.0
send_login
20.0
CRAP 110.8
1 ## Domain Registry Interface, SOAP Transport
2 ##
3 ## Copyright (c) 2008-2011,2013 Patrick Mevzek <netdri@dotandco.com>. 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::Transport::HTTP::SOAPLite;
16  
17 use strict;
18 use warnings;
19  
20 use base qw(Net::DRI::Transport);
21  
22 use Net::DRI::Exception;
23 use Net::DRI::Data::Raw;
24 use Net::DRI::Util;
25 use SOAP::Lite;
26  
27 =pod
28  
29 =head1 NAME
30  
31 Net::DRI::Transport::HTTP::SOAPLite - SOAP Transport for Net::DRI
32  
33 =head1 DESCRIPTION
34  
35 Please see the README file for details.
36  
37 =head1 SUPPORT
38  
39 For now, support questions should be sent to:
40  
41 E<lt>netdri@dotandco.comE<gt>
42  
43 Please also see the SUPPORT file in the distribution.
44  
45 =head1 SEE ALSO
46  
47 E<lt>http://www.dotandco.com/services/software/Net-DRI/E<gt>
48  
49 =head1 AUTHOR
50  
51 Patrick Mevzek, E<lt>netdri@dotandco.comE<gt>
52  
53 =head1 COPYRIGHT
54  
55 Copyright (c) 2008-2011,2013 Patrick Mevzek <netdri@dotandco.com>.
56 All rights reserved.
57  
58 This program is free software; you can redistribute it and/or modify
59 it under the terms of the GNU General Public License as published by
60 the Free Software Foundation; either version 2 of the License, or
61 (at your option) any later version.
62  
63 See the LICENSE file that comes with this distribution for more details.
64  
65 =cut
66  
67 ####################################################################################################
68  
69 sub new
70 {
71 my ($class,$ctx,$rp)=@_;
72 my %opts=%$rp;
73 my $po=$ctx->{protocol};
74  
75 my %t=(message_factory => $po->factories()->{message});
76 if (exists($opts{protocol_connection}) && $opts{protocol_connection})
77 {
78 $t{protocol_connection}=$opts{protocol_connection};
79 Net::DRI::Util::load_module($t{protocol_connection},'transport/soaplite');
80 if ($t{protocol_connection}->can('transport_default'))
81 {
82 %opts=($t{protocol_connection}->transport_default('soaplite'),%opts);
83 }
84 }
85  
86 my $self=$class->SUPER::new($ctx,\%opts); ## We are now officially a Net::DRI::Transport instance
87 $self->is_sync(1);
88 $self->name('soaplite');
89 $self->version('0.1');
90  
91 $t{has_login}=(exists($opts{has_login}) && defined($opts{has_login}))? $opts{has_login} : 0;
92 $t{has_logout}=(exists($opts{has_logout}) && defined($opts{has_logout}))? $opts{has_logout} : 0;
93 $self->has_state($t{has_login});
94  
95 foreach my $p (qw/client_login client_password/)
96 {
97 Net::DRI::Exception::usererr_insufficient_parameters($p.' must be provided') unless (exists($opts{$p}) && defined($opts{$p}));
98 $t{$p}=$opts{$p};
99 }
100  
101 $t{session_data}=$t{has_login}? {} : { id => $t{client_login}, pass => $t{client_password} };
102  
103 foreach my $p (qw/uri proxy_uri/)
104 {
105 Net::DRI::Exception::usererr_insufficient_parameters($p.' must be provided') unless (exists($opts{$p}) && defined($opts{$p}));
106 $t{$p}=$opts{$p};
107 }
108 Net::DRI::Exception::usererr_invalid_parameters('proxy_uri must be http:// or https://') unless ($t{proxy_uri}=~m!^https?://!);
109  
110 my $pc=$opts{protocol_connection};
111 if ($t{has_login} || $t{has_logout})
112 {
113 Net::DRI::Exception::usererr_insufficient_parameters('protocol_connection must be provided') unless (defined($pc));
114 }
115 if ($t{has_login})
116 {
117 foreach my $m (qw/login parse_login extract_session/)
118 {
119 Net::DRI::Exception::usererr_invalid_parameters('Protocol connection class '.$pc.' must have a '.$m.'() method, since has_login=1') unless ($pc->can($m));
120 }
121 }
122 if ($t{has_logout})
123 {
124 foreach my $m (qw/logout parse_logout/)
125 {
126 Net::DRI::Exception::usererr_invalid_parameters('Protocol connection class '.$pc.' must have a '.$m.'() method, since has_logout=1') unless ($pc->can($m));
127 }
128 }
129  
130 $self->{transport}=\%t;
131  
132 if ($self->has_state())
133 {
134 if ($self->defer()) ## we will open, but later
135 {
136 $self->current_state(0);
137 } else ## we will open NOW
138 {
139 $self->open_connection($ctx);
140 }
141 } else
142 {
143 $self->init();
144 $self->time_open(time());
145 }
146  
147 return $self;
148 }
149  
150 sub soap { my ($self,$v)=@_; $self->{transport}->{soap}=$v if defined $v; return $self->{transport}->{soap}; }
151 sub session_data { my ($self,$v)=@_; $self->{transport}->{session_data}=$v if defined $v; return $self->{transport}->{session_data}; }
152  
153 sub init
154 {
155 my ($self)=@_;
156 return if defined($self->soap());
157 my $soap=SOAP::Lite->new()->uri($self->{transport}->{uri})->proxy($self->{transport}->{proxy_uri});
158 $soap->transport()->agent(sprintf('Net::DRI/%s ',$Net::DRI::VERSION).$soap->transport()->agent());
159 $self->soap($soap);
160 return;
161 }
162  
163 sub send_login
164 {
165 my ($self,$ctx)=@_;
166 my $t=$self->{transport};
167 return unless $t->{has_login};
168 foreach my $p (qw/client_login client_password/)
169 {
170 Net::DRI::Exception::usererr_insufficient_parameters($p.' must be defined') unless (exists($t->{$p}) && $t->{$p});
171 }
172  
173 my $pc=$t->{protocol_connection};
174 my $cltrid=$self->generate_trid($self->{logging_ctx}->{registry});
175 my $login=$pc->login($t->{message_factory},$t->{client_login},$t->{client_password},$cltrid);
176 my $res=$self->_send_receive({otype=>'session',oaction=>'login',trid=>$cltrid,phase=>'opening'},$login);
177 my $msg=$t->{message_factory}->();
178 $msg->parse(Net::DRI::Data::Raw->new(1,[$res->result()]));
179 my $rc=$pc->parse_login($msg);
180 die($rc) unless $rc->is_success();
181  
182 $self->session_data($pc->extract_session($msg));
183 return;
184 }
185  
186 sub send_logout
187 {
188 my ($self)=@_;
189 my $t=$self->{transport};
190 return unless $t->{has_logout};
191  
192 my $pc=$t->{protocol_connection};
193 my $cltrid=$self->generate_trid($self->{logging_ctx}->{registry});
194 my $logout=$pc->logout($t->{message_factory},$cltrid,$t->{session_data});
195 my $res=$self->_send_receive({otype=>'session',oaction=>'logout',trid=>$cltrid,phase=>'closing'},$logout);
196 my $msg=$t->{message_factory}->();
197 $msg->parse(Net::DRI::Data::Raw->new(1,[$res->result()]));
198 my $rc=$pc->parse_logout($msg);
199 die($rc) unless $rc->is_success();
200  
201 $self->session_data({});
202 return;
203 }
204  
205 sub _send_receive ## no critic (Subroutines::RequireFinalReturn)
206 {
207 my ($self,$ctx,$msg)=@_;
208 my $soap=$self->soap();
209 my $err;
210 my $res=$soap->on_fault(sub { (undef,$err)=@_; return; })->call($msg->method(),@{$msg->params()});
211 if (my $httpres=$soap->transport()->http_response())
212 {
213 $self->log_output('notice','transport',$ctx,{direction=>'out',message=>$httpres->request()});
214 $self->log_output('notice','transport',$ctx,{direction=>'in', message=>$httpres});
215 } else
216 {
217 $self->log_output('error','transport',$ctx,{direction=>'out',message=>'No response for message '.$soap->serializer()->envelope(method => $msg->method(), @{$msg->params()})});
218 }
219 return $res if defined $res && ref $res && ! $res->fault() && ! defined $err;
220  
221 Net::DRI::Exception->die(1,'transport/soaplite',4,'Unable to send message due to SOAP fault: '.$err->faultcode().' '.$err->faultstring()) if defined $err && ref $err;
222 Net::DRI::Exception->die(1,'transport/soaplite',4,'Unable to send message due to SOAP transport error: '.$soap->transport()->status()) unless $soap->transport()->is_success();
223 Net::DRI::Exception->die(1,'transport/soaplite',4,'Unable to send message due to SOAP deserialization error: '.$err);
224 }
225  
226 sub open_connection
227 {
228 my ($self,$ctx)=@_;
229 $self->init();
230 $self->send_login($ctx);
231 $self->current_state(1);
232 $self->time_open(time());
233 $self->time_used(time());
234 return;
235 }
236  
237 sub close_connection
238 {
239 my ($self)=@_;
240 $self->send_logout();
241 $self->soap(undef);
242 $self->current_state(0);
243 return;
244 }
245  
246 sub end
247 {
248 my ($self)=@_;
249 if ($self->has_state() && $self->current_state())
250 {
251 eval
252 {
253 local $SIG{ALRM}=sub { die 'timeout' };
254 alarm(10);
255 $self->close_connection();
256 };
257 alarm(0); ## since close_connection may die, this must be outside of eval to be executed in all cases
258 }
259 return;
260 }
261  
262 ####################################################################################################
263  
264 sub send ## no critic (Subroutines::ProhibitBuiltinHomonyms)
265 {
266 my ($self,$ctx,$tosend)=@_;
267 return $self->SUPER::send($ctx,$tosend,\&_soap_send,sub {});
268 }
269  
270 sub _soap_send
271 {
272 my ($self,$count,$tosend,$ctx)=@_;
273 my $t=$self->{transport};
274 $tosend->add_session($self->session_data());
275 my $res=$self->_send_receive($ctx,$tosend);
276 $t->{last_reply}=$res;
277 return 1; ## very important
278 }
279  
280 sub receive
281 {
282 my ($self,$ctx,$count)=@_;
283 return $self->SUPER::receive($ctx,\&_soap_receive);
284 }
285  
286 sub _soap_receive
287 {
288 my ($self,$count)=@_;
289 my $t=$self->{transport};
290 my $r=$t->{last_reply};
291 $t->{last_reply}=undef;
292 return Net::DRI::Data::Raw->new(6,[$r->result()]);
293 }
294  
295 ####################################################################################################
296 1;