001/*******************************************************************************
002 * Copyright 2014 The MITRE Corporation
003 *   and the MIT Kerberos and Internet Trust Consortium
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *   http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *******************************************************************************/
017/**
018 * 
019 */
020package org.mitre.discovery.util;
021
022import java.util.ArrayList;
023import java.util.Date;
024import java.util.List;
025import java.util.Set;
026
027import com.google.common.collect.Lists;
028import com.google.common.collect.Sets;
029import com.google.gson.Gson;
030import com.google.gson.JsonElement;
031import com.google.gson.JsonObject;
032import com.google.gson.JsonSyntaxException;
033import com.google.gson.reflect.TypeToken;
034import com.nimbusds.jose.EncryptionMethod;
035import com.nimbusds.jose.JWEAlgorithm;
036import com.nimbusds.jose.JWSAlgorithm;
037
038/**
039 * A collection of null-safe converters from common classes and JSON elements, using GSON.
040 * 
041 * @author jricher
042 *
043 */
044public class JsonUtils {
045
046        private static Gson gson = new Gson();
047
048        /**
049         * Translate a set of strings to a JSON array
050         * @param value
051         * @return
052         */
053        public static JsonElement getAsArray(Set<String> value) {
054                return gson.toJsonTree(value, new TypeToken<Set<String>>(){}.getType());
055        }
056
057        /**
058         * Gets the value of the given member (expressed as integer seconds since epoch) as a Date
059         */
060        public static Date getAsDate(JsonObject o, String member) {
061                if (o.has(member)) {
062                        JsonElement e = o.get(member);
063                        if (e != null && e.isJsonPrimitive()) {
064                                return new Date(e.getAsInt() * 1000L);
065                        } else {
066                                return null;
067                        }
068                } else {
069                        return null;
070                }
071        }
072
073        /**
074         * Gets the value of the given member as a JWE Algorithm, null if it doesn't exist
075         */
076        public static JWEAlgorithm getAsJweAlgorithm(JsonObject o, String member) {
077                String s = getAsString(o, member);
078                if (s != null) {
079                        return JWEAlgorithm.parse(s);
080                } else {
081                        return null;
082                }
083        }
084
085        /**
086         * Gets the value of the given member as a JWE Encryption Method, null if it doesn't exist
087         */
088        public static EncryptionMethod getAsJweEncryptionMethod(JsonObject o, String member) {
089                String s = getAsString(o, member);
090                if (s != null) {
091                        return EncryptionMethod.parse(s);
092                } else {
093                        return null;
094                }
095        }
096
097        /**
098         * Gets the value of the given member as a JWS Algorithm, null if it doesn't exist
099         */
100        public static JWSAlgorithm getAsJwsAlgorithm(JsonObject o, String member) {
101                String s = getAsString(o, member);
102                if (s != null) {
103                        return JWSAlgorithm.parse(s);
104                } else {
105                        return null;
106                }
107        }
108
109        /**
110         * Gets the value of the given member as a string, null if it doesn't exist
111         */
112        public static String getAsString(JsonObject o, String member) {
113                if (o.has(member)) {
114                        JsonElement e = o.get(member);
115                        if (e != null && e.isJsonPrimitive()) {
116                                return e.getAsString();
117                        } else {
118                                return null;
119                        }
120                } else {
121                        return null;
122                }
123        }
124
125        /**
126         * Gets the value of the given member as a boolean, null if it doesn't exist
127         */
128        public static Boolean getAsBoolean(JsonObject o, String member) {
129                if (o.has(member)) {
130                        JsonElement e = o.get(member);
131                        if (e != null && e.isJsonPrimitive()) {
132                                return e.getAsBoolean();
133                        } else {
134                                return null;
135                        }
136                } else {
137                        return null;
138                }
139        }
140
141        /**
142         * Gets the value of the given given member as a set of strings, null if it doesn't exist
143         */
144        public static Set<String> getAsStringSet(JsonObject o, String member) throws JsonSyntaxException {
145                if (o.has(member)) {
146                        if (o.get(member).isJsonArray()) {
147                                return gson.fromJson(o.get(member), new TypeToken<Set<String>>(){}.getType());
148                        } else {
149                                return Sets.newHashSet(o.get(member).getAsString());
150                        }
151                } else {
152                        return null;
153                }
154        }
155
156        /**
157         * Gets the value of the given given member as a set of strings, null if it doesn't exist
158         */
159        public static List<String> getAsStringList(JsonObject o, String member) throws JsonSyntaxException {
160                if (o.has(member)) {
161                        if (o.get(member).isJsonArray()) {
162                                return gson.fromJson(o.get(member), new TypeToken<List<String>>(){}.getType());
163                        } else {
164                                return Lists.newArrayList(o.get(member).getAsString());
165                        }
166                } else {
167                        return null;
168                }
169        }
170
171        /**
172         * Gets the value of the given member as a list of JWS Algorithms, null if it doesn't exist
173         */
174        public static List<JWSAlgorithm> getAsJwsAlgorithmList(JsonObject o, String member) {
175                List<String> strings = getAsStringList(o, member);
176                if (strings != null) {
177                        List<JWSAlgorithm> algs = new ArrayList<JWSAlgorithm>();
178                        for (String alg : strings) {
179                                algs.add(JWSAlgorithm.parse(alg));
180                        }
181                        return algs;
182                } else {
183                        return null;
184                }
185        }
186
187        /**
188         * Gets the value of the given member as a list of JWS Algorithms, null if it doesn't exist
189         */
190        public static List<JWEAlgorithm> getAsJweAlgorithmList(JsonObject o, String member) {
191                List<String> strings = getAsStringList(o, member);
192                if (strings != null) {
193                        List<JWEAlgorithm> algs = new ArrayList<JWEAlgorithm>();
194                        for (String alg : strings) {
195                                algs.add(JWEAlgorithm.parse(alg));
196                        }
197                        return algs;
198                } else {
199                        return null;
200                }
201        }
202
203        /**
204         * Gets the value of the given member as a list of JWS Algorithms, null if it doesn't exist
205         */
206        public static List<EncryptionMethod> getAsEncryptionMethodList(JsonObject o, String member) {
207                List<String> strings = getAsStringList(o, member);
208                if (strings != null) {
209                        List<EncryptionMethod> algs = new ArrayList<EncryptionMethod>();
210                        for (String alg : strings) {
211                                algs.add(EncryptionMethod.parse(alg));
212                        }
213                        return algs;
214                } else {
215                        return null;
216                }
217        }
218
219}