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.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.oauth2.sdk.util.JSONObjectUtils;
029import com.nimbusds.openid.connect.sdk.assurance.claims.CountryCode;
030
031
032/**
033 * UserInfo address claims set, serialisable to a JSON object.
034 *
035 * <p>Related specifications:
036 *
037 * <ul>
038 *     <li>OpenID Connect Core 1.0, section 5.1.1.
039 *     <li>OpenID Connect for Identity Assurance 1.0, section 4.3.
040 * </ul>
041 */
042public class Address extends ClaimsSet {
043
044
045        /**
046         * The formatted claim name.
047         */
048        public static final String FORMATTED_CLAIM_NAME = "formatted";
049
050
051        /**
052         * The street address claim name.
053         */
054        public static final String STREET_ADDRESS_CLAIM_NAME = "street_address";
055
056
057        /**
058         * The locality claim name.
059         */
060        public static final String LOCALITY_CLAIM_NAME = "locality";
061
062
063        /**
064         * The region claim name.
065         */
066        public static final String REGION_CLAIM_NAME = "region";
067
068
069        /**
070         * The postal code claim name.
071         */
072        public static final String POSTAL_CODE_CLAIM_NAME = "postal_code";
073
074
075        /**
076         * The country claim name.
077         */
078        public static final String COUNTRY_CLAIM_NAME = "country";
079        
080        
081        /**
082         * The country code claim name.
083         */
084        public static final String COUNTRY_CODE_CLAIM_NAME = "country_code";
085
086
087        /**
088         * The names of the standard UserInfo address claims.
089         */
090        private static final Set<String> stdClaimNames = new LinkedHashSet<>();
091        
092        
093        static {
094                stdClaimNames.add(FORMATTED_CLAIM_NAME);
095                stdClaimNames.add(STREET_ADDRESS_CLAIM_NAME);
096                stdClaimNames.add(LOCALITY_CLAIM_NAME);
097                stdClaimNames.add(REGION_CLAIM_NAME);
098                stdClaimNames.add(POSTAL_CODE_CLAIM_NAME);
099                stdClaimNames.add(COUNTRY_CLAIM_NAME);
100                stdClaimNames.add(COUNTRY_CODE_CLAIM_NAME);
101        }
102        
103        
104        /**
105         * Gets the names of the standard UserInfo address claims.
106         *
107         * @return The names of the standard UserInfo address claims 
108         *         (read-only set).
109         */
110        public static Set<String> getStandardClaimNames() {
111        
112                return Collections.unmodifiableSet(stdClaimNames);
113        }
114        
115        
116        /**
117         * Creates a new minimal (empty) UserInfo address claims set.
118         */
119        public Address() { }
120
121
122        /**
123         * Creates a new UserInfo address claims set from the specified JSON 
124         * object.
125         *
126         * @param jsonObject The JSON object. Must not be {@code null}.
127         */
128        public Address(final JSONObject jsonObject) {
129
130                super(jsonObject);
131        }
132        
133        
134        /**
135         * Sets the full mailing address, formatted for display or use with a 
136         * mailing label. May contain newlines. Corresponds to the
137         * {@code formatted} claim.
138         *
139         * @param formatted The full mailing address. {@code null} if not 
140         *                  specified.
141         */
142        public void setFormatted(final String formatted) {
143        
144                setClaim(FORMATTED_CLAIM_NAME, formatted);
145        }
146        
147        
148        /**
149         * Gets the full mailing address, formatted for display or use with a
150         * mailing label. May contain newlines. Corresponds to the 
151         * {@code formatted} claim.
152         *
153         * @return The full mailing address, {@code null} if not specified.
154         */
155        public String getFormatted() {
156        
157                return getStringClaim(FORMATTED_CLAIM_NAME);
158        }
159        
160        
161        /**
162         * Sets the full street address component, which may include house
163         * number, street name, PO BOX, and multi-line extended street address
164         * information. May contain newlines. Corresponds to the 
165         * {@code street_address} claim.
166         *
167         * @param streetAddress The full street address component. If
168         *                      {@code null} the claim will be removed.
169         */
170        public void setStreetAddress(final String streetAddress) {
171        
172                setClaim(STREET_ADDRESS_CLAIM_NAME, streetAddress);
173        }
174        
175        
176        /**
177         * Gets the full street address component, which may include house 
178         * number, street name, PO BOX, and multi-line extended street address 
179         * information. May contain newlines. Corresponds to the 
180         * {@code street_address} claim.
181         *
182         * @return The full street address component, {@code null} if not 
183         *         specified.
184         */
185        public String getStreetAddress() {
186        
187                return getStringClaim(STREET_ADDRESS_CLAIM_NAME);
188        }
189        
190        
191        /**
192         * Sets the city or locality component. Corresponds to the 
193         * {@code locality} claim.
194         *
195         * @param locality The city or locality component. If {@code null} the
196         *                 claim will be removed.
197         */
198        public void setLocality(final String locality) {
199        
200                setClaim(LOCALITY_CLAIM_NAME, locality);
201        }
202        
203        
204        /**
205         * Gets the city or locality component. Corresponds to the 
206         * {@code locality} claim, with no language tag.
207         *
208         * @return The city or locality component, {@code null} if not 
209         *         specified.
210         */
211        public String getLocality() {
212        
213                return getStringClaim(LOCALITY_CLAIM_NAME);
214        }
215        
216        
217        /**
218         * Sets the state, province, prefecture or region component. 
219         * Corresponds to the {@code region} claim.
220         *
221         * @param region The state, province, prefecture or region component.
222         *               If {@code null} the claim will be removed.
223         */
224        public void setRegion(final String region) {
225        
226                setClaim(REGION_CLAIM_NAME, region);
227        }
228        
229        
230        /**
231         * Gets the state, province, prefecture or region component. 
232         * Corresponds to the {@code region} claim.
233         *
234         * @return The state, province, prefecture or region component,
235         *         {@code null} if not specified.
236         */
237        public String getRegion() {
238        
239                return getStringClaim(REGION_CLAIM_NAME);
240        }
241        
242        
243        /**
244         * Sets the zip code or postal code component. Corresponds to the
245         * {@code postal_code} claim.
246         *
247         * @param postalCode The zip code or postal code component. If 
248         *                   {@code null} the claim will be removed.
249         */
250        public void setPostalCode(final String postalCode) {
251        
252                setClaim(POSTAL_CODE_CLAIM_NAME, postalCode);
253        }
254        
255        
256        /**
257         * Gets the zip code or postal code component. Corresponds to the
258         * {@code postal_code} claim.
259         *
260         * @return The zip code or postal code component, {@code null} if not 
261         *         specified.
262         */
263        public String getPostalCode() {
264        
265                return getStringClaim(POSTAL_CODE_CLAIM_NAME);
266        }
267        
268        
269        /**
270         * Sets the country name component. Corresponds to the {@code country} 
271         * claim.
272         *
273         * @param country The country name component. If {@code null} the claim
274         *                will be removed.
275         */
276        public void setCountry(final String country) {
277        
278                setClaim(COUNTRY_CLAIM_NAME, country);
279        }
280        
281        
282        /**
283         * Gets the country name component. Corresponds to the {@code country}
284         * claim.
285         *
286         * @return The country name component, {@code null} if not specified.
287         */
288        public String getCountry() {
289        
290                return getStringClaim(COUNTRY_CLAIM_NAME);
291        }
292        
293        
294        /**
295         * Sets the country code component. Corresponds to the
296         * {@code country_code} claim.
297         *
298         * @param countryCode The country code component. If {@code null} the
299         *                    claim will be removed.
300         */
301        public void setCountryCode(final CountryCode countryCode) {
302        
303                String value = countryCode != null ? countryCode.getValue() : null;
304                setClaim(COUNTRY_CODE_CLAIM_NAME, value);
305        }
306        
307        
308        /**
309         * Gets the country code component. Corresponds to the
310         * {@code country_code} claim.
311         *
312         * @return The country code component, {@code null} if not specified.
313         */
314        public CountryCode getCountryCode() {
315        
316                String value = getStringClaim(COUNTRY_CODE_CLAIM_NAME);
317                
318                if (value == null) {
319                        return null;
320                }
321                
322                try {
323                        return CountryCode.parse(value);
324                } catch (ParseException e) {
325                        return null;
326                }
327        }
328
329
330        /**
331         * Parses an address claims set from the specified JSON object string.
332         *
333         * @param json The JSON object string to parse. Must not be
334         *             {@code null}.
335         *
336         * @return The address claims set.
337         *
338         * @throws ParseException If parsing failed.
339         */
340        public static Address parse(final String json)
341                throws ParseException {
342
343                JSONObject jsonObject = JSONObjectUtils.parse(json);
344
345                try {
346                        return new Address(jsonObject);
347
348                } catch (IllegalArgumentException e) {
349
350                        throw new ParseException(e.getMessage(), e);
351                }
352        }
353}