cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1055895 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/client/ main/java/org/apache/cxf/jaxrs/utils/ test/java/org/apache/cxf/jaxrs/ test/java/org/apache/cxf/jaxrs/utils/
Date Thu, 06 Jan 2011 14:37:16 GMT
Author: sergeyb
Date: Thu Jan  6 14:37:16 2011
New Revision: 1055895

URL: http://svn.apache.org/viewvc?rev=1055895&view=rev
Log:
[JAX-RS] Making sure ParameterHandlers are checked in case of String constructors throwing
exceptions

Added:
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer2.java   (with
props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/CustomerParameterHandler.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=1055895&r1=1055894&r2=1055895&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java Thu
Jan  6 14:37:16 2011
@@ -180,8 +180,7 @@ public class WebClient extends AbstractC
     /**
      * Creates WebClient, baseURI will be set to Client currentURI
      * @param client existing client
-     * @param inheritHeaders  if existing Client headers can be inherited by new proxy 
-     *        and subresource proxies if any 
+     * @param inheritHeaders  if existing Client headers can be inherited by new client 
      */
     public static WebClient fromClient(Client client, boolean inheritHeaders) {
         

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java?rev=1055895&r1=1055894&r2=1055895&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
Thu Jan  6 14:37:16 2011
@@ -318,7 +318,11 @@ public final class InjectionUtils {
             // try valueOf
         } catch (WebApplicationException ex) {
             throw ex;
-        } catch (Exception ex) { 
+        } catch (Exception ex) {
+            Object result = createFromParameterHandler(value, pClass, message);
+            if (result != null) {
+                return result;
+            }
             LOG.severe(new org.apache.cxf.common.i18n.Message("CLASS_CONSTRUCTOR_FAILURE",

                                                                BUNDLE, 
                                                                pClass.getName()).toString());
@@ -337,22 +341,31 @@ public final class InjectionUtils {
             }
         }
         
-        if (result == null && message != null) {
+        if (result == null) {
+            result = createFromParameterHandler(value, pClass, message);
+        }
+        
+        if (result == null) {
+            reportServerError("WRONG_PARAMETER_TYPE", pClass.getName());
+        }
+        
+        return result;
+    }
+
+    private static Object createFromParameterHandler(String value, 
+                                                     Class<?> pClass,
+                                                     Message message) {
+        Object result = null;
+        if (message != null) {
             ParameterHandler<?> pm = ProviderFactory.getInstance(message)
                 .createParameterHandler(pClass);
             if (pm != null) {
                 result = pm.fromString(value);
             }
         }
-        
-        if (result != null) {
-            return result;
-        }
-        
-        reportServerError("WRONG_PARAMETER_TYPE", pClass.getName());
-        return null;
+        return result;
     }
-
+    
     public static void reportServerError(String messageName, String parameter) {
         org.apache.cxf.common.i18n.Message errorMessage = 
             new org.apache.cxf.common.i18n.Message(messageName, 

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java?rev=1055895&r1=1055894&r2=1055895&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java Thu Jan 
6 14:37:16 2011
@@ -307,7 +307,9 @@ public class Customer extends AbstractCu
         // complete
     }
     
-    public void testCustomerParam(@QueryParam("p1") Customer c, @QueryParam("p2") Customer[]
c2) {
+    public void testCustomerParam(@QueryParam("p1") Customer c, 
+                                  @QueryParam("p2") Customer[] c2,
+                                  @QueryParam("p3") Customer2 c3) {
         // complete
     }
     

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer2.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer2.java?rev=1055895&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer2.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer2.java Thu Jan
 6 14:37:16 2011
@@ -0,0 +1,35 @@
+/**
+ * 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;
+
+public class Customer2 extends Customer {
+    
+    public Customer2() {
+        
+    }
+    
+    public Customer2(String name) {
+        if (Character.isLowerCase(((CharSequence)name).charAt(0))) {
+            throw new IllegalArgumentException();
+        }
+        //CHECKSTYLE:OFF    
+        setName(name);
+        //CHECKSTYLE:ON
+    }
+}

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

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

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/CustomerParameterHandler.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/CustomerParameterHandler.java?rev=1055895&r1=1055894&r2=1055895&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/CustomerParameterHandler.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/CustomerParameterHandler.java
Thu Jan  6 14:37:16 2011
@@ -24,7 +24,10 @@ import org.apache.cxf.jaxrs.ext.Paramete
 public class CustomerParameterHandler implements ParameterHandler<Customer> {
 
     public Customer fromString(String s) {
-        Customer c = new Customer();
+        if ("noName".equals(s)) {
+            throw new IllegalArgumentException();
+        }
+        Customer c = Character.isLowerCase(((CharSequence)s).charAt(0)) ? new Customer2()
: new Customer();
         c.setName(s);
         return c;
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java?rev=1055895&r1=1055894&r2=1055895&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
Thu Jan  6 14:37:16 2011
@@ -47,6 +47,7 @@ import javax.xml.bind.JAXBContext;
 
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.jaxrs.Customer;
+import org.apache.cxf.jaxrs.Customer2;
 import org.apache.cxf.jaxrs.CustomerGender;
 import org.apache.cxf.jaxrs.CustomerParameterHandler;
 import org.apache.cxf.jaxrs.JAXBContextProvider;
@@ -644,20 +645,49 @@ public class JAXRSUtilsTest extends Asse
         Message messageImpl = createMessage();
         ProviderFactory.getInstance(messageImpl).registerUserProvider(
             new CustomerParameterHandler());
-        Class[] argType = {Customer.class, Customer[].class};
+        Class[] argType = {Customer.class, Customer[].class, Customer2.class};
         Method m = Customer.class.getMethod("testCustomerParam", argType);
         
-        messageImpl.put(Message.QUERY_STRING, "p1=Fred&p2=Barry");
+        messageImpl.put(Message.QUERY_STRING, "p1=Fred&p2=Barry&p3=Jack&p4=John");
         List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m,
null),
                                                            null, 
                                                            messageImpl);
-        assertEquals(2, params.size());
+        assertEquals(3, params.size());
         Customer c = (Customer)params.get(0);
         assertEquals("Fred", c.getName());
         Customer c2 = ((Customer[])params.get(1))[0];
         assertEquals("Barry", c2.getName());
+        Customer2 c3 = (Customer2)params.get(2);
+        assertEquals("Jack", c3.getName());
+        
+        try {
+            messageImpl.put(Message.QUERY_STRING, "p3=noName");
+            JAXRSUtils.processParameters(new OperationResourceInfo(m, null), null, messageImpl);
+            fail("Customer2 constructor does not accept names starting with lower-case chars");
+        } catch (Exception ex) {
+            // expected
+        }
+        
+    }
+    
+    @Test
+    public void testConstructorFirstAndParameterHandler() throws Exception {
+        Message messageImpl = createMessage();
+        ProviderFactory.getInstance(messageImpl).registerUserProvider(
+            new CustomerParameterHandler());
+        Class[] argType = {Customer.class, Customer[].class, Customer2.class};
+        Method m = Customer.class.getMethod("testCustomerParam", argType);
+        
+        messageImpl.put(Message.QUERY_STRING, "p3=jack");
+        List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m,
null),
+                                                           null, 
+                                                           messageImpl);
+        assertEquals(3, params.size());
+        Customer2 c3 = (Customer2)params.get(2);
+        assertEquals("jack", c3.getName());
     }
     
+    
     @Test
     public void testArrayParamNoProvider() throws Exception {
         Message messageImpl = createMessage();



Mime
View raw message