cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r599702 - in /incubator/cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ systests/src/test/java/org/apache/cxf/systest/jaxrs/ systests/src/test/java/org...
Date Fri, 30 Nov 2007 06:20:16 GMT
Author: jliu
Date: Thu Nov 29 22:20:15 2007
New Revision: 599702

URL: http://svn.apache.org/viewvc?rev=599702&view=rev
Log:
CXF-1240. support using String as return type from JAX-RS.

Added:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java
  (with props)
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
  (with props)
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123_returnstring.txt
  (with props)
Modified:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=599702&r1=599701&r2=599702&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Thu Nov 29 22:20:15 2007
@@ -88,6 +88,7 @@
             //TODO: support Calendar type
             //}
         } catch (JAXBException e) {
+            //TODO: better exception handling
             e.printStackTrace();
         }
     }

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=599702&r1=599701&r2=599702&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
Thu Nov 29 22:20:15 2007
@@ -42,6 +42,7 @@
         //TODO: search for EntityProviders from classpath or config file.
         entityProviders.add(new JAXBElementProvider());
         entityProviders.add(new JSONProvider());
+        entityProviders.add(new StringProvider());
 
         sort();
     }
@@ -101,11 +102,25 @@
         return null;
     }
     
+    public boolean registerEntityProvider(EntityProvider e) {
+        entityProviders.add(e);
+        sort();
+        return true;
+    }
+    
+    public boolean deregisterEntityProvider(EntityProvider e) {
+        return entityProviders.remove(e);
+    }
+    
+    public List<EntityProvider> getEntityProviders() {
+        return entityProviders;
+    }
+    
     private boolean matchMineTypes(String[] supportedMimeTypes, String[] requestedMimeTypes)
{
         //TODO:
         for (String supportedMimeType : supportedMimeTypes) {
             for (String requestedMimeType : requestedMimeTypes) {
-                if (supportedMimeType.equals(requestedMimeType)) {
+                if (isMineTypeSupported(requestedMimeType, supportedMimeType)) {
                     return true;
                 }
             }
@@ -114,11 +129,26 @@
         return false;
     }
     
+    private boolean isMineTypeSupported(String requestedMimeType, String supportedMimeType)
{
+        // REVISIT: better algorithm
+        if (supportedMimeType.equals(requestedMimeType)) {
+            return true;
+        } else if (supportedMimeType.startsWith("*/")) {
+            return true;
+        } else if (supportedMimeType.regionMatches(0, requestedMimeType, 0, supportedMimeType.indexOf("/"))
+                   && supportedMimeType.endsWith("/*")) {
+            return true;
+        } 
+
+        return false;
+    }
+    
     /*
-     * sorts the available providers according to the media types they declare support for.

-     * Sorting of media types follows the general rule: x/y < * x < *, i.e. a provider
that 
-     * explicitly lists a media types is sorted before a provider that lists *. 
-     * Quality parameter values are also used such that x/y;q=1.0 < x/y;q=0.7.
+     * sorts the available providers according to the media types they declare
+     * support for. Sorting of media types follows the general rule: x/y < * x < *,
+     * i.e. a provider that explicitly lists a media types is sorted before a
+     * provider that lists *. Quality parameter values are also used such that
+     * x/y;q=1.0 < x/y;q=0.7.
      */    
     private void sort() {
         Collections.sort(entityProviders, new EntityProviderComparator());

Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java?rev=599702&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java
(added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java
Thu Nov 29 22:20:15 2007
@@ -0,0 +1,56 @@
+/**
+ * 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.jaxrs.provider;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.EntityProvider;
+
+import org.apache.cxf.helpers.IOUtils;
+
+public final class StringProvider implements EntityProvider<String>  {
+
+    public boolean supports(Class<?> type) {
+        return type == String.class;
+    }
+
+    public String readFrom(Class<String> type, MediaType m, MultivaluedMap<String,
String> headers,
+                           InputStream is) {
+        try {
+            return IOUtils.toString(is);
+        } catch (IOException e) {
+            // TODO: better exception handling
+        }
+        return null;
+    }
+
+    public void writeTo(String obj, MediaType m, MultivaluedMap<String, Object> headers,
OutputStream os) {
+        try {
+            os.write(obj.getBytes());
+        } catch (IOException e) {
+            //TODO: better exception handling
+        }
+    }
+
+}

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 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=599702&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
(added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
Thu Nov 29 22:20:15 2007
@@ -0,0 +1,122 @@
+/**
+ * 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.jaxrs.provider;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.List;
+
+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.ws.rs.ext.ProviderFactory;
+
+import org.apache.cxf.helpers.IOUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ProviderFactoryImplTest extends Assert {
+
+    @Before
+    public void setUp() throws Exception {
+
+    }
+    
+    @Test
+    public void testSortEntityProviders() throws Exception {
+        ProviderFactoryImpl pf = new ProviderFactoryImpl();
+        pf.registerEntityProvider(new TestStringProvider());
+        
+        List<EntityProvider> providers = pf.getEntityProviders();
+
+        assertTrue(indexOf(providers, TestStringProvider.class) < indexOf(providers, StringProvider.class));
+        //REVISIT the compare algorithm
+        //assertTrue(indexOf(providers, JSONProvider.class) < indexOf(providers, TestStringProvider.class));
+    }
+    
+    @Test
+    public void testGetStringProvider() throws Exception {
+        String[] methodMimeTypes = {"text/html"};
+        EntityProvider provider = ((ProviderFactoryImpl)ProviderFactory.getInstance())
+        .createEntityProvider(String.class, methodMimeTypes, false);
+        assertTrue(provider instanceof StringProvider);
+    }
+    
+    @Test
+    public void testGetStringProviderWildCard() throws Exception {
+        String[] methodMimeTypes = {"text/*"};
+        EntityProvider provider = ((ProviderFactoryImpl)ProviderFactory.getInstance())
+        .createEntityProvider(String.class, methodMimeTypes, false);
+        assertTrue(provider instanceof StringProvider);
+    }
+    
+    @Test
+    public void testGetStringProviderUsingProviderDeclaration() throws Exception {
+        String[] methodMimeTypes = {"text/html"};
+        ProviderFactoryImpl pf = (ProviderFactoryImpl)ProviderFactory.getInstance();
+        pf.registerEntityProvider(new TestStringProvider());
+        EntityProvider provider = ((ProviderFactoryImpl)ProviderFactory.getInstance())
+        .createEntityProvider(String.class, methodMimeTypes, false);
+        assertTrue(provider instanceof TestStringProvider);
+    }
+    
+    private int indexOf(List<EntityProvider> providers, Class providerType) {
+        int index = 0;
+        for (EntityProvider p : providers) {
+            if (p.getClass().isAssignableFrom(providerType)) {
+                break;
+            }
+            index++;
+        }
+        return index;
+    }
+    
+    @ConsumeMime("text/html")
+    @ProduceMime("text/html")
+    private final class TestStringProvider implements EntityProvider<String>  {
+
+        public boolean supports(Class<?> type) {
+            return type == String.class;
+        }
+
+        public String readFrom(Class<String> type, MediaType m, MultivaluedMap<String,
String> headers,
+                               InputStream is) {
+            try {
+                return IOUtils.toString(is);
+            } catch (IOException e) {
+                // TODO: better exception handling
+            }
+            return null;
+        }
+
+        public void writeTo(String obj, MediaType m, MultivaluedMap<String, Object>
headers, 
+                            OutputStream os) {
+            try {
+                os.write(obj.getBytes());
+            } catch (IOException e) {
+                // TODO: better exception handling
+            }
+        }
+
+    }
+}

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImplTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=599702&r1=599701&r2=599702&view=diff
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Thu Nov 29 22:20:15 2007
@@ -70,11 +70,12 @@
     
     @HttpMethod("GET")
     @UriTemplate("/booknames/{bookId}/")
-    public Book getBookName(@UriParam("bookId") int id) throws BookNotFoundFault {
+    @ProduceMime("text/plain")
+    public String getBookName(@UriParam("bookId") int id) throws BookNotFoundFault {
         System.out.println("----invoking getBookName with id: " + id);
         Book book = books.get(new Long(id));
         if (book != null) {
-            return book;
+            return book.getName();
         } else {
             BookNotFoundDetails details = new BookNotFoundDetails();
             details.setId(id);

Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=599702&r1=599701&r2=599702&view=diff
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Thu Nov 29 22:20:15 2007
@@ -57,7 +57,7 @@
     }
     
     @Test
-    public void testGetBook123Int() throws Exception {
+    public void testGetBook123ReturnString() throws Exception {
         String endpointAddress =
             "http://localhost:9080/bookstore/booknames/123"; 
         URL url = new URL(endpointAddress);
@@ -65,7 +65,7 @@
         assertNotNull(in);           
 
         InputStream expected = getClass()
-            .getResourceAsStream("resources/expected_get_book123.txt");
+            .getResourceAsStream("resources/expected_get_book123_returnstring.txt");
 
         assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in)); 
     }

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123_returnstring.txt
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123_returnstring.txt?rev=599702&view=auto
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123_returnstring.txt
(added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/expected_get_book123_returnstring.txt
Thu Nov 29 22:20:15 2007
@@ -0,0 +1 @@
+CXF in Action
\ No newline at end of file

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

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



Mime
View raw message