DRI/Transport/SOAP.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 116 CC 10 SCAR 34.3
CC
10
Cov
0%
new
72.0
soap_fault
6.0
_soap_send
6.0
CRAP 30.8
1 ## Domain Registry Interface, SOAP Transport (HTTP/HTTPS)
2 ##
3 ## Copyright (c) 2005,2009-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::SOAP;
16  
17 use strict;
18 use warnings;
19  
20 use base qw(Net::DRI::Transport);
21  
22 use Net::DRI::Exception;
23  
24 use SOAP::Lite;
25  
26 =pod
27  
28 =head1 NAME
29  
30 Net::DRI::Transport::SOAP - SOAP (HTTP/HTTPS) Transport for Net::DRI
31  
32 =head1 DESCRIPTION
33  
34 When calling new_current_profile with this transport, in ref array of parameters
35 for transport, pass an hash ref with the following keys, as needed:
36  
37 =over
38  
39 =item *
40  
41 C<proxy_url> is a string giving the url to use
42 (mostly worthless since using services, but needed to setup credentials)
43  
44 =item *
45  
46 C<credentials> is an array ref with four items in this order:
47 sitename:port,realm,username,password
48 ex: C<< credentials=>['soap-adh.nic.fr:443','Webservices Adherents AFNIC','USERNAME','PASSWORD'] >>
49  
50 =item *
51  
52 C<service_wsdl> is an hash ref : the key is a string used in Protocol classes,
53 value is an url to the corresponding WSDL file
54 ex: C<< service_wsdl=>{Domain=>'file:./Domain-perl.wsdl'} >>
55  
56 =item *
57  
58 C<ssl_ca_file> is a string giving the local path to the CA certificate file, if using https
59  
60 =back
61  
62 =head2 CURRENT LIMITATIONS
63  
64 =over
65  
66 =item * only for SOAP over HTTP/HTTPS
67  
68 =item * only one CA certificate can be used in each given instance of Net::DRI
69 (because it is given through %ENV)
70  
71 =back
72  
73 =head1 SUPPORT
74  
75 For now, support questions should be sent to:
76  
77 E<lt>netdri@dotandco.comE<gt>
78  
79 Please also see the SUPPORT file in the distribution.
80  
81 =head1 SEE ALSO
82  
83 E<lt>http://www.dotandco.com/services/software/Net-DRI/E<gt>
84  
85 =head1 AUTHOR
86  
87 Patrick Mevzek, E<lt>netdri@dotandco.comE<gt>
88  
89 =head1 COPYRIGHT
90  
91 Copyright (c) 2005,2009-2011,2013 Patrick Mevzek <netdri@dotandco.com>.
92 All rights reserved.
93  
94 This program is free software; you can redistribute it and/or modify
95 it under the terms of the GNU General Public License as published by
96 the Free Software Foundation; either version 2 of the License, or
97 (at your option) any later version.
98  
99 See the LICENSE file that comes with this distribution for more details.
100  
101 =cut
102  
103 ####################################################################################################
104  
105 sub new
106 {
107 my ($class,$ctx,$rp)=@_;
108 my %opts=%$rp;
109 my $self=$class->SUPER::new($ctx,\%opts); ## We are now officially a Net::DRI::Transport instance
110 $self->has_state(0);
111 $self->is_sync(1);
112 $self->name('soap');
113 $self->version('0.1');
114  
115 my %t;
116  
117 Net::DRI::Exception::usererr_insufficient_parameters('proxy_url must be defined') unless (exists($opts{proxy_url}));
118 Net::DRI::Exception::usererr_invalid_parameters('proxy_url must be http:// or https://') unless ($opts{proxy_url}=~m!^https?://!);
119 Net::DRI::Exception::usererr_insufficient_parameters('service_wsdl') unless (exists($opts{service_wsdl}));
120 Net::DRI::Exception::usererr_invalid_parameters('service_wsdl must be a ref hash') unless (ref($opts{service_wsdl}) eq 'HASH'); ## Name (without .wsdl),ex: Domain => Path to corresponding wsdl file
121  
122 my $service=SOAP::Lite->on_fault(\&soap_fault);
123 my %st;
124  
125 while(my ($k,$v)=each(%{$opts{service_wsdl}}))
126 {
127 my $go=$service->service($v);
128 my $t=$go->transport();
129 $t->agent(sprintf('Net::DRI/%s',$Net::DRI::VERSION).$t->agent());
130 if ($self->timeout())
131 {
132 $t->proxy($opts{proxy_url},timeout => $self->timeout());
133 } else
134 {
135 $t->proxy($opts{proxy_url});
136 }
137  
138 # name:port,realm,user,login
139 $t->credentials(@{$opts{credentials}}) if ($opts{credentials} && (ref($opts{credentials}) eq 'ARRAY'));
140  
141 $st{$k}=$go;
142 }
143  
144 $t{service_wsdl}=\%st;
145  
146 if (exists($opts{ssl_ca_file}) && (-s $opts{ssl_ca_file}))
147 {
148 ## How to handle multiple SOAP instances in the same process ??
149 $ENV{HTTPS_CA_FILE}=$opts{ssl_ca_file}; ## no critic (Variables::RequireLocalizedPunctuationVars)
150 }
151  
152 $t{soap}=$service;
153 $self->{transport}=\%t;
154 return $self;
155 }
156  
157 sub soap_fault ## no critic (Subroutines::RequireFinalReturn)
158 {
159 my($soap,$res)=@_;
160 my $msg=ref $res ? $res->faultstring() : $soap->transport()->status();
161 Net::DRI::Exception->die(1,'transport/soap',7,'SOAP fault: '.$msg);
162 }
163  
164 sub send ## no critic (Subroutines::ProhibitBuiltinHomonyms)
165 {
166 my ($self,$ctx,$tosend)=@_;
167 return $self->SUPER::send($ctx,$tosend,\&_soap_send,sub {});
168 }
169  
170 sub _soap_send
171 {
172 my ($self,$count,$tosend)=@_;
173 my $t=$self->{transport};
174 my $so=$t->{soap};
175 my $sw=$t->{service_wsdl}; ## a ref hash
176 my $service=$tosend->service();
177  
178 Net::DRI::Exception::usererr_insufficient_parameters("No wsdl file specified for service $service") unless (exists($sw->{$service}));
179  
180 my $m=$tosend->method();
181 my $r=$sw->{$service}->$m(@{$tosend->params()});
182 $t->{last_reply}=$r;
183 return 1; ## very important
184 }
185  
186 sub receive
187 {
188 my ($self,$ctx,$count)=@_;
189 return $self->SUPER::receive($ctx,\&_soap_receive);
190 }
191  
192 sub _soap_receive
193 {
194 my ($self,$count)=@_;
195 my $t=$self->{transport};
196 my $so=$t->{service};
197 my $r=$t->{last_reply};
198 $t->{last_reply}=undef;
199  
200 return $r; ## will we need one day access to $so ?
201 }
202  
203 ####################################################################################################
204 1;