#!perl
package Apache2::JSONRPC;
use Apache2::Const qw(
TAKE1 OR_ALL OK HTTP_BAD_REQUEST SERVER_ERROR M_GET M_POST
);
use Apache2::RequestRec ();
use Apache2::CmdParms ();
use Apache2::RequestIO
CGI::JSONRPC::Base;
use base qw(CGI::JSONRPC::Base Apache2::Module);
our $VERSION = "0.02";
__PACKAGE__->add([ CookOptions(
[
'JSONRPC_Class',
'Perl class to dispatch JSONRPC c
_POST) {
my $json = $self->apache2_read_post($r) or return HTTP_BAD_REQUEST;
$r->content_type("text/json");
$r->print($self->run_json_request($json));
return O
package CGI::JSONRPC::Session::Obj;
use strict;
our @IGNORE_KEYS = qw( _session _cgi _request );
1;
sub jsonrpc_new {
my($class, $dispatcher, $id, $session,%options) = @_;
warn __PACKAGE__
ill not be possible.";
my $session_key = "jsonrpc_$id";
if ($session and my $data = $session->param($session_key)) {
my $self = $class->_jsonrpc_restore($data);
$self->{_session} =
->{jsonrpc_ignore_keys} = [@IGNORE_KEYS];
return $self;
#return bless $self, $class;
} else {
return bless { id => $id,_session => $session, _session_key => $session_key, jsonrpc
package Apache2::JSONRPC::Session;
use strict;
use Apache2::JSONRPC;
use CGI::JSONRPC::Dispatcher::Session;
use base qw(Apache2::JSONRPC);
1;
sub new {
my ($class,%args) = @_;
%args = __PACKAG
d1 NAME
Apache2::JSONRPC::Session - Dispatcher class for session aware JSONRPC Objects
=head1 SYNOPSIS
package MyDispatch;
use Apache2::JSONRPC::Session;
use base qw(Apache2::JSONRPC::Session);
#
ession portions of CGI::JSONRPC are unmaintained and may change or
disappear without notice (probably change...)
=head1 DESCRIPTION
Apache2::JSONRPC::Dispatcher receives JSONRPC class method calls a
package CGI::JSONRPC::Obj;
use strict;
1;
sub jsonrpc_new {
my($class, $id, $dispatcher) = @_;
return bless { id => $id, dispatcher => $dispatcher }, $class;
}
sub jsonrpc_js_name {
my
my $name = shift || $self->jsonrpc_js_name();
return '';
}
sub jsonrpc_javascript {
my $self = shift;
return $self->js_class
}
=pod
=head1 NAME
CGI::JSONRPC::Obj - Base class for easy
e MyHandler;
use CGI::JSONRPC::Obj;
sub jsonrpc_javascript {
my $js;
# construct javascript object
return
}
sub do_something {
# handler that jsonrpc will call...
}
#!perl
package CGI::JSONRPC::Dispatcher;
use strict;
use warnings;
our $AUTOLOAD;
use Attribute::Handlers;
use attributes;
our %Protected;
return 1;
sub UNIVERSAL::DontDispatch :ATTR(CODE) {
my
($package, $symbol) = @_;
$CGI::JSONRPC::Dispatcher::Protected{$package}{*{$symbol}{NAME}}++;
return 1;
}
sub DISPATCH_OBJECT {
my($class, $to) = @_;
}
sub AUTOLOAD {
my($class, $id, $to) =
$to\::$method_name may not be dispatched\n" if $Protected{$to}{$method_name};
my $object = $to->jsonrpc_new($id, $class);
if(my $method = $object->can($method_name)) {
return $method->($object
package CGI::JSONRPC::Session;
use CGI::JSONRPC;
use CGI::JSONRPC::Dispatcher::Session;
use base qw(CGI::JSONRPC);
1;
sub new {
my($class,%args) = @_;
%args = __PACKAGE__->init_session(%ar
eaders_json {
my $self = shift;
$self->{session}->header('Content-Type' => "text/json");
}
sub default_dispatcher {
'CGI::JSONRPC::Dispatcher::Session'
}
=pod
=head1 NAME
CGI::JSONRPC::S
ession - Persistant CGI handler for JSONRPC
=head1 SYNOPSIS
use CGI;
use MyDispatcher
my $cgi = new CGI;
MyDispatcher->handler($cgi);
exit;
package MyDispatcher;
sub init_session
#!perl
package CGI::JSONRPC::Dispatcher::Session;
use strict;
use warnings;
our $AUTOLOAD;
return 1;
sub AUTOLOAD {
my($jsonrpco, $id, $to) = splice(@_, 0, 3);
(my $method = $AUTOLOAD) =~ s
my $object = $to->jsonrpc_new($id,$jsonrpco->{session});
return unless $object;
my $result = $object->$method(@_);
$object->_jsonrpc_finish() if $object->can('_jsonrpc_finish');
ret
=head1 NAME
CGI::JSONRPC::Session::Dispatcher - Dispatch JSONRPC requests to persistant objects
=head1 SYNOPSIS
package Hello;
use CGI::JSONRPC::Session::Obj;
use base qw(CGI::JSONRPC::Session::Obj
#!perl
package CGI::JSONRPC::Base;
use strict;
use warnings;
use JSON::Syck;
use JSON::Syck qw(Dump Load);
(our $JAVASCRIPT = __FILE__) =~ s{\.pm$}{.js};
1;
sub new {
my($class, %args) = @_;
, $class;
}
sub default_dispatcher {
'CGI::JSONRPC::Dispatcher'
}
sub run_json_request {
my($self, $json) = @_;
my $data = (JSON::Syck::Load($json))[0];
die "Did not get a hash from RP
a) eq 'HASH');
unless($data->{method}) {
warn "JSONRPC payload did not have a method!";
return $self->return_error($data, "JSONRPC payload did not have a method!");
}
return $self
#!perl
package CGI::JSONRPC;
use strict;
use warnings;
use CGI::JSONRPC::Dispatcher;
use CGI::JSONRPC::Base;
use base qw(CGI::JSONRPC::Base);
use CGI;
our $VERSION = "0.11";
return 1;
sub headers
lf = shift;
$self->{cgi}->header("text/javascript");
}
sub headers_json {
my $self = shift;
$self->{cgi}->header("text/json");
}
sub handler {
my($class, $cgi,@args) = @_;
$cgi ||
f($method eq 'POST') {
my $json = $cgi->param('POSTDATA') or die "No POST data was sent!";
print $self->headers_json(), $self->run_json_request($json);
return 1;
} else {