cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1225521 - in /cxf/trunk: 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:07:42 GMT
Author: sergeyb
Date: Thu Dec 29 13:07:42 2011
New Revision: 1225521

URL: http://svn.apache.org/viewvc?rev=1225521&view=rev
Log:
[CXF-4000] Optional formatting of the JSON ouput

Added:
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java
  (with props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=1225521&r1=1225520&r2=1225521&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Thu Dec 29 13:07:42 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/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java?rev=1225521&r1=1225520&r2=1225521&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java Thu
Dec 29 13:07:42 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);

Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java?rev=1225521&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java
(added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java
Thu Dec 29 13:07:42 2011
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.cxf.systest.jaxrs;
+
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import javax.xml.bind.Marshaller;
+
+import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.jaxrs.ext.ResponseHandler;
+import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.message.Message;
+
+public class FormatResponseHandler implements ResponseHandler {
+    @Context
+    private MessageContext mc;
+    
+    @Override
+    public Response handleResponse(Message m, OperationResourceInfo ori, Response response)
{
+        if (mc.getUriInfo().getQueryParameters().containsKey("_format")) {
+            mc.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
+        }
+        return null;
+    }
+
+}

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1225521&r1=1225520&r2=1225521&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Thu Dec 29 13:07:42 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