001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.util;
019
020
021import java.net.URI;
022import java.net.URISyntaxException;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026
027import com.nimbusds.oauth2.sdk.ParseException;
028import net.minidev.json.JSONArray;
029import org.apache.commons.collections.CollectionUtils;
030
031
032/**
033 * JSON array helper methods for parsing and typed retrieval of values.
034 */
035public class JSONArrayUtils {
036
037
038        /**
039         * Parses a JSON array.
040         *
041         * <p>Specific JSON to Java entity mapping (as per JSON Simple):
042         *
043         * <ul>
044         *     <li>JSON numbers mapped to {@code java.lang.Number}.
045         *     <li>JSON integer numbers mapped to {@code long}.
046         *     <li>JSON fraction numbers mapped to {@code double}.
047         * </ul>
048         *
049         * @param s The JSON array string to parse. Must not be {@code null}.
050         *
051         * @return The JSON array.
052         *
053         * @throws ParseException If the string cannot be parsed to a JSON
054         *                        array.
055         */
056        public static JSONArray parse(final String s)
057                throws ParseException {
058
059                Object o = JSONUtils.parseJSON(s);
060
061                if (o instanceof JSONArray)
062                        return (JSONArray)o;
063                else
064                        throw new ParseException("The JSON entity is not an array");
065        }
066
067
068        /**
069         * Converts the specified JSON array to a string list.
070         *
071         * @param jsonArray The JSON array. May be {@code null}.
072         *
073         * @return The corresponding string list, empty list if the JSON array
074         *         is {@code null} or empty.
075         */
076        public static List<String> toStringList(final JSONArray jsonArray) {
077
078                if (CollectionUtils.isEmpty(jsonArray)) {
079                        return Collections.emptyList();
080                }
081
082                List<String> stringList = new ArrayList<>(jsonArray.size());
083
084                for (Object o: jsonArray) {
085
086                        if (o == null) {
087                                continue; // skip
088                        }
089
090                        stringList.add(o.toString());
091                }
092
093                return stringList;
094        }
095
096
097        /**
098         * Converts the specified JSON array to a URI list.
099         *
100         * @param jsonArray The JSON array. May be {@code null}.
101         *
102         * @return The corresponding URI list, empty list if the JSON array is
103         *         {@code null} or empty.
104         *
105         * @throws ParseException If a JSON array item couldn't be parsed to a
106         *                        URI.
107         */
108        public static List<URI> toURIList(final JSONArray jsonArray)
109                throws ParseException {
110
111                if (CollectionUtils.isEmpty(jsonArray)) {
112                        return Collections.emptyList();
113                }
114
115                List<URI> uriList = new ArrayList<>(jsonArray.size());
116
117                for (Object o: jsonArray) {
118
119                        if (o == null) {
120                                continue; // skip
121                        }
122
123                        try {
124                                uriList.add(new URI(o.toString()));
125                        } catch (URISyntaxException e) {
126                                throw new ParseException("Illegal URI: " + e.getMessage(), e);
127                        }
128                }
129
130                return uriList;
131        }
132}