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

filter: Statementstmt 0.0% 0 / 152 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 259 CC 39 SCAR 34.5
CC
39
Cov
0%
unsubscribe
56.0
nack
56.0
ack
42.0
CRAP 31.6
1 package XAS::Lib::Stomp::Utils;
2  
3 our $VERSION = '0.01';
4  
5 use XAS::Lib::Stomp::Frame;
6  
7 use XAS::Class
8 debug => 0,
9 version => $VERSION,
10 base => 'XAS::Base',
11 vars => {
12 PARAMS => {
13 -target => { optional => 1, default => '1.0', regex => qr/(1\.0|1\.1|1\.2)/ },
14 }
15 }
16 ;
17  
18 #use Data::Dumper;
19  
20 # -----------------------------------------------------------
21 # Public Methods
22 # -----------------------------------------------------------
23  
24 sub connect {
25 my $self = shift;
26  
27 my $p = $self->validate_params(\@_, {
28 -login => { optional => 1, default => undef },
29 -passcode => { optional => 1, default => undef },
30 -host => { optional => 1, default => 'localhost' },
31 -heart_beat => { optional => 1, default => '0,0', regex => qr/\d+,\d+/ },
32 -acceptable => { optional => 1, default => '1.0,1.1,1.2',
33 callbacks => {
34 'valid target' => \&_match
35 }
36 }
37 });
38  
39 my $frame;
40 my $header = {};
41  
42 $header->{'login'} = $p->{'login'} if (defined($p->{'login'}));
43 $header->{'passcode'} = $p->{'passcode'} if (defined($p->{'passcode'}));
44  
45 if ($self->target > 1.0) {
46  
47 $header->{'host'} = $p->{'host'};
48 $header->{'heart-beat'} = $p->{'heart_beat'};
49 $header->{'accept-version'} = $p->{'acceptable'};
50  
51 }
52  
53 $frame = XAS::Lib::Stomp::Frame->new(
54 -target => $self->target,
55 -command => 'CONNECT',
56 -headers => $header,
57 -body => ''
58 );
59  
60 return $frame;
61  
62 }
63  
64 sub stomp {
65 my $self = shift;
66  
67 my $p = $self->validate_params(\@_, {
68 -login => { optional => 1, default => undef },
69 -passcode => { optional => 1, default => undef },
70 -host => { optional => 1, default => 'localhost' },
71 -heart_beat => { optional => 1, default => '0,0', regex => qr/\d+,\d+/ },
72 -acceptable => {
73 optional => 1,
74 default => '1.0,1.1,1.2',
75 callbacks => {
76 'valid target' => \&_match
77 }
78 }
79 });
80  
81 my $frame;
82 my $header = {};
83  
84 if ($self->target == 1.0) {
85  
86 $self->throw_msg(
87 'xas.lib.stomp.utils.stomp',
88 'nosup',
89 $self->target,
90 'stomp'
91 );
92  
93 }
94  
95 $header->{'login'} = $p->{'login'} if (defined($p->{'login'}));
96 $header->{'passcode'} = $p->{'passcode'} if (defined($p->{'passcode'}));
97  
98 if ($self->target > 1.0) {
99  
100 $header->{'host'} = $p->{'host'};
101 $header->{'heart-beat'} = $p->{'heart_beat'};
102 $header->{'accept-version'} = $p->{'acceptable'};
103  
104 }
105  
106 $frame = XAS::Lib::Stomp::Frame->new(
107 -target => $self->target,
108 -command => 'STOMP',
109 -headers => $header,
110 -body => ''
111 );
112  
113 return $frame;
114  
115 }
116  
117 sub subscribe {
118 my $self = shift;
119  
120 my $p = $self->validate_params(\@_, {
121 -destination => 1,
122 -id => { optional => 1, default => undef },
123 -receipt => { optional => 1, default => undef },
124 -ack => { optional => 1, default => 'auto', regex => qr/auto|client|client\-individual/ },
125 });
126  
127 my $frame;
128 my $header = {};
129  
130 $header->{'ack'} = $p->{'ack'};
131 $header->{'destination'} = $p->{'destination'};
132 $header->{'receipt'} = $p->{'receipt'} if (defined($p->{'receipt'}));
133  
134 if (defined($p{'-id'})) {
135  
136 $header->{'id'} = $p->{'id'};
137  
138 } else {
139  
140 # v1.1 and greater must have an id header
141  
142 if ($self->target > 1.0) {
143  
144 $self->throw_msg(
145 'xas.lib.stomp.utils.subscribe',
146 'noid',
147 $self->target
148
149 );
150  
151 }
152  
153 }
154  
155 $frame = XAS::Lib::Stomp::Frame->new(
156 -target => $self->target,
157 -command => 'SUBSCRIBE',
158 -headers => $header,
159 -body => ''
160 );
161  
162 return $frame;
163  
164 }
165  
166 sub unsubscribe {
167 my $self = shift;
168  
169 my $p = $self->validate_params(\@_, {
170 -id => { optional => 1, default => undef },
171 -destination => { optional => 1, default => undef },
172 -receipt => { optional => 1, default => undef },
173 });
174  
175 my $frame;
176 my $header = {};
177  
178 $header->{'receipt'} = $p->{'receipt'} if (defined($p->{'receipt'}));
179  
180 # v1.0 should have either a destination and/or id header
181 # v1.1 and greater may have a destination header
182  
183 if (defined($p->{'destination'}) && defined($p->{'id'})) {
184  
185 $header->{'id'} = $p->{'id'};
186 $header->{'destination'} = $p->{'destination'};
187  
188 } elsif (defined($p->{'destination'})) {
189  
190 $header->{'destination'} = $p->{'destination'};
191  
192 } elsif (defined($p->{'id'})) {
193  
194 $header->{'id'} = $p->{'id'};
195  
196 } else {
197  
198 $self->throw_msg(
199 'xas.lib.stomp.utils.unsubscribe',
200 'nopar',
201 $self->target
202 );
203  
204 }
205  
206 if ($self->target > 1.0) {
207  
208 # v1.1 and greater must have an id header
209  
210 unless (defined($header->{'id'})) {
211  
212 $self->throw_msg(
213 'xas.lib.stomp.utils.unsubscribe',
214 'noid',
215 $self->target
216 );
217  
218 }
219  
220 }
221  
222 $frame = XAS::Lib::Stomp::Frame->new(
223 -target => $self->target,
224 -command => 'UNSUBSCRIBE',
225 -headers => $header,
226 -body => ''
227 );
228  
229 return $frame;
230  
231 }
232  
233 sub begin {
234 my $self = shift;
235  
236 my $p = $self->validate_params(\@_, {
237 -transaction => 1,
238 -receipt => { optional => 1, default => undef },
239 });
240  
241 my $frame;
242 my $header = {};
243  
244 $header->{'transaction'} = $p->{'transaction'};
245 $header->{'receipt'} = $p->{'receipt'} if (defined($p->{'receipt'}));
246  
247 $frame = XAS::Lib::Stomp::Frame->new(
248 -target => $self->target,
249 -command => 'BEGIN',
250 -headers => $header,
251 -body => ''
252 );
253  
254 return $frame;
255  
256 }
257  
258 sub commit {
259 my $self = shift;
260  
261 my $p = $self->validate_params(\@_, {
262 -transaction => 1,
263 -receipt => { optional => 1, default => undef },
264 });
265  
266 my $frame;
267 my $header = {};
268  
269 $header->{'transaction'} = $p->{'transaction'};
270 $header->{'receipt'} = $p->{'receipt'} if (defined($p->{'receipt'}));
271  
272 $frame = XAS::Lib::Stomp::Frame->new(
273 -target => $self->target,
274 -command => 'COMMIT',
275 -headers => $header,
276 -body => ''
277 );
278  
279 return $frame;
280  
281 }
282  
283 sub abort {
284 my $self = shift;
285  
286 my $p = $self->validate_params(\@_, {
287 -transaction => 1,
288 -receipt => { optional => 1, default => undef },
289 });
290  
291 my $frame;
292 my $header = {};
293  
294 $header->{'transaction'} = $p->{'transaction'};
295 $header->{'receipt'} = $p->{'receipt'} if (defined($p->{'receipt'}));
296  
297 $frame = XAS::Lib::Stomp::Frame->new(
298 -target => $self->target,
299 -command => 'ABORT',
300 -headers => $header,
301 -body => ''
302 );
303  
304 return $frame;
305  
306 }
307  
308 sub ack {
309 my $self = shift;
310  
311 my $p = $self->validate_params(\@_, {
312 -message_id => 1,
313 -subscription => { optional => 1, default => undef },
314 -receipt => { optional => 1, default => undef },
315 -transaction => { optional => 1, default => undef },
316 });
317  
318 my $frame;
319 my $header = {};
320  
321 $header->{'receipt'} = $p->{'receipt'} if (defined($p->{'receipt'}));
322 $header->{'transaction'} = $p->{'transaction'} if (defined($p->{'transaction'}));
323  
324 if ($self->target < 1.2) {
325  
326 $header->{'message-id'} = $p->{'message_id'};
327  
328 } else {
329  
330 $header->{'id'} = $p->{'message_id'};
331  
332 }
333  
334 if (defined($p->{'subscription'})) {
335  
336 $header->{'subscription'} = $p->{'subscription'};
337  
338 } else {
339  
340 if ($self->target > 1.0) {
341  
342 $self->throw_msg(
343 'xas.lib.stomp.utils.act',
344 'nosub',
345 $self->target
346 );
347  
348 }
349  
350 }
351  
352 $frame = XAS::Lib::Stomp::Frame->new(
353 -target => $self->target,
354 -command => 'ACK',
355 -headers => $header,
356 -body => ''
357 );
358  
359 return $frame;
360  
361 }
362  
363 sub nack {
364 my $self = shift;
365  
366 my $p = $self->validate_params(\@_, {
367 -message_id => 1,
368 -receipt => { optional => 1, default => undef },
369 -subscription => { optional => 1, default => undef },
370 -transaction => { optional => 1, default => undef },
371 });
372  
373 my $frame;
374 my $header = {};
375  
376 $header->{'receipt'} = $p->{'receipt'} if (defined($p->{'receipt'}));
377 $header->{'transaction'} = $p->{'transaction'} if (defined($p->{'transaction'}));
378  
379 if ($self->target == 1.0) {
380  
381 $self->throw_msg(
382 'xas.lib.stomp.utils.nack',
383 'nosup',
384 $self->target,
385 'nack'
386 );
387  
388 }
389  
390 if ($self->target < 1.2) {
391  
392 $header->{'message-id'} = $p->{'message_id'};
393  
394 } else {
395  
396 $header->{'id'} = $p->{'message_id'};
397  
398 }
399  
400 if (defined($p->{'subscription'})) {
401  
402 $header->{'subscription'} = $p->{'subscription'};
403  
404 } else {
405  
406 if ($self->target > 1.0) {
407  
408 $self->throw_msg(
409 'xas.lib.stomp.utils.nact',
410 'nosub',
411 $self->target
412 );
413  
414 }
415  
416 }
417  
418 $frame = XAS::Lib::Stomp::Frame->new(
419 -target => $self->target,
420 -command => 'NACK',
421 -headers => $header,
422 -body => ''
423 );
424  
425 return $frame;
426  
427 }
428  
429 sub disconnect {
430 my $self = shift;
431  
432 my $p = $self->validate_params(\@_, {
433 -receipt => { optional => 1, default => undef }
434 });
435  
436 my $frame;
437 my $header = {};
438  
439 $header->{'receipt'} = $p->{'receipt'} if (defined($p->{'receipt'}));
440  
441 $frame = XAS::Lib::Stomp::Frame->new(
442 -target => $self->target,
443 -command => 'DISCONNECT',
444 -headers => $header,
445 -body => ''
446 );
447  
448 return $frame;
449  
450 }
451  
452 sub send {
453 my $self = shift;
454  
455 my $p = $self->validate_params(\@_, {
456 -destination => 1,
457 -message => 1,
458 -receipt => { optional => 1, default => undef },
459 -persistent => { optional => 1, default => undef },
460 -transaction => { optional => 1, default => undef },
461 -length => { optional => 1, default => undef },
462 -type => { optional => 1, default => 'text/plain' },
463 });
464  
465 my $frame;
466 my $header = {};
467 my $body = $p->{'message'};
468  
469 $header->{'destination'} = $p->{'destination'};
470 $header->{'receipt'} = $p->{'receipt'} if (defined($p->{'receipt'}));
471 $header->{'persistent'} = $p->{'persistent'} if (defined($p->{'presistent'}));
472 $header->{'transaction'} = $p->{'transaction'} if (defined($p->{'transaction'}));
473 $header->{'content-length'} = defined($p->{'length'}) ? $p->{'length'} : length($body);
474  
475 if ($self->target > 1.0) {
476  
477 $header->{'content-type'} = $p->{'type'};
478  
479 }
480  
481 $frame = XAS::Lib::Stomp::Frame->new(
482 -target => $self->target,
483 -command => 'SEND',
484 -headers => $header,
485 -body => $body
486 );
487  
488 return $frame;
489  
490 }
491  
492 sub noop {
493 my $self = shift;
494  
495 my $frame;
496  
497 if ($self->target == 1.0) {
498  
499 $self->throw_msg(
500 'xas.lib.stomp.utils.noop',
501 'nosup',
502 $self->target,
503 'noop'
504 );
505  
506 }
507  
508 $frame = XAS::Lib::Stomp::Frame->new(
509 -target => $self->target,
510 -command => 'NOOP',
511 -headers => {},
512 -body => ''
513 );
514  
515 return $frame;
516  
517 }
518  
519 # -----------------------------------------------------------
520 # Private Methods
521 # -----------------------------------------------------------
522  
523 sub _match {
524 my $buffer = shift;
525  
526 foreach my $item (split(',', $buffer)) {
527  
528 return 0 if ($item !~ m/\d\.\d/);
529  
530 }
531  
532 return 1;
533  
534 }
535  
536 1;
537  
538 __END__
539  
540 =head1 NAME
541  
542 XAS::Lib::Stomp::Utils - STOMP protocol utilities for clients
543  
544 =head1 SYNOPSIS
545  
546 This module uses XAS::Lib::Stomp::Frame to create STOMP frames.
547  
548 use XAS::Lib::Stomp::Utils;
549  
550 my $stomp = XAS::Lib::Stomp::Utils->new();
551 my $frame = $stomp->connect(
552 -login => 'test',
553 -passcode => 'test'
554 );
555  
556 put($frame->to_string);
557  
558 =head1 DESCRIPTION
559  
560 This module is an easy way to create STOMP frames without worrying about
561 the various differences between the protocol versions.
562  
563 =head1 METHODS
564  
565 =head2 new
566  
567 This method initializes the base object. It takes the following parameters:
568  
569 =over 4
570  
571 =item B<-target>
572  
573 This is the targeted version of the STOMP protocol that the frame
574 will represent. Defaults to '1.0'.
575  
576 =back
577  
578 =head2 connect
579  
580 This method creates a "CONNECT" frame. This frame is used to initiate a
581 session with a STOMP server. On STOMP v1.1 and later targets the following
582 headers are automatically set:
583  
584 host
585 heart-beat
586 accept-version
587  
588 Unless otherwise specified, they will be the defaults. This method takes the
589 following parameters:
590  
591 =over 4
592  
593 =item B<-login>
594  
595 An optional login name to be used on the STOMP server.
596  
597 =item B<-passcode>
598  
599 An optional password for the login name on the STOMP server.
600  
601 =item B<-host>
602  
603 An optional virtual host name to connect to on STOMP v1.1 and later servers.
604 Defaults to 'localhost'.
605  
606 =item B<-heart_beat>
607  
608 An optional heart beat request for STOMP v1.1 and later servers. The default
609 is to turn them off.
610  
611 =item B<-acceptable>
612  
613 An optional list of protocol versions that are acceptable to this client for
614 STOMP v1.1 and later clients. The default is '1.0,1.1,1.2'.
615  
616 =back
617  
618 =head2 stomp
619  
620 This method creates a "STOMP" frame, this works the same as connect(), but
621 only works for STOMP v1.1 and later targets. Please see the documentation for
622 connect().
623  
624 =head2 disconnect
625  
626 This method creates a "DISCONNECT" frame. This frame is used to signal the
627 server that you no longer wish to communicate with it. This method takes the
628 following parameters:
629  
630 =over 4
631  
632 =item B<-receipt>
633  
634 An optional receipt that will be returned by the server.
635  
636 =back
637  
638 =head2 subscribe
639  
640 This method create a "SUBSCRIBE" frame. This frame is used to notify
641 the server which queues you want to listen too. The naming of queues is
642 left up to the server implementation. This method takes the following
643 parameters:
644  
645 =over 4
646  
647 =item B<-destination>
648  
649 The name of the queue you wish to subscribe too. Naming convention is
650 server dependent.
651  
652 =item B<-subscription>
653  
654 A mandatory subscription id for usage on STOMP v1.1 and later targets. It
655 has no meaning for STOMP v1.0 servers.
656  
657 =item B<-ack>
658  
659 The type of acknowledgement you would like to receive when messages are sent
660 to a queue. It defaults to 'auto'. It understands 'auto', 'client' and
661 'client-individual'. Please refer to the STOMP protocol reference for
662 what this means.
663  
664 =item B<-receipt>
665  
666 An optional receipt that will be returned by the server.
667  
668 =back
669  
670 =head2 unsubscribe
671  
672 This method creates an "UNSUBSCRIBE" frame. This frame is used to notify the
673 server that you don't want to subscribe to a queue anymore. Subsequently
674 any messages left on that queue will no longer be sent to your client.
675  
676 =over 4
677  
678 =item B<-destination>
679  
680 The optional name of the queue that you subscribed too. STOMP v1.0 targets
681 need a queue name and/or a subscription id to unsubscribe. This is optional
682 on v1.1 and later targets.
683  
684 =item B<-subscription>
685  
686 The id of the subscription, this should be the same as the one used
687 with subscribe(). This is optional on STOMP v1.0 servers and mandatory
688 on v1.1 and later targets.
689  
690 =item B<-receipt>
691  
692 An optional receipt that will be returned by the server.
693  
694 =back
695  
696 =head2 begin
697  
698 This method creates a "BEGIN" frame. This frame signals the server that a
699 transaction is beginning. A transaction is either ended by a "COMMIT" frame
700 or an "ABORT" frame. Any other frame that is sent must have a transaction id
701 associated with them. This method takes the following parameters:
702  
703 =over 4
704  
705 =item B<-transaction>
706  
707 The mandatory id for the transaction.
708  
709 =item B<-receipt>
710  
711 An optional receipt that will be returned by the server.
712  
713 =back
714  
715 =head2 commit
716  
717 This method creates a "COMMIT" frame. This frame signals the end of a
718 transaction. This method takes the following parameters:
719  
720 =over 4
721  
722 =item B<-transaction>
723  
724 The mandatory transaction id from begin().
725  
726 =item B<-receipt>
727  
728 An optional receipt that will be returned by the server.
729  
730 =back
731  
732 =head2 abort
733  
734 This method creates an "ABORT" frame. This frame is used to signal the server
735 that the current transaction is to be aborted.
736  
737 This method takes the following parameters:
738  
739 =over 4
740  
741 =item B<-transaction>
742  
743 The mandatory transaction id from begin().
744  
745 =item B<-receipt>
746  
747 An optional receipt that will be returned by the server.
748  
749 =back
750  
751 =head2 send
752  
753 This method creates a "SEND" frame. This frame is the basis of communication
754 over your queues to the server. This method takes the following parameters:
755  
756 =over 4
757  
758 =item B<-destination>
759  
760 The name of the queue to send the message too.
761  
762 =item B<-message>
763  
764 The message to be sent. No attempt is made to serializes the message.
765  
766 =item B<-transaction>
767  
768 An optional transaction number. This should be the same as for begin().
769  
770 =item B<-length>
771  
772 An optional length for the message. If one is not specified a
773 'content-length' header will be auto generated.
774  
775 =item B<-type>
776  
777 An optional MIME type for the message. If one is not specified, 'text/plain'
778 will be used. This only has meaning for STOMP v1.1 and later targets.
779  
780 =item B<-persistent>
781  
782 An optional header for indicating that this frame should be 'persisted' by
783 the server. What this means, is highly server specific.
784  
785 =item B<-receipt>
786  
787 An optional receipt that will be returned by the server.
788  
789 =back
790  
791 =head2 ack
792  
793 This method creates an "ACK" frame. This frame is used to tell the server that
794 the message was successfully received. This method takes the following
795 parameters:
796  
797 =over 4
798  
799 =item B<-message_id>
800  
801 The id of the message that is being acked.
802  
803 =item B<-subscription>
804  
805 This should match the id from the subscribe() method. This has meaning for
806 STOMP v1.1 and later targets.
807  
808 =item B<-transaction>
809  
810 The transaction id if this ack is part of a transaction.
811  
812 =item B<-receipt>
813  
814 An optional receipt that will be returned by the server.
815  
816 =back
817  
818 =head2 nack
819  
820 This method creates a "NACK" frame. It notifies the server that the message
821 was rejected. It has meaning on STOMP v1.1 and later targets. This method
822 takes the following parameters:
823  
824 =over 4
825  
826 =item B<-message_id>
827  
828 The id of the message that is being nacked.
829  
830 =item B<-subscription>
831  
832 This should match the id from the subscribe() method. This has meaning for
833 STOMP v1.1 and later targets.
834  
835 =item B<-transaction>
836  
837 The transaction id if this nack is part of a transaction.
838  
839 =item B<-receipt>
840  
841 An optional receipt that will be returned by the server.
842  
843 =back
844  
845 =head2 noop
846  
847 This method creates a "NOOP" frame. It has meaning on STOMP v1.1 and
848 later targets.
849  
850 =head1 SEE ALSO
851  
852 =over 4
853  
854 =item L<Net::Stomp|https://metacpan.org/pod/Net::Stomp>
855  
856 =item L<Net::Stomp::Frame|https://metacpan.org/pod/Net::Stomp::Frame>
857  
858 =item L<XAS|XAS>
859  
860 =back
861  
862 For more information on the STOMP protocol, please refer to: L<http://stomp.github.io/> .
863  
864 =head1 AUTHOR
865  
866 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
867  
868 =head1 COPYRIGHT AND LICENSE
869  
870 Copyright (C) 2014 Kevin L. Esteb
871  
872 This library is free software; you can redistribute it and/or modify
873 it under the same terms as Perl itself, either Perl version 5.8.8 or,
874 at your option, any later version of Perl 5 you may have available.
875  
876 See L<http://dev.perl.org/licenses/> for more information.
877  
878 =cut