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