You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by aw...@apache.org on 2009/04/24 00:07:03 UTC

svn commit: r768072 - in /incubator/shindig/trunk/java/common/src: main/java/org/apache/shindig/protocol/ test/java/org/apache/shindig/protocol/

Author: awiner
Date: Thu Apr 23 22:07:03 2009
New Revision: 768072

URL: http://svn.apache.org/viewvc?rev=768072&view=rev
Log:
SHINDIG-1028: Json RPC should support "data" section which sending out error.
ProtocolException now lets you set a response that will fill out "data" in the error object
Patch from Sachin Shenoy, with a few changes:
- SocialSpiException not modified - ProtocolException should be used instead
- Cleaned up tests to use JsonAsserts, not Map.toString()
- Don't bother supporting DataCollection/etc. as "data" in error objects

Modified:
    incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java
    incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/JsonRpcServlet.java
    incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ProtocolException.java
    incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ResponseItem.java
    incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/JsonRpcServletTest.java

Modified: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java?rev=768072&r1=768071&r2=768072&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java (original)
+++ incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ApiServlet.java Thu Apr 23 22:07:03 2009
@@ -142,9 +142,9 @@
 
   protected ResponseItem responseItemFromException(Throwable t) {
     if (t instanceof ProtocolException) {
-      ProtocolException spe = (ProtocolException) t;
-      logger.log(Level.INFO, "Returning a response error as result of a protocol exception", spe);
-      return new ResponseItem(spe.getCode(), spe.getMessage());
+      ProtocolException pe = (ProtocolException) t;
+      logger.log(Level.INFO, "Returning a response error as result of a protocol exception", pe);
+      return new ResponseItem(pe.getCode(), pe.getMessage(), pe.getResponse());
     }
     logger.log(Level.WARNING, "Returning a response error as result of an exception", t);
     return new ResponseItem(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.getMessage());

Modified: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/JsonRpcServlet.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/JsonRpcServlet.java?rev=768072&r1=768071&r2=768072&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/JsonRpcServlet.java (original)
+++ incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/JsonRpcServlet.java Thu Apr 23 22:07:03 2009
@@ -201,7 +201,7 @@
     return dispatcher.getRpcHandler(rpc);
   }
 
-  private Object getJSONResponse(String key, ResponseItem responseItem) {
+  Object getJSONResponse(String key, ResponseItem responseItem) {
     Map<String, Object> result = Maps.newHashMap();
     if (key != null) {
       result.put("id", key);
@@ -258,7 +258,11 @@
     if (StringUtils.isNotBlank(message)) {
       error.put("message", message);
     }
-    
+
+    if (responseItem.getResponse() != null) {
+      error.put("data", responseItem.getResponse());
+    }
+
     return error;
   }
 

Modified: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ProtocolException.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ProtocolException.java?rev=768072&r1=768071&r2=768072&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ProtocolException.java (original)
+++ incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ProtocolException.java Thu Apr 23 22:07:03 2009
@@ -27,24 +27,39 @@
 public class ProtocolException extends RuntimeException {
   private final int errorCode;
 
+  /**
+   * The applicatin specific response value associated with this exception.
+   */
+  private final Object response;
+
   public ProtocolException(int errorCode, String errorMessage, Throwable cause) {
     super(errorMessage, cause);
     checkErrorCode(errorCode);
     this.errorCode = errorCode;
+    this.response = null;
   }
 
   public ProtocolException(int errorCode, String errorMessage) {
+    this(errorCode, errorMessage, null);
+  }
+
+  public ProtocolException(int errorCode, String errorMessage, Object response) {
     super(errorMessage);
     checkErrorCode(errorCode);
     this.errorCode = errorCode;
+    this.response = response;
   }
 
   public int getCode() {
     return errorCode;
   }
 
+  public Object getResponse() {
+    return response;
+  }
+
   private void checkErrorCode(int code) {
-    // 200 is not a legit use of ProtocolExceptions. 
+    // 200 is not a legit use of ProtocolExceptions.
     Preconditions.checkArgument(code != HttpServletResponse.SC_OK,
         "May not use OK error code with ProtocolException");
   }

Modified: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ResponseItem.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ResponseItem.java?rev=768072&r1=768071&r2=768072&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ResponseItem.java (original)
+++ incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/protocol/ResponseItem.java Thu Apr 23 22:07:03 2009
@@ -45,9 +45,20 @@
    * @param errorMessage the Error Message
    */
   public ResponseItem(int errorCode, String errorMessage) {
+    this(errorCode, errorMessage, null);
+  }
+
+  /**
+   * Create a ResponseItem specifying the ResponseError and error Message.
+   * @param errorCode an RPC error code
+   * @param errorMessage the Error Message
+   * @param response the application specific value that will be sent as
+   *     as part of "data" section of the error.
+   */
+  public ResponseItem(int errorCode, String errorMessage, Object response) {
     this.errorCode = errorCode;
     this.errorMessage = errorMessage;
-    this.response = null;
+    this.response = response;
   }
 
   /**
@@ -102,4 +113,4 @@
   public int hashCode() {
     return Objects.hashCode(errorCode, errorMessage, response);
   }
-}
\ No newline at end of file
+}

Modified: incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/JsonRpcServletTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/JsonRpcServletTest.java?rev=768072&r1=768071&r2=768072&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/JsonRpcServletTest.java (original)
+++ incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/protocol/JsonRpcServletTest.java Thu Apr 23 22:07:03 2009
@@ -349,6 +349,34 @@
     JsonAssert.assertJsonEquals("{id:'1',data:{foo:'bar'}}", getOutput());
   }
 
+  public void testGetJsonResponseWithKey() throws Exception {
+    ResponseItem responseItem = new ResponseItem("Name");
+    Object result = servlet.getJSONResponse("my-key", responseItem);
+    JsonAssert.assertObjectEquals("{id: 'my-key', data: 'Name'}", result);
+  }
+
+  public void testGetJsonResponseWithoutKey() throws Exception {
+    ResponseItem responseItem = new ResponseItem("Name");
+    Object result = servlet.getJSONResponse(null, responseItem);
+    JsonAssert.assertObjectEquals("{data: 'Name'}", result);
+  }
+
+  public void testGetJsonResponseErrorWithData() throws Exception {
+    ResponseItem responseItem = new ResponseItem(401, "Error Message", "Optional Data");
+    Object result = servlet.getJSONResponse(null, responseItem);
+    JsonAssert.assertObjectEquals(
+        "{error: {message: 'unauthorized: Error Message', data: 'Optional Data', code: 401}}",
+        result);
+  }
+
+  public void testGetJsonResponseErrorWithoutData() throws Exception {
+    ResponseItem responseItem = new ResponseItem(401, "Error Message");
+    Object result = servlet.getJSONResponse(null, responseItem);
+    JsonAssert.assertObjectEquals(
+        "{error: {message:'unauthorized: Error Message', code:401}}",
+        result);
+  }
+
   private void setupRequest(String json) throws IOException {
     final InputStream in = new ByteArrayInputStream(json.getBytes());
     ServletInputStream stream = new ServletInputStream() {