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