Ichiro,

There is no need on the client-side to include a special JSONRpcClient
library to get
JSON-RPC to work.  Actually, the mootools library has all the functionality
you
need available, including JSON encoding and decoding and an easy JSON HTTP
Request API.

Invoking an RPC-JSON request in javascript can be done like this:
{{{
Wiki.jsonrpc( method, parms,  callback-function(result) );

Example:
Wiki.jsonrpc( method, parms,  function(result){ console.log(result);  });
}}}}
There is no need to register or initialize anything; all what you need is
done by Wiki.jsonrpc.


The rpc "method" name points to the java class implementing the server side
of the rpc protocol.
Today, JSPWiki supports following methods:
"search.suggestions", "search.findpages"  =>
 org.apache.wiki.search.SearchManager.java
"progressTracker.getProgress" => org.apache.wiki.ui.progress.ProgressManager

Both "managers" use the JSONRPCManager.registerGlobalObject() to register
the respective rpc method names, an the RCPCallable() methods.
(check out the java source-code to see how this is done)

Let me know if this gets you any further,

dirk



On Sat, Feb 1, 2014 at 1:34 PM, Ichiro Furusato
<ichiro.furus...@gmail.com>wrote:

> Further investigation suggests a reason why the supporting JavaScript
> library
> for the requestJSON(WikiContext) method in the JSONRPCManager is not
> part of the extant code base or supporting libraries.
>
>     "var jsonrpc = new JSONRpcClient(\"" + jsonurl + "\");");
>
> In particular, the JSONRpcClient is not found anywhere. What I have found
> is that this class seems to be part of a jQuery-based library found at:
>
>     https://github.com/Textalk/jquery.jsonrpcclient.js
>
> This suggests that perhaps the person who wrote the JSONRPCManager
> might have envisaged its use with a jQuery-based environment rather than
> mootools. In any case, the global features of JSONRPCManager work
> (obviously) but the local ones are unsupported in the JavaScript.
>
> Ichiro
>
>
>
> On Fri, Jan 31, 2014 at 11:11 AM, Dirk Frederickx <
> dirk.frederi...@gmail.com
> > wrote:
>
> > Hi Ichiro,
> >
> > Here are a few quick pointers,  hope it can help your investigation.
> >
> > The jsonrpc invocation  javascript is located in jspwiki-common.js,
> around
> > line 400+
> > {{{
> > $jsonid : 10000,
> > jsonrpc: function(method, params, fn) {
> > new Ajax( Wiki.JsonUrl, {
> > postBody: Json.toString({"id":Wiki.$jsonid++, "method":method,
> > "params":params}),
> > method: 'post',
> > onComplete: function(result){
> > var r = Json.evaluate(result,true);
> > if(r){
> > if(r.result){ fn(r.result) }
> > else if(r.error){ fn(r.error) }
> > }
> > }
> > }).request();
> > }
> > }}}
> >
> > In this snippet, the *Wiki.JsonUrl* is retrieved from this snippet in
> > /template/default/commonheader.jsp
> > {{{
> > <meta name="wikiJsonUrl" content='<%=
> >  WikiContext.findContext(pageContext).getURL( WikiContext.NONE,
> "JSON-RPC"
> > ) %>' /><%--unusual pagename--%>
> > }}}
> > The JSON-parameters consist of a unique-id for each invocation, a method
> > directing to the server RPC java classes, and the method parameters. On
> > reception of the response, the resulting JSON is decoded, an the "result"
> > part is returned.
> >
> >
> >
> > You can find a few examples how to invoke the jsonrpc AJAX interface.
> > EG. the implementation of the 'quick- navigation' dropdown is based on
> > json-rpc.
> >
> > Around line 900+ in jspwiki-common.js, you can find following snippet:
> > {{{
> > Wiki.jsonrpc('search.findPages', [qv,20], function(result){
> > $('searchSpin').hide();
> > if(!result.list) return;
> > var frag = new Element('ul');
> >
> > result.list.each(function(el){
> > new Element('li').adopt(
> > new Element('a',{'href':Wiki.getUrl(el.map.page) }).setHTML(el.map.page),
> > new Element('span',{'class':'small'}).setHTML(" ("+el.map.score+")")
> > ).inject(frag);
> > });
> > $('searchOutput').empty().adopt(frag);
> > Wiki.locatemenu( $('query'), $('searchboxMenu') );
> > });
> > }}}
> >
> > The corresponding server-side code is located in SearchManager.java.
> >
> > EXAMPLE of the RPC request message, looking for the string "test":
>  (check
> > out your browser's NETWORK console)
> > {{{
> >
> >    1. {"id":10000,"method":"search.findPages","params":["test",20]}:
> >
> > }}}
> >
> > EXAMPLE of the RPC response message:
> > {{{
> >
> >
> {"id":10000,"result":{"javaClass":"java.util.ArrayList","list":[{"map":{"page":"InsertPageTest","score":110},"javaClass":"java.util.HashMap"},{"map":{"page":"InsertPageTestSections","score":96},"javaClass":"java.util.HashMap"},{"map":{"page":"InsertPageTest_WithUnderscore","score":82},"javaClass":"java.util.HashMap"},{"map":{"page":"WikiEventListener","score":10},"javaClass":"java.util.HashMap"},{"map":{"page":"CSSInWikipages","score":10},"javaClass":"java.util.HashMap"},{"map":{"page":"SearchResultIteratorTag","score":10},"javaClass":"java.util.HashMap"},{"map":{"page":"IfPlugin","score":10},"javaClass":"java.util.HashMap"},{"map":{"page":"SearchPageHelp","score":8},"javaClass":"java.util.HashMap"},{"map":{"page":"WikiGardener","score":8},"javaClass":"java.util.HashMap"},{"map":{"page":"Wiki.Admin.Security","score":3},"javaClass":"java.util.HashMap"},{"map":{"page":"PhotoCollectionPlugin","score":3},"javaClass":"java.util.HashMap"},{"map":{"page":"Configuration","score":3},"javaClass":"java.util.HashMap"}]}}
> >
> > }}}
> >
> > Another example is the implementation of the progress bar for the upload
> of
> > attachements.
> > - javascript/client part : in JSPWiki-common.js around line  380+
> > - java/server part : ProgressManager.java
> >
> >
> > I assume by looking at both parts of the interface, you should be able to
> > understand the puzzle.
> >
> >
> >
> > good luck
> > dirk
> >
> >
> >
> >
> > On Thu, Jan 30, 2014 at 9:56 PM, Ichiro Furusato
> > <ichiro.furus...@gmail.com>wrote:
> >
> > > Hi,
> > >
> > > I'm digging into an AJAX-related project that in theory should be using
> > > the org.apache.wiki.rpc.json.JSONRPCManager to register the callback
> > > used in JavaScript (i.e., via requestJSON(WikiContext)).
> > >
> > > This obviously is working as the search popup uses it, but the existing
> > > code seems broken. The JSONRPCManager spits out the following bit
> > > of JavaScript:
> > >
> > >   "jsonrpc = new JSONRpcClient(\"" + jsonurl + "\");");
> > >
> > > In my tests using a subclass of the RPCSamplePlugin, the above code
> > > flags two errors in Firebug, namely that the 'jsonrpc' variable hasn't
> > > been declared, and the JSONRpcClient class doesn't seem to exist
> > > anywhere in the JSPWiki code (nor can I find it in a JS library).
> > >
> > > Could someone point me to any documentation or examples of how to
> > > use the RPCSamplePlugin, as this doesn't seem to exist on even the
> > > old ecyrd site. Is this broken legacy code or am I just being daft and
> > > can't find something obvious?
> > >
> > > If I can manage to get an example of a JSON-RPC WikiPlugin going
> > > we'll share that code as part of our plugin package, which would help
> > > other developers do JSPWiki+AJAX work.
> > >
> > > Thanks for any assistance,
> > >
> > > Ichiro
> > >
> >
>

Reply via email to