Author: lresende
Date: Wed Jul 16 17:36:25 2008
New Revision: 677479

URL: http://svn.apache.org/viewvc?rev=677479&view=rev
Log:
TUSCANY-1961 - Properly reporting business and runtime exceptions to clients 
using jsonRPC binding

Added:
    
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java
   (with props)
Modified:
    
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java
    
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java
    
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java
    
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java
    
tuscany/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html
    tuscany/java/sca/samples/store/src/main/resources/uiservices/store.html
    tuscany/java/sca/tutorial/assets/uiservices/store.html
    tuscany/java/sca/tutorial/store-eu/uiservices/store-eu.html
    tuscany/java/sca/tutorial/store-mashup/gadget/store-gadget.html

Modified: 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java?rev=677479&r1=677478&r2=677479&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java
 (original)
+++ 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java
 Wed Jul 16 17:36:25 2008
@@ -42,6 +42,7 @@
 import org.osoa.sca.ServiceRuntimeException;
 
 import com.metaparadigm.jsonrpc.JSONRPCBridge;
+import com.metaparadigm.jsonrpc.JSONRPCResult;
 import com.metaparadigm.jsonrpc.JSONRPCServlet;
 
 /**
@@ -166,7 +167,6 @@
         response.setContentType("text/plain;charset=utf-8");
         OutputStream out = response.getOutputStream();
         byte[] bout = smd.getBytes("UTF-8");
-
         out.write(bout);
         out.flush();
         out.close();
@@ -232,30 +232,27 @@
         RuntimeWire wire = componentService.getRuntimeWire(binding, 
serviceContract);
         Operation jsonOperation = findOperation(method);
         Object result = null;
-        JSONObject jsonResponse = new JSONObject();
+      
         try {
-            result = wire.invoke(jsonOperation, args);
-            try {
+               JSONObject jsonResponse = new JSONObject();
+               result = wire.invoke(jsonOperation, args);
+
+               try {
                 jsonResponse.put("result", result);
                 jsonResponse.putOpt("id", id);
+                //get response to send to client
+                return jsonResponse.toString().getBytes("UTF-8");
             } catch (Exception e) {
-                throw new ServiceRuntimeException(e);
+                throw new ServiceRuntimeException("Unable to create JSON 
response", e);
             }
         } catch (InvocationTargetException e) {
-            try {
-                jsonResponse.put("error", e.getCause());
-                jsonResponse.putOpt("id", id);
-            } catch (Exception e1) {
-                throw new ServiceRuntimeException(e);
-            }
+                JSONRPCResult errorResult = new 
JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, e.getCause() );
+             return errorResult.toString().getBytes("UTF-8");
         } catch(RuntimeException e) {
-            e.printStackTrace();
-            throw e;
+             JSONRPCResult errorResult = new 
JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, e.getCause());
+             return errorResult.toString().getBytes("UTF-8");
         }
-        
-        //get response to send to client
-        return jsonResponse.toString().getBytes("UTF-8");
-    }
+   }
 
     /**
      * Find the operation from the component service contract

Added: 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java?rev=677479&view=auto
==============================================================================
--- 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java
 (added)
+++ 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java
 Wed Jul 16 17:36:25 2008
@@ -0,0 +1,9 @@
+package org.apache.tuscany.sca.binding.jsonrpc;
+
+public class BusinessException extends Exception {
+       public BusinessException(String message)
+       {
+               super(message);
+       }
+
+}

Propchange: 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java?rev=677479&r1=677478&r2=677479&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java
 (original)
+++ 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java
 Wed Jul 16 17:36:25 2008
@@ -26,4 +26,8 @@
 public interface Echo {
     
     String echo(String msg);
+    
+    void echoRuntimeException() throws RuntimeException;
+    
+    void echoBusinessException() throws BusinessException;
 }

Modified: 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java?rev=677479&r1=677478&r2=677479&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java
 (original)
+++ 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java
 Wed Jul 16 17:36:25 2008
@@ -31,4 +31,14 @@
         System.out.println("Echo: "+ msg);
         return "echo: " + msg;
     }
+
+       public void echoBusinessException() throws BusinessException {
+               throw new BusinessException("Business Exception");
+               
+       }
+
+       public void echoRuntimeException() throws RuntimeException {
+               throw new RuntimeException("Runtime Exception");
+               
+       }
 }

Modified: 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java?rev=677479&r1=677478&r2=677479&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java
 (original)
+++ 
tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java
 Wed Jul 16 17:36:25 2008
@@ -59,9 +59,38 @@
         WebResponse response = wc.getResource(request);
 
         assertEquals(200, response.getResponseCode());
+        
         JSONObject jsonResp = new JSONObject(response.getText());
         assertEquals("echo: Hello JSON-RPC", jsonResp.getString("result"));
     }
+    
+    public void testRuntimeException() throws Exception{
+       JSONObject jsonRequest = new JSONObject("{ \"method\": 
\"echoRuntimeException\", \"params\": [], \"id\": 2}");
+       
+       WebConversation wc = new WebConversation();
+        WebRequest request   = new PostMethodWebRequest( SERVICE_URL, new 
ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")),"application/json");
+        WebResponse response = wc.getResource(request);
+
+        assertEquals(200, response.getResponseCode());
+        
+        JSONObject jsonErr = new 
JSONObject(response.getText()).getJSONObject("error");
+        
+        assertEquals("Runtime Exception", jsonErr.getString("msg"));
+    }
+    
+    public void testBusinessException() throws Exception{
+       JSONObject jsonRequest = new JSONObject("{ \"method\": 
\"echoBusinessException\", \"params\": [], \"id\": 3}");
+       
+       WebConversation wc = new WebConversation();
+        WebRequest request   = new PostMethodWebRequest( SERVICE_URL, new 
ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")),"application/json");
+        WebResponse response = wc.getResource(request);
+
+        assertEquals(200, response.getResponseCode());
+        
+        JSONObject jsonErr = new 
JSONObject(response.getText()).getJSONObject("error");
+        
+        assertEquals("Business Exception", jsonErr.getString("msg"));
+    }
 
 
 }

Modified: 
tuscany/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html?rev=677479&r1=677478&r2=677479&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html
 (original)
+++ 
tuscany/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html
 Wed Jul 16 17:36:25 2008
@@ -34,7 +34,11 @@
        //@Property
        var locale = Property("locale");
 
-       function catalog_getResponse(items) {
+       function catalog_getResponse(items,exception) {
+               if(exception){
+                       alert(exception.message);
+                       return;
+               }
                var catalog = "";
                for (var i=0; i<items.length; i++)
                        catalog += '<input name="items" type="checkbox" 
value="' + 
@@ -120,4 +124,4 @@
        </form>    
   </div>
 </body>
-</html>
\ No newline at end of file
+</html>

Modified: 
tuscany/java/sca/samples/store/src/main/resources/uiservices/store.html
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/samples/store/src/main/resources/uiservices/store.html?rev=677479&r1=677478&r2=677479&view=diff
==============================================================================
--- tuscany/java/sca/samples/store/src/main/resources/uiservices/store.html 
(original)
+++ tuscany/java/sca/samples/store/src/main/resources/uiservices/store.html Wed 
Jul 16 17:36:25 2008
@@ -35,20 +35,25 @@
        
        var catalogItems;
 
-       function catalog_getResponse(items) {
-               var catalog = "";
-               for (var i=0; i<items.length; i++) {
-                       var item = items[i].name + ' - ' + items[i].price;
-                       catalog += '<input name="items" type="checkbox" 
value="' + 
-                                               item + '">' + item + ' <br>';
+       function catalog_getResponse(items,exception) {
+               if(exception){
+                       alert(exception.message);
+                       return;
                }
-               document.getElementById('catalog').innerHTML=catalog;
-               catalogItems = items;
+               var catalog = "";
+               
+                       for (var i=0; i<items.length; i++) {
+                               var item = items[i].name + ' - ' + 
items[i].price;
+                               catalog += '<input name="items" type="checkbox" 
value="' + 
+                                                       item + '">' + item + ' 
<br>';
+                       }
+                       document.getElementById('catalog').innerHTML=catalog;
+                       catalogItems = items;
        }
        
        function shoppingCart_getResponse(feed) {
                if (feed != null) {
-                       var entries = feed.getElementsByTagName("entry");       
       
+                       var entries = feed.getElementsByTagName("entry"); 
                        var list = "";
                        for (var i=0; i<entries.length; i++) {
                                var content = 
entries[i].getElementsByTagName("content")[0];
@@ -59,12 +64,21 @@
                        document.getElementById("shoppingCart").innerHTML = 
list;
 
                        if (entries.length != 0) {                      
-                               
shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
+                                       try     {
+                                               
shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
+                                       }
+                                       catch(e){
+                                               alert(e);
+                                       }
                        }
                }
        }
        
-       function shoppingTotal_getTotalResponse(total) {
+       function shoppingTotal_getTotalResponse(total,exception) {
+               if(exception) { 
+                       alert(exception.message); 
+                       return;
+               }
                document.getElementById('total').innerHTML = total;
        }
        
@@ -77,6 +91,7 @@
                var j = 0;
                for (var i=0; i<items.length; i++)
                        if (items[i].checked) {
+                                       
                                var entry = '<entry 
xmlns="http://www.w3.org/2005/Atom";><title>item</title><content 
type="text/xml">' +
                        '<Item xmlns="http://services/";>' +
                        '<name xmlns="">' + catalogItems[i].name + '</name>' + 
'<price xmlns="">' + catalogItems[i].price + '</price>' +
@@ -106,9 +121,15 @@
        }       
 
        function init() {
-               catalog.get(catalog_getResponse);
-               shoppingCart.get("", shoppingCart_getResponse);
-       }
+                       
+                       try     {
+                               catalog.get(catalog_getResponse);
+                               shoppingCart.get("", shoppingCart_getResponse);
+                       }
+                       catch(e){
+                               alert(e);
+                       }
+               }
        
 </script>
 

Modified: tuscany/java/sca/tutorial/assets/uiservices/store.html
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/tutorial/assets/uiservices/store.html?rev=677479&r1=677478&r2=677479&view=diff
==============================================================================
--- tuscany/java/sca/tutorial/assets/uiservices/store.html (original)
+++ tuscany/java/sca/tutorial/assets/uiservices/store.html Wed Jul 16 17:36:25 
2008
@@ -35,7 +35,11 @@
        
        var catalogItems;
 
-       function catalog_getResponse(items) {
+       function catalog_getResponse(items,exception) {
+               if(exception){
+                       alert(exception.message);
+                       return;
+               }
                var catalog = "";
                for (var i=0; i<items.length; i++) {
                        var item = items[i].name + ' - ' + items[i].price;
@@ -58,13 +62,22 @@
                        }
                        document.getElementById("shoppingCart").innerHTML = 
list;
 
-                       if (entries.length != 0) {                      
-                               
shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
+                       if (entries.length != 0) {
+                               try {
+                                       
shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
+                               }
+                               catch(e) {
+                                       alert(e);
+                               }
                        }
                }
        }
        
-       function shoppingTotal_getTotalResponse(total) {
+       function shoppingTotal_getTotalResponse(total,exception) {
+               if(exception){
+                       alert(exception.message);
+                       return;
+               }
                document.getElementById('total').innerHTML = total;
        }
        
@@ -106,8 +119,13 @@
        }       
 
        function init() {
-               catalog.get(catalog_getResponse);
-               shoppingCart.get("", shoppingCart_getResponse);
+               try {
+                       catalog.get(catalog_getResponse);
+                       shoppingCart.get("", shoppingCart_getResponse);
+               }
+               catch(e) {
+                       alert(e);
+               }
        }
        
 </script>

Modified: tuscany/java/sca/tutorial/store-eu/uiservices/store-eu.html
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/tutorial/store-eu/uiservices/store-eu.html?rev=677479&r1=677478&r2=677479&view=diff
==============================================================================
--- tuscany/java/sca/tutorial/store-eu/uiservices/store-eu.html (original)
+++ tuscany/java/sca/tutorial/store-eu/uiservices/store-eu.html Wed Jul 16 
17:36:25 2008
@@ -35,7 +35,11 @@
        
        var catalogItems;
 
-       function catalog_getResponse(items) {
+       function catalog_getResponse(items, exception) {
+               if(exception){
+                       alert(exception.message);
+                       return;
+               }
                var catalog = "";
                for (var i=0; i<items.length; i++) {
                        var item = items[i].name + ' - ' + items[i].price;
@@ -58,13 +62,23 @@
                        }
                        document.getElementById("shoppingCart").innerHTML = 
list;
 
-                       if (entries.length != 0) {                      
-                               
shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
+                       if (entries.length != 0) {      
+                               try {
+                                       
shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
+                               }
+                               carch(e) {
+                                       alert(e);
+                               }
+                                       
                        }
                }
        }
        
-       function shoppingTotal_getTotalResponse(total) {
+       function shoppingTotal_getTotalResponse(total,exception) {
+               if(exception){
+                       alert(exception.message);
+                       return;
+               }
                document.getElementById('total').innerHTML = total;
        }
        
@@ -106,8 +120,14 @@
        }       
 
        function init() {
-               catalog.get(catalog_getResponse);
-               shoppingCart.get("", shoppingCart_getResponse);
+               try     {
+                       catalog.get(catalog_getResponse);
+                       shoppingCart.get("", shoppingCart_getResponse);
+               }
+               catch(e) {
+                       alert(e);
+               }
+                       
        }
        
 </script>

Modified: tuscany/java/sca/tutorial/store-mashup/gadget/store-gadget.html
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/tutorial/store-mashup/gadget/store-gadget.html?rev=677479&r1=677478&r2=677479&view=diff
==============================================================================
--- tuscany/java/sca/tutorial/store-mashup/gadget/store-gadget.html (original)
+++ tuscany/java/sca/tutorial/store-mashup/gadget/store-gadget.html Wed Jul 16 
17:36:25 2008
@@ -35,7 +35,11 @@
        
        var catalogItems;
 
-       function catalog_getResponse(items) {
+       function catalog_getResponse(items,exception) {
+               if(exception){
+                       alert(exception.message);
+                       return;
+               }
                var catalog = "";
                for (var i=0; i<items.length; i++) {
                        var item = items[i].name + ' - ' + items[i].price;
@@ -58,13 +62,22 @@
                        }
                        document.getElementById("shoppingCart").innerHTML = 
list;
 
-                       if (entries.length != 0) {                      
-                               
shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
+                       if (entries.length != 0) {
+                               try {                   
+                                       
shoppingTotal.getTotal(shoppingTotal_getTotalResponse);
+                               }
+                               carch(e) {
+                                       alert(e);
+                               }
                        }
                }
        }
        
-       function shoppingTotal_getTotalResponse(total) {
+       function shoppingTotal_getTotalResponse(total,exception) {
+               if(exception){
+                       alert(exception.message);
+                       return;
+               }
                document.getElementById('total').innerHTML = total;
        }
        
@@ -113,8 +126,13 @@
        }               
 
        function init() {
-               catalog.get(catalog_getResponse);
-               shoppingCart.get("", shoppingCart_getResponse);
+                       try {           
+                               catalog.get(catalog_getResponse);
+                               shoppingCart.get("", shoppingCart_getResponse);
+                       }
+                       catch(e) {
+                               alert(e);
+                       }
        }
        
 </script>


Reply via email to