From d87b7a5e7430ab946c851208e45ddbf03330c98f Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 17 Jul 2008 00:36:25 +0000 Subject: TUSCANY-1961 - Properly reporting business and runtime exceptions to clients using jsonRPC binding git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@677479 13f79535-47bb-0310-9956-ffa450edef68 --- .../jsonrpc/provider/JSONRPCServiceServlet.java | 31 +++++++------- .../sca/binding/jsonrpc/BusinessException.java | 9 ++++ .../apache/tuscany/sca/binding/jsonrpc/Echo.java | 4 ++ .../sca/binding/jsonrpc/EchoComponentImpl.java | 10 +++++ .../binding/jsonrpc/JSONRPCServiceTestCase.java | 29 +++++++++++++ .../src/test/resources/content/store.html | 8 +++- .../store/src/main/resources/uiservices/store.html | 49 +++++++++++++++------- java/sca/tutorial/assets/uiservices/store.html | 30 ++++++++++--- .../sca/tutorial/store-eu/uiservices/store-eu.html | 32 +++++++++++--- .../tutorial/store-mashup/gadget/store-gadget.html | 30 ++++++++++--- 10 files changed, 181 insertions(+), 51 deletions(-) create mode 100644 java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java b/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java index da4461c1cd..c0d0415f91 100644 --- a/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java +++ b/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java @@ -42,6 +42,7 @@ import org.json.JSONObject; import org.osoa.sca.ServiceRuntimeException; import com.metaparadigm.jsonrpc.JSONRPCBridge; +import com.metaparadigm.jsonrpc.JSONRPCResult; import com.metaparadigm.jsonrpc.JSONRPCServlet; /** @@ -166,7 +167,6 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { 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 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { 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 diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java new file mode 100644 index 0000000000..14ee22931d --- /dev/null +++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java @@ -0,0 +1,9 @@ +package org.apache.tuscany.sca.binding.jsonrpc; + +public class BusinessException extends Exception { + public BusinessException(String message) + { + super(message); + } + +} diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java index 584228abde..1aff8784f7 100644 --- a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java +++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java @@ -26,4 +26,8 @@ package org.apache.tuscany.sca.binding.jsonrpc; public interface Echo { String echo(String msg); + + void echoRuntimeException() throws RuntimeException; + + void echoBusinessException() throws BusinessException; } diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java index e63cafd6a6..b62fab0840 100644 --- a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java +++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java @@ -31,4 +31,14 @@ public class EchoComponentImpl implements Echo { 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"); + + } } diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java index f937e80cf5..2ad3db1a55 100644 --- a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java +++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java @@ -59,9 +59,38 @@ public class JSONRPCServiceTestCase extends TestCase { 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")); + } } diff --git a/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html b/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html index c2277772bf..58f9f560a7 100644 --- a/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html +++ b/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html @@ -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 - \ No newline at end of file + diff --git a/java/sca/samples/store/src/main/resources/uiservices/store.html b/java/sca/samples/store/src/main/resources/uiservices/store.html index 095570f949..45604463d1 100644 --- a/java/sca/samples/store/src/main/resources/uiservices/store.html +++ b/java/sca/samples/store/src/main/resources/uiservices/store.html @@ -35,20 +35,25 @@ var catalogItems; - function catalog_getResponse(items) { - var catalog = ""; - for (var i=0; i' + item + '
'; + 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' + item + '
'; + } + 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' + '' + catalogItems[i].name + '' + '' + catalogItems[i].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); + } + } diff --git a/java/sca/tutorial/assets/uiservices/store.html b/java/sca/tutorial/assets/uiservices/store.html index 095570f949..f2425b4885 100644 --- a/java/sca/tutorial/assets/uiservices/store.html +++ b/java/sca/tutorial/assets/uiservices/store.html @@ -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 diff --git a/java/sca/tutorial/store-eu/uiservices/store-eu.html b/java/sca/tutorial/store-eu/uiservices/store-eu.html index ec72d9b2a7..4569b76b3f 100644 --- a/java/sca/tutorial/store-eu/uiservices/store-eu.html +++ b/java/sca/tutorial/store-eu/uiservices/store-eu.html @@ -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 diff --git a/java/sca/tutorial/store-mashup/gadget/store-gadget.html b/java/sca/tutorial/store-mashup/gadget/store-gadget.html index c2a8ba5118..c09b9d28c8 100644 --- a/java/sca/tutorial/store-mashup/gadget/store-gadget.html +++ b/java/sca/tutorial/store-mashup/gadget/store-gadget.html @@ -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 -- cgit v1.2.3