You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2014/07/06 13:10:42 UTC

git commit: Document to json @Provider mainly dedicated to wadls (not defined behavior in other cases)

Repository: incubator-fleece
Updated Branches:
  refs/heads/master 5e4bb9431 -> c6f890faf


Document to json @Provider mainly dedicated to wadls (not defined behavior in other cases)


Project: http://git-wip-us.apache.org/repos/asf/incubator-fleece/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-fleece/commit/c6f890fa
Tree: http://git-wip-us.apache.org/repos/asf/incubator-fleece/tree/c6f890fa
Diff: http://git-wip-us.apache.org/repos/asf/incubator-fleece/diff/c6f890fa

Branch: refs/heads/master
Commit: c6f890faf1c7f6d472453618d7f440e34604d1ee
Parents: 5e4bb94
Author: Romain Manni-Bucau <rm...@gmail.com>
Authored: Sun Jul 6 13:09:50 2014 +0200
Committer: Romain Manni-Bucau <rm...@gmail.com>
Committed: Sun Jul 6 13:09:50 2014 +0200

----------------------------------------------------------------------
 .../jaxrs/WadlDocumentMessageBodyWriter.java    | 63 +++++++++++++
 .../fleece/jaxrs/xml/WadlDocumentToJson.java    | 98 ++++++++++++++++++++
 .../jaxrs/xml/WadlDocumentToJsonTest.java       | 61 ++++++++++++
 fleece-jaxrs/src/test/resources/wadl.json       | 76 +++++++++++++++
 4 files changed, 298 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-fleece/blob/c6f890fa/fleece-jaxrs/src/main/java/org/apache/fleece/jaxrs/WadlDocumentMessageBodyWriter.java
