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.id;
019
020
021import java.net.URI;
022import java.net.URISyntaxException;
023
024import net.jcip.annotations.Immutable;
025
026
027/**
028 * Issuer identifier.
029 *
030 * <p>Valid issuer identifiers are URIs with "https" schema and no query or
031 * fragment component.
032 */
033@Immutable
034public final class Issuer extends Identifier {
035
036
037        /**
038         * Checks if the specified string represents a valid issuer identifier.
039         * This method is {@code null}-safe.
040         *
041         * @param value The issuer string.
042         *
043         * @return {@code true} if the string represents a valid issuer
044         *         identifier, else {@code false}.
045         */
046        public static boolean isValid(final String value) {
047
048                if (value == null)
049                        return false;
050
051                try {
052                        return isValid(new URI(value));
053
054                } catch (URISyntaxException e) {
055
056                        return false;
057                }
058        }
059
060
061        /**
062         * Checks if the specified issuer is a valid identifier. This method is
063         * {@code null}-safe.
064         *
065         * @param value The issuer.
066         *
067         * @return {@code true} if the value is a valid identifier, else
068         *         {@code false}.
069         */
070        public static boolean isValid(final Issuer value) {
071
072                if (value == null)
073                        return false;
074
075                try {
076                        return isValid(new URI(value.getValue()));
077
078                } catch (URISyntaxException e) {
079
080                        return false;
081                }
082        }
083
084
085        /**
086         * Checks if the specified URI represents a valid issuer identifier.
087         * This method is {@code null}-safe.
088         *
089         * @param value The URI.
090         *
091         * @return {@code true} if the values represents a valid issuer
092         *         identifier, else {@code false}.
093         */
094        public static boolean isValid(final URI value) {
095
096                if (value == null)
097                        return false;
098
099                if (value.getScheme() == null || ! value.getScheme().equalsIgnoreCase("https"))
100                        return false;
101
102                if (value.getRawQuery() != null)
103                        return false;
104
105                return value.getRawFragment() == null;
106
107        }
108
109
110        /**
111         * Creates a new issuer identifier with the specified value.
112         *
113         * @param value The issuer identifier value. Must not be {@code null}
114         *              or empty string.
115         */
116        public Issuer(final String value) {
117
118                super(value);
119        }
120
121
122        /**
123         * Creates a new issuer identifier with the specified URI value.
124         *
125         * @param value The URI value. Must not be {@code null}.
126         */
127        public Issuer(final URI value) {
128
129                super(value.toString());
130        }
131
132
133        /**
134         * Creates a new issuer identifier with the specified value.
135         *
136         * @param value The value. Must not be {@code null}.
137         */
138        public Issuer(final Identifier value) {
139
140                super(value.getValue());
141        }
142
143
144        /**
145         * Checks if this issuer is a valid identifier. This method is
146         * {@code null}-safe.
147         *
148         * @return {@code true} if the value is a valid identifier, else
149         *         {@code false}.
150         */
151        public boolean isValid() {
152
153                return Issuer.isValid(this);
154        }
155
156
157        @Override
158        public boolean equals(final Object object) {
159        
160                return object instanceof Issuer && this.toString().equals(object.toString());
161        }
162}