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.Collections;
024import java.util.LinkedList;
025import java.util.List;
026
027import com.nimbusds.oauth2.sdk.ParseException;
028
029
030/**
031 * Resource server URI utilities.
032 */
033public final class ResourceUtils {
034        
035        
036        /**
037         * Returns {@code true} if the specified resource URI is valid.
038         *
039         * @param resourceURI The resource URI. Must not be {@code null}.
040         *
041         * @return {@code true} if the resource URI is valid, {@code false} if
042         *         the URI is not absolute or has a fragment.
043         *
044         * @deprecated Use {@link #isLegalResourceURI} instead.
045         */
046        @Deprecated
047        public static boolean isValidResourceURI(final URI resourceURI) {
048                
049                return isLegalResourceURI(resourceURI);
050        }
051        
052        
053        /**
054         * Returns {@code true} if the specified resource URI is legal.
055         *
056         * @param resourceURI The resource URI, {@code null} if not specified.
057         *
058         * @return {@code true} if the resource URI is legal or {@code null},
059         *         {@code false} if the URI is not absolute or has a fragment.
060         */
061        public static boolean isLegalResourceURI(final URI resourceURI) {
062                
063                return resourceURI == null || (resourceURI.isAbsolute() && resourceURI.getFragment() == null);
064        }
065        
066        
067        /**
068         * Ensures the specified resource URIs are legal.
069         *
070         * @param resourceURIs The resource URIs, {@code null} if not
071         *                     specified.
072         *
073         * @return The checked resource URIs, {@code null} if not specified.
074         *
075         * @throws IllegalArgumentException If the resource URIs are not legal
076         *                                  according to
077         *                                  {@link #isLegalResourceURI}.
078         */
079        public static List<URI> ensureLegalResourceURIs(final List<URI> resourceURIs) {
080                
081                if (CollectionUtils.isEmpty(resourceURIs))
082                        return resourceURIs;
083                
084                for (URI resourceURI: resourceURIs) {
085                        
086                        if (resourceURI == null)
087                                continue; // skip
088                        
089                        if (! ResourceUtils.isValidResourceURI(resourceURI))
090                                throw new IllegalArgumentException("Resource URI must be absolute and without a fragment: " + resourceURI);
091                }
092                
093                return resourceURIs;
094        }
095        
096        
097        /**
098         * Parses a list of resource URIs from the specified string list.
099         *
100         * @param stringList The string list, {@code null} if not specified.
101         *
102         * @return The resource URIs, {@code null} if not specified.
103         *
104         * @throws ParseException If parsing failed.
105         */
106        public static List<URI> parseResourceURIs(final List<String> stringList)
107                throws ParseException {
108                
109                if (CollectionUtils.isEmpty(stringList)) {
110                        return null;
111                }
112                
113                List<URI> resources = new LinkedList<>();
114                
115                for (String uriValue: stringList) {
116                        
117                        if (uriValue == null)
118                                continue;
119                        
120                        String errMsg = "Illegal resource parameter: Must be an absolute URI and with no query or fragment";
121                        
122                        URI resourceURI;
123                        try {
124                                resourceURI = new URI(uriValue);
125                        } catch (URISyntaxException e) {
126                                throw new ParseException(errMsg);
127                        }
128                        
129                        if (! ResourceUtils.isLegalResourceURI(resourceURI)) {
130                                throw new ParseException(errMsg);
131                        }
132                        
133                        resources.add(resourceURI);
134                }
135                
136                return Collections.unmodifiableList(resources);
137        }
138        
139        
140        /**
141         * Prevents public instantiation.
142         */
143        private ResourceUtils() {}
144}