001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *
010     *        http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License.
018     */
019    package org.apache.isis.viewer.restfulobjects.applib.util;
020    
021    import java.io.ByteArrayInputStream;
022    import java.io.InputStream;
023    import java.util.List;
024    
025    import com.google.common.base.Charsets;
026    
027    import org.codehaus.jackson.JsonNode;
028    import org.codehaus.jackson.node.JsonNodeFactory;
029    import org.codehaus.jackson.node.ObjectNode;
030    
031    public class JsonNodeUtils {
032    
033        private JsonNodeUtils() {
034        }
035    
036        public static InputStream asInputStream(final JsonNode jsonNode) {
037            final String jsonStr = jsonNode.toString();
038            final byte[] bytes = jsonStr.getBytes(Charsets.UTF_8);
039            return new ByteArrayInputStream(bytes);
040        }
041    
042        /**
043         * Walks the path, ensuring keys exist and are maps, or creating required
044         * maps as it goes.
045         * 
046         * <p>
047         * For example, if given a list ("a", "b", "c") and starting with an empty
048         * map, then will create:
049         * 
050         * <pre>
051         * {
052         *   "a": {
053         *     "b: {
054         *       "c": {
055         *       }       
056         *     }
057         *   }
058         * }
059         */
060        public static ObjectNode walkNodeUpTo(ObjectNode node, final List<String> keys) {
061            for (final String key : keys) {
062                JsonNode jsonNode = node.get(key);
063                if (jsonNode == null) {
064                    jsonNode = new ObjectNode(JsonNodeFactory.instance);
065                    node.put(key, jsonNode);
066                } else {
067                    if (!jsonNode.isObject()) {
068                        throw new IllegalArgumentException(String.format("walking path: '%s', existing key '%s' is not a map", keys, key));
069                    }
070                }
071                node = (ObjectNode) jsonNode;
072            }
073            return node;
074        }
075    
076    }