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.federation.entities;
019
020
021import java.net.URI;
022import java.net.URISyntaxException;
023
024import net.jcip.annotations.Immutable;
025
026import com.nimbusds.oauth2.sdk.ParseException;
027import com.nimbusds.oauth2.sdk.id.Identifier;
028import com.nimbusds.oauth2.sdk.id.Issuer;
029import com.nimbusds.oauth2.sdk.id.Subject;
030import com.nimbusds.oauth2.sdk.util.StringUtils;
031
032
033/**
034 * Federation entity identifier.
035 *
036 * <p>Related specifications:
037 *
038 * <ul>
039 *     <li>OpenID Connect Federation 1.0, section 1.2.
040 * </ul>
041 */
042@Immutable
043public final class EntityID extends Identifier {
044        
045        
046        /**
047         * Creates a new entity identifier from the specified URI.
048         *
049         * @param value The URI. Must not be {@code null}.
050         */
051        public EntityID(final URI value) {
052                this(value.toString());
053        }
054        
055        
056        /**
057         * Creates a new entity identifier from the specified issuer
058         * identifier.
059         *
060         * @param issuer The issuer. Must represent an URI and must not be
061         *               {@code null}.
062         */
063        public EntityID(final Issuer issuer) {
064                this(issuer.getValue());
065        }
066        
067        
068        /**
069         * Creates a new entity identifier from the specified subject
070         * identifier.
071         *
072         * @param subject The subject. Must represent an URI and must not be
073         *                {@code null}.
074         */
075        public EntityID(final Subject subject) {
076                this(subject.getValue());
077        }
078        
079        
080        /**
081         * Creates a new entity identifier with the specified value.
082         *
083         * @param value The identifier value. Must represent an URI and must
084         *              not be {@code null}.
085         */
086        public EntityID(final String value) {
087                super(value);
088                
089                URI uri;
090                try {
091                        uri = new URI(value);
092                } catch (URISyntaxException e) {
093                        throw new IllegalArgumentException("The entity ID must be an URI: " + e.getMessage(), e);
094                }
095                
096                if (! "https".equalsIgnoreCase(uri.getScheme()) && ! "http".equalsIgnoreCase(uri.getScheme())) {
097                        throw new IllegalArgumentException("The entity ID must be an URI with https or http scheme");
098                }
099                
100                if (StringUtils.isBlank(uri.getAuthority())) {
101                        throw new IllegalArgumentException("The entity ID must be an URI with authority (hostname)");
102                }
103        }
104        
105        
106        /**
107         * Returns the entity identifier as an URI.
108         *
109         * @return The entity identifier URI.
110         */
111        public URI toURI() {
112                return URI.create(getValue());
113        }
114        
115        
116        @Override
117        public boolean equals(final Object object) {
118                
119                return object instanceof EntityID &&
120                        this.toString().equals(object.toString());
121        }
122        
123        
124        /**
125         * Parses an entity ID from the specified string.
126         *
127         * @param value The string value. Must not be {@code null}.
128         *
129         * @return The entity ID.
130         *
131         * @throws ParseException On a illegal entity ID.
132         */
133        public static EntityID parse(final String value)
134                throws ParseException {
135                try {
136                        return new EntityID(value);
137                } catch (IllegalArgumentException e) {
138                        throw new ParseException(e.getMessage());
139                }
140        }
141        
142        
143        /**
144         * Parses an entity ID from the specified issuer.
145         *
146         * @param issuer The issuer. Must not be {@code null}.
147         *
148         * @return The entity ID.
149         *
150         * @throws ParseException On a illegal entity ID.
151         */
152        public static EntityID parse(final Issuer issuer)
153                throws ParseException {
154                return parse(issuer.getValue());
155        }
156        
157        
158        /**
159         * Parses an entity ID from the specified subject.
160         *
161         * @param subject The subject. Must not be {@code null}.
162         *
163         * @return The entity ID.
164         *
165         * @throws ParseException On a illegal entity ID.
166         */
167        public static EntityID parse(final Subject subject)
168                throws ParseException {
169                return parse(subject.getValue());
170        }
171}