cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1411658 - in /cxf/trunk: rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/ rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Tue, 20 Nov 2012 13:47:07 GMT
Author: sergeyb
Date: Tue Nov 20 13:47:06 2012
New Revision: 1411658

URL: http://svn.apache.org/viewvc?rev=1411658&view=rev
Log:
[CXF-4641] Introducing QueryContext context

Added:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContext.java
  (with props)
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContextProvider.java
  (with props)
Modified:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Added: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContext.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContext.java?rev=1411658&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContext.java
(added)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContext.java
Tue Nov 20 13:47:06 2012
@@ -0,0 +1,52 @@
+/**
+ * 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;
+
+/**
+ * Captures the converted search expression.
+ */
+public interface QueryContext {
+    
+    /**
+     * Uses {@link SearchBean} and a visitor registered with 
+     * "search.visitor" contextual property to capture and 
+     * convert the original expression
+     * 
+     * @return converted search expression
+     */
+    String getConvertedExpression(String originalExpression);
+    
+    /**
+     * Uses a custom bean and a visitor registered with 
+     * "search.visitor" contextual property to capture and 
+     * convert the original expression
+     * 
+     * @return converted search expression
+     */
+    <T> String getConvertedExpression(String originalExpression, Class<T> beanClass);
+    
+    /**
+     * Uses a custom bean and query classes and a visitor registered with 
+     * "search.visitor" contextual property to capture and 
+     * convert the original expression
+     * 
+     * @return converted search expression
+     */
+    <T, E> E getConvertedExpression(String originalExpression, Class<T> beanClass,
Class<E> queryClass);
+}

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

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

