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.assurance.claims;
019
020
021import java.util.Collections;
022import java.util.LinkedHashSet;
023import java.util.Set;
024
025import net.minidev.json.JSONObject;
026
027import com.nimbusds.oauth2.sdk.ParseException;
028import com.nimbusds.openid.connect.sdk.claims.ClaimsSet;
029
030
031/**
032 * Birthplace claims set, serialisable to a JSON object.
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *     <li>OpenID Connect for Identity Assurance 1.0, section 3.1.
038 * </ul>
039 */
040// TODO In anticipation of https://bitbucket.org/openid/connect/issues/1119/place_of_birth-birthplace
041public final class Birthplace extends ClaimsSet {
042        
043        
044        /**
045         * The country claim name.
046         */
047        public static final String COUNTRY_CLAIM_NAME = "country";
048        
049        
050        /**
051         * The region claim name.
052         */
053        public static final String REGION_CLAIM_NAME = "region";
054        
055        
056        /**
057         * The locality claim name.
058         */
059        public static final String LOCALITY_CLAIM_NAME = "locality";
060        
061        
062        /**
063         * The names of the standard place of birth claims.
064         */
065        private static final Set<String> stdClaimNames = new LinkedHashSet<>();
066        
067        
068        static {
069                stdClaimNames.add(LOCALITY_CLAIM_NAME);
070                stdClaimNames.add(REGION_CLAIM_NAME);
071                stdClaimNames.add(COUNTRY_CLAIM_NAME);
072        }
073        
074        
075        /**
076         * Gets the names of the standard birthplace claims.
077         *
078         * @return The names of the standard birthplace claims (read-only set).
079         */
080        public static Set<String> getStandardClaimNames() {
081                
082                return Collections.unmodifiableSet(stdClaimNames);
083        }
084        
085        
086        /**
087         * Creates a new birthplace claims set.
088         *
089         * @param country  The country, as ISO3166-1 Alpha-2 or ISO3166-3 code,
090         *                 {@code null} if not specified.
091         * @param region   State, province, prefecture, or region component,
092         *                 {@code null} if not specified.
093         * @param locality City or other locality, {@code null} if not
094         *                 specified.       .
095         */
096        public Birthplace(final CountryCode country, final String region, final String locality) {
097        
098                if (country != null) {
099                        setClaim(COUNTRY_CLAIM_NAME, country.getValue());
100                }
101                
102                setClaim(REGION_CLAIM_NAME, region);
103                setClaim(LOCALITY_CLAIM_NAME, locality);
104        }
105        
106        
107        /**
108         * Creates a new birthplace claims set from the specified JSON object.
109         *
110         * @param jsonObject The JSON object. Must not be {@code null}.
111         */
112        public Birthplace(final JSONObject jsonObject) {
113                
114                super(jsonObject);
115        }
116        
117        
118        /**
119         * Gets the country.
120         *
121         * @return The country, {@code null} if not specified or illegal
122         *         ISO 3166-1 alpha-2 (two-letter) country code.
123         */
124        public CountryCode getCountry() {
125        
126                String code = getStringClaim(COUNTRY_CLAIM_NAME);
127                if (code == null) {
128                        return null;
129                }
130                
131                try {
132                        return ISO3166_1Alpha2CountryCode.parse(code);
133                } catch (ParseException e) {
134                        return null;
135                }
136        }
137        
138        
139        /**
140         * Sets the country.
141         *
142         * @param country The country, {@code null} if not specified.
143         */
144        public void setCountry(final CountryCode country) {
145        
146                if (country != null) {
147                        setClaim(COUNTRY_CLAIM_NAME, country.getValue());
148                } else {
149                        setClaim(COUNTRY_CLAIM_NAME, null);
150                }
151        }
152        
153        
154        /**
155         * Gets the tate, province, prefecture, or region component.
156         *
157         * @return The state, province, prefecture, or region component,
158         *         {@code null} if not specified.
159         */
160        public String getRegion() {
161                
162                return getStringClaim(REGION_CLAIM_NAME);
163        }
164        
165        
166        /**
167         * Sets the tate, province, prefecture, or region component.
168         *
169         * @param region The state, province, prefecture, or region component,
170         *               {@code null} if not specified.
171         */
172        public void setRegion(final String region) {
173                
174                setClaim(REGION_CLAIM_NAME, region);
175        }
176        
177        
178        /**
179         * Gets the city or other locality.
180         *
181         * @return The city or other locality, {@code null} if not specified.
182         */
183        public String getLocality() {
184                
185                return getStringClaim(LOCALITY_CLAIM_NAME);
186        }
187        
188        
189        /**
190         * Sets the city or other locality.
191         *
192         * @param locality The city or other locality, {@code null} if not
193         *                 specified.
194         */
195        public void setLocality(final String locality) {
196                
197                setClaim(LOCALITY_CLAIM_NAME, locality);
198        }
199}