cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r627590 - in /incubator/cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/main/j...
Date Wed, 13 Feb 2008 20:42:16 GMT
Author: dkulp
Date: Wed Feb 13 12:42:13 2008
New Revision: 627590

URL: http://svn.apache.org/viewvc?rev=627590&view=rev
Log:
[CXF-1425] Enhancement to JAX-RS spring configuration for registering providers
Patch from Barry Fitzgerald applied

Added:
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BadgerFishProvider.java
  (with props)
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookServerResourceCreatedSpringProviders.java
  (with props)
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java
  (with props)
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123badgerfish.txt
  (with props)
    incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring_providers/
    incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring_providers/WEB-INF/
    incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml
      - copied, changed from r627575, incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring/WEB-INF/beans.xml
    incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring_providers/WEB-INF/web.xml
      - copied unchanged from r627575, incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring/WEB-INF/web.xml
Modified:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java?rev=627590&r1=627589&r2=627590&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
Wed Feb 13 12:42:13 2008
@@ -23,6 +23,9 @@
 import java.util.Arrays;
 import java.util.List;
 
+import javax.ws.rs.ext.EntityProvider;
+import javax.ws.rs.ext.ProviderFactory;
+
 import org.apache.cxf.BusException;
 import org.apache.cxf.binding.BindingConfiguration;
 import org.apache.cxf.binding.BindingFactory;
@@ -35,6 +38,7 @@
 import org.apache.cxf.endpoint.ServerImpl;
 import org.apache.cxf.feature.AbstractFeature;
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
+import org.apache.cxf.jaxrs.provider.ProviderFactoryImpl;
 import org.apache.cxf.service.Service;
 import org.apache.cxf.service.factory.ServiceConstructionException;
 import org.apache.cxf.service.invoker.Invoker;
@@ -62,6 +66,7 @@
     private boolean start = true;
     private JAXRSServiceFactoryBean serviceFactory;
     private List<Object> serviceBeans;
+    private List<EntityProvider> entityProviders;
 
     public JAXRSServerFactoryBean() {
         this(new JAXRSServiceFactoryBean());
@@ -73,6 +78,7 @@
         setBindingId(JAXRSBindingFactory.JAXRS_BINDING_ID);
     }
     
+    @SuppressWarnings("unchecked")
     public Server create() {
         try {
             Endpoint ep = createEndpoint();
@@ -80,12 +86,16 @@
                                     ep, 
                                     getDestinationFactory(), 
                                     getBindingFactory());
-            
+
             if (invoker == null) {
                 ep.getService().setInvoker(createInvoker());
             } else {
                 ep.getService().setInvoker(invoker);
             }
+            if (entityProviders != null) {
+                ProviderFactoryImpl providerFactoryImpl = (ProviderFactoryImpl)ProviderFactory.getInstance();
+                providerFactoryImpl.setUserEntityProviders(entityProviders); 
+            }
             
             if (start) {
                 server.start();
@@ -97,7 +107,7 @@
         } catch (IOException e) {
             throw new ServiceConstructionException(e);
         }
-        
+
         applyFeatures();
         return server;
     }
@@ -257,5 +267,19 @@
     
     public void setResourceProvider(Class c, ResourceProvider rp) {
         serviceFactory.setResourceProvider(c, rp);
+    }
+
+    /**
+     * @return the entityProviders
+     */
+    public List<EntityProvider> getEntityProviders() {
+        return entityProviders;
+    }
+
+    /**
+     * @param entityProviders the entityProviders to set
+     */
+    public void setEntityProviders(List<EntityProvider> entityProviders) {
+        this.entityProviders = entityProviders;
     }
 }

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java?rev=627590&r1=627589&r2=627590&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
Wed Feb 13 12:42:13 2008
@@ -101,7 +101,7 @@
          * class method override any on the resource class; declarations on an
          * EntityProvider for a method argument or return type override those on
          * a resource class or resource method. In the absence of either of
