DRI/Transport/HTTP/SOAPWSDL.pm untested

filter: Statementstmt 0.0% 0 / 162 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 268 CC 26 SCAR 44.2
CC
26
Cov
0%
new
240.0
send_login
20.0
send_logout
12.0
CRAP 83.2
1 ## Domain Registry Interface, SOAP+WSDL 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::SOAPWSDL;
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::WSDL;
26  
27 =pod
28  
29 =head1 NAME
30  
31 Net::DRI::Transport::HTTP::SOAPWSDL - SOAP+WSDL 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/soapwsdl');
80 if ($t{protocol_connection}->can('transport_default'))
81 {
82 %opts=($t{protocol_connection}->transport_default('soapwsdl'),%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('soapwsdl');
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 if ($t{has_login})
95 {
96 foreach my $p (qw/client_login client_password/)
97 {
98 Net::DRI::Exception::usererr_insufficient_parameters($p.' must be provided') unless (exists($opts{$p}) && defined($opts{$p}));
99 $t{$p}=$opts{$p};
100 }
101 $t{session_data}={};
102 }
103  
104 foreach my $p (qw/protocol_connection wsdl_uri proxy_uri servicename portname/)
105 {
106 Net::DRI::Exception::usererr_insufficient_parameters($p.' must be provided') unless (exists($opts{$p}) && defined($opts{$p}));
107 $t{$p}=$opts{$p};
108 }
109 Net::DRI::Exception::usererr_invalid_parameters('proxy_uri must be http:// or https://') unless ($t{proxy_uri}=~m!^https?://!);
110  
111 my $pc=$t{protocol_connection};
112 if ($t{has_login})
113 {
114 foreach my $m (qw/login parse_login extract_session/)
115 {
116 Net::DRI::Exception::usererr_invalid_parameters('Protocol connection class '.$pc.' must have a '.$m.'() method, since has_login=1') unless ($pc->can($m));
117 }
118 }
119 if ($t{has_logout})
120 {
121 foreach my $m (qw/logout parse_logout/)
122 {
123 Net::DRI::Exception::usererr_invalid_parameters('Protocol connection class '.$pc.' must have a '.$m.'() method, since has_logout=1') unless ($pc->can($m));
124 }
125 }
126  
127 $self->{transport}=\%t;
128  
129 if ($self->has_state())
130 {
131 if ($self->defer()) ## we will open, but later
132 {
133 $self->current_state(0);
134 } else ## we will open NOW
135 {
136 $self->open_connection($ctx);
137 }
138 } else
139 {
140 $self->init();
141 $self->time_open(time());
142 }
143  
144 return $self;
145 }
146  
147 sub soap { my ($self,$v)=@_; $self->{transport}->{soap}=$v if defined $v; return $self->{transport}->{soap}; }
148 sub session_data { my ($self,$v)=@_; $self->{transport}->{session_data}=$v if defined $v; return $self->{transport}->{session_data}; }
149  
150 sub init
151 {
152 my ($self)=@_;
153 return if defined($self->soap());
154 my $soap=SOAP::WSDL->new();
155 $soap->wsdl($self->{transport}->{wsdl_uri});
156 $soap->proxy($self->{transport}->{proxy_uri});
157 $soap->wsdlinit();
158 $soap->servicename($self->{transport}->{servicename});
159 $soap->portname($self->{transport}->{portname});
160 $soap->get_client()->get_transport()->agent(sprintf('Net::DRI/%s ',$Net::DRI::VERSION).$soap->get_client()->get_transport()->agent());
161 $self->soap($soap);
162 return;
163 }
164  
165 sub send_login
166 {
167 my ($self,$ctx)=@_;
168 my $t=$self->{transport};
169 return unless $t->{has_login};
170 foreach my $p (qw/client_login client_password/)
171 {
172 Net::DRI::Exception::usererr_insufficient_parameters($p.' must be defined') unless (exists($t->{$p}) && $t->{$p});
173 }
174  
175 my $pc=$t->{protocol_connection};
176 my $cltrid=$self->generate_trid($self->{logging_ctx}->{registry});
177 my $login=$pc->login($t->{message_factory},$t->{client_login},$t->{client_password},$cltrid);
178 my $res=$self->_send_receive({otype=>'session',oaction=>'login',trid=>$cltrid,phase=>'opening'},$login);
179 my $msg=$t->{message_factory}->();
180 $msg->parse(Net::DRI::Data::Raw->new(1,[$res->result()]));
181 my $rc=$pc->parse_login($msg);
182 die($rc) unless $rc->is_success();
183  
184 $self->session_data($pc->extract_session($msg));
185 return;
186 }
187  
188 sub send_logout
189 {
190 my ($self)=@_;
191 my $t=$self->{transport};
192 return unless $t->{has_logout};
193  
194 my $pc=$t->{protocol_connection};
195 my $cltrid=$self->generate_trid($self->{logging_ctx}->{registry});
196 my $logout=$pc->logout($t->{message_factory},$cltrid,$t->{session_data});
197 my $res=$self->_send_receive({otype=>'session',oaction=>'logout',trid=>$cltrid,phase=>'closing'},$logout);
198 my $msg=$t->{message_factory}->();
199 $msg->parse(Net::DRI::Data::Raw->new(1,[$res->result()]));
200 my $rc=$pc->parse_logout($msg);
201 die($rc) unless $rc->is_success();
202  
203 $self->session_data({});
204 return;
205 }
206  
207 sub _send_receive ## no critic (Subroutines::RequireFinalReturn)
208 {
209 my ($self,$ctx,$msg)=@_;
210 my $soap=$self->soap();
211 my $res=$soap->call($msg->method(),%{$msg->params()});
212 if (my $httpres=$soap->get_client()->get_transport()->http_response())
213 {
214 $self->log_output('notice','transport',$ctx,{direction=>'out',message=>$httpres->request()});
215 $self->log_output('notice','transport',$ctx,{direction=>'in', message=>$httpres});
216 } else
217 {
218 $soap->no_dispatch(1);
219 $self->log_output('error','transport',$ctx,{direction=>'out',message=>'No response for message '.$soap->call($msg->method(), %{$msg->params()})});
220 $soap->no_dispatch(0);
221 }
222 return $res if $res; ## SOAP::WSDL::Client returns a fault object on errors, even on transport layer errors.
223  
224 Net::DRI::Exception->die(1,'transport/soapwsdl',4,'Unable to send message due to SOAP fault: '.$res->faultcode().' '.$res->faultstring());
225 }
226  
227 sub open_connection
228 {
229 my ($self,$ctx)=@_;
230 $self->init();
231 $self->send_login($ctx);
232 $self->current_state(1);
233 $self->time_open(time());
234 $self->time_used(time());
235 return;
236 }
237  
238 sub close_connection
239 {
240 my ($self)=@_;
241 $self->send_logout();
242 $self->soap(undef);
243 $self->current_state(0);
244 return;
245 }
246  
247 sub end
248 {
249 my ($self)=@_;
250 if ($self->has_state() && $self->current_state())
251 {
252 eval
253 {
254 local $SIG{ALRM}=sub { die 'timeout' };
255 alarm(10);
256 $self->close_connection();
257 };
258 alarm(0); ## since close_connection may die, this must be outside of eval to be executed in all cases
259 }
260 return;
261 }
262  
263 ####################################################################################################
264  
265 sub send ## no critic (Subroutines::ProhibitBuiltinHomonyms)
266 {
267 my ($self,$ctx,$tosend)=@_;
268 return $self->SUPER::send($ctx,$tosend,\&_soap_send,sub {});
269 }
270  
271 sub _soap_send
272 {
273 my ($self,$count,$tosend,$ctx)=@_;
274 my $t=$self->{transport};
275 $tosend->add_session($self->session_data());
276 my $res=$self->_send_receive($ctx,$tosend);
277 $t->{last_reply}=$res;
278 return 1; ## very important
279 }
280  
281 sub receive
282 {
283 my ($self,$ctx,$count)=@_;
284 return $self->SUPER::receive($ctx,\&_soap_receive);
285 }
286  
287 sub _soap_receive
288 {
289 my ($self,$count)=@_;
290 my $t=$self->{transport};
291 my $r=$t->{last_reply};
292 $t->{last_reply}=undef;
293 return Net::DRI::Data::Raw->new(6,[$r->result()]);
294 }
295  
296 ####################################################################################################
297 1;