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.federation.entities;
019
020
021import java.net.URI;
022import java.util.List;
023
024import net.minidev.json.JSONObject;
025
026import com.nimbusds.oauth2.sdk.ParseException;
027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
028
029
030/**
031 * Federation entity metadata.
032 *
033 * <p>Related specifications:
034 *
035 * <ul>
036 *     <li>OpenID Connect Federation 1.0, section 4.7.
037 * </ul>
038 */
039public class FederationEntityMetadata {
040        
041        
042        /**
043         * The federation fetch endpoint.
044         */
045        private final URI federationFetchEndpoint;
046        
047        
048        /**
049         * The federation list endpoint.
050         */
051        private URI federationListEndpoint;
052        
053        
054        /**
055         * The federation resolve endpoint.
056         */
057        private URI federationResolveEndpoint;
058        
059        
060        /**
061         * The organisation name.
062         */
063        private String organizationName;
064        
065        
066        /**
067         * The contacts.
068         */
069        private List<String> contacts;
070        
071        
072        /**
073         * The policy URI.
074         */
075        private URI policyURI;
076        
077        
078        /**
079         * The homepage URI.
080         */
081        private URI homepageURI;
082        
083        
084        /**
085         * Creates a new federation entity metadata.
086         *
087         * @param federationFetchEndpoint The federation fetch endpoint,
088         *                                required for trust anchors and
089         *                                intermediate entities, optional for
090         *                                leaf entities.
091         */
092        public FederationEntityMetadata(final URI federationFetchEndpoint) {
093                this.federationFetchEndpoint = federationFetchEndpoint;
094        }
095        
096        
097        /**
098         * Gets the federation fetch endpoint. Corresponds to the
099         * {@code federation_fetch_endpoint} metadata field.
100         *
101         * @return The federation fetch endpoint, {@code null} if not
102         *         specified.
103         */
104        public URI getFederationFetchEndpointURI() {
105                return federationFetchEndpoint;
106        }
107        
108        
109        /**
110         * Gets the federation list endpoint. Corresponds to the
111         * {@code federation_list_endpoint} metadata field.
112         *
113         * @return The federation list endpoint, {@code null} if not specified.
114         */
115        public URI getFederationListEndpointURI() {
116                return federationListEndpoint;
117        }
118        
119        
120        /**
121         * Sets the federation list endpoint. Corresponds to the
122         * {@code federation_list_endpoint} metadata field.
123         *
124         * @param federationListEndpoint The federation list endpoint,
125         *                               {@code null} if not specified.
126         */
127        public void setFederationListEndpointURI(final URI federationListEndpoint) {
128                this.federationListEndpoint = federationListEndpoint;
129        }
130        
131        
132        /**
133         * Gets the federation resolve endpoint. Corresponds to the
134         * {@code federation_resolve_endpoint} metadata field.
135         *
136         * @return The federation resolve endpoint, {@code null} if not
137         *         specified.
138         */
139        public URI getFederationResolveEndpointURI() {
140                return federationResolveEndpoint;
141        }
142        
143        
144        /**
145         * Sets the federation resolve endpoint. Corresponds to the
146         * {@code federation_resolve_endpoint} metadata field.
147         *
148         * @param federationResolveEndpoint The federation resolve endpoint,
149         *                                  {@code null} if not specified.
150         */
151        public void setFederationResolveEndpointURI(final URI federationResolveEndpoint) {
152                this.federationResolveEndpoint = federationResolveEndpoint;
153        }
154        
155        
156        /**
157         * Gets the organisation name. Corresponds to the
158         * {@code organization_name} metadata field.
159         *
160         * @return The organisation name, {@code null} if not specified.
161         */
162        public String getOrganizationName() {
163                return organizationName;
164        }
165        
166        
167        /**
168         * Sets the organisation name. Corresponds to the
169         * {@code organization_name} metadata field.
170         *
171         * @param organizationName The organisation name, {@code null} if not
172         *                         specified.
173         */
174        public void setOrganizationName(final String organizationName) {
175                this.organizationName = organizationName;
176        }
177        
178        
179        /**
180         * Gets the entity contacts. Corresponds to the {@code contacts}
181         * metadata field.
182         *
183         * @return The contacts, such as names, e-mail addresses and phone
184         *         numbers, {@code null} if not specified.
185         */
186        public List<String> getContacts() {
187                return contacts;
188        }
189        
190        
191        /**
192         * Sets the entity contacts. Corresponds to the {@code contacts}
193         * metadata field.
194         *
195         * @param contacts The contacts, such as names, e-mail addresses and
196         *                 phone numbers, {@code null} if not specified.
197         */
198        public void setContacts(final List<String> contacts) {
199                this.contacts = contacts;
200        }
201        
202        
203        /**
204         * Gets the conditions and policies documentation URI. Corresponds to
205         * the {@code policy_uri} metadata field.
206         *
207         * @return The policy URI, {@code null} if not specified.
208         */
209        public URI getPolicyURI() {
210                return policyURI;
211        }
212        
213        
214        /**
215         * Sets the conditions and policies documentation URI. Corresponds to
216         * the {@code policy_uri} metadata field.
217         *
218         * @param policyURI The policy URI, {@code null} if not specified.
219         */
220        public void setPolicyURI(final URI policyURI) {
221                this.policyURI = policyURI;
222        }
223        
224        
225        /**
226         * Gets the homepage URI. Corresponds to the {@code homepage_uri}
227         * metadata field.
228         *
229         * @return The homepage URI, {@code null} if not specified.
230         */
231        public URI getHomepageURI() {
232                return homepageURI;
233        }
234        
235        
236        /**
237         * Sets the homepage URI. Corresponds to the {@code homepage_uri}
238         * metadata field.
239         *
240         * @param homepageURI The homepage URI, {@code null} if not specified.
241         */
242        public void setHomepageURI(final URI homepageURI) {
243                this.homepageURI = homepageURI;
244        }
245        
246        
247        /**
248         * Returns a JSON object representation of this federation entity
249         * metadata.
250         *
251         * <p>Example:
252         *
253         * <pre>
254         * {
255         *   "federation_fetch_endpoint" : "https://example.com/federation_fetch",
256         *   "federation_list_endpoint" : "https://example.com/federation_list",
257         *   "name"                     : "The example cooperation",
258         *   "homepage_uri"             : "https://www.example.com"
259         * }
260         * </pre>
261         *
262         * @return The JSON object.
263         */
264        public JSONObject toJSONObject() {
265                
266                JSONObject o = new JSONObject();
267                if (getFederationFetchEndpointURI() != null) {
268                        o.put("federation_fetch_endpoint", getFederationFetchEndpointURI().toString());
269                }
270                if (getFederationListEndpointURI() != null) {
271                        o.put("federation_list_endpoint", getFederationListEndpointURI().toString());
272                }
273                if (getFederationResolveEndpointURI() != null) {
274                        o.put("federation_resolve_endpoint", getFederationResolveEndpointURI().toString());
275                }
276                if (getOrganizationName() != null) {
277                        o.put("organization_name", getOrganizationName());
278                }
279                if (getContacts() != null) {
280                        o.put("contacts", getContacts());
281                }
282                if (getPolicyURI() != null) {
283                        o.put("policy_uri", getPolicyURI().toString());
284                }
285                if (getHomepageURI() != null) {
286                        o.put("homepage_uri", getHomepageURI().toString());
287                }
288                return o;
289        }
290        
291        
292        /**
293         * Parses a federation entity metadata from the specified a JSON
294         * object.
295         *
296         * <p>Example:
297         *
298         * <pre>
299         * {
300         *   "federation_fetch_endpoint" : "https://example.com/federation_fetch",
301         *   "federation_list_endpoint" : "https://example.com/federation_list",
302         *   "name"                     : "The example cooperation",
303         *   "homepage_uri"             : "https://www.example.com"
304         * }
305         * </pre>
306         *
307         * @param jsonObject The JSON object. Must not be {@code null}.
308         *
309         * @return The federation entity metadata.
310         *
311         * @throws ParseException If parsing failed.
312         */
313        public static FederationEntityMetadata parse(final JSONObject jsonObject)
314                throws ParseException {
315                
316                FederationEntityMetadata metadata = new FederationEntityMetadata(JSONObjectUtils.getURI(jsonObject, "federation_fetch_endpoint", null));
317                metadata.setFederationListEndpointURI(JSONObjectUtils.getURI(jsonObject, "federation_list_endpoint", null));
318                metadata.setFederationResolveEndpointURI(JSONObjectUtils.getURI(jsonObject, "federation_resolve_endpoint", null));
319                metadata.setOrganizationName(JSONObjectUtils.getString(jsonObject, "organization_name", null));
320                metadata.setContacts(JSONObjectUtils.getStringList(jsonObject, "contacts", null));
321                metadata.setPolicyURI(JSONObjectUtils.getURI(jsonObject, "policy_uri", null));
322                metadata.setHomepageURI(JSONObjectUtils.getURI(jsonObject, "homepage_uri", null));
323                return metadata;
324        }
325        
326        
327        /**
328         * Parses a federation entity metadata from the specified JSON object
329         * string.
330         *
331         * <p>Example:
332         *
333         * <pre>
334         * {
335         *   "federation_fetch_endpoint" : "https://example.com/federation_fetch",
336         *   "federation_list_endpoint" : "https://example.com/federation_list",
337         *   "name"                     : "The example cooperation",
338         *   "homepage_uri"             : "https://www.example.com"
339         * }
340         * </pre>
341         *
342         * @param json The JSON object string. Must not be {@code null}.
343         *
344         * @return The federation entity metadata.
345         *
346         * @throws ParseException If parsing failed.
347         */
348        public static FederationEntityMetadata parse(final String json)
349                throws ParseException {
350                
351                return parse(JSONObjectUtils.parse(json));
352        }
353}