-         * these annotations, support for any media type (¡°*¡±) is assumed.
+         * these annotations, support for any media type (* / *) is assumed.
          */   
         
         String[] mimeTypes = {"*/*"};

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java?rev=627590&r1=627589&r2=627590&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
Wed Feb 13 12:42:13 2008
@@ -37,20 +37,19 @@
 
 //NOTE: ProviderFactory should provide a method that can pass in media types
 public class ProviderFactoryImpl extends ProviderFactory {
-    protected List<EntityProvider> entityProviders = new ArrayList<EntityProvider>();
+    protected List<EntityProvider> defaultEntityProviders = new ArrayList<EntityProvider>();
+    protected List<EntityProvider> userEntityProviders = new ArrayList<EntityProvider>();
     protected List<HeaderProvider> headerProviders = new ArrayList<HeaderProvider>();
   
 
     public ProviderFactoryImpl() {
         //TODO: search for EntityProviders from classpath or config file.
-        //or dynamically instantiate them based on mime types being active 
-        entityProviders.add(new JAXBElementProvider());
-        entityProviders.add(new JSONProvider());
-        entityProviders.add(new StringProvider());
-        entityProviders.add(new DOMSourceProvider());
-        entityProviders.add(new AtomFeedProvider());
-        entityProviders.add(new AtomEntryProvider());
-
-        sort();
+        defaultEntityProviders.add(new JAXBElementProvider());
+        defaultEntityProviders.add(new JSONProvider());
+        defaultEntityProviders.add(new StringProvider());
+        defaultEntityProviders.add(new DOMSourceProvider());
+        defaultEntityProviders.add(new AtomFeedProvider());
+        defaultEntityProviders.add(new AtomEntryProvider());
+        sort(defaultEntityProviders);
     }
     
     public <T> T createInstance(Class<T> type) {
@@ -62,42 +61,32 @@
    
     @SuppressWarnings("unchecked")
     public <T> EntityProvider<T> createEntityProvider(Class<T> type) {
-        for (EntityProvider<T> ep : entityProviders) {
-            if (ep.supports(type)) {
-                return ep;
-            }
+        
+        //Try user provided providers
+        EntityProvider<T> ep = chooseEntityProvider(userEntityProviders, type);
+        
+        //If none found try the default ones
+        if (ep == null) {
+            ep = chooseEntityProvider(defaultEntityProviders, type);
         }     
         
-        return null;
+        return ep;
     }
 
     @SuppressWarnings("unchecked")
     public <T> EntityProvider<T> createEntityProvider(Class<T> type, String[]
requestedMimeTypes,
                                                       boolean isConsumeMime) {
 
-        for (EntityProvider<T> ep : entityProviders) {
-            String[] supportedMimeTypes = {"*/*"};            
-            if (isConsumeMime) {
-                ConsumeMime c = ep.getClass().getAnnotation(ConsumeMime.class);
-                if (c != null) {
-                    supportedMimeTypes = c.value();               
-                }           
-            } else {
-                ProduceMime c = ep.getClass().getAnnotation(ProduceMime.class);
-                if (c != null) {
-                    supportedMimeTypes = c.value();               
-                }                  
-            }
-            
-            String[] availableMimeTypes = JAXRSUtils.intersectMimeTypes(requestedMimeTypes,
-                                                                        supportedMimeTypes);
-
-            if (availableMimeTypes.length != 0 && ep.supports(type)) {
-                return ep;
-            }
+      //Try user defined providers
+        EntityProvider<T> ep = chooseEntityProvider(userEntityProviders, 
+                                                    type, requestedMimeTypes, isConsumeMime);
+        
+        //If none found try the default ones
+        if (ep == null) {
+            ep = chooseEntityProvider(defaultEntityProviders, type, requestedMimeTypes, isConsumeMime);
         }     
         
-        return null;
+        return ep;
     }
     
     @SuppressWarnings("unchecked")
@@ -111,18 +100,14 @@
         return null;
     }
     
-    public boolean registerEntityProvider(EntityProvider e) {
-        entityProviders.add(e);
-        sort();
+    public boolean registerUserEntityProvider(EntityProvider e) {
+        userEntityProviders.add(e);
+        sort(userEntityProviders);
         return true;
     }
     
-    public boolean deregisterEntityProvider(EntityProvider e) {
-        return entityProviders.remove(e);
-    }
-    
-    public List<EntityProvider> getEntityProviders() {
-        return entityProviders;
+    public boolean deregisterUserEntityProvider(EntityProvider e) {
+        return userEntityProviders.remove(e);
     }
    
     /*
@@ -132,7 +117,7 @@
      * provider that lists *. Quality parameter values are also used such that
      * x/y;q=1.0 < x/y;q=0.7.
      */    
-    private void sort() {
+    private void sort(List<EntityProvider> entityProviders) {
         Collections.sort(entityProviders, new EntityProviderComparator());
     }
     
@@ -164,6 +149,83 @@
             
             return str1.compareTo(str2);
         }
+    }
+
+    /**
+     * Choose the first Entity provider that matches the requestedMimeTypes 
+     * for a sorted list of Entity providers
+     * Returns null if none is found.
+     * @param <T>
+     * @param entityProviders
+     * @param type
+     * @param requestedMimeTypes
+     * @param isConsumeMime
+     * @return
+     */
+    private EntityProvider chooseEntityProvider(List<EntityProvider> entityProviders,
Class<?> type, 
+                                                 String[] requestedMimeTypes, boolean isConsumeMime)
{
+        for (EntityProvider<?> ep : entityProviders) {
+            String[] supportedMimeTypes = {"*/*"};            
+            if (isConsumeMime) {
+                ConsumeMime c = ep.getClass().getAnnotation(ConsumeMime.class);
+                if (c != null) {
+                    supportedMimeTypes = c.value();               
+                }           
+            } else {
+                ProduceMime c = ep.getClass().getAnnotation(ProduceMime.class);
+                if (c != null) {
+                    supportedMimeTypes = c.value();               
+                }                  
+            }
+            
+            String[] availableMimeTypes = JAXRSUtils.intersectMimeTypes(requestedMimeTypes,
+                                                                        supportedMimeTypes);
+
+            if (availableMimeTypes.length != 0 && ep.supports(type)) {
+                return ep;
+            }
+        }     
+        
+        return null;
+        
+    }
+    
+    /**
+     * Choose the first Entity provider that matches the type for a sorted list of Entity
providers
+     * Returns null if none is found.
+     * @param <T>
+     * @param entityProviders
+     * @param type
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    private <T> EntityProvider<T> chooseEntityProvider(List<EntityProvider>
entityProviders, Class<T> type) {
+        
+        for (EntityProvider<T> ep : entityProviders) {
+            if (ep.supports(type)) {
+                return ep;
+            }
+        }           
+        return null;
+    }
+    
+    
+    public List<EntityProvider> getDefaultEntityProviders() {
+        return defaultEntityProviders;
+    }
+
+
+    public List<EntityProvider> getUserEntityProviders() {
+        return userEntityProviders;
+    }
+
+    /**
+     * Use for injection of entityProviders
+     * @param entityProviders the entityProviders to set
+     */
+    public void setUserEntityProviders(List<EntityProvider> userEntityProviders) {
+        this.userEntityProviders = userEntityProviders;
+        sort(this.userEntityProviders);
     }
 
 }

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java?rev=627590&r1=627589&r2=627590&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
Wed Feb 13 12:42:13 2008
@@ -65,9 +65,11 @@
         } else if ("binding".equals(name)) {
             setFirstChildAsProperty(el, ctx, bean, "bindingConfig");
         } else if ("inInterceptors".equals(name) || "inFaultInterceptors".equals(name)
-            || "outInterceptors".equals(name) || "outFaultInterceptors".equals(name)
-            || "features".equals(name) || "schemaLocations".equals(name) 
-            || "serviceBeans".equals(name)) {
+            || "outInterceptors".equals(name) || "outFaultInterceptors".equals(name)) {
+            List list = ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
+            bean.addPropertyValue(name, list);
+        } else if ("features".equals(name) || "schemaLocations".equals(name) 
+            || "entityProviders".equals(name) || "serviceBeans".equals(name)) {
             List list = ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
             bean.addPropertyValue(name, list);
         } else {

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd?rev=627590&r1=627589&r2=627590&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd (original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd Wed Feb 13
12:42:13 2008
@@ -45,6 +45,7 @@
             <xsd:element name="properties" type="beans:mapType" minOccurs="0"/>
             <xsd:element name="serviceBeans" type="xsd:anyType" minOccurs="0"/>
             <xsd:element name="serviceFactory" type="xsd:anyType" minOccurs="0"/>
+            <xsd:element name="entityProviders" type="xsd:anyType" minOccurs="0"/>
           </xsd:all>
           <xsd:attributeGroup ref="cxf-beans:beanAttributes"/>
           <xsd:attribute name="address" type="xsd:string" />

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java?rev=627590&r1=627589&r2=627590&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
Wed Feb 13 12:42:13 2008
@@ -29,6 +29,7 @@
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.EntityProvider;
 import javax.ws.rs.ext.ProviderFactory;
+import javax.xml.bind.annotation.XmlRootElement;
 
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
@@ -47,9 +48,10 @@
     @Test
     public void testSortEntityProviders() throws Exception {
         ProviderFactoryImpl pf = new ProviderFactoryImpl();
-        pf.registerEntityProvider(new TestStringProvider());
+        pf.registerUserEntityProvider(new TestStringProvider());
+        pf.registerUserEntityProvider(new StringProvider());
         
-        List<EntityProvider> providers = pf.getEntityProviders();
+        List<EntityProvider> providers = pf.getUserEntityProviders();
 
         assertTrue(indexOf(providers, TestStringProvider.class) < indexOf(providers, StringProvider.class));
         //REVISIT the compare algorithm
@@ -96,7 +98,7 @@
     public void testGetStringProviderUsingProviderDeclaration() throws Exception {
         String[] methodMimeTypes = {"text/html"};
         ProviderFactoryImpl pf = (ProviderFactoryImpl)ProviderFactory.getInstance();
-        pf.registerEntityProvider(new TestStringProvider());
+        pf.registerUserEntityProvider(new TestStringProvider());
         EntityProvider provider = ((ProviderFactoryImpl)ProviderFactory.getInstance())
         .createEntityProvider(String.class, methodMimeTypes, false);
         assertTrue(provider instanceof TestStringProvider);
@@ -110,6 +112,31 @@
         assertTrue(provider instanceof JSONProvider);
     }
     
+    @Test
+    public void testRegisterCustomJSONEntityProvider() throws Exception {
+        ProviderFactoryImpl pf = (ProviderFactoryImpl)ProviderFactory.getInstance();
+        pf.registerUserEntityProvider(new CustomJSONProvider());
+        
+        String[] methodMimeTypes = {"application/json"};
+        EntityProvider provider = pf.createEntityProvider(org.apache.cxf.jaxrs.resources.Book.class,

+                                                          methodMimeTypes, 
+                                                          true);
+        assertTrue("User Registered provider was not returned first", provider instanceof
CustomJSONProvider);
+    }
+    
+    @Test
+    public void testRegisterCustomEntityProvider() throws Exception {
+        ProviderFactoryImpl pf = (ProviderFactoryImpl)ProviderFactory.getInstance();
+        pf.registerUserEntityProvider(new CustomWidgetProvider());
+        
+        String[] methodMimeTypes = {"application/widget"};
+        EntityProvider provider = pf.createEntityProvider(org.apache.cxf.jaxrs.resources.Book.class,

+                                                          methodMimeTypes, 
+                                                          true);
+        assertTrue("User Registered provider was not returned first", 
+                   provider instanceof CustomWidgetProvider);
+    }
+    
     private int indexOf(List<EntityProvider> providers, Class providerType) {
         int index = 0;
         for (EntityProvider p : providers) {
@@ -146,6 +173,48 @@
             } catch (IOException e) {
                 // TODO: better exception handling
             }
+        }
+
+    }
+    
+    @ConsumeMime("application/json")
+    @ProduceMime("application/json")
+    private final class CustomJSONProvider implements EntityProvider<String>  {
+
+        public boolean supports(Class<?> type) {
+            return type.getAnnotation(XmlRootElement.class) != null;
+        }
+
+        public String readFrom(Class<String> type, MediaType m, MultivaluedMap<String,
String> headers,
+                               InputStream is) {    
+            //Dummy
+            return null;
+        }
+
+        public void writeTo(String obj, MediaType m, MultivaluedMap<String, Object>
headers, 
+                            OutputStream os) {
+            //Dummy
+        }
+
+    }
+    
+    @ConsumeMime("application/widget")
+    @ProduceMime("application/widget")
+    private final class CustomWidgetProvider implements EntityProvider<String>  {
+
+        public boolean supports(Class<?> type) {
+            return type.getAnnotation(XmlRootElement.class) != null;
+        }
+
+        public String readFrom(Class<String> type, MediaType m, MultivaluedMap<String,
String> headers,
+                               InputStream is) {    
+            //Dummy
+            return null;
+        }
+
+        public void writeTo(String obj, MediaType m, MultivaluedMap<String, Object>
headers, 
+                            OutputStream os) {
+            //Dummy
         }
 
     }

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BadgerFishProvider.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BadgerFishProvider.java?rev=627590&view=auto
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BadgerFishProvider.java
(added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BadgerFishProvider.java
Wed Feb 13 12:42:13 2008
@@ -0,0 +1,106 @@
+/**
+ * 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 java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.ProduceMime;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.EntityProvider;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.codehaus.jettison.badgerfish.BadgerFishXMLInputFactory;
+import org.codehaus.jettison.badgerfish.BadgerFishXMLOutputFactory;
+
+@ProduceMime("application/json")
+@ConsumeMime("application/json")
+public final class BadgerFishProvider implements EntityProvider<Object>  {
+
+    static Map<Class, JAXBContext> jaxbContexts = new WeakHashMap<Class, JAXBContext>();
+
+    public boolean supports(Class<?> type) {
+        return type.getAnnotation(XmlRootElement.class) != null;
+    }
+
+    public Object readFrom(Class<Object> type, MediaType m, MultivaluedMap<String,
String> headers,
+                           InputStream is) {
+        try {
+            JAXBContext context = getJAXBContext(type);
+            Unmarshaller unmarshaller = context.createUnmarshaller();
+               
+            BadgerFishXMLInputFactory factory = new BadgerFishXMLInputFactory();
+            XMLStreamReader xsw = factory.createXMLStreamReader(is);            
+            Object obj = unmarshaller.unmarshal(xsw);
+            xsw.close();
+            return obj;
+        } catch (JAXBException e) {
+            e.printStackTrace();         
+        } catch (XMLStreamException e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
+
+    public void writeTo(Object obj, MediaType m, MultivaluedMap<String, Object> headers,
OutputStream os) {
+        try {
+            JAXBContext context = getJAXBContext(obj.getClass());
+            Marshaller marshaller = context.createMarshaller();
+            //marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
+
+            // Set up the JSON StAX implementation
+            
+            XMLOutputFactory factory = new BadgerFishXMLOutputFactory();
+            XMLStreamWriter xsw = factory.createXMLStreamWriter(os);            
+            marshaller.marshal(obj, xsw);
+            xsw.close();
+            
+        } catch (JAXBException e) {
+            e.printStackTrace();
+        } catch (XMLStreamException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private JAXBContext getJAXBContext(Class type) throws JAXBException {
+        synchronized (jaxbContexts) {
+            JAXBContext context = jaxbContexts.get(type);
+            if (context == null) {
+                context = JAXBContext.newInstance(type);
+                jaxbContexts.put(type, context);
+            }
+            return context;
+        }
+    }
+}

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

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

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookServerResourceCreatedSpringProviders.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookServerResourceCreatedSpringProviders.java?rev=627590&view=auto
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookServerResourceCreatedSpringProviders.java
(added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookServerResourceCreatedSpringProviders.java
Wed Feb 13 12:42:13 2008
@@ -0,0 +1,89 @@
+/**
+ * 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 java.net.URISyntaxException;
+
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.mortbay.jetty.Connector;
+import org.mortbay.jetty.Handler;
+import org.mortbay.jetty.handler.DefaultHandler;
+import org.mortbay.jetty.handler.HandlerCollection;
+import org.mortbay.jetty.nio.SelectChannelConnector;
+import org.mortbay.jetty.webapp.WebAppContext;
+
+
+public class BookServerResourceCreatedSpringProviders extends AbstractBusTestServerBase {
+
+    private org.mortbay.jetty.Server server;
+    
+    protected void run() {
+        System.out.println("Starting Server");
+
+        server = new org.mortbay.jetty.Server();
+
+        SelectChannelConnector connector = new SelectChannelConnector();
+        connector.setPort(9080);
+        server.setConnectors(new Connector[] {connector});
+
+        WebAppContext webappcontext = new WebAppContext();
+        String contextPath = null;
+        try {
+            contextPath = getClass().getResource("/jaxrs_spring_providers").toURI().getPath();
+        } catch (URISyntaxException e1) {
+            e1.printStackTrace();
+        }
+        webappcontext.setContextPath("/");
+
+        webappcontext.setWar(contextPath);
+
+        HandlerCollection handlers = new HandlerCollection();
+        handlers.setHandlers(new Handler[] {webappcontext, new DefaultHandler()});
+
+        server.setHandler(handlers);
+        try {
+            server.start();
+                       
+        } catch (Exception e) {
+            e.printStackTrace();
+        }     
+    }
+
+    public boolean stopInProcess() throws Exception {
+        boolean ret = super.stopInProcess();
+        if (server != null) {
+            server.stop();
+        }
+        return ret;
+    }    
+    
+    public static void main(String args[]) {
+        try {
+            BookServerResourceCreatedSpringProviders s = new BookServerResourceCreatedSpringProviders();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }
+
+}

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

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

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java?rev=627590&view=auto
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java
(added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java
Wed Feb 13 12:42:13 2008
@@ -0,0 +1,64 @@
+/**
+ * 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 java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JAXRSClientServerResourceCreatedSpringProviderTest extends AbstractBusClientServerTestBase
{
+
+    @BeforeClass
+    public static void startServers() throws Exception {
+        assertTrue("server did not launch correctly",
+                   launchServer(BookServerResourceCreatedSpringProviders.class, true));
+    }
+    
+    @Test
+    public void testGetBook123() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:9080/bookstore/books/123"; 
+        URL url = new URL(endpointAddress);
+        URLConnection connect = url.openConnection();
+        connect.addRequestProperty("Accept", "application/json");
+        InputStream in = connect.getInputStream();
+        assertNotNull(in);           
+
+        //Ensure BadgerFish output as this should have replaced the standard JSONProvider
+        InputStream expected = getClass()
+            .getResourceAsStream("resources/expected_get_book123badgerfish.txt");
+
+        assertEquals("BadgerFish output not correct", 
+                     getStringFromInputStream(expected).trim(),
+                     getStringFromInputStream(in).trim());
+    }
+    
+    private String getStringFromInputStream(InputStream in) throws Exception {
+        return IOUtils.toString(in);
+    }
+
+}

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

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

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123badgerfish.txt
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123badgerfish.txt?rev=627590&view=auto
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123badgerfish.txt
(added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123badgerfish.txt
Wed Feb 13 12:42:13 2008
@@ -0,0 +1 @@
+{"Book":{"id":{"$":"123"},"name":{"$":"CXF in Action"}}}

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

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123badgerfish.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml
(from r627575, incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring/WEB-INF/beans.xml)
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml?p2=incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml&p1=incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring/WEB-INF/beans.xml&r1=627575&r2=627590&rev=627590&view=diff
==============================================================================
--- incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring/WEB-INF/beans.xml (original)
+++ incubator/cxf/trunk/systests/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml
Wed Feb 13 12:42:13 2008
@@ -17,13 +17,6 @@
   specific language governing permissions and limitations
   under the License.
 -->
-<!-- START SNIPPET: beans -->
-<!--beans xmlns="http://www.springframework.org/schema/beans"
-  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xmlns:simple="http://cxf.apache.org/simple"
-  xsi:schemaLocation="
-  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
-  http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd"-->
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxrs="http://cxf.apache.org/jaxrs"
@@ -33,21 +26,20 @@
 http://cxf.apache.org/jaxrs
 http://cxf.apache.org/schemas/jaxrs.xsd">
 
-  <import resource="classpath:META-INF/cxf/cxf.xml" />
-  <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
-  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
-  
-  <jaxrs:server id="bookservice"
-		        address="/">
-    <jaxrs:serviceBeans>
-      <ref bean="petstore"/>	
-      <ref bean="bookstore"/>
-    </jaxrs:serviceBeans>		   
-  </jaxrs:server>
-  <bean id="bookstore" scope="prototype" class="org.apache.cxf.systest.jaxrs.BookStore">
-  </bean>
-  <bean id="petstore" scope="prototype" class="org.apache.cxf.systest.jaxrs.PetStore">
-  </bean>
+    <import resource="classpath:META-INF/cxf/cxf.xml" />
+    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
+    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 
+    <jaxrs:server id="bookservice"
+                  address="/">
+        <jaxrs:serviceBeans>
+            <ref bean="petstore"/>
+            <ref bean="bookstore"/>
+        </jaxrs:serviceBeans>
+        <jaxrs:entityProviders>
+            <bean class="org.apache.cxf.systest.jaxrs.BadgerFishProvider"/>
+        </jaxrs:entityProviders>
+    </jaxrs:server>
+    <bean id="bookstore" scope="prototype" class="org.apache.cxf.systest.jaxrs.BookStore"/>
+    <bean id="petstore" scope="prototype" class="org.apache.cxf.systest.jaxrs.PetStore"/>
 </beans>
-<!-- END SNIPPET: beans -->



Mime
View raw message