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