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