Author: sergeyb
Date: Thu Sep 29 09:00:23 2011
New Revision: 1177229
URL: http://svn.apache.org/viewvc?rev=1177229&view=rev
Log:
Merged revisions 1177096 via svnmerge from
https://svn.apache.org/repos/asf/cxf/trunk
........
r1177096 | sergeyb | 2011-09-28 23:47:15 +0100 (Wed, 28 Sep 2011) | 1 line
[CXF-3832] Updating JSONProvider and SourceProvider to handle Documents
........
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/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java
Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Sep 29 09:00:23 2011
@@ -1 +1 @@
-/cxf/trunk:1177164
+/cxf/trunk:1177096,1177164
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=1177229&r1=1177228&r2=1177229&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 Sep 29 09:00:23 2011
@@ -52,12 +52,15 @@ import javax.xml.stream.XMLStreamExcepti
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
+import org.w3c.dom.Document;
+
import org.apache.cxf.jaxrs.ext.MessageContext;
-import org.apache.cxf.jaxrs.provider.AbstractJAXBProvider.CollectionWrapper;
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.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;
@@ -178,18 +181,27 @@ public class JSONProvider extends Abstra
this.namespaceMap.putAll(namespaceMap);
}
+ @Override
+ public boolean isReadable(Class<?> type, Type genericType, Annotation[]
anns, MediaType mt) {
+ return super.isReadable(type, genericType, anns, mt) ||
Document.class.isAssignableFrom(type);
+ }
+
public Object readFrom(Class<Object> type, Type genericType, Annotation[]
anns, MediaType mt,
MultivaluedMap<String, String> headers, InputStream is)
throws IOException {
try {
+ InputStream realStream = getInputStream(type, genericType, is);
+ if (Document.class.isAssignableFrom(type)) {
+ W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
+ copyReaderToWriter(createReader(type, realStream, false),
writer);
+ return writer.getDocument();
+ }
boolean isCollection =
InjectionUtils.isSupportedCollectionOrArray(type);
Class<?> theGenericType = isCollection ?
InjectionUtils.getActualType(genericType) : type;
Class<?> theType = getActualType(theGenericType, genericType,
anns);
Unmarshaller unmarshaller = createUnmarshaller(theType,
genericType, isCollection);
-
- InputStream realStream = getInputStream(type, genericType, is);
XMLStreamReader xsr = createReader(type, realStream, isCollection);
Object response = null;
@@ -291,6 +303,12 @@ public class JSONProvider extends Abstra
return "{\"" + name + "\":";
}
+ public boolean isWriteable(Class<?> type, Type genericType, Annotation[]
anns, MediaType mt) {
+
+ return super.isWriteable(type, genericType, anns, mt)
+ || Document.class.isAssignableFrom(type);
+ }
+
public void writeTo(Object obj, Class<?> cls, Type genericType,
Annotation[] anns,
MediaType m, MultivaluedMap<String, Object> headers, OutputStream os)
throws IOException {
@@ -305,10 +323,14 @@ public class JSONProvider extends Abstra
}
try {
- String encoding = HttpUtils.getSetEncoding(m, headers, "UTF-8");
-
+ String enc = HttpUtils.getSetEncoding(m, headers, "UTF-8");
+ if (Document.class.isAssignableFrom(cls)) {
+ XMLStreamWriter writer = createWriter(obj, cls, genericType,
enc, os, false);
+
copyReaderToWriter(StaxUtils.createXMLStreamReader((Document)obj), writer);
+ return;
+ }
if (InjectionUtils.isSupportedCollectionOrArray(cls)) {
- marshalCollection(cls, obj, genericType, encoding, os, m,
anns);
+ marshalCollection(cls, obj, genericType, enc, os, m, anns);
} else {
Object actualObject = checkAdapter(obj, cls, anns, true);
Class<?> actualClass = obj != actualObject ?
actualObject.getClass() : cls;
@@ -316,7 +338,7 @@ public class JSONProvider extends Abstra
genericType = actualClass;
}
- marshal(actualObject, actualClass, genericType, encoding, os);
+ marshal(actualObject, actualClass, genericType, enc, os);
}
} catch (JAXBException e) {
@@ -328,6 +350,13 @@ public class JSONProvider extends Abstra
}
}
+ protected void copyReaderToWriter(XMLStreamReader reader, XMLStreamWriter
writer)
+ throws Exception {
+ writer.writeStartDocument();
+ StaxUtils.copy(reader, writer);
+ writer.writeEndDocument();
+ }
+
protected void marshalCollection(Class<?> originalCls, Object collection,
Type genericType, String encoding,
OutputStream os, MediaType m,
Annotation[] anns)
@@ -411,12 +440,12 @@ public class JSONProvider extends Abstra
protected XMLStreamWriter createWriter(Object actualObject, Class<?>
actualClass,
Type genericType, String enc, OutputStream os, boolean isCollection)
throws Exception {
+ QName qname = getQName(actualClass, genericType, actualObject, true);
+
if (BADGER_FISH_CONVENTION.equals(convention)) {
return JSONUtils.createBadgerFishWriter(os);
}
- QName qname = getQName(actualClass, genericType, actualObject, true);
-
Configuration config =
JSONUtils.createConfiguration(namespaceMap,
writeXsiType && !ignoreNamespaces,
Modified:
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
URL:
http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java?rev=1177229&r1=1177228&r2=1177229&view=diff
==============================================================================
---
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
(original)
+++
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
Thu Sep 29 09:00:23 2011
@@ -55,7 +55,7 @@ import org.apache.cxf.staxutils.StaxUtil
@Produces({"application/xml", "application/*+xml", "text/xml" })
@Consumes({"application/xml", "application/*+xml", "text/xml", "text/html" })
public class SourceProvider extends AbstractConfigurableProvider implements
- MessageBodyReader<Object>, MessageBodyWriter<Source> {
+ MessageBodyReader<Object>, MessageBodyWriter<Object> {
private static final String PREFERRED_FORMAT = "source-preferred-format";
@Context
@@ -63,7 +63,8 @@ public class SourceProvider extends Abst
public boolean isWriteable(Class<?> type, Type genericType, Annotation[]
annotations, MediaType mt) {
- return Source.class.isAssignableFrom(type);
+ return Source.class.isAssignableFrom(type)
+ || Document.class.isAssignableFrom(type);
}
public boolean isReadable(Class<?> type, Type genericType, Annotation[]
annotations, MediaType mt) {
@@ -148,13 +149,15 @@ public class SourceProvider extends Abst
}
}
- public void writeTo(Source source, Class<?> clazz, Type genericType,
Annotation[] annotations,
+ public void writeTo(Object source, Class<?> clazz, Type genericType,
Annotation[] annotations,
MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os)
throws IOException {
String encoding = HttpUtils.getSetEncoding(mt, headers, "UTF-8");
- XMLStreamReader reader = StaxUtils.createXMLStreamReader(source);
+ XMLStreamReader reader =
+ source instanceof Source ?
StaxUtils.createXMLStreamReader((Source)source)
+ : StaxUtils.createXMLStreamReader((Document)source);
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os, encoding);
try {
StaxUtils.copy(reader, writer);
@@ -174,7 +177,7 @@ public class SourceProvider extends Abst
}
}
- public long getSize(Source source, Class<?> type, Type genericType,
Annotation[] annotations,
+ public long getSize(Object source, Class<?> type, Type genericType,
Annotation[] annotations,
MediaType mt) {
return -1;
}
Modified:
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
URL:
http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java?rev=1177229&r1=1177228&r2=1177229&view=diff
==============================================================================
---
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
(original)
+++
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
Thu Sep 29 09:00:23 2011
@@ -22,6 +22,7 @@ package org.apache.cxf.jaxrs.provider;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.io.StringReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
@@ -48,8 +49,12 @@ import javax.xml.bind.annotation.XmlElem
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
+import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.stream.StreamSource;
+import org.w3c.dom.Document;
+
+import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.jaxrs.fortest.jaxb.packageinfo.Book2;
import org.apache.cxf.jaxrs.impl.MetadataMap;
import org.apache.cxf.jaxrs.provider.JAXBElementProviderTest.TagVO2Holder;
@@ -60,6 +65,7 @@ import org.apache.cxf.jaxrs.resources.Su
import org.apache.cxf.jaxrs.resources.TagVO;
import org.apache.cxf.jaxrs.resources.TagVO2;
import org.apache.cxf.jaxrs.resources.Tags;
+import org.apache.cxf.staxutils.StaxUtils;
import org.junit.Assert;
import org.junit.Ignore;
@@ -266,6 +272,46 @@ public class JSONProviderTest extends As
}
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testCopyReaderToDocument() throws Exception {
+ String s = "{\"tagVO\":{\"group\":\"b\",\"name\":\"a\"}}";
+
+ ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
+
+ Document doc = (Document)new
JSONProvider().readFrom((Class)Document.class, Document.class,
+ new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
+ new MetadataMap<String, String>(), is);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os);
+ StaxUtils.copy(doc, writer);
+ writer.writeEndDocument();
+ String s2 = os.toString();
+ assertTrue(s2.contains("<group>b</group><name>a</name>"));
+ }
+
+ @Test
+ public void testWriteDocumentToWriter() throws Exception {
+ TagVO tag = createTag("a", "b");
+
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+ new JAXBElementProvider().writeTo(tag, (Class)TagVO.class,
TagVO.class,
+ TagVO.class.getAnnotations(),
MediaType.APPLICATION_XML_TYPE,
+ new MetadataMap<String, Object>(), os);
+ Document doc = DOMUtils.readXml(new StringReader(os.toString()));
+
+
+ ByteArrayOutputStream os2 = new ByteArrayOutputStream();
+
+ new JSONProvider().writeTo(
+ doc, Document.class, Document.class,
+ new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
+ new MetadataMap<String, Object>(), os2);
+ String s = os2.toString();
+ assertEquals("{\"tagVO\":{\"group\":\"b\",\"name\":\"a\"}}", s);
+ }
+
@Test
public void testWriteBookWithStringConverter() throws Exception {
JSONProvider p = new JSONProvider();
Modified:
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java
URL:
http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java?rev=1177229&r1=1177228&r2=1177229&view=diff
==============================================================================
---
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java
(original)
+++
cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/SourceProviderTest.java
Thu Sep 29 09:00:23 2011
@@ -22,6 +22,8 @@ package org.apache.cxf.jaxrs.provider;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.io.StringReader;
+import java.lang.annotation.Annotation;
import java.util.Collections;
import javax.ws.rs.core.MediaType;
@@ -38,6 +40,7 @@ import javax.xml.transform.stream.Stream
import org.w3c.dom.Document;
+import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.jaxrs.ext.MessageContext;
import org.apache.cxf.jaxrs.ext.MessageContextImpl;
import org.apache.cxf.jaxrs.impl.MetadataMap;
@@ -105,6 +108,22 @@ public class SourceProviderTest extends
}
@Test
+ public void testWriteToDocument() throws Exception {
+ SourceProvider p = new SourceProvider();
+
+ Document doc = DOMUtils.readXml(new StringReader("<test/>"));
+
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+ p.writeTo(doc, Document.class, Document.class,
+ new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
+ new MetadataMap<String, Object>(), os);
+ String s = os.toString();
+ assertEquals("<test/>", s);
+
+ }
+
+ @Test
public void testReadFromWithPreferredFormat() throws Exception {
TestSourceProvider p = new TestSourceProvider();
p.getMessage().put("source-preferred-format", "sax");