Author: lresende
Date: Tue May  5 23:10:08 2009
New Revision: 772005

URL: http://svn.apache.org/viewvc?rev=772005&view=rev
Log:
TUSCANY-2968 - Fixing exception handling issues to properly report exceptions 
as json error responses

Modified:
    
tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatInterceptor.java
    
tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatServiceProvider.java

Modified: 
tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatInterceptor.java
URL: 
http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatInterceptor.java?rev=772005&r1=772004&r2=772005&view=diff
==============================================================================
--- 
tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatInterceptor.java
 (original)
+++ 
tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatInterceptor.java
 Tue May  5 23:10:08 2009
@@ -19,17 +19,11 @@
 
 package org.apache.tuscany.sca.binding.http.wireformat.jsonrpc.provider;
 
-import java.util.List;
-
 import org.apache.tuscany.sca.binding.http.HTTPBinding;
-import org.apache.tuscany.sca.databinding.javabeans.SimpleJavaDataBinding;
-import org.apache.tuscany.sca.databinding.json.JSONDataBinding;
-import org.apache.tuscany.sca.interfacedef.DataType;
-import org.apache.tuscany.sca.interfacedef.Interface;
-import org.apache.tuscany.sca.interfacedef.Operation;
 import org.apache.tuscany.sca.invocation.Interceptor;
 import org.apache.tuscany.sca.invocation.Invoker;
 import org.apache.tuscany.sca.invocation.Message;
+import org.apache.tuscany.sca.invocation.MessageFactory;
 import org.apache.tuscany.sca.runtime.RuntimeWire;
 import org.json.JSONArray;
 import org.json.JSONObject;
@@ -38,16 +32,18 @@
 import com.metaparadigm.jsonrpc.JSONRPCResult;
 
 public class JSONRPCWireFormatInterceptor implements Interceptor {
-   private Invoker next;
+    private Invoker next;
     
     private RuntimeWire runtimeWire;
     private HTTPBinding binding;
     
-    //TODO: Pass messageFactory to create fault messages when error occur
-    public JSONRPCWireFormatInterceptor(HTTPBinding binding, RuntimeWire 
runtimeWire) {
+    private MessageFactory messageFactory;
+    
+    public JSONRPCWireFormatInterceptor(HTTPBinding binding, RuntimeWire 
runtimeWire, MessageFactory messageFactory) {
         this.binding = binding;
         this.runtimeWire = runtimeWire;
         
+        this.messageFactory = messageFactory;
     }
 
     public Invoker getNext() {
@@ -92,11 +88,14 @@
             // They should be reported to the client JavaScript as proper
             // JavaScript exceptions.
 
-            throw new RuntimeException("Error invoking service :" + 
re.getMessage(), re);
+            //throw new RuntimeException("Error invoking service :" + 
re.getMessage(), re);
 
             //FIXME should create a fault message and stuff the JSON Result in 
the body of the message
             //JSONRPCResult errorResult = new 
JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, re);
             //return errorResult.toString().getBytes("UTF-8");
+            
+            Throwable exception = new RuntimeException("Error invoking service 
:" + re.getMessage(), re);
+            return createJSONFaultMessage(re);
         }
 
         Object result = null;
@@ -117,11 +116,19 @@
             //exception thrown while executing the invocation
             //FIXME should create a fault message and stuff the JSON Result in 
the body of the message
             Throwable exception = (Throwable)responseMessage.getBody();
+            return createJSONFaultMessage( exception);
             //JSONRPCResult errorResult = new 
JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, exception );
             //return errorResult.toString().getBytes("UTF-8");
         }
         
+    }
+    
+    private Message createJSONFaultMessage(Throwable throwable) {
+        Message jsonFaultMessage = messageFactory.createMessage();
+        
+        JSONRPCResult jsonFault = new 
JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, null, throwable);
+        jsonFaultMessage.setBody(jsonFault);
         
-        return responseMessage;
+        return jsonFaultMessage;
     }
 }

Modified: 
tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatServiceProvider.java
URL: 
http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatServiceProvider.java?rev=772005&r1=772004&r2=772005&view=diff
==============================================================================
--- 
tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatServiceProvider.java
 (original)
+++ 
tuscany/branches/sca-java-1.x/modules/binding-http-new-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/http/wireformat/jsonrpc/provider/JSONRPCWireFormatServiceProvider.java
 Tue May  5 23:10:08 2009
@@ -26,6 +26,7 @@
 import org.apache.tuscany.sca.assembly.WireFormat;
 import org.apache.tuscany.sca.binding.http.HTTPBinding;
 import 
org.apache.tuscany.sca.binding.http.wireformat.jsonrpc.JSONRPCWireFormat;
+import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.databinding.javabeans.SimpleJavaDataBinding;
 import org.apache.tuscany.sca.databinding.json.JSONDataBinding;
@@ -34,6 +35,7 @@
 import org.apache.tuscany.sca.interfacedef.InterfaceContract;
 import org.apache.tuscany.sca.interfacedef.Operation;
 import org.apache.tuscany.sca.invocation.Interceptor;
+import org.apache.tuscany.sca.invocation.MessageFactory;
 import org.apache.tuscany.sca.invocation.Phase;
 import org.apache.tuscany.sca.provider.WireFormatProvider;
 import org.apache.tuscany.sca.runtime.RuntimeComponent;
@@ -43,6 +45,8 @@
  * @version $Rev$ $Date$
  */
 public class JSONRPCWireFormatServiceProvider implements WireFormatProvider {
+    private MessageFactory messageFactory;
+    
     private RuntimeComponent component;
     private RuntimeComponentService service;
     private InterfaceContract serviceContract;
@@ -53,7 +57,10 @@
                                             RuntimeComponentService service, 
                                             Binding binding) {
         
-        super();
+
+        ModelFactoryExtensionPoint modelFactories = 
extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class);
+        messageFactory = modelFactories.getFactory(MessageFactory.class);
+        
         this.component = component;
         this.service = service;
         this.serviceContract = service.getInterfaceContract();
@@ -76,7 +83,7 @@
             BindingRRB rrbBinding = (BindingRRB) binding;
             WireFormat wireFormat = rrbBinding.getRequestWireFormat();
             if(wireFormat != null && wireFormat instanceof JSONRPCWireFormat) {
-                return new JSONRPCWireFormatInterceptor((HTTPBinding) binding, 
service.getRuntimeWire(binding));
+                return new JSONRPCWireFormatInterceptor((HTTPBinding) binding, 
service.getRuntimeWire(binding), messageFactory);
             }
         }
         


Reply via email to