001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2020, 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.trust.constraints;
019
020
021import com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.openid.connect.sdk.federation.entities.EntityID;
023
024
025/**
026 * Entity ID constraint.
027 *
028 * <p>Related specifications:
029 *
030 * <ul>
031 *     <li>OpenID Connect Federation 1.0, section 7.3.
032 *     <li>RFC 5280, section 4.2.1.10.
033 * </ul>
034 */
035public abstract class EntityIDConstraint {
036        
037        
038        /**
039         * Matches an entity ID with this constraint.
040         *
041         * @param entityID The entity ID to match. Must not be {@code null}.
042         *
043         * @return {@code true} if this constraint matches the specified entity
044         *         ID, else {@code false}.
045         */
046        public abstract boolean matches(final EntityID entityID);
047        
048        
049        @Override
050        public abstract String toString();
051        
052        
053        @Override
054        public abstract boolean equals(final Object other);
055        
056        
057        /**
058         * Parses an entity ID constraint.
059         *
060         * @param value The string value.
061         *
062         * @return The parsed {@link ExactMatchEntityIDConstraint} or
063         *         {@link SubtreeEntityIDConstraint}.
064         *
065         * @throws ParseException If parsing failed.
066         */
067        public static EntityIDConstraint parse(final String value)
068                throws ParseException {
069                
070                try {
071                        return new SubtreeEntityIDConstraint(value);
072                } catch (IllegalArgumentException e) {
073                        try {
074                                return new ExactMatchEntityIDConstraint(new EntityID(value));
075                        } catch (IllegalArgumentException e2) {
076                                throw new ParseException(e2.getMessage(), e2);
077                        }
078                }
079        }
080}