blib/lib/XAS/Lib/Stomp/Frame.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 180 CC 14 SCAR 27.9
CC
14
Cov
0%
as_string
42.0
_decode_headers
30.0
init
12.0
CRAP 16.2
1 package XAS::Lib::Stomp::Frame;
2  
3 our $VERSION = '0.01';
4  
5 use XAS::Class
6 debug => 0,
7 version => $VERSION,
8 base => 'XAS::Base',
9 accessors => 'eol header',
10 mutators => 'target command body',
11 constants => 'CRLF',
12 codec => 'unicode',
13 constant => {
14 LF => "\n",
15 EOF => "\000",
16 },
17 vars => {
18 PARAMS => {
19 -body => { optional => 1, default => undef },
20 -command => { optional => 1, default => undef },
21 -headers => { optional => 1, default => undef },
22 -target => { optional => 1, default => '1.0', regex => qr/(1\.0|1\.1|1\.2)/ },
23 }
24 }
25 ;
26  
27 our %ENCODE_MAP = (
28 "\r" => "\\r",
29 "\n" => "\\n",
30 ":" => "\\c",
31 "\\" => "\\\\",
32 );
33  
34 our %DECODE_MAP = reverse %ENCODE_MAP;
35  
36 #use Data::Dumper;
37 #use Data::Hexdumper;
38  
39 # ----------------------------------------------------------------------
40 # Public Methods
41 # ----------------------------------------------------------------------
42  
43 sub as_string {
44 my $self = shift;
45  
46 # protocol spec is unclear about the case of the command,
47 # so uppercase the command, Why, just because I can.
48  
49 my $frame;
50 my $command = uc($self->command);
51 my $headers = $self->header->devolve;
52 my $body = $self->body;
53  
54 # special handling for NOOPs
55  
56 if ($command eq 'NOOP') {
57  
58 $command = '';
59 $headers = {};
60 $body = '';
61  
62 }
63  
64 if ($self->target > 1.1) {
65  
66 $frame = encode('utf8', $command) . $self->eol;
67  
68 } else {
69  
70 $frame = $command . $self->eol;
71  
72 }
73  
74 # v1.0 and v1.1 is unclear about spaces between headers and values
75 # nor the case of the header.
76 #
77 # v1.2 says there should be no 'padding' in headers and values, not
78 # sure what 'padding' means. It also adds the capability to 'escape'
79 # certain values. Please see %ENCODE_MAP and %DECODE_MAP for those
80 # values.
81 #
82 # So add a space and lowercase the header. Why, just because I can.
83  
84 if (keys %{$headers}) {
85  
86 $self->_encode_headers(\$headers) if ($self->target > 1.1);
87  
88 while (my ($key, $value) = each(%{$headers})) {
89  
90 if (defined($value)) {
91  
92 $frame .= lc($key) . ': ' . $value . $self->eol();
93  
94 }
95  
96 }
97  
98 } else {
99  
100 $frame .= $self->eol();
101  
102 }
103  
104 $frame .= $self->eol();
105 $frame .= $body;
106 $frame .= EOF;
107  
108 return $frame;
109  
110 }
111  
112 # ----------------------------------------------------------------------
113 # Private Methods
114 # ----------------------------------------------------------------------
115  
116 sub init {
117 my $class = shift;
118  
119 my $self = $class->SUPER::init(@_);
120
121 $headers = $self->headers || {};
122 $self->{eol} = ($self->target > 1.1) ? CRLF : LF;
123  
124 $self->_decode_headers(\$headers) if ($self->target > 1.1);
125 $self->{header} = XAS::Lib::Stomp::Frame::Headers->new($headers);
126  
127 return $self;
128  
129 }
130  
131 sub _encode_headers {
132 my $self = shift;
133 my $headers = shift; # a pointer to a reference of a hash, oh my...
134  
135 my $ENCODE_KEYS = '['.join('', map(sprintf('\\x%02x', ord($_)), keys(%ENCODE_MAP))).']';
136  
137 while (my ($k, $v) = each(%$$headers)) {
138  
139 $k = encode('utf8', $k);
140 $v = encode('utf8', $v);
141  
142 $v =~ s/($ENCODE_KEYS)/$ENCODE_MAP{$1}/ego;
143 $k =~ s/($ENCODE_KEYS)/$ENCODE_MAP{$1}/ego;
144  
145 $$headers->{$k} = $v;
146  
147 }
148  
149 }
150  
151 sub _decode_headers {
152 my $self = shift;
153 my $headers = shift; # a pointer to a reference of a hash, oh my...
154  
155 while (my ($k, $v) = each(%$$headers)) {
156  
157 $k = decode('utf8', $k);
158 $v = decode('utf8', $v);
159  
160 if ($v =~ m/(\\.)/) {
161  
162 unless ($v =~ s/(\\.)/$DECODE_MAP{$1}/eg) {
163  
164 $self->throw_msg(
165 'xas.lib.stomp.frame.decode_header',
166 'badval',
167 );
168  
169 }
170  
171 }
172  
173 if ($k =~ m/(\\.)/) {
174  
175 unless ($k =~ s/(\\.)/$DECODE_MAP{$1}/eg) {
176  
177 $self->throw_msg(
178 'xas.lib.stomp.frame.decode_header',
179 'badkey'
180 );
181  
182 }
183  
184 }
185  
186 $$headers->{$k} = $v;
187  
188 }
189  
190 }
191  
192 package # hide from cpan...
193 XAS::Lib::Stomp::Frame::Headers;
194  
195 our $VERSION = '0.01';
196  
197 use XAS::Class
198 version => $VERSION,
199 base => 'XAS::Base',
200 constants => 'REFS',
201 ;
202  
203 #use Data::Dumper;
204  
205 sub remove {
206 my ($self, $key) = @_;
207  
208 my @x = grep { $_ ne $key } @{$self->{_methods}};
209 $self->{_methods} = \@x;
210  
211 delete($self->{$key});
212  
213 no warnings;
214 no strict REFS;
215  
216 *$key = undef;
217  
218 }
219  
220 sub add {
221 my ($self, $key, $value) = @_;
222  
223 $key =~ s/-/_/g;
224  
225 $self->{$key} = $value;
226 push(@{$self->{_methods}}, $key);
227  
228 no warnings;
229 no strict REFS;
230  
231 *$key = sub {
232 my $self = shift;
233 $self->{$key} = shift if @_;
234 return $self->{$key};
235 };
236  
237 }
238  
239 sub introspect {
240 my $self = shift;
241  
242 return $self->{_methods};
243  
244 }
245  
246 sub devolve {
247 my $self = shift;
248  
249 my $value;
250 my $header = {};
251  
252 foreach my $key (@{$self->{_methods}}) {
253  
254 $value = $self->{$key};
255 $key =~ s/_/-/g;
256 $header->{$key} = $value;
257  
258 }
259  
260 return $header;
261  
262 }
263  
264 sub init {
265 my $self = shift;
266 my $configs = shift;
267  
268 $self->{config} = $configs;
269 $self->{_methods} = undef;
270  
271 # turn frame headers into mutators of there values
272  
273 while (my ($key, $value) = each(%$configs)) {
274  
275 $key =~ s/-/_/g;
276  
277 $self->{$key} = $value;
278 push(@{$self->{_methods}}, $key);
279  
280 no warnings;
281 no strict REFS;
282  
283 *$key = sub {
284 my $self = shift;
285 $self->{$key} = shift if @_;
286 return $self->{$key};
287 };
288  
289 }
290  
291 return $self;
292  
293 }
294  
295 1;
296  
297 __END__
298  
299 =head1 NAME
300  
301 XAS::Lib::Stomp::Frame - A STOMP Frame
302  
303 =head1 SYNOPSIS
304  
305 use XAS::Lib::Stomp::Frame;
306  
307 my $frame = XAS::Lib::Stomp::Frame->new(
308 -target => '1.0',
309 -command => $command,
310 -headers => $headers,
311 -body => $body,
312 );
313  
314 ... or ...
315  
316 my $frame = XAS:::Lib::Stomp::Frame->new();
317  
318 $frame->target('1.0');
319 $frame->command('MESSAGE');
320 $frame->header->add('destination', '/queue/foo');
321 $frame->body('this is the body');
322  
323 ... stringification ...
324  
325 my $string = $frame->as_string;
326  
327 =head1 DESCRIPTION
328  
329 This module encapsulates a STOMP frame. STOMP is the Streaming Text
330 Orientated Messaging Protocol (or the Protocol Briefly Known as TTMP
331 and Represented by the symbol :ttmp). It's a simple and easy to
332 implement protocol for working with Message Orientated Middleware from
333 any language.
334  
335 A STOMP frame consists of a command, a series of headers and a body.
336  
337 =head1 METHODS
338  
339 =head2 new
340  
341 Create a new XAS::Lib::Stomp::Frame object:
342  
343 my $frame = XAS::Lib::Stomp::Frame->new(
344 -command => $command,
345 -headers => $headers,
346 -body => $body,
347 );
348  
349 It can take the following parameters:
350  
351 =over 4
352  
353 =item B<-target>
354  
355 Specify a STOMP protocol version number. It currently supports 1.0,
356 1.1 and 1.2, defaulting to 1.0.
357  
358 =item B<-command>
359  
360 The command verb.
361  
362 =item B<-headers>
363  
364 Headers for this command. This supports the 'bytes_message' header which
365 indicates a binary body.
366  
367 =item B<-body>
368  
369 A body for the command.
370  
371 =back
372  
373 =head2 as_string
374  
375 Create a buffer from the serialized frame.
376  
377 my $buffer = $frame->as_string;
378  
379 =head2 header
380  
381 This returns a XAS::Lib::Stomp::Frame::Headers object. This object contains
382 auto generated mutators of the header fields in a STOMP frame.
383  
384 =head1 MUTATORS
385  
386 =head2 command
387  
388 This get/sets STOMP frames command verb.
389  
390 =head2 body
391  
392 This get/sets the body of the STOMP frame.
393  
394 =head1 XAS::Lib::Stomp::Frame::Headers
395  
396 This is an internal class that auto generates mutators for the headers in a
397 STOMP frame. Any dashes in the header names are converted to underscores
398 for the mutators name.
399  
400 Example, a header of:
401  
402 content-type: test/plain
403  
404 Will become the mutator content_type().
405  
406 The usual way to access the headers is as follows:
407  
408 my $type = $frame->header->content_type;
409  
410 $frame->header->content_type('text/plain');
411  
412 The following methods are also available.
413  
414 =head2 devolve
415  
416 This will create a hash with header/value pairs. Any underscores are
417 converted to dashes in the headers name. Primarily used during
418 stringification of the STOMP frame.
419  
420 =head2 introspect
421  
422 This will return a list of headers.
423  
424 =head2 add($name, $value)
425  
426 This will add a header.
427  
428 =over 4
429  
430 =item B<$name>
431  
432 The name of the header.
433  
434 =item B<$value>
435  
436 The value for the header.
437  
438 =back
439  
440 =head2 remove($name)
441  
442 This will remove a header.
443  
444 =over 4
445  
446 =item B<$name>
447  
448 The name of the header to remove.
449  
450 =back
451  
452 =head1 ACKNOWLEDGEMENTS
453  
454 This module is based on L<Net::Stomp::Frame|https://metacpan.org/pod/Net::Stomp::Frame> by Leon Brocard <acme@astray.com>.
455  
456 =head1 SEE ALSO
457  
458 =over 4
459  
460 =item L<XAS|XAS>
461  
462 =back
463  
464 For more information on the STOMP protocol, please refer to: L<http://stomp.github.io/> .
465  
466 =head1 AUTHOR
467  
468 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
469  
470 =head1 COPYRIGHT AND LICENSE
471  
472 Copyright (C) 2014 Kevin L. Esteb
473  
474 This library is free software; you can redistribute it and/or modify
475 it under the same terms as Perl itself, either Perl version 5.8.8 or,
476 at your option, any later version of Perl 5 you may have available.
477  
478 See L<http://dev.perl.org/licenses/> for more information.
479  
480 =cut