incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1131257 - in /sling/trunk: bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/ bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/ launchpad/builder/src/main/bundles/ launchpad/integration-tests/sr...
Date Fri, 03 Jun 2011 21:52:19 GMT
Author: justin
Date: Fri Jun  3 21:52:18 2011
New Revision: 1131257

URL: http://svn.apache.org/viewvc?rev=1131257&view=rev
Log:
SLING-1533 - removing query language validation from json query servlet and now doing it inside
the resource resolver. also adding code which injects jcr:path and jcr:score if those aren't
in the column list

Modified:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
    sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
    sling/trunk/launchpad/builder/src/main/bundles/list.xml
    sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java?rev=1131257&r1=1131256&r2=1131257&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
Fri Jun  3 21:52:18 2011
@@ -23,7 +23,6 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.LinkedList;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Set;
@@ -36,7 +35,9 @@ import javax.jcr.NamespaceException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
+import javax.jcr.query.Query;
 import javax.jcr.query.QueryResult;
+import javax.jcr.query.Row;
 import javax.jcr.query.RowIterator;
 import javax.servlet.http.HttpServletRequest;
 
@@ -94,6 +95,15 @@ public class JcrResourceResolver
 
     public static final String PROP_REDIRECT_EXTERNAL_STATUS = "sling:status";
 
+    @SuppressWarnings("deprecation")
+    private static final String DEFAULT_QUERY_LANGUAGE = Query.XPATH;
+
+    /** column name for node path */
+    private static final String QUERY_COLUMN_PATH = "jcr:path";
+
+    /** column name for score value */
+    private static final String QUERY_COLUMN_SCORE = "jcr:score";
+
     /** The root provider for the resource tree. */
     private final JcrResourceProviderEntry rootProvider;
 
@@ -905,9 +915,12 @@ public class JcrResourceResolver
                                                         final String language)
     throws SlingException {
         checkClosed();
+
+        final String queryLanguage = isSupportedQueryLanguage(language) ? language : DEFAULT_QUERY_LANGUAGE;
+
         try {
             QueryResult result = JcrResourceUtil.query(adaptTo(Session.class), query,
-                language);
+                queryLanguage);
             final String[] colNames = result.getColumnNames();
             final RowIterator rows = result.getRows();
             return new Iterator<Map<String, Object>>() {
@@ -918,14 +931,31 @@ public class JcrResourceResolver
                 public Map<String, Object> next() {
                     Map<String, Object> row = new HashMap<String, Object>();
                     try {
-                        Value[] values = rows.nextRow().getValues();
+                        Row jcrRow = rows.nextRow();
+                        boolean didPath = false;
+                        boolean didScore = false;
+                        Value[] values = jcrRow.getValues();
                         for (int i = 0; i < values.length; i++) {
                             Value v = values[i];
                             if (v != null) {
-                                row.put(colNames[i],
+                                String colName = colNames[i];
+                                row.put(colName,
                                     JcrResourceUtil.toJavaObject(values[i]));
+                                if (colName.equals(QUERY_COLUMN_PATH)) {
+                                    didPath = true;
+                                }
+                                if (colName.equals(QUERY_COLUMN_SCORE)) {
+                                    didScore = true;
+                                }
                             }
                         }
+                        if (!didPath) {
+                            row.put(QUERY_COLUMN_PATH, jcrRow.getPath());
+                        }
+                        if (!didScore) {
+                            row.put(QUERY_COLUMN_SCORE, jcrRow.getScore());
+                        }
+
                     } catch (RepositoryException re) {
                         LOGGER.error(
                             "queryResources$next: Problem accessing row values",
@@ -1289,4 +1319,19 @@ public class JcrResourceResolver
 
         return absPath;
     }
+
+    private boolean isSupportedQueryLanguage(String language) {
+        try {
+        String[] supportedLanguages = adaptTo(Session.class).getWorkspace().
+            getQueryManager().getSupportedQueryLanguages();
+            for (String lang : supportedLanguages) {
+                if (lang.equals(language)) {
+                    return true;
+                }
+            }
+        } catch (RepositoryException e) {
+            LOGGER.error("Unable to discover supported query languages", e);
+        }
+        return false;
+    }
 }

Modified: sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java?rev=1131257&r1=1131256&r2=1131257&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
(original)
+++ sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
Fri Jun  3 21:52:18 2011
@@ -24,8 +24,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-import javax.jcr.query.Query;
-
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
@@ -114,8 +112,7 @@ public class JsonQueryServlet extends Sl
      *
      */
     protected String getQueryType(SlingHttpServletRequest req) {
-        return (req.getParameter(QUERY_TYPE) != null && req.getParameter(
-                QUERY_TYPE).equals(Query.SQL)) ? Query.SQL : Query.XPATH;
+        return req.getParameter(QUERY_TYPE);
     }
     
 

Modified: sling/trunk/launchpad/builder/src/main/bundles/list.xml
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/builder/src/main/bundles/list.xml?rev=1131257&r1=1131256&r2=1131257&view=diff
==============================================================================
--- sling/trunk/launchpad/builder/src/main/bundles/list.xml (original)
+++ sling/trunk/launchpad/builder/src/main/bundles/list.xml Fri Jun  3 21:52:18 2011
@@ -101,7 +101,7 @@
         <bundle>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.servlets.get</artifactId>
-            <version>2.1.2</version>
+            <version>2.1.3-SNAPSHOT</version>
         </bundle>
         <bundle>
             <groupId>org.apache.sling</groupId>

Modified: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java?rev=1131257&r1=1131256&r2=1131257&view=diff
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
(original)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
Fri Jun  3 21:52:18 2011
@@ -111,6 +111,11 @@ public class JsonQueryServletTest extend
         assertCount(5, query, "sql", 0, 0);
     }
     
+    public void testSql2() throws IOException {
+        final String query = "select * from [nt:unstructured] where ISDESCENDANTNODE('" +
testPath + "/folderB')";
+        assertCount(5, query, "JCR-SQL2", 0, 0);
+    }
+
     public void testOffset() throws IOException {
         assertCount(3, "/" + testPath + "/folderC/*", "xpath", 2, 0);
     }



Mime
View raw message