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.ClientID;
028import com.nimbusds.oauth2.sdk.id.Identifier;
029import com.nimbusds.oauth2.sdk.id.Issuer;
030import com.nimbusds.oauth2.sdk.id.Subject;
031import com.nimbusds.oauth2.sdk.util.StringUtils;
032
033
034/**
035 * Federation entity identifier.
036 *
037 * <p>Related specifications:
038 *
039 * <ul>
040 *     <li>OpenID Connect Federation 1.0, section 1.2.
041 * </ul>
042 */
043@Immutable
044public final class EntityID extends Identifier {
045        
046        
047        private static final long serialVersionUID = -2884746939238001871L;
048        
049        
050        /**
051         * Creates a new entity identifier from the specified URI.
052         *
053         * @param value The URI. Must not be {@code null}.
054         *
055         * @throws IllegalArgumentException On a illegal entity ID.
056         */
057        public EntityID(final URI value) {
058                this(value.toString());
059        }
060        
061        
062        /**
063         * Creates a new entity identifier from the specified issuer
064         * identifier.
065         *
066         * @param issuer The issuer. Must represent an URI and must not be
067         *               {@code null}.
068         *
069         * @throws IllegalArgumentException On a illegal entity ID.
070         */
071        public EntityID(final Issuer issuer) {
072                this(issuer.getValue());
073        }
074        
075        
076        /**
077         * Creates a new entity identifier from the specified subject
078         * identifier.
079         *
080         * @param subject The subject. Must represent an URI and must not be
081         *                {@code null}.
082         *
083         * @throws IllegalArgumentException On a illegal entity ID.
084         */
085        public EntityID(final Subject subject) {
086                this(subject.getValue());
087        }
088        
089        
090        /**
091         * Creates a new entity identifier from the specified client
092         * identifier.
093         *
094         * @param clientID The client ID. Must represent an URI and must not be
095         *                 {@code null}.
096         */
097        public EntityID(final ClientID clientID) {
098                this(clientID.getValue());
099        }
100        
101        
102        /**
103         * Creates a new entity identifier with the specified value.
104         *
105         * @param value The identifier value. Must represent an URI and must
106         *              not be {@code null}.
107         *
108         * @throws IllegalArgumentException On a illegal entity ID.
109         */
110        public EntityID(final String value) {
111                super(value);
112                
113                URI uri;
114                try {
115                        uri = new URI(value);
116                } catch (URISyntaxException e) {
117                        throw new IllegalArgumentException("The entity ID must be an URI: " + e.getMessage(), e);
118                }
119                
120                if (! "https".equalsIgnoreCase(uri.getScheme()) && ! "http".equalsIgnoreCase(uri.getScheme())) {
121                        throw new IllegalArgumentException("The entity ID must be an URI with https or http scheme");
122                }
123                
124                if (StringUtils.isBlank(uri.getAuthority())) {
125                        throw new IllegalArgumentException("The entity ID must be an URI with authority (hostname)");
126                }
127        }
128        
129        
130        /**
131         * Returns the entity identifier as an URI.
132         *
133         * @return The entity identifier URI.
134         */
135        public URI toURI() {
136                return URI.create(getValue());
137        }
138        
139        /**
140         * Returns the entity identifier as an issuer.
141         *
142         * @return The issuer.
143         */
144        public Issuer toIssuer() {
145                return new Issuer(getValue());
146        }
147        
148        
149        /**
150         * Returns the entity identifier as a subject.
151         *
152         * @return The subject.
153         */
154        public Subject toSubject() {
155                return new Subject(getValue());
156        }
157        
158        
159        /**
160         * Returns the entity identifier as a client ID.
161         *
162         * @return The client ID.
163         */
164        public ClientID toClientID() {
165                return new ClientID(getValue());
166        }
167        
168        
169        @Override
170        public boolean equals(final Object object) {
171                
172                return object instanceof EntityID &&
173                        this.toString().equals(object.toString());
174        }
175        
176        
177        /**
178         * Parses an entity ID from the specified string.
179         *
180         * @param value The string value. Must not be {@code null}.
181         *
182         * @return The entity ID.
183         *
184         * @throws ParseException On a illegal entity ID.
185         */
186        public static EntityID parse(final String value)
187                throws ParseException {
188                try {
189                        return new EntityID(value);
190                } catch (IllegalArgumentException e) {
191                        throw new ParseException(e.getMessage());
192                }
193        }
194        
195        
196        /**
197         * Parses an entity ID from the specified issuer.
198         *
199         * @param issuer The issuer. Must not be {@code null}.
200         *
201         * @return The entity ID.
202         *
203         * @throws ParseException On a illegal entity ID.
204         */
205        public static EntityID parse(final Issuer issuer)
206                throws ParseException {
207                return parse(issuer.getValue());
208        }
209        
210        
211        /**
212         * Parses an entity ID from the specified subject.
213         *
214         * @param subject The subject. Must not be {@code null}.
215         *
216         * @return The entity ID.
217         *
218         * @throws ParseException On a illegal entity ID.
219         */
220        public static EntityID parse(final Subject subject)
221                throws ParseException {
222                return parse(subject.getValue());
223        }
224        
225        
226        /**
227         * Parses an entity ID from the specified client ID.
228         *
229         * @param clientID The client ID. Must not be {@code null}.
230         *
231         * @return The entity ID.
232         *
233         * @throws ParseException On a illegal entity ID.
234         */
235        public static EntityID parse(final ClientID clientID)
236                throws ParseException {
237                return parse(clientID.getValue());
238        }
239}