Author: hlship
Date: Thu Sep  4 09:02:54 2008
New Revision: 692133

URL: http://svn.apache.org/viewvc?rev=692133&view=rev
Log:
TAPESTRY-2286: Support JSONArray as a valid return type from AJAX event handlers

Added:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessor.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessorTest.java
Modified:
    tapestry/tapestry5/trunk/src/site/apt/guide/ajax.apt
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java

Modified: tapestry/tapestry5/trunk/src/site/apt/guide/ajax.apt
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/src/site/apt/guide/ajax.apt?rev=692133&r1=692132&r2=692133&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/src/site/apt/guide/ajax.apt (original)
+++ tapestry/tapestry5/trunk/src/site/apt/guide/ajax.apt Thu Sep  4 09:02:54 
2008
@@ -321,7 +321,9 @@
   * A Block or Component to render as the response.  The response will be a 
JSON hash, with a "content" key
     whose value is the rendered markup.  This is the basis for updates with 
the Zone component.
 
-  * A {{{../apidocs/org/apache/tapestry5/json/JSONObject.html}JSONObject}}, 
which will be sent as the response.
+  * A {{{../apidocs/org/apache/tapestry5/json/JSONObject.html}JSONObject}} or 
+    {{{../apidocs/org/apache/tapestry5/json/JSONArray.html}JSONArray}}, 
+    which will be sent as the response.
 
   * A {{{../apidocs/org/apache/tapestry5/StreamResponse.html}StreamResponse}}, 
which will be sent as the response.
 

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessor.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessor.java?rev=692133&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessor.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessor.java
 Thu Sep  4 09:02:54 2008
@@ -0,0 +1,53 @@
+//  Copyright 2008 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.internal.services;
+
+import org.apache.tapestry5.ContentType;
+import org.apache.tapestry5.SymbolConstants;
+import org.apache.tapestry5.internal.InternalConstants;
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.ioc.annotations.Symbol;
+import org.apache.tapestry5.json.JSONArray;
+import org.apache.tapestry5.services.ComponentEventResultProcessor;
+import org.apache.tapestry5.services.Response;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+public class JSONArrayEventResultProcessor implements 
ComponentEventResultProcessor<JSONArray>
+{
+    private final Response response;
+    private final String outputEncoding;
+
+    public JSONArrayEventResultProcessor(Response response,
+
+                                         @Inject 
@Symbol(SymbolConstants.CHARSET)
+                                         String outputEncoding)
+    {
+        this.response = response;
+        this.outputEncoding = outputEncoding;
+    }
+
+    public void processResultValue(JSONArray value) throws IOException
+    {
+        ContentType contentType = new 
ContentType(InternalConstants.JSON_MIME_TYPE, outputEncoding);
+
+        PrintWriter pw = response.getPrintWriter(contentType.toString());
+
+        pw.print(value.toString());
+
+        pw.flush();
+    }
+}

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=692133&r1=692132&r2=692133&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
 Thu Sep  4 09:02:54 2008
@@ -42,6 +42,7 @@
 import org.apache.tapestry5.ioc.services.*;
 import org.apache.tapestry5.ioc.util.StrategyRegistry;
 import org.apache.tapestry5.ioc.util.TimeInterval;
+import org.apache.tapestry5.json.JSONArray;
 import org.apache.tapestry5.json.JSONObject;
 import org.apache.tapestry5.runtime.Component;
 import org.apache.tapestry5.runtime.ComponentResourcesAware;
@@ -1331,9 +1332,9 @@
      * Contributes handlers for the following types: <dl> <dt>Object</dt> 
<dd>Failure case, added to provide more useful
      * exception message</dd> <dt>[EMAIL PROTECTED] RenderCommand}</dt> 
<dd>Typically, a [EMAIL PROTECTED] org.apache.tapestry5.Block}</dd>
      * <dt>[EMAIL PROTECTED] Component}</dt> <dd>Renders the component and its 
body</dd> <dt>[EMAIL PROTECTED]
-     * org.apache.tapestry5.json.JSONObject}</dt> <dd>The JSONObject is 
returned as a text/javascript response</dd>
-     * <dt>[EMAIL PROTECTED] org.apache.tapestry5.StreamResponse}</dt> <dd>The 
stream response is sent as the actual response</dd>
-     * </dl>
+     * org.apache.tapestry5.json.JSONObject} or [EMAIL PROTECTED] 
org.apache.tapestry5.json.JSONArray}</dt> <dd>The JSONObject is
+     * returned as a text/javascript response</dd> <dt>[EMAIL PROTECTED] 
org.apache.tapestry5.StreamResponse}</dt> <dd>The stream
+     * response is sent as the actual response</dd> </dl>
      */
 
     public void contributeAjaxComponentEventResultProcessor(
@@ -1342,6 +1343,7 @@
         configuration.add(RenderCommand.class, 
locator.autobuild(RenderCommandComponentEventResultProcessor.class));
         configuration.add(Component.class, 
locator.autobuild(AjaxComponentInstanceEventResultProcessor.class));
         configuration.add(JSONObject.class, 
locator.autobuild(JSONObjectEventResultProcessor.class));
+        configuration.add(JSONArray.class, 
locator.autobuild(JSONArrayEventResultProcessor.class));
         configuration.add(StreamResponse.class, new 
StreamResponseResultProcessor(response));
     }
 

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessorTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessorTest.java?rev=692133&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessorTest.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/JSONArrayEventResultProcessorTest.java
 Thu Sep  4 09:02:54 2008
@@ -0,0 +1,51 @@
+//  Copyright 2008 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.internal.services;
+
+import org.apache.tapestry5.internal.test.InternalBaseTestCase;
+import org.apache.tapestry5.json.JSONArray;
+import org.apache.tapestry5.services.Response;
+import org.testng.annotations.Test;
+
+import java.io.CharArrayWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+public class JSONArrayEventResultProcessorTest extends InternalBaseTestCase
+{
+    @Test
+    public void response_sent() throws IOException
+    {
+        String encoding = "UTF-8";
+        Response response = mockResponse();
+
+        CharArrayWriter writer = new CharArrayWriter();
+        PrintWriter pw = new PrintWriter(writer);
+
+        
expect(response.getPrintWriter("application/json;charset=UTF-8")).andReturn(pw);
+
+        replay();
+
+        JSONArray array = new JSONArray("   [ \"fred\", \"barney\" \n\n]");
+
+        JSONArrayEventResultProcessor p = new 
JSONArrayEventResultProcessor(response, encoding);
+
+        p.processResultValue(array);
+
+        verify();
+
+        assertEquals(writer.toString(), "[\"fred\",\"barney\"]");
+    }
+}


Reply via email to