You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by gk...@apache.org on 2015/02/27 14:22:24 UTC
svn commit: r1662679 - in /turbine/core/trunk: ./
src/java/org/apache/turbine/modules/screens/
src/java/org/apache/turbine/services/jsonrpc/
src/test/org/apache/turbine/services/jsonrpc/ xdocs/services/
Author: gk
Date: Fri Feb 27 13:22:23 2015
New Revision: 1662679
URL: http://svn.apache.org/r1662679
Log:
- Update metaparadigm with jabsorg (available now in maven central) JSON-RPC to allow for async AJAX calls
- Add asyn example in jsonrpc-service.xml
Modified:
turbine/core/trunk/pom.xml
turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java
turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JSONProcessor.java
turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JsonRpcService.java
turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpc.java
turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpcService.java
turbine/core/trunk/src/test/org/apache/turbine/services/jsonrpc/JsonrpcServicelTest.java
turbine/core/trunk/xdocs/services/jsonrpc-service.xml
Modified: turbine/core/trunk/pom.xml
URL: http://svn.apache.org/viewvc/turbine/core/trunk/pom.xml?rev=1662679&r1=1662678&r2=1662679&view=diff
==============================================================================
--- turbine/core/trunk/pom.xml (original)
+++ turbine/core/trunk/pom.xml Fri Feb 27 13:22:23 2015
@@ -982,10 +982,45 @@
<optional>true</optional>
</dependency>
<dependency>
- <groupId>com.metaparadigm</groupId>
- <artifactId>json-rpc</artifactId>
- <version>1.0</version>
+ <groupId>org.jabsorb</groupId>
+ <artifactId>jabsorb</artifactId>
+ <version>1.3.2</version>
<type>jar</type>
+ <scope>compile</scope>
+ <optional>true</optional>
+ <!-- exclude old versions -->
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jdk14</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </exclusion>
+ <!-- renamed -->
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>jcl104-over-slf4j</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.7.10</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency> <!-- delegate slf4j to log4j -->
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.7.10</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency> <!-- redirect JCL to slf4j -->
+ <groupId>org.slf4j</groupId>
+ <artifactId>jcl-over-slf4j</artifactId>
+ <version>1.7.10</version>
<optional>true</optional>
</dependency>
<dependency>
Modified: turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java?rev=1662679&r1=1662678&r2=1662679&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java Fri Feb 27 13:22:23 2015
@@ -31,8 +31,8 @@ import org.apache.turbine.annotation.Tur
import org.apache.turbine.pipeline.PipelineData;
import org.apache.turbine.services.jsonrpc.JsonRpcService;
import org.apache.turbine.util.RunData;
+import org.jabsorb.JSONRPCBridge;
-import com.metaparadigm.jsonrpc.JSONRPCBridge;
/**
* A Screen class for dealing with JSON-RPC requests. Typically you would
Modified: turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JSONProcessor.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JSONProcessor.java?rev=1662679&r1=1662678&r2=1662679&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JSONProcessor.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JSONProcessor.java Fri Feb 27 13:22:23 2015
@@ -7,11 +7,12 @@ import javax.servlet.http.HttpServletReq
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.jabsorb.JSONRPCBridge;
+import org.jabsorb.JSONRPCResult;
import org.json.JSONArray;
+import org.json.JSONException;
import org.json.JSONObject;
-import com.metaparadigm.jsonrpc.JSONRPCBridge;
-import com.metaparadigm.jsonrpc.JSONRPCResult;
public class JSONProcessor
{
@@ -44,7 +45,7 @@ public class JSONProcessor
//json_res = json_bridge.call(new Object[] {request}, object_id, methodName, arguments);
json_res = json_bridge.call(new Object[] {request}, json_req);
}
- catch (ParseException e)
+ catch (JSONException e)
{
log.error(".processCall(): can't parse call: " + cdata, e);
json_res = JSONRPCResult.MSG_ERR_PARSE;
Modified: turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JsonRpcService.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JsonRpcService.java?rev=1662679&r1=1662678&r2=1662679&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JsonRpcService.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/JsonRpcService.java Fri Feb 27 13:22:23 2015
@@ -25,8 +25,8 @@ import javax.servlet.http.HttpServletReq
import javax.servlet.http.HttpSession;
import org.apache.turbine.services.Service;
+import org.jabsorb.JSONRPCBridge;
-import com.metaparadigm.jsonrpc.JSONRPCBridge;
/**
* The interface an JsonRpcService implements.
Modified: turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpc.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpc.java?rev=1662679&r1=1662678&r2=1662679&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpc.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpc.java Fri Feb 27 13:22:23 2015
@@ -25,8 +25,8 @@ import javax.servlet.http.HttpServletReq
import javax.servlet.http.HttpSession;
import org.apache.turbine.services.TurbineServices;
+import org.jabsorb.JSONRPCBridge;
-import com.metaparadigm.jsonrpc.JSONRPCBridge;
/**
* This is a static accessor class for {@link JsonRpcService}.
Modified: turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpcService.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpcService.java?rev=1662679&r1=1662678&r2=1662679&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpcService.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/services/jsonrpc/TurbineJsonRpcService.java Fri Feb 27 13:22:23 2015
@@ -27,8 +27,8 @@ import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.turbine.services.TurbineBaseService;
+import org.jabsorb.JSONRPCBridge;
-import com.metaparadigm.jsonrpc.JSONRPCBridge;
/**
* This is a service that will respond to JSON-RPC calls.
@@ -59,14 +59,12 @@ public class TurbineJsonRpcService
public void registerObjectGlobal(String key, Object value)
{
- JSONRPCBridge.getGlobalBridge().setDebug(DEBUG);
JSONRPCBridge.getGlobalBridge().registerObject(key, value);
}
public void registerObject(HttpSession session, String key, Object value)
{
JSONRPCBridge json_bridge = getBridge(session);
- json_bridge.setDebug(DEBUG);
json_bridge.registerObject(key, value);
}
Modified: turbine/core/trunk/src/test/org/apache/turbine/services/jsonrpc/JsonrpcServicelTest.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/test/org/apache/turbine/services/jsonrpc/JsonrpcServicelTest.java?rev=1662679&r1=1662678&r2=1662679&view=diff
==============================================================================
--- turbine/core/trunk/src/test/org/apache/turbine/services/jsonrpc/JsonrpcServicelTest.java (original)
+++ turbine/core/trunk/src/test/org/apache/turbine/services/jsonrpc/JsonrpcServicelTest.java Fri Feb 27 13:22:23 2015
@@ -27,12 +27,11 @@ import java.util.Map;
import org.apache.turbine.TurbineConstants;
import org.apache.turbine.test.BaseTestCase;
import org.apache.turbine.util.TurbineConfig;
+import org.jabsorb.JSONRPCBridge;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
-import com.metaparadigm.jsonrpc.JSONRPCBridge;
-
public class JsonrpcServicelTest
extends BaseTestCase
{
Modified: turbine/core/trunk/xdocs/services/jsonrpc-service.xml
URL: http://svn.apache.org/viewvc/turbine/core/trunk/xdocs/services/jsonrpc-service.xml?rev=1662679&r1=1662678&r2=1662679&view=diff
==============================================================================
--- turbine/core/trunk/xdocs/services/jsonrpc-service.xml (original)
+++ turbine/core/trunk/xdocs/services/jsonrpc-service.xml Fri Feb 27 13:22:23 2015
@@ -32,7 +32,7 @@
<p>
The JSON-RPC Service supports JavaScript to Java AJAX communications using
-<a href="http://oss.metaparadigm.com/jsonrpc/">JSON-RPC-Java</a>.
+<a href="https://code.google.com/p/jabsorb/">JSON-RPC-Java</a> (Jabsorb replaces project oss.metaparadigm.com/jsonrpc/).
</p>
</section>
@@ -95,7 +95,9 @@ public class MyJsonScreen extends JSONSc
}
]]></source>
+<subsection name="Client Side: Synchronous AJAX Calls">
<p>
+
Now we shift focus to your template classes. Firstly, there are a few useful
utility functions that you need to make sure are available to the pages that
will include AJAX functionality:
@@ -104,47 +106,49 @@ will include AJAX functionality:
<source><![CDATA[
// Body onload utility (supports multiple onload functions)
function SafeAddOnload(func) {
- var oldonload = window.onload;
- if (typeof window.onload != 'function') {
- window.onload = func;
- } else {
- window.onload = function() {
- oldonload();
- func();
- };
- }
+ var oldonload = window.onload;
+ if (typeof window.onload != 'function') {
+ window.onload = func;
+ } else {
+ window.onload = function() {
+ oldonload();
+ func();
+ };
+ }
}
// Prepare for possible JSON-RPC requests.
// jsonurl must be set before calling this function.
-function jsonOnLoad() {
- try {
- jsonrpc = new JSONRpcClient(jsonurl);
- }
- catch(e) {
- if(e.message) {
- alert(e.message);
- }
- else {
- alert(e);
- }
- }
+// First example synchronous call
+function jsonOnLoadSync() {
+ try {
+ jsonrpc = new JSONRpcClient(jsonurl);
+ }
+ catch(e) {
+ if(e.message) {
+ alert(e.message);
+ }
+ else {
+ alert(e);
+ }
+ }
}
+
// Process a JSON-RPC request.
function jsonEval(evalStr) {
- try {
- return eval(evalStr);
- }
- catch(e) {
- if(e.javaStack) {
- alert("Exception: \n\n" + e.javaStack);
- }
- else {
- alert("Exception: \n\n" + e);
- }
- }
- return null;
+ try {
+ return eval(evalStr);
+ }
+ catch(e) {
+ if(e.javaStack) {
+ alert("Exception: \n\n" + e.javaStack);
+ }
+ else {
+ alert("Exception: \n\n" + e);
+ }
+ }
+ return null;
}
]]></source>
@@ -159,7 +163,7 @@ $page.addScript($content.getURI('scripts
]]></source>
<p>
-Then you need to set up the specific handler for the page:
+Then you need to set up the specific handler for the page (synchronous example):
</p>
<source><![CDATA[
@@ -185,6 +189,106 @@ Then you need to set up the specific han
</script>
]]></source>
+</subsection>
+<subsection name="Client Side: Asynchronous AJAX Call">
+
+<p>
+Alternatively, asynchronous calls require handling of the deferred objects, which could be done by using some kind of <a href="https://www.promisejs.org/">Promise</a>) library.
+As an example a <a href="http://api.jquery.com/category/deferred-object/">JQuery Promise Object model</a> (supported since version 1.5) is shown.
+Usage of asynchronous calls conforms more to <a href="ttps://xhr.spec.whatwg.org/#synchronous-flag">XMLHttpRequest specification</a>.
+</p>
+
+<source><![CDATA[
+
+// Prepare for possible JSON-RPC requests.
+// jsonurl must be set before calling this function.
+// Second example asynchronous call
+var deferred;
+function jsonOnLoad() {
+ try {
+ // This feature is available since jabsorb 1.1 release, cft. webapps/jaxrpc/CHANGES.txt.
+ // JQuery Promises are available since jQuery 1.5/1.8 (with major changes).
+ // This is just an example, any other Promise handling will do it.
+ if (deferred != undefined) {
+ return deferred;
+ } else {
+ deferred = jQuery.Deferred(function(defer) {
+ // Add a default function as first parameter to enable asynchronous call.
+ jsonrpc = new JSONRpcClient(function(result, e) {
+ defer.resolve(result,e);
+ }, jsonurl);
+ }).promise();
+ return deferred;
+ }
+ }
+ catch(e) {
+ if(e.message) {
+ alert(e.message);
+ }
+ else {
+ alert(e);
+ }
+ }
+}
+
+]]></source>
+
+<p>
+In these pages you also need to include the JavaScript necessary to process the
+JSON calls - this file is available as part of the JSON-RPC-Java distribution
+(it is included in the <code>webapps\jsonrpc</code> directory):
+</p>
+
+<source><![CDATA[
+$page.addScript($content.getURI('scripts/jsonrpc.js'))
+]]></source>
+
+<p>
+Then you need to set up the specific handler for the page (in this case a asynchronous handler):
+</p>
+
+<source><![CDATA[
+<script type="text/javascript">
+<!--
+ ## Set up the JSON-RPC handler.
+ var jsonurl = '$link.setScreen("MyJsonScreen")';
+
+ ## myArg below would be provided when you call this function from your
+ ## web page (usually you would retrieve something via the DOM or your
+ ## favorite JavaScript DOM wrapper library).
+ function retrieveHello(myArg) {
+ ## This is a an ansynchronous call as you provide as first parameter a callback function to getHello
+ ## (Jabsorb JSON-RPC library expects it this way).
+ var promiseA = jsonOnLoad();
+ var promiseB = jQuery.Deferred();
+ promiseA.done(
+ function() {
+ jQuery.Deferred(function(deferred) {
+ jsonrpc.myFunctions.getHello( function(res,e) {
+ deferred.resolve(); // not used
+ promiseB.resolve(res);
+ }, " + myArg + ")" );
+ }).promise();
+ }
+ );
+ return jQuery.when(promiseA, promiseB);
+ }
+
+ # call function
+ var helloResult;
+ var promise = retrieveHello(myArg);
+ promise.done(
+ function(resultA,resultB) {
+ helloResult = resultB;
+ ## Here or in another dependent Promise you would again use the DOM to include the result somewhere on your
+ ## page.
+ }
+ );
+//-->
+</script>
+]]></source>
+
+</subsection>
<p>
The above code is executable by users that are not logged into your application.
Your Screen class can extend JSONSecureScreen to require that users be logged in