You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2008/03/05 14:35:44 UTC
svn commit: r633842 - in /incubator/sling/trunk:
launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/
sling/servlets-default/src/main/java/org/apache/sling/servlets/
Author: bdelacretaz
Date: Wed Mar 5 05:35:42 2008
New Revision: 633842
URL: http://svn.apache.org/viewvc?rev=633842&view=rev
Log:
SLING-307 - JsonQueryServlet - contributed by Philipp Koch, thanks!
Added:
incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java (with props)
incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonQueryServlet.java (with props)
Modified:
incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java
incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonRendererServlet.java
Modified: incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java?rev=633842&r1=633841&r2=633842&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java (original)
+++ incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java Wed Mar 5 05:35:42 2008
@@ -38,6 +38,7 @@
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.sling.launchpad.webapp.integrationtest.helpers.HttpAnyMethod;
import org.apache.sling.launchpad.webapp.integrationtest.helpers.UslingIntegrationTestClient;
import org.apache.sling.ujax.UjaxPostServlet;
@@ -253,11 +254,20 @@
}
}
+ /** retrieve the contents of given URL and assert its content type */
+ protected String getContent(String url, String expectedContentType) throws IOException {
+ return getContent(url, expectedContentType, null);
+ }
+
/** retrieve the contents of given URL and assert its content type
* @throws IOException
* @throws HttpException */
- protected String getContent(String url, String expectedContentType) throws IOException {
+ protected String getContent(String url, String expectedContentType, List<NameValuePair> params) throws IOException {
final GetMethod get = new GetMethod(url);
+ if(params != null) {
+ final NameValuePair [] nvp = new NameValuePair[0];
+ get.setQueryString(params.toArray(nvp));
+ }
final int status = httpClient.executeMethod(get);
assertEquals("Expected status 200 for " + url,200,status);
final Header h = get.getResponseHeader("Content-Type");
Added: incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java?rev=633842&view=auto
==============================================================================
--- incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java (added)
+++ incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java Wed Mar 5 05:35:42 2008
@@ -0,0 +1,140 @@
+/*
+ * 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.sling.launchpad.webapp.integrationtest;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.httpclient.NameValuePair;
+
+
+/** Test the {link JsonQueryServlet) functionality.
+ * We don't need to test the repository query feature, just
+ * make sure that the query servlet parameters are interpreted correctly.
+ */
+public class JsonQueryServletTest extends HttpTestBase {
+
+ private String testFolderUrl;
+ private final String testPath = "/" + getClass().getSimpleName() + "_" + System.currentTimeMillis();
+ private final static String counterCode = "out.print(data.length);";
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ testFolderUrl = testClient.createNode(HTTP_BASE_URL + testPath, null);
+
+ // create subfolders A..E with subnodes 0..4, each contains its name as a text property
+ for(char folder = 'A'; folder <= 'E'; folder++) {
+ final Map<String, String> props = new HashMap<String, String> ();
+ props.put("creator", getClass().getSimpleName());
+ props.put("text", "folder " + folder);
+ final String subfolderUrl = testClient.createNode(testFolderUrl + "/folder" + folder, props);
+ for(int i=0; i < 5; i++) {
+ props.put("text", "folder " + folder + " node "+ i);
+ testClient.createNode(subfolderUrl + "/node" + i, props);
+ }
+
+ }
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ if(testFolderUrl != null) {
+ testClient.delete(WEBDAV_BASE_URL + testPath);
+ }
+ }
+
+ private void assertCount(int expectedCount, String statement, String queryType, int offset, int rows)
+ throws IOException {
+ final List<NameValuePair> params = new ArrayList<NameValuePair>();
+ params.add(new NameValuePair("statement", statement));
+ if(queryType != null) {
+ params.add(new NameValuePair("queryType", queryType));
+ }
+ if(offset > 0) {
+ params.add(new NameValuePair("offset", String.valueOf(offset)));
+ }
+ if(rows > 0) {
+ params.add(new NameValuePair("rows", String.valueOf(rows)));
+ }
+ final String json = getContent(testFolderUrl + ".query.json", CONTENT_TYPE_JSON, params);
+ assertJavascript(
+ expectedCount + ".0",
+ json,
+ counterCode,
+ "statement=" + statement + ", queryType=" + queryType
+ );
+ }
+
+ public void testFolderQuery() throws IOException {
+ assertCount(1, "/" + testPath + "/folderC", "xpath", 0, 0);
+ }
+
+ public void testSubFolderQuery() throws IOException {
+ assertCount(5, "/" + testPath + "/folderA/*", "xpath", 0, 0);
+ }
+
+ public void testDefaultQueryType() throws IOException {
+ assertCount(5, "/" + testPath + "/folderE/*", null, 0, 0);
+ }
+
+ public void testSql() throws IOException {
+ final String query = "select * from nt:unstructured where jcr:path like '" + testPath + "/folderB/%'";
+ assertCount(5, query, "sql", 0, 0);
+ }
+
+ public void testOffset() throws IOException {
+ assertCount(3, "/" + testPath + "/folderC/*", "xpath", 2, 0);
+ }
+
+ public void testRows() throws IOException {
+ assertCount(2, "/" + testPath + "/folderC/*", "xpath", 0, 2);
+ }
+
+ public void testPropertyParam() throws IOException {
+ final String url = testFolderUrl + ".query.json";
+ final String statement = "/" + testPath + "/folderB/node3";
+ final List<NameValuePair> params = new ArrayList<NameValuePair>();
+ params.add(new NameValuePair("statement", statement));
+
+ String json = getContent(url, CONTENT_TYPE_JSON, params);
+ assertJavascript("1.0", json, counterCode);
+ assertJavascript("ok", json, "if(!data[0].text) out.print('ok')");
+ assertJavascript("ok", json, "if(!data[0].creator) out.print('ok')");
+
+ params.add(new NameValuePair("property", "text"));
+ json = getContent(url, CONTENT_TYPE_JSON, params);
+ assertJavascript("1.0", json, counterCode);
+ assertJavascript("ok", json, "if(data[0].text) out.print('ok')");
+ assertJavascript("folder B node 3", json, "out.print(data[0].text)");
+ assertJavascript("ok", json, "if(!data[0].creator) out.print('ok')");
+
+ params.add(new NameValuePair("property", "creator"));
+ json = getContent(url, CONTENT_TYPE_JSON, params);
+ assertJavascript("1.0", json, counterCode);
+ assertJavascript("ok", json, "if(data[0].text) out.print('ok')");
+ assertJavascript("folder B node 3", json, "out.print(data[0].text)");
+ assertJavascript("ok", json, "if(data[0].creator) out.print('ok')");
+ assertJavascript(getClass().getSimpleName(), json, "out.print(data[0].creator)");
+ }
+}
Propchange: incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/sling/trunk/launchpad/webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonQueryServletTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added: incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonQueryServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonQueryServlet.java?rev=633842&view=auto
==============================================================================
--- incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonQueryServlet.java (added)
+++ incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonQueryServlet.java Wed Mar 5 05:35:42 2008
@@ -0,0 +1,223 @@
+/*
+ * 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.sling.servlets;
+
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.SlingException;
+import org.apache.sling.commons.json.io.JSONWriter;
+import org.apache.sling.commons.json.JSONException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.jcr.Session;
+import javax.jcr.Node;
+import javax.jcr.Value;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryManager;
+import javax.jcr.query.QueryResult;
+import javax.jcr.query.RowIterator;
+import javax.jcr.query.Row;
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * A SlingSafeMethodsServlet that renders the search results as JSON data
+ *
+ * @scr.service
+ * interface="javax.servlet.Servlet"
+ *
+ * @scr.component
+ * immediate="true"
+ * metatype="false"
+ *
+ * @scr.property
+ * name="service.description"
+ * value="Default Query Servlet"
+ *
+ * @scr.property
+ * name="service.vendor"
+ * value="The Apache Software Foundation"
+ *
+ * Use this as the default query servlet for json get requests for Sling
+ * @scr.property
+ * name="sling.servlet.resourceTypes"
+ * value="sling/servlet/default"
+ *
+ * @scr.property
+ * name="sling.servlet.extensions"
+ * value="json"
+ *
+ * @scr.property
+ * name="sling.servlet.selectors"
+ * value="query"
+ */
+public class JsonQueryServlet extends SlingSafeMethodsServlet {
+ private final Logger log = LoggerFactory.getLogger(JsonQueryServlet.class);
+
+ /** Search clause */
+ public static final String STATEMENT = "statement";
+
+ /** Query type */
+ public static final String QUERY_TYPE = "queryType";
+
+ /** Result set offset */
+ public static final String OFFSET = "offset";
+
+ /** Number of rows requested */
+ public static final String ROWS = "rows";
+
+ /** property to append to the result */
+ public static final String PROPERTY = "property";
+
+ /** exerpt lookup path */
+ public static final String EXCERPT_PATH = "excerptPath";
+
+ /** rep:exerpt */
+ private static final String REP_EXCERPT = "rep:excerpt()";
+
+ @Override
+ protected void doGet(SlingHttpServletRequest req,
+ SlingHttpServletResponse resp) throws IOException {
+ dumpResult(req, resp);
+ }
+
+ /**
+ * Dumps the result as JSON object.
+ *
+ * @param req request
+ * @param resp response
+ * @throws IOException in case the search will unexpectedly fail
+ */
+ private void dumpResult(SlingHttpServletRequest req,
+ SlingHttpServletResponse resp) throws IOException {
+ try {
+ Session s = req.getResourceResolver().adaptTo(Session.class);
+ if (s == null) {
+ throw new IOException("No JCR Session available");
+ }
+ String statement = req.getParameter(STATEMENT);
+ String queryType =
+ (req.getParameter(QUERY_TYPE) != null &&
+ req.getParameter(QUERY_TYPE).equals(Query.SQL)) ?
+ Query.SQL : Query.XPATH;
+ QueryManager qm = s.getWorkspace().getQueryManager();
+ Query query = qm.createQuery(statement, queryType);
+ QueryResult result = query.execute();
+ RowIterator rows = result.getRows();
+ String cols[] = result.getColumnNames();
+ resp.setContentType(JsonRendererServlet.responseContentType);
+ final JSONWriter w = new JSONWriter(resp.getWriter());
+ w.array();
+ if (req.getParameter(OFFSET) != null) {
+ long skip = Long.parseLong(req.getParameter(OFFSET));
+ rows.skip(skip);
+ }
+ long count = -1;
+ if (req.getParameter(ROWS) != null) {
+ count = Long.parseLong(req.getParameter(ROWS));
+ }
+
+ List<String> properties = new ArrayList<String>();
+ if (req.getParameterValues(PROPERTY) != null) {
+ for (String property: req.getParameterValues(PROPERTY)) {
+ properties.add(property);
+ }
+ }
+
+ String exerptPath = "";
+ if (req.getParameter(EXCERPT_PATH) != null) {
+ exerptPath = req.getParameter(EXCERPT_PATH);
+ }
+
+ // iterate through the result set and build the "json result"
+ while (rows.hasNext() && count != 0) {
+ Row row = rows.nextRow();
+ String path = row.getValue("jcr:path").getString();
+ Node node = (Node) s.getItem(path);
+ String name = node.getName();
+
+ w.object();
+ w.key("name");
+ w.value(name);
+ for (String colName: cols) {
+ w.key(colName);
+ String strValue = "";
+ if (colName.equals(REP_EXCERPT)) {
+ Value ev = row.getValue("rep:excerpt(" + exerptPath + ")");
+ strValue = ev == null ? "" : ev.getString();
+ } else {
+ Value value = row.getValue(colName);
+ strValue = formatValue(value);
+ }
+ w.value(strValue);
+ }
+
+ // load properties and add it to the result set
+ for (String property: properties) {
+ if (node.hasProperty(property)) {
+ Value value = node.getProperty(property).getValue();
+ String strValue = formatValue(value);
+ w.key(property);
+ w.value(strValue);
+ }
+ }
+ w.endObject();
+ count--;
+ }
+ w.endArray();
+ } catch (JSONException je) {
+ throw wrapException(je);
+ } catch (RepositoryException re) {
+ throw wrapException(re);
+ }
+ }
+
+ private String formatValue(Value value) {
+ String strValue = "";
+ try {
+ if (value != null) {
+ switch (value.getType()) {
+ case PropertyType.DATE: strValue = value.getDate().toString(); break;
+ case PropertyType.LONG: strValue = String.valueOf(value.getLong());break;
+ case PropertyType.DOUBLE: strValue = String.valueOf(value.getDouble()); break;
+ case PropertyType.BINARY: strValue = "[binary]"; break;
+ default:
+ strValue = value.getString();
+ }
+ }
+ } catch (RepositoryException re) {
+ // ignore
+ }
+ return strValue;
+ }
+
+ /**
+ * @param e
+ * @throws org.apache.sling.api.SlingException wrapping the given exception
+ */
+ private SlingException wrapException(Exception e) {
+ log.warn("Error in QueryServlet: " + e.toString(), e);
+ return new SlingException(e.toString(), e);
+ }
+}
Propchange: incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonQueryServlet.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonQueryServlet.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Modified: incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonRendererServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonRendererServlet.java?rev=633842&r1=633841&r2=633842&view=diff
==============================================================================
--- incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonRendererServlet.java (original)
+++ incubator/sling/trunk/sling/servlets-default/src/main/java/org/apache/sling/servlets/JsonRendererServlet.java Wed Mar 5 05:35:42 2008
@@ -69,7 +69,7 @@
private static final long serialVersionUID = 5577121546674133317L;
- private static final String responseContentType = "application/json";
+ static final String responseContentType = "application/json";
private final JsonItemWriter itemWriter;