Author: fmeschbe
Date: Fri Jun 12 21:22:05 2009
New Revision: 784278
URL: http://svn.apache.org/viewvc?rev=784278&view=rev
Log:
SLING-1006 Return multi-value properties correctly
Modified:
incubator/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
Modified:
incubator/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java?rev=784278&r1=784277&r2=784278&view=diff
==============================================================================
---
incubator/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
(original)
+++
incubator/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java
Fri Jun 12 21:22:05 2009
@@ -20,13 +20,12 @@
import java.io.IOException;
import java.io.InputStream;
+import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import javax.jcr.RepositoryException;
-import javax.jcr.Value;
import javax.jcr.query.Query;
import org.apache.sling.api.SlingException;
@@ -35,10 +34,10 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
-import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -58,6 +57,9 @@
* @scr.property name="sling.servlet.selectors" value="query"
*/
public class JsonQueryServlet extends SlingSafeMethodsServlet {
+
+ private static final long serialVersionUID = 1L;
+
private final Logger log = LoggerFactory.getLogger(JsonQueryServlet.class);
/** Search clause */
@@ -183,35 +185,33 @@
return;
}
-
- ResourceResolver resolver = nodeRes.getResourceResolver();
+ // get the properties of the resource, nothing todo if none
+ ValueMap props = nodeRes.adaptTo(ValueMap.class);
+ if (props == null) {
+ return;
+ }
+
+ // get the actual properties now into the JSON output
for (String property : properties) {
- Resource prop = resolver.getResource(nodeRes, property);
- if (prop != null) {
- String strValue;
- Value value = prop.adaptTo(Value.class);
- if (value != null) {
- strValue = formatValue(value);
- } else {
- strValue = prop.adaptTo(String.class);
- if (strValue == null) {
- strValue = "";
- }
- }
+ Object value = props.get(property);
+ if (value != null) {
w.key(property);
- w.value(strValue);
- }
- }
- }
+ if (value.getClass().isArray()) {
- private String formatValue(Value value) {
- try {
- return formatValue(JcrResourceUtil.toJavaObject(value));
- } catch (RepositoryException re) {
- // might log
+ w.array();
+ for (int i = 0; i < Array.getLength(value); i++) {
+ w.value(formatValue(Array.get(value, i)));
+ }
+ w.endArray();
+
+ } else {
+
+ w.value(formatValue(value));
+
+ }
+ }
}
- return "";
}
private String formatValue(Object value) {