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