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 ++++++++++++++++++++ 5 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java (limited to 'java/sca/modules/binding-jsonrpc-runtime/src') 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")); + } } -- cgit v1.2.3