[ 
https://issues.apache.org/jira/browse/SLING-7509?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16380587#comment-16380587
 ] 

ASF GitHub Bot commented on SLING-7509:
---------------------------------------

volteanu commented on a change in pull request #5: SLING-7509 - Add QueryClient
URL: 
https://github.com/apache/sling-org-apache-sling-testing-clients/pull/5#discussion_r171301465
 
 

 ##########
 File path: 
src/main/resources/org/apache/sling/testing/clients/query/testQuery.json.jsp
 ##########
 @@ -0,0 +1,136 @@
+<%--
+/*
+ * 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.
+--%><%@ page session="false"
+%><%@ page import="
+           java.io.PrintWriter,
+           java.util.Date,
+           javax.jcr.Node,
+           javax.jcr.NodeIterator,
+           javax.jcr.RepositoryException,
+           javax.jcr.Session,
+           javax.jcr.query.Query,
+           javax.jcr.query.QueryManager,
+           javax.jcr.query.QueryResult,
+           javax.jcr.query.Row,
+           javax.jcr.query.RowIterator,
+           org.apache.jackrabbit.value.StringValue"
+%><%@ taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0";
+%><sling:defineObjects/><%
+
+    response.setContentType("application/json");
+    response.setCharacterEncoding("UTF-8");
+
+    final PrintWriter pw = response.getWriter();
+
+    try {
+        final QueryManager qm = 
slingRequest.getResourceResolver().adaptTo(Session.class)
+                .getWorkspace().getQueryManager();
+
+        long before = 0;
+        long after = 0;
+        long total = 0;
+
+        String query = slingRequest.getParameter("query");
+        String type = slingRequest.getParameter("type");
+        boolean showResults = 
!("false".equalsIgnoreCase(slingRequest.getParameter("showresults")));
+
+        boolean tidy = false;
+        for (String selector : 
slingRequest.getRequestPathInfo().getSelectors()) {
+            if ("tidy".equals(selector)) {
+                tidy = true;
+            }
+        }
+
+        if ((query == null) || query.equals("") || (type == null) || 
type.equals("")) {
+          response.sendError(400, "Parameters query and type are required"); 
// invalid request
+          return;
+        }
+
+        // prepare
+        javax.jcr.query.Query q = qm.createQuery(query, type);
+
+        // execute
+        before = new Date().getTime();
+        QueryResult result = q.execute();
+        after = new Date().getTime();
+
+        // collect results
+        String firstSelector = null;
+        if (result.getSelectorNames().length > 1) {
+            firstSelector = result.getSelectorNames()[0];
+            try {
+                String[] columnNames = result.getColumnNames();
+                if (columnNames.length > 0) {
+                    String firstColumnName = columnNames[0];
+                    int firstDot = firstColumnName.indexOf('.');
+                    if (firstDot > 0) {
+                        firstSelector = firstColumnName.substring(0, firstDot);
+                    }
+                }
+            } catch (Exception e) {
+                log.warn("Could not get selector name of the first column", e);
+            }
+        }
+
+        // start the response json object
+        pw.println("{");
 
 Review comment:
   Switched to `com.fasterxml.jackson` in `QueryServlet`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Add QueryClient
> ---------------
>
>                 Key: SLING-7509
>                 URL: https://issues.apache.org/jira/browse/SLING-7509
>             Project: Sling
>          Issue Type: New Feature
>          Components: Apache Sling Testing Clients
>            Reporter: Valentin Olteanu
>            Priority: Major
>
> Currently, there is no way to run queries in sling using the clients. This is 
> needed in several tests to search content and assert the effects of a feature.
>  
> The solution proposed in 
> [https://github.com/apache/sling-org-apache-sling-testing-clients/pull/5] 
>  * installs a custom query servlet
>  * runs the query in any of the supported format
>  * returns results as json



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to