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