cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1567194 - in /cxf/trunk/rt/rs/extensions/search/src: main/java/org/apache/cxf/jaxrs/ext/search/ main/java/org/apache/cxf/jaxrs/ext/search/fiql/ main/java/org/apache/cxf/jaxrs/ext/search/odata/ test/java/org/apache/cxf/jaxrs/ext/search/odata/
Date Tue, 11 Feb 2014 16:01:33 GMT
Author: sergeyb
Date: Tue Feb 11 16:01:32 2014
New Revision: 1567194

URL: http://svn.apache.org/r1567194
Log:
[CXF-5430] Support for cases when the bean and search properties names differ

Modified:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/fiql/FiqlParser.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParser.java
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParserTest.java

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java?rev=1567194&r1=1567193&r2=1567194&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java
Tue Feb 11 16:01:32 2014
@@ -34,21 +34,52 @@ import org.apache.cxf.jaxrs.ext.search.B
 import org.apache.cxf.jaxrs.ext.search.collections.CollectionCheck;
 import org.apache.cxf.jaxrs.ext.search.collections.CollectionCheckInfo;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
+import org.apache.cxf.message.MessageUtils;
 
 public abstract class AbstractSearchConditionParser<T> implements SearchConditionParser<T>
{
     
     
     protected final Map<String, String> contextProperties;
     protected final Class<T> conditionClass;
+    protected Beanspector<T> beanspector;       
+    protected Map<String, String> beanPropertiesMap;
+    
     
     protected AbstractSearchConditionParser(Class<T> tclass) {
-        this(tclass, Collections.<String, String>emptyMap());
+        this(tclass, Collections.<String, String>emptyMap(), null);
     }
     
-    protected AbstractSearchConditionParser(Class<T> tclass, Map<String, String>
contextProperties) {
+    protected AbstractSearchConditionParser(Class<T> tclass, 
+                                            Map<String, String> contextProperties,
+                                            Map<String, String> beanProperties) {
         this.conditionClass = tclass;
         this.contextProperties = contextProperties == null 
             ? Collections.<String, String>emptyMap() : contextProperties;
+        beanspector = SearchBean.class.isAssignableFrom(tclass) ? null : new Beanspector<T>(tclass);
+        this.beanPropertiesMap = beanProperties;
+    }
+    
+    protected String getActualSetterName(String setter) {
+        String beanPropertyName = beanPropertiesMap == null ? null : beanPropertiesMap.get(setter);
+        return beanPropertyName != null ? beanPropertyName : setter;
+    }
+    
+    protected TypeInfo getTypeInfo(String setter, String value) 
+        throws SearchParseException, PropertyNotFoundException {
+        
+        String name = getSetter(setter);
+        
+        TypeInfo typeInfo = null;
+        try {
+            typeInfo = beanspector != null ? beanspector.getAccessorTypeInfo(name) 
+                    : new TypeInfo(String.class, String.class);
+        } catch (Exception e) {
+            // continue
+        }
+        if (typeInfo == null && !MessageUtils.isTrue(contextProperties.get(SearchUtils.LAX_PROPERTY_MATCH)))
{
+            throw new PropertyNotFoundException(name, value);
+        }
+        return typeInfo;
     }
     
     protected String getSetter(String setter) {

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/fiql/FiqlParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/fiql/FiqlParser.java?rev=1567194&r1=1567193&r2=1567194&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/fiql/FiqlParser.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/fiql/FiqlParser.java
Tue Feb 11 16:01:32 2014
@@ -27,15 +27,12 @@ import java.util.regex.Pattern;
 
 import org.apache.cxf.jaxrs.ext.search.AbstractSearchConditionParser;
 import org.apache.cxf.jaxrs.ext.search.AndSearchCondition;
-import org.apache.cxf.jaxrs.ext.search.Beanspector;
 import org.apache.cxf.jaxrs.ext.search.Beanspector.TypeInfo;
 import org.apache.cxf.jaxrs.ext.search.ConditionType;
 import org.apache.cxf.jaxrs.ext.search.OrSearchCondition;
-import org.apache.cxf.jaxrs.ext.search.PropertyNotFoundException;
 import org.apache.cxf.jaxrs.ext.search.SearchBean;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 import org.apache.cxf.jaxrs.ext.search.SearchParseException;
-import org.apache.cxf.jaxrs.ext.search.SearchUtils;
 import org.apache.cxf.jaxrs.ext.search.SimpleSearchCondition;
 import org.apache.cxf.message.MessageUtils;
 
@@ -99,9 +96,6 @@ public class FiqlParser<T> extends Abstr
         COMPARATORS_PATTERN_SINGLE_EQUALS = Pattern.compile(s2);
     }
 
-    private Beanspector<T> beanspector;       
-    private Map<String, String> beanPropertiesMap;
-    
     private Map<String, ConditionType> operatorsMap = OPERATORS_MAP;
     private Pattern comparatorsPattern = COMPARATORS_PATTERN;
     /**
@@ -135,12 +129,8 @@ public class FiqlParser<T> extends Abstr
     public FiqlParser(Class<T> tclass, 
                       Map<String, String> contextProperties,
                       Map<String, String> beanProperties) {
-        super(tclass, contextProperties);
-        
-        beanspector = SearchBean.class.isAssignableFrom(tclass) 
-            ? null : new Beanspector<T>(tclass);
+        super(tclass, contextProperties, beanProperties);
         
-        this.beanPropertiesMap = beanProperties;
         if (MessageUtils.isTrue(this.contextProperties.get(SUPPORT_SINGLE_EQUALS))) {
             operatorsMap = new HashMap<String, ConditionType>(operatorsMap);
             operatorsMap.put("=", ConditionType.EQUALS);
@@ -172,7 +162,6 @@ public class FiqlParser<T> extends Abstr
      */
     public SearchCondition<T> parse(String fiqlExpression) throws SearchParseException
{
         ASTNode<T> ast = parseAndsOrsBrackets(fiqlExpression);
-        // System.out.println(ast);
         return ast.build();
     }
 
@@ -267,18 +256,13 @@ public class FiqlParser<T> extends Abstr
             }
             