----------------------------------------------------------------------
diff --git a/fleece-jaxrs/src/main/java/org/apache/fleece/jaxrs/WadlDocumentMessageBodyWriter.java b/fleece-jaxrs/src/main/java/org/apache/fleece/jaxrs/WadlDocumentMessageBodyWriter.java
new file mode 100644
index 0000000..55f1dd3
--- /dev/null
+++ b/fleece-jaxrs/src/main/java/org/apache/fleece/jaxrs/WadlDocumentMessageBodyWriter.java
@@ -0,0 +1,63 @@
+/*
+ * 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.fleece.jaxrs;
+
+import org.apache.fleece.jaxrs.xml.WadlDocumentToJson;
+import org.w3c.dom.Document;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.xml.stream.XMLStreamException;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import static org.apache.fleece.jaxrs.Jsons.isJson;
+
+public class WadlDocumentMessageBodyWriter implements MessageBodyWriter<Document> {
+    private final WadlDocumentToJson converter = new WadlDocumentToJson();
+
+    @Override
+    public boolean isWriteable(final Class<?> aClass, final Type type,
+                               final Annotation[] annotations, final MediaType mediaType) {
+        return isJson(mediaType) && Document.class.isAssignableFrom(aClass);
+    }
+
+    @Override
+    public long getSize(final Document document, final Class<?> aClass,
+                        final Type type, final Annotation[] annotations,
+                        final MediaType mediaType) {
+        return -1;
+    }
+
+    @Override
+    public void writeTo(final Document document, final Class<?> aClass,
+                        final Type type, final Annotation[] annotations,
+                        final MediaType mediaType, final MultivaluedMap<String, Object> stringObjectMultivaluedMap,
+                        final OutputStream outputStream) throws IOException, WebApplicationException {
+        try {
+            outputStream.write(converter.convert(document).getBytes());
+        } catch (final XMLStreamException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-fleece/blob/c6f890fa/fleece-jaxrs/src/main/java/org/apache/fleece/jaxrs/xml/WadlDocumentToJson.java
----------------------------------------------------------------------
diff --git a/fleece-jaxrs/src/main/java/org/apache/fleece/jaxrs/xml/WadlDocumentToJson.java b/fleece-jaxrs/src/main/java/org/apache/fleece/jaxrs/xml/WadlDocumentToJson.java
new file mode 100644
index 0000000..a580e75
--- /dev/null
+++ b/fleece-jaxrs/src/main/java/org/apache/fleece/jaxrs/xml/WadlDocumentToJson.java
@@ -0,0 +1,98 @@
+/*
+ * 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.fleece.jaxrs.xml;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import javax.json.Json;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonBuilderFactory;
+import javax.json.JsonObjectBuilder;
+import javax.xml.stream.XMLStreamException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+
+public class WadlDocumentToJson {
+    private final JsonBuilderFactory builderFactory = Json.createBuilderFactory(Collections.<String, Object>emptyMap());
+
+    public String convert(final Document doc) throws XMLStreamException {
+        final JsonObjectBuilder builder = builderFactory.createObjectBuilder();
+        if (doc.getChildNodes().getLength() != 1) {
+            return "{}";
+        }
+        final Node item = doc.getChildNodes().item(0);
+        return builder.add(item.getNodeName(), createNode(item)).build().toString();
+    }
+
+    private void addChildrens(final String nodeName, final JsonObjectBuilder builder, final NodeList children) {
+        final Map<String, Collection<Node>> nodesByName = new LinkedHashMap<String, Collection<Node>>();
+        for (int i = 0; i < children.getLength(); i++) {
+            final Node node = children.item(i);
+            if ("#text".equals(node.getNodeName())) {
+                continue;
+            }
+
+            final String name = node.getNodeName();
+            Collection<Node> nodes = nodesByName.get(name);
+            if (nodes == null) {
+                nodes = new LinkedList<Node>();
+                nodesByName.put(name, nodes);
+            }
+            nodes.add(node);
+        }
+
+        for (final Map.Entry<String, Collection<Node>> entry : nodesByName.entrySet()) {
+            final JsonArrayBuilder arrayBuilder = builderFactory.createArrayBuilder();
+            for (final Node n : entry.getValue()) {
+                final JsonObjectBuilder jsonObjectBuilder = createNode(n);
+                if (jsonObjectBuilder != null) {
+                    arrayBuilder.add(jsonObjectBuilder);
+                }
+            }
+            builder.add(entry.getKey(), arrayBuilder);
+        }
+    }
+
+    private JsonObjectBuilder createNode(final Node node) {
+        JsonObjectBuilder childBuilder = null;
+
+        if (node.hasAttributes()) {
+            childBuilder = builderFactory.createObjectBuilder();
+            final NamedNodeMap attributes = node.getAttributes();
+            for (int j = 0; j < attributes.getLength(); j++) {
+                final Node namedItem = attributes.item(j);
+                childBuilder.add(namedItem.getNodeName(), namedItem.getNodeValue());
+            }
+        }
+
+        if (node.hasChildNodes()) {
+            if (childBuilder == null) {
+                childBuilder = builderFactory.createObjectBuilder();
+            }
+            addChildrens(node.getNodeName(), childBuilder, node.getChildNodes());
+        }
+        return childBuilder;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-fleece/blob/c6f890fa/fleece-jaxrs/src/test/java/org/apache/fleece/jaxrs/xml/WadlDocumentToJsonTest.java
----------------------------------------------------------------------
diff --git a/fleece-jaxrs/src/test/java/org/apache/fleece/jaxrs/xml/WadlDocumentToJsonTest.java b/fleece-jaxrs/src/test/java/org/apache/fleece/jaxrs/xml/WadlDocumentToJsonTest.java
new file mode 100644
index 0000000..efab474
--- /dev/null
+++ b/fleece-jaxrs/src/test/java/org/apache/fleece/jaxrs/xml/WadlDocumentToJsonTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.fleece.jaxrs.xml;
+
+import org.junit.Test;
+import org.w3c.dom.Document;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.ByteArrayInputStream;
+
+import static org.junit.Assert.assertEquals;
+
+public class WadlDocumentToJsonTest {
+    @Test
+    public void xmlToJson() throws Exception {
+        final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
+            .parse(new ByteArrayInputStream(("" +
+                    "<application xmlns=\"http://wadl.dev.java.net/2009/02\">\n" +
+                    "    <resources base=\"http://example.com/api\">\n" +
+                    "        <resource path=\"books\">\n" +
+                    "            <method name=\"GET\"/>\n" +
+                    "            <resource path=\"{bookId}\">\n" +
+                    "                <param required=\"true\" style=\"template\" name=\"bookId\"/>\n" +
+                    "                <method name=\"GET\"/>\n" +
+                    "                <method name=\"DELETE\"/>\n" +
+                    "                <resource path=\"reviews\">\n" +
+                    "                    <method name=\"GET\">\n" +
+                    "                        <request>\n" +
+                    "                            <param name=\"page\" required=\"false\" default=\"1\" style=\"query\"/>\n" +
+                    "                            <param name=\"size\" required=\"false\" default=\"20\" style=\"query\"/>\n" +
+                    "                        </request>\n" +
+                    "                    </method>\n" +
+                    "                </resource>\n" +
+                    "            </resource>\n" +
+                    "        </resource>\n" +
+                    "        <resource path=\"readers\">\n" +
+                    "            <method name=\"GET\"/>\n" +
+                    "        </resource>\n" +
+                    "    </resources>\n" +
+                    "</application>").getBytes()));
+
+        final String json = new WadlDocumentToJson().convert(doc);
+        assertEquals("{\"application\":{\"xmlns\":\"http://wadl.dev.java.net/2009/02\",\"resources\":[{\"base\":\"http://example.com/api\",\"resource\":[{\"path\":\"books\",\"method\":[{\"name\":\"GET\"}],\"resource\":[{\"path\":\"{bookId}\",\"param\":[{\"name\":\"bookId\",\"required\":\"true\",\"style\":\"template\"}],\"method\":[{\"name\":\"GET\"},{\"name\":\"DELETE\"}],\"resource\":[{\"path\":\"reviews\",\"method\":[{\"name\":\"GET\",\"request\":[{\"param\":[{\"default\":\"1\",\"name\":\"page\",\"required\":\"false\",\"style\":\"query\"},{\"default\":\"20\",\"name\":\"size\",\"required\":\"false\",\"style\":\"query\"}]}]}]}]}]},{\"path\":\"readers\",\"method\":[{\"name\":\"GET\"}]}]}]}}", json);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-fleece/blob/c6f890fa/fleece-jaxrs/src/test/resources/wadl.json
----------------------------------------------------------------------
diff --git a/fleece-jaxrs/src/test/resources/wadl.json b/fleece-jaxrs/src/test/resources/wadl.json
new file mode 100644
index 0000000..93abd8f
--- /dev/null
+++ b/fleece-jaxrs/src/test/resources/wadl.json
@@ -0,0 +1,76 @@
+{
+    "application": {
+        "xmlns": "http://wadl.dev.java.net/2009/02",
+        "resources": [
+            {
+                "base": "http://example.com/api",
+                "resource": [
+                    {
+                        "path": "books",
+                        "method": [
+                            {
+                                "name": "GET"
+                            }
+                        ],
+                        "resource": [
+                            {
+                                "path": "{bookId}",
+                                "method": [
+                                    {
+                                        "name": "GET"
+                                    },
+                                    {
+                                        "name": "DELETE"
+                                    }
+                                ],
+                                "param": [
+                                    {
+                                        "name": "bookId",
+                                        "required": "true",
+                                        "style": "template"
+                                    }
+                                ],
+                                "resource": [
+                                    {
+                                        "path": "reviews",
+                                        "method": [
+                                            {
+                                                "name": "GET",
+                                                "request": [
+                                                    {
+                                                        "param": [
+                                                            {
+                                                                "default": "1",
+                                                                "name": "page",
+                                                                "required": "false",
+                                                                "style": "query"
+                                                            },
+                                                            {
+                                                                "default": "20",
+                                                                "name": "size",
+                                                                "required": "false",
+                                                                "style": "query"
+                                                            }
+                                                        ]
+                                                    }
+                                                ]
+                                            }
+                                        ]
+                                    }
+                                ]
+                            }
+                        ]
+                    },
+                    {
+                        "path": "readers",
+                        "method": [
+                            {
+                                "name": "GET"
+                            }
+                        ]
+                    }
+                ]
+            }
+        ]
+    }
+}
\ No newline at end of file