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.openid.connect.sdk.id;
019
020
021import java.net.URI;
022
023import net.jcip.annotations.Immutable;
024
025import com.nimbusds.oauth2.sdk.id.Audience;
026import com.nimbusds.oauth2.sdk.id.Identifier;
027
028
029/**
030 * Sector identifier.
031 *
032 * <p>Related specifications:
033 *
034 * <ul>
035 *     <li>OpenID Connect Core 1.0, section 8.1.
036 * </ul>
037 */
038@Immutable
039public final class SectorID extends Identifier {
040        
041        
042        private static final long serialVersionUID = -3769967342420085584L;
043        
044        
045        /**
046         * Ensures the URI has a {@code https} scheme.
047         *
048         * @param sectorURI The URI. Must have a {@code https} scheme and not
049         *                  be {@code null}.
050         */
051        public static void ensureHTTPScheme(final URI sectorURI) {
052
053                if (! "https".equalsIgnoreCase(sectorURI.getScheme())) {
054                        throw new IllegalArgumentException("The URI must have a https scheme");
055                }
056        }
057
058
059        /**
060         * Ensures the URI contains a host component.
061         *
062         * @param sectorURI The URI. Must contain a host component and not be
063         *                  {@code null}.
064         *
065         * @return The host component.
066         */
067        public static String ensureHostComponent(final URI sectorURI) {
068
069                String host = sectorURI.getHost();
070
071                if (host == null) {
072                        throw new IllegalArgumentException("The URI must contain a host component");
073                }
074
075                return host;
076        }
077        
078
079        /**
080         * Creates a new sector identifier based on a internet host.
081         *
082         * <p>Example host: client.example.com
083         *
084         * @param host The host. The value is not validated. Must not be empty
085         *             or {@code null}.
086         */
087        public SectorID(final String host) {
088                super(host);
089        }
090
091
092        /**
093         * Creates a new sector identifier based on the host component of an
094         * URI.
095         *
096         * <p>Example URI: https://client.example.com
097         *
098         * @param uri The URI. Must contain a host component and must not be
099         *            {@code null}.
100         */
101        public SectorID(final URI uri) {
102                super(ensureHostComponent(uri));
103        }
104        
105        
106        /**
107         * Creates a new sector identifier based on an audience.
108         *
109         * @param audience The audience. Must not be empty or {@code null}.
110         */
111        public SectorID(final Audience audience) {
112                super(audience.getValue());
113        }
114        
115        
116        /**
117         * Creates a new sector identifier based on a generic identifier.
118         *
119         * @param identifier The identifier. Must not be empty or {@code null}.
120         */
121        public SectorID(final Identifier identifier) {
122                super(identifier.getValue());
123        }
124}