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