Added: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContextProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContextProvider.java?rev=1411658&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContextProvider.java
(added)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/QueryContextProvider.java
Tue Nov 20 13:47:06 2012
@@ -0,0 +1,83 @@
+/**
+ * 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 javax.ws.rs.ext.Provider;
+
+import org.apache.cxf.jaxrs.ext.ContextProvider;
+import org.apache.cxf.message.Message;
+
+@Provider
+public class QueryContextProvider implements ContextProvider<QueryContext> {
+
+    public QueryContext createContext(Message message) {
+        return new QueryContextImpl(message);
+    }
+
+    private static class QueryContextImpl implements QueryContext {
+        
+        private SearchContext searchContext;
+        private Message message;
+        public QueryContextImpl(Message message) {
+            this.searchContext = new SearchContextImpl(message);
+            this.message = message;
+        }
+
+        @Override
+        public String getConvertedExpression(String originalExpression) {
+            return getConvertedExpression(originalExpression, SearchBean.class);
+        }
+
+        @Override
+        public <T> String getConvertedExpression(String originalExpression, Class<T>
beanClass) {
+            return getConvertedExpression(originalExpression, beanClass, String.class);
+            
+        }
+        
+        @Override
+        public <T, E> E getConvertedExpression(String originalExpression,
+                                               Class<T> beanClass,
+                                               Class<E> queryClass) {
+            SearchConditionVisitor<T, E> visitor = getVisitor();
+            if (visitor == null) {
+                return null;
+            }
+            
+            SearchCondition<T> cond = searchContext.getCondition(originalExpression,
beanClass);
+            if (cond == null) {
+                return null;
+            }
+            cond.accept(visitor);
+            return queryClass.cast(visitor.getQuery());
+            
+        }
+        
+        @SuppressWarnings("unchecked")
+        private <T, Y> SearchConditionVisitor<T, Y> getVisitor() {
+            Object visitor = message.getContextualProperty(SearchUtils.SEARCH_VISITOR_PROPERTY);
+            if (visitor == null) {
+                return null;
+            } else {
+                //TODO: consider introducing SearchConditionVisitor.getBeanClass &&

+                //      SearchConditionVisitor.getQueryClass to avoid such casts
+                return (SearchConditionVisitor<T, Y>)visitor;
+            }
+        }
+    }
+}

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

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

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java?rev=1411658&r1=1411657&r2=1411658&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
Tue Nov 20 13:47:06 2012
@@ -30,6 +30,7 @@ public final class SearchUtils {
     public static final String TIMEZONE_SUPPORT_PROPERTY = "search.timezone.support";
     public static final String LAX_PROPERTY_MATCH = "search.lax.property.match";
     public static final String BEAN_PROPERTY_MAP = "search.bean.property.map";
+    public static final String SEARCH_VISITOR_PROPERTY = "search.visitor";
     
     private SearchUtils() {
         

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java?rev=1411658&r1=1411657&r2=1411658&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
Tue Nov 20 13:47:06 2012
@@ -19,6 +19,7 @@
 package org.apache.cxf.jaxrs.ext.search.sql;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -35,6 +36,11 @@ public class SQLPrinterVisitor<T> extend
     private String tableAlias;
     private List<String> columns;
     
+    // Can be useful when some other code will build Select and From clauses.
+    public SQLPrinterVisitor() {
+        this(null, null, Collections.<String>emptyList());
+    }
+    
     public SQLPrinterVisitor(String table, String... columns) {
         this(null, table, Arrays.asList(columns));
     }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java?rev=1411658&r1=1411657&r2=1411658&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java Tue
Nov 20 13:47:06 2012
@@ -29,7 +29,10 @@ import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.interceptor.Interceptor;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.ext.search.QueryContextProvider;
+import org.apache.cxf.jaxrs.ext.search.SearchBean;
 import org.apache.cxf.jaxrs.ext.search.SearchContextProvider;
+import org.apache.cxf.jaxrs.ext.search.sql.SQLPrinterVisitor;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.jaxrs.provider.BinaryDataProvider;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
@@ -64,6 +67,7 @@ public class BookServer extends Abstract
         providers.add(new GenericHandlerWriter());
         providers.add(new FaultyRequestHandler());
         providers.add(new SearchContextProvider());
+        providers.add(new QueryContextProvider());
         sf.setProviders(providers);
         List<Interceptor<? extends Message>> outInts = new ArrayList<Interceptor<?
extends Message>>();
         outInts.add(new CustomOutInterceptor());
@@ -76,7 +80,7 @@ public class BookServer extends Abstract
         sf.setAddress("http://localhost:" + PORT + "/");
 
         sf.getProperties(true).put("org.apache.cxf.jaxrs.mediaTypeCheck.strict", true);
-        
+        sf.getProperties().put("search.visitor", new SQLPrinterVisitor<SearchBean>("books"));
         server = sf.create();
         BusFactory.setDefaultBus(null);
         BusFactory.setThreadDefaultBus(null);

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1411658&r1=1411657&r2=1411658&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Tue
Nov 20 13:47:06 2012
@@ -78,6 +78,7 @@ import org.apache.cxf.common.util.ProxyH
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.Nullable;
 import org.apache.cxf.jaxrs.ext.Oneway;
+import org.apache.cxf.jaxrs.ext.search.QueryContext;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 import org.apache.cxf.jaxrs.ext.search.SearchContext;
 import org.apache.cxf.jaxrs.ext.xml.XMLInstruction;
@@ -632,6 +633,15 @@ public class BookStore {
     }
     
     @GET
+    @Path("/books/querycontext/{expression}")
+    @Produces("text/plain")
+    public String getBookQueryContext(@PathParam("expression") String expression, 
+                                      @Context QueryContext searchContext) 
+        throws BookNotFoundFault {
+        return searchContext.getConvertedExpression(expression);
+    }
+    
+    @GET
     @Path("/books/{search}/chapter/{chapter}")
     @Produces("application/xml")
     public Chapter getChapterFromSelectedBook(@Context SearchContext searchContext,

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1411658&r1=1411657&r2=1411658&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Tue Nov 20 13:47:06 2012
@@ -1063,6 +1063,18 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testGetSearchBookSQL() throws Exception {
+        String address = "http://localhost:" + PORT 
+            + "/bookstore/books/querycontext/id=ge=123";
+                          
+        WebClient client = WebClient.create(address);
+        client.accept("text/plain");
+        String sql = client.get(String.class);
+        assertEquals("SELECT * FROM books WHERE id >= '123'", sql);
+    }
+    
+    
+    @Test
     public void testGetBook123CGLIB() throws Exception {
         getAndCompareAsStrings("http://localhost:" + PORT + "/bookstore/books/123/cglib",
                                "resources/expected_get_book123.txt",



Mime
View raw message