cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1225523 - in /cxf/branches/2.4.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Thu, 29 Dec 2011 13:13:26 GMT
Author: sergeyb
Date: Thu Dec 29 13:13:25 2011
New Revision: 1225523

URL: http://svn.apache.org/viewvc?rev=1225523&view=rev
Log:
Merged revisions 1225521 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1225521 | sergeyb | 2011-12-29 13:07:42 +0000 (Thu, 29 Dec 2011) | 1 line
  
  [CXF-4000] Optional formatting of the JSON ouput
........

Added:
    cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java
      - copied unchanged from r1225521, cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java
Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
    cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Dec 29 13:13:25 2011
@@ -1 +1 @@
-/cxf/trunk:1225217,1225494
+/cxf/trunk:1225217,1225494,1225521

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=1225523&r1=1225522&r2=1225523&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Thu Dec 29 13:13:25 2011
@@ -24,7 +24,10 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.SequenceInputStream;
+import java.io.StringReader;
+import java.io.Writer;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.Arrays;
@@ -54,16 +57,20 @@ import javax.xml.stream.XMLStreamWriter;
 
 import org.w3c.dom.Document;
 
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.JAXBUtils;
 import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler;
+import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.staxutils.StaxUtils;
 import org.apache.cxf.staxutils.W3CDOMStreamWriter;
 import org.codehaus.jettison.mapped.Configuration;
 import org.codehaus.jettison.mapped.SimpleConverter;
 import org.codehaus.jettison.mapped.TypeConverter;
+import org.codehaus.jettison.util.StringIndenter;
 
 @Produces("application/json")
 @Consumes("application/json")
@@ -430,11 +437,23 @@ public class JSONProvider extends Abstra
     
     protected void marshal(Marshaller ms, Object actualObject, Class<?> actualClass,

                   Type genericType, String enc, OutputStream os, boolean isCollection) throws
Exception {
+        OutputStream actualOs = os; 
         
+        MessageContext mc = getContext();
+        if (mc != null && MessageUtils.isTrue(mc.get(Marshaller.JAXB_FORMATTED_OUTPUT)))
{
+            actualOs = new CachedOutputStream();    
+        }
         XMLStreamWriter writer = createWriter(actualObject, actualClass, genericType, enc,

-                                              os, isCollection);
+                                              actualOs, isCollection);
         ms.marshal(actualObject, writer);
         writer.close();
+        if (os != actualOs) {
+            StringIndenter formatter = new StringIndenter(
+                IOUtils.newStringFromBytes(((CachedOutputStream)actualOs).getBytes()));
+            Writer outWriter = new OutputStreamWriter(os, enc);
+            IOUtils.copy(new StringReader(formatter.result()), outWriter, 2048);
+            outWriter.close();
+        }
     }
     
     protected XMLStreamWriter createWriter(Object actualObject, Class<?> actualClass,


Modified: cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java?rev=1225523&r1=1225522&r2=1225523&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
(original)
+++ cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
Thu Dec 29 13:13:25 2011
@@ -52,7 +52,7 @@ public class BookServer extends Abstract
         jaxbElementClassMap.put(BookNoXmlRootElement.class.getName(), "BookNoXmlRootElement");
         jaxbProvider.setJaxbElementClassMap(jaxbElementClassMap);
         providers.add(jaxbProvider);
-        
+        providers.add(new FormatResponseHandler());
         providers.add(new GenericHandlerWriter());
         providers.add(new FaultyRequestHandler());
         sf.setProviders(providers);

Modified: cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1225523&r1=1225522&r2=1225523&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Thu Dec 29 13:13:25 2011
@@ -146,7 +146,6 @@ public class JAXRSClientServerBookTest e
         String name = "Many        spaces";
         wc.query("name", name);
         String content = wc.get(String.class);
-        System.out.println(content);
         assertTrue(content.contains("<!DOCTYPE Something SYSTEM 'my.dtd'>"));
         assertTrue(content.contains("<?xmlstylesheet href='" + base + "/common.css'?>"));
         assertTrue(content.contains("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""));
@@ -985,6 +984,33 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testFormattedJSON() {
+        WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/books/123");
+        wc.accept("application/json");
+        String response = wc.get(String.class);
+        // {"Book":{"id":123,"name":"CXF in Action"}}
+        
+        assertTrue(response.startsWith("{"));
+        assertTrue(response.endsWith("}"));
+        assertTrue(response.contains("\"Book\":{"));
+        assertTrue(response.indexOf("\"Book\":{") == 1);
+        
+        wc.query("_format", "");
+        response = wc.get(String.class);
+        //{
+        //    "Book":{
+        //      "id":123,
+        //      "name":"CXF in Action"
+        //    }
+        //}
+        assertTrue(response.startsWith("{"));
+        assertTrue(response.endsWith("}"));
+        assertTrue(response.contains("\"Book\":{"));
+        assertFalse(response.indexOf("\"Book\":{") == 1);
+        
+    }
+    
+    @Test
     public void testGetBook123() throws Exception {
         getAndCompareAsStrings("http://localhost:" + PORT + "/bookstore/books/123",
                                "resources/expected_get_book123.txt",



Mime
View raw message