| 1 | package Catalyst::Controller::MessageDriven; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Data::Serializer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use MooseX::Types::Moose qw/Str/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | BEGIN { extends 'Catalyst::Controller' } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Catalyst::Controller::MessageDriven | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | package MyApp::Controller::Queue; | ||||||||||||||||||||||||||||
| 17 | use Moose; | ||||||||||||||||||||||||||||
| 18 | BEGIN { extends 'Catalyst::Controller::MessageDriven' } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub some_action : Local { | ||||||||||||||||||||||||||||
| 21 | my ($self, $c, $message) = @_; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Handle message | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Reply with a minimal response message | ||||||||||||||||||||||||||||
| 26 | my $response = { type => 'testaction_response' }; | ||||||||||||||||||||||||||||
| 27 | $c->stash->{response} = $response; | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | A Catalyst controller base class for use with Catalyst::Engine::Stomp, | ||||||||||||||||||||||||||||
| 33 | which handles YAML-serialized messages. A top-level "type" key in the | ||||||||||||||||||||||||||||
| 34 | YAML determines the action dispatched to. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head2 begin | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Deserializes the request into C<< $c->stash->{request} >> | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 default | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Dispatches to method named by the key C<< $c->stash->{request}->{type} >> | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head2 end | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Serializes the response from C<< $c->stash->{response} >> | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | In the configuration file add the following to set the value for a parameter | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | <MessageDriven> | ||||||||||||||||||||||||||||
| 55 | type_key foo | ||||||||||||||||||||||||||||
| 56 | </MessageDriven> | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head2 type_key | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | The hash key the module will try to pull out the received message to call | ||||||||||||||||||||||||||||
| 61 | within the controller. This defaults to 'type'. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 serializer | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | The serializer used to serialiser/deserialise. See Data::Serializer to see | ||||||||||||||||||||||||||||
| 66 | what is available. Defaults to YAML. JSON is anotther that is available. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | class_type 'Data::Serializer'; | |||||||||||||||||||||||||||
| 72 | 12 | ▶ | my $serializer_t = subtype 'Data::Serializer', where { 1 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | coerce $serializer_t, from 'Str', | ||||||||||||||||||||||||||||
| 74 | 12 | ▶ | via { Data::Serializer->new( serializer => $_ ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | has serializer => ( | |||||||||||||||||||||||||||
| 77 | isa => $serializer_t, is => 'ro', required => 1, | ||||||||||||||||||||||||||||
| 78 | default => 'YAML', coerce => 1, | ||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | has type_key => ( | |||||||||||||||||||||||||||
| 82 | is => 'ro', required =>1, | ||||||||||||||||||||||||||||
| 83 | default => 'type', | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | has trust_jmstype => ( | |||||||||||||||||||||||||||
| 87 | is => 'ro', required => 1, | ||||||||||||||||||||||||||||
| 88 | default => 0, | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub begin : Private { | ||||||||||||||||||||||||||
|
Subroutine
begin: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 0 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Deserialize the request message | ||||||||||||||||||||||||||||
| 96 | 0 | my $message; | |||||||||||||||||||||||||||
| 97 | 0 | my $s = $self->serializer; | |||||||||||||||||||||||||||
| 98 | 0 | eval { | |||||||||||||||||||||||||||
| 99 | 0 | my $body = $c->request->body; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | open my $IN, "$body" or die "can't open temp file $body"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | my $raw_request = do { local $/; <$IN> }; | |||||||||||||||||||||||||||
| 102 | 0 | $c->stash->{raw_request} = $raw_request; | |||||||||||||||||||||||||||
| 103 | 0 | $message = $s->raw_deserialize($raw_request); | |||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | # can't reply - reply_to is embedded in the message | ||||||||||||||||||||||||||||
| 107 | 0 | $c->error("exception in deserialize: $@"); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||
| 110 | 0 | $c->stash->{request} = $message; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 2 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub _errors_to_response { | ||||||||||||||||||||||||||
|
Subroutine
_errors_to_response: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 115 | 0 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ( scalar(@{$c->error}) == 1 && ref($c->error->[0]) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
scalar @{$c->error;} == 1 and ref $c->error->[0]
MC/DC0%0/2
scalar @{$c->error;} == 1 and ref $c->error->[0]
scalar @{$c->error;} == 1
ref $c->error->[0]
Truth table0%0/3
A and B
A= scalar @{$c->error;} == 1B= ref $c->error->[0]
| |||||||||||||||||||||||||||||
| 118 | # A single object exists as an error, throw that back as is | ||||||||||||||||||||||||||||
| 119 | 0 | $c->log->error('Exception thrown: ' . $c->error->[0]); | |||||||||||||||||||||||||||
| 120 | 0 | return $c->error->[0]; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | 0 | $c->log->error($_) for @{$c->error}; # Log errors in Catalyst | |||||||||||||||||||||||||||
| 124 | 0 | my $error = join "\n", @{$c->error}; # Stringyfy them | |||||||||||||||||||||||||||
| 125 | 0 | return { status => 'ERROR', error => $error}; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | return; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub end : Private { | ||||||||||||||||||||||||||
|
Subroutine
end: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 0 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # Engine will send our reply based on the value of this header. | ||||||||||||||||||||||||||||
| 135 | 0 | $c->response->headers->header( 'X-Reply-Address' => $c->stash->{request}->{reply_to} ); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # The wire response | ||||||||||||||||||||||||||||
| 138 | 0 | my $output; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Load a serializer | ||||||||||||||||||||||||||||
| 141 | 0 | my $s = $self->serializer; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Custom error handler - steal errors from catalyst and dump them into | ||||||||||||||||||||||||||||
| 144 | # the stash, to get them serialized out as the reply. | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if (scalar @{$c->error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $c->stash->{response} = $self->_errors_to_response($c); | |||||||||||||||||||||||||||
| 147 | 0 | $c->clear_errors; | |||||||||||||||||||||||||||
| 148 | 0 | $c->response->status(400); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # Serialize the response | ||||||||||||||||||||||||||||
| 152 | 0 | eval { | |||||||||||||||||||||||||||
| 153 | 0 | $output = $s->raw_serialize( $c->stash->{response} ); | |||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | my $error = "exception in serialize: $@"; | |||||||||||||||||||||||||||
| 157 | 0 | $c->stash->{response} = { status => 'ERROR', error => $error }; | |||||||||||||||||||||||||||
| 158 | 0 | $output = $s->serialize( $c->stash->{response} ); | |||||||||||||||||||||||||||
| 159 | 0 | $c->response->status(400); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | $c->response->output( $output ); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 2 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub default : Private { | ||||||||||||||||||||||||||
|
Subroutine
default: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | 0 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # Forward the request to the appropriate action, based on the | ||||||||||||||||||||||||||||
| 170 | # message type. | ||||||||||||||||||||||||||||
| 171 | 0 | my $action; | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | if ( !defined $self->type_key | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $self->type_key or $self->trust_jmstype
MC/DC0%0/2
not defined $self->type_key or $self->trust_jmstype
not defined $self->type_key
$self->trust_jmstype
Truth table0%0/3
not(A) or B
A= not defined $self->type_keyB= $self->trust_jmstype
| |||||||||||||||||||||||||||||
| 173 | or $self->trust_jmstype ) { | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | $action = $c->request->headers->header('JMSType') | ||||||||||||||||||||||||||
|
Condition0%0/3
$c->request->headers->header('JMSType') || $c->request->headers->header('type')
MC/DC0%0/2
$c->request->headers->header('JMSType') || $c->request->headers->header('type')
$c->request->headers->header('JMSType')
$c->request->headers->header('type')
Truth table0%0/3
A || B
A= $c->request->headers->header('JMSType')B= $c->request->headers->header('type')
| |||||||||||||||||||||||||||||
| 175 | || $c->request->headers->header('type'); | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | else { | ||||||||||||||||||||||||||||
| 178 | 0 | $action = $c->stash->{request}->{ $self->type_key }; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if (defined $action) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $c->forward($action, [$c->stash->{request},$c->request->headers]); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | else { | ||||||||||||||||||||||||||||
| 184 | 0 | $c->error('no message type specified'); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 2 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 AUTHOR AND CONTRIBUTORS | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | See information in L<Catalyst::Engine::Stomp> | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Copyright (C) 2009 Venda Ltd | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 199 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||||||||||
| 200 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =cut | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||