View Javadoc

1   /*
2    * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.saml2.metadata.impl;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import javax.xml.namespace.QName;
24  
25  import org.joda.time.DateTime;
26  import org.opensaml.common.impl.AbstractSignableSAMLObject;
27  import org.opensaml.saml2.common.Extensions;
28  import org.opensaml.saml2.metadata.AdditionalMetadataLocation;
29  import org.opensaml.saml2.metadata.AffiliationDescriptor;
30  import org.opensaml.saml2.metadata.AttributeAuthorityDescriptor;
31  import org.opensaml.saml2.metadata.AuthnAuthorityDescriptor;
32  import org.opensaml.saml2.metadata.ContactPerson;
33  import org.opensaml.saml2.metadata.EntityDescriptor;
34  import org.opensaml.saml2.metadata.IDPSSODescriptor;
35  import org.opensaml.saml2.metadata.Organization;
36  import org.opensaml.saml2.metadata.PDPDescriptor;
37  import org.opensaml.saml2.metadata.RoleDescriptor;
38  import org.opensaml.saml2.metadata.SPSSODescriptor;
39  import org.opensaml.xml.XMLObject;
40  import org.opensaml.xml.util.AttributeMap;
41  import org.opensaml.xml.util.IndexedXMLObjectChildrenList;
42  import org.opensaml.xml.util.XMLObjectChildrenList;
43  
44  /**
45   * Concretate implementation of {@link org.opensaml.saml2.metadata.EntitiesDescriptor}.
46   */
47  public class EntityDescriptorImpl extends AbstractSignableSAMLObject implements EntityDescriptor {
48  
49      /** Entity ID of this Entity. */
50      private String entityID;
51  
52      /** ID attribute. */
53      private String id;
54  
55      /** validUntil attribute. */
56      private DateTime validUntil;
57  
58      /** cacheDurection attribute. */
59      private Long cacheDuration;
60  
61      /** Extensions child. */
62      private Extensions extensions;
63  
64      /** Role descriptors for this entity. */
65      private final IndexedXMLObjectChildrenList<RoleDescriptor> roleDescriptors;
66  
67      /** Affiliatition descriptor for this entity. */
68      private AffiliationDescriptor affiliationDescriptor;
69  
70      /** Organization the administers this entity. */
71      private Organization organization;
72  
73      /** Contact persons for this entity. */
74      private final XMLObjectChildrenList<ContactPerson> contactPersons;
75  
76      /** Additional metadata locations for this entity. */
77      private final XMLObjectChildrenList<AdditionalMetadataLocation> additionalMetadata;
78  
79      /** "anyAttribute" attributes. */
80      private final AttributeMap unknownAttributes;
81  
82      /**
83       * Constructor.
84       * 
85       * @param namespaceURI the namespace the element is in
86       * @param elementLocalName the local name of the XML element this Object represents
87       * @param namespacePrefix the prefix for the given namespace
88       */
89      protected EntityDescriptorImpl(String namespaceURI, String elementLocalName, String namespacePrefix) {
90          super(namespaceURI, elementLocalName, namespacePrefix);
91          roleDescriptors = new IndexedXMLObjectChildrenList<RoleDescriptor>(this);
92          contactPersons = new XMLObjectChildrenList<ContactPerson>(this);
93          additionalMetadata = new XMLObjectChildrenList<AdditionalMetadataLocation>(this);
94          unknownAttributes = new AttributeMap(this);
95      }
96  
97      /** {@inheritDoc} */
98      public String getEntityID() {
99          return entityID;
100     }
101 
102     /** {@inheritDoc} */
103     public void setEntityID(String newId) {
104         if (newId != null && newId.length() > 1024) {
105             throw new IllegalArgumentException("Entity ID can not exceed 1024 characters in length");
106         }
107         entityID = prepareForAssignment(entityID, newId);
108     }
109 
110     /** {@inheritDoc} */
111     public String getID() {
112         return id;
113     }
114 
115     /** {@inheritDoc} */
116     public void setID(String newID) {
117         String oldID = this.id;
118         this.id = prepareForAssignment(this.id, newID);
119         registerOwnID(oldID, this.id);
120     }
121 
122     /** {@inheritDoc} */
123     public boolean isValid() {
124         if (null == validUntil) {
125             return true;
126         }
127 
128         DateTime now = new DateTime();
129         return now.isBefore(validUntil);
130     }
131 
132     /** {@inheritDoc} */
133     public DateTime getValidUntil() {
134         return validUntil;
135     }
136 
137     /** {@inheritDoc} */
138     public void setValidUntil(DateTime newValidUntil) {
139         validUntil = prepareForAssignment(validUntil, newValidUntil);
140     }
141 
142     /** {@inheritDoc} */
143     public Long getCacheDuration() {
144         return cacheDuration;
145     }
146 
147     /** {@inheritDoc} */
148     public void setCacheDuration(Long duration) {
149         cacheDuration = prepareForAssignment(cacheDuration, duration);
150     }
151 
152     /** {@inheritDoc} */
153     public Extensions getExtensions() {
154         return extensions;
155     }
156 
157     /** {@inheritDoc} */
158     public void setExtensions(Extensions newExtensions) {
159         extensions = prepareForAssignment(extensions, newExtensions);
160     }
161 
162     /** {@inheritDoc} */
163     public List<RoleDescriptor> getRoleDescriptors() {
164         return roleDescriptors;
165     }
166 
167     /** {@inheritDoc} */
168     public List<RoleDescriptor> getRoleDescriptors(QName typeOrName) {
169         return (List<RoleDescriptor>) roleDescriptors.subList(typeOrName);
170     }
171 
172     /** {@inheritDoc} */
173     public List<RoleDescriptor> getRoleDescriptors(QName type, String supportedProtocol) {
174         ArrayList<RoleDescriptor> supportingRoleDescriptors = new ArrayList<RoleDescriptor>();
175         for (RoleDescriptor descriptor : roleDescriptors.subList(type)) {
176             if (descriptor.isSupportedProtocol(supportedProtocol)) {
177                 supportingRoleDescriptors.add(descriptor);
178             }
179         }
180 
181         return supportingRoleDescriptors;
182     }
183 
184     /** {@inheritDoc} */
185     public IDPSSODescriptor getIDPSSODescriptor(String supportedProtocol) {
186         List<RoleDescriptor> descriptors = getRoleDescriptors(IDPSSODescriptor.DEFAULT_ELEMENT_NAME, supportedProtocol);
187         if (descriptors.size() > 0) {
188             return (IDPSSODescriptor) descriptors.get(0);
189         }
190 
191         return null;
192     }
193 
194     /** {@inheritDoc} */
195     public SPSSODescriptor getSPSSODescriptor(String supportedProtocol) {
196         List<RoleDescriptor> descriptors = getRoleDescriptors(SPSSODescriptor.DEFAULT_ELEMENT_NAME, supportedProtocol);
197         if (descriptors.size() > 0) {
198             return (SPSSODescriptor) descriptors.get(0);
199         }
200 
201         return null;
202     }
203 
204     /** {@inheritDoc} */
205     public AuthnAuthorityDescriptor getAuthnAuthorityDescriptor(String supportedProtocol) {
206         List<RoleDescriptor> descriptors = getRoleDescriptors(AuthnAuthorityDescriptor.DEFAULT_ELEMENT_NAME,
207                 supportedProtocol);
208         if (descriptors.size() > 0) {
209             return (AuthnAuthorityDescriptor) descriptors.get(0);
210         }
211 
212         return null;
213     }
214 
215     /** {@inheritDoc} */
216     public AttributeAuthorityDescriptor getAttributeAuthorityDescriptor(String supportedProtocol) {
217         List<RoleDescriptor> descriptors = getRoleDescriptors(AttributeAuthorityDescriptor.DEFAULT_ELEMENT_NAME,
218                 supportedProtocol);
219         if (descriptors.size() > 0) {
220             return (AttributeAuthorityDescriptor) descriptors.get(0);
221         }
222 
223         return null;
224     }
225 
226     /** {@inheritDoc} */
227     public PDPDescriptor getPDPDescriptor(String supportedProtocol) {
228         List<RoleDescriptor> descriptors = getRoleDescriptors(PDPDescriptor.DEFAULT_ELEMENT_NAME, supportedProtocol);
229         if (descriptors.size() > 0) {
230             return (PDPDescriptor) descriptors.get(0);
231         }
232 
233         return null;
234     }
235 
236     /** {@inheritDoc} */
237     public AffiliationDescriptor getAffiliationDescriptor() {
238         return affiliationDescriptor;
239     }
240 
241     /** {@inheritDoc} */
242     public void setAffiliationDescriptor(AffiliationDescriptor descriptor) {
243         affiliationDescriptor = prepareForAssignment(affiliationDescriptor, descriptor);
244     }
245 
246     /** {@inheritDoc} */
247     public Organization getOrganization() {
248         return organization;
249     }
250 
251     /** {@inheritDoc} */
252     public void setOrganization(Organization newOrganization) {
253         organization = prepareForAssignment(organization, newOrganization);
254     }
255 
256     /** {@inheritDoc} */
257     public List<ContactPerson> getContactPersons() {
258         return contactPersons;
259     }
260 
261     /** {@inheritDoc} */
262     public List<AdditionalMetadataLocation> getAdditionalMetadataLocations() {
263         return additionalMetadata;
264     }
265 
266     /**
267      * {@inheritDoc}
268      */
269     public AttributeMap getUnknownAttributes() {
270         return unknownAttributes;
271     }
272 
273     /** {@inheritDoc} */
274     public String getSignatureReferenceID() {
275         return id;
276     }
277 
278     /** {@inheritDoc} */
279     public List<XMLObject> getOrderedChildren() {
280         ArrayList<XMLObject> children = new ArrayList<XMLObject>();
281 
282         if (getSignature() != null) {
283             children.add(getSignature());
284         }
285         children.add(getExtensions());
286         children.addAll(roleDescriptors);
287         children.add(getAffiliationDescriptor());
288         children.add(getOrganization());
289         children.addAll(contactPersons);
290         children.addAll(additionalMetadata);
291 
292         return Collections.unmodifiableList(children);
293     }
294 }