XDI/Message.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« XDI.pm ↑ Summary XDI/Connection.pm »
1 package XDI::Message;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Data::Dumper;
9 use Storable qw(dclone);
10 use Scalar::Util;
11 use Data::UUID;
12 use DateTime::Format::RFC3339;
13  
14 require Exporter;
15 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
16  
17 # set the version for version checking
18 $VERSION = 0.05;
19  
20 @ISA = qw(Exporter);
21 @EXPORT = qw();
22 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
23  
24 # your exported package globals go here,
25 # as well as any optionally exported functions
26 @EXPORT_OK = qw();
27  
28  
29 my %fields = (
30 id => undef,
31 timestamp => undef,
32 from_graph => undef,
33 from => undef,
34 target => undef,
35 link_contract => undef,
36 secret => undef,
37 operations => undef,
38 type => undef
39 );
40  
41  
42 our $AUTOLOAD;
43 our $USE_LOCAL_MESSAGE = 1;
44  
45 sub new {
46 my $class = shift;
47 my $self = {%fields,};
48 bless($self,$class);
49 my $ug = new Data::UUID;
50 # $self->{'id'} = $ug->create_str() ;
51 $self->{'id'} = int(rand 10);
52 $self->{'timestamp'} = &_timestamp;
53 $self->{'operations'} = [];
54 my ($var_hash) = @_;
55 if (defined $var_hash ) {
56 if (ref $var_hash eq "HASH"){
57 foreach my $varkey (keys %{$var_hash}) {
58 if (exists $self->{$varkey}) {
59 $self->{$varkey} = $var_hash->{$varkey};
60 }
61 }
62 } else {
63 croak "Initialization failed: parameters not passed as hash reference";
64 }
65 }
66 return $self;
67 }
68  
69  
70 sub AUTOLOAD {
71 my $self = shift;
72 my $type = ref($self)
73 or croak "($AUTOLOAD): $self is not an object";
74 my $name = $AUTOLOAD;
75 $name =~ s/.*://;
76 unless ( exists $self->{$name} ) {
77 carp "$name not permitted in class $type";
78 return;
79 }
80  
81 if (@_) {
82 my $obj = shift;
83 if ( ref $obj ne "" ) {
84 return $self->{$name} = dclone $obj;
85 }
86 else {
87 return $self->{$name} = $obj;
88 }
89  
90 }
91 else {
92 return $self->{$name};
93 }
94 }
95  
96 sub get {
97 my $self = shift;
98 my $statement = shift;
99 my $op = '$get';
100 return $self->_add_op($op,$statement);
101 }
102  
103 sub add {
104 my $self = shift;
105 my $statement = shift;
106 my $op = '$add';
107 return $self->_add_op($op,$statement);
108 }
109  
110 sub mod {
111 my $self = shift;
112 my $statement = shift;
113 my $op = '$mod';
114 return $self->_add_op($op,$statement);
115 }
116  
117 sub del {
118 my $self = shift;
119 my $statement = shift;
120 my $op = '$del';
121 return $self->_add_op($op,$statement);
122 }
123  
124 sub to_string {
125 my $self = shift;
126 my @statements;
127 push(@statements,$self->_local_requestor);
128 push(@statements,$self->_destination);
129 push(@statements,$self->_timestamp_statement);
130 push(@statements,$self->_link_contract);
131 if (defined $self->secret) {
132 push(@statements, $self->_auth_statement)
133 }
134 foreach my $op (@{$self->operations}) {
135 push(@statements,$self->_operation($self->type,$op));
136 }
137 return (join("\n",@statements));
138 }
139  
140 sub _id {
141 my $self = shift;
142 my $id = '$(!' . $self->id . ')';
143 return $id;
144 }
145  
146 sub _timestamp {
147 my $now = DateTime->now;
148 my $f = DateTime::Format::RFC3339->new();
149 my $ts = $f->format_datetime($now);
150 return $ts;
151 }
152  
153 sub _local_requestor {
154 my $self = shift;
155 my $string = "(" . $self->from_graph . ')/$add/' . $self->from . '$($msg)'. $self->_id;
156 return $string;
157 }
158  
159 sub _destination {
160 my $self = shift;
161 my $string = $self->from . '$($msg)' . $self->_id . '/$is()/(' . $self->target . ')';
162 return $string;
163 }
164  
165 sub _timestamp_statement {
166 my $self = shift;
167 my $string = $self->from . '$($msg)' . $self->_id . '$d/!/(data:,' . $self->timestamp . ')';
168 return $string;
169 }
170  
171 sub _link_contract {
172 my $self = shift;
173 my $string = $self->from . '$($msg)' . $self->_id . '/$do/' . $self->link_contract . '$do';
174 return $string;
175 }
176  
177 sub _operation {
178 my $self = shift;
179 my ($op,$statement) = @_;
180 my $string = $self->from . '$($msg)' . $self->_id . '$do/' . $op . '/' . $statement;
181 return $string;
182 }
183  
184 sub _auth_statement {
185 my $self = shift;
186 my $string = $self->from . '$($msg)' . $self->_id .'$secret$!($token)/!/(data:,' . $self->secret . ')';
187 return $string;
188 }
189  
190 sub _add_op {
191 my $self = shift;
192 my ($op,$statement) = @_;
193 if (! defined $self->type) {
194 $self->type($op);
195 } elsif ($self->type ne $op) {
196 carp "XDI message may only carry one type of operations: $op($self->{'type'})";
197 return 0;
198 }
199 push(@{$self->operations}, $statement);
200 return 1;
201 }
202  
203  
204 sub DESTROY { }
205  
206  
207 END { } # module clean-up code here (global destructor)
208  
209 1;
210  
211 =head1 NAME
212  
213 XDI::Message - Message object for XDI client
214  
215 =head1 VERSION
216  
217 Version 0.01
218  
219 =cut
220  
221 =head1 SYNOPSIS
222
223 use XDI;
224
225 my $xdi = XDI->new('=tester');
226 my $c = $xdi->connect({ target => '=markus', secret => "foosh"});
227 $msg = $c->message({link_contract => '=!91F2.8153.F600.AE24'});
228 $msg->add('(=markus/+friend/=tester)');
229 $c->post($msg);
230
231 =head1 EXPORTS
232  
233  
234 =head1 XDI::Message
235  
236 XDI::Message automatically contructs the XDI message 'envelope' and let's the user
237 enter queries in the XDI statement format. Queries or operations available to the user are
238 the common CRUD methods ($get,$mod,$add,$del in XDI syntax). Multiple statements can
239 be included in a message, but they must all be of the same operation type; ie: no mixing
240 $get and $mod in the same message.
241  
242 XDI statements in a message do not have an order of execution and currently a single failed
243 statement will roll back the entire message transaction.
244  
245 The progression from XDI to XDI::Connection to XDI::Message is designed to help construct the
246 message in short, logical steps--not throwing too many configuration options in one blow; however,
247 XDI::Message can stand alone to build the complete XDI message.
248  
249 Since the message transport is a simple http post, XDI::message can be converted to plain text
250 string and included in a post as the body content.
251  
252 =head1 DESCRIPTION
253  
254 The XDI::Message module provides an object for constructing XDI messages L<https://wiki.oasis-open.org/xdi/XdiMessagePatterns>
255  
256 =head2 Notation and Conventions
257  
258 $xdi Root object defining identity of querier
259 $c Connection object defining graph target and permissions
260 $msg Message object for XDI messages
261 $hash Reference to a hash of key/attribute values
262 $target iname or inumber of the graph which is queried
263 $secret Shared secret for access to the I<target> graph
264 $graph From graph
265 $from XDI entity making the query
266 $lc link contract
267
268 =head2 Usage Outline
269  
270 =head3 Using XDI::Message to construct a self-contained XDI message
271  
272 XDI messaging syntax is defined at L<https://wiki.oasis-open.org/xdi/XdiMessagePatterns>
273  
274 use XDI::Message;
275
276 my $msg = new XDI::Message;
277
278 Set the ID of the user making the query
279  
280 $msg->from("=tester");
281
282 Set the ID of the graph that is making the request. If this is being implemented in a
283 peer to peer manner, this will be the same ID as the requestor. In a mediated model where
284 a third party is making the requests on behalf of the requestor (perhaps a link contract management
285 provider that has permissions to create link contracts on the requestor's behalf) this will
286 be a different ID;
287  
288 $msg->from_graph('@kynetx');
289
290 The target graph is the authoritative graph for the XDI entity that will be queried. When
291 set in the XDI::Message context, it is up to the user to guarantee that the C<target> is
292 a valid XDI target
293  
294 $msg->target('@example');
295
296 Link contracts are still being developed. Some XDI servers may not yet enforce link contracts,
297 but those servers should ignore the link contract statement so it is always better to include
298 it. In the beginning phases of link contracts, try the target's graph root; ie: '@example' or '=tester'
299  
300 $msg->link_contract('@example');
301
302 Every graph owner can set the authorization method for their graph, but a shared secret is the
303 default method at this early time of implementation
304  
305 $msg->secret('kltpzyxM');
306
307 With the configuration done, the client can now configure the operations (CRUD) the user would
308 like to perform on the target graph
309
310  
311 =head1 SUBROUTINES/METHODS
312  
313 XDI and it's members support the common PERL OO-style syntax via AUTOLOAD
314  
315 The first operation method called (get,add,mod,del) will set the XDI::Message object type
316 and all further operations must be of the same type else the method will fail with an error.
317  
318 =head2 Constructor
319  
320 $msg = new XDI::Message;
321
322 $msg = XDI::Message({from => '=tester', secret => 'kltpzyxM'})
323  
324 =head2 get - Return an XDI graph
325  
326 Get the whole graph for a user
327  
328 $msg->get('=!1111');
329
330 Get a portion of the graph
331  
332 $msg->get('@example$(+passport)$!(+country)');
333 $msg->get('@example+tel$!1');
334
335 $msg->get('@example+passport');
336  
337 =head2 add - Add a new element to an XDI graph
338  
339 Add a literal node
340  
341 $msg->add('(@example+work+fax$!(+tel)/!/(data:,+1.801.555.1212))');
342
343 Add a new context node
344  
345 $msg->add('(@example/()/$*(+tel))');
346  
347 =head2 mod - Modify an existing element of an XDI graph
348  
349 $msg->mod('(@example$(+address)$!1$!(+state)/!/(data:,UT))');
350  
351 =head2 del - Delete a portion of an XDI graph
352  
353 $msg->del('@example+birthdate');
354  
355 =head2 to_string - Convert the XDI::Message object to a string
356  
357 $content = $msg->to_string();
358  
359 =head1 AUTHOR
360  
361 Mark Horstmeier <solargroovey@gmail.com>
362  
363 =head1 COPYRIGHT AND LICENSE
364  
365 Copyright (C) 2007-2012 Kynetx, Inc.
366  
367 The perl XDI client is free software; you can redistribute it and/or
368 modify it under the terms of the GNU General Public License as
369 published by the Free Software Foundation; either version 2 of
370 the License, or (at your option) any later version.
371  
372 This program is distributed in the hope that it will be
373 useful, but WITHOUT ANY WARRANTY; without even the implied
374 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
375 PURPOSE. See the GNU General Public License for more details.
376  
377 You should have received a copy of the GNU General Public
378 License along with this program; if not, write to the Free
379 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
380 MA 02111-1307 USA
381  
382  
« XDI.pm ↑ Summary XDI/Connection.pm »