Whitespace nazism to remove trailing and tabs
[catagits/Catalyst-Engine-STOMP.git] / lib / Catalyst / Controller / MessageDriven.pm
CommitLineData
0a663589 1package Catalyst::Controller::MessageDriven;
2use Moose;
a5ae1e8c 3use Data::Serializer;
0a663589 4
5BEGIN { extends 'Catalyst::Controller' }
6
d78bb739 7=head1 NAME
8
9Catalyst::Controller::MessageDriven
10
11=head1 SYNOPSIS
12
13 package MyApp::Controller::Queue;
14 use Moose;
15 BEGIN { extends 'Catalyst::Controller::MessageDriven' }
16
5ec5e0b1 17 sub some_action : Local {
bf8937b7 18 my ($self, $c, $message) = @_;
19
5ec5e0b1 20 # Handle message
bf8937b7 21
d78bb739 22 # Reply with a minimal response message
23 my $response = { type => 'testaction_response' };
24 $c->stash->{response} = $response;
25 }
26
27=head1 DESCRIPTION
28
29A Catalyst controller base class for use with Catalyst::Engine::Stomp,
30which handles YAML-serialized messages. A top-level "type" key in the
5ec5e0b1 31YAML determines the action dispatched to.
d78bb739 32
33=cut
34
a5ae1e8c 35__PACKAGE__->config( serializer => 'YAML' );
36
5ec5e0b1 37sub begin : Private {
38 my ($self, $c) = @_;
39
40 # Deserialize the request message
bf8937b7 41 my $message;
5ec5e0b1 42 my $serializer = $self->config->{serializer};
43 my $s = Data::Serializer->new( serializer => $serializer );
44 eval {
45 my $body = $c->request->body;
46 open my $IN, "$body" or die "can't open temp file $body";
47 $message = $s->raw_deserialize(do { local $/; <$IN> });
48 };
49 if ($@) {
50 # can't reply - reply_to is embedded in the message
51 $c->error("exception in deserialize: $@");
52 }
53 else {
54 $c->stash->{request} = $message;
55 }
bf8937b7 56}
0a663589 57
bf8937b7 58sub end : Private {
5ec5e0b1 59 my ($self, $c) = @_;
60
61 # Engine will send our reply based on the value of this header.
62 $c->response->headers->header( 'X-Reply-Address' => $c->stash->{request}->{reply_to} );
63
64 # The wire response
65 my $output;
66
67 # Load a serializer
68 my $serializer = $self->config->{serializer};
69 my $s = Data::Serializer->new( serializer => $serializer );
70
71 # Custom error handler - steal errors from catalyst and dump them into
72 # the stash, to get them serialized out as the reply.
73 if (scalar @{$c->error}) {
74 my $error = join "\n", @{$c->error};
75 $c->stash->{response} = { status => 'ERROR', error => $error };
76 $output = $s->serialize( $c->stash->{response} );
77 $c->clear_errors;
78 $c->response->status(400);
79 }
80
81 # Serialize the response
82 eval {
83 $output = $s->raw_serialize( $c->stash->{response} );
84 };
85 if ($@) {
86 my $error = "exception in serialize: $@";
87 $c->stash->{response} = { status => 'ERROR', error => $error };
88 $output = $s->serialize( $c->stash->{response} );
89 $c->response->status(400);
90 }
91
92 $c->response->output( $output );
0a663589 93}
94
95sub default : Private {
5ec5e0b1 96 my ($self, $c) = @_;
97
98 # Forward the request to the appropriate action, based on the
99 # message type.
100 my $action = $c->stash->{request}->{type};
101 if (defined $action) {
102 $c->forward($action, [$c->stash->{request}]);
103 }
104 else {
105 $c->error('no message type specified');
106 }
0a663589 107}
108
109__PACKAGE__->meta->make_immutable;
110