cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r793570 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/ rt/frontend/jaxrs/src/main...
Date Mon, 13 Jul 2009 12:47:11 GMT
Author: sergeyb
Date: Mon Jul 13 12:47:10 2009
New Revision: 793570

URL: http://svn.apache.org/viewvc?rev=793570&view=rev
Log:
JAX-RS : support for explicitly provided CachingEventStreamWriters

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java?rev=793570&r1=793569&r2=793570&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
Mon Jul 13 12:47:10 2009
@@ -205,6 +205,10 @@
         
         if (status >= 400) {
             
+            if (m.getReturnType() == Response.class && m.getExceptionTypes().length
== 0) {
+                return;
+            }
+            
             ResponseExceptionMapper<?> mapper = findExceptionMapper(m, message);
             if (mapper != null) {
                 Throwable t = mapper.fromResponse(r);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java?rev=793570&r1=793569&r2=793570&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalMessageContext.java
Mon Jul 13 12:47:10 2009
@@ -81,8 +81,9 @@
     public void put(Object key, Object value) {
         if (get() != null) {
             get().put(key, value);
+        } else {
+            throw new IllegalStateException("MessageContext is not set");
         }
-        throw new IllegalStateException("MessageContext is not set");
     }
 
     public <T, E> T getResolver(Class<T> resolverClass, Class<E> resolveClazz)
{

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java?rev=793570&r1=793569&r2=793570&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
Mon Jul 13 12:47:10 2009
@@ -257,10 +257,12 @@
     }
     
     private void checkCachedStream(Message m, OutputStream osOriginal, boolean enabled) throws
Exception {
-        if (!enabled) {
-            return;
+        XMLStreamWriter writer = null;
+        if (enabled) {
+            writer = m.getContent(XMLStreamWriter.class);
+        } else {
+            writer = (XMLStreamWriter)m.get(XMLStreamWriter.class.getName());
         }
-        XMLStreamWriter writer = m.getContent(XMLStreamWriter.class);
         if (writer instanceof CachingXmlEventWriter) {
             CachingXmlEventWriter cache = (CachingXmlEventWriter)writer;
             if (cache.getEvents().size() != 0) {
@@ -272,11 +274,13 @@
             m.setContent(XMLStreamWriter.class, null);
             return;
         }
-        OutputStream os = m.getContent(OutputStream.class);
-        if (os != osOriginal && os instanceof CachedOutputStream) {
-            CachedOutputStream cos = (CachedOutputStream)os;
-            if (cos.size() != 0) {
-                cos.writeCacheTo(osOriginal);
+        if (enabled) {
+            OutputStream os = m.getContent(OutputStream.class);
+            if (os != osOriginal && os instanceof CachedOutputStream) {
+                CachedOutputStream cos = (CachedOutputStream)os;
+                if (cos.size() != 0) {
+                    cos.writeCacheTo(osOriginal);
+                }
             }
         }
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=793570&r1=793569&r2=793570&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Mon Jul 13 12:47:10 2009
@@ -141,16 +141,18 @@
 
     protected Object doUnmarshal(Unmarshaller unmarshaller, InputStream is, MediaType mt)

         throws JAXBException {
-        MessageContext mc = getContext();
-        if (mc != null) {
-            XMLStreamReader reader = getContext().getContent(XMLStreamReader.class);
-            if (reader != null) {
-                return unmarshalFromReader(unmarshaller, reader, mt);
-            }
+        XMLStreamReader reader = getStreamReader(is, mt);
+        if (reader != null) {
+            return unmarshalFromReader(unmarshaller, reader, mt);
         }
         return unmarshalFromInputStream(unmarshaller, is, mt);
     }
     
+    protected XMLStreamReader getStreamReader(InputStream is, MediaType mt) {
+        MessageContext mc = getContext();
+        return mc != null ? mc.getContent(XMLStreamReader.class) : null;
+    }
+    
     protected Object unmarshalFromInputStream(Unmarshaller unmarshaller, InputStream is,
MediaType mt) 
         throws JAXBException {
         return unmarshaller.unmarshal(is);
@@ -190,14 +192,9 @@
             ms.setProperty(entry.getKey(), entry.getValue());
         }
         
-        XMLStreamWriter writer = null;
         MessageContext mc = getContext();
         if (mc != null) {
-            writer = mc.getContent(XMLStreamWriter.class);
-            if (writer == null && enableStreaming) {
-                writer = StaxUtils.createXMLStreamWriter(os);
-            }
-            // check Marshaller properties here as well which might've been set earlier on,
+            // check Marshaller properties which might've been set earlier on,
             // they'll overwrite statically configured ones
             for (String key : MARSHALLER_PROPERTIES) {
                 Object value = mc.get(key);
@@ -207,13 +204,29 @@
             }
             
         }
+        XMLStreamWriter writer = getStreamWriter(obj, os, mt);
         if (writer != null) {
+            if (mc != null) {
+                mc.put(XMLStreamWriter.class.getName(), writer);
+            }
             marshalToWriter(ms, obj, writer, mt);
         } else {
             marshalToOutputStream(ms, obj, os, mt);
         }
     }
     
+    protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os, MediaType mt)
{
+        XMLStreamWriter writer = null;
+        MessageContext mc = getContext();
+        if (mc != null) {
+            writer = mc.getContent(XMLStreamWriter.class);
+            if (writer == null && enableStreaming) {
+                writer = StaxUtils.createXMLStreamWriter(os);
+            }
+        }
+        return writer;
+    }
+    
     protected void marshalToOutputStream(Marshaller ms, Object obj, OutputStream os, MediaType
mt) 
         throws Exception {
         ms.marshal(obj, os);

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java?rev=793570&r1=793569&r2=793570&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
Mon Jul 13 12:47:10 2009
@@ -79,6 +79,29 @@
         assertEquals("123", values.getFirst("id"));
         assertEquals("3", values.getFirst("order"));
     }
+    
+    @Test
+    public void testMatchWithMultipleMatrixParams() throws Exception {
+        URITemplate uriTemplate = 
+            new URITemplate("renderwidget/id/{id}/type/{type}/size/{size}/locale/{locale}/{properties}");
+        MultivaluedMap<String, String> values = new MetadataMap<String, String>();
+
+        assertTrue(uriTemplate.match("renderwidget/id/1007/type/1/size/1/locale/en_US/properties;a=b",

+                                     values));
+        assertEquals("1007", values.getFirst("id"));
+    }
+    
+    @Test
+    public void testMatchWithMultipleMatrixParams2() throws Exception {
+        URITemplate uriTemplate = 
+            new URITemplate("renderwidget/id/{id}/type/{type}/size/{size}/locale/{locale}/{properties}");
+        MultivaluedMap<String, String> values = new MetadataMap<String, String>();
+
+        assertTrue(uriTemplate.match(
+                   "renderwidget/id/1007/type/1/size/1/locale/en_US/properties;numResults=1;foo=bar",

+                    values));
+        assertEquals("1007", values.getFirst("id"));
+    }
 
     @Test
     public void testMatchWithMatrixOnClearPath3() throws Exception {

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java?rev=793570&r1=793569&r2=793570&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
Mon Jul 13 12:47:10 2009
@@ -18,8 +18,6 @@
  */
 package org.apache.cxf.jaxrs.model.wadl;
 
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.List;
@@ -84,14 +82,14 @@
         assertNotNull(r);
         assertEquals(WadlGenerator.WADL_TYPE.toString(),
                      r.getMetadata().getFirst(HttpHeaders.CONTENT_TYPE));
-        File f = new File("test.xml");
-        f.delete();
-        f.createNewFile();
-        System.out.println(f.getAbsolutePath());
-        FileOutputStream fos = new FileOutputStream(f);
-        fos.write(r.getEntity().toString().getBytes());
-        fos.flush();
-        fos.close();
+//        File f = new File("test.xml");
+//        f.delete();
+//        f.createNewFile();
+//        System.out.println(f.getAbsolutePath());
+//        FileOutputStream fos = new FileOutputStream(f);
+//        fos.write(r.getEntity().toString().getBytes());
+//        fos.flush();
+//        fos.close();
     }
     
     @Test

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=793570&r1=793569&r2=793570&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Mon Jul 13
12:47:10 2009
@@ -244,6 +244,13 @@
     }
     
     @GET
+    @Path("/books/{bookId}/")
+    @Produces("text/xml")
+    public Book getBookTextXml(@PathParam("bookId") String id) throws BookNotFoundFault {
+        return doGetBook(id);
+    }
+    
+    @GET
     @Path("/books/wrapper/{bookId}/")
     @Produces("application/xml")
     public BookWrapper getWrappedBook(@PathParam("bookId") Long id) throws BookNotFoundFault
{

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java?rev=793570&r1=793569&r2=793570&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerStreamingTest.java
Mon Jul 13 12:47:10 2009
@@ -20,17 +20,24 @@
 package org.apache.cxf.systest.jaxrs;
 
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.core.MediaType;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Unmarshaller;
+import javax.xml.stream.XMLStreamWriter;
 
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
+import org.apache.cxf.staxutils.CachingXmlEventWriter;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
 
@@ -48,10 +55,17 @@
             sf.setResourceClasses(BookStore.class);
             sf.setResourceProvider(BookStore.class,
                                    new SingletonResourceProvider(new BookStore()));
-            JAXBElementProvider p = new JAXBElementProvider();
-            p.setEnableBuffering(true);
-            p.setEnableStreaming(true);
-            sf.setProvider(p);
+            JAXBElementProvider p1 = new JAXBElementProvider();
+            p1.setEnableBuffering(true);
+            p1.setEnableStreaming(true);
+            
+            JAXBElementProvider p2 = new CustomJaxbProvider();
+            p2.setProduceMediaTypes(Collections.singletonList("text/xml"));
+            
+            List<Object> providers = new ArrayList<Object>();
+            providers.add(p1);
+            providers.add(p2);
+            sf.setProviders(providers);
             sf.setAddress("http://localhost:9080/");
             Map<String, Object> properties = new HashMap<String, Object>();
             properties.put("org.apache.cxf.serviceloader-context", "true");
@@ -85,6 +99,12 @@
                       "application/xml", 200);
     }
     
+    @Test
+    public void testGetBookUsingStaxWriter() throws Exception {
+        getAndCompare("http://localhost:9080/bookstore/books/123",
+                      "text/xml", 200);
+    }
+    
     private void getAndCompare(String address, 
                                String acceptType,
                                int expectedStatus) throws Exception {
@@ -107,4 +127,16 @@
         Unmarshaller u = c.createUnmarshaller();
         return (Book)u.unmarshal(is);
     }
+    
+    @Ignore
+    public static class CustomJaxbProvider extends JAXBElementProvider {
+        @Override
+        protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os, MediaType
mt) {
+            if (mt.equals(MediaType.TEXT_XML_TYPE)) {
+                return new CachingXmlEventWriter();
+            } else {
+                throw new RuntimeException();
+            }
+        }
+    }
 }



Mime
View raw message