Skip to content

Commit

Permalink
Item14033: Convert JsonRpcContrib to Request::JSON
Browse files Browse the repository at this point in the history
This is incomplete.  It appears functional but there are several issues:

Request::param() and Request::JSON::param() collide.  They can coexist
as long as the JSON version is read-only on the JSON parm data. so that
write requests can be passed to the CGI implementation.  If it needs to
be read-write, this will probably have to be split into a separate
jsonparam() routine.

Errors encountered in Request::JSON are not passed through to the UI.
this needs some additional work.
  • Loading branch information
gac410 committed May 8, 2016
1 parent ec89a13 commit 65fcd8a
Show file tree
Hide file tree
Showing 6 changed files with 470 additions and 25 deletions.
1 change: 1 addition & 0 deletions JsonRpcContrib/lib/Foswiki/Contrib/JsonRpcContrib/MANIFEST
Expand Up @@ -8,6 +8,7 @@ lib/Foswiki/Contrib/JsonRpcContrib.pm 0644
lib/Foswiki/Contrib/JsonRpcContrib/Request.pm 0644
lib/Foswiki/Contrib/JsonRpcContrib/Response.pm 0644
lib/Foswiki/Contrib/JsonRpcContrib/Server.pm 0644
lib/Foswiki/Request/JSON.pm 0644
pub/System/JsonRpcContrib/jquery.jsonrpc.js 0644
pub/System/JsonRpcContrib/jquery.jsonrpc.js.gz 0644
pub/System/JsonRpcContrib/jquery.jsonrpc.uncompressed.js 0644
Expand Down
12 changes: 11 additions & 1 deletion JsonRpcContrib/lib/Foswiki/Contrib/JsonRpcContrib/Request.pm
Expand Up @@ -149,24 +149,34 @@ sub id {

##############################################################################
sub param {
my ( $this, $key, $value ) = @_;
my ( $this, @p ) = @_;
my ( $key, $value ) = @p;

print STDERR "Jsonrpc::Request::param() entered\n";
print STDERR Data::Dumper::Dumper( \@p );

return unless $key;

print STDERR Data::Dumper::Dumper( \$this->{data}{params}{$key} );

$this->{data}{params}{$key} = $value if defined $value;
return $this->{data}{params}{$key};
}

##############################################################################
sub params {
print STDERR "Jsonrpc::Request::params() entered\n";
print STDERR Data::Dumper::Dumper( \$_[0]->{data}{params} );
return $_[0]->{data}{params};
}

##############################################################################
sub method {
my ( $this, $value ) = @_;

print STDERR "Jsonrpc::Request::method() entered\n";
$this->{data}{method} = $value if defined $value;
print STDERR Data::Dumper::Dumper( \$_[0]->{data}{method} );
return $this->{data}{method};
}

Expand Down
43 changes: 20 additions & 23 deletions JsonRpcContrib/lib/Foswiki/Contrib/JsonRpcContrib/Server.pm
Expand Up @@ -25,7 +25,7 @@ use Foswiki::Contrib::JsonRpcContrib::Error ();
use Foswiki::Contrib::JsonRpcContrib::Request ();
use Foswiki::Contrib::JsonRpcContrib::Response ();

use constant TRACE => 0; # toggle me
use constant TRACE => 1; # toggle me

# Error codes for json-rpc response
# -32700: Parse error - Invalid JSON was received by the server.
Expand All @@ -42,6 +42,7 @@ use constant TRACE => 0; # toggle me
################################################################################
# static
sub writeDebug {
print STDERR $_[0] . "\n";
Foswiki::Func::writeDebug '- JsonRpcContrib::Server - ' . $_[0];
}

Expand Down Expand Up @@ -76,38 +77,33 @@ sub dispatch {
$Foswiki::Plugins::SESSION = $session;
$this->{session} = $session;

my $request;
try {
$request = new Foswiki::Contrib::JsonRpcContrib::Request($session);
my $request = Foswiki::Func::getRequestObject();

unless ( $request->isa('Foswiki::Request::JSON') ) {
print STDERR "NOT Foswiki::Request::JSON \n";
try {
$request = new Foswiki::Contrib::JsonRpcContrib::Request($session);
}
catch Foswiki::Contrib::JsonRpcContrib::Error with {
my $error = shift;
Foswiki::Contrib::JsonRpcContrib::Response->print(
$session,
code => $error->{code},
message => $error->{message}
);
};
}
catch Foswiki::Contrib::JsonRpcContrib::Error with {
my $error = shift;
Foswiki::Contrib::JsonRpcContrib::Response->print(
$session,
code => $error->{code},
message => $error->{message}
);
};
return unless defined $request;

# get topic parameter and set the location overriding any
# other value derived from the namespace param
my $topic = $request->param('topic')
|| $Foswiki::cfg{HomeTopicName};
( $session->{webName}, $session->{topicName} ) =
Foswiki::Func::normalizeWebTopicName( $Foswiki::cfg{UsersWebName},
$topic );
writeDebug("topic=$topic") if TRACE;

# get handler for this namespace
my $handler = $this->getHandler($request);
unless ( defined $handler ) {
Foswiki::Contrib::JsonRpcContrib::Response->print(
$session,
code => -32601,
message => "Invalid invocation - unknown handler for "
. $request->namespace() . "."
. $request->method(),
. ( $request->namespace() || '' ) . "."
. ( $request->method() || '' ),
id => $request->id()
);
return;
Expand Down Expand Up @@ -213,6 +209,7 @@ sub dispatch {
sub getHandler {
my ( $this, $request ) = @_;

print STDERR "getHandler called\n";
my $namespace = $request->namespace();
return unless $namespace;

Expand Down

0 comments on commit 65fcd8a

Please sign in to comment.