             String name = unwrapSetter(propertyName);
-            String beanPropertyName = beanPropertiesMap == null ? null : beanPropertiesMap.get(name);
-            if (beanPropertyName != null) {
-                name = beanPropertyName;
-            }
-            
+        
+            name = getActualSetterName(name);
             TypeInfoObject castedValue = parseType(propertyName, name, value);
             if (castedValue != null) {
                 return new Comparison(name, operator, castedValue);
-            } else if (MessageUtils.isTrue(contextProperties.get(SearchUtils.LAX_PROPERTY_MATCH)))
{
-                return null;
             } else {
-                throw new PropertyNotFoundException(name, value);
+                return null;
             }
         } else {
             throw new SearchParseException("Not a comparison expression: " + expr);
@@ -287,18 +271,9 @@ public class FiqlParser<T> extends Abstr
 
     
     private TypeInfoObject parseType(String originalName, String setter, String value) throws
SearchParseException {
-        String name = getSetter(setter);
-        
-        try {
-            TypeInfo typeInfo = 
-                beanspector != null ? beanspector.getAccessorTypeInfo(name) 
-                    : new TypeInfo(String.class, String.class);
-            Object object = parseType(originalName, null, null, setter, typeInfo, value);
-            return new TypeInfoObject(object, typeInfo);
-        } catch (Exception e) {
-            return null;
-        }
-        
+        TypeInfo typeInfo = getTypeInfo(setter, value);
+        Object object = parseType(originalName, null, null, setter, typeInfo, value);
+        return new TypeInfoObject(object, typeInfo);
     }
     
     protected boolean isCount(String propName) {

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParser.java?rev=1567194&r1=1567193&r2=1567194&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParser.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParser.java
Tue Feb 11 16:01:32 2014
@@ -19,11 +19,12 @@
 package org.apache.cxf.jaxrs.ext.search.odata;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.cxf.jaxrs.ext.search.AbstractSearchConditionParser;
 import org.apache.cxf.jaxrs.ext.search.AndSearchCondition;
-import org.apache.cxf.jaxrs.ext.search.Beanspector;
 import org.apache.cxf.jaxrs.ext.search.Beanspector.TypeInfo;
 import org.apache.cxf.jaxrs.ext.search.ConditionType;
 import org.apache.cxf.jaxrs.ext.search.OrSearchCondition;
@@ -81,11 +82,9 @@ public class ODataParser<T> extends Abst
     
     private class FilterExpressionVisitor implements ExpressionVisitor {
         private final T condition;
-        private final Beanspector< T > beanspector;
         
         FilterExpressionVisitor(final T condition) {
             this.condition = condition;
-            this.beanspector = new Beanspector<T>(condition);
         }
 
         @Override
@@ -188,12 +187,9 @@ public class ODataParser<T> extends Abst
         
         @Override
         public Object visitProperty(PropertyExpression propertyExpression, String uriLiteral,
EdmTyped edmProperty) {
-            try {
-                final TypeInfo typeInfo = beanspector.getAccessorTypeInfo(uriLiteral);
-                return new TypedProperty(typeInfo, uriLiteral);
-            } catch (Exception ex) {
-                throw new SearchParseException("Failed to get type information from property
path: " + uriLiteral, ex);
-            }
+            String setter = getActualSetterName(uriLiteral);
+            final TypeInfo typeInfo = ODataParser.this.getTypeInfo(setter, null);
+            return new TypedProperty(typeInfo, setter);
         }
 
         @Override
@@ -229,14 +225,41 @@ public class ODataParser<T> extends Abst
     }
     
     /**
-     * Creates OData 2.0 parser.
+     * Creates OData parser.
      * 
      * @param conditionClass - class of T used to create condition objects. Class T must
have
      *            accessible no-arguments constructor and complementary setters to these
used in 
-     *            OData 2.0 $filter expressions.
+     *            OData $filter expressions.
      */
     public ODataParser(final Class< T > conditionClass) {    
-        super(conditionClass);
+        this(conditionClass, Collections.<String, String>emptyMap());
+    }
+    
+    /**
+     * Creates OData parser.
+     * 
+     * @param tclass - class of T used to create condition objects in built syntax tree.
Class T must have
+     *            accessible no-arg constructor and complementary setters to these used in

+     *            OData $filter expressions.
+     * @param contextProperties            
+     */
+    public ODataParser(Class<T> tclass, Map<String, String> contextProperties)
{
+        this(tclass, contextProperties, null);
+    }
+    
+    /**
+     * Creates OData parser.
+     * 
+     * @param tclass - class of T used to create condition objects in built syntax tree.
Class T must have
+     *            accessible no-arg constructor and complementary setters to these used in
+     *            OData $filter expressions.
+     * @param contextProperties            
+     */
+    public ODataParser(Class<T> tclass, 
+                      Map<String, String> contextProperties,
+                      Map<String, String> beanProperties) {
+        super(tclass, contextProperties, beanProperties);
+        
         this.parser = new FilterParserImpl(null);
     }
     

Modified: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParserTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParserTest.java?rev=1567194&r1=1567193&r2=1567194&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParserTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/odata/ODataParserTest.java
Tue Feb 11 16:01:32 2014
@@ -18,6 +18,8 @@
  */
 package org.apache.cxf.jaxrs.ext.search.odata;
 
+import java.util.Collections;
+
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 import org.apache.cxf.jaxrs.ext.search.SearchParseException;
 
@@ -73,7 +75,8 @@ public class ODataParserTest extends Ass
 
     @Before
     public void setUp() {
-        parser = new ODataParser<Person>(Person.class);
+        parser = new ODataParser<Person>(Person.class, Collections.<String, String>emptyMap(),

+            Collections.singletonMap("thename", "FirstName"));
     }
 
     @Test
@@ -84,6 +87,13 @@ public class ODataParserTest extends Ass
     }
     
     @Test
+    public void testFilterByFirstNameEqualsValueNonMatchingProperty() throws SearchParseException
{
+        SearchCondition< Person > filter = parser.parse("thename eq 'Tom'");
+        assertTrue(filter.isMet(new Person("Tom", "Bombadil")));
+        assertFalse(filter.isMet(new Person("Peter", "Bombadil")));
+    }
+    
+    @Test
     public void testFilterByFirstAndLastNameEqualValue() throws SearchParseException {
         SearchCondition< Person > filter = parser.parse("FirstName eq 'Tom' and LastName
eq 'Bombadil'");
         assertTrue(filter.isMet(new Person("Tom", "Bombadil")));



Mime
View raw message