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