You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2012/11/30 16:45:53 UTC

svn commit: r1415705 - in /cxf/trunk/rt/rs/extensions/search/src: main/java/org/apache/cxf/jaxrs/ext/search/ldap/ test/java/org/apache/cxf/jaxrs/ext/search/ldap/

Author: sergeyb
Date: Fri Nov 30 15:45:52 2012
New Revision: 1415705

URL: http://svn.apache.org/viewvc?rev=1415705&view=rev
Log:
[CXF-4667] Initial support for FIQL to LDAP query conversion

Added:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/ldap/
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitor.java   (with props)
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/ldap/
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitorTest.java   (with props)

Added: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitor.java?rev=1415705&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitor.java (added)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitor.java Fri Nov 30 15:45:52 2012
@@ -0,0 +1,101 @@
+/**
+ * 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.ldap;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.cxf.jaxrs.ext.search.AbstractSearchConditionVisitor;
+import org.apache.cxf.jaxrs.ext.search.ConditionType;
+import org.apache.cxf.jaxrs.ext.search.PrimitiveStatement;
+import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+/**
+ * Initial Implementation of http://tools.ietf.org/html/rfc4515
+ */
+public class LdapQueryVisitor<T> extends AbstractSearchConditionVisitor<T, String> {
+
+    private StringBuilder sb = new StringBuilder();
+    
+    public LdapQueryVisitor() {
+        this(Collections.<String, String>emptyMap());
+    }
+    
+    public LdapQueryVisitor(Map<String, String> fieldMap) {
+        super(fieldMap);
+    }
+    
+    public void visit(SearchCondition<T> sc) {
+        
+        PrimitiveStatement statement = sc.getStatement();
+        if (statement != null) {
+            if (statement.getProperty() != null) {
+                String rvalStr = statement.getValue().toString();
+                String name = getRealPropertyName(statement.getProperty());
+               
+                sb.append("(");
+                if (sc.getConditionType() == ConditionType.NOT_EQUALS) {
+                    sb.append("!");
+                }
+                
+                String ldapOperator = conditionTypeToLdapOperator(sc.getConditionType());
+                sb.append(name).append(ldapOperator).append(rvalStr);
+                
+                sb.append(")");
+            }
+        } else {
+            sb.append("(");
+            if (sc.getConditionType() == ConditionType.AND) {
+                sb.append("&");
+            } else {
+                sb.append("|");
+            }
+            
+            for (SearchCondition<T> condition : sc.getSearchConditions()) {
+                condition.accept(this);
+            }
+            sb.append(")");
+        }
+    }
+    
+    public String getQuery() {
+        return sb.toString();
+    }
+    
+    public static String conditionTypeToLdapOperator(ConditionType ct) {
+        String op;
+        switch (ct) {
+        case EQUALS:
+        case NOT_EQUALS:
+            op = "=";
+            break;
+        case GREATER_THAN:
+        case GREATER_OR_EQUALS:
+            op = ">=";
+            break;
+        case LESS_THAN:
+        case LESS_OR_EQUALS:
+            op = "<=";
+            break;
+        default:
+            String msg = String.format("Condition type %s is not supported", ct.name());
+            throw new RuntimeException(msg);
+        }
+        return op;
+    }
+}

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

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

Added: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitorTest.java?rev=1415705&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitorTest.java (added)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/ldap/LdapQueryVisitorTest.java Fri Nov 30 15:45:52 2012
@@ -0,0 +1,131 @@
+/**
+ * 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.ldap;
+
+import java.util.Date;
+
+import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+import org.apache.cxf.jaxrs.ext.search.SearchParseException;
+import org.apache.cxf.jaxrs.ext.search.fiql.FiqlParser;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
+
+public class LdapQueryVisitorTest extends Assert {
+    private FiqlParser<Condition> parser = new FiqlParser<Condition>(Condition.class);
+
+    @Test
+    public void testSimple() throws SearchParseException {
+        SearchCondition<Condition> filter = parser.parse("name!=ami");
+        LdapQueryVisitor<Condition> visitor = new LdapQueryVisitor<Condition>();
+        filter.accept(visitor.visitor());
+        String ldap = visitor.getQuery();
+        
+        assertEquals("(!name=ami)", ldap);
+    }
+    
+    @Test
+    public void testAndQuery() throws SearchParseException {
+        SearchCondition<Condition> filter = parser.parse("name==ami*;level=gt=10");
+        LdapQueryVisitor<Condition> visitor = new LdapQueryVisitor<Condition>();
+        filter.accept(visitor.visitor());
+        String ldap = visitor.getQuery();
+        
+        assertEquals("(&(name=ami*)(level>=10))", ldap);
+    }
+    
+    @Test
+    public void testOrQuery() throws SearchParseException {
+        SearchCondition<Condition> filter = parser.parse("name==ami*,level=gt=10");
+        LdapQueryVisitor<Condition> visitor = new LdapQueryVisitor<Condition>();
+        filter.accept(visitor.visitor());
+        String ldap = visitor.getQuery();
+        
+        assertEquals("(|(name=ami*)(level>=10))", ldap);
+    }
+    
+    @Test
+    public void testAndOrQuery() throws SearchParseException {
+        SearchCondition<Condition> filter = 
+            parser.parse("name==foo;(name!=bar,level=le=10)");
+        LdapQueryVisitor<Condition> visitor = new LdapQueryVisitor<Condition>();
+        filter.accept(visitor.visitor());
+        String ldap = visitor.getQuery();
+        
+        assertEquals("(&(name=foo)(|(!name=bar)(level<=10)))", ldap);
+    }
+    
+    @Test
+    public void testComplexQuery() throws SearchParseException {
+        SearchCondition<Condition> filter = 
+            parser.parse("(name==test,level==18);(name==test1,level!=19)");
+        LdapQueryVisitor<Condition> visitor = new LdapQueryVisitor<Condition>();
+        filter.accept(visitor.visitor());
+        String ldap = visitor.getQuery();
+        assertEquals("(&(|(name=test)(level=18))(|(name=test1)(!level=19)))", ldap);
+    }
+    
+    
+    @Ignore
+    public static class Condition {
+        private String name;
+        private Integer level;
+        private Date time;
+
+        public Condition() {
+        }
+
+        public Condition(String name, Integer level, Date time) {
+            this.name = name;
+            this.level = level;
+            this.time = time;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public Integer getLevel() {
+            return level;
+        }
+
+        public void setLevel(Integer level) {
+            this.level = level;
+        }
+
+        public Date getTime() {
+            return time;
+        }
+
+        public void setTime(Date time) {
+            this.time = time;
+        }
+
+        public void setException(Exception ex) {
+            // do nothing
+        }
+
+    }
+}

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

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