| 1 | # | ||
| 2 | # This file is part of CatalystX-ExtJS-Direct | ||
| 3 | # | ||
| 4 | # This software is Copyright (c) 2014 by Moritz Onken. | ||
| 5 | # | ||
| 6 | # This is free software, licensed under: | ||
| 7 | # | ||
| 8 | # The (three-clause) BSD License | ||
| 9 | # | ||
| 10 | package CatalystX::Controller::ExtJS::Direct::API; | ||
| 11 | $CatalystX::Controller::ExtJS::Direct::API::VERSION = '2.1.5'; | ||
| 12 | # ABSTRACT: API and router controller for Ext.Direct | ||
| 13 | use Moose; | ||
| 14 | extends qw(Catalyst::Controller::REST); | ||
| 15 | use MooseX::MethodAttributes; | ||
| 16 | |||
| 17 | use List::Util qw(first); | ||
| 18 | use List::MoreUtils (); | ||
| 19 | use Scalar::Util qw(blessed); | ||
| 20 | use JSON (); | ||
| 21 | use CatalystX::Controller::ExtJS::Direct::Route; | ||
| 22 | |||
| 23 | __PACKAGE__->config( | ||
| 24 | |||
| 25 | action => { | ||
| 26 | end => { ActionClass => '+CatalystX::Action::ExtJS::Serialize' }, | ||
| 27 | begin => { ActionClass => '+CatalystX::Action::ExtJS::Deserialize' }, | ||
| 28 | index => { Path => undef }, | ||
| 29 | router => { Path => 'router' }, | ||
| 30 | src => { Local => undef }, | ||
| 31 | }, | ||
| 32 | |||
| 33 | default => 'application/json' | ||
| 34 | |||
| 35 | ); | ||
| 36 | |||
| 37 | |||
| 38 | has 'api' => ( is => 'rw', lazy_build => 1 ); | ||
| 39 | |||
| 40 | has 'routes' => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); | ||
| 41 | |||
| 42 | has 'namespace' => ( is => 'rw' ); | ||
| 43 | |||
| 44 | sub index { } | ||
| 45 | |||
| 46 | sub src { | ||
| 47 | my ($self, $c) = @_; | ||
| 48 | $c->res->content_type('application/javascript'); | ||
| 49 | $c->res->body( 'Ext.app.REMOTING_API = ' . $self->encoded_api($c) . ';' ); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _build_api { | ||
| 53 | my ($self) = @_; | ||
| 54 | my $c = $self->_app; | ||
| 55 | my $data = {}; | ||
| 56 | foreach my $name ( $c->controllers ) { | ||
| 57 | my $controller = $c->controller($name); | ||
| 58 | $name =~ s/^API:://; | ||
| 59 | $name =~ s/:://g; | ||
| 60 | my $meta = $controller->meta; | ||
| 61 | next | ||
| 62 | unless ( $controller->can('is_direct') || $meta->does_role('CatalystX::Controller::ExtJS::Direct') ); | ||
| 63 | my @methods; | ||
| 64 | foreach my $method ( sort { $a->name cmp $b->name } $controller->get_action_methods ) { | ||
| 65 | next | ||
| 66 | unless ( my $action = $controller->action_for( $method->name ) ); | ||
| 67 | next unless ( exists $action->attributes->{Direct} ); | ||
| 68 | my @routes = | ||
| 69 | CatalystX::Controller::ExtJS::Direct::Route::Factory->build( | ||
| 70 | $c->dispatcher, $action ); | ||
| 71 | foreach my $route (@routes) { | ||
| 72 | $self->routes->{$name}->{ $route->name } = $route; | ||
| 73 | push( @methods, $route->build_api ); | ||
| 74 | } | ||
| 75 | |||
| 76 | } | ||
| 77 | $data->{$name} = [@methods]; | ||
| 78 | } | ||
| 79 | return { | ||
| 80 | url => $c->dispatcher->uri_for_action( $self->action_for('router') ) | ||
| 81 | ->as_string, | ||
| 82 | type => 'remoting', | ||
| 83 | actions => $data | ||
| 84 | }; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub encoded_api { | ||
| 88 | my ( $self, $c ) = @_; | ||
| 89 | return JSON::encode_json( $self->set_namespace( $self->api, $c ? $c->req->params->{namespace} : () ) ); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub router { | ||
| 93 | my ( $self, $c ) = @_; | ||
| 94 | my $reqs = ref $c->req->data eq 'ARRAY' ? $c->req->data : [ $c->req->data ]; | ||
| 95 | my $api = $self->api; # populates $self->routes | ||
| 96 | my $routes = $self->routes; | ||
| 97 | if ( keys %{ $c->req->body_params } | ||
| 98 | && ( my $params = $c->req->body_params ) ) | ||
| 99 | { | ||
| 100 | $reqs = [ | ||
| 101 | { | ||
| 102 | map { | ||
| 103 | my $orig = $_; | ||
| 104 | $orig =~ s/^ext//; | ||
| 105 | ( lc($orig) => delete $params->{$_} ) | ||
| 106 | } qw(extType extAction extMethod extTID extUpload) | ||
| 107 | } | ||
| 108 | ]; | ||
| 109 | if ( $params->{extData} ) { | ||
| 110 | $reqs->[0]->{data} = JSON::decode_json( delete $params->{extData} ); | ||
| 111 | } else { | ||
| 112 | $reqs->[0]->{data} = [{%$params}]; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | my @requests; | ||
| 117 | |||
| 118 | foreach my $req (@$reqs) { | ||
| 119 | unless ( $req && $req->{action} | ||
| 120 | && exists $routes->{ $req->{action} } | ||
| 121 | && exists $routes->{ $req->{action} }->{ $req->{method} } ) | ||
| 122 | { | ||
| 123 | $self->status_bad_request( $c, { message => sprintf('method %s in action %s does not exist', $req->{method} || '', $req->{action} || '') } ); | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | my $route = $routes->{ $req->{action} }->{ $req->{method} }; | ||
| 127 | |||
| 128 | push(@requests, $route->prepare_request($req)); | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | my @res; | ||
| 133 | REQUESTS: | ||
| 134 | foreach my $req (@requests) { | ||
| 135 | $req->{data} = [$req->{data}] if(ref $req->{data} ne "ARRAY"); | ||
| 136 | $c->stash->{upload} = 1 if ( $req->{upload} ); | ||
| 137 | |||
| 138 | my $route = $routes->{ $req->{action} }->{ $req->{method} }; | ||
| 139 | my $params = @{$req->{data}} && ref $req->{data}->[-1] eq 'HASH' ? $req->{data}->[-1] : undef; | ||
| 140 | my $body; | ||
| 141 | { | ||
| 142 | local $c->{response} = $c->response_class->new({}); | ||
| 143 | local $c->{stash} = {}; | ||
| 144 | local $c->{request} = $c->req; | ||
| 145 | local $c->{error} = undef; | ||
| 146 | |||
| 147 | $c->req->parameters($params); | ||
| 148 | $c->req->body_parameters($params); | ||
| 149 | my %req = $route->request($req); | ||
| 150 | $c->req($c->request_class->new(%{$c->req}, %req)); | ||
| 151 | eval { | ||
| 152 | $c->visit($route->build_url( $req->{data} )); | ||
| 153 | my $response = $c->res; | ||
| 154 | if ( $response->content_type eq 'application/json' ) { | ||
| 155 | (my $res_body = $response->body || '') =~ s/^\xEF\xBB\xBF//; # remove BOM | ||
| 156 | my $json = JSON::decode_json( $res_body ); | ||
| 157 | $body = $json; | ||
| 158 | } else { | ||
| 159 | $body = $response->body; | ||
| 160 | } | ||
| 161 | |||
| 162 | if(@{$c->error}) { 0 } | ||
| 163 | elsif($response->status >= 400) { | ||
| 164 | $c->error($body); | ||
| 165 | 0; | ||
| 166 | } else { 1 } | ||
| 167 | } or do { | ||
| 168 | my $msg; | ||
| 169 | if(@{ $c->error } && List::MoreUtils::all { ref $_ } @{ $c->error }) { | ||
| 170 | $msg = @{$c->error} == 1 ? $c->error->[0] : $c->error; | ||
| 171 | $msg = "$msg" if(blessed $msg); | ||
| 172 | } elsif(scalar @{ $c->error }) { | ||
| 173 | $msg = join "\n", map { blessed $_ ? "$_" : $_ } @{ $c->error }; | ||
| 174 | } else { | ||
| 175 | $msg = join("\n", "$@", $c->response->body || ()); | ||
| 176 | } | ||
| 177 | push(@res, { type => 'exception', tid => $req->{tid}, message => $msg, status_code => $c->res->status }); | ||
| 178 | $c->log->error($msg) if($c->debug && !ref $msg); | ||
| 179 | next REQUESTS; | ||
| 180 | }; | ||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | my $res = { map { $_ => $req->{$_} } qw(action method tid type) }; | ||
| 187 | push( @res, { %$res, result => $body } ); | ||
| 188 | |||
| 189 | } | ||
| 190 | $c->stash->{rest} = @res != 1 ? \@res : $res[0]; | ||
| 191 | |||
| 192 | } | ||
| 193 | |||
| 194 | sub set_namespace { | ||
| 195 | my ($self, $api, $namespace) = @_; | ||
| 196 | return $api unless($namespace && $namespace =~ /^\w+(\.\w+)?$/); | ||
| 197 | return {%$api, namespace => $namespace }; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub end { | ||
| 201 | my ( $self, $c ) = @_; | ||
| 202 | $c->stash->{rest} ||= $self->set_namespace( $self->api, $c->req->params->{namespace} ); | ||
| 203 | } | ||
| 204 | |||
| 205 | 1; | ||
| 206 | |||
| 207 | __END__ |