XDI/Connection.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« XDI/Message.pm ↑ Summary
1 package XDI::Connection;
2  
3 1 ▶use lib qw(..);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 carp
10 croak
11 cluck
12 1 );
13 0 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=2.6
14 use Data::Dumper;
15 use Storable qw(dclone);
16 use Scalar::Util;
17 use HTTP::Request;
18 use LWP::UserAgent;
19  
20 use XDI qw(s_debug);
21 use XDI::Message;
22  
23 require Exporter;
24 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
25  
26 # set the version for version checking
27 $VERSION = 0.05;
28  
29 @ISA = qw(Exporter);
30 @EXPORT = qw(
31 iname_lookup
32 inumber_lookup
33 lookup
34 );
35 %EXPORT_TAGS = ( );
36  
37 @EXPORT_OK = qw($Var1 %Hashit &func3);
38  
39 use vars qw($Var1 %Hashit);
40 # non-exported package globals go here
41 use vars qw();
42  
43 # file-private lexicals go here
44 my %fields = (
45 target => undef,
46 secret => undef,
47 resolve => 1,
48 server => undef,
49 context => 0
50 );
51  
52 our $AUTOLOAD;
53 our $USE_LOCAL_MESSAGE = 1;
54 our $XRI_AUTHORITY = "http://beta.xri.net/xdi2-xrinet/";
55  
56 sub new {
57 my $class = shift;
58 my $xdi = shift;
59 my $self = {%fields,};
60 bless($self,$class);
61 if (ref $xdi ne 'XDI') {
62 my ($p,$s) = XDI->s_debug();
63 carp "$p requires object 'XDI' in $s";
64 return undef;
65 } else {
66 $self->{'__xdi__'} = $xdi;
67 }
68 my ($var_hash) = @_;
69 if (defined $var_hash ) {
70 if (ref $var_hash eq "HASH"){
71 foreach my $varkey (keys %{$var_hash}) {
72 if (exists $self->{$varkey}) {
73 $self->{$varkey} = $var_hash->{$varkey};
74 }
75 }
76 } else {
77 croak "Initialization failed: parameters not passed as hash reference or iname string";
78 }
79 }
80 $self->{'server'} = lookup($self->target)->[2] unless (defined $self->{'server'});
81
82 return $self;
83 }
84  
85 sub message {
86 my $self = shift;
87 #TODO: fix constructor to allow for messages passed from XDI::Connection
88 my $msg = XDI::Message->new(@_);
89 $msg->from_graph($self->__xdi__->from_graph);
90 $msg->from($self->__xdi__->from);
91 $msg->target($self->target);
92 $msg->secret($self->secret) if (defined $self->secret);
93
94 return $msg;
95 }
96  
97  
98 sub post {
99 my $self = shift;
100 my $logger = get_logger();
101 my ($msg,$test) = @_;
102 my $body;
103 if (ref $msg eq 'XDI::Message') {
104 $body = $msg->to_string();
105 } elsif(ref $msg eq '') {
106 $body = $msg;
107 } else {
108 return undef;
109 }
110 my $resp = _post($self->server,$body,$self->context);
111 if (defined $resp) {
112 return _xdi_response($resp);
113 } else {
114 return undef;
115 }
116 }
117  
118 sub _post {
119 my ($server, $body, $context) = @_;
120 my $logger = get_logger();
121 my $request = HTTP::Request->new( 'POST', $server);
122 my $ua = new LWP::UserAgent;
123 my $cheader = 'application/xdi+json';
124 if ($context) {
125 $cheader .= ';contexts=1';
126 }
127 $request->header('accept' => $cheader);
128 $request->content($body);
129 my $response = $ua->request($request);
130 my $code = $response->code;
131 if ($response->is_success()) {
132 return $response->content;
133 } else {
134 carp "Post: ", $response->status_line;
135 return undef;
136 }
137
138 }
139  
140 sub _xdi_response {
141 my ($json) = @_;
142 my $struct = XDI::_decode($json);
143 if (defined $struct) {
144 my $tuple = XDI::pick_xdi_tuple($struct,['$false$string','!']);
145 if (defined $tuple) {
146 carp "XDI server returned \$false: ",$tuple->[2];
147 return undef;
148 } else {
149 return $struct;
150 }
151 }
152 return undef;
153
154 }
155  
156 sub lookup {
157 my $obj = shift;
158 my $xdi;
159 if (ref $obj eq "XDI::Connection") {
160 $xdi = shift;
161 } else {
162 $xdi = $obj;
163 }
164 if (XDI::is_inumber($xdi)) {
165 return inumber_lookup($xdi);
166 } else {
167 return iname_lookup($xdi);
168 }
169 }
170  
171 sub iname_lookup {
172 my $obj = shift;
173 my $iname;
174 if (ref $obj eq "XDI::Connection") {
175 $iname = shift;
176 } else {
177 $iname = $obj;
178 }
179 my $struct = xdi_lookup($iname);
180 my $temp = XDI::pick_xdi_tuple($struct,[$iname,'$is']);
181 my $inumber = $temp->[2];
182 my $subject = '('. $inumber . ')$!($uri)';
183 $temp = XDI::pick_xdi_tuple($struct,[$subject,'!']);
184 my $url = $temp->[2];
185 return [$iname,$inumber,$url];
186
187 }
188  
189 sub inumber_lookup {
190 my $obj = shift;
191 my $inumber;
192 if (ref $obj eq "XDI::Connection") {
193 $inumber = shift;
194 } else {
195 $inumber = $obj;
196 }
197 my $iname = undef;
198 my $struct = xdi_lookup($inumber);
199 #print Dumper($struct);
200 my $subject = '('. $inumber . ')$!($uri)';
201 my $temp = XDI::pick_xdi_tuple($struct,[$subject,'!']);
202 my $url = $temp->[2];
203 return [$iname,$inumber,$url];
204
205 }
206  
207  
208 sub xdi_lookup {
209 my $obj = shift;
210 my $iname;
211 if (ref $obj eq "XDI::Connection") {
212 $iname = shift;
213 } else {
214 $iname = $obj;
215 }
216 my $authority = $XRI_AUTHORITY;
217 # Populate the msg with dummy XDI data
218 my $rstruct = {
219 "from_graph" => '=1111',
220 "from" => '=1111',
221 "target" => $iname,
222 "link_contract" => '',
223 };
224 my $msg = XDI::Message->new($rstruct);
225 $msg->get($iname);
226 #print "$authority: ",$msg->to_string, "\n";
227 my $resp = _post($authority,$msg->to_string);
228 return XDI::_decode($resp);
229 }
230  
231 sub set_resolution_authority {
232 my ($authority) = @_;
233 $XRI_AUTHORITY = $authority;
234 return $XRI_AUTHORITY;
235 }
236  
237 sub AUTOLOAD {
238 my $self = shift;
239 my $type = ref($self)
240 or croak "($AUTOLOAD): $self is not an object";
241 my $name = $AUTOLOAD;
242 $name =~ s/.*://;
243 unless ( exists $self->{$name} ) {
244 carp "$name not permitted in class $type";
245 return;
246 }
247  
248 if (@_) {
249 my $obj = shift;
250 if ( ref $obj ne "" ) {
251 return $self->{$name} = dclone $obj;
252 }
253 else {
254 return $self->{$name} = $obj;
255 }
256  
257 }
258 else {
259 return $self->{$name};
260 }
261 }
262  
263 sub DESTROY { }
264  
265 END { } # module clean-up code here (global destructor)
266  
267 1;
268  
269 =head1 NAME
270  
271 XDI::Connection - XDI Connection and iname resolution object
272  
273 =head1 VERSION
274  
275 Version 0.01
276  
277 =cut
278  
279 =head1 SYNOPSIS
280
281 use XDI;
282
283 my $xdi = new XDI;
284 my $c = $xdi->connect();
285
286 ..
287
288 my $xdi_hash = {
289 'from' => '=my_graph',
290 'from_graph' => '@xdiserver'
291 };
292
293 my $xdi = XDI->new($hash);
294 my $c = $xdi->connect({ target => '=other_graph', secret => "foosh"});
295
296 =head1 EXPORTS
297  
298 iname_lookup
299 inumber_lookup
300 lookup
301  
302 =head1 XDI::Connection
303  
304 Set the target graph and shared secret for the object. Default behavior is to do
305 a lookup on I<target> to get the inumber and server URI that is hosting the graph. You can
306 provide either an iname or an inumber as the I<target> value, but the current services
307 do not allow an inumber to be resolved back to an iname.
308  
309 =head1 DESCRIPTION
310  
311 The XDI perl module provides iname resolution and provides an http post method to send
312 messages
313  
314 =head2 Notation and Conventions
315  
316 $xdi Root object defining identity of querier
317 $c Connection object defining graph target and permissions
318 $msg Message object for XDI messages
319 $hash Reference to a hash of key/attribute values
320 $target iname or inumber of the graph which is queried
321 $secret Shared secret for access to the I<target> graph
322  
323 =head2 Usage Outline
324  
325 The primary method for creating a Connection object is via the C<connect()> method of the XDI object
326  
327 $c = $xdi->connect();
328
329  
330 You can pass any XDI::Connection initialization parameters to C<connect()>
331  
332 $hash = {
333 target => E<lt>iname|inumberE<gt>, # eg: =my_personal_cloud
334 secret => $secret
335 };
336
337 $c = $xdi->connect($hash);
338
339 Given an I<iname>, the default behavior for XDI is to attempt to resolve the iname to it's
340 corresponding inumber using the iname resolution service at xri2xdi.net. This service
341 also returns the URI to the graph that is authoritative for said I<iname>, during testing and
342 development that often proves inconvenient so you can override this behavior
343  
344 $c->resolve(0);
345
346 Of course, if you do that, you will have to specify the URI of the graph to which you are
347 sending a query
348  
349 $c->server("http://example/=my_personal_cloud");
350
351 You may have noticed that we haven't actually I<connected> to anything yet. I reserve the right
352 to make checking the target graph for a valid link contract part of the connection process
353  
354 =head3 secret
355  
356 =over 2
357  
358 XDI security policy is currently under discussion by the XDI Technical Committee so the placeholder for a more robust policy is to use a shared secret. Please note that the policy allows for arbitrarily complex expressions and Javascript is proposed for the expression syntax. L<https://wiki.oasis-open.org/xdi/XdiPolicyExpression> As the policy matures, I expect to need to update the client
359  
360 =back
361  
362 $graph = $c->post($msg);
363
364 The result is a JSON encoded representation of the nodes requested in the $get operation. Other
365 operations will return an empty hash {} upon success. Default behavior is to automatically convert
366 the JSON to a perl hash object
367  
368 =head1 SUBROUTINES/METHODS
369  
370 XDI and it's members support the common PERL OO-style syntax via AUTOLOAD
371  
372 =head2 Constructor
373  
374 Generally, the constructor for the XDI::Connection object is not called directly.
375
376 =head2 Instance variables
377  
378 Instance variables are only accessible through the respective getter/setter methods. Allowed
379 variables are:
380  
381 =over 4
382  
383 =item C<target> #iname/inumber (string)
384  
385 =item C<secret> #target graph's shared secret (string)
386  
387 =item C<resolve> #perform automatic lookup on I<target> (boolean)
388  
389 =item C<server> #url (string)
390  
391 =item C<context> #include contexts nodes in results (boolean)
392  
393 =back
394  
395 =head2 message
396  
397 $msg = $c->message();
398
399 As usual, you can pass valid message constructor parameters to XDI::Connection::message
400  
401 $msg = $c->message({ link_contract => '=!1111'});
402
403 =head2 post
404  
405 $result = $c->post($msg);
406 $result = $c->post($xdi_string);
407
408 I<post> accepts either a XDI::Message object or a perl string as a parameter and returns a perl
409 hash object. Currently $add operations return an empty hash {}. In case of an HTTP or XDI error,
410 undef is returned. The error message can be accessed by encapsulating the post in an eval block
411  
412 eval {
413 $result = $c->post($msg);
414 };
415
416 if ($@) {
417 # error handler
418 }
419
420 =head2 iname_lookup
421  
422 $tuple = iname_lookup('=tester');
423
424 =head2 inumber_lookup
425  
426 $tuple = inumber_lookup('=!3436.F6A6.3644.4D74');
427
428 =head2 lookup
429  
430 $tuple = lookup(<iname | inumber>);
431
432 C<$tuple> is [iname,inumber,url] # for inumber_lookup, C<iname> will be undef
433  
434 lookup, iname_lookup, and inumber_lookup are convenience functions to call the XDI resolution service
435 built into XDI::Connection. C<lookup> uses a simple method to determine whether the parameter
436 is an inumber or iname and then performs the appropriate I<x_lookup>
437  
438 Technically, these are both XDI messages to the $XRI_AUTHORITY server
439 but I<x_lookup> encapsulates the whole process into an anonymous XDI message.
440  
441 =head2 set_resolution_authority
442  
443 Since the XDI resolution process is still in development, C<set_resolution_authority> allows the
444 developer to specify explicitly the resolution server. In case the resolution URI changes,
445 this will allow discovery to continue until the module can be updated
446
447  
448 =head1 AUTHOR
449  
450 Mark Horstmeier <solargroovey@gmail.com>
451  
452 =head1 COPYRIGHT AND LICENSE
453  
454 Copyright (C) 2007-2012 Kynetx, Inc.
455  
456 The perl XDI client is free software; you can redistribute it and/or
457 modify it under the terms of the GNU General Public License as
458 published by the Free Software Foundation; either version 2 of
459 the License, or (at your option) any later version.
460  
461 This program is distributed in the hope that it will be
462 useful, but WITHOUT ANY WARRANTY; without even the implied
463 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
464 PURPOSE. See the GNU General Public License for more details.
465  
466 You should have received a copy of the GNU General Public
467 License along with this program; if not, write to the Free
468 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
469 MA 02111-1307 USA
470  
« XDI/Message.pm ↑ Summary