cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1547752 - in /cxf/trunk/rt/rs/extensions/search/src: main/java/org/apache/cxf/jaxrs/ext/search/ test/java/org/apache/cxf/jaxrs/ext/search/
Date Wed, 04 Dec 2013 10:33:37 GMT
Author: sergeyb
Date: Wed Dec  4 10:33:37 2013
New Revision: 1547752

URL: http://svn.apache.org/r1547752
Log:
[CXF-5430] Making it possible to register a custom parser

Added:
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplCustomParserTest.java
  (with props)
Modified:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchConditionParser.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchConditionParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchConditionParser.java?rev=1547752&r1=1547751&r2=1547752&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchConditionParser.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchConditionParser.java
Wed Dec  4 10:33:37 2013
@@ -19,9 +19,8 @@
 package org.apache.cxf.jaxrs.ext.search;
 
 /**
- * Search Language Parser implementation.
- * FIQL Language parser is the default implementation. 
+ * Search Expression Parser converts an expression into SearchCondition.
  */
 public interface SearchConditionParser<T> {
-    SearchCondition<T> parse(String fiqlExpression) throws SearchParseException;
+    SearchCondition<T> parse(String searchExpression) throws SearchParseException;
 }

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java?rev=1547752&r1=1547751&r2=1547752&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java
Wed Dec  4 10:33:37 2013
@@ -170,9 +170,12 @@ public class SearchContextImpl implement
     private <T> SearchConditionParser<T> getParser(Class<T> cls, 
                                                    Map<String, String> beanProperties,
                                                    Map<String, String> parserProperties)
{
-        // we can use this method as a parser factory, ex
-        // we can get parsers capable of parsing XQuery and other languages
-        // depending on the properties set by a user
+        
+        Object parserProp = message.getContextualProperty(SearchConditionParser.class.getName());
+        if (parserProp != null) {
+            return getCustomParser(parserProp);
+        }
+        
         Map<String, String> props = null;
         if (parserProperties == null) {
             props = new LinkedHashMap<String, String>(4);
@@ -199,4 +202,9 @@ public class SearchContextImpl implement
         
         return new FiqlParser<T>(cls, props, beanProps); 
     }
+    
+    @SuppressWarnings("unchecked")
+    private <T> SearchConditionParser<T> getCustomParser(Object parserProp) {
+        return (SearchConditionParser<T>)parserProp;
+    }
 }

Added: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplCustomParserTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplCustomParserTest.java?rev=1547752&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplCustomParserTest.java
(added)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplCustomParserTest.java
Wed Dec  4 10:33:37 2013
@@ -0,0 +1,70 @@
+/**
+ * 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.ext.search;
+
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SearchContextImplCustomParserTest extends Assert {
+
+    @Test
+    public void testQuery() {
+        Message m = new MessageImpl();
+        m.put(SearchContextImpl.CUSTOM_SEARCH_QUERY_PARAM_NAME, "$customfilter");
+        m.put(SearchConditionParser.class.getName(), new CustomParser());
+        m.put(Message.QUERY_STRING, "$customfilter=color is red");
+        SearchCondition<Color> sc = new SearchContextImpl(m).getCondition(Color.class);
+        
+        assertTrue(sc.isMet(new Color("red")));
+        assertFalse(sc.isMet(new Color("blue")));
+    }
+    
+    private static class CustomParser implements SearchConditionParser<Color> {
+
+        @Override
+        public SearchCondition<Color> parse(String searchExpression) throws SearchParseException
{
+            if (!searchExpression.startsWith("color is ")) {
+                throw new SearchParseException();
+            }
+            String value = searchExpression.substring(9);
+            SearchCondition<Color> color = new PrimitiveSearchCondition<Color>("color",

+                                               value,
+                                               ConditionType.EQUALS,
+                                               new Color(value));
+            
+            return color;
+        }
+        
+    }
+
+    private static class Color {
+        private String color;
+        public Color(String color) {
+            this.color = color;
+        }
+
+        @SuppressWarnings("unused")
+        public String getColor() {
+            return color;
+        }
+    }
+}

Propchange: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplCustomParserTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplCustomParserTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message