| 1 | package MojoX::Dispatcher::Qooxdoo::Jsonrpc; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Mojo::JSON qw(encode_json decode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Mojo::Base 'Mojolicious::Controller'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our $toUTF8 = find_encoding('utf8'); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | warn "MojoX::Dispatcher::Qooxdoo::Jsonrpc is DEPRECATED. Please switch to using Mojolicious::Plugin::Qooxdoo.\n" unless $ENV{DISABLE_DEPRECATION_WARNING_MPQ}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '0.96'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub dispatch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dispatch: not called CC=27 SCAR=65.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # We have to differentiate between POST and GET requests, because | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # the data is not sent in the same place.. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | my $log = $self->app->log; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # send warnings to log file preserving the origin | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | local $SIG{__WARN__} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $message = shift; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | $message =~ s/\n$//; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | @_ = ($log, $message); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | goto &Mojo::Log::warn; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $id; | |||||||||||||||||||||||||||||||||||||||||
| 35 | my $data; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my $cross_domain; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | for ( $self->req->method ){ | |||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | /^POST$/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | # Data comes as JSON object, so fetch a reference to it | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $data = eval { decode_json($self->req->body) }; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $error = "Invalid json string: " . $@; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $id = $data->{id}; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $cross_domain = 0; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | /^GET$/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $v = $self->param('_ScriptTransport_data'); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $data = eval { decode_json($self->param('_ScriptTransport_data')) }; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($@){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $error = "Invalid json string: " . $@ . " " .Dumper $self->param; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $id = $self->param('_ScriptTransport_id') ; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $cross_domain = 1; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $error = "request must be POST or GET. Can't handle '".$self->req->method."'"; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if (not defined $id){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $error = "Missing 'id' property in JsonRPC request."; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # Check if desired service is available | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $service = $data->{service} or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $error = "Missing service property in JsonRPC request."; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # Check if method is not private (marked with a leading underscore) | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $method = $data->{method} or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $error = "Missing method property in JsonRPC request."; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $log->error($error); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $self->render(text => $error, status=>500); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $params = $data->{params} || []; # is a reference, so "unpack" it | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{'params'} || []
MC/DC0%0/1
$data->{'params'} || []
$data->{'params'}
Truth table0%0/2
A
A= $data->{'params'}
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # invocation of method in class according to request | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my $reply = eval{ | |||||||||||||||||||||||||||||||||||||||||
| 98 | # make sure there are not foreign signal handlers | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # messing with our problems | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||||||||||||||||
| 101 | # Getting available services from stash | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $svc = $self->stash('services')->{$service}; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | die { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | origin => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | message => "service $service not available", | ||||||||||||||||||||||||||||||||||||||||||
| 107 | code=> 2 | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } if not ref $svc; | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | die { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | origin => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 112 | message => "your rpc service object (".ref($svc).") must provide an allow_rpc_access method", | ||||||||||||||||||||||||||||||||||||||||||
| 113 | code=> 2 | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } unless $svc->can('allow_rpc_access'); | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($svc->can('controller')){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | # initialize session if it does not exists yet | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $svc->controller($self); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($svc->can('mojo_session')){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | # initialize session if it does not exists yet | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $log->warn('mojo_session is deprecated. Use controller->session instead'); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $session = $self->stash->{'mojo.session'} ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->stash->{"mojo.session"} ||= {}
MC/DC0%0/1
$self->stash->{"mojo.session"} ||= {}
$self->stash->{"mojo.session"}
Truth table0%0/2
A
A= $self->stash->{"mojo.session"}
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $svc->mojo_session($session); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($svc->can('mojo_stash')){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $log->warn('mojo_stash is deprecated. Use controller->stash instead'); | |||||||||||||||||||||||||||||||||||||||||
| 131 | # initialize session if it does not exists yet | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $svc->mojo_stash($self->stash); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | die { | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | origin => 1, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | message => "rpc access to method $method denied", | ||||||||||||||||||||||||||||||||||||||||||
| 138 | code=> 6 | ||||||||||||||||||||||||||||||||||||||||||
| 139 | } unless $svc->allow_rpc_access($method); | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | die { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | origin => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 143 | message => "method $method does not exist.", | ||||||||||||||||||||||||||||||||||||||||||
| 144 | code=> 4 | ||||||||||||||||||||||||||||||||||||||||||
| 145 | } if not $svc->can($method); | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | $log->debug("call $method(".encode_json($params).")"); | |||||||||||||||||||||||||||||||||||||||||
| 148 | # reply | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | $svc->$method(@$params); | |||||||||||||||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if ($@){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my $error; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | for (ref $@){ | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | /HASH/ && $@->{message} && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/HASH/u and $@->{"message"}
MC/DC0%0/2
/HASH/u and $@->{"message"}
/HASH/u
$@->{"message"}
Truth table0%0/3
A and B
A= /HASH/uB= $@->{"message"}
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | $error = { | ||||||||||||||||||||||||||||||||||||||||||
| 158 | origin => $@->{origin} || 2, | ||||||||||||||||||||||||||||||||||||||||||
| 159 | message => $@->{message}, | ||||||||||||||||||||||||||||||||||||||||||
| 160 | code=>$@->{code} | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@->{'origin'} || 2
MC/DC0%0/1
$@->{'origin'} || 2
$@->{'origin'}
Truth table0%0/2
A
A= $@->{'origin'}
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | /.+/ && $@->can('message') && $@->can('code') && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
/.+/u and $@->can("message") and $@->can("code")
/.+/u and $@->can("message")
MC/DC0%0/3
/.+/u and $@->can("message") and $@->can("code")
/.+/u
$@->can("message")
$@->can("code")
Truth table0%0/4
A and B and C
A= /.+/uB= $@->can("message")C= $@->can("code")
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | $error = { | |||||||||||||||||||||||||||||||||||||||||
| 166 | origin => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | message => $@->message(), | ||||||||||||||||||||||||||||||||||||||||||
| 168 | code=>$@->code() | ||||||||||||||||||||||||||||||||||||||||||
| 169 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | $error = { | |||||||||||||||||||||||||||||||||||||||||
| 173 | origin => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 174 | message => "error while processing ${service}::$method: $@", | ||||||||||||||||||||||||||||||||||||||||||
| 175 | code=> 9999 | ||||||||||||||||||||||||||||||||||||||||||
| 176 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $reply = encode_json({ id => $id, error => $error }); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | $log->error("JsonRPC Error $error->{code}: $error->{message}"); | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | $reply = encode_json({ id => $id, result => $reply }); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | $log->debug("return ".$reply); | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if ($cross_domain){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | # for GET requests, qooxdoo expects us to send a javascript method | ||||||||||||||||||||||||||||||||||||||||||
| 188 | # and to wrap our json a litte bit more | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | $self->res->headers->content_type('application/javascript; charset=utf-8'); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | $reply = "qx.io.remote.transport.Script._requestFinished( $id, " . $reply . ");"; | |||||||||||||||||||||||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | $self->res->headers->content_type('application/json; charset=utf-8'); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # the render takes care of encoding the output, so make sure we re-decode | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # the json stuf | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | $self->render(text => $toUTF8->decode($reply)); | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | MojoX::Dispatcher::Qooxdoo::Jsonrpc - Dispatcher for Qooxdoo Json Rpc Calls | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | THIS MODULE IS DEPRECATED. USE L<Mojolicious::Plugin::Qooxdoo> INSTEAD. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | # lib/your-application.pm | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | use base 'Mojolicious'; | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | use RpcService; | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | sub startup { | ||||||||||||||||||||||||||||||||||||||||||
| 218 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | # instantiate all services | ||||||||||||||||||||||||||||||||||||||||||
| 221 | my $services= { | ||||||||||||||||||||||||||||||||||||||||||
| 222 | Test => RpcService->new(), | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | # add a route to the Qooxdoo dispatcher and route to it | ||||||||||||||||||||||||||||||||||||||||||
| 228 | my $r = $self->routes; | ||||||||||||||||||||||||||||||||||||||||||
| 229 | $r->route('/qooxdoo') -> to( | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 'Jsonrpc#dispatch', | ||||||||||||||||||||||||||||||||||||||||||
| 231 | services => $services, | ||||||||||||||||||||||||||||||||||||||||||
| 232 | debug => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 233 | namespace => 'MojoX::Dispatcher::Qooxdoo' | ||||||||||||||||||||||||||||||||||||||||||
| 234 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | L<MojoX::Dispatcher::Qooxdoo::Jsonrpc> dispatches incoming | ||||||||||||||||||||||||||||||||||||||||||
| 243 | rpc requests from a qooxdoo application to your services and renders | ||||||||||||||||||||||||||||||||||||||||||
| 244 | a (hopefully) valid json reply. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | =head1 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | This example exposes a service named "Test" in a folder "RpcService". | ||||||||||||||||||||||||||||||||||||||||||
| 250 | The Mojo application is named "QooxdooServer". The scripts are in | ||||||||||||||||||||||||||||||||||||||||||
| 251 | the 'example' directory. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | First create this application using | ||||||||||||||||||||||||||||||||||||||||||
| 253 | "mojolicious generate app QooxdooServer". | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | Then, lets write the service: | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | Change to the root directory "qooxdoo_server" of your fresh | ||||||||||||||||||||||||||||||||||||||||||
| 258 | Mojo-Application and make a dir named 'qooxdoo-services' | ||||||||||||||||||||||||||||||||||||||||||
| 259 | for the services you want to expose. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | Our "Test"-service could look like: | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | package RpcService; | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | # if you want to access mojo specific information | ||||||||||||||||||||||||||||||||||||||||||
| 268 | # provide a controller property, it will be set to the | ||||||||||||||||||||||||||||||||||||||||||
| 269 | # current controller as the request is dispached. | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # see L<Mojolicious::Controller> for documentation. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | has 'controller'; | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | # MANDADROY access check method. The method is called right before the actual | ||||||||||||||||||||||||||||||||||||||||||
| 274 | # method call, after assigning mojo_session and mojo_stash properties are set. | ||||||||||||||||||||||||||||||||||||||||||
| 275 | # These can be used for providing dynamic access control | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | our %access = ( | ||||||||||||||||||||||||||||||||||||||||||
| 278 | add => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 279 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | sub allow_rpc_access { | ||||||||||||||||||||||||||||||||||||||||||
| 282 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 283 | my $method = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 284 | # check if we can access | ||||||||||||||||||||||||||||||||||||||||||
| 285 | return $access{$method}; | ||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | sub add{ | ||||||||||||||||||||||||||||||||||||||||||
| 289 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 290 | my @params = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | # Debug message on Mojo-server console (or log) | ||||||||||||||||||||||||||||||||||||||||||
| 293 | print "Debug: $params[0] + $params[1]\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | # uncomment if you want to die without further handling | ||||||||||||||||||||||||||||||||||||||||||
| 296 | # die; | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | # uncomment if you want to die with a message in a hash | ||||||||||||||||||||||||||||||||||||||||||
| 299 | # die {code => 20, message => "Test died on purpose :-)"}; | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | # uncomment if you want to die with your homemade error object | ||||||||||||||||||||||||||||||||||||||||||
| 303 | # die MyException->new(code=>123,message=>'stupid error message'); | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | my $result = $params[0] + $params[1] | ||||||||||||||||||||||||||||||||||||||||||
| 306 | return $result; | ||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | package MyException; | ||||||||||||||||||||||||||||||||||||||||||
| 310 | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||||
| 311 | has 'code'; | ||||||||||||||||||||||||||||||||||||||||||
| 312 | has 'message'; | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | The Dispatcher executes all calls to your service module within an eval | ||||||||||||||||||||||||||||||||||||||||||
| 316 | wrapper and will send any execptions you generate within back to the | ||||||||||||||||||||||||||||||||||||||||||
| 317 | qooxdoo application as well as into the Mojolicious logfile. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | Now, lets write our application. Normally one would use the services of | ||||||||||||||||||||||||||||||||||||||||||
| 320 | L<Mojolicious::Plugin::QooxdooJsonrpc> for this. If you want to use the | ||||||||||||||||||||||||||||||||||||||||||
| 321 | dipatcher directly, this is how it is done. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | package QooxdooServer; | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 326 | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | use RpcService::Test; | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | use Mojo::Base 'Mojolicious'; | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | # This method will run once at server start | ||||||||||||||||||||||||||||||||||||||||||
| 333 | sub startup { | ||||||||||||||||||||||||||||||||||||||||||
| 334 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | my $services= { | ||||||||||||||||||||||||||||||||||||||||||
| 337 | Test => RpcService::Test->new(), | ||||||||||||||||||||||||||||||||||||||||||
| 338 | # more services here | ||||||||||||||||||||||||||||||||||||||||||
| 339 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | # tell Mojo about your services: | ||||||||||||||||||||||||||||||||||||||||||
| 342 | my $r = $self->routes; | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | # this sends all requests for "/qooxdoo" in your Mojo server | ||||||||||||||||||||||||||||||||||||||||||
| 345 | # to our little dispatcher. | ||||||||||||||||||||||||||||||||||||||||||
| 346 | # change this at your own taste. | ||||||||||||||||||||||||||||||||||||||||||
| 347 | $r->route('/qooxdoo')->to(' | ||||||||||||||||||||||||||||||||||||||||||
| 348 | jsonrpc#dispatch', | ||||||||||||||||||||||||||||||||||||||||||
| 349 | services => $services, | ||||||||||||||||||||||||||||||||||||||||||
| 350 | namespace => 'MojoX::Dispatcher::Qooxdoo' | ||||||||||||||||||||||||||||||||||||||||||
| 351 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | Now start your Mojo Server by issuing C<script/QooxdooServer daemon>. | ||||||||||||||||||||||||||||||||||||||||||
| 358 | If you want to change any options, type C<script/QooxdooServer help>. | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | =head2 Security | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | MojoX::Dispatcher::Qooxdoo::Jsonrpc calls the C<allow_rpc_access> | ||||||||||||||||||||||||||||||||||||||||||
| 363 | method to check if rpc access should be allowed. The result of this | ||||||||||||||||||||||||||||||||||||||||||
| 364 | request is NOT cached, so you can use this method to provide dynamic access control | ||||||||||||||||||||||||||||||||||||||||||
| 365 | or even do initialization tasks that are required before handling each request. | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | S<Matthias Bloch, E<lt>matthias@puffin.chE<gt>>, | ||||||||||||||||||||||||||||||||||||||||||
| 370 | S<Tobias Oetiker, E<lt>tobi@oetiker.chE<gt>>. | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | This Module is sponsored by OETIKER+PARTNER AG | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | Copyright (C) 2010 by :m) | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 381 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||||||||||||||||||||||||
| 382 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | =cut | ||||||||||||||||||||||||||||||||||||||||||